From 5a691153a1405e2c6b12bf496ba5305be2d98385 Mon Sep 17 00:00:00 2001 From: Ryan Oldenburg Date: Wed, 1 Sep 2021 16:23:44 -0500 Subject: [PATCH] add lower level png decoding api --- src/pixie/fileformats/png.nim | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/src/pixie/fileformats/png.nim b/src/pixie/fileformats/png.nim index b7e4bca..a3a565c 100644 --- a/src/pixie/fileformats/png.nim +++ b/src/pixie/fileformats/png.nim @@ -14,6 +14,10 @@ type width, height: int bitDepth, colorType, compressionMethod, filterMethod, interlaceMethod: uint8 + Png* = ref object + width*, height*, channels*: int + data*: seq[ColorRGBA] + template failInvalid() = raise newException(PixieError, "Invalid PNG buffer, unable to load") @@ -316,9 +320,8 @@ proc decodeImageData( else: discard # Not possible, parseHeader validates -proc decodePng*(data: string): Image {.raises: [PixieError].} = - ## Decodes the PNG data into an Image. - +proc decodePngRaw*(data: string): Png {.raises: [PixieError].} = + ## Decodes the PNG data. if data.len < (8 + (8 + 13 + 4) + 4): # Magic bytes + IHDR + IEND failInvalid() @@ -420,11 +423,19 @@ proc decodePng*(data: string): Image {.raises: [PixieError].} = if prevChunkType != "IEND": failInvalid() - var pixels = decodeImageData(header, palette, transparency, imageData) - pixels.toPremultipliedAlpha() + result = Png() + result.width = header.width + result.height = header.height + result.channels = 4 + result.data = decodeImageData(header, palette, transparency, imageData) - result = newImage(header.width, header.height) - copyMem(result.data[0].addr, pixels[0].addr, pixels.len * 4) +proc decodePng*(data: string): Image {.raises: [PixieError].} = + ## Decodes the PNG data into an Image. + let png = decodePngRaw(data) + png.data.toPremultipliedAlpha() + + result = newImage(png.width, png.height) + copyMem(result.data[0].addr, png.data[0].addr, png.data.len * 4) proc encodePng*( width, height, channels: int, data: pointer, len: int