pdf: fix inverted K channel in Adobe YCCK JPEGs (#924)
- Commit
3dcd5278442ac5771de70c95184bfc412ae2faa3- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-28 07:11 UTC (8 days ago)
- Parents
3a9ec97
The reporter's document (fetched and verified) embeds DeviceCMYK/DCTDecode images with an Adobe APP14 transform 2 (YCCK) segment: jpeg-decoder returns all four YCCK channels in inverted-ink polarity (verified byte-for-byte against libjpeg), but decode_jpeg_rgba_decoded inverted only CMY, leaving K inverted — every channel computed A*(255-A_K)/255 instead of A*A_K/255, solarising shadows toward magenta exactly as in the report screenshot. Transform-0 CMYK was already correct. Both the RGBA and gray/soft-mask CMYK arms now invert all four components for transform 2. Regression: hand-built YCCK JPEG test (correct output black vs old (178,0,225)) end-to-end through extract_image. Verified in the cargo pdf_render suite (660 passed).
Diff
pdf/src/main/rust/src/images_part15.rs
diff --git a/pdf/src/main/rust/src/images_part15.rs b/pdf/src/main/rust/src/images_part15.rsindex 962e0dbfd9..15350435c4 100644--- a/pdf/src/main/rust/src/images_part15.rs+++ b/pdf/src/main/rust/src/images_part15.rs@@ -155,6 +155,50 @@ mod dct_decode_tests { assert_eq!(inverted[3], 255, "alpha is untouched by /Decode"); } + /// Issue #924: an Adobe YCCK (APP14 transform 2) CMYK JPEG reaches the CMYK arm+ /// with ALL FOUR samples in inverted-ink polarity (verified against the real+ /// DeviceCMYK/DCTDecode images in the reported PETA certificate, whose+ /// jpeg-decoder samples match libjpeg's inverted Adobe output byte for byte).+ /// The old code inverted CMY but not K, so each channel came out as+ /// A*(255-A_K)/255 instead of A*A_K/255 and dark colours solarised.+ ///+ /// Reuses the cyan fixture's entropy (raw planes 0,255,255,255) with the+ /// transform byte patched to 2: jpeg-decoder's YCCK conversion yields+ /// (178, 0, 225, 0) — inverted ink for a pixel whose true ink is+ /// (77, 255, 30, 255), i.e. full-strength K, i.e. black. Without the K+ /// inversion the same pixel renders (178, 0, 225), a bright magenta.+ #[test]+ fn ycck_k_is_inverted_with_the_other_components() {+ let mut jpeg = cyan_cmyk_jpeg();+ let adobe = jpeg.windows(5).position(|w| w == b"Adobe").expect("fixture has an APP14 segment");+ jpeg[adobe + 11] = 2;+ assert_eq!(jpeg_adobe_transform(&jpeg), Some(2), "fixture must be Adobe transform 2");++ let (w, h, rgba) = decode_jpeg_rgba_decoded(&jpeg, false).expect("fixture must decode");+ assert_eq!((w, h), (1, 1));+ assert_eq!(+ &rgba[0..3], &[0, 0, 0],+ "full K in a YCCK JPEG is black; (178, 0, 225) would mean K kept the wrong polarity"+ );++ // The gray (soft-mask) path shares the polarity rule.+ let (_, _, gray) = decode_jpeg_gray(&jpeg).expect("fixture must decode");+ assert_eq!(gray, vec![0], "a black YCCK pixel has zero luminance in the mask path too");++ // End to end through the image XObject path used for the reported file.+ let doc = Document::with_version("1.7");+ let dict = dictionary! {+ "Type" => "XObject", "Subtype" => "Image",+ "Width" => 1, "Height" => 1, "BitsPerComponent" => 8,+ "ColorSpace" => "DeviceCMYK",+ "Filter" => "DCTDecode",+ };+ let out = extract_image(&doc, &Stream::new(dict, jpeg), 0xFF00_0000, &HashMap::new())+ .expect("a YCCK image must not be dropped");+ assert_eq!(out.format, 0, "a CMYK JPEG belongs on the Rust decode path, not the passthrough");+ assert_eq!(&out.data[0..3], &[0, 0, 0]);+ }+ /// End to end: the same JPEG in an image XObject with `/Decode [1 0 1 0 1 0 1 0]` /// must come back as decoded RGBA, NOT as `format: 1` passthrough bytes — Android's /// decoder has no way to apply /Decode, so the passthrough was the negative.
pdf/src/main/rust/src/images_part8.rs
diff --git a/pdf/src/main/rust/src/images_part8.rs b/pdf/src/main/rust/src/images_part8.rsindex c22ec2c4f1..a468b649f1 100644--- a/pdf/src/main/rust/src/images_part8.rs+++ b/pdf/src/main/rust/src/images_part8.rs@@ -65,21 +65,30 @@ pub(crate) fn decode_jpeg_rgba_decoded(data: &[u8], invert_components: bool) -> out } jpeg_decoder::PixelFormat::CMYK32 => {- // jpeg-decoder has ALREADY un-inverted the Adobe convention before we see- // these bytes: `color_convert_line_cmyk` emits 255-c/255-m/255-y/255-k, and- // `color_convert_line_ycck` emits correct CMY but 255-k. So inverting all- // four again produced a lurid negative for transform 0, and blew out blacks- // for transform 2. Correct residual correction: invert CMY only for YCCK- // (transform 2), and never invert K.+ // The two Adobe transforms reach us in DIFFERENT polarities, verified+ // against real Photoshop/YCCK files (issue #924, the PETA certificate:+ // DeviceCMYK DCTDecode, APP14 transform 2):+ // - transform 0 (`color_convert_line_cmyk`) emits 255 minus each stored+ // sample, and Adobe stores CMYK inverted, so those bytes are already+ // true ink amounts.+ // - transform 2 (`color_convert_line_ycck`) converts the YCC triple —+ // which Adobe computed FROM the inverted CMY — back to (255-C, 255-M,+ // 255-Y), and emits 255 minus the stored K, which is likewise the+ // inverted K. All four bytes are therefore inverted ink amounts.+ // Inverting only CMY for YCCK (the previous code) left K in the wrong+ // polarity: RGB came out as A*(255-A_K)/255 instead of A*A_K/255 per+ // channel (A = inverted ink), which brightens every shadow toward a+ // solarised magenta — exactly the reported colour glitch. Invert all+ // four for YCCK so the CMYK->RGB conversion below sees true ink. if pixels.len() < (w as usize * h as usize * 4) { return None; }- let invert_cmy = jpeg_adobe_transform(data) == Some(2);+ let invert_all = jpeg_adobe_transform(data) == Some(2); let mut out = vec![0u8; dw * dh * 4]; for y in 0..dh { for x in 0..dw { let s = src(x, y) * 4; let (mut c, mut m, mut yy) = (pixels[s], pixels[s+1], pixels[s+2]); let mut k = pixels[s+3];- if invert_cmy { c = 255 - c; m = 255 - m; yy = 255 - yy; }+ if invert_all { c = 255 - c; m = 255 - m; yy = 255 - yy; k = 255 - k; } // /Decode applies to the finished samples, so it comes after the // Adobe/YCCK correction above and covers K as well as CMY. if invert_components { c = 255 - c; m = 255 - m; yy = 255 - yy; k = 255 - k; }@@ -232,14 +241,17 @@ pub(crate) fn decode_jpeg_gray(data: &[u8]) -> Option<(u32,u32,Vec<u8>)> { // A CMYK JPEG used as an /SMask previously fell into `_ =>`, and the caller's // RGBA fallback then took the R channel as luminance. Convert properly here. if pixels.len() < wh * 4 { return None; }- let invert_cmy = jpeg_adobe_transform(data) == Some(2);+ // Same YCCK polarity as `decode_jpeg_rgba_decoded`: all four samples+ // are inverted ink for transform 2, so K is inverted too (issue #924).+ let invert_all = jpeg_adobe_transform(data) == Some(2); let mut out = vec![0u8; dw * dh]; for y in 0..dh { for x in 0..dw { let s = src(x, y) * 4; let (mut c, mut m, mut yy) = (pixels[s], pixels[s+1], pixels[s+2]);- let k = pixels[s+3] as u16;- if invert_cmy { c = 255 - c; m = 255 - m; yy = 255 - yy; }+ let mut k = pixels[s+3];+ if invert_all { c = 255 - c; m = 255 - m; yy = 255 - yy; k = 255 - k; }+ let k = k as u16; let kf = 255u16.saturating_sub(k); let r = (255u16 - c as u16) * kf / 255; let g = (255u16 - m as u16) * kf / 255;