Files
image/tests/png.sakura
T
2026-10-28 08:26:26 -05:00

141 lines
6.3 KiB
Plaintext

#! Contains the tests of `image::png`: the test images read, broken ones
#! refused, and images written and read back exactly, in the color type and
#! depth that hold them.
import std::testing;
import image;
# The bytes of a PNG that `IHDR` gives its depth and color type at.
const depth_at: u64 = 24;
const color_at: u64 = 25;
# The shifts of a big-endian word's bytes, the most significant first.
const byte_shifts: u32[4] = [24, 16, 8, 0];
# The CRC-32 of bytes, as PNG's chunks end with.
def crc32(data: strview): u32 {
var crc: u32 = 0xFFFFFFFF;
for byte in data {
crc ^= byte as u32;
for _ in 0..8 {
crc = (crc & 1) != 0 ? 0xEDB88320 ^ (crc >> 1) : crc >> 1;
}
}
return crc ^ 0xFFFFFFFF;
}
# Appends a PNG chunk, its length, type, data, and CRC.
def chunk(into: &mut string, kind: strview, data: strview): void {
val start := into.length;
for shift in byte_shifts {
into.push(((data.length as u32) >> shift) as u8);
}
into.append(kind);
into.append(data);
val crc := crc32(into.view()[start + 4:kind.length + data.length]);
for shift in byte_shifts {
into.push((crc >> shift) as u8);
}
}
# Writes an image as PNG, and whether it reads back exactly, in a color type
# and depth.
def round_trips(picture: &image::image, given: image::png::options, color: u8, depth: u8): bool {
val written := image::png::encode(picture, given) else (_) { return false; };
val again := read_back(written.view());
return apart(&again, picture) == 0 && written[color_at] == color && written[depth_at] == depth;
}
@test def reads_png_of_every_color_type(t: &mut tester): void {
t.expects(stray("tests/images/rgba8.png", "rgba") == 0);
t.expects(stray("tests/images/rgb8.png", "rgb") == 0);
t.expects(stray("tests/images/gray8.png", "gray") == 0);
t.expects(stray("tests/images/gray16.png", "gray") == 0);
t.expects(stray("tests/images/rgba16.png", "rgba") == 0);
t.expects(stray("tests/images/palette4.png", "palette") == 0);
t.expects(stray("tests/images/interlaced.png", "rgba") == 0);
}
@test def refuses_broken_png(t: &mut tester): void {
val whole := bytes_of("tests/images/rgb8.png");
t.expects(fails(image::decode(whole.view()[0:whole.length - 20]), image::image_error::malformed));
# A changed byte breaks its chunk's CRC.
var changed := string::copy_of(&whole);
changed[40] = changed[40] ^ 0x10;
t.expects(fails(image::decode(changed.view()), image::image_error::malformed));
# A header too large is refused before anything is inflated.
var huge := string::from("\x89PNG\r\n\x1A\n", nothing {});
chunk(&mut huge, "IHDR", "\x00\x01\x00\x00\x00\x01\x00\x00\x08\x06\x00\x00\x00");
t.expects(fails(image::decode(huge.view()), image::image_error::too_large));
# A critical chunk PNG doesn't define can't be read past; an ancillary one can.
var critical := string::from(whole.view()[0:33], nothing {});
chunk(&mut critical, "ABCD", "data");
critical.append(whole.view()[33:whole.length - 33]);
t.expects(fails(image::decode(critical.view()), image::image_error::unsupported));
var ancillary := string::from(whole.view()[0:33], nothing {});
chunk(&mut ancillary, "abCD", "data");
ancillary.append(whole.view()[33:whole.length - 33]);
t.expects(image::decode(ancillary.view()) is value);
}
@test def writes_png_in_the_least_color_type(t: &mut tester): void {
val given := image::png::options {};
# Gray of 1, 4, and 8 bits, gray with alpha, RGB, and RGBA.
t.expects(round_trips(&made_of("bw", 24, 20), given, 0, 1));
t.expects(round_trips(&made_of("gray4", 24, 20), given, 0, 4));
t.expects(round_trips(&made_of("gray", 24, 20), given, 0, 8));
t.expects(round_trips(&made_of("gray_alpha", 24, 20), given, 4, 8));
t.expects(round_trips(&made_of("rgb", 24, 20), given, 2, 8));
t.expects(round_trips(&made_of("noise", 24, 20), given, 6, 8));
# A palette, of as few bits as its colors need, its alpha in `tRNS`.
t.expects(round_trips(&made_of("palette", 24, 20), given, 3, 4));
t.expects(round_trips(&made_of("rgba", 12, 10), given, 3, 8));
t.expects(round_trips(&made_of("rgba", 24, 20), given, 6, 8));
# RGBA of 8 bits, where it's asked for whatever the pixels are.
t.expects(round_trips(&made_of("bw", 24, 20), image::png::options { reduced = false }, 6, 8));
}
@test def writes_png_at_every_level_and_interlaced(t: &mut tester): void {
for level in 0u..10u {
t.expects(round_trips(&made_of("rgb", 24, 20), image::png::options { level = level as u32 }, 2, 8));
}
# Each pass of Adam7, even those an image too small has none of.
t.expects(round_trips(&made_of("rgba", 24, 20), image::png::options { interlaced = true }, 6, 8));
t.expects(round_trips(&made_of("noise", 13, 11), image::png::options { interlaced = true }, 3, 8));
t.expects(round_trips(&made_of("noise", 19, 17), image::png::options { interlaced = true }, 6, 8));
t.expects(round_trips(&made_of("bw", 3, 2), image::png::options { interlaced = true }, 0, 1));
t.expects(round_trips(&made_of("noise", 1, 1), image::png::options { interlaced = true }, 3, 1));
}
@test def compresses_what_repeats(t: &mut tester): void {
# A ramp repeated down: each row the same, which the filters make zeros.
val flat := made_of("rgb", 64, 256);
val stored := image::png::encode(&flat, image::png::options { level = 0 }) else (_) { t.expects(false); return; };
val compressed := image::png::encode(&flat, image::png::options { level = 9 }) else (_) { t.expects(false); return; };
t.expects(stored.length > 64 * 256 * 3);
t.expects(compressed.length < 1000);
# Noise, too many colors for a palette, across blocks of several kinds.
val noise := made_of("noise", 96, 96);
t.expects(round_trips(&noise, image::png::options {}, 6, 8));
t.expects(round_trips(&noise, image::png::options { level = 1 }, 6, 8));
}
@test def writes_again_what_it_reads(t: &mut tester): void {
for file in test_files {
val read := image::load(file) else (_) { t.expects(false); continue; };
val written := image::png::encode(&read) else (_) { t.expects(false); continue; };
val again := read_back(written.view());
t.expects(apart(&again, &read) == 0);
}
}
# The test images, PNG and JPEG.
const test_files: strview[9] = [
"tests/images/rgba8.png", "tests/images/rgb8.png", "tests/images/gray8.png", "tests/images/gray16.png",
"tests/images/rgba16.png", "tests/images/palette4.png", "tests/images/interlaced.png",
"tests/images/rgb.jpg", "tests/images/gray.jpg",
];