fix(codecs): use probe_size, Limits::none, and u64 max_alloc

This commit is contained in:
Dylan M. Taylor
2026-11-14 21:55:28 +00:00
parent 3cb16efabb
commit b3a8bf7df9
6 changed files with 12 additions and 15 deletions
+1 -2
View File
@@ -1406,8 +1406,7 @@ impl<'a> Scene<'a> {
use craft_image_codecs::{ChannelLayout, EncodeOptions, Format, Image as CodecImage, SampleType};
let blob = self.doc.image_blob(im).filter(|b| !b.bytes.is_empty()).ok_or_else(|| "an image has no pixels and was left out".to_string())?;
// The pixels it converts to (RGBA, 4 bytes each) are read from its header before decoding.
let info = craft_image_codecs::probe(&blob.bytes).map_err(|_| UNDECODED.to_string())?;
let (w, h) = (info.width, info.height);
let (w, h) = craft_image_codecs::probe_size(&blob.bytes).map_err(|_| UNDECODED.to_string())?;
if u64::from(w) * u64::from(h) > MAX_IMAGE_PIXELS {
return Err("an image is too large for DXF and was left out".to_string());
}
+2 -2
View File
@@ -276,7 +276,7 @@ enum SampleSlice<'a> {
fn decode_cmyk_tiff_samples(bytes: &[u8], info: &craft_tiff::image::ImageInfo) -> Option<Samples> {
let _ = is_cmyk_tiff_info(info)?;
let _ = pixels(info.width, info.height)?;
let opts = craft_image_codecs::DecodeOptions { limits: CodecLimits::unlimited(), keep_orientation: true };
let opts = craft_image_codecs::DecodeOptions { limits: CodecLimits::none(), keep_orientation: true };
let img = craft_image_codecs::decode_as_with(Format::Tiff, bytes, &opts).ok()?;
if img.layout != ChannelLayout::Cmyk || img.width != info.width || img.height != info.height {
return None;
@@ -616,7 +616,7 @@ pub fn extra_samples_tiff_rgba(bytes: &[u8], rgb: impl Fn([f32; 4]) -> [f32; 3])
let n = usize::try_from(pixels(w, h)?).ok()?;
// ExtraSamples 1: associated alpha (premultiplied colour), 2: unassociated alpha, 0: not alpha.
let extra = ifd.u64s(craft_tiff::tags::EXTRA_SAMPLES).and_then(|v| v.first().copied()).and_then(|v| u16::try_from(v).ok());
let opts = craft_image_codecs::DecodeOptions { limits: CodecLimits::unlimited(), keep_orientation: true };
let opts = craft_image_codecs::DecodeOptions { limits: CodecLimits::none(), keep_orientation: true };
let img = craft_image_codecs::decode_as_with(Format::Tiff, bytes, &opts).ok()?;
let (samples, colour, alpha) = match img.layout {
ChannelLayout::CmykA if is_cmyka => (5, 4, true),
+3 -5
View File
@@ -156,8 +156,7 @@ impl ImageBlob {
/// decoded none (an image past the allowance).
pub fn checked_with_pixels(mut self, proxy: bool) -> Option<(Self, Option<image::DynamicImage>)> {
let max = crate::MAX_RASTER_ALLOC;
let info = craft_image_codecs::probe(&self.bytes).ok()?;
let (w, h) = (info.width, info.height);
let (w, h) = craft_image_codecs::probe_size(&self.bytes).ok()?;
if u64::from(w) * u64::from(h) > max / 4 {
return Some((self, None));
}
@@ -214,7 +213,7 @@ pub fn missing_placeholder() -> Option<Vec<u8>> {
fn decode_dynamic(bytes: &[u8], max_alloc: u64) -> Result<image::DynamicImage, bool> {
use craft_image_codecs::{ChannelLayout, CodecError, DecodeOptions, Limits, SampleType};
let opts = DecodeOptions { limits: Limits { max_alloc: Some(max_alloc), ..Limits::default() }, ..DecodeOptions::default() };
let opts = DecodeOptions { limits: Limits { max_alloc, ..Limits::default() }, ..DecodeOptions::default() };
let img = craft_image_codecs::decode_with(bytes, &opts).map_err(|e| matches!(e, CodecError::LimitExceeded(_)))?;
let (w, h) = img.dimensions();
match (img.layout(), img.sample_type()) {
@@ -234,8 +233,7 @@ fn decode_dynamic(bytes: &[u8], max_alloc: u64) -> Result<image::DynamicImage, b
/// `bytes` decoded and shrunk to fit [`PROXY_SIZE`] as a PNG; `None` when small enough already or
/// undecodable.
fn proxy_png(bytes: &[u8]) -> Option<Vec<u8>> {
let info = craft_image_codecs::probe(bytes).ok()?;
let (w, h) = (info.width, info.height);
let (w, h) = craft_image_codecs::probe_size(bytes).ok()?;
if w.max(h) <= PROXY_SIZE {
return None;
}
+1 -1
View File
@@ -347,7 +347,7 @@ pub fn full_bytes(p: &PlacedDocument, blob: &ImageBlob) -> Option<Arc<Vec<u8>>>
/// The size in pixels of image bytes `bytes` (a preview), read from its header.
pub fn image_size(bytes: &[u8]) -> Option<(u32, u32)> {
craft_image_codecs::probe(bytes).ok().map(|i| (i.width, i.height))
craft_image_codecs::probe_size(bytes).ok()
}
// ---------- reading files ----------
+2 -2
View File
@@ -318,8 +318,8 @@ pub(super) fn png_8_bit(bytes: &[u8]) -> Option<Vec<u8>> {
pub(super) fn embed(blob: &vectorcraft_doc::ImageBlob, interpolate: bool, sixteen_bit: bool) -> Option<Image> {
// The header's size first: the writer's decoders allocate what it declares, and the encoded
// bytes of an image that is far larger than any the app reads may be a few kilobytes.
let info = craft_image_codecs::probe(&blob.bytes).ok();
if info.as_ref().is_some_and(|i| u64::from(i.width) * u64::from(i.height) > crate::images::MAX_EMBED_PIXELS) {
let dims = craft_image_codecs::probe_size(&blob.bytes).ok();
if dims.is_some_and(|(w, h)| u64::from(w) * u64::from(h) > crate::images::MAX_EMBED_PIXELS) {
return None;
}
let bytes = blob.bytes.as_ref().clone();
+3 -3
View File
@@ -417,7 +417,7 @@ pub(crate) fn recode_cmyk(
let jpeg = jpeg_components(&blob.bytes).is_some();
let icc = icc.filter(|_| convert.is_none());
if jpeg && convert.is_none() && alpha.is_none() {
let dims = craft_image_codecs::probe(&blob.bytes).ok().map(|i| (i.width, i.height));
let dims = craft_image_codecs::probe_size(&blob.bytes).ok();
if let Some((w, h)) = dims.filter(|d| kept(&rule, *d, size, true)) {
// The JPEG data as it is, tagged with `icc` rather than with its own segments; one
// larger than [`MAX_EMBED_PIXELS`] is not written.
@@ -511,8 +511,8 @@ pub(crate) fn mask_alpha(blob: &ImageBlob, width: u32, height: u32, clip: bool)
}
// The size from the header first: a mask of another size, or too large to hold as RGBA, is
// not decoded at all.
let info = craft_image_codecs::probe(&blob.bytes).ok()?;
if (info.width, info.height) != (width, height) || u64::from(width) * u64::from(height) * 4 > MAX_DECODE {
let (w, h) = craft_image_codecs::probe_size(&blob.bytes).ok()?;
if (w, h) != (width, height) || u64::from(width) * u64::from(height) * 4 > MAX_DECODE {
return None;
}
let img = decoded(&blob.bytes)?.into_rgba8();