#[path = "support/alloc.rs"]
mod alloc;
#[path = "support/synth.rs"]
mod synth;
use alloc::{CURRENT, PEAK};
use std::sync::atomic::Ordering;
use synth::seg;
use rust_j2k::decode;
#[global_allocator]
static ALLOCATOR: alloc::PeakAlloc = alloc::PeakAlloc;
const SIDE: u32 = 1024;
const LEVELS: u8 = 5;
fn single_tile(irreversible: bool) -> Vec<u8> {
let siz = synth::siz_body([SIDE, SIDE, 0, 0, SIDE, SIDE, 0, 0], &[[7, 1, 1]]);
let bands = 1 + 3 * usize::from(LEVELS);
let qcd = if irreversible {
let mut body = vec![0x42];
for _ in 0..bands {
body.extend_from_slice(&(8u16 << 11).to_be_bytes());
}
body
} else {
let mut body = vec![0x40];
body.extend(std::iter::repeat_n(8 << 3, bands));
body
};
let mut cs = vec![0xFF, 0x4F];
cs.extend(seg(0xFF51, &siz));
cs.extend(seg(
0xFF52,
&[0, 0, 0, 1, 0, LEVELS, 4, 4, 0, u8::from(!irreversible)],
));
cs.extend(seg(0xFF5C, &qcd));
let packets = usize::from(LEVELS) + 1;
let psot = 12 + 2 + packets as u32;
cs.extend_from_slice(&[0xFF, 0x90, 0, 10, 0, 0]);
cs.extend_from_slice(&psot.to_be_bytes());
cs.extend_from_slice(&[0, 1, 0xFF, 0x93]);
cs.extend(std::iter::repeat_n(0, packets));
cs.extend_from_slice(&[0xFF, 0xD9]);
cs
}
fn peak(bytes: &[u8]) -> usize {
let before = CURRENT.load(Ordering::SeqCst);
PEAK.store(before, Ordering::SeqCst);
let image = decode(bytes).expect("the flat image decodes");
let samples = &image.components[0].samples;
assert_eq!(samples.len(), (SIDE * SIDE) as usize);
assert!(
samples.iter().all(|&v| v == 128),
"a flat image at mid-gray"
);
PEAK.load(Ordering::SeqCst) - before
}
#[test]
fn single_tile_decode_peaks_near_twice_the_output() {
let output = (SIDE * SIDE) as usize * size_of::<i32>();
for irreversible in [true, false] {
let peak = peak(&single_tile(irreversible));
let ratio = peak as f64 / output as f64;
assert!(
ratio < 2.1,
"irreversible {irreversible}: peak {peak} bytes is {ratio:.2} times the \
{output}-byte output"
);
}
}