diff --git a/crates/doc/src/decode.rs b/crates/doc/src/decode.rs new file mode 100644 index 0000000000..2eec586e71 --- /dev/null +++ b/crates/doc/src/decode.rs @@ -0,0 +1,79 @@ +//! 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`]. + +/// `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> { + 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 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 +/// are, 32-bit float RGB and RGBA as they are, anything else as 8-bit RGBA. +fn dynamic(img: craft_image_codecs::Image) -> Option { + use craft_image_codecs::{ChannelLayout as L, SampleType as S}; + use image::{DynamicImage as D, ImageBuffer as B}; + let (w, h) = img.dimensions(); + Some(match (img.layout(), img.sample_type()) { + (L::Gray, S::U8) => D::ImageLuma8(B::from_raw(w, h, img.into_data())?), + (L::GrayA, S::U8) => D::ImageLumaA8(B::from_raw(w, h, img.into_data())?), + (L::Rgb, S::U8) => D::ImageRgb8(B::from_raw(w, h, img.into_data())?), + (L::Rgba, S::U8) => D::ImageRgba8(B::from_raw(w, h, img.into_data())?), + (layout @ (L::Gray | L::GrayA | L::Rgb | L::Rgba), S::U16) => { + let v = img.to_u16_samples()?; + match layout { + L::Gray => D::ImageLuma16(B::from_raw(w, h, v)?), + L::GrayA => D::ImageLumaA16(B::from_raw(w, h, v)?), + L::Rgb => D::ImageRgb16(B::from_raw(w, h, v)?), + _ => D::ImageRgba16(B::from_raw(w, h, v)?), + } + } + _ => D::ImageRgba8(B::from_raw(w, h, img.to_rgba8())?), + }) +} + +/// 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`; +/// 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; + let channels = match i.color_type { + _ if format == F::Gif => 4, + T::Indexed => 3 + u64::from(i.transparent_index.is_some()), + T::Bitmap => 1, + _ => u64::from(i.channels.max(1)), + }; + return Some((i.width, i.height, channels * if deep { 2 } else { 1 })); + } + if format != F::Png { + return crate::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()?)); + let (depth, color) = (*bytes.get(24)?, *bytes.get(25)?); + let channels: u64 = match color { + 0 => 1, + 2 | 3 => 3, + 4 => 2, + 6 => 4, + _ => return None, + }; + Some((be(16)?, be(20)?, channels * if depth == 16 { 2 } else { 1 })) +} diff --git a/crates/doc/src/lib.rs b/crates/doc/src/lib.rs index 987c3a206d..0e9544b5c5 100644 --- a/crates/doc/src/lib.rs +++ b/crates/doc/src/lib.rs @@ -14,6 +14,7 @@ pub mod cmyk; pub mod color_mask; pub mod compound_shape; pub mod corners; +pub mod decode; pub mod depth; pub mod dimension; pub mod graph; diff --git a/crates/engine/src/cmd/fileio/load.rs b/crates/engine/src/cmd/fileio/load.rs index 02af949913..e26d9a765a 100644 --- a/crates/engine/src/cmd/fileio/load.rs +++ b/crates/engine/src/cmd/fileio/load.rs @@ -163,10 +163,10 @@ fn image_format(f: craft_image_codecs::Format) -> Option<&'static Format> { } /// 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. +/// ([`vectorcraft_doc::decode::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)?; + let (w, h, per_pixel) = vectorcraft_doc::decode::decoded_layout(bytes, format)?; Some((format, w, h, per_pixel)) } @@ -1026,12 +1026,12 @@ pub fn raster_image(bytes: &[u8]) -> Result { // 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"))?; + vectorcraft_doc::decode::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"))); } - let (img, profile) = vectorcraft_render::decode_image(kind, bytes, MAX_RASTER_ALLOC).map_err(err)?; + let (img, profile) = vectorcraft_doc::decode::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 { @@ -1111,7 +1111,7 @@ pub fn texture_image(bytes: &[u8]) -> Result { } None => { 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(); + let img = vectorcraft_doc::decode::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/render/src/lib.rs b/crates/render/src/lib.rs index 39fb455e91..c49671fd8c 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, decode_image, decoded_layout, image_decodes, reduced_size}; +pub use paint::{decode_failures, 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 5d3a91e752..c4c68a596c 100644 --- a/crates/render/src/paint.rs +++ b/crates/render/src/paint.rs @@ -190,83 +190,7 @@ 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 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 -/// are, 32-bit float RGB and RGBA as they are, anything else as 8-bit RGBA. -fn dynamic(img: craft_image_codecs::Image) -> Option { - use craft_image_codecs::{ChannelLayout as L, SampleType as S}; - use image::{DynamicImage as D, ImageBuffer as B}; - let (w, h) = img.dimensions(); - Some(match (img.layout(), img.sample_type()) { - (L::Gray, S::U8) => D::ImageLuma8(B::from_raw(w, h, img.into_data())?), - (L::GrayA, S::U8) => D::ImageLumaA8(B::from_raw(w, h, img.into_data())?), - (L::Rgb, S::U8) => D::ImageRgb8(B::from_raw(w, h, img.into_data())?), - (L::Rgba, S::U8) => D::ImageRgba8(B::from_raw(w, h, img.into_data())?), - (layout @ (L::Gray | L::GrayA | L::Rgb | L::Rgba), S::U16) => { - let v = img.to_u16_samples()?; - match layout { - L::Gray => D::ImageLuma16(B::from_raw(w, h, v)?), - L::GrayA => D::ImageLumaA16(B::from_raw(w, h, v)?), - L::Rgb => D::ImageRgb16(B::from_raw(w, h, v)?), - _ => D::ImageRgba16(B::from_raw(w, h, v)?), - } - } - _ => D::ImageRgba8(B::from_raw(w, h, img.to_rgba8())?), - }) -} - -/// 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`; -/// 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; - let channels = match i.color_type { - _ if format == F::Gif => 4, - T::Indexed => 3 + u64::from(i.transparent_index.is_some()), - T::Bitmap => 1, - _ => u64::from(i.channels.max(1)), - }; - return Some((i.width, i.height, channels * if deep { 2 } else { 1 })); - } - 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()?)); - let (depth, color) = (*bytes.get(24)?, *bytes.get(25)?); - let channels: u64 = match color { - 0 => 1, - 2 | 3 => 3, - 4 => 2, - 6 => 4, - _ => return None, - }; - Some((be(16)?, be(20)?, channels * if depth == 16 { 2 } else { 1 })) + vectorcraft_doc::decode::decode_image(format, bytes, allowance).map(|(img, _)| img) } /// `bytes` decoded within [`MAX_DECODE_ALLOC`], counted as RGBA: the image's size is read from its @@ -293,8 +217,8 @@ fn decode_in_room(bytes: &[u8]) -> Result (DecodeError::Limit(m), Undecoded::Unreadable), e => unreadable(e.to_string()), @@ -360,7 +284,7 @@ struct Reduction { fn reduction(bytes: &[u8], allowance: u64) -> Option { use craft_image_codecs::Format as F; let format = drawn_format(bytes).filter(|f| matches!(f, F::Png | F::Jpeg | F::Gif | F::WebP))?; - let (w, h, per_pixel) = decoded_layout(bytes, format)?; + let (w, h, per_pixel) = vectorcraft_doc::decode::decoded_layout(bytes, format)?; let pixels = u64::from(w) * u64::from(h); let total_bytes = pixels.saturating_mul(per_pixel); // An image of more pixels than the allowance has bytes (four times what it holds as RGBA) is diff --git a/crates/svg/src/import/files.rs b/crates/svg/src/import/files.rs index 8b612f78c1..631bed3d0c 100644 --- a/crates/svg/src/import/files.rs +++ b/crates/svg/src/import/files.rs @@ -939,25 +939,17 @@ fn png_takes_icc(img: &image::DynamicImage, icc: &[u8]) -> bool { /// can read (a TIFF's read by [`vectorcraft_doc::cmyk::tiff_icc_profile`]); `None` when they /// can't be. fn decoded(bytes: &[u8], format: image::ImageFormat) -> Option<(image::DynamicImage, Option>)> { - use image::ImageDecoder as _; let max = vectorcraft_doc::MAX_RASTER_ALLOC; - let mut reader = image::ImageReader::new(std::io::Cursor::new(bytes)).with_guessed_format().ok()?; - if reader.format().is_none() { - reader.set_format(format); - } - let mut limits = image::Limits::default(); - limits.max_alloc = Some(max); - reader.limits(limits); - let tiff = reader.format() == Some(image::ImageFormat::Tiff); - let mut decoder = reader.into_decoder().ok()?; - let (w, h) = decoder.dimensions(); - if decoder.total_bytes() > max || u64::from(w) * u64::from(h) * 4 > max { + let kind = craft_image_codecs::detect(bytes).or_else(|| format.extensions_str().iter().find_map(|e| craft_image_codecs::from_extension(e)))?; + let (w, h, per_pixel) = vectorcraft_doc::decode::decoded_layout(bytes, kind)?; + let pixels = u64::from(w) * u64::from(h); + if pixels.saturating_mul(per_pixel) > max || pixels * 4 > max { return None; } - // 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 icc = if tiff { vectorcraft_doc::cmyk::tiff_icc_profile(bytes) } else { decoder.icc_profile().ok().flatten() }; - Some((image::DynamicImage::from_decoder(decoder).ok()?, icc)) + let (img, icc) = vectorcraft_doc::decode::decode_image(kind, bytes, max).ok()?; + // A TIFF's profile is read by the TIFF reader, as before. + let icc = if kind == craft_image_codecs::Format::Tiff { vectorcraft_doc::cmyk::tiff_icc_profile(bytes) } else { icc }; + Some((img, icc)) } /// The pixels of TIFF `bytes`, as stored, read as Open and Place read them: a TIFF with extra @@ -1507,4 +1499,45 @@ mod tests { )] ); } + + /// SVG import's 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 std::io::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, _) = super::decoded(&bytes, f).unwrap_or_else(|| panic!("{what}")); + 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:?}"); + checked += 1; + } + } + assert!(checked >= 20, "{checked}"); + } }