146 lines
4.6 KiB
Plaintext
146 lines
4.6 KiB
Plaintext
#! 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 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() };
|
||
|
|
}
|