Merge pull request #277 from guzba/master

easy encode raw png
This commit is contained in:
treeform 2021-09-02 10:36:42 -07:00 committed by GitHub
commit bd27f65b25
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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: