diff --git a/Cargo.lock b/Cargo.lock index 62062913b0..cf8bb8c76d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6690,6 +6690,7 @@ dependencies = [ "chrono", "craft-image-codecs", "craft-stream-codecs", + "flate2", "image", "jpeg-encoder", "kurbo 0.13.1", diff --git a/crates/doc/Cargo.toml b/crates/doc/Cargo.toml index 930fc2df7d..a7d2850a1e 100644 --- a/crates/doc/Cargo.toml +++ b/crates/doc/Cargo.toml @@ -28,6 +28,7 @@ thiserror = { workspace = true } chrono = { workspace = true } [dev-dependencies] +flate2 = { workspace = true } # The CMYK JPEG reader `cmyk` had before craft-image-codecs, kept as the reference its inks match. zune-jpeg = { workspace = true } jpeg-encoder = { workspace = true } diff --git a/crates/doc/src/decode.rs b/crates/doc/src/decode.rs index 2eec586e71..24dea325f7 100644 --- a/crates/doc/src/decode.rs +++ b/crates/doc/src/decode.rs @@ -1,15 +1,15 @@ //! Images decoded through craft-image-codecs as the image crate gave them (#778): the renderer, -//! File › Open and Place, and SVG import read their pixels with [`decode_image`], and size them -//! from their header with [`decoded_layout`]. +//! File › Open and Place, SVG import, and embedded and linked images read their pixels with +//! [`decode_image`] and [`decode_within`], and size them from their header with [`decoded_layout`]. + +/// Decoded pixels, as stored and as the image crate gave them, and the ICC profile the file embeds. +pub type DecodedImage = (image::DynamicImage, Option>); /// `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. -/// The renderer, File › Open and Place, and SVG import read images with it. -pub fn decode_image( - format: craft_image_codecs::Format, - bytes: &[u8], - allowance: u64, -) -> Result<(image::DynamicImage, Option>), craft_image_codecs::CodecError> { +/// The renderer, File › Open and Place, SVG import, and embedded and linked images read images +/// with it. +pub fn decode_image(format: craft_image_codecs::Format, bytes: &[u8], allowance: u64) -> Result { 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, @@ -20,6 +20,26 @@ pub fn decode_image( Ok((img, icc)) } +/// `bytes` decoded within `max` bytes, counted as the file's samples and as RGBA (#1717), the +/// pixels as stored and as the image crate gave them, with the ICC profile the file embeds. +/// Image blobs read their pixels with it. `Ok(None)` when they would take more than `max` as +/// samples or as RGBA, or when the decode needs more than `max`. `Err` when the bytes aren't an +/// image the decoder reads. +pub fn decode_within(bytes: &[u8], max: u64) -> Result, craft_image_codecs::CodecError> { + let format = craft_image_codecs::detect(bytes).ok_or(craft_image_codecs::CodecError::UnknownFormat)?; + let (w, h, per_pixel) = + decoded_layout(bytes, format).ok_or_else(|| craft_image_codecs::CodecError::InvalidImage("the image's size can't be read".into()))?; + let pixels = u64::from(w) * u64::from(h); + if pixels.saturating_mul(per_pixel) > max || pixels.saturating_mul(4) > max { + return Ok(None); + } + match decode_image(format, bytes, max) { + Ok(img) => Ok(Some(img)), + Err(craft_image_codecs::CodecError::LimitExceeded(_)) => Ok(None), + Err(e) => Err(e), + } +} + /// A decoded image as the image crate's: 8- and 16-bit gray, gray and alpha, RGB and RGBA as they /// are, 32-bit float RGB and RGBA as they are, anything else as 8-bit RGBA. fn dynamic(img: craft_image_codecs::Image) -> Option { diff --git a/crates/doc/src/links.rs b/crates/doc/src/links.rs index 80740b7cc2..4a2c36c09a 100644 --- a/crates/doc/src/links.rs +++ b/crates/doc/src/links.rs @@ -155,22 +155,12 @@ impl ImageBlob { /// [`Self::checked_with_proxy`], with the pixels it decoded: `None` beside the blob when it /// decoded none (an image past the allowance). pub fn checked_with_pixels(mut self, proxy: bool) -> Option<(Self, Option)> { - let max = crate::MAX_RASTER_ALLOC; - let reader = || image::ImageReader::new(Cursor::new(self.bytes.as_slice())).with_guessed_format().ok(); - let (w, h) = reader()?.into_dimensions().ok()?; - if u64::from(w) * u64::from(h) > max / 4 { - return Some((self, None)); - } - let mut r = reader()?; - let mut limits = image::Limits::default(); - limits.max_alloc = Some(max); - r.limits(limits); - let img = match r.decode() { - Ok(img) => img, - Err(image::ImageError::Limits(_)) => return Some((self, None)), + let img = match crate::decode::decode_within(&self.bytes, crate::MAX_RASTER_ALLOC) { + Ok(Some((img, _))) => img, + Ok(None) => return Some((self, None)), Err(_) => return None, }; - if proxy && self.proxy.is_none() && w.max(h) > PROXY_SIZE { + if proxy && self.proxy.is_none() && img.width().max(img.height()) > PROXY_SIZE { self.proxy = proxy_of(&img).map(Arc::new); } Some((self, Some(img))) @@ -210,21 +200,16 @@ pub fn missing_placeholder() -> Option> { /// `bytes` decoded and shrunk to fit [`PROXY_SIZE`] as a PNG; `None` when small enough already or /// undecodable. fn proxy_png(bytes: &[u8]) -> Option> { - let (w, h) = image::ImageReader::new(Cursor::new(bytes)).with_guessed_format().ok()?.into_dimensions().ok()?; + let format = craft_image_codecs::detect(bytes)?; + let (w, h, _) = crate::decode::decoded_layout(bytes, format)?; if w.max(h) <= PROXY_SIZE { return None; } - // The proxy decodes within the project's raster cap, counted as RGBA like every other - // decode path (#3597): the header alone decides whether the pixels could ever fit, and the - // decoder gets the same budget instead of image's own default. - if u64::from(w) * u64::from(h) > crate::MAX_RASTER_ALLOC / 4 { - return None; - } - let mut reader = image::ImageReader::new(Cursor::new(bytes)).with_guessed_format().ok()?; - let mut limits = image::Limits::default(); - limits.max_alloc = Some(crate::MAX_RASTER_ALLOC); - reader.limits(limits); - proxy_of(&reader.decode().ok()?) + // The proxy decodes within the project's raster cap, counted as the file's samples and as + // RGBA like every other decode path (#3597, #1717): the header alone decides whether the + // pixels could ever fit, and the decoder gets the same budget. + let (img, _) = crate::decode::decode_within(bytes, crate::MAX_RASTER_ALLOC).ok()??; + proxy_of(&img) } /// Decoded image `img` shrunk to fit [`PROXY_SIZE`] as a PNG. diff --git a/crates/doc/src/pixels.rs b/crates/doc/src/pixels.rs index f589ca9eda..64ff0752d2 100644 --- a/crates/doc/src/pixels.rs +++ b/crates/doc/src/pixels.rs @@ -20,15 +20,7 @@ pub type PixelPick<'a> = (Option<[f64; 4]>, usize, &'a dyn Fn(f64, f64) -> bool) /// the header first: the decoder's limit is on the file's samples, and a gray image is a quarter /// of what it becomes here (#1717). fn rgba_within(bytes: &[u8], max: u64) -> Option { - let (w, h) = image::ImageReader::new(Cursor::new(bytes)).with_guessed_format().ok()?.into_dimensions().ok()?; - if u64::from(w) * u64::from(h) > max / 4 { - return None; - } - let mut reader = image::ImageReader::new(Cursor::new(bytes)).with_guessed_format().ok()?; - let mut limits = image::Limits::default(); - limits.max_alloc = Some(max); - reader.limits(limits); - Some(reader.decode().ok()?.into_rgba8()) + Some(crate::decode::decode_within(bytes, max).ok()??.0.into_rgba8()) } /// An image file's stored size in pixels, read from its header by craft-image-codecs without @@ -399,30 +391,43 @@ mod tests { assert!(ImageBlob::png(b"not a png".to_vec()).sample_pixels(&[(None, 100, &any)]).is_none()); } - /// A GIF whose `w` × `h` screen holds one opaque red pixel at the top left. The file holds no - /// other pixels, and the decoder makes them transparent. - fn gif_with_one_red_pixel(w: u16, h: u16) -> Vec { - let mut gif = b"GIF89a".to_vec(); - gif.extend_from_slice(&w.to_le_bytes()); - gif.extend_from_slice(&h.to_le_bytes()); - // Flags for a global color table of two colors, the background and aspect bytes, then the - // table: red and black. - gif.extend_from_slice(&[0x80, 0, 0, 255, 0, 0, 0, 0, 0]); - // A 1 × 1 image at (0, 0), its LZW data (clear, color 0, end), and the end of the file. - gif.extend_from_slice(&[0x2C, 0, 0, 0, 0, 1, 0, 1, 0, 0, 2, 2, 0x44, 0x01, 0, 0x3B]); - gif + /// An RGBA PNG of `w` × `h` pixels with one opaque red pixel at the top left, the rest + /// transparent. Written row by row so the 11,586² allowance image is not held decoded + /// while it is encoded. + fn png_with_one_red_pixel(w: u32, h: u32) -> Vec { + use std::io::Write; + let mut z = flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::fast()); + let mut row = vec![0u8; 1 + w as usize * 4]; + row[1..5].copy_from_slice(&[255, 0, 0, 255]); + z.write_all(&row).unwrap(); + row[1..5].fill(0); + for _ in 1..h { + z.write_all(&row).unwrap(); + } + let idat = [&b"IDAT"[..], &z.finish().unwrap()].concat(); + let ihdr = [&b"IHDR"[..], &w.to_be_bytes(), &h.to_be_bytes(), &[8, 6, 0, 0, 0]].concat(); + let mut out = b"\x89PNG\r\n\x1a\n".to_vec(); + for chunk in [&ihdr[..], &idat[..], b"IEND"] { + out.extend((chunk.len() as u32 - 4).to_be_bytes()); + out.extend(chunk); + let mut crc = flate2::Crc::new(); + crc.update(chunk); + out.extend(crc.sum().to_be_bytes()); + } + out } /// The pixels are decoded within the allowance, not within the decoder's own 512 MB: an RGBA - /// image of 11,586 × 11,586 pixels (536,941,584 bytes) is recolored and sampled. + /// image of 11,586 × 11,586 pixels (536,941,584 bytes) is recolored and sampled. A PNG, not a + /// sparse GIF: craft-image-codecs refuses a GIF whose unbacked logical screen is past 256 MiB. #[test] fn pixels_past_the_decoders_own_limit_are_decoded_within_the_allowance() { let (w, h) = (11_586, 11_586); let rgba = u64::from(w) * u64::from(h) * 4; assert!(rgba > 512 << 20); - let gif = gif_with_one_red_pixel(w, h); - assert!(rgba_within(&gif, rgba - 1).is_none(), "past the allowance"); - let b = ImageBlob::new("image/gif", gif); + let png = png_with_one_red_pixel(w, h); + assert!(rgba_within(&png, rgba - 1).is_none(), "past the allowance"); + let b = ImageBlob::png(png); assert!(b.map_rgb(|[r, g, bl]| [bl, g, r]).is_some(), "red becomes blue"); assert_eq!(b.sample(0.5, 0.5, 1), Some([255, 0, 0, 255])); assert_eq!(b.sample(f64::from(w) - 0.5, f64::from(h) - 0.5, 1), None, "transparent"); @@ -570,4 +575,55 @@ mod tests { assert_eq!(la8.coverage(), Some((4, 1, vec![255, 0, 204, 128]))); assert_eq!(ImageBlob::png(vec![1, 2, 3]).coverage(), None); } + + /// Embedded-image pixels through craft-image-codecs (#778) are those the image crate decoded: + /// the same layout and samples for PNG, GIF, WebP, TIFF and BMP at each depth the image + /// crate writes, and JPEG samples within two steps. + #[test] + fn decoded_pixels_are_those_the_image_crate_read() { + use image::{DynamicImage as D, ImageFormat as F}; + let rgba = + image::RgbaImage::from_fn(9, 5, |x, y| image::Rgba([(x * 29) as u8, (y * 47) as u8, ((x + 2 * y) * 9) as u8, 255 - (x * y * 6) as u8])); + let deep = image::ImageBuffer::, _>::from_fn(9, 5, |x, y| { + image::Rgba([(x * 7_001) as u16, (y * 12_007) as u16, 5, 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::ImageLuma16(D::ImageRgba16(deep).to_luma16()), + ]; + let mut checked = 0; + for img in &images { + for f in [F::Png, F::Gif, F::WebP, F::Tiff, F::Bmp, F::Jpeg] { + let mut bytes = Vec::new(); + if img.write_to(&mut Cursor::new(&mut bytes), f).is_err() { + continue; + } + let what = format!("{f:?} {:?}", img.color()); + let want = image::load_from_memory_with_format(&bytes, f).unwrap(); + let (got, _) = crate::decode::decode_within(&bytes, MAX_RASTER_ALLOC) + .unwrap_or_else(|e| panic!("{what}: {e}")) + .unwrap_or_else(|| panic!("{what}: past the allowance")); + let tolerance = if f == F::Jpeg { 2 * 257 } else { 0 }; + if f != F::Jpeg { + assert_eq!(got.color(), want.color(), "{what}"); + } + let worst = got.to_rgba16().as_raw().iter().zip(want.to_rgba16().as_raw()).map(|(a, b)| a.abs_diff(*b)).max(); + assert!(worst.is_some_and(|w| w <= tolerance), "{what}: {worst:?}"); + // Link previews and the checked-image path read the same pixels. + let handed = ImageBlob::new(format!("image/{f:?}").to_ascii_lowercase(), bytes.clone()) + .checked_with_pixels(false) + .unwrap_or_else(|| panic!("{what}: checked")) + .1 + .unwrap_or_else(|| panic!("{what}: checked past the allowance")); + assert_eq!(handed.color(), got.color(), "{what} checked"); + assert_eq!(handed.as_bytes(), got.as_bytes(), "{what} checked"); + checked += 1; + } + } + assert!(checked >= 20, "{checked}"); + } }