easy encode raw png
This commit is contained in:
parent
67729bc270
commit
592b242139
1 changed files with 9 additions and 5 deletions
|
@ -320,6 +320,11 @@ proc decodeImageData(
|
||||||
else:
|
else:
|
||||||
discard # Not possible, parseHeader validates
|
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].} =
|
proc decodePngRaw*(data: string): Png {.raises: [PixieError].} =
|
||||||
## Decodes the PNG data.
|
## Decodes the PNG data.
|
||||||
if data.len < (8 + (8 + 13 + 4) + 4): # Magic bytes + IHDR + IEND
|
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].} =
|
proc decodePng*(data: string): Image {.raises: [PixieError].} =
|
||||||
## Decodes the PNG data into an Image.
|
## Decodes the PNG data into an Image.
|
||||||
let png = decodePngRaw(data)
|
newImage(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*(
|
proc encodePng*(
|
||||||
width, height, channels: int, data: pointer, len: int
|
width, height, channels: int, data: pointer, len: int
|
||||||
|
@ -518,6 +519,9 @@ proc encodePng*(
|
||||||
result.add("IEND")
|
result.add("IEND")
|
||||||
result.addUint32(crc32(result[result.len - 4 ..< result.len]).swap())
|
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].} =
|
proc encodePng*(image: Image): string {.raises: [PixieError].} =
|
||||||
## Encodes the image data into the PNG file format.
|
## Encodes the image data into the PNG file format.
|
||||||
if image.data.len == 0:
|
if image.data.len == 0:
|
||||||
|
|
Loading…
Reference in a new issue