dymokomi/luce-png
Source of release 0.10.1
0.10.1 / src / png / tests.lucb
#==============================================================================================
#
# png/tests - The module's tests
#
#==============================================================================================
test "an RGBA round trip is exact":
var pixels: u8[4000]
for i in 0..<pixels.length:
pixels[i] = (u8)((i * 7 + i // 40) % 256)
var out = Buffer()
defer out.close()
try encode_rgba8(pixels, 25, 40, &out)
var back: u8[4000]
try decode_rgba8(out.view(), back)
for i in 0..<pixels.length:
assert(back[i] == pixels[i])
test "16-bit samples, streamed rows and the Raster route round-trip":
var words: u16[1200]
for i in 0..<words.length:
words[i] = (u16)((i * 2654435761) >> 7)
var out = Buffer()
defer out.close()
var encoder = try Encoder.begin(20, 15, .rgba16, &out, EncodeOptions(level = 9, band_rows = 4))
defer encoder.close()
var y: usize = 0
while y < 15:
let rows: usize = 4 if y + 4 <= 15 else 15 - y
try encoder.write_rows16(words[y * 80..<(y + rows) * 80], rows)
y += rows
try encoder.finish()
let found = try info(out.view())
assert(found.width == 20 and found.height == 15 and found.bits == 16 and found.color == 6 and found.alpha)
var back: u16[1200]
try decode_rgba16(out.view(), back)
for i in 0..<words.length:
assert(back[i] == words[i])
# The same picture as a Raster: its samples come back as they went in.
var raster = Raster()
defer raster.close()
raster.encoded = out.data[..<out.used]
try probe(&raster)
try raster.allocate()
try decode(&raster)
for i in 0..<words.length:
assert(raster.pixels[i] == (f64)words[i])
var again = Buffer()
defer again.close()
try encode(&raster, &again, EncodeOptions(filter = 4))
var empty: u8[]
raster.encoded = empty
var third: u16[1200]
try decode_rgba16(again.view(), third)
for i in 0..<words.length:
assert(third[i] == words[i])
test "gray formats expand to RGBA":
var gray: u8[18]
for i in 0..<gray.length:
gray[i] = (u8)(i * 14)
var out = Buffer()
defer out.close()
var encoder = try Encoder.begin(3, 3, .gray_alpha8, &out)
defer encoder.close()
try encoder.write_rows(gray, 3)
try encoder.finish()
var back: u8[36]
try decode_rgba8(out.view(), back)
for p in 0..<(usize)9:
assert(back[p * 4] == gray[p * 2] and back[p * 4 + 1] == gray[p * 2] and back[p * 4 + 2] == gray[p * 2] and back[p * 4 + 3] == gray[p * 2 + 1])
test "the encoder refuses bad requests":
var out = Buffer()
defer out.close()
var pixels: u8[64]
var refused = false
encode_rgba8(pixels, 4, 4, &out, EncodeOptions(level = 10)) catch failure:
refused = failure.code == invalid
recover ()
assert(refused)
var encoder = try Encoder.begin(4, 4, .rgba8, &out)
defer encoder.close()
try encoder.write_rows(pixels[..<32], 2)
refused = false
encoder.finish() catch failure:
refused = failure.code == invalid
recover ()
assert(refused)
refused = false
encoder.write_rows(pixels, 4) catch failure:
refused = failure.code == invalid
recover ()
assert(refused)
refused = false
var words: u16[4]
encoder.write_rows16(words, 1) catch failure:
refused = failure.code == invalid
recover ()
assert(refused)
## Rows a sink was handed, kept in one picture, checking they come in order.
struct Collected:
var pixels: u8[]
var next: usize
var calls: usize
func collect(context: void*?, first: usize, count: usize, bytes: const u8[]) -> !:
let into = (Collected*)(context else return)
if first != into.next:
error(invalid, "rows out of order")
let width = bytes.length // count
memory.copy(into.pixels[first * width..], bytes, bytes.length)
into.next = first + count
into.calls += 1
test "a PNG from another encoder streams its rows through an inflate window":
# RGB rows, every filter in turn, deflated whole and split over IDAT chunks as
# other encoders write them: the general path, not the banded one.
let width: usize = 1200
let height: usize = 400
let stride = width * 3 + 1
let raw = try new u8[stride * height] --- in memory.heap
defer free(raw) in memory.heap
for y in 0..<height:
raw[y * stride] = (u8)(y % 5)
for x in 1..<stride:
raw[y * stride + x] = (u8)((x * 7 + y * 13 + (x * y) // 97) % 256)
let packed = try new u8[raw.length + 4096] --- in memory.heap
defer free(packed) in memory.heap
let used = try flate.deflate_zlib(raw, 6, packed)
var out = Buffer()
defer out.close()
try out.put(signature)
var header: u8[13]
put32(&header[0], (u32)width)
put32(&header[4], (u32)height)
header[8] = 8
header[9] = 2
try chunk(&out, "IHDR", header)
let third = used // 3
try chunk(&out, "IDAT", packed[..<third])
try chunk(&out, "IDAT", packed[third..<2 * third])
try chunk(&out, "IDAT", packed[2 * third..<used])
try chunk(&out, "IEND", b"")
let whole = try new u8[width * height * 4] --- in memory.heap
defer free(whole) in memory.heap
try decode_rgba8(out.view(), whole)
var rows = Collected(pixels = try new u8[width * height * 4] --- in memory.heap, next = 0, calls = 0)
defer free(rows.pixels) in memory.heap
try decode_rows(out.view(), collect, (void*)&rows)
assert(rows.next == height and rows.calls == (height + 63) // 64)
for i in 0..<whole.length:
assert(rows.pixels[i] == whole[i])
# A damaged filter byte fails the streamed decode too.
raw[5 * stride] = 9
let damaged = try flate.deflate_zlib(raw, 6, packed)
var bad = Buffer()
defer bad.close()
try bad.put(signature)
try chunk(&bad, "IHDR", header)
try chunk(&bad, "IDAT", packed[..<damaged])
try chunk(&bad, "IEND", b"")
rows.next = 0
var failed = false
decode_rows(bad.view(), collect, (void*)&rows) catch failure:
failed = failure.code == corrupt
recover ()
assert(failed)
## A file in memory read through `ReadAt`, counting the most read at once.
struct Held:
var data: const u8[]
var reads: usize
var largest: usize
func read_held(context: void*?, at: u64, into: u8[]) -> usize!:
let held = (Held*)(context else return 0)
if at >= (u64)held.data.length:
return 0
let count = into.length if (usize)at + into.length <= held.data.length else held.data.length - (usize)at
memory.copy(into, held.data[(usize)at..], count)
held.reads += 1
held.largest = count if count > held.largest else held.largest
return count
## `data` decoded through `decode_rows_at` as `decode_rows` decodes it; whether the rows agree.
func same_rows_at(data: const u8[], width: usize, height: usize, deep: bool) -> bool!:
let pixel: usize = 8 if deep else 4
var expected = Collected(pixels = try new u8[width * height * pixel] --- in memory.heap, next = 0, calls = 0)
defer free(expected.pixels) in memory.heap
try decode_rows(data, collect, (void*)&expected, deep)
var rows = Collected(pixels = try new u8[width * height * pixel] --- in memory.heap, next = 0, calls = 0)
defer free(rows.pixels) in memory.heap
var held = Held(data = data, reads = 0, largest = 0)
try decode_rows_at(read_held, (void*)&held, (u64)data.length, collect, (void*)&rows, deep)
if rows.next != height:
return false
for i in 0..<expected.pixels.length:
if rows.pixels[i] != expected.pixels[i]:
return false
return true
test "a file read in pieces decodes as the file whole, its banded and general paths":
# Noise over 2 MB of IDAT data, in chunks of 7000 bytes as other encoders
# write them, so pieces cross the reads' blocks and the chunks' ends.
let width: usize = 900
let height: usize = 700
let stride = width * 4 + 1
let raw = try new u8[stride * height] --- in memory.heap
defer free(raw) in memory.heap
var seed: u32 = 12345
for y in 0..<height:
raw[y * stride] = (u8)(y % 5)
for x in 1..<stride:
seed = seed *% 1103515245 +% 12345
raw[y * stride + x] = (u8)(seed >> 24)
let packed = try new u8[raw.length + 65536] --- in memory.heap
defer free(packed) in memory.heap
let used = try flate.deflate_zlib(raw, 1, packed)
var out = Buffer()
defer out.close()
try out.put(signature)
var header: u8[13]
put32(&header[0], (u32)width)
put32(&header[4], (u32)height)
header[8] = 8
header[9] = 6
try chunk(&out, "IHDR", header)
try chunk(&out, "tEXt", b"Comment\x00in pieces")
var at: usize = 0
while at < used:
let end = at + 7000 if at + 7000 < used else used
try chunk(&out, "IDAT", packed[at..<end])
at = end
try chunk(&out, "IEND", b"")
assert(try same_rows_at(out.view(), width, height, false))
assert(try same_rows_at(out.view(), width, height, true))
# This encoder's bands, decoded a group at a time on every processor.
let pixels = try new u8[width * height * 4] --- in memory.heap
defer free(pixels) in memory.heap
for i in 0..<pixels.length:
pixels[i] = raw[(i // (width * 4)) * stride + 1 + i % (width * 4)]
var banded = Buffer()
defer banded.close()
try encode_rgba8(pixels, width, height, &banded, EncodeOptions(level = 1, band_rows = 16))
assert(try same_rows_at(banded.view(), width, height, false))
# A damaged IDAT chunk, one in the middle, fails on its CRC.
let damaged = try new u8[out.view().length] --- in memory.heap
defer free(damaged) in memory.heap
memory.copy(damaged, out.view(), damaged.length)
damaged[damaged.length // 2] ^= 1
var rows = Collected(pixels = try new u8[width * height * 4] --- in memory.heap, next = 0, calls = 0)
defer free(rows.pixels) in memory.heap
var held = Held(data = damaged, reads = 0, largest = 0)
var failed = false
decode_rows_at(read_held, (void*)&held, (u64)damaged.length, collect, (void*)&rows) catch failure:
failed = failure.code == corrupt
recover ()
assert(failed)
# A file cut short fails too, and every cut is a clean failure.
for cut in [(usize)7, 40, damaged.length // 3, damaged.length - 5]:
var short = Held(data = out.view()[..<cut], reads = 0, largest = 0)
rows.next = 0
failed = false
decode_rows_at(read_held, (void*)&short, (u64)cut, collect, (void*)&rows) catch failure:
failed = true
recover ()
assert(failed)
# The reads stay a block at most.
rows.next = 0
held = Held(data = out.view(), reads = 0, largest = 0)
try decode_rows_at(read_held, (void*)&held, (u64)out.view().length, collect, (void*)&rows)
assert(held.largest <= feed_block and held.reads > 2)
test "the start of a file up to its first IDAT probes as the file whole":
# A palette with transparency, so the probe's slices come from the start.
var out = Buffer()
defer out.close()
try out.put(signature)
var header: u8[13]
put32(&header[0], 4)
put32(&header[4], 2)
header[8] = 8
header[9] = 3
try chunk(&out, "IHDR", header)
try chunk(&out, "PLTE", b"\x00\x00\x00\xff\x00\x00")
try chunk(&out, "tRNS", b"\x80")
let raw: u8[10] = [0, 0, 1, 0, 1, 0, 1, 1, 0, 0]
var packed: u8[1024]
let used = try flate.deflate_zlib(raw, 6, packed)
try chunk(&out, "IDAT", packed[..<used])
try chunk(&out, "IEND", b"")
let data = out.view()
var have: usize = 1
while true:
let need = try head_bytes(data[..<have])
if need == 0:
break
assert(need > have and need <= data.length)
have = need
var whole = Raster()
defer whole.close()
whole.encoded = try new u8[data.length] --- in memory.heap
memory.copy(whole.encoded, data, data.length)
try probe(&whole)
var head = Raster()
defer head.close()
head.encoded = try new u8[have] --- in memory.heap
memory.copy(head.encoded, data, have)
try probe_head(&head)
assert(head.width == 4 and head.height == 2 and head.channels.length == whole.channels.length and head.photo == 3)
assert((str)head.palette == (str)whole.palette and (str)head.transparency == (str)whole.transparency)
# mark: Color chunks ==========================================================================
from luce_std import files
import c as libc
## The bytes of tests/fixtures/color/NAME, kept in the heap for the run (tests run from the
## repository root).
func color_fixture(name: str) -> const u8[]:
var buffer: u8[256]
let path = strings.format(buffer, f"tests/fixtures/color/{name}") else trap("a fixture path is too long")
with memory.heap:
return (files.read((libc.str)path) else trap("cannot read a color fixture")).value
test "color chunks are read as libpng reads them":
let both = try info(color_fixture("cicp-iccp.png"))
let cicp = both.colorimetry.cicp else trap("cICP is read")
assert(cicp.color_primaries == 12 and cicp.transfer_function == 13 and cicp.matrix_coefficients == 0 and cicp.video_full_range == 1)
assert(both.colorimetry.has_icc and (str)both.colorimetry.icc_name == "also")
let profile = with_profile(&both)
assert((str)profile == (str)color_fixture("cicp-iccp.icc"))
let tagged = try info(color_fixture("gama-chrm-srgb.png"))
assert(tagged.colorimetry.srgb_intent == 1 and tagged.colorimetry.gamma == 45455 and not tagged.colorimetry.has_icc)
let white = tagged.colorimetry.chromaticities else trap("cHRM is read")
assert(white.white_x == 31270 and white.blue_y == 6000)
# Out of range, out of place, too short or repeated chunks are ignored.
let ignored = try info(color_fixture("color-invalid.png"))
assert(ignored.colorimetry.srgb_intent == none and ignored.colorimetry.gamma == none and ignored.colorimetry.cicp == none)
let late = try info(color_fixture("iccp-after-plte.png"))
assert(not late.colorimetry.has_icc and late.colorimetry.gamma == none)
let small = try info(color_fixture("gray-iccp.png"))
assert(not small.colorimetry.has_icc)
let two = try info(color_fixture("two-iccp.png"))
assert((str)with_profile(&two) == (str)color_fixture("two-iccp.icc"))
test "an ICC profile libpng refuses is none":
let names: str[6] = ["rgb-gray-profile.png", "gray-rgb-profile.png", "iccp-abstract.png", "iccp-truncated.png", "iccp-no-keyword.png", "wpt-cicp-and-iccp.png"]
for name in names:
let found = try info(color_fixture(name))
var profile: u8[]? = none
with memory.heap:
profile = try icc_profile(&found)
assert(profile == none)
# libpng stops at the declared length: trailing data, and a damaged Adler-32, are kept.
let longer = try info(color_fixture("iccp-extra-data.png"))
assert((str)with_profile(&longer) == (str)color_fixture("iccp-extra-data.icc"))
let swap = try info(color_fixture("wpt-swap-990000-iccp.png"))
assert((str)with_profile(&swap) == (str)color_fixture("wpt-swap-990000-iccp.icc"))
## The profile of `found`, kept in the heap for the run.
func with_profile(found: const Info*) -> const u8[]:
with memory.heap:
let profile = icc_profile(found) else trap("the profile inflates")
return profile else trap("there is a profile")
## An ICC profile libpng accepts: a v4 RGB monitor header, no tags, and varied
## bytes after it, so it stays larger than libpng's shortest iCCP chunk compressed.
func tiny_profile(out: u8[]):
for at in 0..<out.length:
out[at] = (u8)((at * 131 + 7) % 251) if at >= 132 else 0
out[2] = (u8)(out.length >> 8)
out[3] = (u8)out.length
out[8] = 4
out[9] = 0x30
for (at, byte) in b"mntrRGB XYZ ".indexed():
out[12 + at] = byte
for (at, byte) in b"acsp".indexed():
out[36 + at] = byte
# The D50 illuminant, s15Fixed16: 0.9642, 1.0, 0.8249.
let illuminant: u8[12] = [0, 0, 0xF6, 0xD6, 0, 1, 0, 0, 0, 0, 0xD3, 0x2D]
for at in 0..<(usize)12:
out[68 + at] = illuminant[at]
test "an embedded ICC profile reads back":
var profile: u8[1024]
tiny_profile(profile)
var pixels: u8[64]
var output = Buffer()
defer output.close()
try encode_rgba8(pixels, 4, 4, &output, EncodeOptions(icc_profile = profile))
let found = try info(output.data[..<output.used])
let read = (try icc_profile(&found)) else trap("the iCCP profile did not read back")
defer free(read)
assert(read.length == 1024)
for at in 0..<(usize)1024:
assert(read[at] == profile[at])