mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-03 06:31:42 +01:00
heif: hold the canvas the decoder really allocates to max_alloc
This commit is contained in:
1 parent
a11545db2d
commit
ceb8ae76dc
2 files changed
+27
-2
No files matched your search
@@ -56,7 +56,10 @@ pub(crate) fn decode(bytes: &[u8], limits: &Limits, keep_orientation: bool) -> R
|
||||
limits.check_bytes(info.coded_width, info.coded_height, peak)?;
|
||||
}
|
||||
// Photoshop on macOS opens HEIC through Apple's decoder, so match its chroma upsampling.
|
||||
let options = craft_heif::Options { max_pixels: limits.max_pixels, apply_transforms: !keep_orientation, chroma: craft_heif::Chroma::Apple };
|
||||
// A grid's payload can declare a larger canvas than the `ispe` probed above. craft-heif holds
|
||||
// the canvas it really decodes to `max_pixels`, so `max_alloc` is passed down as pixels too.
|
||||
let max_pixels = limits.max_pixels.min(limits.max_alloc / peak);
|
||||
let options = craft_heif::Options { max_pixels, apply_transforms: !keep_orientation, chroma: craft_heif::Chroma::Apple };
|
||||
let mut decoded = craft_heif::decode(bytes, &options).map_err(err)?;
|
||||
// The decoder re-reads the container; trust its output's shape, not the probe's.
|
||||
let layout = if decoded.has_alpha { ChannelLayout::Rgba } else { ChannelLayout::Rgb };
|
||||
@@ -143,6 +146,27 @@ mod tests {
|
||||
assert_eq!(with(peak).unwrap().sample_type(), crate::image::SampleType::F32);
|
||||
}
|
||||
|
||||
/// A grid whose payload declares a larger canvas (64 x 64) than its `ispe` (33 x 33): the
|
||||
/// byte budget applies to the canvas that is decoded, not the one the container declares.
|
||||
#[cfg(feature = "heif")]
|
||||
#[test]
|
||||
fn a_grid_canvas_larger_than_its_declared_size_is_held_to_max_alloc() {
|
||||
use craft_heif::testdata::{Spec, build};
|
||||
let grey = |_, _| [128u16, 128, 128];
|
||||
let mut bytes = build(&Spec::new(33, 33, &grey));
|
||||
// The grid item's payload: version, flags, rows - 1, columns - 1, output width and height.
|
||||
let at = bytes.windows(8).position(|w| w == [0, 0, 1, 1, 0, 33, 0, 33]).unwrap();
|
||||
bytes[at + 5] = 64;
|
||||
bytes[at + 7] = 64;
|
||||
let with = |max_alloc| crate::decode_with(&bytes, &crate::DecodeOptions { limits: Limits { max_alloc, ..Default::default() }, ..Default::default() });
|
||||
// Enough for the declared 33 x 33 (three bytes a sample at the peak), not for 64 x 64.
|
||||
// Refused by the decoder before it allocates the canvas, not by the size check on its output.
|
||||
match with(33 * 33 * 3 * 3) {
|
||||
Err(CodecError::LimitExceeded(m)) => assert!(m.contains("pixel limit"), "{m}"),
|
||||
r => panic!("{:?}", r.map(|i| i.dimensions())),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "heif")]
|
||||
#[test]
|
||||
fn a_heic_rs_panic_is_a_malformed_file_error() {
|
||||
|
||||
@@ -152,7 +152,8 @@ fn limits_are_checked_before_decoding() {
|
||||
let bytes = Limits { max_alloc: 64 * 64 * 3 - 1, ..Default::default() };
|
||||
assert!(matches!(decode_with(checker_64(), &tight(bytes)), Err(CodecError::LimitExceeded(_))));
|
||||
// An 8-bit file peaks at the decoder's 16-bit buffer plus the 8-bit output: 3 bytes a sample.
|
||||
let peak = 64 * 64 * 3 * 3;
|
||||
// The budget is held against the picture as coded, which this file pads to 10240 pixels.
|
||||
let peak = 10240 * 3 * 3;
|
||||
let short = Limits { max_alloc: peak - 1, ..Default::default() };
|
||||
assert!(matches!(decode_with(checker_64(), &tight(short)), Err(CodecError::LimitExceeded(_))));
|
||||
decode_with(checker_64(), &tight(Limits { max_alloc: peak, ..Default::default() })).unwrap();
|
||||
|
||||
Reference in new issue
Block a user