Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-11-11 11:52:19 -06:00
commit 637b1df8a7
69 files changed
+7703

No files matched your search

+94
View File
@@ -0,0 +1,94 @@
#! Contains the tests of `image::jpeg`: the test images read, what isn't read
#! refused, and images written and read back near the colors they were, as
#! JPEG keeps them.
import std::testing;
import malunal::image;
# The byte of a JPEG this encoder writes whose frame says how many components
# it has: past the start of image, JFIF's 18, the quantization tables' 4, each
# table's 65, and the frame's marker, length, precision, height, and width.
def components_of(written: &vector<u8>, tables: u64): u8 {
return written[2 + 18 + 4 + 65 * tables + 9];
}
# Writes an image as JPEG, and how far what's read back strays in color.
def written_apart(picture: &image::image, given: image::jpeg::options): u32 {
val written := image::jpeg::encode(picture, given) else (_) { return 256; };
val again := read_back(written.span);
return colors_apart(&again, picture);
}
@test def reads_baseline_jpeg(t: &mut tester): void {
# Quality 95 strays a little from the pattern, as JPEG does.
val color := stray("tests/images/rgb.jpg", "rgb");
val gray := stray("tests/images/gray.jpg", "gray");
t.expects(color <= 12);
t.expects(gray <= 4);
}
@test def refuses_jpeg_it_does_not_read(t: &mut tester): void {
# Arithmetic coding, and samples of 12 bits.
t.expects(fails(image::decode("\xFF\xD8\xFF\xC9\x00\x0B\x08\x00\x10\x00\x10\x01\x01\x11\x00\xFF\xD9"), image::image_error::unsupported));
t.expects(fails(image::decode("\xFF\xD8\xFF\xC0\x00\x0B\x0C\x00\x10\x00\x10\x01\x01\x11\x00\xFF\xD9"), image::image_error::unsupported));
# No frame at all.
t.expects(fails(image::decode("\xFF\xD8\xFF\xD9"), image::image_error::malformed));
t.expects(fails(image::decode("\xFF\xD8\xFF\xC0\x00\x0B\x08\x80\x00\x80\x00\x01\x01\x11\x00\xFF\xD9"), image::image_error::too_large));
# Cut short, it gives what it has.
val whole := bytes_of("tests/images/rgb.jpg");
val cut := image::decode(whole.span[0:whole.length - 100]) else (_) { t.expects(false); return; };
t.expects(cut.width == 24 && cut.height == 20);
}
@test def writes_jpeg_near_its_colors(t: &mut tester): void {
val picture := made_of("rgb", 24, 20);
# Every chroma sample kept, at quality 100, strays only as rounding does.
t.expects(written_apart(&picture, image::jpeg::options { quality = 100, chroma = image::jpeg::subsampling::none }) <= 3);
t.expects(written_apart(&picture, image::jpeg::options { quality = 95, chroma = image::jpeg::subsampling::none }) <= 8);
t.expects(written_apart(&picture, image::jpeg::options { quality = 95, chroma = image::jpeg::subsampling::across }) <= 10);
t.expects(written_apart(&picture, image::jpeg::options { quality = 95 }) <= 12);
t.expects(written_apart(&picture, image::jpeg::options { quality = 50 }) <= 24);
}
@test def writes_gray_jpeg_as_gray(t: &mut tester): void {
val gray := made_of("gray", 24, 20);
val written := image::jpeg::encode(&gray, image::jpeg::options { quality = 100 }) else (_) { t.expects(false); return; };
t.expects(components_of(&written, 1) == 1);
val again := read_back(written.span);
t.expects(apart(&again, &gray) <= 2);
val color := image::jpeg::encode(&made_of("rgb", 24, 20)) else (_) { t.expects(false); return; };
t.expects(components_of(&color, 2) == 3);
}
@test def writes_jpeg_smaller_at_lower_quality(t: &mut tester): void {
val noise := made_of("noise", 64, 64);
val high := image::jpeg::encode(&noise, image::jpeg::options { quality = 95 }) else (_) { t.expects(false); return; };
val low := image::jpeg::encode(&noise, image::jpeg::options { quality = 30 }) else (_) { t.expects(false); return; };
t.expects(low.length < high.length);
t.expects(read_back(low.span).width == 64);
}
@test def writes_jpeg_of_any_size(t: &mut tester): void {
# Sizes of part of an MCU, of every subsampling.
for chroma in subsamplings {
for index in 0u..5u {
val picture := made_of("rgb", widths[index], heights[index]);
t.expects(written_apart(&picture, image::jpeg::options { quality = 100, chroma = chroma }) <= 16);
}
}
var wide := image::image { width = 70000, height = 1, pixels = vector::<u8>::new() };
wide.pixels.resize(70000 * 4);
t.expects(fails_writing(image::jpeg::encode(&wide), image::image_error::too_large));
}
# Sizes of images, across and down, of part of an MCU, no wider than the
# pattern's red stays below 256, past which a sharp edge of red is, which
# halved chroma blurs.
const widths: u32[5] = [1, 17, 8, 25, 3];
const heights: u32[5] = [1, 9, 16, 2, 31];
const subsamplings: image::jpeg::subsampling[3] = [
image::jpeg::subsampling::none, image::jpeg::subsampling::across, image::jpeg::subsampling::both,
];