Initial Commit
No files matched your search
@@ -0,0 +1,82 @@
|
||||
#! Contains the tests of `image`'s own functions: which format bytes, or a
|
||||
#! file's extension, say they're in, and an image read and written by them.
|
||||
|
||||
import std::io;
|
||||
import std::testing;
|
||||
import malunal::image;
|
||||
|
||||
@test def finds_each_format_by_its_first_bytes(t: &mut tester): void {
|
||||
val png := bytes_of("tests/images/rgb8.png");
|
||||
val jpeg := bytes_of("tests/images/rgb.jpg");
|
||||
t.expects(format_is(png.view(), image::format::png));
|
||||
t.expects(format_is(jpeg.view(), image::format::jpeg));
|
||||
t.expects(format_is("<?xml version='1.0'?>\n<!-- drawn -->\n<!DOCTYPE svg [<!ENTITY a 'b'>]><svg/>", image::format::svg));
|
||||
t.expects(format_is("<svg:svg xmlns:svg='http://www.w3.org/2000/svg'/>", image::format::svg));
|
||||
t.expects(image::format_of("<html><svg/></html>") is none);
|
||||
t.expects(image::format_of("GIF89a") is none);
|
||||
t.expects(fails(image::decode("plain text"), image::image_error::unrecognized));
|
||||
t.expects(fails(image::load("tests/images/missing.png"), image::image_error::unreadable));
|
||||
}
|
||||
|
||||
@test def names_formats_by_their_extensions(t: &mut tester): void {
|
||||
t.expects(image::format_named("png") is some(_) && image::format_named("PNG").unwrap() == image::format::png);
|
||||
t.expects(image::format_named("jpg").unwrap() == image::format::jpeg);
|
||||
t.expects(image::format_named("JPEG").unwrap() == image::format::jpeg);
|
||||
t.expects(image::format_named("jfif").unwrap() == image::format::jpeg);
|
||||
t.expects(image::format_named("svg").unwrap() == image::format::svg);
|
||||
t.expects(image::format_named("gif") is none);
|
||||
t.expects(image::format_named("") is none);
|
||||
}
|
||||
|
||||
@test def writes_in_the_format_asked(t: &mut tester): void {
|
||||
val picture := made_of("rgba", 24, 20);
|
||||
val png := image::encode(&picture, image::format::png) else (_) { t.expects(false); return; };
|
||||
val jpeg := image::encode(&picture, image::format::jpeg) else (_) { t.expects(false); return; };
|
||||
t.expects(format_is(png.view(), image::format::png) && format_is(jpeg.view(), image::format::jpeg));
|
||||
val again := read_back(png.view());
|
||||
t.expects(apart(&again, &picture) == 0);
|
||||
t.expects(fails_writing(image::encode(&picture, image::format::svg), image::image_error::unsupported));
|
||||
|
||||
# An image whose pixels aren't as many as its size says isn't written.
|
||||
var short := made_of("rgb", 4, 4);
|
||||
short.height = 5;
|
||||
t.expects(fails_writing(image::encode(&short, image::format::png), image::image_error::malformed));
|
||||
t.expects(fails_writing(image::encode(&short, image::format::jpeg), image::image_error::malformed));
|
||||
val empty := image::image { pixels = vector::<u8>::new() };
|
||||
t.expects(fails_writing(image::encode(&empty, image::format::png), image::image_error::malformed));
|
||||
}
|
||||
|
||||
@test def writes_into_a_stream(t: &mut tester): void {
|
||||
val picture := made_of("palette", 24, 20);
|
||||
var out := string::new(nothing {});
|
||||
t.expects(image::write_to(&picture, image::format::png, &mut out) is value);
|
||||
val again := read_back(out.view());
|
||||
t.expects(apart(&again, &picture) == 0);
|
||||
}
|
||||
|
||||
@test def saves_by_the_extension_and_loads_again(t: &mut tester): void {
|
||||
val picture := made_of("gray_alpha", 24, 20);
|
||||
t.expects(image::save(&picture, "tests/written.png") is value);
|
||||
val loaded := image::load("tests/written.png") else (_) { t.expects(false); return; };
|
||||
t.expects(apart(&loaded, &picture) == 0);
|
||||
t.expects(image::save(&picture, "tests/written.jpg") is value);
|
||||
val lossy := image::load("tests/written.jpg") else (_) { t.expects(false); return; };
|
||||
t.expects(lossy.width == 24 && lossy.height == 20);
|
||||
|
||||
match image::save(&picture, "tests/written.gif") {
|
||||
value(_) => t.expects(false);
|
||||
error(e) => t.expects(e == image::image_error::unrecognized);
|
||||
}
|
||||
match image::save(&picture, "tests/written.svg") {
|
||||
value(_) => t.expects(false);
|
||||
error(e) => t.expects(e == image::image_error::unsupported);
|
||||
}
|
||||
match image::save(&picture, "tests/no/such/folder/written.png") {
|
||||
value(_) => t.expects(false);
|
||||
error(e) => t.expects(e == image::image_error::unwritable);
|
||||
}
|
||||
for written in ["tests/written.png", "tests/written.jpg"] {
|
||||
val at := path::parse(written) else (_) { continue; };
|
||||
_ = io::remove(&at);
|
||||
}
|
||||
}
|
||||
|
After Width: | Height: | Size: 429 B |
|
After Width: | Height: | Size: 222 B |
|
After Width: | Height: | Size: 204 B |
|
After Width: | Height: | Size: 285 B |
|
After Width: | Height: | Size: 260 B |
|
After Width: | Height: | Size: 811 B |
|
After Width: | Height: | Size: 167 B |
|
After Width: | Height: | Size: 171 B |
|
After Width: | Height: | Size: 157 B |
@@ -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.view());
|
||||
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.view()[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.view());
|
||||
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.view()).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,
|
||||
];
|
||||
@@ -0,0 +1,10 @@
|
||||
#! Contains the `main` that runs image's tests, each in a copy of the program of
|
||||
#! its own. `blossom test` runs them, and `-- --only TEXT` those whose name
|
||||
#! holds the text.
|
||||
|
||||
import system;
|
||||
import std::testing;
|
||||
|
||||
def main(args: span<strview>): void {
|
||||
exit(tester::run(args));
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
#! 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 malunal::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",
|
||||
];
|
||||
@@ -0,0 +1,146 @@
|
||||
#! Contains what the tests of `image` share: the pattern the test images were
|
||||
#! made of, images made of it and of others in memory, and how far one image
|
||||
#! strays from another.
|
||||
|
||||
import std::io;
|
||||
import malunal::image;
|
||||
|
||||
# The pattern the test images were made of, at a pixel, for a kind of image,
|
||||
# and others images are made of to be written: gray exact at 4 bits, black and
|
||||
# white, gray with alpha, and noise.
|
||||
def expected(kind: strview, x: u32, y: u32): u8[4] {
|
||||
if kind == "palette" {
|
||||
val entry := ((x >> 2) + (y >> 2) * 6) % 16;
|
||||
return [(16 * entry) as u8, (255 - 16 * entry) as u8, (8 * entry) as u8, (255 - 8 * entry) as u8];
|
||||
}
|
||||
if kind == "gray" {
|
||||
val g := (12 * y) as u8;
|
||||
return [g, g, g, 255];
|
||||
}
|
||||
if kind == "gray4" {
|
||||
val g := (17 * ((x + y) % 16)) as u8;
|
||||
return [g, g, g, 255];
|
||||
}
|
||||
if kind == "bw" {
|
||||
val g: u8 = ((x / 3 + y / 2) % 2 == 0) ? 0 : 255;
|
||||
return [g, g, g, 255];
|
||||
}
|
||||
if kind == "gray_alpha" {
|
||||
val g := (12 * y) as u8;
|
||||
return [g, g, g, (55 + 8 * x) as u8];
|
||||
}
|
||||
if kind == "noise" {
|
||||
var seed := (x * 7919 + y * 104729 + 12345) as u32;
|
||||
seed = seed ^ (seed << 13);
|
||||
seed = seed ^ (seed >> 17);
|
||||
seed = seed ^ (seed << 5);
|
||||
return [seed as u8, (seed >> 8) as u8, (seed >> 16) as u8, (seed >> 24) as u8];
|
||||
}
|
||||
val alpha: u8 = kind == "rgba" ? (55 + 8 * x) as u8 : 255;
|
||||
return [(10 * x) as u8, (12 * y) as u8, (5 * (x + y)) as u8, alpha];
|
||||
}
|
||||
|
||||
# An image of a size, of a kind's pattern.
|
||||
def made_of(kind: strview, width: u32, height: u32): image::image {
|
||||
var pixels := vector::<u8>::new();
|
||||
for y in 0..height {
|
||||
for x in 0..width {
|
||||
val color := expected(kind, x, y);
|
||||
for channel in 0u..4u {
|
||||
pixels.append(color[channel]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return image::image { width = width, height = height, pixels = move(pixels) };
|
||||
}
|
||||
|
||||
# How far an image strays from another at most, in any channel, or 256 where
|
||||
# they're of other sizes.
|
||||
def apart(a: &image::image, b: &image::image): u32 {
|
||||
if a.width != b.width || a.height != b.height || a.pixels.length != b.pixels.length {
|
||||
return 256;
|
||||
}
|
||||
var most: u32 = 0;
|
||||
for index in 0..a.pixels.length {
|
||||
val x := a.pixels[index] as u32;
|
||||
val y := b.pixels[index] as u32;
|
||||
val gap := x > y ? x - y : y - x;
|
||||
most = gap > most ? gap : most;
|
||||
}
|
||||
return most;
|
||||
}
|
||||
|
||||
# How far an image strays from another in its colors alone, as JPEG keeps
|
||||
# them, or 256 where they're of other sizes.
|
||||
def colors_apart(a: &image::image, b: &image::image): u32 {
|
||||
if a.width != b.width || a.height != b.height || a.pixels.length != b.pixels.length {
|
||||
return 256;
|
||||
}
|
||||
var most: u32 = 0;
|
||||
for index in 0..a.pixels.length {
|
||||
if index % 4 == 3 {
|
||||
continue;
|
||||
}
|
||||
val x := a.pixels[index] as u32;
|
||||
val y := b.pixels[index] as u32;
|
||||
val gap := x > y ? x - y : y - x;
|
||||
most = gap > most ? gap : most;
|
||||
}
|
||||
return most;
|
||||
}
|
||||
|
||||
# How far an image strays from the pattern at most, in any channel, or 256 where
|
||||
# it can't be read or is another size.
|
||||
def stray(file: strview, kind: strview): u32 {
|
||||
val read := image::load(file) else { return 256; };
|
||||
val want := made_of(kind, 24, 20);
|
||||
return apart(&read, &want);
|
||||
}
|
||||
|
||||
# A pixel's red, green, blue, and alpha.
|
||||
def rgba(r: u8, g: u8, b: u8, a: u8): u8[4] {
|
||||
return [r, g, b, a];
|
||||
}
|
||||
|
||||
# Whether reading an image failed, for a reason.
|
||||
def fails(read: result<image::image, image::image_error>, why: image::image_error): bool {
|
||||
match read {
|
||||
value(&_) => return false;
|
||||
error(e) => return e == why;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
# Whether writing an image failed, for a reason.
|
||||
def fails_writing(written: result<vector<u8>, image::image_error>, why: image::image_error): bool {
|
||||
match written {
|
||||
value(&_) => return false;
|
||||
error(e) => return e == why;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
# Whether bytes are found to be in a format.
|
||||
def format_is(bytes: span<u8>, which: image::format): bool {
|
||||
val found := image::format_of(bytes) else { return false; };
|
||||
return found == which;
|
||||
}
|
||||
|
||||
# Reads a test image's bytes.
|
||||
def bytes_of(file: strview): string {
|
||||
val at := path::parse(file) else (_) { return string::new(nothing {}); };
|
||||
match io::read_file(&at) {
|
||||
value(&read) => return string::copy_of(&read);
|
||||
error(_) => return string::new(nothing {});
|
||||
}
|
||||
return string::new(nothing {});
|
||||
}
|
||||
|
||||
# Reads bytes as an image, or an empty one where they aren't.
|
||||
def read_back(bytes: span<u8>): image::image {
|
||||
match image::decode(bytes) {
|
||||
value(&made) => return image::image { width = made.width, height = made.height, pixels = vector::<u8>::copy_of(&made.pixels) };
|
||||
error(_) => return image::image { pixels = vector::<u8>::new() };
|
||||
}
|
||||
return image::image { pixels = vector::<u8>::new() };
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
#! Contains the tests of `image::svg`: shapes, sizes, strokes, gradients,
|
||||
#! groups, and style sheets drawn, and broken SVG refused.
|
||||
|
||||
import std::testing;
|
||||
import malunal::image;
|
||||
|
||||
# Draws an SVG at its own size.
|
||||
def draw(text: strview): image::image {
|
||||
return read_back(text);
|
||||
}
|
||||
|
||||
@test def draws_svg_shapes(t: &mut tester): void {
|
||||
val made := draw("""
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="40" height="20">
|
||||
<rect x="0" y="0" width="10" height="10" fill="red"/>
|
||||
<circle cx="25" cy="10" r="6" fill="#0000ff"/>
|
||||
<rect x="10.5" y="15" width="1" height="5" fill="lime"/>
|
||||
<path d="M30 0 h10 v10 h-10 z m3 3 v4 h4 v-4 z" fill="black" fill-rule="evenodd"/>
|
||||
</svg>
|
||||
""");
|
||||
t.expects(made.width == 40 && made.height == 20);
|
||||
t.expects(made.at(5, 5) == rgba(255, 0, 0, 255));
|
||||
t.expects(made.at(25, 10) == rgba(0, 0, 255, 255));
|
||||
t.expects(made.at(15, 5) == rgba(0, 0, 0, 0));
|
||||
# Half a pixel covered is half as opaque, and still the color.
|
||||
val half := made.at(10, 17);
|
||||
t.expects(half[1] == 255 && half[3] >= 126 && half[3] <= 129);
|
||||
# The even-odd rule leaves a hole where the inner square crosses the outer.
|
||||
t.expects(made.at(31, 1) == rgba(0, 0, 0, 255));
|
||||
t.expects(made.at(35, 5) == rgba(0, 0, 0, 0));
|
||||
}
|
||||
|
||||
@test def draws_svg_at_the_size_asked(t: &mut tester): void {
|
||||
val text := "<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 10 5'><rect width='5' height='5' fill='#808080'/></svg>";
|
||||
val own := draw(text);
|
||||
t.expects(own.width == 10 && own.height == 5);
|
||||
|
||||
val wide := image::decode(text, image::svg_size { width = 100 }) else (_) { t.expects(false); return; };
|
||||
t.expects(wide.width == 100 && wide.height == 50);
|
||||
t.expects(wide.at(25, 25) == rgba(128, 128, 128, 255) && wide.at(75, 25) == rgba(0, 0, 0, 0));
|
||||
|
||||
val scaled := image::decode(text, image::svg_size { scale = 3 }) else (_) { t.expects(false); return; };
|
||||
t.expects(scaled.width == 30 && scaled.height == 15);
|
||||
|
||||
# A view box of another shape is fit within, centered.
|
||||
val square := image::decode(text, image::svg_size { width = 20, height = 20 }) else (_) { t.expects(false); return; };
|
||||
t.expects(square.at(5, 2) == rgba(0, 0, 0, 0) && square.at(5, 10) == rgba(128, 128, 128, 255));
|
||||
}
|
||||
|
||||
@test def strokes_svg_paths(t: &mut tester): void {
|
||||
val made := draw("""
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="40" height="30">
|
||||
<line x1="10" y1="5" x2="30" y2="5" stroke="black" stroke-width="4"/>
|
||||
<line x1="10" y1="15" x2="30" y2="15" stroke="black" stroke-width="4" stroke-linecap="square"/>
|
||||
<line x1="10" y1="25" x2="30" y2="25" stroke="black" stroke-width="4" stroke-dasharray="5 5"/>
|
||||
</svg>
|
||||
""");
|
||||
t.expects(made.at(20, 5) == rgba(0, 0, 0, 255) && made.at(20, 3) == rgba(0, 0, 0, 255) && made.at(20, 8) == rgba(0, 0, 0, 0));
|
||||
# A butt cap ends at the end, and a square one past it.
|
||||
t.expects(made.at(8, 5) == rgba(0, 0, 0, 0) && made.at(8, 15) == rgba(0, 0, 0, 255));
|
||||
# Dashes, then gaps, five long each.
|
||||
t.expects(made.at(12, 25) == rgba(0, 0, 0, 255) && made.at(17, 25) == rgba(0, 0, 0, 0) && made.at(22, 25) == rgba(0, 0, 0, 255));
|
||||
}
|
||||
|
||||
@test def paints_svg_gradients(t: &mut tester): void {
|
||||
val made := draw("""
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="256" height="4">
|
||||
<linearGradient id="ramp"><stop offset="0" stop-color="black"/><stop offset="1" stop-color="white"/></linearGradient>
|
||||
<rect width="256" height="4" fill="url(#ramp)"/>
|
||||
</svg>
|
||||
""");
|
||||
val middle := made.at(128, 2);
|
||||
t.expects(middle[0] >= 126 && middle[0] <= 130 && middle[3] == 255);
|
||||
t.expects(made.at(0, 2)[0] <= 2 && made.at(255, 2)[0] >= 253);
|
||||
}
|
||||
|
||||
@test def composites_svg_groups(t: &mut tester): void {
|
||||
val made := draw("""
|
||||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="40" height="20">
|
||||
<defs>
|
||||
<rect id="block" width="10" height="10"/>
|
||||
<clipPath id="left"><rect width="25" height="20"/></clipPath>
|
||||
</defs>
|
||||
<g opacity="0.5" fill="blue">
|
||||
<use xlink:href="#block"/>
|
||||
<use xlink:href="#block" x="5"/>
|
||||
</g>
|
||||
<rect x="20" y="0" width="10" height="10" fill="red" clip-path="url(#left)"/>
|
||||
</svg>
|
||||
""");
|
||||
# The group is drawn whole, then made half as opaque, so where its parts
|
||||
# overlap is no more opaque than where they don't.
|
||||
val alone := made.at(2, 5);
|
||||
val overlapped := made.at(7, 5);
|
||||
t.expects(alone[2] == 255 && alone[3] >= 127 && alone[3] <= 128);
|
||||
t.expects(overlapped[3] == alone[3]);
|
||||
# The clip keeps the red left of 25.
|
||||
t.expects(made.at(22, 5) == rgba(255, 0, 0, 255) && made.at(27, 5) == rgba(0, 0, 0, 0));
|
||||
}
|
||||
|
||||
@test def styles_svg_by_its_sheets(t: &mut tester): void {
|
||||
val made := draw("""
|
||||
<svg xmlns="http://www.w3.org/2000/svg" width="40" height="10" style="color: #00ff00">
|
||||
<style>
|
||||
.warm { fill: orange }
|
||||
#hot { fill: red }
|
||||
g rect { stroke: none }
|
||||
</style>
|
||||
<rect class="warm" width="10" height="10" fill="blue"/>
|
||||
<rect class="warm" id="hot" x="10" width="10" height="10"/>
|
||||
<rect x="20" width="10" height="10" style="fill: currentColor"/>
|
||||
<rect x="30" width="10" height="10" fill="purple" fill-opacity="0.5"/>
|
||||
</svg>
|
||||
""");
|
||||
t.expects(made.at(5, 5) == rgba(255, 165, 0, 255));
|
||||
t.expects(made.at(15, 5) == rgba(255, 0, 0, 255));
|
||||
t.expects(made.at(25, 5) == rgba(0, 255, 0, 255));
|
||||
val faint := made.at(35, 5);
|
||||
t.expects(faint[0] == 128 && faint[3] >= 127 && faint[3] <= 128);
|
||||
}
|
||||
|
||||
@test def refuses_broken_svg(t: &mut tester): void {
|
||||
t.expects(fails(image::decode("<svg xmlns='http://www.w3.org/2000/svg'><rect></svg>"), image::image_error::malformed));
|
||||
t.expects(fails(image::decode("<svg xmlns='http://www.w3.org/2000/svg' width='0' height='10'/>"), image::image_error::malformed));
|
||||
t.expects(fails(image::decode("<svg xmlns='http://www.w3.org/2000/svg' width='100000' height='100000'/>"), image::image_error::too_large));
|
||||
# Path data broken partway is drawn as far as it goes.
|
||||
val partial := draw("<svg xmlns='http://www.w3.org/2000/svg' width='10' height='10'><path d='M0 0 H10 V10 H0 Z L oops'/></svg>");
|
||||
t.expects(partial.at(5, 5) == rgba(0, 0, 0, 255));
|
||||
}
|
||||