Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-10-28 08:26:26 -05:00
commit f40701771e
69 files changed
+7703

No files matched your search

+123
View File
@@ -0,0 +1,123 @@
#! 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 `image.bench`, as an executable: the VM's times
#! aren't the program's.
import std::io;
import std::time;
import 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);
}
+78
View File
@@ -0,0 +1,78 @@
# Writes the images `tests/` reads into `tests/images/`, with Windows' own encoders, which are no
# part of image's decoders. Each is 24 by 20 pixels of a pattern the tests work out again, as
# `tests/support.sakura` does, red rising across, green down, and blue along the diagonal:
#
# r = 10x, g = 12y, b = 5(x + y), a = 55 + 8x where it has alpha
#
# powershell -File tools/test_images.ps1
Add-Type -AssemblyName PresentationCore
$out = Join-Path $PSScriptRoot "..\tests\images"
New-Item -ItemType Directory -Force $out | Out-Null
$width = 24; $height = 20
$formats = [System.Windows.Media.PixelFormats]
# The pattern as BGRA, and as the samples of the other formats.
function Pattern([string]$kind) {
$bytes = New-Object System.Collections.Generic.List[byte]
for ($y = 0; $y -lt $height; $y++) {
for ($x = 0; $x -lt $width; $x++) {
$r = 10 * $x; $g = 12 * $y; $b = 5 * ($x + $y); $a = 55 + 8 * $x
switch ($kind) {
"bgra" { $bytes.Add($b); $bytes.Add($g); $bytes.Add($r); $bytes.Add($a) }
"bgr" { $bytes.Add($b); $bytes.Add($g); $bytes.Add($r) }
"gray" { $bytes.Add($g) }
"gray16" { $v = $g * 257; $bytes.Add($v -band 0xFF); $bytes.Add($v -shr 8) }
"rgba64" { foreach ($c in @($r, $g, $b, $a)) { $v = $c * 257; $bytes.Add($v -band 0xFF); $bytes.Add($v -shr 8) } }
"index" { $bytes.Add([byte]((($x -shr 2) + ($y -shr 2) * 6) % 16)) }
"bw" { }
}
}
}
return ,$bytes.ToArray()
}
function Save($encoder, [string]$name, $source) {
$encoder.Frames.Add([System.Windows.Media.Imaging.BitmapFrame]::Create($source))
$stream = [System.IO.File]::Create((Join-Path $out $name))
$encoder.Save($stream)
$stream.Close()
}
function Source($format, [byte[]]$pixels, [int]$bitsPerPixel, $palette = $null) {
$stride = [int][Math]::Ceiling($width * $bitsPerPixel / 8.0)
return [System.Windows.Media.Imaging.BitmapSource]::Create($width, $height, 96, 96, $format, $palette, $pixels, $stride)
}
function Png([string]$name, $source, [bool]$interlaced = $false) {
$encoder = New-Object System.Windows.Media.Imaging.PngBitmapEncoder
if ($interlaced) { $encoder.Interlace = [System.Windows.Media.Imaging.PngInterlaceOption]::On }
Save $encoder $name $source
}
Png "rgba8.png" (Source $formats::Bgra32 (Pattern "bgra") 32)
Png "rgb8.png" (Source $formats::Bgr24 (Pattern "bgr") 24)
Png "gray8.png" (Source $formats::Gray8 (Pattern "gray") 8)
Png "gray16.png" (Source $formats::Gray16 (Pattern "gray16") 16)
Png "rgba16.png" (Source $formats::Rgba64 (Pattern "rgba64") 64)
Png "interlaced.png" (Source $formats::Bgra32 (Pattern "bgra") 32) $true
# Sixteen colors, entry i being (16i, 255 - 16i, 8i), each with alpha 255 - 8i.
$colors = New-Object System.Collections.Generic.List[System.Windows.Media.Color]
for ($i = 0; $i -lt 16; $i++) { $colors.Add([System.Windows.Media.Color]::FromArgb(255 - 8 * $i, 16 * $i, 255 - 16 * $i, 8 * $i)) }
$palette = New-Object System.Windows.Media.Imaging.BitmapPalette -ArgumentList (,$colors)
$indices = Pattern "index"
$packed = New-Object byte[] (12 * $height)
for ($i = 0; $i -lt $indices.Length; $i++) {
$row = [Math]::Floor($i / $width); $column = $i % $width
$packed[$row * 12 + ($column -shr 1)] = $packed[$row * 12 + ($column -shr 1)] -bor ($indices[$i] -shl (4 * (1 - ($column -band 1))))
}
Png "palette4.png" (Source $formats::Indexed4 $packed 4 $palette)
$jpeg = New-Object System.Windows.Media.Imaging.JpegBitmapEncoder
$jpeg.QualityLevel = 95
Save $jpeg "rgb.jpg" (Source $formats::Bgr24 (Pattern "bgr") 24)
$gray = New-Object System.Windows.Media.Imaging.JpegBitmapEncoder
$gray.QualityLevel = 95
Save $gray "gray.jpg" (Source $formats::Gray8 (Pattern "gray") 8)
Get-ChildItem $out | ForEach-Object { "$($_.Name) $($_.Length)" }