mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 19:14:26 +00:00
Merge commit '462426a93700501537a6889f6e5bfe8135c5cbae' into fix/exr-mixed-regression-integration
This commit is contained in:
+151
-35
@@ -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, TileCoordinates};
|
||||
use exr::image::read::image::ReadImage;
|
||||
use exr::image::read::layers::{ChannelsReader, ReadAllLayers, ReadChannels};
|
||||
use exr::meta::MetaData;
|
||||
@@ -248,8 +248,18 @@ fn window_size(size: exr::math::Vec2<usize>) -> (u32, u32) {
|
||||
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)?;
|
||||
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,122 @@ 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())
|
||||
}
|
||||
|
||||
/// 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) -> 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)?;
|
||||
let mut blocks: std::collections::HashSet<TileCoordinates> = header.blocks_increasing_y_order().map(|block| block.location).collect();
|
||||
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 chunk = Chunk::read(&mut Cursor::new(chunk), meta).map_err(map_exr)?;
|
||||
let tile = header.get_block_data_indices(&chunk.compressed_block).map_err(map_exr)?;
|
||||
if let CompressedBlock::ScanLine(block) = &chunk.compressed_block {
|
||||
// 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
|
||||
)));
|
||||
}
|
||||
}
|
||||
if !blocks.remove(&tile) {
|
||||
return Err(malformed(format!(
|
||||
"part {index} has no free slot for block {:?} at level {:?}: it is outside the part or another chunk holds it",
|
||||
tile.tile_index, tile.level_index
|
||||
)));
|
||||
}
|
||||
if reader.filter_block(tile) {
|
||||
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)?;
|
||||
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,357 @@ 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> {
|
||||
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 { (3, 2, &["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^15 × 2^15 tiles of three floats let a chunk hold up to 12 GiB, 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 << 15, 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 << 15, 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());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user