[raster] Toolbox: probe/decode with caps, convert, crop, rotate, resize, presets, compress to size, HEIC via WIC

This commit is contained in:
2026-11-16 13:30:42 +01:00
parent ede068f7c9
commit ce1e8785d9
16 changed files with 2394 additions and 1 deletions
+311
View File
@@ -0,0 +1,311 @@
//! Fixtures built in code: nothing binary is checked in.
#![allow(dead_code)]
use image::{DynamicImage, ImageBuffer, Rgb, RgbImage, Rgba, RgbaImage};
/// Deterministic xorshift64* generator, so every run mutates the same bytes.
pub struct XorShift(u64);
impl XorShift {
pub fn new(seed: u64) -> Self {
Self(seed.max(1))
}
pub fn next_u64(&mut self) -> u64 {
let mut x = self.0;
x ^= x >> 12;
x ^= x << 25;
x ^= x >> 27;
self.0 = x;
x.wrapping_mul(0x2545_F491_4F6C_DD1D)
}
pub fn below(&mut self, n: usize) -> usize {
if n == 0 { 0 } else { (self.next_u64() % n as u64) as usize }
}
pub fn byte(&mut self) -> u8 {
(self.next_u64() >> 56) as u8
}
}
/// A small RGB image with a gradient and a few hard edges.
pub fn sample_rgb(width: u32, height: u32) -> DynamicImage {
let img: RgbImage = ImageBuffer::from_fn(width, height, |x, y| {
let r = (x * 255 / width.max(1)) as u8;
let g = (y * 255 / height.max(1)) as u8;
let b = if (x / 8 + y / 8) % 2 == 0 { 40 } else { 210 };
Rgb([r, g, b])
});
DynamicImage::ImageRgb8(img)
}
/// A small RGBA image whose alpha varies, including fully transparent pixels.
pub fn sample_rgba(width: u32, height: u32) -> DynamicImage {
let img: RgbaImage = ImageBuffer::from_fn(width, height, |x, y| {
let r = (x * 255 / width.max(1)) as u8;
let g = (y * 255 / height.max(1)) as u8;
let a = if x < width / 4 { 0 } else { ((x + y) * 7 % 256) as u8 };
Rgba([r, g, 128, a])
});
DynamicImage::ImageRgba8(img)
}
/// A noisy photo-like image: a smooth gradient plus per-pixel noise (hard to compress).
pub fn noisy_rgb(width: u32, height: u32, seed: u64) -> DynamicImage {
let mut rng = XorShift::new(seed);
let mut img = RgbImage::new(width, height);
for (x, y, px) in img.enumerate_pixels_mut() {
let n = rng.next_u64();
let base_r = (x * 200 / width) as i32;
let base_g = (y * 200 / height) as i32;
let noise = |shift: u32| ((n >> shift) & 0x3F) as i32 - 32;
px.0 = [
(base_r + noise(0)).clamp(0, 255) as u8,
(base_g + noise(8)).clamp(0, 255) as u8,
(120 + noise(16)).clamp(0, 255) as u8,
];
}
DynamicImage::ImageRgb8(img)
}
pub fn crc32(data: &[u8]) -> u32 {
let mut crc = 0xFFFF_FFFFu32;
for &b in data {
crc ^= u32::from(b);
for _ in 0..8 {
let mask = (crc & 1).wrapping_neg();
crc = (crc >> 1) ^ (0xEDB8_8320 & mask);
}
}
!crc
}
pub fn png_chunk(out: &mut Vec<u8>, kind: &[u8; 4], data: &[u8]) {
out.extend_from_slice(&(data.len() as u32).to_be_bytes());
let mut body = Vec::with_capacity(4 + data.len());
body.extend_from_slice(kind);
body.extend_from_slice(data);
out.extend_from_slice(&body);
out.extend_from_slice(&crc32(&body).to_be_bytes());
}
/// A hand-built PNG with an arbitrary IHDR size (8-bit RGB) and a tiny stored-zlib IDAT.
pub fn png_with_size(width: u32, height: u32) -> Vec<u8> {
let mut out = vec![0x89, b'P', b'N', b'G', 0x0D, 0x0A, 0x1A, 0x0A];
let mut ihdr = Vec::new();
ihdr.extend_from_slice(&width.to_be_bytes());
ihdr.extend_from_slice(&height.to_be_bytes());
ihdr.extend_from_slice(&[8, 2, 0, 0, 0]);
png_chunk(&mut out, b"IHDR", &ihdr);
// zlib header + one empty stored final block + adler32 of nothing.
let idat = [0x78, 0x01, 0x01, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x01];
png_chunk(&mut out, b"IDAT", &idat);
png_chunk(&mut out, b"IEND", &[]);
out
}
pub fn encode_with_image(img: &DynamicImage, fmt: image::ImageFormat) -> Vec<u8> {
let mut out = std::io::Cursor::new(Vec::new());
img.write_to(&mut out, fmt).expect("fixture encodes");
out.into_inner()
}
pub fn jpeg_fixture(width: u32, height: u32) -> Vec<u8> {
encode_with_image(&sample_rgb(width, height), image::ImageFormat::Jpeg)
}
/// Replace the dimensions in the first SOF0/SOF2 segment of a JPEG.
pub fn patch_jpeg_sof(mut jpeg: Vec<u8>, width: u16, height: u16) -> Vec<u8> {
let mut i = 2;
while i + 9 < jpeg.len() {
if jpeg[i] != 0xFF {
i += 1;
continue;
}
let marker = jpeg[i + 1];
let len = u16::from_be_bytes([jpeg[i + 2], jpeg[i + 3]]) as usize;
if matches!(marker, 0xC0..=0xC2) {
jpeg[i + 5..i + 7].copy_from_slice(&height.to_be_bytes());
jpeg[i + 7..i + 9].copy_from_slice(&width.to_be_bytes());
return jpeg;
}
i += 2 + len;
}
panic!("no SOF in fixture");
}
/// Insert a minimal EXIF APP1 segment (one IFD0 entry: Orientation) right after SOI.
pub fn jpeg_with_orientation(jpeg: &[u8], orientation: u16) -> Vec<u8> {
let mut tiff = Vec::new();
tiff.extend_from_slice(b"II*\0");
tiff.extend_from_slice(&8u32.to_le_bytes());
tiff.extend_from_slice(&1u16.to_le_bytes()); // one entry
tiff.extend_from_slice(&0x0112u16.to_le_bytes()); // Orientation
tiff.extend_from_slice(&3u16.to_le_bytes()); // SHORT
tiff.extend_from_slice(&1u32.to_le_bytes()); // count
tiff.extend_from_slice(&orientation.to_le_bytes());
tiff.extend_from_slice(&[0, 0]); // padding of the 4-byte value field
tiff.extend_from_slice(&0u32.to_le_bytes()); // no next IFD
let mut payload = b"Exif\0\0".to_vec();
payload.extend_from_slice(&tiff);
let mut out = jpeg[..2].to_vec();
out.extend_from_slice(&[0xFF, 0xE1]);
out.extend_from_slice(&((payload.len() + 2) as u16).to_be_bytes());
out.extend_from_slice(&payload);
out.extend_from_slice(&jpeg[2..]);
out
}
/// GIF89a whose logical screen and image descriptor claim `w × h`, with a tiny LZW stream.
pub fn gif_with_size(width: u16, height: u16) -> Vec<u8> {
let mut out = b"GIF89a".to_vec();
out.extend_from_slice(&width.to_le_bytes());
out.extend_from_slice(&height.to_le_bytes());
out.extend_from_slice(&[0x80, 0, 0]); // global colour table of 2 entries
out.extend_from_slice(&[0, 0, 0, 255, 255, 255]);
out.push(0x2C);
out.extend_from_slice(&0u16.to_le_bytes());
out.extend_from_slice(&0u16.to_le_bytes());
out.extend_from_slice(&width.to_le_bytes());
out.extend_from_slice(&height.to_le_bytes());
out.push(0);
out.push(2); // LZW minimum code size
out.extend_from_slice(&[2, 0x44, 0x01, 0]); // clear, a pixel, end
out.push(0x3B);
out
}
/// Little-endian baseline TIFF whose IFD claims `w × h` (8-bit grey, one strip of 1 byte).
pub fn tiff_with_size(width: u32, height: u32) -> Vec<u8> {
let entries: [(u16, u16, u32, u32); 8] = [
(256, 4, 1, width), // ImageWidth LONG
(257, 4, 1, height), // ImageLength LONG
(258, 3, 1, 8), // BitsPerSample
(259, 3, 1, 1), // Compression none
(262, 3, 1, 1), // BlackIsZero
(273, 4, 1, 0), // StripOffsets (patched below)
(278, 4, 1, height), // RowsPerStrip
(279, 4, 1, 1), // StripByteCounts
];
let mut out = b"II*\0".to_vec();
out.extend_from_slice(&8u32.to_le_bytes());
out.extend_from_slice(&(entries.len() as u16).to_le_bytes());
let data_offset = 8 + 2 + entries.len() as u32 * 12 + 4;
for (tag, kind, count, value) in entries {
out.extend_from_slice(&tag.to_le_bytes());
out.extend_from_slice(&kind.to_le_bytes());
out.extend_from_slice(&count.to_le_bytes());
let v = if tag == 273 { data_offset } else { value };
if kind == 3 {
out.extend_from_slice(&(v as u16).to_le_bytes());
out.extend_from_slice(&[0, 0]);
} else {
out.extend_from_slice(&v.to_le_bytes());
}
}
out.extend_from_slice(&0u32.to_le_bytes());
out.push(0x80);
out
}
/// ICO header claiming three entries that point outside the file with silly sizes.
pub fn bogus_ico() -> Vec<u8> {
let mut out = vec![0, 0, 1, 0, 3, 0];
for i in 0..3u32 {
out.extend_from_slice(&[0, 0, 0, 0, 1, 0, 32, 0]);
out.extend_from_slice(&(0xFFFF_FFF0u32 - i).to_le_bytes()); // size
out.extend_from_slice(&(0x7FFF_0000u32 + i).to_le_bytes()); // offset
}
out.extend_from_slice(&[0xAB; 20]);
out
}
fn bmff_box(kind: &[u8; 4], body: &[u8]) -> Vec<u8> {
let mut out = ((body.len() + 8) as u32).to_be_bytes().to_vec();
out.extend_from_slice(kind);
out.extend_from_slice(body);
out
}
fn full_box(kind: &[u8; 4], version: u8, body: &[u8]) -> Vec<u8> {
let mut b = vec![version, 0, 0, 0];
b.extend_from_slice(body);
bmff_box(kind, &b)
}
/// A syntactically valid HEIF container (ftyp, meta with hdlr/pitm/iinf/iloc/iprp, mdat) holding one
/// `hvc1` item whose HEVC payload is a dummy NAL unit. Written by hand from ISO/IEC 23008-12; the image data
/// itself does not decode, so on a machine with the codecs this still yields a typed error.
pub fn minimal_heic(width: u32, height: u32) -> Vec<u8> {
let mut ftyp = b"heic".to_vec();
ftyp.extend_from_slice(&0u32.to_be_bytes());
ftyp.extend_from_slice(b"mif1heic");
let ftyp = bmff_box(b"ftyp", &ftyp);
let mut hdlr = vec![0; 4];
hdlr.extend_from_slice(b"pict");
hdlr.extend_from_slice(&[0; 12]);
hdlr.push(0);
let hdlr = full_box(b"hdlr", 0, &hdlr);
let pitm = full_box(b"pitm", 0, &1u16.to_be_bytes());
let mut infe = Vec::new();
infe.extend_from_slice(&1u16.to_be_bytes());
infe.extend_from_slice(&0u16.to_be_bytes());
infe.extend_from_slice(b"hvc1");
infe.push(0);
let infe = full_box(b"infe", 2, &infe);
let mut iinf = 1u16.to_be_bytes().to_vec();
iinf.extend_from_slice(&infe);
let iinf = full_box(b"iinf", 0, &iinf);
// hvcC: version 1, Main profile, level 3.0, 4:2:0, 8 bit, 4-byte NAL lengths, no parameter-set arrays.
let hvcc = bmff_box(
b"hvcC",
&[1, 0x01, 0x60, 0, 0, 0, 0x90, 0, 0, 0, 0, 0, 90, 0xF0, 0x00, 0xFC, 0xFD, 0xF8, 0xF8, 0, 0, 0x0F, 0],
);
let mut ispe = width.to_be_bytes().to_vec();
ispe.extend_from_slice(&height.to_be_bytes());
let ispe = full_box(b"ispe", 0, &ispe);
let mut ipco = hvcc;
ipco.extend_from_slice(&ispe);
let ipco = bmff_box(b"ipco", &ipco);
let mut ipma = 1u32.to_be_bytes().to_vec();
ipma.extend_from_slice(&1u16.to_be_bytes());
ipma.extend_from_slice(&[2, 0x81, 0x02]);
let ipma = full_box(b"ipma", 0, &ipma);
let mut iprp = ipco;
iprp.extend_from_slice(&ipma);
let iprp = bmff_box(b"iprp", &iprp);
let payload: Vec<u8> = {
let nal = [0x26, 0x01, 0xAF, 0x13, 0x9C, 0x50, 0x00, 0x00];
let mut p = (nal.len() as u32).to_be_bytes().to_vec();
p.extend_from_slice(&nal);
p
};
// iloc is built twice: once to learn the meta size, once with the real mdat offset.
let build = |offset: u32| {
let mut iloc = vec![0x44, 0x00];
iloc.extend_from_slice(&1u16.to_be_bytes());
iloc.extend_from_slice(&1u16.to_be_bytes());
iloc.extend_from_slice(&0u16.to_be_bytes());
iloc.extend_from_slice(&1u16.to_be_bytes());
iloc.extend_from_slice(&offset.to_be_bytes());
iloc.extend_from_slice(&(payload.len() as u32).to_be_bytes());
let iloc = full_box(b"iloc", 0, &iloc);
let mut meta = Vec::new();
for part in [&hdlr, &pitm, &iinf, &iloc, &iprp] {
meta.extend_from_slice(part);
}
full_box(b"meta", 0, &meta)
};
let meta_len = build(0).len();
let offset = (ftyp.len() + meta_len + 8) as u32;
let meta = build(offset);
let mut out = ftyp;
out.extend_from_slice(&meta);
out.extend_from_slice(&bmff_box(b"mdat", &payload));
out
}