Add GIF file format.

This commit is contained in:
treeform 2021-03-01 08:53:15 -08:00
parent d248457b0b
commit c8269d5db9
3 changed files with 11 additions and 3 deletions

View file

@ -28,6 +28,7 @@ Format | Read | Write |
PNG | ✅ | ✅ | PNG | ✅ | ✅ |
JPEG | ✅ | | JPEG | ✅ | |
BMP | ✅ | ✅ | BMP | ✅ | ✅ |
GIF | ✅ | |
SVG | ✅ | | SVG | ✅ | |
### Joins and caps ### Joins and caps

View file

@ -1,12 +1,12 @@
import bumpy, chroma, flatty/binny, os, pixie/blends, pixie/common, import bumpy, chroma, flatty/binny, os, pixie/blends, pixie/common,
pixie/fileformats/bmp, pixie/fileformats/jpg, pixie/fileformats/png, pixie/fileformats/bmp, pixie/fileformats/gif, pixie/fileformats/jpg, pixie/fileformats/png,
pixie/fileformats/svg, pixie/images, pixie/masks, pixie/paints, pixie/paths, vmath pixie/fileformats/svg, pixie/images, pixie/masks, pixie/paints, pixie/paths, vmath
export blends, bumpy, chroma, common, images, masks, paints, paths, vmath export blends, bumpy, chroma, common, images, masks, paints, paths, vmath
type type
FileFormat* = enum FileFormat* = enum
ffPng, ffBmp, ffJpg ffPng, ffBmp, ffJpg, ffGif
proc decodeImage*(data: string | seq[uint8]): Image = proc decodeImage*(data: string | seq[uint8]): Image =
## Loads an image from a memory. ## Loads an image from a memory.
@ -19,6 +19,8 @@ proc decodeImage*(data: string | seq[uint8]): Image =
elif data.len > 5 and elif data.len > 5 and
(data.readStr(0, 5) == xmlSignature or data.readStr(0, 4) == svgSignature): (data.readStr(0, 5) == xmlSignature or data.readStr(0, 4) == svgSignature):
decodeSvg(data) decodeSvg(data)
elif data.len > 6 and data.readStr(0, 6) in gifSignatures:
decodeGif(data)
else: else:
raise newException(PixieError, "Unsupported image file format") raise newException(PixieError, "Unsupported image file format")
@ -35,6 +37,8 @@ proc encodeImage*(image: Image, fileFormat: FileFormat): string =
image.encodeJpg() image.encodeJpg()
of ffBmp: of ffBmp:
image.encodeBmp() image.encodeBmp()
of ffGif:
raise newException(PixieError, "Unsupported image format")
proc writeFile*(image: Image, filePath: string, fileFormat: FileFormat) = proc writeFile*(image: Image, filePath: string, fileFormat: FileFormat) =
## Writes an image to a file. ## Writes an image to a file.

View file

@ -1,4 +1,4 @@
import pixie/fileformats/gif, pixie/fileformats/png import pixie/fileformats/gif, pixie/fileformats/png, pixie
var img = decodeGIF(readFile("tests/images/gif/3x5.gif")) var img = decodeGIF(readFile("tests/images/gif/3x5.gif"))
writeFile("tests/images/gif/3x5.png", img.encodePng()) writeFile("tests/images/gif/3x5.png", img.encodePng())
@ -8,3 +8,6 @@ writeFile("tests/images/gif/audrey.png", img2.encodePng())
var img3 = decodeGIF(readFile("tests/images/gif/sunflower.gif")) var img3 = decodeGIF(readFile("tests/images/gif/sunflower.gif"))
writeFile("tests/images/gif/sunflower.png", img3.encodePng()) writeFile("tests/images/gif/sunflower.png", img3.encodePng())
var img4 = readImage("tests/images/gif/sunflower.gif")
doAssert img3.data == img4.data