fix(codecs): keep the remaining decodes within the project's limits

A stored texture longer than the codec's default 2^18-pixel side still
reads back, and a gray PNG is not expanded to RGBA just to get its
levels. The AI writer and the EPS scene refuse an oversized image from
its header, then decode within MAX_IMAGE_PIXELS, keeping the pixels at
their stored depth. Flatten's see-through check uses ImageBlob's
allowance, and links.unembed decodes within MAX_RASTER_ALLOC.
This commit is contained in:
FredApps
2026-11-16 07:50:43 +01:00
parent 3e72836b00
commit 46ddf177e3
6 changed files with 47 additions and 18 deletions
+9 -1
View File
@@ -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))
}
+8
View File
@@ -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);
+1 -5
View File
@@ -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<String> {
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()
}
+5 -1
View File
@@ -2648,7 +2648,11 @@ fn convert_image(bytes: &[u8], ext: &str) -> std::result::Result<Vec<u8>, 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<u8> = img.to_rgba8().as_chunks::<4>().0.iter().flat_map(|p| [p[0], p[1], p[2]]).collect();
craft_image_codecs::Image::from_raw(
+11 -5
View File
@@ -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
+13 -6
View File
@@ -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<image::RgbaImage> {
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