124 lines
4.8 KiB
Plaintext
124 lines
4.8 KiB
Plaintext
#! Times writing and reading each format, of two images of 1024 by 1024 pixels:
|
|
#! one as a photograph is, smooth gradients with a little noise over them, and
|
|
#! one as an interface is, flat colors with hard edges. PNG is written at levels
|
|
#! 1, 6, and 9, and JPEG at quality 90, its chrominance halved both ways and
|
|
#! kept whole. Each is the best of three.
|
|
#!
|
|
#! `blossom build`, then run `malunal.image.bench`, as an executable: the
|
|
#! VM's times aren't the program's.
|
|
|
|
import std::io;
|
|
import std::time;
|
|
import malunal::image;
|
|
|
|
const side: u32 = 1024;
|
|
const tries: u32 = 3;
|
|
|
|
# A photograph's worth: gradients across, down, and along the diagonal, and
|
|
# noise of a few levels over them.
|
|
def photograph(): image::image {
|
|
var pixels := vector::<u8>::new();
|
|
pixels.reserve((side as u64) * (side as u64) * 4);
|
|
var seed: u32 = 12345;
|
|
for y in 0..side {
|
|
for x in 0..side {
|
|
seed = seed ^ (seed << 13);
|
|
seed = seed ^ (seed >> 17);
|
|
seed = seed ^ (seed << 5);
|
|
val noise := (seed % 9) as i32 - 4;
|
|
pixels.append(clamp((x * 255 / side) as i32 + noise));
|
|
pixels.append(clamp((y * 255 / side) as i32 + noise));
|
|
pixels.append(clamp(((x + y) * 255 / (2 * side)) as i32 - noise));
|
|
pixels.append(255);
|
|
}
|
|
}
|
|
return image::image { width = side, height = side, pixels = move(pixels) };
|
|
}
|
|
|
|
# An interface's worth: panels of a few flat colors, each with a border.
|
|
def interface(): image::image {
|
|
var pixels := vector::<u8>::new();
|
|
pixels.reserve((side as u64) * (side as u64) * 4);
|
|
for y in 0..side {
|
|
for x in 0..side {
|
|
val panel := (x / 128 + (y / 96) * 3) % 5;
|
|
val border := x % 128 < 2 || y % 96 < 2;
|
|
val color: u32 = border ? 0x303030 : panel_colors[panel as u64];
|
|
pixels.append((color >> 16) as u8);
|
|
pixels.append((color >> 8) as u8);
|
|
pixels.append(color as u8);
|
|
pixels.append(255);
|
|
}
|
|
}
|
|
return image::image { width = side, height = side, pixels = move(pixels) };
|
|
}
|
|
|
|
const panel_colors: u32[5] = [0xF0F0F0, 0x2D6CDF, 0xE8E8E8, 0xFFFFFF, 0xD04040];
|
|
|
|
def clamp(value: i32): u8 {
|
|
return (value < 0 ? 0 : (value > 255 ? 255 : value)) as u8;
|
|
}
|
|
|
|
# Writes an image, then reads it, each the best of a few tries, and says how
|
|
# long each took and how many bytes it was.
|
|
def time_png(what: strview, picture: &image::image, level: u32): void {
|
|
var best_write: f64 = 1e30;
|
|
var best_read: f64 = 1e30;
|
|
var size: u64 = 0;
|
|
for _ in 0..tries {
|
|
var at := instant::now();
|
|
val written := image::png::encode(picture, image::png::options { level = level }) else (_) { println("couldn't write"); return; };
|
|
val wrote := at.elapsed().in_nanoseconds as f64;
|
|
at = instant::now();
|
|
val read := image::decode(written.view()) else (_) { println("couldn't read"); return; };
|
|
val took := at.elapsed().in_nanoseconds as f64;
|
|
best_write = wrote < best_write ? wrote : best_write;
|
|
best_read = took < best_read ? took : best_read;
|
|
size = written.length;
|
|
if read.pixels.length != picture.pixels.length {
|
|
println("read back another size");
|
|
}
|
|
}
|
|
report(what, best_write, best_read, size);
|
|
}
|
|
|
|
def time_jpeg(what: strview, picture: &image::image, chroma: image::jpeg::subsampling): void {
|
|
var best_write: f64 = 1e30;
|
|
var best_read: f64 = 1e30;
|
|
var size: u64 = 0;
|
|
for _ in 0..tries {
|
|
var at := instant::now();
|
|
val written := image::jpeg::encode(picture, image::jpeg::options { quality = 90, chroma = chroma }) else (_) { println("couldn't write"); return; };
|
|
val wrote := at.elapsed().in_nanoseconds as f64;
|
|
at = instant::now();
|
|
val read := image::decode(written.view()) else (_) { println("couldn't read"); return; };
|
|
val took := at.elapsed().in_nanoseconds as f64;
|
|
best_write = wrote < best_write ? wrote : best_write;
|
|
best_read = took < best_read ? took : best_read;
|
|
size = written.length;
|
|
if read.pixels.length != picture.pixels.length {
|
|
println("read back another size");
|
|
}
|
|
}
|
|
report(what, best_write, best_read, size);
|
|
}
|
|
|
|
def report(what: strview, wrote: f64, read: f64, size: u64): void {
|
|
val pixels := (side as f64) * (side as f64);
|
|
println($"{what}: written in {(wrote / 1000000) as u64} ms ({(pixels * 1000 / wrote) as u64} MP/s), read in {(read / 1000000) as u64} ms, {size / 1024} KiB");
|
|
}
|
|
|
|
def main(): void {
|
|
val photo := photograph();
|
|
val flat := interface();
|
|
time_png("PNG level 1, photograph", &photo, 1);
|
|
time_png("PNG level 6, photograph", &photo, 6);
|
|
time_png("PNG level 9, photograph", &photo, 9);
|
|
time_png("PNG level 1, interface", &flat, 1);
|
|
time_png("PNG level 6, interface", &flat, 6);
|
|
time_png("PNG level 9, interface", &flat, 9);
|
|
time_jpeg("JPEG 90, 4:2:0, photograph", &photo, image::jpeg::subsampling::both);
|
|
time_jpeg("JPEG 90, 4:4:4, photograph", &photo, image::jpeg::subsampling::none);
|
|
time_jpeg("JPEG 90, 4:2:0, interface", &flat, image::jpeg::subsampling::both);
|
|
}
|