use std::path::PathBuf;
use rust_j2k::{
DecodeOptions, Error, WorkLimit, decode, decode_with, read_header, read_header_with,
};
fn fixture(name: &str) -> Vec<u8> {
let path = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
.join("tests/fixtures/sample_limit")
.join(name);
std::fs::read(&path).unwrap_or_else(|e| panic!("{}: {e}", path.display()))
}
fn flat() -> Vec<u8> {
fixture("flat_16385_tiled_lossless.j2k")
}
fn flat_header_only() -> Vec<u8> {
let mut bytes = flat();
let sot = bytes.windows(2).position(|w| w == [0xFF, 0x90]).unwrap();
bytes.truncate(sot + 2);
bytes
}
fn in_jp2(codestream: &[u8]) -> Vec<u8> {
let bx = |kind: &[u8; 4], payload: &[u8]| {
let mut out = (8 + payload.len() as u32).to_be_bytes().to_vec();
out.extend_from_slice(kind);
out.extend_from_slice(payload);
out
};
let mut ihdr = 16385u32.to_be_bytes().to_vec();
ihdr.extend_from_slice(&16385u32.to_be_bytes());
ihdr.extend_from_slice(&[0, 1, 7, 7, 0, 0]);
let jp2h = [bx(b"ihdr", &ihdr), bx(b"colr", &[1, 0, 0, 0, 0, 0, 17])].concat();
[
vec![0, 0, 0, 12, b'j', b'P', b' ', b' ', 0x0D, 0x0A, 0x87, 0x0A],
bx(b"ftyp", b"jp2 \0\0\0\0jp2 "),
bx(b"jp2h", &jp2h),
bx(b"jp2c", codestream),
]
.concat()
}
fn with_limit(limit: WorkLimit) -> DecodeOptions {
DecodeOptions::default().with_sample_limit(limit)
}
fn picture(c: usize, x: i32, y: i32) -> i32 {
[(x >> 8) * 10, (y >> 8) * 15, ((x >> 9) ^ (y >> 9)) * 30][c] & 255
}
#[test]
fn a_6000x4000_rgb_image_decodes_at_defaults_and_matches_openjpeg() {
let image = decode(&fixture("rgb_6000x4000.jp2")).expect("decodes at defaults");
assert_eq!((image.width, image.height), (6000, 4000));
assert_eq!(image.components.len(), 3);
for (c, component) in image.components.iter().enumerate() {
assert_eq!((component.width, component.height), (6000, 4000));
assert_eq!((component.bit_depth, component.signed), (8, false));
let wrong = (0..4000)
.flat_map(|y| (0..6000).map(move |x| (x, y)))
.zip(&component.samples)
.filter(|&((x, y), &s)| s != picture(c, x, y))
.count();
assert_eq!(wrong, 0, "plane {c}: {wrong} samples differ from OpenJPEG");
}
}
#[test]
fn over_the_default_limit_is_refused_and_the_message_names_the_option() {
for (what, bytes) in [
("header only", flat_header_only()),
("JP2 header only", in_jp2(&flat_header_only())),
("file", flat()),
] {
for result in [decode(&bytes).map(|_| ()), read_header(&bytes).map(|_| ())] {
match result {
Err(Error::Limit(m)) => {
assert!(
m.starts_with("image area 16385x16385 needs 268468225 samples, over the sample limit of 268435456"),
"{what}: {m}"
);
assert!(m.contains("`DecodeOptions::sample_limit`"), "{what}: {m}");
assert!(m.contains("`WorkLimit::Raised(2)`"), "{what}: {m}");
assert!(m.contains("`WorkLimit::Unlimited`"), "{what}: {m}");
}
other => panic!("{what}: {other:?}"),
}
}
}
}
#[test]
fn raised_and_unlimited_admit_an_image_over_the_default() {
let codestream = flat_header_only();
let file = in_jp2(&codestream);
for (bytes, limit) in [
(&codestream, WorkLimit::Raised(2)),
(&codestream, WorkLimit::Unlimited),
(&file, WorkLimit::Raised(2)),
(&file, WorkLimit::Unlimited),
] {
let options = with_limit(limit.clone());
let header = read_header_with(bytes, &options).unwrap_or_else(|e| panic!("{limit:?}: {e}"));
assert_eq!((header.width, header.height), (16385, 16385), "{limit:?}");
match decode_with(bytes, &options) {
Err(Error::Codestream(_)) => {}
other => panic!("{limit:?}: expected the missing tile data, got {other:?}"),
}
}
assert!(matches!(
read_header_with(&file, &with_limit(WorkLimit::Raised(1))),
Err(Error::Limit(_))
));
}
#[test]
fn a_reduced_decode_of_an_image_over_the_limit_is_admitted() {
let bytes = flat();
let reduced = DecodeOptions::default().with_resolution_reduction(3);
let image = decode_with(&bytes, &reduced).expect("fits once reduced");
assert_eq!((image.width, image.height), (2049, 2049));
let [component] = &image.components[..] else {
panic!("{} components", image.components.len());
};
assert_eq!(component.samples.len(), 2049 * 2049);
assert!(component.samples.iter().all(|&s| s == 0));
let header = read_header_with(&bytes, &reduced).expect("header fits once reduced");
assert_eq!((header.width, header.height), (2049, 2049));
let header_only = flat_header_only();
let one = DecodeOptions::default().with_resolution_reduction(1);
assert!(read_header_with(&header_only, &one).is_ok());
assert!(matches!(
decode_with(&header_only, &one),
Err(Error::Codestream(_))
));
}
#[test]
fn a_reduced_image_still_over_the_limit_names_the_reduction() {
let mut bytes = flat_header_only();
assert_eq!(&bytes[2..4], &[0xFF, 0x51]);
for at in [8, 12, 24, 28] {
bytes[at..at + 4].copy_from_slice(&40_000u32.to_be_bytes());
}
let options = DecodeOptions::default().with_resolution_reduction(1);
match read_header_with(&bytes, &options) {
Err(Error::Limit(m)) => assert!(
m.starts_with(
"image area 20000x20000 at resolution reduction 1 needs 400000000 samples"
) && m.contains("`WorkLimit::Raised(2)`"),
"{m}"
),
other => panic!("{other:?}"),
}
}
#[test]
fn a_sample_limit_raised_by_zero_is_invalid_options() {
let options = with_limit(WorkLimit::Raised(0));
for bytes in [flat(), b"not a codestream".to_vec()] {
for result in [
decode_with(&bytes, &options).map(|_| ()),
read_header_with(&bytes, &options).map(|_| ()),
] {
match result {
Err(Error::InvalidOptions(m)) => {
assert_eq!(
m,
"`DecodeOptions::sample_limit` is `WorkLimit::Raised(0)`, but the \
factor must be at least 1"
);
}
other => panic!("{other:?}"),
}
}
}
}
#[test]
fn the_sample_limit_defaults_to_the_default_bound() {
assert_eq!(DecodeOptions::default().sample_limit, WorkLimit::Default);
assert_eq!(
with_limit(WorkLimit::Unlimited).sample_limit,
WorkLimit::Unlimited
);
}
fn segment(marker: u16, body: &[u8]) -> Vec<u8> {
let mut out = marker.to_be_bytes().to_vec();
out.extend_from_slice(&(body.len() as u16 + 2).to_be_bytes());
out.extend_from_slice(body);
out
}
fn main_header(size: u32, tile: u32, components: u16, layers: u16, extra: &[u8]) -> Vec<u8> {
let mut siz = vec![0, 0];
for field in [size, size, 0, 0, tile, tile, 0, 0] {
siz.extend_from_slice(&field.to_be_bytes());
}
siz.extend_from_slice(&components.to_be_bytes());
for _ in 0..components {
siz.extend_from_slice(&[7, 1, 1]);
}
let mut cod = vec![0, 0];
cod.extend_from_slice(&layers.to_be_bytes());
cod.extend_from_slice(&[0, 6, 0, 0, 0, 1]);
let qcd = [&[0x40][..], &[8 << 3; 19]].concat();
[
vec![0xFF, 0x4F],
segment(0xFF51, &siz),
segment(0xFF52, &cod),
segment(0xFF5C, &qcd),
extra.to_vec(),
]
.concat()
}
fn tile_part(index: u32, body: &[u8]) -> Vec<u8> {
let mut sot = (index as u16).to_be_bytes().to_vec();
sot.extend_from_slice(&(14 + body.len() as u32).to_be_bytes());
sot.extend_from_slice(&[0, 1]);
[segment(0xFF90, &sot), vec![0xFF, 0x93], body.to_vec()].concat()
}
fn many_tiles_of_empty_packets(side: u32, tile: u32, components: u16) -> Vec<u8> {
let mut out = main_header(side * tile, tile, components, 1, &[]);
let packets = vec![0u8; 7 * usize::from(components)];
for index in 0..side * side {
out.extend_from_slice(&tile_part(index, &packets));
}
out.extend_from_slice(&[0xFF, 0xD9]);
out
}
#[test]
fn many_tiles_of_geometry_are_held_to_the_packet_work_bound() {
let bytes = many_tiles_of_empty_packets(7, 2800, 1);
assert!(bytes.len() < 2048, "{} bytes", bytes.len());
let reduced = DecodeOptions::default().with_resolution_reduction(6);
let header = read_header_with(&bytes, &reduced).expect("the reduced area fits");
assert_eq!((header.width, header.height), (307, 307));
match decode_with(&bytes, &reduced) {
Err(Error::Limit(m)) => {
assert!(m.contains("`DecodeOptions::packet_work_limit`"), "{m}");
assert!(
m.contains("retry with at least `WorkLimit::Raised(2)`"),
"{m}"
);
}
other => panic!("{:?}", other.map(|i| (i.width, i.height))),
}
let raised = reduced.with_packet_work_limit(WorkLimit::Raised(2));
let image = decode_with(&bytes, &raised).expect("admitted under Raised(2)");
assert_eq!((image.width, image.height), (307, 307));
assert!(image.components[0].samples.iter().all(|&s| s == 128));
}
fn named_factor(message: &str) -> u32 {
let (_, rest) = message
.split_once("`WorkLimit::Raised(")
.unwrap_or_else(|| panic!("no factor named: {message}"));
rest.split_once(')').unwrap().0.parse().unwrap()
}
fn refused_at_r6(bytes: &[u8], limit: WorkLimit) -> String {
let options = DecodeOptions::default()
.with_resolution_reduction(6)
.with_packet_work_limit(limit);
match decode_with(bytes, &options) {
Err(Error::Limit(m)) => m,
other => panic!("{:?}", other.map(|i| (i.width, i.height))),
}
}
#[test]
fn the_dropped_blocks_factor_counts_the_tiles_earlier_components() {
let bytes = many_tiles_of_empty_packets(6, 2000, 2);
let message = refused_at_r6(&bytes, WorkLimit::Default);
assert!(
message.contains("code-blocks in one component of tile"),
"{message}"
);
assert!(
message.contains("steps left of the packet work bound"),
"{message}"
);
assert_eq!(named_factor(&message), 2, "{message}");
let options = DecodeOptions::default()
.with_resolution_reduction(6)
.with_packet_work_limit(WorkLimit::Raised(2));
let image = decode_with(&bytes, &options).expect("admitted under Raised(2)");
assert_eq!(image.components.len(), 2);
}
fn poc_reach_past_the_dropped_blocks() -> Vec<u8> {
let (components, layers) = (37u16, 64770u16);
let mut entry = vec![0, 0];
entry.extend_from_slice(&layers.to_be_bytes());
entry.extend_from_slice(&[7, components as u8, 0]);
let poc = segment(0xFF5F, &[&entry[..], &entry[..]].concat());
let mut out = main_header(64, 64, components, layers, &poc);
out.extend_from_slice(&tile_part(0, &[0; 16]));
out.extend_from_slice(&[0xFF, 0xD9]);
out
}
#[test]
fn the_poc_factor_counts_the_dropped_blocks_charged_before_it() {
let bytes = poc_reach_past_the_dropped_blocks();
let message = refused_at_r6(&bytes, WorkLimit::Default);
assert!(
message.contains("volumes of tile 0 reach 16775430"),
"{message}"
);
assert_eq!(named_factor(&message), 2, "{message}");
let options = DecodeOptions::default()
.with_resolution_reduction(6)
.with_packet_work_limit(WorkLimit::Raised(2));
let result = decode_with(&bytes, &options);
assert!(!matches!(result, Err(Error::Limit(_))), "{result:?}");
}