#[path = "support/alloc.rs"]
mod alloc;
use alloc::{CURRENT, PEAK};
use std::sync::atomic::Ordering;
use rust_j2k::{DecodeOptions, Region, Window, decode_with};
#[global_allocator]
static ALLOCATOR: alloc::PeakAlloc = alloc::PeakAlloc;
#[test]
fn a_small_window_peaks_at_the_tile_it_reads_not_the_image() {
let path = concat!(
env!("CARGO_MANIFEST_DIR"),
"/tests/fixtures/sample_limit/flat_32769x16385_tiled_lossless.j2k"
);
let bytes = std::fs::read(path).expect("read the fixture");
let options =
DecodeOptions::default().with_region(Region::Window(Window::new(1000, 1000, 1256, 1256)));
let before = CURRENT.load(Ordering::SeqCst);
PEAK.store(before, Ordering::SeqCst);
let image = decode_with(&bytes, &options).expect("the window decodes");
let peak = PEAK.load(Ordering::SeqCst) - before;
assert_eq!((image.width, image.height), (256, 256));
let tile = 4096 * 4096 * 4;
let image_bytes = 32769usize * 16385 * 4;
eprintln!(
"peak {peak} bytes: {:.2} times one tile's samples, {:.3} times the image's",
peak as f64 / tile as f64,
peak as f64 / image_bytes as f64
);
assert!(peak < tile * 3 / 2, "peak {peak} bytes");
}