diff --git a/crates/image-codecs/src/codecs/jpeg.rs b/crates/image-codecs/src/codecs/jpeg.rs index 390166cc3..415a5d717 100644 --- a/crates/image-codecs/src/codecs/jpeg.rs +++ b/crates/image-codecs/src/codecs/jpeg.rs @@ -198,6 +198,15 @@ fn data_end(b: &[u8]) -> (DataEnd, u64) { data_end_mode(b, false) } +/// Whether JPEG `b` holds too little scan data for a `w` × `h` frame to be worth decoding: no scan +/// data at all, or under one byte for every [`MAX_PIXELS_PER_BYTE`] pixels (counted on the scan +/// data the marker walk crosses, so APP padding doesn't count). `zune-jpeg` allocates the whole +/// output before it reads a scan, so a few bytes could otherwise ask for a frame of any size. +pub(crate) fn scan_too_short(b: &[u8], w: u32, h: u32) -> bool { + let (end, scan_bytes) = data_end(b); + end == DataEnd::Empty || u64::from(w) * u64::from(h) / MAX_PIXELS_PER_BYTE > scan_bytes +} + /// Whether the marker walk actually reaches the first EOI, with malformed segment lengths refused. pub(crate) fn reaches_eoi(b: &[u8]) -> bool { data_end_mode(b, true).0 == DataEnd::Complete diff --git a/crates/image-codecs/src/jpeg_inks.rs b/crates/image-codecs/src/jpeg_inks.rs new file mode 100644 index 000000000..226703643 --- /dev/null +++ b/crates/image-codecs/src/jpeg_inks.rs @@ -0,0 +1,98 @@ +//! A JPEG's headers and four-component samples read the way `zune-jpeg` and libjpeg-style readers +//! give them, for apps that keep a CMYK JPEG's inks: the component count and ICC profile from the +//! headers alone, and the ink amounts of a CMYK or YCCK JPEG. +//! +//! [`crate::decode`] reads the same files differently, on purpose: it keeps the samples of a +//! four-component JPEG without Adobe's marker as they are, rounds its YCCK conversion, and hands +//! arithmetic-coded, lossless and 12-bit files to its whole-coefficient decoder. The readers here +//! follow the older convention instead, so a document that kept a CMYK JPEG's inks that way reads +//! the same: every four-component file is taken to hold Adobe's inverted samples, YCCK is turned +//! into CMYK as libjpeg does (truncating), and only the 8-bit Huffman-coded frames `zune-jpeg` +//! decodes are read. + +use zune_core::bytestream::ZCursor; +use zune_core::colorspace::ColorSpace; +use zune_core::options::DecoderOptions; +use zune_jpeg::JpegDecoder; + +use crate::Format; +use crate::error::CodecError; +use crate::image::{ChannelLayout, Image, SampleType}; +use crate::options::Limits; + +/// A decoder of `bytes` whose headers have been read, with the default options (16384 pixels a +/// side at most); `None` when they aren't a JPEG or its headers can't be read. +fn headers(bytes: &[u8]) -> Option>> { + if !bytes.starts_with(&[0xFF, 0xD8]) { + return None; + } + let mut d = JpegDecoder::new_with_options(ZCursor::new(bytes), DecoderOptions::default()); + d.decode_headers().ok()?; + Some(d) +} + +/// The number of colour components the frame header of JPEG `bytes` declares, whatever it is; +/// `None` when they aren't a JPEG whose headers can be read. +pub fn jpeg_components(bytes: &[u8]) -> Option { + Some(usize::from(headers(bytes)?.info()?.components)) +} + +/// The width and height the frame header of JPEG `bytes` declares, for any colour type; `None` +/// when they aren't a JPEG whose headers can be read (which includes sides past 16384 pixels). +pub fn jpeg_dimensions(bytes: &[u8]) -> Option<(u32, u32)> { + let (w, h) = headers(bytes)?.dimensions()?; + Some((u32::try_from(w).ok()?, u32::try_from(h).ok()?)) +} + +/// The ICC profile of JPEG `bytes`, put together from all its numbered `ICC_PROFILE` APP2 +/// segments; `None` without one, when it is empty, and when the headers can't be read. +pub fn jpeg_icc_profile(bytes: &[u8]) -> Option> { + headers(bytes)?.icc_profile().filter(|icc| !icc.is_empty()) +} + +/// The ink amounts of a four-component (CMYK or YCCK) JPEG: C, M, Y and K bytes a pixel, 0 for +/// no ink, as a [`ChannelLayout::Cmyk`] image. The samples are taken to be Adobe's inverted ones +/// whether or not the file has Adobe's marker; YCCK is first turned into CMYK as libjpeg does +/// (the complement of the YCbCr colour's RGB, each value truncated). +/// +/// Errors: bytes that aren't a JPEG with readable headers ([`CodecError::InvalidImage`]); one of +/// other than four components, or a frame `zune-jpeg` doesn't decode, such as an arithmetic-coded +/// or 12-bit one ([`CodecError::Unsupported`]); a size past `limits`; scan data too short for the +/// frame (under a byte for every 2048 pixels, refused before anything is allocated); and data that +/// doesn't decode, +/// including frames `zune-jpeg` panics on, which are caught. +pub fn decode_jpeg_inks(bytes: &[u8], limits: &Limits) -> Result { + let bad = |m: &str| CodecError::InvalidImage(format!("JPEG inks: {m}")); + let mut d = headers(bytes).ok_or_else(|| bad("not a JPEG with readable headers"))?; + let space = d.input_colorspace().ok_or_else(|| bad("no colour space"))?; + let components = d.info().map_or(0, |i| i.components); + if components != 4 || !matches!(space, ColorSpace::CMYK | ColorSpace::YCCK) { + return Err(CodecError::unsupported(Format::Jpeg, format!("inks of a {components}-component {space:?} image"))); + } + let (w, h) = d.dimensions().ok_or_else(|| bad("no size"))?; + let (w, h) = (u32::try_from(w).map_err(|_| bad("width"))?, u32::try_from(h).map_err(|_| bad("height"))?); + limits.check(w, h, ChannelLayout::Cmyk, SampleType::U8)?; + if crate::codecs::jpeg::scan_too_short(bytes, w, h) { + return Err(bad("the scan data is too short for the frame it declares")); + } + d.set_options(DecoderOptions::default().jpeg_set_out_colorspace(space)); + // zune-jpeg 0.5 asserts on some subsampled four-component frames whose first component isn't + // the largest (a scratch row too short for its upsampler): that is an error here, not a panic. + let decoded = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| d.decode())); + let mut data = decoded + .map_err(|_| CodecError::unsupported(Format::Jpeg, "inks: zune-jpeg failed on this file"))? + .map_err(|e| CodecError::unsupported(Format::Jpeg, format!("inks: {e}")))?; + for p in data.as_chunks_mut::<4>().0 { + if space == ColorSpace::YCCK { + let [y, cb, cr] = [p[0], p[1], p[2]].map(f32::from); + // libjpeg's YCCK → CMYK: the complement of the YCbCr colour's RGB. + p[0] = (434.456 - y - 1.402 * cr).clamp(0.0, 255.0) as u8; + p[1] = (119.541 - y + 0.344 * cb + 0.714 * cr).clamp(0.0, 255.0) as u8; + p[2] = (481.816 - y - 1.772 * cb).clamp(0.0, 255.0) as u8; + } + for v in p.iter_mut() { + *v = 255 - *v; + } + } + Image::from_u8(w, h, ChannelLayout::Cmyk, data) +} diff --git a/crates/image-codecs/src/lib.rs b/crates/image-codecs/src/lib.rs index 826b7e24c..7e2d29362 100644 --- a/crates/image-codecs/src/lib.rs +++ b/crates/image-codecs/src/lib.rs @@ -26,6 +26,7 @@ mod fidelity; mod format; mod image; mod inspection; +pub mod jpeg_inks; pub mod jpeg_sef; mod lab; mod options; diff --git a/crates/image-codecs/tests/it/jpeg_inks.rs b/crates/image-codecs/tests/it/jpeg_inks.rs new file mode 100644 index 000000000..b7cdfe699 --- /dev/null +++ b/crates/image-codecs/tests/it/jpeg_inks.rs @@ -0,0 +1,215 @@ +//! `jpeg_inks`: four-component JPEGs read as Adobe-inverted inks, as `zune-jpeg`-based readers +//! kept them, beside `decode`'s own reading. + +use craft_image_codecs::jpeg_inks::{decode_jpeg_inks, jpeg_components, jpeg_dimensions, jpeg_icc_profile}; +use craft_image_codecs::*; + +/// A `w` × `h` four-component JPEG of `samples` at quality 100, written by `jpeg-encoder` as +/// `ty`, with ICC profile `icc` when given. +fn four(w: u16, h: u16, samples: &[u8], ty: jpeg_encoder::ColorType, icc: Option<&[u8]>) -> Vec { + let mut out = Vec::new(); + let mut enc = jpeg_encoder::Encoder::new(&mut out, 100); + enc.set_sampling_factor(jpeg_encoder::SamplingFactor::R_4_4_4); + if let Some(icc) = icc { + enc.add_icc_profile(icc).unwrap(); + } + enc.encode(samples, w, h, ty).unwrap(); + out +} + +/// Flat 8×8 blocks of ink, so the coding keeps each block's value. +fn blocks(w: usize, h: usize) -> Vec { + (0..w * h) + .flat_map(|i| { + let b = ((i % w) / 8 + (i / w) / 8 * 7) as u8; + [b.wrapping_mul(37), b.wrapping_mul(91), 255 - b.wrapping_mul(13), b.wrapping_mul(53)] + }) + .collect() +} + +/// The segments of `bytes` up to the scan without Adobe's APP14 marker. +fn without_adobe(bytes: &[u8]) -> Vec { + let mut out = bytes[..2].to_vec(); + let mut at = 2; + loop { + let marker = bytes[at + 1]; + if marker == 0xDA { + out.extend_from_slice(&bytes[at..]); + return out; + } + let len = usize::from(u16::from_be_bytes([bytes[at + 2], bytes[at + 3]])); + if !(marker == 0xEE && &bytes[at + 4..at + 9] == b"Adobe") { + out.extend_from_slice(&bytes[at..at + 2 + len]); + } + at += 2 + len; + } +} + +/// Adobe CMYK reads to the inks it was written with, as `decode` reads it; without the marker +/// the samples are still taken as inverted (where `decode` keeps them as they are), and YCCK +/// turns into the same inks closely, truncating where `decode` rounds. +#[test] +fn four_component_jpegs_read_as_inverted_inks() { + let (w, h) = (32u16, 16u16); + let inks = blocks(usize::from(w), usize::from(h)); + let cmyk = four(w, h, &inks, jpeg_encoder::ColorType::Cmyk, None); + let read = decode_jpeg_inks(&cmyk, &Limits::default()).unwrap(); + assert_eq!((read.dimensions(), read.layout()), ((32, 16), ChannelLayout::Cmyk)); + assert!(read.data().iter().zip(&inks).all(|(a, b)| a.abs_diff(*b) <= 2), "Adobe CMYK keeps its inks"); + assert_eq!(read.data(), decode(&cmyk).unwrap().data(), "as decode reads Adobe CMYK"); + // No marker: inverted all the same, where decode keeps the stored samples. + let bare = without_adobe(&cmyk); + assert_ne!(bare, cmyk); + let unmarked = decode_jpeg_inks(&bare, &Limits::default()).unwrap(); + assert_eq!(unmarked.data(), read.data()); + let kept = decode(&bare).unwrap(); + assert!(kept.data().iter().zip(read.data()).all(|(k, r)| *k == 255 - *r), "decode keeps them as stored"); + // YCCK: libjpeg's conversion, truncated. + let ycck = four(w, h, &inks, jpeg_encoder::ColorType::CmykAsYcck, None); + let read = decode_jpeg_inks(&ycck, &Limits::default()).unwrap(); + assert!(read.data().iter().zip(&inks).all(|(a, b)| a.abs_diff(*b) <= 3), "YCCK keeps its inks closely"); + let rounded = decode(&ycck).unwrap(); + let differ = read.data().iter().zip(rounded.data()).filter(|(a, b)| a != b).count(); + assert!(read.data().iter().zip(rounded.data()).all(|(a, b)| a.abs_diff(*b) <= 1), "within a step of decode's rounding"); + assert!(differ > 0, "truncation differs from rounding somewhere"); +} + +/// The headers alone give the component count of any JPEG and its ICC profile, put together from +/// its numbered segments. +#[test] +fn headers_give_components_and_icc() { + let icc: Vec = (0..200_000u32).map(|i| (i * 7 % 251) as u8).collect(); + let cmyk = four(8, 8, &[9; 256], jpeg_encoder::ColorType::Cmyk, Some(&icc)); + assert_eq!((jpeg_components(&cmyk), jpeg_dimensions(&cmyk)), (Some(4), Some((8, 8)))); + assert_eq!(jpeg_icc_profile(&cmyk).as_deref(), Some(icc.as_slice()), "three APP2 segments"); + let gray = encode(&Image::from_u8(8, 8, ChannelLayout::Gray, vec![50; 64]).unwrap(), Format::Jpeg, &EncodeOptions::default()).unwrap(); + assert_eq!((jpeg_components(&gray), jpeg_icc_profile(&gray)), (Some(1), None)); + for not in [&b""[..], b"\xFF\xD8", b"\x89PNG\r\n\x1a\n"] { + assert_eq!((jpeg_components(not), jpeg_icc_profile(not), jpeg_dimensions(not)), (None, None, None)); + } +} + +/// A frame header past 16384 pixels a side, `zune-jpeg`'s default, isn't read. +#[test] +fn sides_past_zunes_default_are_not_read() { + let mut gray = encode(&Image::from_u8(8, 8, ChannelLayout::Gray, vec![50; 64]).unwrap(), Format::Jpeg, &EncodeOptions::default()).unwrap(); + let sof = gray.windows(2).position(|w| w == [0xFF, 0xC0]).unwrap(); + gray[sof + 5..sof + 9].copy_from_slice(&[0x40, 0x00, 0x40, 0x00]); + assert_eq!(jpeg_dimensions(&gray), Some((16384, 16384))); + gray[sof + 7..sof + 9].copy_from_slice(&[0x40, 0x01]); + assert_eq!((jpeg_dimensions(&gray), jpeg_components(&gray)), (None, None)); +} + +/// What isn't a four-component image `zune-jpeg` decodes is refused: other component counts, an +/// arithmetic-coded or 12-bit frame (`decode` hands those to craft-jpeg), a size past the limits, +/// and data that isn't a JPEG. +#[test] +fn other_jpegs_are_refused() { + let gray = encode(&Image::from_u8(8, 8, ChannelLayout::Gray, vec![50; 64]).unwrap(), Format::Jpeg, &EncodeOptions::default()).unwrap(); + assert!(matches!(decode_jpeg_inks(&gray, &Limits::default()), Err(CodecError::Unsupported { .. }))); + let cmyk = four(16, 8, &[40; 16 * 8 * 4], jpeg_encoder::ColorType::Cmyk, None); + let sof = cmyk.windows(2).position(|w| w == [0xFF, 0xC0]).unwrap(); + for (marker, precision) in [(0xC9u8, 8u8), (0xC1, 12)] { + let mut other = cmyk.clone(); + other[sof + 1] = marker; + other[sof + 4] = precision; + assert!(decode_jpeg_inks(&other, &Limits::default()).is_err(), "SOF {marker:#x}, {precision} bits"); + } + // A tiny file declaring a large frame is refused before zune-jpeg allocates its output. + let mut huge = cmyk.clone(); + huge[sof + 5..sof + 9].copy_from_slice(&[0x20, 0x00, 0x20, 0x00]); + assert!(matches!(decode_jpeg_inks(&huge, &Limits::default()), Err(CodecError::InvalidImage(m)) if m.contains("too short"))); + let tight = Limits { max_pixels: 100, ..Limits::default() }; + assert!(matches!(decode_jpeg_inks(&cmyk, &tight), Err(CodecError::LimitExceeded(_)))); + assert!(matches!(decode_jpeg_inks(b"not a jpeg", &Limits::default()), Err(CodecError::InvalidImage(_)))); + assert!(decode_jpeg_inks(&cmyk[..12], &Limits::default()).is_err(), "headers cut short"); +} + +/// The ink reader four-component JPEGs were read with before (vectorcraft's `jpeg_inks`, on +/// `zune-jpeg` as this crate uses it), kept as the reference the readers here match. +fn reference_inks(bytes: &[u8]) -> Option<(usize, usize, Vec)> { + use zune_core::{bytestream::ZCursor, colorspace::ColorSpace, options::DecoderOptions}; + let mut d = zune_jpeg::JpegDecoder::new_with_options(ZCursor::new(bytes), DecoderOptions::default()); + d.decode_headers().ok()?; + let space = d.input_colorspace()?; + if d.info()?.components != 4 || !matches!(space, ColorSpace::CMYK | ColorSpace::YCCK) { + return None; + } + let (w, h) = d.dimensions()?; + d.set_options(DecoderOptions::default().jpeg_set_out_colorspace(space)); + let mut data = d.decode().ok()?; + for p in data.as_chunks_mut::<4>().0 { + if space == ColorSpace::YCCK { + let [y, cb, cr] = [p[0], p[1], p[2]].map(f32::from); + p[0] = (434.456 - y - 1.402 * cr).clamp(0.0, 255.0) as u8; + p[1] = (119.541 - y + 0.344 * cb + 0.714 * cr).clamp(0.0, 255.0) as u8; + p[2] = (481.816 - y - 1.772 * cb).clamp(0.0, 255.0) as u8; + } + for v in p.iter_mut() { + *v = 255 - *v; + } + } + Some((w, h, data)) +} + +/// Over CMYK and YCCK JPEGs of many sizes, samplings, qualities and pictures, with and without +/// Adobe's marker, baseline and progressive, the inks and component counts read the same as the +/// reference reader. +#[test] +fn matches_the_reference_reader() { + use jpeg_encoder::{ColorType, Encoder, SamplingFactor}; + let (mut seen, mut refused) = (0, 0); + for (i, (w, h)) in [(1u16, 1u16), (7, 3), (8, 8), (17, 9), (33, 31), (64, 5), (100, 77)].into_iter().enumerate() { + let (wu, n) = (usize::from(w), usize::from(w) * usize::from(h)); + let noise: Vec = (0..n * 4).map(|k| ((k as u64).wrapping_mul(2_654_435_761).wrapping_add(i as u64 * 97) >> 7) as u8).collect(); + let ramp: Vec = (0..n * 4).map(|k| (k / 4 % wu * 255 / wu.max(2)) as u8).collect(); + for samples in [&noise, &ramp] { + for ty in [ColorType::Cmyk, ColorType::CmykAsYcck] { + for (q, sampling, progressive) in + [(100, SamplingFactor::R_4_4_4, false), (75, SamplingFactor::R_4_2_0, false), (40, SamplingFactor::R_4_2_2, true)] + { + let mut out = Vec::new(); + let mut enc = Encoder::new(&mut out, q); + enc.set_sampling_factor(sampling); + enc.set_progressive(progressive); + enc.encode(samples, w, h, ty).unwrap(); + for file in [without_adobe(&out), out] { + let read = decode_jpeg_inks(&file, &Limits::default()); + assert_eq!(jpeg_components(&file), Some(4)); + let (dw, dh) = jpeg_dimensions(&file).unwrap(); + assert_eq!((usize::from(w), usize::from(h)), (dw as usize, dh as usize)); + let Some((rw, rh, inks)) = reference_inks(&file) else { + eprintln!("refused: {w}x{h} {ty:?} q{q} progressive {progressive}"); + assert!(read.is_err(), "refused by both: {w}x{h} {ty:?} q{q}"); + refused += 1; + continue; + }; + let read = read.unwrap(); + assert_eq!(read.dimensions(), (rw as u32, rh as u32)); + assert_eq!(read.data(), inks.as_slice(), "{w}x{h} {ty:?} q{q}"); + seen += 1; + } + } + } + } + } + eprintln!("read {seen}, refused {refused}"); + assert_eq!(seen + refused, 7 * 2 * 2 * 3 * 2); + assert!(seen > refused); +} + +/// zune-jpeg 0.5 panics on some subsampled CMYK frames whose first component isn't the largest +/// (here `jpeg-encoder`'s 2×2 K with C, M and Y at 1×1, progressive): an error, not a panic. +#[test] +fn frames_zune_panics_on_are_errors() { + for (w, h) in [(1u16, 1u16), (3, 3)] { + let mut out = Vec::new(); + let mut enc = jpeg_encoder::Encoder::new(&mut out, 70); + enc.set_sampling_factor(jpeg_encoder::SamplingFactor::F_2_2); + enc.set_progressive(true); + enc.encode(&vec![9; usize::from(w * h) * 4], w, h, jpeg_encoder::ColorType::Cmyk).unwrap(); + assert!(std::panic::catch_unwind(|| reference_inks(&out)).is_err(), "the reference reader panics on {w}x{h}"); + assert!(matches!(decode_jpeg_inks(&out, &Limits::default()), Err(CodecError::Unsupported { .. })), "{w}x{h}"); + assert_eq!((jpeg_components(&out), jpeg_dimensions(&out)), (Some(4), Some((u32::from(w), u32::from(h))))); + } +} diff --git a/crates/image-codecs/tests/it/main.rs b/crates/image-codecs/tests/it/main.rs index 4eda74a26..380e37565 100644 --- a/crates/image-codecs/tests/it/main.rs +++ b/crates/image-codecs/tests/it/main.rs @@ -14,6 +14,7 @@ mod exr_stream; mod fidelity; mod gif_frames; mod heif; +mod jpeg_inks; mod jpegls_oracle; mod limits; mod malformed;