fix(image-codecs): JPEG inks of frames zune-jpeg panics on are an error

This commit is contained in:
FredApps committed 2026-11-16 01:05:44 +01:00
1 parent 927eabc6e6
commit a61b1ad53c
2 files changed
+24 -2

No files matched your search

+8 -2
View File
@@ -57,7 +57,8 @@ pub fn jpeg_icc_profile(bytes: &[u8]) -> Option<Vec<u8>> {
///
/// 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<Image, CodecError> {
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<Image, CodecErr
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)?;
d.set_options(DecoderOptions::default().jpeg_set_out_colorspace(space));
let mut data = d.decode().map_err(|e| CodecError::unsupported(Format::Jpeg, format!("inks: {e}")))?;
// 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);
+16
View File
@@ -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)))));
}
}