#[path = "support/synth.rs"]
mod synth;
use rust_j2k::{DecodeOptions, Error, Region, Window, decode_with, read_header_with};
use synth::seg;
fn large_header_only() -> Vec<u8> {
header_only(40000)
}
fn header_only(side: u32) -> Vec<u8> {
let siz = synth::siz_body(
[side, side, 100, 200, 24000, 24000, 0, 0],
&[[7, 1, 1], [7, 2, 3]],
);
let cod = synth::cod_body(1, 6, 0, 0);
let qcd = synth::qcd_none_body(2, &[8; 19]);
[
vec![0xFF, 0x4F],
seg(0xFF51, &siz),
seg(0xFF52, &cod),
seg(0xFF5C, &qcd),
vec![0xFF, 0x90],
]
.concat()
}
fn in_jp2(codestream: &[u8]) -> Vec<u8> {
let ihdr = synth::bx(b"ihdr", &synth::ihdr_payload(39800, 39900, 2, [7, 7, 0, 0]));
let colr = synth::bx(b"colr", &synth::colr_enum(17));
synth::jp2_file(&[ihdr, colr], &[], codestream, &[])
}
fn window(x0: u32, y0: u32, x1: u32, y1: u32) -> DecodeOptions {
DecodeOptions::default().with_region(Region::Window(Window::new(x0, y0, x1, y1)))
}
fn empty_for_component_1() -> DecodeOptions {
window(16999, 16999, 17001, 17001)
}
fn assert_invalid_options<T: std::fmt::Debug>(result: Result<T, Error>) {
match result {
Err(Error::InvalidOptions(_)) => {}
other => panic!("expected Error::InvalidOptions, got {other:?}"),
}
}
#[test]
fn read_header_with_reports_an_invalid_window_before_the_limit() {
assert_invalid_options(read_header_with(
&large_header_only(),
&empty_for_component_1(),
));
}
#[test]
fn decode_with_reports_an_invalid_window_before_the_limit() {
assert_invalid_options(decode_with(&large_header_only(), &empty_for_component_1()));
}
#[test]
fn a_jp2_file_reports_an_invalid_window_before_the_limit() {
let bytes = in_jp2(&large_header_only());
assert_invalid_options(read_header_with(&bytes, &empty_for_component_1()));
assert_invalid_options(decode_with(&bytes, &empty_for_component_1()));
}
#[test]
fn a_valid_window_of_the_same_image_is_still_held_to_the_tiles_read() {
let codestream = large_header_only();
let jp2 = in_jp2(&codestream);
let options = window(200, 300, 300, 400);
for bytes in [&codestream, &jp2] {
for result in [
read_header_with(bytes, &options).map(drop),
decode_with(bytes, &options).map(drop),
] {
match result {
Err(Error::Limit(m)) => {
assert!(
m.message()
.contains("the area of the tiles read (23900x23800"),
"{m}"
)
}
other => panic!("expected Error::Limit, got {other:?}"),
}
}
}
}
fn in_paletted_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 = 39800u32.to_be_bytes().to_vec();
ihdr.extend_from_slice(&39900u32.to_be_bytes());
ihdr.extend_from_slice(&2u16.to_be_bytes());
ihdr.extend_from_slice(&[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, 0, 1, 0, 0];
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_paletted_jp2_file_reports_an_invalid_window_before_the_limit() {
let bytes = in_paletted_jp2(&large_header_only());
assert_invalid_options(read_header_with(&bytes, &empty_for_component_1()));
assert_invalid_options(decode_with(&bytes, &empty_for_component_1()));
let srgb = empty_for_component_1().with_color(rust_j2k::ColorOutput::Srgb);
assert_invalid_options(read_header_with(&bytes, &srgb));
assert_invalid_options(decode_with(&bytes, &srgb));
}
fn jp2_with(side: u32, boxes: &[Vec<u8>]) -> Vec<u8> {
let mut ihdr = (side - 200).to_be_bytes().to_vec();
ihdr.extend_from_slice(&(side - 100).to_be_bytes());
ihdr.extend_from_slice(&2u16.to_be_bytes());
ihdr.extend_from_slice(&[7, 7, 0, 0]);
let jp2h = [
bx(b"ihdr", &ihdr),
bx(b"colr", &[1, 0, 0, 0, 0, 0, 17]),
boxes.concat(),
]
.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", &header_only(side)),
]
.concat()
}
fn bx(kind: &[u8; 4], payload: &[u8]) -> Vec<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
}
fn assert_file_format<T: std::fmt::Debug>(result: Result<T, Error>) {
match result {
Err(Error::FileFormat(_)) => {}
other => panic!("expected Error::FileFormat, got {other:?}"),
}
}
fn two_cdefs() -> Vec<Vec<u8>> {
let cdef = bx(b"cdef", &[0, 2, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 2]);
vec![cdef.clone(), cdef]
}
#[test]
fn a_fault_of_the_file_comes_before_a_refused_window_on_any_size() {
for side in [1000, 40000] {
let bytes = jp2_with(side, &two_cdefs());
assert_file_format(read_header_with(&bytes, &empty_for_component_1()));
assert_file_format(decode_with(&bytes, &empty_for_component_1()));
}
}
#[test]
fn a_fault_of_a_paletted_file_comes_before_a_refused_window_on_any_size() {
let pclr = bx(b"pclr", &[0, 1, 3, 7, 7, 7, 10, 20, 30]);
let cmap = bx(b"cmap", &[0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 2, 0, 1, 0, 0]);
for side in [1000, 40000] {
let mut boxes = vec![pclr.clone(), cmap.clone()];
boxes.extend(two_cdefs());
let bytes = jp2_with(side, &boxes);
assert_file_format(read_header_with(&bytes, &empty_for_component_1()));
assert_file_format(decode_with(&bytes, &empty_for_component_1()));
}
}
#[test]
fn a_missing_tile_or_component_is_invalid_options_on_a_large_image() {
let codestream = large_header_only();
let jp2 = in_jp2(&codestream);
let tile = DecodeOptions::default().with_region(Region::Tile(99));
let component = DecodeOptions::default().with_components(Some(vec![5]));
for bytes in [&codestream, &jp2] {
for options in [&tile, &component] {
assert_invalid_options(read_header_with(bytes, options));
assert_invalid_options(decode_with(bytes, options));
}
}
}