add lower level png decoding api
This commit is contained in:
parent
e06e13b2ce
commit
5a691153a1
1 changed files with 18 additions and 7 deletions
|
@ -14,6 +14,10 @@ type
|
||||||
width, height: int
|
width, height: int
|
||||||
bitDepth, colorType, compressionMethod, filterMethod, interlaceMethod: uint8
|
bitDepth, colorType, compressionMethod, filterMethod, interlaceMethod: uint8
|
||||||
|
|
||||||
|
Png* = ref object
|
||||||
|
width*, height*, channels*: int
|
||||||
|
data*: seq[ColorRGBA]
|
||||||
|
|
||||||
template failInvalid() =
|
template failInvalid() =
|
||||||
raise newException(PixieError, "Invalid PNG buffer, unable to load")
|
raise newException(PixieError, "Invalid PNG buffer, unable to load")
|
||||||
|
|
||||||
|
@ -316,9 +320,8 @@ proc decodeImageData(
|
||||||
else:
|
else:
|
||||||
discard # Not possible, parseHeader validates
|
discard # Not possible, parseHeader validates
|
||||||
|
|
||||||
proc decodePng*(data: string): Image {.raises: [PixieError].} =
|
proc decodePngRaw*(data: string): Png {.raises: [PixieError].} =
|
||||||
## Decodes the PNG data into an Image.
|
## 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
|
||||||
failInvalid()
|
failInvalid()
|
||||||
|
|
||||||
|
@ -420,11 +423,19 @@ proc decodePng*(data: string): Image {.raises: [PixieError].} =
|
||||||
if prevChunkType != "IEND":
|
if prevChunkType != "IEND":
|
||||||
failInvalid()
|
failInvalid()
|
||||||
|
|
||||||
var pixels = decodeImageData(header, palette, transparency, imageData)
|
result = Png()
|
||||||
pixels.toPremultipliedAlpha()
|
result.width = header.width
|
||||||
|
result.height = header.height
|
||||||
|
result.channels = 4
|
||||||
|
result.data = decodeImageData(header, palette, transparency, imageData)
|
||||||
|
|
||||||
result = newImage(header.width, header.height)
|
proc decodePng*(data: string): Image {.raises: [PixieError].} =
|
||||||
copyMem(result.data[0].addr, pixels[0].addr, pixels.len * 4)
|
## 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*(
|
proc encodePng*(
|
||||||
width, height, channels: int, data: pointer, len: int
|
width, height, channels: int, data: pointer, len: int
|
||||||
|
|
Loading…
Reference in a new issue