pdf: fix transposed CalRGB matrix rendering white as cyan (issue #321)
- Commit
fcd90138e5c70af045f49f68be5abbc7d5fd8b01- Author
- Vayun Mathur <[email protected]>
- When
- 2026-08-08 00:31 UTC (4 months ago)
- Parents
fdc5c53
Diff
pdf/src/main/rust/src/color.rs
diff --git a/pdf/src/main/rust/src/color.rs b/pdf/src/main/rust/src/color.rsindex f07ceef6eb..6192d44444 100644--- a/pdf/src/main/rust/src/color.rs+++ b/pdf/src/main/rust/src/color.rs@@ -296,10 +296,17 @@ pub(crate) fn read_gamma_rgb(dict: &lopdf::Dictionary) -> Option<[f64;3]> { pub(crate) fn read_matrix_cal(dict: &lopdf::Dictionary) -> Option<[[f64;3];3]> { let arr = dict.get(b"Matrix").ok().and_then(|o| o.as_array().ok())?; if arr.len()>=9 {+ // PDF stores the CalRGB Matrix column-major as+ // [XA YA ZA XB YB ZB XC YC ZC], defining+ // X = XA·A + XB·B + XC·C, Y = YA·A + …, Z = ZA·A + …+ // eval_cs_to_rgb multiplies rows against (A,B,C), so store it as rows+ // [[XA XB XC],[YA YB YC],[ZA ZB ZC]] (i.e. transposed from the array+ // order). Storing it in raw array order transposes the transform and+ // turns e.g. CalRGB white into cyan (issue #321 colorrenderexample). Some([- [num(&arr[0])?, num(&arr[1])?, num(&arr[2])?],- [num(&arr[3])?, num(&arr[4])?, num(&arr[5])?],- [num(&arr[6])?, num(&arr[7])?, num(&arr[8])?],+ [num(&arr[0])?, num(&arr[3])?, num(&arr[6])?],+ [num(&arr[1])?, num(&arr[4])?, num(&arr[7])?],+ [num(&arr[2])?, num(&arr[5])?, num(&arr[8])?], ]) } else { None } }
pdf/src/main/rust/src/issue321_tests.rs
diff --git a/pdf/src/main/rust/src/issue321_tests.rs b/pdf/src/main/rust/src/issue321_tests.rsindex 1dd40743e0..6638b22413 100644--- a/pdf/src/main/rust/src/issue321_tests.rs+++ b/pdf/src/main/rust/src/issue321_tests.rs@@ -225,9 +225,79 @@ mod issue321 { report } + /// Debug helper: print the most frequent fill colors (ARGB) a fixture page+ /// emits, to compare against a reference render when chasing color bugs.+ /// Ignored by default; run with:+ /// cargo test -p pdf_render issue321_fill_colors -- --ignored --nocapture+ #[test]+ #[ignore]+ fn issue321_fill_colors() {+ let dir = match fixtures_dir() {+ Some(d) => d,+ None => return,+ };+ let name = std::env::var("FILL_PDF").unwrap_or_else(|_| "colorrenderexample".to_string());+ let page_idx: usize = std::env::var("FILL_PAGE").ok().and_then(|s| s.parse().ok()).unwrap_or(0);+ let path = dir.join(format!("{}.pdf", name));+ let bytes = std::fs::read(&path).expect("read fixture");+ let doc = load_document_lenient(&bytes).expect("load");+ let pages = doc.get_pages();+ let (_, page_id) = pages.iter().nth(page_idx).expect("page");+ let pd = interpret_page(&doc, *page_id).expect("interpret");+ let mut counts: std::collections::HashMap<u32, usize> = std::collections::HashMap::new();+ for p in &pd.prims {+ match p {+ Prim::Fill { argb, .. } => *counts.entry(*argb).or_default() += 1,+ Prim::Text { argb, outline: false, .. } => *counts.entry(*argb).or_default() += 1,+ _ => {}+ }+ }+ let mut v: Vec<(u32, usize)> = counts.into_iter().collect();+ v.sort_by(|a, b| b.1.cmp(&a.1));+ println!("=== {} p{} top fill/text colors (ARGB) ===", name, page_idx);+ for (argb, n) in v.iter().take(25) {+ println!(+ " #{:08X} a={} r={} g={} b={} ×{}",+ argb, (argb >> 24) & 0xFF, (argb >> 16) & 0xFF, (argb >> 8) & 0xFF, argb & 0xFF, n+ );+ }+ // Sample decoded image pixels (RGBA format 0) at a 3×3 grid so colors can+ // be diffed against a reference renderer.+ let mut img_no = 0;+ for p in &pd.prims {+ if let Prim::Image { w, h, format, data, .. } = p {+ if *format != 0 || data.is_empty() {+ continue;+ }+ println!(" image#{} {}x{}:", img_no, w, h);+ for fy in [0.25, 0.5, 0.75] {+ let mut row = String::new();+ for fx in [0.25, 0.5, 0.75] {+ let px = ((*w as f64 * fx) as u32).min(w - 1);+ let py = ((*h as f64 * fy) as u32).min(h - 1);+ let i = ((py * w + px) * 4) as usize;+ if i + 3 < data.len() {+ row += &format!(+ " ({:3},{:3},{:3},{:3})",+ data[i], data[i + 1], data[i + 2], data[i + 3]+ );+ }+ }+ println!(" {}", row);+ }+ img_no += 1;+ if img_no >= 6 {+ break;+ }+ }+ }+ }+ /// Drive every fixture and print a per-PDF / per-page report. This test never /// fails on rendering defects itself (it is a diagnostic harness); it only /// fails if the harness cannot run. Skips cleanly when fixtures are absent.+ /// fails on rendering defects itself (it is a diagnostic harness); it only+ /// fails if the harness cannot run. Skips cleanly when fixtures are absent. #[test] fn issue321_report() { let dir = match fixtures_dir() {