From 3590f96ba90c7aca5b618adceffc35a974da6bf2 Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Mon, 16 Nov 2026 05:45:22 +0100 Subject: [PATCH] =?UTF-8?q?engine:=20File=20=E2=80=BA=20Open=20and=20Place?= =?UTF-8?q?=20read=20rasters=20through=20craft-image-codecs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The raster path tells formats with craft-image-codecs' detect, reads the size from the header (vectorcraft-doc's header_size, the renderer's decoded_layout, a Targa's own header) and decodes with the renderer's decode_image, the pixels as stored, with the profile the file embeds. The size notes Open, Place and the Links panel give, and Glass's Load Texture, read the same way. opened_rasters_are_the_pixels_the_image_crate_read compares BMP, TGA and TIFF at 8 and 16 bits, gray and color, with and without alpha, with the image crate's pixels, and PNG, JPEG, GIF and WebP stored as they are. --- crates/engine/src/cmd/fileio/load.rs | 74 ++++++++++---------- crates/engine/src/cmd/fileio/tests_raster.rs | 52 ++++++++++++++ crates/engine/src/cmd/fileio/tgaread.rs | 9 +++ crates/render/src/lib.rs | 2 +- crates/render/src/paint.rs | 27 +++++-- 5 files changed, 120 insertions(+), 44 deletions(-) diff --git a/crates/engine/src/cmd/fileio/load.rs b/crates/engine/src/cmd/fileio/load.rs index f10541a55d..02af949913 100644 --- a/crates/engine/src/cmd/fileio/load.rs +++ b/crates/engine/src/cmd/fileio/load.rs @@ -1,7 +1,5 @@ //! `document.open`: every readable format into a new document. -use std::io::Cursor; - use serde_json::{Value, json}; use vectorcraft_doc::{Document, ImageBlob, ImageObject, Node, NodeKind}; use vectorcraft_geom::Affine; @@ -133,7 +131,7 @@ pub fn detect(name: &str, bytes: &[u8]) -> Option<&'static Format> { if super::heifread::is_heif(bytes) { return format("heif"); } - if let Some(f) = image::guess_format(bytes).ok().and_then(image_format) { + if let Some(f) = craft_image_codecs::detect(bytes).and_then(image_format) { return Some(f); } // A version 2 PICT is known by its version opcodes after the picture's frame, at byte 522 of a @@ -159,9 +157,17 @@ pub fn detect(name: &str, bytes: &[u8]) -> Option<&'static Format> { by_name } -/// The readable raster format behind an `image` crate format. -fn image_format(f: image::ImageFormat) -> Option<&'static Format> { - f.extensions_str().iter().find_map(|e| format(e)).filter(|f| f.read && f.raster) +/// The readable raster format behind a craft-image-codecs format. +fn image_format(f: craft_image_codecs::Format) -> Option<&'static Format> { + f.extensions().iter().find_map(|e| format(e)).filter(|f| f.read && f.raster) +} + +/// The image's size and the bytes a pixel of it takes decoded, from its header +/// ([`vectorcraft_render::decoded_layout`]), for bytes in a format craft-image-codecs tells. +fn header_layout(bytes: &[u8]) -> Option<(craft_image_codecs::Format, u32, u32, u64)> { + let format = craft_image_codecs::detect(bytes)?; + let (w, h, per_pixel) = vectorcraft_render::decoded_layout(bytes, format)?; + Some((format, w, h, per_pixel)) } /// Why a file carrying editing data (an SVG or PDF saved with Preserve Editing) opened as plain @@ -892,22 +898,18 @@ pub(crate) use vectorcraft_doc::MAX_RASTER_ALLOC; /// says so; the pixels aren't read): Open, Place and the Links panel say it ([`size_note`], #1479). /// `None` for images within the allowance, and for bytes that aren't an image. pub(crate) fn too_large_note(bytes: &[u8]) -> Option { - use image::ImageDecoder as _; if let Some((w, h)) = vectorcraft_doc::cmyk::oversized_tiff(bytes) { return Some(format!("an image of {w} × {h} pixels is too large to draw")); } - let reader = image::ImageReader::new(Cursor::new(bytes)).with_guessed_format().ok()?; - let format = reader.format()?; - let d = reader.into_decoder().ok()?; - let (w, h) = d.dimensions(); + let (format, w, h, per_pixel) = header_layout(bytes)?; // The allowance also counts the pixels as RGBA: an image of fewer than 4 bytes a pixel (gray, // gray with alpha, RGB) takes more than the decoder counts (#1717). let rgba_over = u64::from(w) * u64::from(h) > MAX_RASTER_ALLOC / 4; - let samples_over = d.total_bytes() > MAX_RASTER_ALLOC; + let samples_over = (u64::from(w) * u64::from(h)).saturating_mul(per_pixel) > MAX_RASTER_ALLOC; // The renderer can stream a non-interlaced PNG a row at a time when those buffers fit. Its // full decoded byte count can exceed the allowance even though drawing it at native size is // safe; use the renderer's decision for that case instead of warning from total_bytes alone. - let png_streams_at_size = samples_over && format == image::ImageFormat::Png && vectorcraft_render::reduced_size(bytes) == Some((w, h)); + let png_streams_at_size = samples_over && format == craft_image_codecs::Format::Png && vectorcraft_render::reduced_size(bytes) == Some((w, h)); (rgba_over || (samples_over && !png_streams_at_size)).then(|| format!("an image of {w} × {h} pixels is too large to draw")) } @@ -918,7 +920,7 @@ pub(crate) fn too_large_note(bytes: &[u8]) -> Option { pub(crate) fn size_note(bytes: &[u8]) -> Option { let note = too_large_note(bytes)?; let Some(drawn) = vectorcraft_render::reduced_size(bytes) else { return Some(note) }; - let (w, h) = image::ImageReader::new(Cursor::new(bytes)).with_guessed_format().ok()?.into_dimensions().ok()?; + let (w, h) = vectorcraft_doc::header_size(bytes)?; (drawn != (w, h)).then(|| format!("an image of {w} × {h} pixels is shown at reduced size")) } @@ -978,17 +980,14 @@ pub fn raster_image(bytes: &[u8]) -> Result { if super::pxrread::is_pxr(bytes) { return as_png(super::pxrread::decode(bytes, MAX_RASTER_ALLOC).map_err(err)?, None, ppi); } - let mut reader = image::ImageReader::new(Cursor::new(bytes)).with_guessed_format().map_err(err)?; - if reader.format().is_none() && super::tgaread::is_targa(bytes) { - reader.set_format(image::ImageFormat::Tga); + let mut detected = craft_image_codecs::detect(bytes); + if detected.is_none() && super::tgaread::is_targa(bytes) { + detected = Some(craft_image_codecs::Format::Tga); } - if reader.format().is_none() && super::pcxread::is_pcx(bytes) { + if (detected.is_none() || detected == Some(craft_image_codecs::Format::Pcx)) && super::pcxread::is_pcx(bytes) { return as_png(super::pcxread::decode(bytes, MAX_RASTER_ALLOC).map_err(err)?, None, ppi); } - let mut limits = image::Limits::default(); - limits.max_alloc = Some(MAX_RASTER_ALLOC); - reader.limits(limits); - let kind = reader.format().ok_or_else(|| err("not an image VectorCraft reads (see document.formats)"))?; + let kind = detected.ok_or_else(|| err("not an image VectorCraft reads (see document.formats)"))?; let f = image_format(kind).ok_or_else(|| err(format!("{kind:?} images can't be opened (see document.formats)")))?; // A photo whose EXIF orientation turns or flips it (a camera held upright) is stored as it is // seen, so it places the way it was taken (#4284); the others keep their own bytes. @@ -1009,8 +1008,8 @@ pub fn raster_image(bytes: &[u8]) -> Result { _ => None, }; if let Some(kept) = kept { - let (width, height) = reader.into_dimensions().map_err(err)?; - return stored(kept, f.mime, (width, height), ppi); + let size = vectorcraft_doc::header_size(bytes).ok_or_else(|| err("the image's size can't be read"))?; + return stored(kept, f.mime, size, ppi); } let extra_samples = || { let cms = vectorcraft_color::cms::active(); @@ -1023,21 +1022,23 @@ pub fn raster_image(bytes: &[u8]) -> Result { None => match (f.id == "tiff").then(|| vectorcraft_doc::cmyk::group3_tiff_rgba(bytes)).flatten() { Some(read) => image::DynamicImage::ImageRgba8(read.map_err(err)?), None => { - use image::ImageDecoder as _; - let mut decoder = reader.into_decoder().map_err(err)?; // The allowance counts the decoded samples and the RGBA pixels they become (#1717). - let (w, h) = decoder.dimensions(); - if decoder.total_bytes() > MAX_RASTER_ALLOC || u64::from(w) * u64::from(h) * 4 > MAX_RASTER_ALLOC { + // A Targa file's size is read from its header, which no signature points a probe at. + let tga = || (kind == craft_image_codecs::Format::Tga).then(|| super::tgaread::size(bytes)).flatten().map(|(w, h)| (w, h, 4)); + let (w, h, per_pixel) = + vectorcraft_render::decoded_layout(bytes, kind).or_else(tga).ok_or_else(|| err("the image's size can't be read"))?; + let pixels = u64::from(w) * u64::from(h); + if pixels.saturating_mul(per_pixel) > MAX_RASTER_ALLOC || pixels * 4 > MAX_RASTER_ALLOC { return Err(err(format!("an image of {w} × {h} pixels is too large to draw"))); } - // A profile the decoder can't read leaves the image without one. The image decoder - // reads a TIFF's tags within the size of its pixels, so a small TIFF's profile is - // read by the TIFF decoder itself. + let (img, profile) = vectorcraft_render::decode_image(kind, bytes, MAX_RASTER_ALLOC).map_err(err)?; + // A profile the decoder can't read leaves the image without one. A TIFF's profile is + // read by the TIFF reader, as before. icc = match f.id { "tiff" => vectorcraft_doc::cmyk::tiff_icc_profile(bytes), - _ => decoder.icc_profile().ok().flatten(), + _ => profile, }; - image::DynamicImage::from_decoder(decoder).map_err(err)? + img } }, }; @@ -1109,11 +1110,8 @@ pub fn texture_image(bytes: &[u8]) -> Result { LoadedTexture::from_rgba(inks.width, inks.height, &rgba) } None => { - let mut reader = image::ImageReader::new(Cursor::new(blob.bytes.as_slice())).with_guessed_format().map_err(err)?; - let mut limits = image::Limits::default(); - limits.max_alloc = Some(MAX_RASTER_ALLOC); - reader.limits(limits); - let img = reader.decode().map_err(err)?.to_rgba8(); + let kind = craft_image_codecs::detect(&blob.bytes).ok_or_else(|| err("the image can't be read as a texture"))?; + let img = vectorcraft_render::decode_image(kind, &blob.bytes, MAX_RASTER_ALLOC).map_err(err)?.0.to_rgba8(); LoadedTexture::from_rgba(img.width(), img.height(), img.as_raw()) } }; diff --git a/crates/engine/src/cmd/fileio/tests_raster.rs b/crates/engine/src/cmd/fileio/tests_raster.rs index 769398f4c8..b43f3d0547 100644 --- a/crates/engine/src/cmd/fileio/tests_raster.rs +++ b/crates/engine/src/cmd/fileio/tests_raster.rs @@ -444,3 +444,55 @@ fn export_for_screens_rows_keep_their_own_size() { .unwrap(); assert_eq!(rgba(&b64(&r["files"][0])).dimensions(), (512, 256), "{r}"); } + +/// File › Open's raster path reads images through craft-image-codecs (#778) as it read them with +/// the image crate: each format the image crate decoded for it (BMP, TGA, TIFF at 8 and 16 bits, +/// gray and color, with and without alpha; a PNG, JPEG, GIF and WebP stored as they are) gives +/// the same size, sample depth and pixels. +#[test] +fn opened_rasters_are_the_pixels_the_image_crate_read() { + use image::{DynamicImage as D, ImageFormat as F}; + let rgba = image::RgbaImage::from_fn(11, 6, |x, y| image::Rgba([(x * 23) as u8, (y * 41) as u8, ((x ^ y) * 13) as u8, 255 - (x * y * 3) as u8])); + let deep = image::ImageBuffer::, _>::from_fn(11, 6, |x, y| { + image::Rgba([(x * 5_003) as u16, (y * 9_011) as u16, 77, 65_535 - (x * y) as u16]) + }); + let images = [ + D::ImageRgba8(rgba.clone()), + D::ImageRgb8(D::ImageRgba8(rgba.clone()).to_rgb8()), + D::ImageLuma8(D::ImageRgba8(rgba.clone()).to_luma8()), + D::ImageLumaA8(D::ImageRgba8(rgba).to_luma_alpha8()), + D::ImageRgba16(deep.clone()), + D::ImageRgb16(D::ImageRgba16(deep.clone()).to_rgb16()), + D::ImageLuma16(D::ImageRgba16(deep).to_luma16()), + ]; + let mut checked = 0; + for img in &images { + for f in [F::Bmp, F::Tga, F::Tiff, F::Png, F::Jpeg, F::Gif, F::WebP] { + let mut bytes = Vec::new(); + if img.write_to(&mut std::io::Cursor::new(&mut bytes), f).is_err() { + continue; + } + let what = format!("{f:?} {:?}", img.color()); + let mut want = image::load_from_memory_with_format(&bytes, f).unwrap(); + let opened = super::load::raster_image(&bytes).unwrap_or_else(|e| panic!("{what}: {e}")); + assert_eq!((opened.width, opened.height), (want.width(), want.height()), "{what}"); + if opened.blob.mime != "image/png" { + // Stored as they are: the file's own bytes. + assert_eq!(opened.blob.bytes.as_slice(), bytes.as_slice(), "{what}"); + checked += 1; + continue; + } + if f == F::Tga { + let mut px = want.to_rgba8(); + super::tgaread::fix_alpha(&bytes, &mut px); + want = D::ImageRgba8(px); + } + let got = image::load_from_memory_with_format(&opened.blob.bytes, F::Png).unwrap(); + let deep_samples = |c: image::ColorType| c.bytes_per_pixel() > c.channel_count(); + assert_eq!(deep_samples(got.color()), deep_samples(want.color()), "{what}: {:?} {:?}", got.color(), want.color()); + assert_eq!(got.to_rgba16().as_raw(), want.to_rgba16().as_raw(), "{what}"); + checked += 1; + } + } + assert!(checked >= 25, "{checked}"); +} diff --git a/crates/engine/src/cmd/fileio/tgaread.rs b/crates/engine/src/cmd/fileio/tgaread.rs index 2033e45e66..a8cf0abb68 100644 --- a/crates/engine/src/cmd/fileio/tgaread.rs +++ b/crates/engine/src/cmd/fileio/tgaread.rs @@ -23,6 +23,15 @@ pub(super) fn is_targa(bytes: &[u8]) -> bool { && descriptor & 0xc0 == 0 } +/// A Targa file's size, from its header (there is no signature for a size probe to go by). +pub(super) fn size(bytes: &[u8]) -> Option<(u32, u32)> { + let le = |at: usize| Some(u32::from(u16::from_le_bytes(bytes.get(at..at + 2)?.try_into().ok()?))); + if !is_targa(bytes) { + return None; + } + Some((le(12)?, le(14)?)) +} + /// Give `img`, decoded from Targa file `bytes`, the transparency the file stores. Bits 0 to 3 of /// the image descriptor give the number of attribute bits per pixel, and the extension area's /// Attributes Type, when the file has one, says what they hold: diff --git a/crates/render/src/lib.rs b/crates/render/src/lib.rs index c49671fd8c..39fb455e91 100644 --- a/crates/render/src/lib.rs +++ b/crates/render/src/lib.rs @@ -20,7 +20,7 @@ mod ink; mod live; mod lru; mod paint; -pub use paint::{decode_failures, image_decodes, reduced_size}; +pub use paint::{decode_failures, decode_image, decoded_layout, image_decodes, reduced_size}; mod pattern; pub mod placed_document; pub mod proof; diff --git a/crates/render/src/paint.rs b/crates/render/src/paint.rs index 45a3153fcf..5d3a91e752 100644 --- a/crates/render/src/paint.rs +++ b/crates/render/src/paint.rs @@ -190,12 +190,25 @@ fn drawn_format(bytes: &[u8]) -> Option { /// `bytes` decoded as `format` within `allowance` bytes: the pixels as stored, not turned upright, /// as the image crate gave them. fn decode_as(format: craft_image_codecs::Format, bytes: &[u8], allowance: u64) -> Result { + decode_image(format, bytes, allowance).map(|(img, _)| img) +} + +/// `bytes` decoded as `format` within `allowance` bytes, the pixels as stored (not turned +/// upright) and as the image crate gave them ([`dynamic`]), with the ICC profile the file embeds. +/// File › Open and Place read images with it as the renderer does. +pub fn decode_image( + format: craft_image_codecs::Format, + bytes: &[u8], + allowance: u64, +) -> Result<(image::DynamicImage, Option>), craft_image_codecs::CodecError> { let opts = craft_image_codecs::DecodeOptions { limits: craft_image_codecs::Limits { max_width: u32::MAX, max_height: u32::MAX, max_pixels: u64::MAX, max_alloc: allowance }, keep_orientation: true, }; - let img = craft_image_codecs::decode_as_with(format, bytes, &opts)?; - dynamic(img).ok_or_else(|| craft_image_codecs::CodecError::InvalidImage("the decoded samples don't fit the image".into())) + let mut img = craft_image_codecs::decode_as_with(format, bytes, &opts)?; + let icc = img.icc.take(); + let img = dynamic(img).ok_or_else(|| craft_image_codecs::CodecError::InvalidImage("the decoded samples don't fit the image".into()))?; + Ok((img, icc)) } /// A decoded image as the image crate's: 8- and 16-bit gray, gray and alpha, RGB and RGBA as they @@ -224,8 +237,9 @@ fn dynamic(img: craft_image_codecs::Image) -> Option { /// The image's size and the bytes a pixel of it takes decoded ([`decode_as`]), read from its /// header: palettes as RGB (RGBA with a transparent entry; GIF frames always RGBA), bitmaps as 8-bit -/// gray, 16-bit samples as two bytes. A PNG whose header alone is there is read from its `IHDR`. -fn decoded_layout(bytes: &[u8], format: craft_image_codecs::Format) -> Option<(u32, u32, u64)> { +/// gray, 16-bit samples as two bytes. A PNG whose header alone is there is read from its `IHDR`; +/// a format whose header says only its size (TGA, ICO and the like) counts RGBA. +pub fn decoded_layout(bytes: &[u8], format: craft_image_codecs::Format) -> Option<(u32, u32, u64)> { use craft_image_codecs::{Format as F, RasterColorType as T}; if let Some(i) = craft_image_codecs::inspect_raster(bytes).ok().flatten() { let deep = i.bits_per_sample > 8 && i.color_type != T::Indexed; @@ -237,7 +251,10 @@ fn decoded_layout(bytes: &[u8], format: craft_image_codecs::Format) -> Option<(u }; return Some((i.width, i.height, channels * if deep { 2 } else { 1 })); } - if format != F::Png || bytes.get(12..16)? != b"IHDR" { + if format != F::Png { + return vectorcraft_doc::header_size(bytes).map(|(w, h)| (w, h, 4)); + } + if bytes.get(12..16)? != b"IHDR" { return None; } let be = |at: usize| Some(u32::from_be_bytes(bytes.get(at..at + 4)?.try_into().ok()?));