dymokomi/luce-png
Source of release 0.10.1
0.10.1 / src / png / chunks.lucb
#==============================================================================================
#
# png/chunks - Chunk parsing, the header, palette and transparency
#
#==============================================================================================
## Where an IDAT chunk's data is in the file.
struct Piece:
var start: usize
var length: usize
## A parsed PNG: its header, palette, transparency, the IDAT chunks in order, and
## the band layout from a `luPD` chunk (0 bands without one).
struct Parsed:
var info: Info
var palette: const u8[]
var transparency: const u8[]
var pieces: Piece[]
var count: usize
var bands: usize
var band_rows: usize
## A file walked in place (png/source): its palette and transparency, kept.
var kept: u8[]
func close():
if self.pieces.length > 0:
free(self.pieces) in memory.heap
var empty: Piece[]
self.pieces = empty
if self.kept.length > 0:
free(self.kept) in memory.heap
var none_kept: u8[]
self.kept = none_kept
## The IDAT data of chunk `index`.
func piece(data: const u8[], index: usize) -> const u8[]:
let p = self.pieces[index]
return data[p.start..<p.start + p.length]
## Every IDAT chunk's CRC, checked on up to `threads` threads.
func check_idat(data: const u8[], threads: usize) -> !:
var job = CrcJob(data = data, pieces = self.pieces[..<self.count], failed = false)
try run_tasks(crc_task, (void*)&job, self.count, threads)
if job.failed:
error(corrupt, "PNG CRC mismatch")
struct CrcJob:
var data: const u8[]
var pieces: const Piece[]
var failed: bool
func crc_task(context: void*, index: usize):
let job = (CrcJob*)context
if not chunk_crc_ok(job.data, job.pieces[index].start - 4, job.pieces[index].length):
job.failed = true
## Whether the chunk whose type starts at `at` with `length` data bytes has its CRC.
func chunk_crc_ok(data: const u8[], at: usize, length: usize) -> bool:
let end = at + 4 + length
let stored = ((u32)data[end] << 24) | ((u32)data[end + 1] << 16) | ((u32)data[end + 2] << 8) | (u32)data[end + 3]
return flate.crc32(data[at..<end]) == stored
## Walk the chunks, validating their order and contents; `check_idat` also checks
## the IDAT CRCs (decoding checks them in parallel otherwise).
func parse(data: const u8[], check_idat: bool) -> Parsed!:
var result = Parsed()
errdefer result.close()
var reader = Reader(data = data, little = false)
if (str)(try reader.take(8)) != (str)signature:
error(corrupt, "invalid PNG signature")
var walk = Walk()
while reader.position < reader.data.length:
let size = (usize)(try reader.uint(4))
let start = reader.position
let name = (str)(try reader.take(4))
let body = try reader.take(size)
_ = try reader.uint(4)
if name != "IDAT" or check_idat:
if not chunk_crc_ok(data, start, size):
error(corrupt, "PNG CRC mismatch")
try take_chunk(&result, &walk, name, body, start, size)
if walk.end:
if reader.position != reader.data.length:
error(corrupt, "invalid PNG IEND")
break
return try walk.finish(&result)
## The chunks before the first IDAT of a file's start: its header, palette and
## transparency, for a probe.
func parse_head(data: const u8[]) -> Parsed!:
var result = Parsed()
errdefer result.close()
var reader = Reader(data = data, little = false)
if (str)(try reader.take(8)) != (str)signature:
error(corrupt, "invalid PNG signature")
var walk = Walk()
while reader.position + 8 <= reader.data.length:
let size = (usize)(try reader.uint(4))
let start = reader.position
let name = (str)(try reader.take(4))
if name == "IDAT" or name == "IEND":
break
let body = try reader.take(size)
_ = try reader.uint(4)
if not chunk_crc_ok(data, start, size):
error(corrupt, "PNG CRC mismatch")
try take_chunk(&result, &walk, name, body, start, size)
if not walk.header:
error(corrupt, "PNG must start with IHDR")
result.info.alpha = result.info.color == 4 or result.info.color == 6 or result.transparency.length > 0
if not result.info.animated:
result.info.frame_count = 1
return result
## Where a walk through the chunks stands.
struct Walk:
var header: bool
var pixels: bool
var ended_pixels: bool
var end: bool
## The walk is over: it must have reached IEND.
func finish(result: Parsed*) -> Parsed!:
if not self.end:
error(corrupt, "PNG has no IEND")
result.info.alpha = result.info.color == 4 or result.info.color == 6 or result.transparency.length > 0
if not result.info.animated:
result.info.frame_count = 1
return *result
## One chunk, its type at `start` in the file and `size` bytes of data in
## `body` (unread, empty, for an IDAT read from a file): checked against the
## chunks before it and taken into `result`.
func take_chunk(result: Parsed*, walk: Walk*, name: str, body: const u8[], start: usize, size: usize) -> !:
if not walk.header and name != "IHDR":
error(corrupt, "PNG must start with IHDR")
if name == "IHDR":
if walk.header or size != 13:
error(corrupt, "invalid PNG IHDR")
var h = Reader(data = body, little = false)
result.info.width = (usize)(try h.uint(4))
result.info.height = (usize)(try h.uint(4))
result.info.bits = (usize)(try h.byte())
result.info.color = (usize)(try h.byte())
if try h.byte() != 0 or try h.byte() != 0:
error(unsupported, "unsupported PNG compression or filter method")
let interlace = try h.byte()
if interlace > 1:
error(corrupt, "invalid PNG interlace method")
result.info.interlaced = interlace == 1
let b = result.info.bits
let color = result.info.color
if not ((color == 0 and (b == 1 or b == 2 or b == 4 or b == 8 or b == 16)) or (color == 3 and (b == 1 or b == 2 or b == 4 or b == 8)) or ((color == 2 or color == 4 or color == 6) and (b == 8 or b == 16))):
error(corrupt, "invalid PNG color type or bit depth")
if result.info.width == 0 or result.info.height == 0 or result.info.width > 1000000 or result.info.height > 1000000:
error(invalid, "invalid dimensions or channel count")
walk.header = true
elif name == "PLTE":
if walk.pixels or result.palette.length != 0 or size == 0 or size > 768 or size % 3 != 0:
error(corrupt, "invalid PNG palette")
result.palette = body
elif name == "tRNS":
let color = result.info.color
if walk.pixels or result.transparency.length != 0 or (color == 0 and size != 2) or (color == 2 and size != 6) or (color == 3 and (size == 0 or size > result.palette.length // 3)) or color == 4 or color == 6:
error(corrupt, "invalid PNG transparency")
result.transparency = body
elif name == "IDAT":
if walk.ended_pixels or (result.info.color == 3 and result.palette.length == 0):
error(corrupt, "invalid PNG IDAT order")
walk.pixels = true
if result.count == result.pieces.length:
let grown = try new Piece[result.count * 2 + 16] --- in memory.heap
for i in 0..<result.count:
grown[i] = result.pieces[i]
if result.pieces.length > 0:
free(result.pieces) in memory.heap
result.pieces = grown
result.pieces[result.count] = Piece(start = start + 4, length = size)
result.count += 1
elif name == "IEND":
if size != 0 or not walk.pixels:
error(corrupt, "invalid PNG IEND")
walk.end = true
elif name == "luPD":
# This encoder's band layout: version, rows a band, bands.
if size == 9 and body[0] == 1:
result.band_rows = (usize)(((u32)body[1] << 24) | ((u32)body[2] << 16) | ((u32)body[3] << 8) | (u32)body[4])
result.bands = (usize)(((u32)body[5] << 24) | ((u32)body[6] << 16) | ((u32)body[7] << 8) | (u32)body[8])
elif name == "cICP" or name == "iCCP" or name == "sRGB" or name == "gAMA" or name == "cHRM":
take_color_chunk(result, walk, name, body)
elif name == "acTL":
try take_actl(result, walk, body)
elif (name.bytes[0] & 32) == 0:
error(unsupported, "unknown critical PNG chunk")
if walk.pixels and name != "IDAT":
walk.ended_pixels = true