Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@ bindings/generated
dump.txt
tests/fileformats/jpeg/generated
tests/fileformats/jpeg/diffs
tests/fileformats/webp/generated
tests/fileformats/webp/diffs
*.dylib
tmp/
15 changes: 13 additions & 2 deletions src/pixie.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import
std/[os, strutils],
bumpy, chroma, flatty/binny, vmath,
pixie/[common, contexts, fonts, imagebase64, images, internal, paints, paths],
pixie/fileformats/[bmp, gif, jpeg, png, ppm, qoi, svg]
pixie/fileformats/[bmp, gif, jpeg, png, ppm, qoi, svg, webp]

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

type
FileFormat* = enum
PngFormat, BmpFormat, JpegFormat, GifFormat, QoiFormat, PpmFormat
PngFormat, BmpFormat, JpegFormat, GifFormat, QoiFormat, PpmFormat,
WebpFormat

converter autoStraightAlpha*(c: ColorRGBX): ColorRGBA {.inline, raises: [].} =
## Convert a premultiplied alpha RGBA to a straight alpha RGBA.
Expand Down Expand Up @@ -41,6 +42,10 @@ proc decodeImageDimensions*(
equalMem(data, ppmSignatures[1].cstring, 2)
):
decodePpmDimensions(data, len)
elif len > 12 and
equalMem(data, WebpRiffSignature.cstring, 4) and
equalMem(cast[pointer](cast[uint](data) + 8), WebpSignature.cstring, 4):
decodeWebpDimensions(data, len)
else:
raise newException(PixieError, "Unsupported image file format")

Expand All @@ -67,6 +72,9 @@ proc decodeImage*(data: string): Image {.raises: [PixieError].} =
decodeQoi(data).convertToImage()
elif data.len > 9 and data.readStr(0, 2) in ppmSignatures:
decodePpm(data)
elif data.len > 12 and data.readStr(0, 4) == WebpRiffSignature and
data.readStr(8, 4) == WebpSignature:
decodeWebp(data)
else:
raise newException(PixieError, "Unsupported image file format")

Expand Down Expand Up @@ -103,6 +111,8 @@ proc encodeImage*(
raise newException(PixieError, "Unsupported file format")
of PpmFormat:
image.encodePpm()
of WebpFormat:
raise newException(PixieError, "Unsupported file format")

proc writeFile*(image: Image, filePath: string) {.raises: [PixieError].} =
## Writes an image to a file.
Expand All @@ -112,6 +122,7 @@ proc writeFile*(image: Image, filePath: string) {.raises: [PixieError].} =
of ".jpg", ".jpeg": JpegFormat
of ".qoi": QoiFormat
of ".ppm": PpmFormat
of ".webp": WebpFormat
else:
raise newException(PixieError, "Unsupported file extension")

Expand Down
Loading
Loading