Skip to content

Commit bc268d5

Browse files
authored
Merge pull request #581 from treeform/webp-decoder
Add pure Nim WebP decoder
2 parents d557b42 + b01abd6 commit bc268d5

139 files changed

Lines changed: 3732 additions & 2 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,7 @@ bindings/generated
2525
dump.txt
2626
tests/fileformats/jpeg/generated
2727
tests/fileformats/jpeg/diffs
28+
tests/fileformats/webp/generated
29+
tests/fileformats/webp/diffs
2830
*.dylib
2931
tmp/

src/pixie.nim

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ import
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

77
export bumpy, chroma, common, contexts, fonts, imagebase64, images, paints,
88
paths, vmath
99

1010
type
1111
FileFormat* = enum
12-
PngFormat, BmpFormat, JpegFormat, GifFormat, QoiFormat, PpmFormat
12+
PngFormat, BmpFormat, JpegFormat, GifFormat, QoiFormat, PpmFormat,
13+
WebpFormat
1314

1415
converter 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

107117
proc 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

Comments
 (0)