From 592b242139c4ccfa530ae2c7d42509da5c45ad7c Mon Sep 17 00:00:00 2001 From: Ryan Oldenburg Date: Wed, 1 Sep 2021 23:43:05 -0500 Subject: [PATCH] easy encode raw png --- src/pixie/fileformats/png.nim | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/pixie/fileformats/png.nim b/src/pixie/fileformats/png.nim index a3a565c..cfd65a2 100644 --- a/src/pixie/fileformats/png.nim +++ b/src/pixie/fileformats/png.nim @@ -320,6 +320,11 @@ proc decodeImageData( else: discard # Not possible, parseHeader validates +proc newImage*(png: Png): Image {.raises: [PixieError].} = + result = newImage(png.width, png.height) + copyMem(result.data[0].addr, png.data[0].addr, png.data.len * 4) + result.data.toPremultipliedAlpha() + proc decodePngRaw*(data: string): Png {.raises: [PixieError].} = ## Decodes the PNG data. if data.len < (8 + (8 + 13 + 4) + 4): # Magic bytes + IHDR + IEND @@ -431,11 +436,7 @@ proc decodePngRaw*(data: string): Png {.raises: [PixieError].} = 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) + newImage(decodePngRaw(data)) proc encodePng*( width, height, channels: int, data: pointer, len: int @@ -518,6 +519,9 @@ proc encodePng*( result.add("IEND") result.addUint32(crc32(result[result.len - 4 ..< result.len]).swap()) +proc encodePng*(png: Png): string {.raises: [PixieError].} = + encodePng(png.width, png.height, 4, png.data[0].addr, png.data.len * 4) + proc encodePng*(image: Image): string {.raises: [PixieError].} = ## Encodes the image data into the PNG file format. if image.data.len == 0: