22 std/ [os, strutils],
33 bumpy, chroma, flatty/ binny, vmath,
44 pixie/ [common, contexts, fonts, imagebase64, images, internal, paints, paths],
5- pixie/ fileformats/ [bmp, gif, jpeg, png, ppm, qoi, svg]
5+ pixie/ fileformats/ [bmp, gif, jpeg, png, ppm, qoi, svg, webp ]
66
77export bumpy, chroma, common, contexts, fonts, imagebase64, images, paints,
88 paths, vmath
99
1010type
1111 FileFormat * = enum
12- PngFormat , BmpFormat , JpegFormat , GifFormat , QoiFormat , PpmFormat
12+ PngFormat , BmpFormat , JpegFormat , GifFormat , QoiFormat , PpmFormat ,
13+ WebpFormat
1314
1415converter autoStraightAlpha * (c: ColorRGBX ): ColorRGBA {.inline , raises : [].} =
1516 # # Convert a premultiplied alpha RGBA to a straight alpha RGBA.
@@ -41,6 +42,10 @@ proc decodeImageDimensions*(
4142 equalMem (data, ppmSignatures[1 ].cstring , 2 )
4243 ):
4344 decodePpmDimensions (data, len)
45+ elif len > 12 and
46+ equalMem (data, WebpRiffSignature .cstring , 4 ) and
47+ equalMem (cast [pointer ](cast [uint ](data) + 8 ), WebpSignature .cstring , 4 ):
48+ decodeWebpDimensions (data, len)
4449 else :
4550 raise newException (PixieError , " Unsupported image file format" )
4651
@@ -67,6 +72,9 @@ proc decodeImage*(data: string): Image {.raises: [PixieError].} =
6772 decodeQoi (data).convertToImage ()
6873 elif data.len > 9 and data.readStr (0 , 2 ) in ppmSignatures:
6974 decodePpm (data)
75+ elif data.len > 12 and data.readStr (0 , 4 ) == WebpRiffSignature and
76+ data.readStr (8 , 4 ) == WebpSignature :
77+ decodeWebp (data)
7078 else :
7179 raise newException (PixieError , " Unsupported image file format" )
7280
@@ -103,6 +111,8 @@ proc encodeImage*(
103111 raise newException (PixieError , " Unsupported file format" )
104112 of PpmFormat :
105113 image.encodePpm ()
114+ of WebpFormat :
115+ raise newException (PixieError , " Unsupported file format" )
106116
107117proc writeFile * (image: Image , filePath: string ) {.raises : [PixieError ].} =
108118 # # Writes an image to a file.
@@ -112,6 +122,7 @@ proc writeFile*(image: Image, filePath: string) {.raises: [PixieError].} =
112122 of " .jpg" , " .jpeg" : JpegFormat
113123 of " .qoi" : QoiFormat
114124 of " .ppm" : PpmFormat
125+ of " .webp" : WebpFormat
115126 else :
116127 raise newException (PixieError , " Unsupported file extension" )
117128
0 commit comments