dymokomi/luce-color
Source of release 0.8.0
0.8.0 / tests / icc / parse.lucb
#==============================================================================================
#
# tests/icc/parse - reading profiles: tags, curves, lookup tables and refusals
#
# DESCRIPTION:
# What each fixture holds is checked in full against skcms by tests/run.py (the
# icc_tool dump against expected.txt); these tests read the parts by name.
#
#==============================================================================================
test "a matrix/TRC profile reads its tags":
let p = test_profile("para-types.icc")
assert(p.has_trc and p.has_to_xyzd50 and not p.has_a2b and not p.has_b2a and p.has_cicp)
assert(p.data_color_space == signature_rgb and p.pcs == signature_xyz and p.input_channel_count() == 3)
# para function type 0 is a pure gamma, type 3 has c and d, type 4 is all seven.
assert(near(p.trc[0].parametric.g, 2.2, 0.0001) and p.trc[0].parametric.a == 1.0)
assert(near(p.trc[1].parametric.d, 0.04045, 0.00002) and p.trc[1].parametric.e == 0.0)
assert(near(p.trc[2].parametric.e, 0.001, 0.00002) and near(p.trc[2].parametric.f, 0.002, 0.00002))
# Types 1 and 2 get d = -b/a, and type 2's c is both e and f.
let types12 = test_profile("para-types-12.icc")
let g = types12.trc[1].parametric
assert(near(g.d, 0.1 / 1.1, 0.00002) and g.e == g.f and near(g.e, 0.05, 0.00002))
assert(p.cicp.color_primaries == 12 and p.cicp.transfer_characteristics == 13 and p.cicp.video_full_range_flag == 1)
let chad = p.chad() else trap("the profile has a chad")
assert(near(chad.vals[0][0], 1.0479, 0.00002))
let white = p.white_point() else trap("the profile has a wtpt")
assert(near(white[0], 0.9642, 0.00002) and white[1] == 1.0)
let tag = p.tag_by_index(0) else trap("the profile has tags")
assert(tag.signature == 0x7258595A and tag.kind == signature_xyz and tag.size == 20)
assert(p.tag_by_index(p.tag_count) == none and p.tag_by_signature(0x41324230) == none)
test "lookup tables read their curves, grids and matrices":
let lab = test_profile("mft2-lab.icc")
assert(lab.has_a2b and lab.has_b2a and lab.pcs == signature_lab and not lab.has_trc)
assert(lab.a2b.input_channels == 3 and lab.a2b.output_channels == 3 and lab.a2b.matrix_channels == 0)
assert(lab.a2b.grid_points[0] == 9 and lab.a2b.grid_16.length == 9 * 9 * 9 * 6)
# Identity output tables become the identity transfer function.
assert(lab.a2b.output_curves[0].table_entries == 0 and lab.a2b.input_curves[0].table_entries == 256)
let cmyk = test_profile("mft1-cmyk.icc")
assert(cmyk.data_color_space == signature_cmyk and cmyk.input_channel_count() == 4)
assert(cmyk.a2b.input_channels == 4 and cmyk.a2b.grid_8.length == 81 * 3 and cmyk.b2a.output_channels == 4)
let mab = test_profile("mab-xyz.icc")
assert(mab.a2b.input_channels == 3 and mab.a2b.matrix_channels == 3 and mab.a2b.grid_8.length == 125 * 3)
# The matrix of an XYZ-PCS lookup is scaled by 65535 / 32768.
assert(near(mab.a2b.matrix.vals[0][0], 0.436065674, 0.0001) and near(mab.a2b.matrix.vals[1][3], 0.001 * 65535.0 / 32768.0, 0.0001))
assert(mab.b2a.matrix_channels == 3 and mab.b2a.output_channels == 3 and mab.b2a.grid_16.length == 27 * 6)
test "tables and gray profiles read":
let gray = test_profile("gray-ktrc.icc")
assert(gray.data_color_space == signature_gray and gray.input_channel_count() == 1 and gray.has_to_xyzd50)
assert(near(gray.to_xyzd50.vals[0][0], 0.9642, 0.00002) and gray.to_xyzd50.vals[0][1] == 0.0)
let gamma = test_profile("curv-gamma.icc")
# One curv entry is a gamma in 8.8 fixed point; none is the identity.
assert(gamma.trc[0].parametric.g == 461.0 / 256.0 and gamma.trc[1].parametric.g == 1.0)
let identity = test_profile("identity-tables.icc")
assert(identity.trc[0].table_entries == 256)
test "malformed and unusable profiles are refused":
let names: str[10] = ["bad-signature.icc", "bad-illuminant.icc", "bad-version.icc", "bad-tag-past-end.icc", "bad-para.icc", "bad-para-negative-d.icc", "bad-clut.icc", "oss-fuzz-testcase-57426.icc", "oss-fuzz-testcase-59551.icc", "oss-fuzz-testcase-60281.icc"]
for name in names:
assert(parse(test_profile_bytes(name)) == none)
var empty: const u8[]
assert(parse(empty) == none)
# Truncated anywhere, a good profile is refused or read without reading past its bytes.
let data = test_profile_bytes("mab-xyz.icc")
for length in 0..<data.length:
_ = parse(data[..<length])