diff --git a/README.md b/README.md index 1e16747..1c4ee16 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,7 @@ Format | Read | Write | PNG | ✅ | ✅ | JPEG | ✅ | | BMP | ✅ | ✅ | +GIF | ✅ | | SVG | ✅ | | ### Joins and caps diff --git a/src/pixie.nim b/src/pixie.nim index 74a956e..7fc8a6c 100644 --- a/src/pixie.nim +++ b/src/pixie.nim @@ -1,12 +1,12 @@ 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 export blends, bumpy, chroma, common, images, masks, paints, paths, vmath type FileFormat* = enum - ffPng, ffBmp, ffJpg + ffPng, ffBmp, ffJpg, ffGif proc decodeImage*(data: string | seq[uint8]): Image = ## Loads an image from a memory. @@ -19,6 +19,8 @@ proc decodeImage*(data: string | seq[uint8]): Image = elif data.len > 5 and (data.readStr(0, 5) == xmlSignature or data.readStr(0, 4) == svgSignature): decodeSvg(data) + elif data.len > 6 and data.readStr(0, 6) in gifSignatures: + decodeGif(data) else: raise newException(PixieError, "Unsupported image file format") @@ -35,6 +37,8 @@ proc encodeImage*(image: Image, fileFormat: FileFormat): string = image.encodeJpg() of ffBmp: image.encodeBmp() + of ffGif: + raise newException(PixieError, "Unsupported image format") proc writeFile*(image: Image, filePath: string, fileFormat: FileFormat) = ## Writes an image to a file. diff --git a/tests/test_gif.nim b/tests/test_gif.nim index b3d9316..c6cbb80 100644 --- a/tests/test_gif.nim +++ b/tests/test_gif.nim @@ -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")) 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")) writeFile("tests/images/gif/sunflower.png", img3.encodePng()) + +var img4 = readImage("tests/images/gif/sunflower.gif") +doAssert img3.data == img4.data