diff --git a/crates/render/Cargo.toml b/crates/render/Cargo.toml index 5e03dfdfc0..61569cc79e 100644 --- a/crates/render/Cargo.toml +++ b/crates/render/Cargo.toml @@ -21,10 +21,9 @@ vello_common = { version = "0.2", default-features = false } image = { workspace = true } craft-gif = { workspace = true } craft-image-codecs = { workspace = true } +jpeg-encoder = { workspace = true } craft-stream-codecs = { workspace = true } flate2 = { workspace = true } -# TIFF LZW compression (MIT OR Apache-2.0). -weezl = "0.2" log = { workspace = true } # Combining marks inside a lam-alef cluster (MIT OR Apache-2.0; already used by vectorcraft-text). unicode-general-category = "1" @@ -38,6 +37,8 @@ vello_cpu = { workspace = true, features = ["multithreading"] } winsafe = { version = "0.0.29", default-features = false, features = ["kernel"] } [dev-dependencies] +# The TIFF LZW writer before craft-stream-codecs, the reference encode/tests_tiff.rs compares it with. +weezl = "0.2" vectorcraft-testkit = { workspace = true } # A variable font built in code (no font file is committed). vectorcraft-text = { workspace = true, features = ["test-fonts"] } diff --git a/crates/render/src/encode/jpeg.rs b/crates/render/src/encode/jpeg.rs index 403a07558b..596399c3e7 100644 --- a/crates/render/src/encode/jpeg.rs +++ b/crates/render/src/encode/jpeg.rs @@ -5,7 +5,7 @@ use std::collections::HashMap; -use craft_image_codecs::{ChannelLayout, EncodeOptions, Format, Image, JpegFormat}; +use jpeg_encoder::{ColorType, Density, Encoder}; use vectorcraft_color::cms::{self, lab}; use crate::Rendered; @@ -124,41 +124,97 @@ impl Default for JpegOptions { /// Encode `px` (`o.color_model`'s channels per pixel: RGB, CMYK ink amounts with 0 = no ink, or /// grey) at `quality` 0–100, with the resolution `ppi` (if any) in the JFIF header. pub fn encode(px: &[u8], width: u32, height: u32, quality: u8, ppi: Option, o: &JpegOptions) -> Result, String> { - if u16::try_from(width).is_err() || u16::try_from(height).is_err() { + let fail = |e: jpeg_encoder::EncodingError| format!("JPEG encoding failed: {e}"); + let (Ok(w), Ok(h)) = (u16::try_from(width), u16::try_from(height)) else { return Err(format!("{width} × {height} pixels is too large for JPEG (at most 65535 pixels a side): lower the resolution")); - } + }; if px.len() as u64 != u64::from(width) * u64::from(height) * o.color_model.channels() as u64 { return Err("JPEG encoding failed: pixel buffer doesn't match the image size".into()); } - let layout = match o.color_model { - ColorModel::Rgb => ChannelLayout::Rgb, - ColorModel::Cmyk => ChannelLayout::Cmyk, - ColorModel::Gray => ChannelLayout::Gray, - }; - let mut img = Image::from_u8(width, height, layout, px.to_vec()).map_err(|e| format!("JPEG encoding failed: {e}"))?; - if let Some(ppi) = ppi.filter(|p| p.is_finite() && *p >= 1.0) { - let d = ppi.round().clamp(1.0, f64::from(u16::MAX)) as f32; - img.meta.dpi = Some((d, d)); + let mut buf = Vec::new(); + let mut enc = Encoder::new(&mut buf, quality.clamp(1, 100)); + if let Some(ppi) = ppi { + let d = ppi.round().clamp(1.0, u16::MAX as f64) as u16; + enc.set_density(Density::Inch { x: d, y: d }); + } + match o.method { + Method::Baseline => {} + Method::Optimized => enc.set_optimized_huffman_tables(true), + Method::Progressive => { + enc.set_optimized_huffman_tables(true); + enc.set_progressive_scans(o.scans.clamp(*SCANS.start(), *SCANS.end())); + } } if o.embed_icc { let icc = cms::icc_bytes(&o.color_model.profile()).map_err(|e| format!("JPEG colour profile: {e}"))?; - img.icc = Some(icc.to_vec()); + enc.add_icc_profile(&icc).map_err(fail)?; } - let (jpeg_format, jpeg_progressive_scans) = match o.method { - Method::Baseline => (JpegFormat::Baseline, None), - Method::Optimized => (JpegFormat::Optimized, None), - Method::Progressive => (JpegFormat::Progressive, Some(o.scans.clamp(*SCANS.start(), *SCANS.end()))), + let ty = match o.color_model { + ColorModel::Rgb => ColorType::Rgb, + ColorModel::Cmyk => ColorType::Cmyk, + ColorModel::Gray => ColorType::Luma, }; - let opts = EncodeOptions { - jpeg_quality: quality.clamp(1, 100), - jpeg_chroma_subsampling: false, - jpeg_format, - jpeg_progressive_scans, - embed_icc: o.embed_icc, - embed_metadata: true, - ..Default::default() + enc.encode(px, w, h, ty).map_err(fail)?; + // JFIF describes one- and three-component images (greyscale and YCbCr), so its APP0 segment + // must not head a four-component CMYK frame: the file then says two different things about + // its colours, and a strict reader rejects it rather than guessing. Adobe's own CMYK JPEGs + // carry the APP14 marker the encoder already writes, and no JFIF (#3084). + if o.color_model == ColorModel::Cmyk { + unlabel_jfif(&mut buf, ppi); + } + Ok(buf) +} + +/// Replace the leading APP0/JFIF segment, which the encoder always writes, with an EXIF segment +/// carrying the same resolution (TIFF `XResolution`, `YResolution` and `ResolutionUnit`, which is +/// where a reader looks when there is no JFIF). Without a resolution the segment is simply +/// dropped. +fn unlabel_jfif(buf: &mut Vec, ppi: Option) { + // SOI, then APP0 with its two length bytes and the tag it is identified by. + let Some(head) = buf.get(..13) else { return }; + if head[..4] != [0xFF, 0xD8, 0xFF, 0xE0] || &head[6..11] != b"JFIF\0" { + return; + } + let len = usize::from(u16::from_be_bytes([head[4], head[5]])); + if 4 + len > buf.len() { + return; + } + let exif = ppi.filter(|p| p.is_finite() && *p >= 1.0).map(|p| exif_resolution(p.round() as u32)).unwrap_or_default(); + buf.splice(2..4 + len, exif); +} + +/// An APP1 EXIF segment holding `dpi` as IFD0's resolution: big-endian TIFF, three entries, each +/// rational pointing past the directory. +fn exif_resolution(dpi: u32) -> Vec { + let mut tiff = Vec::new(); + tiff.extend_from_slice(b"MM\0*"); // big-endian, TIFF magic + tiff.extend_from_slice(&8u32.to_be_bytes()); // IFD0 right after the header + tiff.extend_from_slice(&3u16.to_be_bytes()); // three entries + // Entry: tag, type (5 = rational, 3 = short), count, then the value or its offset. + let entry = |tag: u16, ty: u16, value: [u8; 4]| { + let mut e = Vec::with_capacity(12); + e.extend_from_slice(&tag.to_be_bytes()); + e.extend_from_slice(&ty.to_be_bytes()); + e.extend_from_slice(&1u32.to_be_bytes()); + e.extend_from_slice(&value); + e }; - craft_image_codecs::encode(&img, Format::Jpeg, &opts).map_err(|e| format!("JPEG encoding failed: {e}")) + // The two rationals sit after the directory: header 8 + count 2 + 3 entries + next-IFD 4. + let after = 8 + 2 + 3 * 12 + 4; + tiff.extend(entry(282, 5, (after as u32).to_be_bytes())); // XResolution + tiff.extend(entry(283, 5, (after as u32 + 8).to_be_bytes())); // YResolution + tiff.extend(entry(296, 3, [0, 2, 0, 0])); // ResolutionUnit: inches + tiff.extend_from_slice(&0u32.to_be_bytes()); // no next IFD + for _ in 0..2 { + tiff.extend_from_slice(&dpi.to_be_bytes()); + tiff.extend_from_slice(&1u32.to_be_bytes()); + } + let mut seg = vec![0xFF, 0xE1]; + let body_len = 6 + tiff.len() + 2; + seg.extend_from_slice(&u16::try_from(body_len).unwrap_or(u16::MAX).to_be_bytes()); + seg.extend_from_slice(b"Exif\0\0"); + seg.extend_from_slice(&tiff); + seg } /// `img` flattened on white as RGB, or as grey with each colour's lightness. diff --git a/crates/render/src/encode/tests_tiff.rs b/crates/render/src/encode/tests_tiff.rs index e10739e67a..392e4ed6d2 100644 --- a/crates/render/src/encode/tests_tiff.rs +++ b/crates/render/src/encode/tests_tiff.rs @@ -202,3 +202,33 @@ fn packbits_round_trips() { assert_eq!(out, data); } } + +/// TIFF's LZW strips through craft-stream-codecs (#778) read back as the strip they were made of +/// with weezl, the writer before them, and take no more bytes than weezl's: the same bytes until +/// the code table first fills, after which the two clear it at different codes. +#[test] +fn lzw_strips_read_back_as_weezl_reads_them() { + let mut seed = 0x9e37_79b9_7f4a_7c15u64; + let mut noise = |n: usize, spread: u64| { + (0..n) + .map(|_| { + seed ^= seed << 13; + seed ^= seed >> 7; + seed ^= seed << 17; + (seed % spread) as u8 + }) + .collect::>() + }; + let strips = + [vec![], vec![7], vec![0; 70_000], noise(8_192, 256), noise(65_536, 4), noise(200_000, 256), (0..=255).cycle().take(30_000).collect()]; + for strip in &strips { + let weezl = weezl::encode::Encoder::with_tiff_size_switch(weezl::BitOrder::Msb, 8).encode(strip).unwrap(); + let craft = craft_stream_codecs::lzw::encode(strip, true); + let back = weezl::decode::Decoder::with_tiff_size_switch(weezl::BitOrder::Msb, 8).decode(&craft).unwrap(); + assert!(back == *strip, "{} bytes read back", strip.len()); + assert!(craft.len() <= weezl.len(), "{} bytes: {} against {}", strip.len(), craft.len(), weezl.len()); + // Up to the first table reset (4,093 codes of at most 12 bits) the codes are the same. + let same = craft.iter().zip(&weezl).take_while(|(a, b)| a == b).count(); + assert!(same >= weezl.len().min(4_300), "{} bytes: the same for {same}", strip.len()); + } +} diff --git a/crates/render/src/paint.rs b/crates/render/src/paint.rs index 8650ae7886..45a3153fcf 100644 --- a/crates/render/src/paint.rs +++ b/crates/render/src/paint.rs @@ -157,30 +157,131 @@ pub fn image_decodes() -> u64 { /// [`decode_in_room`]'s image or error. #[cfg(test)] -pub(crate) fn decode_within_allowance(bytes: &[u8]) -> image::ImageResult { +pub(crate) fn decode_within_allowance(bytes: &[u8]) -> Result { decode_in_room(bytes).map_err(|(e, _)| e) } +/// Why [`decode_in_room`] gave no image. +#[derive(Debug)] +pub(crate) enum DecodeError { + /// The image takes more than the decode allowance, or than the room the images being painted + /// leave: it may be read at [`reduced_size`]. + Limit(String), + /// Its pixels can't be read. + Unreadable(String), +} + +impl std::fmt::Display for DecodeError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + DecodeError::Limit(why) => write!(f, "the image is too large to decode: {why}"), + DecodeError::Unreadable(why) => write!(f, "the image's pixels can't be read: {why}"), + } + } +} + +/// The formats the renderer draws, told from their bytes: those the image crate read for it +/// before craft-image-codecs did (#778). TGA has no signature to tell it by and was never drawn. +fn drawn_format(bytes: &[u8]) -> Option { + use craft_image_codecs::Format as F; + craft_image_codecs::detect(bytes).filter(|f| matches!(f, F::Png | F::Jpeg | F::Gif | F::WebP | F::Tiff | F::Bmp)) +} + +/// `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 { + 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())) +} + +/// 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`. +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 || 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 })) +} + /// `bytes` decoded within [`MAX_DECODE_ALLOC`], counted as RGBA: the image's size is read from its /// header first, and an image of more than that as RGBA gets a limit error before anything is /// decoded (#1717). A failure comes with why the image gave no pixels ([`room_for`]). -fn decode_in_room(bytes: &[u8]) -> Result { +fn decode_in_room(bytes: &[u8]) -> Result { let allowance = allowance(); - let unreadable = |e| (e, Undecoded::Unreadable); - let reader = || -> image::ImageResult<_> { - let mut r = image::ImageReader::new(std::io::Cursor::new(bytes)).with_guessed_format()?; - let mut limits = image::Limits::default(); - limits.max_alloc = Some(allowance); - r.limits(limits); - Ok(r) + let unreadable = |e: String| (DecodeError::Unreadable(e), Undecoded::Unreadable); + let format = drawn_format(bytes).ok_or_else(|| unreadable("not an image the renderer draws".into()))?; + let (w, h) = match vectorcraft_doc::header_size(bytes) { + Some(size) => size, + None => { + return Err(match craft_image_codecs::probe_size(bytes) { + Err(craft_image_codecs::CodecError::LimitExceeded(e)) => (DecodeError::Limit(e), Undecoded::Unreadable), + Err(e) => unreadable(e.to_string()), + Ok(_) => unreadable("the image's size can't be read".into()), + }); + } }; - let (w, h) = reader().and_then(|r| r.into_dimensions()).map_err(unreadable)?; - let bytes = (u64::from(w) * u64::from(h)).saturating_mul(4); + let rgba = (u64::from(w) * u64::from(h)).saturating_mul(4); let pinned = crate::images::store().pinned_bytes() as u64; - if let Err(why) = room_for(bytes, pinned) { - return Err((image::ImageError::Limits(image::error::LimitError::from_kind(image::error::LimitErrorKind::InsufficientMemory)), why)); + if let Err(why) = room_for(rgba, pinned) { + return Err((DecodeError::Limit(format!("{w} × {h} pixels take {rgba} bytes as RGBA")), why)); } - reader().and_then(|r| r.decode()).map_err(unreadable) + // Within the allowance as RGBA and as decoded, a limit the decoder meets is a file that can't + // hold the pixels its header declares: they can't be read. + let samples_fit = + decoded_layout(bytes, format).is_some_and(|(_, _, per_pixel)| (u64::from(w) * u64::from(h)).saturating_mul(per_pixel) <= allowance); + decode_as(format, bytes, allowance).map_err(|e| match e { + craft_image_codecs::CodecError::LimitExceeded(m) if !samples_fit => (DecodeError::Limit(m), Undecoded::Unreadable), + e => unreadable(e.to_string()), + }) } /// Whether an image of `rgba` bytes may be decoded while the images being painted hold `pinned` @@ -240,40 +341,33 @@ struct Reduction { /// [`reduced_size`]'s reading of `bytes` within `allowance`. fn reduction(bytes: &[u8], allowance: u64) -> Option { - use image::ImageDecoder as _; - let mut reader = image::ImageReader::new(std::io::Cursor::new(bytes)).with_guessed_format().ok()?; - let format = reader.format()?; - if !matches!(format, image::ImageFormat::Png | image::ImageFormat::Jpeg | image::ImageFormat::Gif | image::ImageFormat::WebP) { - return None; - } - let mut limits = image::Limits::default(); - limits.max_alloc = Some(allowance); - reader.limits(limits); - let d = reader.into_decoder().ok()?; - let (w, h) = d.dimensions(); + 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 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 // not read. - if pixels == 0 || pixels > allowance || (pixels.saturating_mul(4) <= allowance && d.total_bytes() <= allowance) { + if pixels == 0 || pixels > allowance || (pixels.saturating_mul(4) <= allowance && total_bytes <= allowance) { return None; } let to = fitted_dims(w, h, allowance)?; // A PNG that isn't interlaced or animated (and upright in pre-pixel EXIF) is read a row at a time when the reader's buffers fit the allowance. - let row_scratch = if format == image::ImageFormat::Png { + let row_scratch = if format == F::Png { let streamed = craft_image_codecs::PngDecodeRows::new_streaming(bytes, &png_streaming_limits(allowance)) .ok() .filter(|dec| dec.streamable()) .and_then(|dec| png_row_scratch(&dec.header())); - streamed.or_else(|| png_row_scratch_from_header(bytes, w, h, d.total_bytes())).filter(|&s| s <= allowance) + streamed.or_else(|| png_row_scratch_from_header(bytes, w, h, total_bytes)).filter(|&s| s <= allowance) } else { None }; let rows = row_scratch.is_some(); let scratch = row_scratch.unwrap_or_else(|| { - let band = u64::from(BAND) * u64::from(w) * (u64::from(d.color_type().bytes_per_pixel()) + 4); - d.total_bytes().saturating_add(band) + let band = u64::from(BAND) * u64::from(w) * (per_pixel + 4); + total_bytes.saturating_add(band) }); - (rows || d.total_bytes() <= allowance).then_some(Reduction { size: (w, h), to, rows, scratch }) + (rows || total_bytes <= allowance).then_some(Reduction { size: (w, h), to, rows, scratch }) } /// Streaming retains rows, not the declared full image. Preserve the renderer's source-pixel @@ -439,11 +533,7 @@ fn shrunk_with_png_limits(bytes: &[u8], r: &Reduction, png_limits: &craft_image_ .ok()?; } } else { - let mut reader = image::ImageReader::new(std::io::Cursor::new(bytes)).with_guessed_format().ok()?; - let mut limits = image::Limits::default(); - limits.max_alloc = Some(allowance()); - reader.limits(limits); - let img = reader.decode().ok()?; + let img = decode_as(drawn_format(bytes)?, bytes, allowance()).ok()?; let (w, h) = (img.width(), img.height()); for y in (0..h).step_by(BAND as usize) { let band = img.crop_imm(0, y, w, BAND.min(h - y)).to_rgba8(); @@ -563,7 +653,7 @@ fn decode_or_reduction(bytes: &[u8]) -> Result Ok(img), - Err((image::ImageError::Limits(_), Undecoded::Unreadable)) => Err(reduction(bytes, allowance()).ok_or(Undecoded::Unreadable)), + Err((DecodeError::Limit(_), Undecoded::Unreadable)) => Err(reduction(bytes, allowance()).ok_or(Undecoded::Unreadable)), Err((_, why)) => Err(Err(why)), } } @@ -730,6 +820,78 @@ mod tests { assert!(red_at_its_coverage(&reduced)); } + /// The renderer's decoding through craft-image-codecs gives the pixels the image crate gave it + /// (#778): the same layout and samples for PNGs of each color type and depth, GIF, WebP, TIFF and + /// BMP, and JPEG samples within two steps (the two decoders' IDCTs round differently). The EXIF + /// orientation is left to the caller, as before. + #[test] + fn decoded_pixels_are_those_the_image_crate_gave() { + use image::{DynamicImage as D, ImageFormat as F}; + let rgba = image::RgbaImage::from_fn(13, 7, |x, y| image::Rgba([(x * 19) as u8, (y * 37) as u8, ((x + y) * 11) as u8, (x * y * 5) as u8])); + let rgba16 = image::ImageBuffer::, _>::from_fn(13, 7, |x, y| { + image::Rgba([(x * 4_999) as u16, (y * 9_001) as u16, 300, 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.clone()).to_luma_alpha8()), + D::ImageRgba16(rgba16.clone()), + D::ImageRgb16(D::ImageRgba16(rgba16.clone()).to_rgb16()), + D::ImageLuma16(D::ImageRgba16(rgba16.clone()).to_luma16()), + D::ImageLumaA16(D::ImageRgba16(rgba16).to_luma_alpha16()), + ]; + let encoded = |img: &D, f: F| { + let mut out = Vec::new(); + img.write_to(&mut std::io::Cursor::new(&mut out), f).ok().map(|()| out) + }; + let mut files: Vec<(String, Vec)> = Vec::new(); + for img in &images { + for f in [F::Png, F::Tiff, F::Bmp, F::Gif, F::WebP, F::Jpeg] { + if let Some(bytes) = encoded(img, f) { + files.push((format!("{f:?} {:?}", img.color()), bytes)); + } + } + } + // A palette PNG with a transparent entry and a 1-bit gray PNG, as other writers make them. + let png = |w: u32, h: u32, depth: u8, color: u8, extra: &[&[u8]], rows: &[u8]| { + let mut z = flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::default()); + std::io::Write::write_all(&mut z, rows).unwrap(); + let idat = [&b"IDAT"[..], &z.finish().unwrap()].concat(); + let ihdr = [&b"IHDR"[..], &w.to_be_bytes(), &h.to_be_bytes(), &[depth, color, 0, 0, 0]].concat(); + let mut out = b"\x89PNG\r\n\x1a\n".to_vec(); + for chunk in [&ihdr[..]].into_iter().chain(extra.iter().copied()).chain([&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 + }; + let plte: &[u8] = b"PLTE\xff\0\0\0\xff\0\0\0\xff"; + files.push(("indexed PNG".into(), png(4, 2, 8, 3, &[plte, b"tRNS\xff\0"], &[0, 0, 1, 2, 1, 0, 2, 1, 0, 0]))); + files.push(("indexed PNG, opaque".into(), png(4, 2, 8, 3, &[plte], &[0, 0, 1, 2, 1, 0, 2, 1, 0, 0]))); + files.push(("1-bit PNG".into(), png(10, 2, 1, 0, &[], &[0, 0b1010_1100, 0b0100_0000, 0, 0b0011_1111, 0b1100_0000]))); + assert!(files.len() > 20, "{}", files.len()); + for (what, bytes) in &files { + let want = image::load_from_memory(bytes).unwrap(); + let got = decode_in_room(bytes).map_err(|(e, _)| e).unwrap(); + let jpeg = what.starts_with("Jpeg"); + if !jpeg { + assert_eq!(got.color(), want.color(), "{what}"); + } + let (g, w) = (got.to_rgba16(), want.to_rgba16()); + assert_eq!(g.dimensions(), w.dimensions(), "{what}"); + let tolerance = if jpeg { 2 * 257 } else { 0 }; + let worst = g.as_raw().iter().zip(w.as_raw()).map(|(a, b)| a.abs_diff(*b)).max().unwrap_or(0); + assert!(worst <= tolerance, "{what}: samples {worst} apart"); + // Read past the allowance at reduced size, the same image is read whole first. + let whole = shrunk(bytes, &Reduction { size: w.dimensions(), to: w.dimensions(), rows: false, scratch: 0 }).unwrap(); + assert_eq!((u32::from(whole.width()), u32::from(whole.height())), w.dimensions(), "{what}"); + } + } + /// Opaque red beside clear black, every other column of a `w` × 1 image. fn striped(w: u32) -> image::RgbaImage { image::RgbaImage::from_fn(w, 1, |x, _| if x % 2 == 0 { image::Rgba([200, 30, 30, 255]) } else { image::Rgba([0, 0, 0, 0]) }) diff --git a/crates/render/src/tests.rs b/crates/render/src/tests.rs index b562e85865..a25738c5ba 100644 --- a/crates/render/src/tests.rs +++ b/crates/render/src/tests.rs @@ -1686,16 +1686,18 @@ fn tiff_header(w: u32, h: u32) -> Vec { #[test] fn an_image_too_large_as_rgba_is_not_decoded() { let over = paint::decode_within_allowance(&png_header(23_171, 23_170)); - assert!(matches!(over, Err(image::ImageError::Limits(_))), "{over:?}"); + assert!(matches!(over, Err(paint::DecodeError::Limit(_))), "{over:?}"); + assert!(over.unwrap_err().to_string().starts_with("the image is too large to decode: ")); let failed = decode_failures(); assert!(paint::decode_pixmap(&png_header(23_171, 23_170)).is_none()); assert!(decode_failures() > failed); // Within the allowance: decoded, and its missing rows can't be read. let within = paint::decode_within_allowance(&png_header(23_170, 23_170)); - assert!(matches!(within, Err(image::ImageError::Decoding(_))), "{within:?}"); + assert!(matches!(within, Err(paint::DecodeError::Unreadable(_))), "{within:?}"); + assert!(within.unwrap_err().to_string().starts_with("the image's pixels can't be read: ")); // A header whose size as RGBA does not fit in 64 bits gets the same limit error. let huge = paint::decode_within_allowance(&tiff_header(1 << 31, 1 << 31)); - assert!(matches!(huge, Err(image::ImageError::Limits(_))), "{huge:?}"); + assert!(matches!(huge, Err(paint::DecodeError::Limit(_))), "{huge:?}"); } /// A PNG of `w` × `h` noisy pixels cut off halfway through its bytes: its header reads, and its