diff --git a/crates/effects/src/pixel/loaded.rs b/crates/effects/src/pixel/loaded.rs index b36db047fc..fe822ffd64 100644 --- a/crates/effects/src/pixel/loaded.rs +++ b/crates/effects/src/pixel/loaded.rs @@ -107,12 +107,20 @@ impl LoadedTexture { } let mut opts = craft_image_codecs::DecodeOptions::default(); opts.limits.max_alloc = vectorcraft_doc::MAX_RASTER_ALLOC; + // The codec default caps each side at 2^18; a stored texture can be longer than that and + // still fit the live and allocation budgets. + opts.limits.max_width = u32::MAX; + opts.limits.max_height = u32::MAX; let img = craft_image_codecs::decode_as_with(craft_image_codecs::Format::Png, png, &opts).ok()?; let (width, height) = img.dimensions(); if width == 0 || height == 0 { return None; } - let luma = image::RgbaImage::from_raw(width, height, img.to_rgba8()).map(image::DynamicImage::ImageRgba8)?.into_luma8().into_raw(); + use craft_image_codecs::{ChannelLayout as L, SampleType as S}; + let luma = match (img.layout(), img.sample_type()) { + (L::Gray, S::U8) => img.into_data(), + _ => image::RgbaImage::from_raw(width, height, img.to_rgba8()).map(image::DynamicImage::ImageRgba8)?.into_luma8().into_raw(), + }; Some(Self::new(width, height, luma)) } diff --git a/crates/effects/src/pixel/tests.rs b/crates/effects/src/pixel/tests.rs index 34212ddd66..6dd7942e5e 100644 --- a/crates/effects/src/pixel/tests.rs +++ b/crates/effects/src/pixel/tests.rs @@ -1704,6 +1704,14 @@ fn a_loaded_texture_is_its_gray_over_middle_gray() { assert!(texture("not base64!").is_none() && texture("aGVsbG8=").is_none(), "base64 of a PNG only"); } +/// A stored texture longer than the codec's default 2^18-pixel side still reads back. +#[test] +fn a_stored_texture_longer_than_the_codec_default_side_reads_back() { + let t = LoadedTexture::from_rgba(262_145, 1, &vec![128; 262_145 * 4]).and_then(|t| t.to_stored()).unwrap(); + let loaded = texture(&t).unwrap(); + assert_eq!(loaded.size(), (262_145, 1)); +} + #[test] fn glass_bends_the_image_by_a_loaded_texture() { let (w, h) = (48, 40); diff --git a/crates/engine/src/cmd/flatten/analysis.rs b/crates/engine/src/cmd/flatten/analysis.rs index 6104e719c2..70eab3c3e5 100644 --- a/crates/engine/src/cmd/flatten/analysis.rs +++ b/crates/engine/src/cmd/flatten/analysis.rs @@ -70,11 +70,7 @@ pub(crate) fn carry(from: &Node, mut to: Node) -> Node { /// The keys of `doc`'s images that have see-through pixels (an alpha channel that isn't opaque /// everywhere). pub(crate) fn see_through_images(doc: &Document) -> BTreeSet { - let see_through = |b: &ImageBlob| { - b.mime != "image/jpeg" - && craft_image_codecs::decode(&b.bytes) - .is_ok_and(|i| i.layout().has_alpha() && i.to_rgba8().as_chunks::<4>().0.iter().any(|p| p[3] < 255)) - }; + let see_through = |b: &ImageBlob| b.mime != "image/jpeg" && b.has_transparency(); doc.images.iter().filter(|(_, b)| see_through(b)).map(|(k, _)| k.clone()).collect() } diff --git a/crates/engine/src/cmd/links.rs b/crates/engine/src/cmd/links.rs index ded6aa0b9b..1ec0f7ceaa 100644 --- a/crates/engine/src/cmd/links.rs +++ b/crates/engine/src/cmd/links.rs @@ -2648,7 +2648,11 @@ fn convert_image(bytes: &[u8], ext: &str) -> std::result::Result, String "bmp" => craft_image_codecs::Format::Bmp, _ => return Err(format!("can't write .{ext} images")), }; - let img = craft_image_codecs::decode(bytes).map_err(|e| e.to_string())?; + let mut opts = craft_image_codecs::DecodeOptions::default(); + opts.limits.max_alloc = vectorcraft_doc::MAX_RASTER_ALLOC; + opts.limits.max_width = u32::MAX; + opts.limits.max_height = u32::MAX; + let img = craft_image_codecs::decode_with(bytes, &opts).map_err(|e| e.to_string())?; let raw = if format == craft_image_codecs::Format::Jpeg { let rgb: Vec = img.to_rgba8().as_chunks::<4>().0.iter().flat_map(|p| [p[0], p[1], p[2]]).collect(); craft_image_codecs::Image::from_raw( diff --git a/crates/eps/src/legacy_ai/writer.rs b/crates/eps/src/legacy_ai/writer.rs index 8e0bdd0801..0551d1821c 100644 --- a/crates/eps/src/legacy_ai/writer.rs +++ b/crates/eps/src/legacy_ai/writer.rs @@ -1183,11 +1183,17 @@ impl<'a> Writer<'a> { { return img.clone(); } - let img = craft_image_codecs::decode(&blob.bytes) - .ok() - .and_then(|i| image::RgbaImage::from_raw(i.width(), i.height(), i.to_rgba8())) - .map(image::DynamicImage::ImageRgba8) - .map(Arc::new); + let too_large = |w: u32, h: u32| w == 0 || h == 0 || u64::from(w) * u64::from(h) > MAX_IMAGE_PIXELS; + let img = if vectorcraft_doc::header_size(&blob.bytes).is_some_and(|(w, h)| too_large(w, h)) { + None + } else { + vectorcraft_doc::decode::decode_within(&blob.bytes, MAX_IMAGE_PIXELS.saturating_mul(8)) + .ok() + .flatten() + .map(|(img, _)| img) + .filter(|img| !too_large(img.width(), img.height())) + } + .map(Arc::new); self.last_image = Some((blob.clone(), img.clone())); self.last_coverage = None; img diff --git a/crates/eps/src/scene.rs b/crates/eps/src/scene.rs index 8ebdc383ef..5bbb1c9fed 100644 --- a/crates/eps/src/scene.rs +++ b/crates/eps/src/scene.rs @@ -1707,15 +1707,22 @@ impl<'a> Scene<'a> { /// Image `blob` as RGBA pixels; `None` (with a warning) when it can't be written. fn decode_image(&mut self, blob: Option<&ImageBlob>) -> Option { - let Some(img) = blob.and_then(|blob| craft_image_codecs::decode(&blob.bytes).ok()) else { - self.warn(UNDECODED); - return None; - }; - if u64::from(img.width()) * u64::from(img.height()) > MAX_IMAGE_PIXELS { + let too_large = |w: u32, h: u32| w == 0 || h == 0 || u64::from(w) * u64::from(h) > MAX_IMAGE_PIXELS; + if blob.is_some_and(|b| vectorcraft_doc::header_size(&b.bytes).is_some_and(|(w, h)| too_large(w, h))) { self.warn(LARGE_IMAGE); return None; } - image::RgbaImage::from_raw(img.width(), img.height(), img.to_rgba8()) + let Some(img) = blob.and_then(|blob| { + vectorcraft_doc::decode::decode_within(&blob.bytes, MAX_IMAGE_PIXELS.saturating_mul(8)).ok().flatten().map(|(img, _)| img) + }) else { + self.warn(UNDECODED); + return None; + }; + if too_large(img.width(), img.height()) { + self.warn(LARGE_IMAGE); + return None; + } + Some(img.to_rgba8()) } /// Draw the image of `codes[i]` on the unit square mapped by `place`. Its operators go in when