#[path = "support/synth.rs"]
mod synth;
use std::path::PathBuf;
use synth::{bx, seg, tile_part};
use rust_j2k::{
DecodeOptions, Error, LimitDetail, LimitKind, Region, Window, 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_32769x16385_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 ihdr = synth::ihdr_payload(16385, 32769, 1, [7, 7, 0, 0]);
synth::jp2_file(
&[bx(b"ihdr", &ihdr), bx(b"colr", &synth::colr_enum(17))],
&[],
codestream,
&[],
)
}
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");
}
}
#[cfg(target_pointer_width = "64")]
#[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.message().starts_with("image area 32769x16385 needs 536920065 samples, over the sample limit of 536870912"),
"{what}: {m}"
);
assert!(
m.message().contains("`DecodeOptions::sample_limit`"),
"{what}: {m}"
);
assert!(
m.message().contains("`WorkLimit::Raised(2)`"),
"{what}: {m}"
);
assert!(
m.message().contains("`WorkLimit::Unlimited`"),
"{what}: {m}"
);
assert_eq!(m.kind(), LimitKind::Samples, "{what}");
assert_eq!(m.retry_with(), Some(WorkLimit::Raised(2)), "{what}");
}
other => panic!("{what}: {other:?}"),
}
}
}
}
#[cfg(target_pointer_width = "64")]
#[test]
fn a_retry_with_the_typed_hint_admits_the_flat_file() {
let bytes = flat_header_only();
let Err(Error::Limit(detail)) = decode(&bytes) else {
panic!("expected a limit refusal at the default");
};
assert_eq!(detail.kind(), LimitKind::Samples);
let options = DecodeOptions::default()
.with_retry(&detail)
.expect("a retry for the sample limit");
assert_eq!(options.sample_limit, WorkLimit::Raised(2));
let header = read_header_with(&bytes, &options).expect("the header reads under the retry");
assert_eq!((header.width, header.height), (32769, 16385));
assert!(matches!(
decode_with(&bytes, &options),
Err(Error::Codestream(_))
));
}
#[test]
fn a_sentinel_2_true_color_image_is_under_the_default_limit() {
let mut bytes = main_header(10980, 1024, 3, 1, &[]);
bytes.extend_from_slice(&[0xFF, 0x90]);
let header = read_header(&bytes).expect("10980x10980x3 is under the default");
assert_eq!((header.width, header.height), (10980, 10980));
assert_eq!(header.components.len(), 3);
assert!(matches!(decode(&bytes), Err(Error::Codestream(_))));
}
#[cfg(not(target_pointer_width = "64"))]
#[test]
fn over_the_default_limit_is_refused_as_address_space() {
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_eq!(m.kind(), LimitKind::AddressSpace, "{what}: {m}");
assert_eq!(m.retry_with(), None, "{what}");
}
other => panic!("{what}: {other:?}"),
}
}
}
}
#[cfg(target_pointer_width = "64")]
#[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), (32769, 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), (4097, 2049));
let [component] = &image.components[..] else {
panic!("{} components", image.components.len());
};
assert_eq!(component.samples.len(), 4097 * 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), (4097, 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(_))
));
}
#[cfg(target_pointer_width = "64")]
#[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(&50_000u32.to_be_bytes());
}
let options = DecodeOptions::default().with_resolution_reduction(1);
match read_header_with(&bytes, &options) {
Err(Error::Limit(m)) => assert!(
m.message().starts_with(
"image area 25000x25000 at resolution reduction 1 needs 625000000 samples"
) && m.message().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.message(),
"`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 main_header(size: u32, tile: u32, components: u16, layers: u16, extra: &[u8]) -> Vec<u8> {
let siz = synth::siz_body(
[size, size, 0, 0, tile, tile, 0, 0],
&vec![[7, 1, 1]; usize::from(components)],
);
let cod = synth::cod_body(layers, 6, 0, 0);
let qcd = synth::qcd_none_body(2, &[8; 19]);
[
vec![0xFF, 0x4F],
seg(0xFF51, &siz),
seg(0xFF52, &cod),
seg(0xFF5C, &qcd),
extra.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 as u16, 0, 1, &[], &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.message().contains("`DecodeOptions::packet_work_limit`"),
"{m}"
);
assert!(
m.message()
.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(limit: &LimitDetail) -> u32 {
assert_eq!(limit.kind(), LimitKind::PacketWork, "{limit}");
let Some(WorkLimit::Raised(n)) = limit.retry_with() else {
panic!("no factor given: {limit:?}");
};
assert!(
limit
.message()
.contains(&format!("`WorkLimit::Raised({n})`")),
"{limit}"
);
n
}
fn refused_at_r6(bytes: &[u8], limit: WorkLimit) -> LimitDetail {
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
.message()
.contains("code-blocks in one component of tile"),
"{message}"
);
assert!(
message
.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 = seg(0xFF5F, &[&entry[..], &entry[..]].concat());
let mut out = main_header(64, 64, components, layers, &poc);
out.extend_from_slice(&tile_part(0, 0, 1, &[], &[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
.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:?}");
}
fn window(x0: u32, y0: u32, x1: u32, y1: u32) -> DecodeOptions {
DecodeOptions::default().with_region(Region::Window(Window::new(x0, y0, x1, y1)))
}
fn limit<T: std::fmt::Debug>(result: Result<T, Error>) -> String {
match result {
Err(Error::Limit(m)) => m.message().to_owned(),
other => panic!("expected Error::Limit, got {other:?}"),
}
}
#[test]
fn a_small_window_of_an_image_over_the_limit_decodes_at_the_default() {
let bytes = flat();
for (x0, y0, x1, y1) in [
(1000, 1000, 1256, 1256),
(3968, 3968, 4224, 4224),
(32513, 16129, 32769, 16385),
] {
let options = window(x0, y0, x1, y1);
let header = read_header_with(&bytes, &options).expect("the tiles read fit");
assert_eq!((header.width, header.height), (256, 256));
let image = decode_with(&bytes, &options).expect("the tiles read fit");
assert_eq!((image.width, image.height), (256, 256));
let [component] = &image.components[..] else {
panic!("{} components", image.components.len());
};
assert_eq!(component.samples.len(), 256 * 256);
assert!(component.samples.iter().all(|&s| s == 0));
}
}
#[cfg(target_pointer_width = "64")]
#[test]
fn a_window_is_held_to_the_samples_of_the_tiles_it_reads() {
let bytes = flat_header_only();
let thirty_two = window(0, 0, 32768, 16384);
let header = read_header_with(&bytes, &thirty_two).expect("32 tiles fit");
assert_eq!((header.width, header.height), (32768, 16384));
assert!(matches!(
decode_with(&bytes, &thirty_two),
Err(Error::Codestream(_))
));
let all = window(1, 1, 32769, 16385);
for m in [
limit(read_header_with(&bytes, &all)),
limit(decode_with(&bytes, &all)),
] {
assert!(
m.starts_with(
"the area of the tiles read (32769x16385) needs 536920065 samples, over the \
sample limit of 536870912"
),
"{m}"
);
assert!(m.contains("`WorkLimit::Raised(2)`"), "{m}");
}
let m = limit(read_header_with(&bytes, &window(0, 0, 32769, 16384)));
assert!(
m.starts_with("the area of the tiles read (32769x16384) needs 536887296 samples"),
"{m}"
);
let m = limit(decode_with(&bytes, &window(0, 0, 40000, 20000)));
assert!(
m.starts_with("image area 32769x16385 needs 536920065 samples"),
"{m}"
);
let reduced = all.clone().with_resolution_reduction(1);
let header = read_header_with(&bytes, &reduced).expect("fits at reduction 1");
assert_eq!((header.width, header.height), (16384, 8192));
let raised = all.with_sample_limit(WorkLimit::Raised(2));
assert!(read_header_with(&bytes, &raised).is_ok());
}
#[test]
fn a_tile_is_held_to_its_own_samples() {
let bytes = flat();
for (tile, width, height) in [(8, 1, 4096), (44, 1, 1)] {
let options = DecodeOptions::default().with_region(Region::Tile(tile));
let image = decode_with(&bytes, &options).expect("one tile fits");
assert_eq!((image.width, image.height), (width, height), "tile {tile}");
assert!(image.components[0].samples.iter().all(|&s| s == 0));
}
let header_only = flat_header_only();
let first = DecodeOptions::default().with_region(Region::Tile(0));
let header = read_header_with(&header_only, &first).expect("one tile fits");
assert_eq!((header.width, header.height), (4096, 4096));
assert!(matches!(
decode_with(&header_only, &first),
Err(Error::Codestream(_))
));
let mut one_tile = main_header(23171, 23171, 1, 1, &[]);
one_tile.extend_from_slice(&[0xFF, 0x90]);
for m in [
limit(read_header_with(&one_tile, &first)),
limit(decode_with(&one_tile, &first)),
] {
assert!(
m.starts_with("image area 23171x23171 needs 536895241 samples"),
"{m}"
);
}
}
fn with_mct(mut bytes: Vec<u8>) -> Vec<u8> {
let siz_end = 4 + usize::from(u16::from_be_bytes([bytes[4], bytes[5]]));
assert_eq!(bytes[siz_end..siz_end + 2], [0xFF, 0x52]);
bytes[siz_end + 8] = 1;
bytes
}
#[test]
fn a_component_subset_counts_every_component_of_the_tiles_read() {
let tiled = with_mct(many_tiles_of_empty_packets(16, 1024, 3));
for subset in [vec![0], vec![2, 1]] {
let options = window(100, 200, 356, 456).with_components(Some(subset.clone()));
let image = decode_with(&tiled, &options).expect("one tile of three components fits");
assert_eq!(image.components.len(), subset.len());
for component in &image.components {
assert_eq!(component.samples.len(), 256 * 256);
assert!(component.samples.iter().all(|&s| s == 128));
}
}
let subset = DecodeOptions::default().with_components(Some(vec![0]));
let m = limit(decode_with(&tiled, &subset));
assert!(
m.starts_with("a decode of 3 components needs 805306368 samples"),
"{m}"
);
let mut single = with_mct(main_header(16384, 16384, 3, 1, &[]));
single.extend_from_slice(&[0xFF, 0x90]);
let options = window(0, 0, 16384, 16000).with_components(Some(vec![0]));
for m in [
limit(read_header_with(&single, &options)),
limit(decode_with(&single, &options)),
] {
assert!(
m.starts_with("a decode of the tiles read in 3 components needs 805306368 samples"),
"{m}"
);
}
}
fn paletted_jp2(size: u32, 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 = size.to_be_bytes().to_vec();
ihdr.extend_from_slice(&size.to_be_bytes());
ihdr.extend_from_slice(&[0, 1, 7, 7, 0, 0]);
let pclr = [0, 1, 3, 7, 7, 7, 10, 20, 30];
let cmap = [0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 2];
let jp2h = [
bx(b"ihdr", &ihdr),
bx(b"colr", &[1, 0, 0, 0, 0, 0, 16]),
bx(b"pclr", &pclr),
bx(b"cmap", &cmap),
]
.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()
}
#[test]
fn a_palette_under_a_window_counts_the_window() {
let mut codestream = main_header(16384, 1024, 1, 1, &[]);
codestream.extend_from_slice(&[0xFF, 0x90]);
let file = paletted_jp2(16384, &codestream);
let m = limit(read_header_with(&file, &DecodeOptions::default()));
assert!(
m.starts_with("palette expansion to 3 channels needs 805306368 samples"),
"{m}"
);
let header = read_header_with(&file, &window(0, 0, 4096, 4096)).expect("the window fits");
assert_eq!((header.width, header.height), (4096, 4096));
assert_eq!(header.components.len(), 3);
let m = limit(read_header_with(&file, &window(0, 0, 16384, 15000)));
assert!(
m.starts_with("palette expansion to 3 channels needs 737280000 samples"),
"{m}"
);
}