From ddbe78231b5b23273b3d191f04b1a38f4c0445b3 Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Mon, 16 Nov 2026 13:10:48 +0100 Subject: [PATCH] craft-video: pass the packet's Adobe APP14 transform to the MJPEG decoder (#1847) mjpeg::Decoder called craft_jpeg::decode with no Adobe transform, so a packet with numeric component ids and APP14 transform 0 (RGB as stored) was converted as YCbCr and came out in the wrong colours. The decoder now reads the transform with craft_jpeg::adobe_transform, as the JPEG image readers do. A regression test injects APP14 transforms 0 and 1; it fails without the fix. --- crates/video/src/mjpeg/mod.rs | 5 ++++- crates/video/tests/mjpeg.rs | 38 +++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/crates/video/src/mjpeg/mod.rs b/crates/video/src/mjpeg/mod.rs index 2a3b1e4b5..acd073f6c 100644 --- a/crates/video/src/mjpeg/mod.rs +++ b/crates/video/src/mjpeg/mod.rs @@ -102,9 +102,12 @@ impl Decoder { if !is_packet(packet) { return Err(Error::Invalid("the packet does not start with a JPEG SOI marker".to_string())); } + // An Adobe APP14 segment decides whether 3 and 4 components are YCbCr/YCCK or RGB/CMYK: + // RGB packets with numeric component ids are only told apart from YCbCr by it. + let adobe = craft_jpeg::adobe_transform(packet); // AVI `MJPG` packets may leave out the Huffman tables; every other packet keeps its own let packet = craft_jpeg::with_avi1_default_huffman(packet); - let d = craft_jpeg::decode(&packet, &self.limits, None)?; + let d = craft_jpeg::decode(&packet, &self.limits, adobe)?; Ok(Frame { width: d.width, height: d.height, channels: d.components, samples: d.samples, damaged: d.damaged }) } } diff --git a/crates/video/tests/mjpeg.rs b/crates/video/tests/mjpeg.rs index d83b61438..740ac8ea5 100644 --- a/crates/video/tests/mjpeg.rs +++ b/crates/video/tests/mjpeg.rs @@ -224,3 +224,41 @@ fn ffmpeg_decodes_what_we_encode() { assert!(q >= 28.0, "{name}: ffmpeg's decode is far from the input: {q:.1} dB"); } } + +/// `packet` with an Adobe APP14 segment carrying `transform`, right after SOI. +fn with_app14(packet: &[u8], transform: u8) -> Vec { + let mut out = packet[..2].to_vec(); + out.extend_from_slice(&[0xFF, 0xEE, 0, 14]); + out.extend_from_slice(b"Adobe"); + out.extend_from_slice(&[0, 100, 0, 0, 0, 0, transform]); + out.extend_from_slice(&packet[2..]); + out +} + +#[test] +fn adobe_transform_zero_keeps_numeric_id_components_as_rgb() { + // The encoder stores YCbCr under component ids 1, 2, 3. An Adobe APP14 transform of 0 says the + // three components are R, G and B as stored, so the decoder must not convert them again. + let (r, g, b) = (200.0f32, 60.0f32, 30.0f32); + let pixels: Vec = [r as u8, g as u8, b as u8].repeat(16 * 16); + let enc = Encoder::new(EncoderConfig { quality: 100, subsampling: ChromaSubsampling::S444 }).unwrap(); + let packet = enc.encode(16, 16, 3, &pixels).unwrap(); + let dec = Decoder::new(); + let ycc = [0.299 * r + 0.587 * g + 0.114 * b, 128.0 - 0.168_736 * r - 0.331_264 * g + 0.5 * b, 128.0 + 0.5 * r - 0.418_688 * g - 0.081_312 * b]; + let as_rgb = dec.decode(&with_app14(&packet, 0)).unwrap(); + assert_eq!(as_rgb.channels, 3); + for px in bytes(&as_rgb).as_chunks::<3>().0 { + for (got, want) in px.iter().zip(ycc) { + assert!((f32::from(*got) - want).abs() <= 3.0, "{px:?} against stored {ycc:?}"); + } + } + // Transform 1 (YCbCr), and no APP14 at all, convert back to the picture's colour. + for packet in [with_app14(&packet, 1), packet] { + let f = dec.decode(&packet).unwrap(); + for px in bytes(&f).as_chunks::<3>().0 { + for (got, want) in px.iter().zip([r, g, b]) { + assert!((f32::from(*got) - want).abs() <= 3.0, "{px:?}"); + } + } + } +}