mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 15:55:14 +00:00
exr: integrate mixed deep/flat decoding with regression coverage (#1853)
* EXR: decode a flat part of a file that also holds deep parts decode_part refused every part of a file with any deep part, and decode_display_part, which checked only the selected part, still failed because the exr crate's image reader validates every header and refuses deep ones before it reads a chunk. Both now decide from the selected part's own header. When any part is deep, the selected flat part is read chunk by chunk: the flat headers are validated, every part's offset table is walked from the end of the headers, and only the selected table's chunks are read. A chunk must start past the last offset table and inside the file, carry the selected part's number and hold a block no other chunk of the part holds; anything else is Malformed. Files with only flat parts keep the crate's reader. The file-size ratio check, which was skipped for every part when any part was deep, now applies to the flat parts of such a file; deep parts are not counted. * EXR: a flat part beside a deep part needs a chunk for each of its blocks The chunk reader for a flat part of a file that also holds deep parts refused only a second chunk for the same block. A tiled part's chunk that named a level the part does not have was skipped without decompressing, so the tile it took the place of had no chunk and the part decoded with that tile left at zero. Each chunk now has to hold one of the part's own blocks, taken from the header, that no other chunk holds; anything else is Malformed. Since a part has as many chunks as blocks, every block then has a chunk. The mixed-file tests also run with a tiled flat part, and a new test names a missing level and a repeated tile. * EXR: a scan-line chunk beside a deep part must start at its block's first line The block index of a scan-line chunk is its y offset divided by the lines a block holds, rounded toward zero. A chunk whose y was not the first line of a block, or lay up to 15 lines above the data window of a ZIP part, still named a block and was decoded into it. Such a chunk is now Malformed. * EXR: a block too large to address is LimitExceeded instead of a panic The exr crate bounds a flat chunk by a block's pixels times the bytes a pixel takes, an unchecked product, before it reads the chunk. A 1 x 1 part with five float channels in 10^9 x 10^9 tiles passed every size limit, and decode_part and decode_display_part then panicked with an overflow, for files of flat parts only and for mixed files alike. read_meta now computes the product with checked arithmetic for each flat part and returns LimitExceeded when it does not fit in a usize. * EXR: a chunk beside a deep part must fit the rest of the file The exr crate allocates a chunk's declared payload, up to the size of a block, before it reads it. In a mixed file whose flat part has large tiles, a chunk declaring i32::MAX bytes made a 2 GiB allocation from a file of a few hundred bytes. read_flat_part_chunks now reads the declared length first and returns Malformed when it is longer than the bytes left in the file. * test(exr): verify mixed-part ratio and display block budgets * fix(exr): bound mixed-part payload and block bookkeeping allocations * fix(exr): expose internal ordinal helpers to regression tests --------- Co-authored-by: dotCooCoo <RobertLeeLW@gmail.com>
This commit is contained in:
1 parent
8e03cd016d
commit
a62de6c7c3
2 files changed
+700
-37
No files matched your search
+250
-36
@@ -7,7 +7,7 @@
|
||||
//! - [`decode_part`] decodes the channels of one flat part that a filter keeps, in their stored
|
||||
//! sample type, over the part's data window. Every compression the `exr` crate reads is read,
|
||||
//! scan lines and tiles alike (the largest resolution level). Other parts' chunks are skipped
|
||||
//! unread, and channels the filter drops are never allocated.
|
||||
//! unread, and channels the filter drops are never allocated. Other parts may be deep.
|
||||
//! - [`write`] writes one scan-line part.
|
||||
//!
|
||||
//! The `exr` crate sizes buffers from headers before it reads pixels, so everything that reaches
|
||||
@@ -18,7 +18,7 @@
|
||||
use std::io::Cursor;
|
||||
|
||||
use exr::block::UncompressedBlock;
|
||||
use exr::block::chunk::TileCoordinates;
|
||||
use exr::block::chunk::{Chunk, CompressedBlock, CompressedScanLineBlock, CompressedTileBlock, TileCoordinates};
|
||||
use exr::image::read::image::ReadImage;
|
||||
use exr::image::read::layers::{ChannelsReader, ReadAllLayers, ReadChannels};
|
||||
use exr::meta::MetaData;
|
||||
@@ -245,11 +245,21 @@ fn window_size(size: exr::math::Vec2<usize>) -> (u32, u32) {
|
||||
|
||||
/// Reads the headers and checks every part's declared size, alone and together, before any pixel
|
||||
/// allocation. Headers are read without the crate's validation, which refuses deep parts.
|
||||
fn read_meta(bytes: &[u8], limits: &Limits) -> Result<MetaData> {
|
||||
pub(super) fn read_meta(bytes: &[u8], limits: &Limits) -> Result<MetaData> {
|
||||
check_previews(bytes)?;
|
||||
let meta = MetaData::read_from_buffered(Cursor::new(bytes), false).map_err(map_exr)?;
|
||||
let (mut total, mut dwa) = (0u64, 0u64);
|
||||
for header in meta.headers.iter() {
|
||||
let (mut total, mut flat, mut dwa) = (0u64, 0u64, 0u64);
|
||||
for (index, header) in meta.headers.iter().enumerate() {
|
||||
// The crate sizes a flat chunk's bound as a block's pixels times the bytes a pixel takes,
|
||||
// without checking the product, before it reads the chunk.
|
||||
let block_pixels = match header.blocks {
|
||||
exr::meta::BlockDescription::Tiles(tiles) => (tiles.tile_size.0 as u64).checked_mul(tiles.tile_size.1 as u64),
|
||||
exr::meta::BlockDescription::ScanLines => (header.compression.scan_lines_per_block() as u64).checked_mul(header.layer_size.0 as u64),
|
||||
};
|
||||
let block_bytes = block_pixels.and_then(|n| n.checked_mul(header.channels.bytes_per_pixel as u64)).and_then(|n| usize::try_from(n).ok());
|
||||
if !header.deep && block_bytes.is_none() {
|
||||
return Err(Error::LimitExceeded(format!("a block of part {index} holds more bytes than this platform can address")));
|
||||
}
|
||||
let (w, h) = window_size(header.layer_size);
|
||||
if w > limits.max_width || h > limits.max_height {
|
||||
return Err(Error::LimitExceeded(format!("a {w}x{h} part exceeds {}x{}", limits.max_width, limits.max_height)));
|
||||
@@ -260,23 +270,24 @@ fn read_meta(bytes: &[u8], limits: &Limits) -> Result<MetaData> {
|
||||
}
|
||||
let bytes = pixels.saturating_mul(header.channels.list.len().max(1) as u64 * 4);
|
||||
total = total.saturating_add(bytes);
|
||||
if matches!(header.compression, ExrCompression::DWAA(_) | ExrCompression::DWAB(_)) {
|
||||
dwa = dwa.saturating_add(bytes);
|
||||
if !header.deep {
|
||||
flat = flat.saturating_add(bytes);
|
||||
if matches!(header.compression, ExrCompression::DWAA(_) | ExrCompression::DWAB(_)) {
|
||||
dwa = dwa.saturating_add(bytes);
|
||||
}
|
||||
}
|
||||
}
|
||||
if total > limits.max_bytes {
|
||||
return Err(Error::LimitExceeded(format!("{total} bytes across {} parts exceed {}", meta.headers.len(), limits.max_bytes)));
|
||||
}
|
||||
// The crate allocates a flat part's samples before reading its chunks, so the file must be
|
||||
// able to hold them. DWA's run-length channels are zlib-packed on top and reach about
|
||||
// 132000:1, so DWA parts are counted at a quarter. Deep parts size their samples from the
|
||||
// file's own counts.
|
||||
if !meta.requirements.has_deep_data {
|
||||
let declared = total.saturating_sub(dwa).saturating_add(dwa / 4);
|
||||
let capacity = (bytes.len() as u64).saturating_mul(limits.max_ratio).saturating_add(1 << 20);
|
||||
if declared > capacity {
|
||||
return Err(Error::LimitExceeded(format!("a {} byte file cannot hold {declared} bytes of samples", bytes.len())));
|
||||
}
|
||||
// A flat part's samples are allocated before its chunks are read, so the file must be able
|
||||
// to hold the flat parts' samples. DWA's run-length channels are zlib-packed on top and reach
|
||||
// about 132000:1, so DWA parts are counted at a quarter. Deep parts size their samples from
|
||||
// the file's own counts and are not counted here.
|
||||
let declared = flat.saturating_sub(dwa).saturating_add(dwa / 4);
|
||||
let capacity = (bytes.len() as u64).saturating_mul(limits.max_ratio).saturating_add(1 << 20);
|
||||
if declared > capacity {
|
||||
return Err(Error::LimitExceeded(format!("a {} byte file cannot hold {declared} bytes of samples", bytes.len())));
|
||||
}
|
||||
Ok(meta)
|
||||
}
|
||||
@@ -459,19 +470,24 @@ pub fn decode_display_part(
|
||||
}
|
||||
let block_limit = std::cell::Cell::new(None);
|
||||
let select = DisplaySelect { index, keep, fill, limits, seen: std::cell::Cell::new(0), block_limit: &block_limit };
|
||||
let image = ReadImage::new(ReadAllLayers { read_channels: select }, |_| {})
|
||||
.from_buffered(Cursor::new(bytes))
|
||||
.map_err(|error| block_limit.take().map(Error::LimitExceeded).unwrap_or_else(|| map_exr(error)))?;
|
||||
let channels = image
|
||||
.layer_data
|
||||
.into_iter()
|
||||
.nth(index)
|
||||
.ok_or_else(|| malformed("the part was not read"))?
|
||||
.channel_data
|
||||
.into_iter()
|
||||
.flatten()
|
||||
.map(|(name, samples)| DisplayChannel { name, samples })
|
||||
.collect();
|
||||
let exr_error = |error| block_limit.take().map(Error::LimitExceeded).unwrap_or_else(|| map_exr(error));
|
||||
let planes = if meta.headers.iter().any(|header| header.deep) {
|
||||
let header = meta.headers.get(index).ok_or_else(|| malformed(format!("part {index}")))?;
|
||||
select.seen.set(index);
|
||||
let mut reader = select.create_channels_reader(header).map_err(exr_error)?;
|
||||
read_flat_part_chunks(bytes, &meta, index, &mut reader, limits)?;
|
||||
reader.into_channels()
|
||||
} else {
|
||||
ReadImage::new(ReadAllLayers { read_channels: select }, |_| {})
|
||||
.from_buffered(Cursor::new(bytes))
|
||||
.map_err(exr_error)?
|
||||
.layer_data
|
||||
.into_iter()
|
||||
.nth(index)
|
||||
.ok_or_else(|| malformed("the part was not read"))?
|
||||
.channel_data
|
||||
};
|
||||
let channels = planes.into_iter().flatten().map(|(name, samples)| DisplayChannel { name, samples }).collect();
|
||||
Ok(DisplayPart { info, channels })
|
||||
}
|
||||
|
||||
@@ -732,22 +748,220 @@ impl ChannelsReader for SelectReader {
|
||||
}
|
||||
}
|
||||
|
||||
/// Where the headers end and the offset tables begin: [`MetaData::read_from_buffered`] consumes
|
||||
/// exactly the version field and the headers.
|
||||
fn tables_start(bytes: &[u8]) -> Result<u64> {
|
||||
let mut cursor = Cursor::new(bytes);
|
||||
MetaData::read_from_buffered(&mut cursor, false).map_err(map_exr)?;
|
||||
Ok(cursor.position())
|
||||
}
|
||||
|
||||
/// Stable dense ordinal without materializing every tile. The library's block iterator
|
||||
/// internally allocates a Vec of all coordinates, which is unsuitable for cropped reads.
|
||||
pub(super) fn flat_block_ordinal(header: &Header, tile: TileCoordinates) -> Result<usize> {
|
||||
use exr::math::Vec2;
|
||||
use exr::meta::attribute::LevelMode;
|
||||
let mut start = 0usize;
|
||||
let tile_size = header.max_block_pixel_size();
|
||||
if tile_size.0 == 0 || tile_size.1 == 0 {
|
||||
return Err(malformed("flat block has a zero dimension"));
|
||||
}
|
||||
let mut visit = |level: Vec2<usize>, size: Vec2<usize>| -> Result<Option<usize>> {
|
||||
let nx = size.0.div_ceil(tile_size.0);
|
||||
let ny = size.1.div_ceil(tile_size.1);
|
||||
if tile.level_index == level {
|
||||
if tile.tile_index.0 >= nx || tile.tile_index.1 >= ny {
|
||||
return Err(malformed("flat tile lies outside its level"));
|
||||
}
|
||||
return tile
|
||||
.tile_index
|
||||
.1
|
||||
.checked_mul(nx)
|
||||
.and_then(|n| n.checked_add(tile.tile_index.0))
|
||||
.and_then(|n| start.checked_add(n))
|
||||
.map(Some)
|
||||
.ok_or_else(|| malformed("block ordinal overflow"));
|
||||
}
|
||||
start = nx.checked_mul(ny).and_then(|n| start.checked_add(n)).ok_or_else(|| malformed("block count overflow"))?;
|
||||
Ok(None)
|
||||
};
|
||||
match header.blocks {
|
||||
exr::meta::BlockDescription::Tiles(tiles) => match tiles.level_mode {
|
||||
LevelMode::Singular => {
|
||||
if let Some(i) = visit(Vec2(0, 0), header.layer_size)? {
|
||||
return Ok(i);
|
||||
}
|
||||
}
|
||||
LevelMode::MipMap => {
|
||||
for (level, size) in exr::meta::mip_map_levels(tiles.rounding_mode, header.layer_size) {
|
||||
if let Some(i) = visit(Vec2(level, level), size)? {
|
||||
return Ok(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
LevelMode::RipMap => {
|
||||
for (level, size) in exr::meta::rip_map_levels(tiles.rounding_mode, header.layer_size) {
|
||||
if let Some(i) = visit(level, size)? {
|
||||
return Ok(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
exr::meta::BlockDescription::ScanLines => {
|
||||
if let Some(i) = visit(Vec2(0, 0), header.layer_size)? {
|
||||
return Ok(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(malformed("flat tile names a missing level"))
|
||||
}
|
||||
|
||||
/// Reads the chunks of flat part `index` of a file that also holds deep parts into `reader`.
|
||||
///
|
||||
/// The `exr` crate's image reader validates every header before it reads a chunk and refuses
|
||||
/// deep headers, so here only the flat headers are validated, the offset tables (one `u64` per
|
||||
/// chunk, a table per part in header order) are walked from the end of the headers, and only the
|
||||
/// selected part's chunks are read. Each chunk must start past the last offset table and inside
|
||||
/// the file, carry the selected part's number, and hold one of the part's blocks that no other
|
||||
/// chunk of the part holds, so the part's chunks hold every one of its blocks. A scan-line
|
||||
/// chunk's y must be the first line of its block, and no chunk may declare more bytes than the
|
||||
/// file has left after it.
|
||||
fn read_flat_part_chunks(bytes: &[u8], meta: &MetaData, index: usize, reader: &mut impl ChannelsReader, limits: &Limits) -> Result<()> {
|
||||
let header = meta.headers.get(index).ok_or_else(|| malformed(format!("part {index}")))?;
|
||||
let multipart = meta.requirements.is_multilayer();
|
||||
let mut long_names = false;
|
||||
for flat in meta.headers.iter().filter(|h| !h.deep) {
|
||||
flat.validate(multipart, &mut long_names, false).map_err(map_exr)?;
|
||||
}
|
||||
let truncated = || malformed("the offset tables run past the end of the file");
|
||||
let (mut table, mut tables_end) = (0u64, tables_start(bytes)?);
|
||||
for (i, h) in meta.headers.iter().enumerate() {
|
||||
if i == index {
|
||||
table = tables_end;
|
||||
}
|
||||
let len = u64::try_from(h.chunk_count).ok().and_then(|n| n.checked_mul(8)).ok_or_else(truncated)?;
|
||||
tables_end = tables_end.checked_add(len).filter(|end| *end <= bytes.len() as u64).ok_or_else(truncated)?;
|
||||
}
|
||||
let offsets = usize::try_from(table)
|
||||
.ok()
|
||||
.and_then(|start| bytes.get(start..start.checked_add(header.chunk_count.checked_mul(8)?)?))
|
||||
.ok_or_else(truncated)?;
|
||||
// One bit per block, not a Vec/HashSet of every coordinate. A cropped display must not
|
||||
// permit unbounded bookkeeping for an enormous data window of tiny tiles.
|
||||
let tracking_bytes = header.chunk_count.div_ceil(8);
|
||||
if tracking_bytes as u64 > limits.max_bytes || tracking_bytes > isize::MAX as usize {
|
||||
return Err(Error::LimitExceeded("block tracking exceeds the byte budget".into()));
|
||||
}
|
||||
let mut seen = Vec::new();
|
||||
seen.try_reserve_exact(tracking_bytes).map_err(|_| Error::LimitExceeded("cannot allocate block tracking".into()))?;
|
||||
seen.resize(tracking_bytes, 0u8);
|
||||
for &entry in offsets.as_chunks::<8>().0 {
|
||||
let offset = u64::from_le_bytes(entry);
|
||||
let chunk = usize::try_from(offset)
|
||||
.ok()
|
||||
.filter(|_| offset >= tables_end)
|
||||
.and_then(|start| bytes.get(start..))
|
||||
.filter(|rest| !rest.is_empty())
|
||||
.ok_or_else(|| malformed(format!("chunk offset {offset} is outside the chunk area of the file")))?;
|
||||
if multipart {
|
||||
let part = chunk.get(..4).and_then(|b| <[u8; 4]>::try_from(b).ok()).map(i32::from_le_bytes);
|
||||
let part = part.ok_or_else(|| malformed(format!("the chunk at offset {offset} is cut off")))?;
|
||||
if usize::try_from(part).ok() != Some(index) {
|
||||
return Err(malformed(format!("the chunk at offset {offset} belongs to part {part}, not part {index}")));
|
||||
}
|
||||
}
|
||||
// The crate allocates a chunk's declared payload before it reads it, so the payload must
|
||||
// fit in the rest of the file. The length follows the part number and the block's y, or
|
||||
// its four tile coordinates.
|
||||
let fields = if matches!(header.blocks, exr::meta::BlockDescription::Tiles(_)) { 16 } else { 4 };
|
||||
let size_at = if multipart { 4 } else { 0 } + fields;
|
||||
let size = chunk.get(size_at..size_at + 4).and_then(|b| <[u8; 4]>::try_from(b).ok()).map(i32::from_le_bytes);
|
||||
let size = size.ok_or_else(|| malformed(format!("the chunk at offset {offset} is cut off")))?;
|
||||
let left = chunk.len() - (size_at + 4);
|
||||
if usize::try_from(size).is_ok_and(|size| size > left) {
|
||||
return Err(malformed(format!("the chunk at offset {offset} declares {size} bytes, but {left} remain in the file")));
|
||||
}
|
||||
let size = usize::try_from(size).map_err(|_| malformed("negative packed chunk length"))?;
|
||||
// Read coordinates without allocating the payload. Validate their real clipped block
|
||||
// and budgets before Chunk::read uses the nominal (possibly huge) tile dimensions.
|
||||
let coordinate_at = if multipart { 4 } else { 0 };
|
||||
let shape = if matches!(header.blocks, exr::meta::BlockDescription::Tiles(_)) {
|
||||
let coordinates = TileCoordinates::read(&mut Cursor::new(&chunk[coordinate_at..])).map_err(map_exr)?;
|
||||
CompressedBlock::Tile(CompressedTileBlock { coordinates, compressed_pixels_le: Vec::new() })
|
||||
} else {
|
||||
let y = chunk
|
||||
.get(coordinate_at..coordinate_at + 4)
|
||||
.and_then(|b| <[u8; 4]>::try_from(b).ok())
|
||||
.ok_or_else(|| malformed("truncated scan-line coordinate"))?;
|
||||
CompressedBlock::ScanLine(CompressedScanLineBlock { y_coordinate: i32::from_le_bytes(y), compressed_pixels_le: Vec::new() })
|
||||
};
|
||||
let tile = header.get_block_data_indices(&shape).map_err(map_exr)?;
|
||||
if let CompressedBlock::ScanLine(block) = &shape {
|
||||
// The block index is rounded toward zero, so a y that is not the first line of a
|
||||
// block, or lies just above the data window, would still name a block.
|
||||
let lines = header.compression.scan_lines_per_block() as i64;
|
||||
let first = i64::from(header.own_attributes.layer_position.y()) + tile.tile_index.y() as i64 * lines;
|
||||
if i64::from(block.y_coordinate) != first {
|
||||
return Err(malformed(format!(
|
||||
"the scan-line chunk at offset {offset} starts at y {}, which is not the first line of a block",
|
||||
block.y_coordinate
|
||||
)));
|
||||
}
|
||||
}
|
||||
let ordinal = flat_block_ordinal(header, tile)?;
|
||||
let flag = 1u8 << (ordinal % 8);
|
||||
let byte = seen.get_mut(ordinal / 8).ok_or_else(|| malformed("block is outside its chunk table"))?;
|
||||
if *byte & flag != 0 {
|
||||
return Err(malformed("duplicate flat block"));
|
||||
}
|
||||
*byte |= flag;
|
||||
if reader.filter_block(tile) {
|
||||
let area = header.get_absolute_block_pixel_coordinates(tile).map_err(map_exr)?.size;
|
||||
let raw_size = area
|
||||
.0
|
||||
.checked_mul(area.1)
|
||||
.and_then(|n| n.checked_mul(header.channels.bytes_per_pixel))
|
||||
.ok_or_else(|| Error::LimitExceeded("flat block byte size overflows".into()))?;
|
||||
if raw_size as u64 > limits.max_bytes || raw_size > isize::MAX as usize {
|
||||
return Err(Error::LimitExceeded("flat block exceeds the byte budget".into()));
|
||||
}
|
||||
// Packed size need not be less than raw size for every accepted compression.
|
||||
// Bound its allocation independently, rather than rejecting valid compressed data
|
||||
// solely because a clipped edge block has fewer raw bytes.
|
||||
if size as u64 > limits.max_bytes || size > isize::MAX as usize {
|
||||
return Err(Error::LimitExceeded("packed flat block exceeds the byte budget".into()));
|
||||
}
|
||||
let chunk = Chunk::read(&mut Cursor::new(chunk), meta).map_err(map_exr)?;
|
||||
let block = UncompressedBlock::decompress_chunk(chunk, meta, false).map_err(map_exr)?;
|
||||
reader.read_block(header, block).map_err(map_exr)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Decodes the channels of flat part `index` that `keep` accepts (by the name as stored), in
|
||||
/// their stored type. A file with deep parts, or a kept channel that is subsampled, is
|
||||
/// [`Error::Unsupported`].
|
||||
/// their stored type. A deep part, or a kept channel that is subsampled, is
|
||||
/// [`Error::Unsupported`]; the file's other parts may be deep.
|
||||
pub fn decode_part(bytes: &[u8], index: usize, keep: &dyn Fn(&str) -> bool, limits: &Limits) -> Result<Part> {
|
||||
let meta = read_meta(bytes, limits)?;
|
||||
if meta.requirements.has_deep_data {
|
||||
let header = meta.headers.get(index).ok_or_else(|| malformed(format!("part {index} of {}", meta.headers.len())))?;
|
||||
if header.deep {
|
||||
return Err(Error::Unsupported("deep data (use the deep reader)".into()));
|
||||
}
|
||||
let header = meta.headers.get(index).ok_or_else(|| malformed(format!("part {index} of {}", meta.headers.len())))?;
|
||||
if matches!(header.compression, ExrCompression::HTJ2K32 | ExrCompression::HTJ2K256) {
|
||||
return Err(Error::Unsupported("HTJ2K compression".into()));
|
||||
}
|
||||
let info = part_info(index, header);
|
||||
let select = Select { index, keep, seen: std::cell::Cell::new(0) };
|
||||
let image = ReadImage::new(ReadAllLayers { read_channels: select }, |_| {}).from_buffered(Cursor::new(bytes)).map_err(map_exr)?;
|
||||
let planes = image.layer_data.into_iter().nth(index).map(|l| l.channel_data).ok_or_else(|| malformed("the part was not read"))?;
|
||||
let planes = if meta.headers.iter().any(|header| header.deep) {
|
||||
select.seen.set(index);
|
||||
let mut reader = select.create_channels_reader(header).map_err(map_exr)?;
|
||||
read_flat_part_chunks(bytes, &meta, index, &mut reader, limits)?;
|
||||
reader.into_channels()
|
||||
} else {
|
||||
let image = ReadImage::new(ReadAllLayers { read_channels: select }, |_| {}).from_buffered(Cursor::new(bytes)).map_err(map_exr)?;
|
||||
image.layer_data.into_iter().nth(index).map(|l| l.channel_data).ok_or_else(|| malformed("the part was not read"))?
|
||||
};
|
||||
let channels = info.channels.iter().zip(planes).filter_map(|(c, p)| Some(Channel { name: c.name.clone(), samples: p? })).collect();
|
||||
Ok(Part { info, channels })
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
//! The preview check is PhotoCraft's test; the rest builds files with the `exr` crate.
|
||||
//! The preview check is PhotoCraft's test; the rest builds files with the `exr` crate, except the
|
||||
//! files that mix flat and deep parts, which are built by hand.
|
||||
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
|
||||
|
||||
use exr::meta::attribute::{Preview, Text};
|
||||
@@ -258,3 +259,451 @@ fn hostile_files_never_panic() {
|
||||
}
|
||||
assert!(parts(b"not exr", &limits).is_err());
|
||||
}
|
||||
|
||||
// A multi-part file with a flat part and a deep scan-line part, built by hand per the OpenEXR file
|
||||
// layout (the `exr` crate's writer refuses deep headers): version 2 with the deep and multi-part
|
||||
// flags, the headers and an empty header, one offset table per part, then chunks that each start
|
||||
// with their part number. Both parts are uncompressed, two chunks a part: the deep part and a
|
||||
// scan-line flat part hold one scan line a chunk, and a tiled flat part one 2 × 2 tile a chunk.
|
||||
|
||||
fn mixed_attr(out: &mut Vec<u8>, name: &str, ty: &str, payload: &[u8]) {
|
||||
out.extend_from_slice(name.as_bytes());
|
||||
out.push(0);
|
||||
out.extend_from_slice(ty.as_bytes());
|
||||
out.push(0);
|
||||
out.extend_from_slice(&(payload.len() as i32).to_le_bytes());
|
||||
out.extend_from_slice(payload);
|
||||
}
|
||||
|
||||
/// The version field and the headers of a mixed file whose flat part is `w × h` with float
|
||||
/// `B`, `G`, `R` channels, in scan lines or (with `tiled`) in one level of 2 × 2 tiles, and whose
|
||||
/// deep part is 3 × 2 with one float `A` channel. Both parts are uncompressed.
|
||||
fn mixed_headers(deep_first: bool, tiled: bool, (w, h): (usize, usize)) -> Vec<u8> {
|
||||
mixed_headers_with(deep_first, tiled, (w, h), 0, &["B", "G", "R"], 2)
|
||||
}
|
||||
|
||||
/// [`mixed_headers`] with the flat part's compression code (0 for none, or 3 for ZIP, which
|
||||
/// holds 16 scan lines a block), its float channels, and the width and height of its tiles.
|
||||
fn mixed_headers_with(deep_first: bool, tiled: bool, (w, h): (usize, usize), compression: u8, flat_channels: &[&str], tile: u32) -> Vec<u8> {
|
||||
mixed_headers_with_deep_size(deep_first, tiled, (w, h), compression, flat_channels, tile, (3, 2))
|
||||
}
|
||||
|
||||
fn mixed_headers_with_deep_size(
|
||||
deep_first: bool,
|
||||
tiled: bool,
|
||||
(w, h): (usize, usize),
|
||||
compression: u8,
|
||||
flat_channels: &[&str],
|
||||
tile: u32,
|
||||
deep_size: (usize, usize),
|
||||
) -> Vec<u8> {
|
||||
let mut out = Vec::new();
|
||||
out.extend_from_slice(&0x0131_2f76u32.to_le_bytes());
|
||||
out.extend_from_slice(&(2u32 | 0x800 | 0x1000).to_le_bytes());
|
||||
for deep in if deep_first { [true, false] } else { [false, true] } {
|
||||
let (w, h, channels): (usize, usize, &[&str]) = if deep { (deep_size.0, deep_size.1, &["A"]) } else { (w, h, flat_channels) };
|
||||
let mut chlist = Vec::new();
|
||||
for name in channels {
|
||||
chlist.extend_from_slice(name.as_bytes());
|
||||
chlist.push(0);
|
||||
chlist.extend_from_slice(&2i32.to_le_bytes()); // float
|
||||
chlist.extend_from_slice(&[0u8; 4]); // pLinear and reserved
|
||||
chlist.extend_from_slice(&1i32.to_le_bytes());
|
||||
chlist.extend_from_slice(&1i32.to_le_bytes());
|
||||
}
|
||||
chlist.push(0);
|
||||
let window: Vec<u8> = [0i32, 0, w as i32 - 1, h as i32 - 1].iter().flat_map(|v| v.to_le_bytes()).collect();
|
||||
let tiled = tiled && !deep;
|
||||
let compression = if deep { 0 } else { compression };
|
||||
let lines = if compression == 3 { 16 } else { 1 };
|
||||
let chunks = if tiled { w.div_ceil(tile as usize) * h.div_ceil(tile as usize) } else { h.div_ceil(lines) };
|
||||
mixed_attr(&mut out, "channels", "chlist", &chlist);
|
||||
mixed_attr(&mut out, "chunkCount", "int", &(chunks as i32).to_le_bytes());
|
||||
mixed_attr(&mut out, "compression", "compression", &[compression]);
|
||||
mixed_attr(&mut out, "dataWindow", "box2i", &window);
|
||||
mixed_attr(&mut out, "displayWindow", "box2i", &window);
|
||||
mixed_attr(&mut out, "lineOrder", "lineOrder", &[0]);
|
||||
if deep {
|
||||
mixed_attr(&mut out, "maxSamplesPerPixel", "int", &1i32.to_le_bytes());
|
||||
}
|
||||
mixed_attr(&mut out, "name", "string", if deep { b"deep" } else { b"flat" });
|
||||
mixed_attr(&mut out, "pixelAspectRatio", "float", &1f32.to_le_bytes());
|
||||
mixed_attr(&mut out, "screenWindowCenter", "v2f", &[0u8; 8]);
|
||||
mixed_attr(&mut out, "screenWindowWidth", "float", &1f32.to_le_bytes());
|
||||
if tiled {
|
||||
// `tile` × `tile` tiles, one level, rounded down.
|
||||
let mut desc: Vec<u8> = [tile, tile].iter().flat_map(|v| v.to_le_bytes()).collect();
|
||||
desc.push(0);
|
||||
mixed_attr(&mut out, "tiles", "tiledesc", &desc);
|
||||
}
|
||||
let ty: &[u8] = if deep {
|
||||
b"deepscanline"
|
||||
} else if tiled {
|
||||
b"tiledimage"
|
||||
} else {
|
||||
b"scanlineimage"
|
||||
};
|
||||
mixed_attr(&mut out, "type", "string", ty);
|
||||
if deep {
|
||||
mixed_attr(&mut out, "version", "int", &1i32.to_le_bytes());
|
||||
}
|
||||
out.push(0); // end of this header
|
||||
}
|
||||
out.push(0); // end of the header list
|
||||
out
|
||||
}
|
||||
|
||||
/// Sample `i` of a flat channel: `R` is 1, 2, 3, …, `G` 10 more and `B` 100 more, top row first.
|
||||
fn mixed_flat(channel: f32, i: usize) -> f32 {
|
||||
channel + 1.0 + i as f32
|
||||
}
|
||||
|
||||
/// A mixed file with a 3 × 2 flat part, and its chunk offsets as `offsets[part][chunk]`. The
|
||||
/// tiled flat part's chunks hold the tiles at (0, 0) and (1, 0).
|
||||
fn mixed_file(deep_first: bool, tiled: bool) -> (Vec<u8>, [[usize; 2]; 2]) {
|
||||
let (w, h) = (3usize, 2usize);
|
||||
let mut out = mixed_headers(deep_first, tiled, (w, h));
|
||||
let chunks: Vec<Vec<Vec<u8>>> = (0..2i32)
|
||||
.map(|part| {
|
||||
let deep = (part == 0) == deep_first;
|
||||
(0..h)
|
||||
.map(|y| {
|
||||
let mut c = Vec::new();
|
||||
c.extend_from_slice(&part.to_le_bytes());
|
||||
if tiled && !deep {
|
||||
// Tile (y, 0) at level (0, 0): columns 2y and 2y + 1 that lie inside.
|
||||
[y as i32, 0, 0, 0].iter().for_each(|v| c.extend_from_slice(&v.to_le_bytes()));
|
||||
let xs = 2 * y..(2 * y + 2).min(w);
|
||||
c.extend_from_slice(&((xs.len() * h * 3 * 4) as i32).to_le_bytes());
|
||||
for row in 0..h {
|
||||
for channel in [100.0, 10.0, 0.0] {
|
||||
xs.clone().for_each(|x| c.extend_from_slice(&mixed_flat(channel, row * w + x).to_le_bytes()));
|
||||
}
|
||||
}
|
||||
return c;
|
||||
}
|
||||
c.extend_from_slice(&(y as i32).to_le_bytes());
|
||||
if deep {
|
||||
// Packed count table, packed and unpacked sample data: one sample a pixel.
|
||||
for size in [w * 4, w * 4, w * 4] {
|
||||
c.extend_from_slice(&(size as u64).to_le_bytes());
|
||||
}
|
||||
(1..=w as u32).for_each(|n| c.extend_from_slice(&n.to_le_bytes()));
|
||||
(0..w).for_each(|_| c.extend_from_slice(&0.5f32.to_le_bytes()));
|
||||
} else {
|
||||
c.extend_from_slice(&((w * 3 * 4) as i32).to_le_bytes());
|
||||
for channel in [100.0, 10.0, 0.0] {
|
||||
(0..w).for_each(|x| c.extend_from_slice(&mixed_flat(channel, y * w + x).to_le_bytes()));
|
||||
}
|
||||
}
|
||||
c
|
||||
})
|
||||
.collect()
|
||||
})
|
||||
.collect();
|
||||
let mut offsets = [[0usize; 2]; 2];
|
||||
let mut at = out.len() + 2 * h * 8;
|
||||
for (part, lines) in chunks.iter().enumerate() {
|
||||
for (y, c) in lines.iter().enumerate() {
|
||||
offsets[part][y] = at;
|
||||
out.extend_from_slice(&(at as u64).to_le_bytes());
|
||||
at += c.len();
|
||||
}
|
||||
}
|
||||
chunks.into_iter().flatten().for_each(|c| out.extend_from_slice(&c));
|
||||
(out, offsets)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_flat_part_beside_a_deep_part_decodes() {
|
||||
let limits = Limits::default();
|
||||
for (deep_first, tiled) in [(false, false), (true, false), (false, true), (true, true)] {
|
||||
let (bytes, _) = mixed_file(deep_first, tiled);
|
||||
let (flat, deep) = if deep_first { (1, 0) } else { (0, 1) };
|
||||
let parts = parts(&bytes, &limits).unwrap();
|
||||
assert_eq!((parts[flat].deep, parts[deep].deep, parts[flat].name.as_deref()), (false, true, Some("flat")));
|
||||
|
||||
let part = decode_part(&bytes, flat, &all, &limits).unwrap();
|
||||
for (name, channel) in [("R", 0.0), ("G", 10.0), ("B", 100.0)] {
|
||||
let expected: Vec<f32> = (0..6).map(|i| mixed_flat(channel, i)).collect();
|
||||
assert_eq!(part.channel(name).unwrap().samples, Samples::F32(expected), "deep first {deep_first} tiled {tiled} {name}");
|
||||
}
|
||||
let only_g = decode_part(&bytes, flat, &|n| n == "G", &limits).unwrap();
|
||||
assert_eq!(only_g.channels.iter().map(|c| c.name.as_str()).collect::<Vec<_>>(), ["G"]);
|
||||
|
||||
let display = decode_display_part(&bytes, flat, &|n| n == "flat.R", &|_| 9.0, &limits).unwrap();
|
||||
assert_eq!(display.channels.len(), 1);
|
||||
assert_eq!((display.channels[0].name.as_str(), display.channels[0].samples.as_slice()), ("flat.R", &[1.0, 2.0, 3.0, 4.0, 5.0, 6.0][..]));
|
||||
|
||||
assert!(matches!(decode_part(&bytes, deep, &all, &limits), Err(Error::Unsupported(_))));
|
||||
assert!(matches!(decode_display_part(&bytes, deep, &all, &|_| 0.0, &limits), Err(Error::Unsupported(_))));
|
||||
assert!(matches!(decode_part(&bytes, 2, &all, &limits), Err(Error::Malformed(_))));
|
||||
let deep_image = crate::deep::decode_deep(&bytes, &limits).unwrap();
|
||||
assert_eq!((deep_image.width, deep_image.height), (3, 2));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_flat_part_s_chunks_beside_a_deep_part_are_checked_before_they_are_read() {
|
||||
let limits = Limits::default();
|
||||
for deep_first in [false, true] {
|
||||
let (good, offsets) = mixed_file(deep_first, false);
|
||||
let (flat, deep) = if deep_first { (1, 0) } else { (0, 1) };
|
||||
let tables = mixed_headers(deep_first, false, (3, 2)).len();
|
||||
let entry = tables + flat * 2 * 8;
|
||||
let with_offset = |slot: usize, value: u64| {
|
||||
let mut b = good.clone();
|
||||
b[entry + slot * 8..entry + slot * 8 + 8].copy_from_slice(&value.to_le_bytes());
|
||||
b
|
||||
};
|
||||
let refused = |b: &[u8]| {
|
||||
for r in [decode_part(b, flat, &all, &limits).map(|_| ()), decode_display_part(b, flat, &all, &|_| 0.0, &limits).map(|_| ())] {
|
||||
assert!(matches!(&r, Err(Error::Malformed(_))), "deep first {deep_first}: {r:?}");
|
||||
}
|
||||
};
|
||||
// The deep part's chunk, the end of the file and past it, the offset tables, the same
|
||||
// chunk twice.
|
||||
refused(&with_offset(0, offsets[deep][0] as u64));
|
||||
refused(&with_offset(0, good.len() as u64));
|
||||
refused(&with_offset(0, u64::MAX));
|
||||
refused(&with_offset(0, (tables + 8) as u64));
|
||||
refused(&with_offset(1, offsets[flat][0] as u64));
|
||||
// A chunk naming another part, and one declaring more bytes than a block holds.
|
||||
let mut b = good.clone();
|
||||
b[offsets[flat][1]..offsets[flat][1] + 4].copy_from_slice(&(deep as i32).to_le_bytes());
|
||||
refused(&b);
|
||||
let mut b = good.clone();
|
||||
b[offsets[flat][0] + 8..offsets[flat][0] + 12].copy_from_slice(&i32::MAX.to_le_bytes());
|
||||
refused(&b);
|
||||
// Offset tables longer than the file.
|
||||
refused(&good[..tables + 20]);
|
||||
|
||||
for n in 0..good.len() {
|
||||
let _ = decode_part(&good[..n], flat, &all, &limits);
|
||||
let _ = decode_display_part(&good[..n], flat, &all, &|_| 0.0, &limits);
|
||||
}
|
||||
for i in 0..good.len() {
|
||||
for v in [0u8, 1, 0x7F, 0xFF] {
|
||||
let mut b = good.clone();
|
||||
b[i] = v;
|
||||
if let Ok(p) = decode_part(&b, flat, &all, &limits) {
|
||||
let n = p.info.data_window.width as usize * p.info.data_window.height as usize;
|
||||
assert!(p.channels.iter().all(|c| c.samples.len() == n));
|
||||
}
|
||||
let _ = decode_display_part(&b, flat, &all, &|_| 0.0, &limits);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_tiled_flat_part_beside_a_deep_part_must_hold_each_of_its_tiles() {
|
||||
let limits = Limits::default();
|
||||
for deep_first in [false, true] {
|
||||
let (good, offsets) = mixed_file(deep_first, true);
|
||||
let flat = usize::from(deep_first);
|
||||
// The second chunk names tile (0, 0) at level (1, 1), which a one-level part does not
|
||||
// have, or tile (0, 0) again, so tile (1, 0) has no chunk.
|
||||
for coordinates in [[0i32, 0, 1, 1], [0, 0, 0, 0]] {
|
||||
let mut b = good.clone();
|
||||
let at = offsets[flat][1] + 4;
|
||||
coordinates.iter().enumerate().for_each(|(i, v)| b[at + i * 4..at + i * 4 + 4].copy_from_slice(&v.to_le_bytes()));
|
||||
for r in [decode_part(&b, flat, &all, &limits).map(|_| ()), decode_display_part(&b, flat, &all, &|_| 0.0, &limits).map(|_| ())] {
|
||||
assert!(matches!(&r, Err(Error::Malformed(_))), "deep first {deep_first} {coordinates:?}: {r:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_scan_line_chunk_beside_a_deep_part_must_start_at_its_block_s_first_line() {
|
||||
// A ZIP flat part (16 lines a block) holds its 3 × 2 pixels in one chunk, stored raw, as a
|
||||
// block is when compressing it does not make it smaller; the deep part follows.
|
||||
let limits = Limits::default();
|
||||
let (w, h) = (3usize, 2usize);
|
||||
let (plain, offsets) = mixed_file(false, false);
|
||||
let deep = &plain[offsets[1][0]..];
|
||||
let with_y = |y: i32| {
|
||||
let mut out = mixed_headers_with(false, false, (w, h), 3, &["B", "G", "R"], 2);
|
||||
let mut chunk = Vec::new();
|
||||
chunk.extend_from_slice(&0i32.to_le_bytes());
|
||||
chunk.extend_from_slice(&y.to_le_bytes());
|
||||
chunk.extend_from_slice(&((w * h * 3 * 4) as i32).to_le_bytes());
|
||||
for row in 0..h {
|
||||
for channel in [100.0, 10.0, 0.0] {
|
||||
(0..w).for_each(|x| chunk.extend_from_slice(&mixed_flat(channel, row * w + x).to_le_bytes()));
|
||||
}
|
||||
}
|
||||
let flat_at = out.len() + 3 * 8;
|
||||
let deep_at = flat_at + chunk.len();
|
||||
for at in [flat_at, deep_at, deep_at + offsets[1][1] - offsets[1][0]] {
|
||||
out.extend_from_slice(&(at as u64).to_le_bytes());
|
||||
}
|
||||
out.extend_from_slice(&chunk);
|
||||
out.extend_from_slice(deep);
|
||||
out
|
||||
};
|
||||
let part = decode_part(&with_y(0), 0, &all, &limits).unwrap();
|
||||
assert_eq!(part.channel("R").unwrap().samples, Samples::F32((0..6).map(|i| mixed_flat(0.0, i)).collect()));
|
||||
// Each of these y values rounds to block 0, but none is that block's first line.
|
||||
for y in [-15, -1, 1, 15] {
|
||||
let b = with_y(y);
|
||||
for r in [decode_part(&b, 0, &all, &limits).map(|_| ()), decode_display_part(&b, 0, &all, &|_| 0.0, &limits).map(|_| ())] {
|
||||
assert!(matches!(&r, Err(Error::Malformed(_))), "y {y}: {r:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A mixed file whose flat part, first, is one pixel of these float channels (each 1.0) in one
|
||||
/// `tile` × `tile` tile, held by a chunk that declares `size` payload bytes.
|
||||
fn mixed_one_tile(channels: &[&str], tile: u32, size: i32) -> Vec<u8> {
|
||||
let (plain, offsets) = mixed_file(false, false);
|
||||
let mut out = mixed_headers_with(false, true, (1, 1), 0, channels, tile);
|
||||
let mut chunk = Vec::new();
|
||||
[0i32, 0, 0, 0, 0, size].iter().for_each(|v| chunk.extend_from_slice(&v.to_le_bytes()));
|
||||
channels.iter().for_each(|_| chunk.extend_from_slice(&1f32.to_le_bytes()));
|
||||
let flat_at = out.len() + 3 * 8;
|
||||
let deep_at = flat_at + chunk.len();
|
||||
for at in [flat_at, deep_at, deep_at + offsets[1][1] - offsets[1][0]] {
|
||||
out.extend_from_slice(&(at as u64).to_le_bytes());
|
||||
}
|
||||
out.extend_from_slice(&chunk);
|
||||
out.extend_from_slice(&plain[offsets[1][0]..]);
|
||||
out
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_chunk_beside_a_deep_part_must_fit_the_rest_of_the_file() {
|
||||
// 2^14 × 2^14 tiles of three floats let a chunk hold up to 3 GiB (also on 32-bit), so the crate would
|
||||
// allocate the 2 GiB this chunk declares before it found the file too short.
|
||||
let limits = Limits::default();
|
||||
let channels = ["B", "G", "R"];
|
||||
let good = mixed_one_tile(&channels, 1 << 14, 12);
|
||||
assert_eq!(decode_part(&good, 0, &all, &limits).unwrap().channel("R").unwrap().samples, Samples::F32(vec![1.0]));
|
||||
let b = mixed_one_tile(&channels, 1 << 14, i32::MAX);
|
||||
for r in [decode_part(&b, 0, &all, &limits).map(|_| ()), decode_display_part(&b, 0, &all, &|_| 0.0, &limits).map(|_| ())] {
|
||||
assert!(matches!(&r, Err(Error::Malformed(m)) if m.contains("remain in the file")), "{r:?}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_tile_too_large_to_address_is_refused_before_a_chunk_is_read() {
|
||||
// Five float channels in 10^9 × 10^9 tiles: 20 bytes a pixel times 10^18 pixels does not fit
|
||||
// in a 64-bit size, though the 1 × 1 part passes every size limit.
|
||||
let limits = Limits::default();
|
||||
let channels = ["A", "B", "C", "D", "E"];
|
||||
let tile = 1_000_000_000u32;
|
||||
let mixed = mixed_one_tile(&channels, tile, 20);
|
||||
|
||||
// A file of flat parts only, read by the crate's own reader, with its 4 × 4 tiles widened.
|
||||
let mut flat_only = file(vec![layer(None, 1, 1, &channels, true, exr::prelude::Compression::Uncompressed)], (1, 1));
|
||||
let at = flat_only.windows(9).position(|w| w == b"tiledesc\0").unwrap() + 9 + 4;
|
||||
[tile, tile].iter().enumerate().for_each(|(i, v)| flat_only[at + i * 4..at + i * 4 + 4].copy_from_slice(&v.to_le_bytes()));
|
||||
|
||||
for (name, b) in [("mixed", &mixed), ("flat only", &flat_only)] {
|
||||
let results = [
|
||||
parts(b, &limits).map(|_| ()),
|
||||
decode_part(b, 0, &all, &limits).map(|_| ()),
|
||||
decode_display_part(b, 0, &all, &|_| 0.0, &limits).map(|_| ()),
|
||||
];
|
||||
for r in results {
|
||||
assert!(matches!(&r, Err(Error::LimitExceeded(_))), "{name}: {r:?}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_flat_parts_of_a_mixed_file_must_fit_the_file() {
|
||||
// 1024 × 1024 × 3 float samples (12 MiB) declared in a file of a few hundred bytes: over a
|
||||
// ratio of 1 plus the 1 MiB slack, though another part is deep.
|
||||
let headers = mixed_headers(false, false, (1024, 1024));
|
||||
assert!(parts(&headers, &Limits::default()).is_ok());
|
||||
let tight = Limits { max_ratio: 1, ..Limits::default() };
|
||||
for r in [parts(&headers, &tight).map(|_| ()), decode_part(&headers, 0, &all, &tight).map(|_| ())] {
|
||||
assert!(matches!(&r, Err(Error::LimitExceeded(_))), "{r:?}");
|
||||
}
|
||||
// The deep part's samples are not counted, and a small flat part passes the same ratio.
|
||||
assert!(parts(&mixed_file(false, false).0, &tight).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn deep_samples_do_not_count_against_the_flat_expansion_ratio() {
|
||||
// The 4 MiB deep plane would exceed max_ratio=1 plus the 1 MiB slack if counted.
|
||||
// Header inspection does not allocate those samples; deep decoding bounds its own counts.
|
||||
let headers = mixed_headers_with_deep_size(false, false, (1, 1), 0, &["R"], 2, (1024, 1024));
|
||||
let limits = Limits { max_ratio: 1, max_bytes: 8 * 1024 * 1024, ..Limits::default() };
|
||||
assert!(parts(&headers, &limits).is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mixed_display_decode_bounds_each_block_not_the_whole_flat_part() {
|
||||
for (tiled, block_bytes) in [(false, 36), (true, 48)] {
|
||||
for deep_first in [false, true] {
|
||||
let (bytes, _) = mixed_file(deep_first, tiled);
|
||||
let flat = usize::from(deep_first);
|
||||
let keep = |name: &str| name == "flat.R";
|
||||
// The full flat part is 72 bytes; the selected plane is 24 bytes.
|
||||
let limits = Limits { max_bytes: block_bytes, ..Limits::default() };
|
||||
let part = decode_display_part(&bytes, flat, &keep, &|_| 0.0, &limits).unwrap();
|
||||
assert_eq!(part.channels[0].samples, [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]);
|
||||
let tight = Limits { max_bytes: block_bytes - 1, ..limits };
|
||||
assert!(matches!(decode_display_part(&bytes, flat, &keep, &|_| 0.0, &tight), Err(Error::LimitExceeded(_))));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn present_packed_payload_is_bounded_before_allocation() {
|
||||
let packed = 1024 * 1024;
|
||||
let mut bytes = mixed_one_tile(&["R"], 1 << 15, packed);
|
||||
// Make the declared payload present, not merely a truncated-file error. The nominal
|
||||
// huge tile allows this size in exr::Chunk::read, but the actual flat image is 1x1.
|
||||
bytes.resize(bytes.len() + packed as usize, 0);
|
||||
let limits = Limits { max_bytes: 4, ..Limits::default() };
|
||||
assert!(matches!(
|
||||
decode_display_part(&bytes, 0, &|_| true, &|_| 0.0, &limits),
|
||||
Err(Error::LimitExceeded(message)) if message.contains("packed flat block")
|
||||
));
|
||||
// decode_part also includes the deep header's 24 bytes in its aggregate header budget.
|
||||
let limits = Limits { max_bytes: 32, ..limits };
|
||||
assert!(matches!(decode_part(&bytes, 0, &all, &limits), Err(Error::LimitExceeded(message)) if message.contains("packed flat block")));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cropped_mixed_display_bounds_block_tracking() {
|
||||
let mut bytes = mixed_headers_with(false, true, (100, 100), 0, &["R"], 1);
|
||||
// Both display windows are 1x1; the flat data window still has 10,000 one-pixel tiles.
|
||||
let needle = b"displayWindow\0box2i\0";
|
||||
let positions: Vec<usize> = bytes.windows(needle.len()).enumerate().filter_map(|(i, w)| (w == needle).then_some(i + needle.len() + 4)).collect();
|
||||
for at in positions {
|
||||
bytes[at..at + 16].fill(0);
|
||||
}
|
||||
// Supply all offset-table bytes so validation reaches bookkeeping, not truncation.
|
||||
bytes.resize(bytes.len() + (10_000 + 2) * 8, 0);
|
||||
let limits = Limits { max_bytes: 4, ..Limits::default() };
|
||||
assert!(matches!(
|
||||
decode_display_part(&bytes, 0, &|_| true, &|_| 0.0, &limits),
|
||||
Err(Error::LimitExceeded(message)) if message.contains("block tracking")
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dense_block_ordinals_cover_scan_lines_and_all_tile_levels() {
|
||||
use exr::block::chunk::TileCoordinates;
|
||||
use exr::meta::attribute::LevelMode;
|
||||
for tiled in [false, true] {
|
||||
let bytes = file(vec![layer(None, 9, 7, &["R"], tiled, exr::prelude::Compression::ZIP1)], (9, 7));
|
||||
let mut meta = read_meta(&bytes, &Limits::default()).unwrap();
|
||||
let header = &mut meta.headers[0];
|
||||
for mode in [LevelMode::Singular, LevelMode::MipMap, LevelMode::RipMap] {
|
||||
if let exr::meta::BlockDescription::Tiles(tiles) = &mut header.blocks {
|
||||
tiles.level_mode = mode;
|
||||
}
|
||||
for (i, block) in header.blocks_increasing_y_order().enumerate() {
|
||||
assert_eq!(flat_block_ordinal(header, block.location).unwrap(), i);
|
||||
}
|
||||
let absent = TileCoordinates { tile_index: exr::math::Vec2(0, 0), level_index: exr::math::Vec2(100, 100) };
|
||||
assert!(flat_block_ordinal(header, absent).is_err());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user