zippy 0.8.1
This commit is contained in:
parent
054424f78c
commit
d65fd68161
3 changed files with 31 additions and 12 deletions
|
@ -5,10 +5,10 @@ license = "MIT"
|
||||||
|
|
||||||
srcDir = "src"
|
srcDir = "src"
|
||||||
|
|
||||||
requires "nim >= 1.4.0"
|
requires "nim >= 1.4.8"
|
||||||
requires "vmath >= 1.1.0"
|
requires "vmath >= 1.1.0"
|
||||||
requires "chroma >= 0.2.5"
|
requires "chroma >= 0.2.5"
|
||||||
requires "zippy >= 0.7.4"
|
requires "zippy >= 0.8.1"
|
||||||
requires "flatty >= 0.2.2"
|
requires "flatty >= 0.2.2"
|
||||||
requires "nimsimd >= 1.0.0"
|
requires "nimsimd >= 1.0.0"
|
||||||
requires "bumpy >= 1.0.3"
|
requires "bumpy >= 1.0.3"
|
||||||
|
|
|
@ -101,7 +101,10 @@ proc decodeGif*(data: string): Image {.raises: [PixieError].} =
|
||||||
|
|
||||||
# Turn full lzw data into bit stream.
|
# Turn full lzw data into bit stream.
|
||||||
var
|
var
|
||||||
bs = initBitStream(lzwData)
|
bs = BitStreamReader(
|
||||||
|
src: cast[ptr UncheckedArray[uint8]](lzwData[0].addr),
|
||||||
|
len: lzwData.len
|
||||||
|
)
|
||||||
bitSize = lzwMinBitSize + 1
|
bitSize = lzwMinBitSize + 1
|
||||||
currentCodeTableMax = (1 shl (bitSize)) - 1
|
currentCodeTableMax = (1 shl (bitSize)) - 1
|
||||||
codeLast: int = -1
|
codeLast: int = -1
|
||||||
|
@ -110,10 +113,10 @@ proc decodeGif*(data: string): Image {.raises: [PixieError].} =
|
||||||
|
|
||||||
# Main decode loop.
|
# Main decode loop.
|
||||||
while codeLast != endCode:
|
while codeLast != endCode:
|
||||||
if bs.pos + bitSize.int > bs.data.len * 8: failInvalid()
|
if bs.pos + bitSize.int > bs.len * 8: failInvalid()
|
||||||
var
|
var
|
||||||
# Read variable bits out of the table.
|
# Read variable bits out of the table.
|
||||||
codeId = bs.readBits(bitSize).int
|
codeId = bs.readBits(bitSize.int).int
|
||||||
# Some time we need to carry over table information.
|
# Some time we need to carry over table information.
|
||||||
carryOver: seq[int]
|
carryOver: seq[int]
|
||||||
|
|
||||||
|
|
|
@ -165,14 +165,31 @@ proc unfilter(
|
||||||
discard # Not possible, parseHeader validates
|
discard # Not possible, parseHeader validates
|
||||||
|
|
||||||
proc decodeImageData(
|
proc decodeImageData(
|
||||||
|
data: string,
|
||||||
header: PngHeader,
|
header: PngHeader,
|
||||||
palette: seq[ColorRGB],
|
palette: seq[ColorRGB],
|
||||||
transparency, data: string
|
transparency: string,
|
||||||
|
idats: seq[(int, int)]
|
||||||
): seq[ColorRGBA] =
|
): seq[ColorRGBA] =
|
||||||
|
if idats.len == 0:
|
||||||
|
failInvalid()
|
||||||
|
|
||||||
result.setLen(header.width * header.height)
|
result.setLen(header.width * header.height)
|
||||||
|
|
||||||
let
|
let
|
||||||
uncompressed = try: uncompress(data) except ZippyError: failInvalid()
|
uncompressed =
|
||||||
|
if idats.len > 1:
|
||||||
|
var imageData: string
|
||||||
|
for (start, len) in idats:
|
||||||
|
let op = imageData.len
|
||||||
|
imageData.setLen(imageData.len + len)
|
||||||
|
copyMem(imageData[op].addr, data[start].unsafeAddr, len)
|
||||||
|
try: uncompress(imageData) except ZippyError: failInvalid()
|
||||||
|
else:
|
||||||
|
let
|
||||||
|
(start, len) = idats[0]
|
||||||
|
p = data[start].unsafeAddr
|
||||||
|
try: uncompress(p, len) except ZippyError: failInvalid()
|
||||||
valuesPerPixel =
|
valuesPerPixel =
|
||||||
case header.colorType:
|
case header.colorType:
|
||||||
of 0: 1
|
of 0: 1
|
||||||
|
@ -340,7 +357,8 @@ proc decodePngRaw*(data: string): Png {.raises: [PixieError].} =
|
||||||
counts = ChunkCounts()
|
counts = ChunkCounts()
|
||||||
header: PngHeader
|
header: PngHeader
|
||||||
palette: seq[ColorRGB]
|
palette: seq[ColorRGB]
|
||||||
transparency, imageData: string
|
transparency: string
|
||||||
|
idats: seq[(int, int)]
|
||||||
prevChunkType: string
|
prevChunkType: string
|
||||||
|
|
||||||
# First chunk must be IHDR
|
# First chunk must be IHDR
|
||||||
|
@ -402,9 +420,7 @@ proc decodePngRaw*(data: string): Png {.raises: [PixieError].} =
|
||||||
failInvalid()
|
failInvalid()
|
||||||
if header.colorType == 3 and counts.PLTE == 0:
|
if header.colorType == 3 and counts.PLTE == 0:
|
||||||
failInvalid()
|
failInvalid()
|
||||||
let op = imageData.len
|
idats.add((pos, chunkLen))
|
||||||
imageData.setLen(imageData.len + chunkLen)
|
|
||||||
copyMem(imageData[op].addr, data[pos].unsafeAddr, chunkLen)
|
|
||||||
of "IEND":
|
of "IEND":
|
||||||
if chunkLen != 0:
|
if chunkLen != 0:
|
||||||
failInvalid()
|
failInvalid()
|
||||||
|
@ -432,7 +448,7 @@ proc decodePngRaw*(data: string): Png {.raises: [PixieError].} =
|
||||||
result.width = header.width
|
result.width = header.width
|
||||||
result.height = header.height
|
result.height = header.height
|
||||||
result.channels = 4
|
result.channels = 4
|
||||||
result.data = decodeImageData(header, palette, transparency, imageData)
|
result.data = decodeImageData(data, header, palette, transparency, idats)
|
||||||
|
|
||||||
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.
|
||||||
|
|
Loading…
Reference in a new issue