diff --git a/crates/image-codecs/src/jpeg_inks.rs b/crates/image-codecs/src/jpeg_inks.rs index e17066294..6cf6e453d 100644 --- a/crates/image-codecs/src/jpeg_inks.rs +++ b/crates/image-codecs/src/jpeg_inks.rs @@ -57,7 +57,8 @@ pub fn jpeg_icc_profile(bytes: &[u8]) -> Option> { /// /// 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`; and data that doesn't decode. +/// or 12-bit one ([`CodecError::Unsupported`]); a size past `limits`; 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"))?; @@ -70,7 +71,12 @@ pub fn decode_jpeg_inks(bytes: &[u8], limits: &Limits) -> Result().0 { if space == ColorSpace::YCCK { let [y, cb, cr] = [p[0], p[1], p[2]].map(f32::from); diff --git a/crates/image-codecs/tests/it/jpeg_inks.rs b/crates/image-codecs/tests/it/jpeg_inks.rs index 3729127f3..605d8186e 100644 --- a/crates/image-codecs/tests/it/jpeg_inks.rs +++ b/crates/image-codecs/tests/it/jpeg_inks.rs @@ -193,3 +193,19 @@ fn matches_the_reference_reader() { 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))))); + } +}