Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
cov/c3 ▾

Modern-Apps.git / pdf / src / main / rust / src / reference_diff_tests_part3.rs

reference_diff_tests_part3.rs426 lines · 17.5 KiBRaw
1/// FINDING (recorded, not a pass/fail gate): the two renderers disagree
2/// systematically on `DeviceCMYK`, by up to ~40 levels per channel.
3///
4/// We convert arithmetically in `color.rs:12` `cmyk_to_argb`:
5/// `r = (1-c)(1-k)`, `g = (1-m)(1-k)`, `b = (1-y)(1-k)`. `hayro` instead runs
6/// every DeviceCMYK sample through an embedded CGATS TR 001 (SWOP) ICC profile
7/// (`hayro-interpret-0.7.0/src/color.rs:1066`, `CMYK_TRANSFORM`), which is what
8/// Acrobat and pdf.js effectively do.
9///
10/// NEITHER IS WRONG. ISO 32000-1 §8.6.4 makes the device colour spaces
11/// explicitly device-dependent, and §8.6.5.6 provides `/DefaultCMYK` precisely
12/// so that a document that needs a defined colorimetry can ask for one; absent
13/// that entry the mapping is the consumer's choice. This test therefore only
14/// pins the divergence so that it is a known, measured quantity rather than a
15/// surprise the next time someone diffs against a reference — and so that a
16/// future change to `cmyk_to_argb` shows up here as a change in the printed
17/// deltas.
18#[test]
19#[ignore]
20fn refdiff_devicecmyk_diverges_because_the_reference_is_colour_managed() {
21 let patches: [[f64; 4]; 6] = [
22 [0.0, 1.0, 1.0, 0.0],
23 [1.0, 0.0, 1.0, 0.0],
24 [1.0, 1.0, 0.0, 0.0],
25 [0.0, 0.0, 0.0, 0.5],
26 [0.2, 0.4, 0.6, 0.1],
27 [0.0, 0.0, 0.0, 1.0],
28 ];
29 let mut ops = Vec::new();
30 for (i, p) in patches.iter().enumerate() {
31 let (col, row) = (i % 3, i / 3);
32 ops.push(op("k", vec![n(p[0]), n(p[1]), n(p[2]), n(p[3])]));
33 ops.push(op(
34 "re",
35 vec![n(10.0 + col as f64 * 64.0), n(10.0 + row as f64 * 96.0), n(56.0), n(88.0)],
36 ));
37 ops.push(op("f", vec![]));
38 }
39 let r = render_both("devicecmyk", &pdf_bytes(ops, dictionary! {}));
40 let mut worst = 0f64;
41 for (i, p) in patches.iter().enumerate() {
42 let (col, row) = (i % 3, i / 3);
43 let x = 15.0 + col as f64 * 64.0;
44 let y = 15.0 + row as f64 * 96.0;
45 let a = interior_mean(&r.ours, r.w, r.h, r.page_h, [x, y, x + 46.0, y + 78.0]);
46 let b = interior_mean(&r.theirs, r.w, r.h, r.page_h, [x, y, x + 46.0, y + 78.0]);
47 // The PostScript `setcmykcolor` additive formula, for reference: it is
48 // the OTHER common reading, and is not what either renderer does.
49 let ps = [
50 (1.0 - (p[0] + p[3]).min(1.0)) * 255.0,
51 (1.0 - (p[1] + p[3]).min(1.0)) * 255.0,
52 (1.0 - (p[2] + p[3]).min(1.0)) * 255.0,
53 ];
54 let d = (0..3).map(|c| (a[c] - b[c]).abs()).fold(0.0, f64::max);
55 worst = worst.max(d);
56 println!(
57 " cmyk{p:?}: ours=[{:.0},{:.0},{:.0}] hayro=[{:.0},{:.0},{:.0}] \
58 ps_additive=[{:.0},{:.0},{:.0}] max_delta={d:.0}",
59 a[0], a[1], a[2], b[0], b[1], b[2], ps[0], ps[1], ps[2]
60 );
61 }
62 println!(" worst DeviceCMYK channel delta = {worst:.0}/255");
63 // Deliberately loose: this asserts the divergence stays a colour-management
64 // difference (measured worst = 109/255, on saturated cyan+magenta) and has
65 // not become a gross error such as a swapped channel.
66 assert!(worst <= 120.0, "DeviceCMYK divergence grew past colour management: {worst}");
67}
68
69/// Nonzero vs even-odd on a self-intersecting star: the classic place to get
70/// §8.5.3.3.2 backwards and never notice, because both rules look plausible.
71#[test]
72#[ignore]
73fn refdiff_winding_rules_on_a_self_intersecting_star() {
74 let star = |close: &str| {
75 let pts = [(100.0, 190.0), (145.0, 55.0), (30.0, 140.0), (170.0, 140.0), (55.0, 55.0)];
76 let mut v = vec![
77 op("rg", vec![n(0.1), n(0.2), n(0.8)]),
78 op("m", vec![n(pts[0].0), n(pts[0].1)]),
79 ];
80 for p in &pts[1..] {
81 v.push(op("l", vec![n(p.0), n(p.1)]));
82 }
83 v.push(op("h", vec![]));
84 v.push(op(close, vec![]));
85 v
86 };
87 let _ = compare_page("star_nonzero", &pdf_bytes(star("f"), dictionary! {}));
88 let _ = compare_page("star_evenodd", &pdf_bytes(star("f*"), dictionary! {}));
89}
90
91/// Indexed and Separation, plus a Type 2 tint transform. Getting the Separation
92/// tint→alternate direction backwards, or the Indexed lookup stride wrong, is a
93/// misreading no self-referential test can catch.
94#[test]
95#[ignore]
96fn refdiff_indexed_and_separation_colour() {
97 let mut doc = Document::with_version("1.7");
98 // Indexed palette: 3 entries of DeviceRGB.
99 let palette: Vec<u8> = vec![255, 0, 0, 0, 255, 0, 0, 0, 255];
100 let indexed = Object::Array(vec![
101 Object::Name(b"Indexed".to_vec()),
102 Object::Name(b"DeviceRGB".to_vec()),
103 Object::Integer(2),
104 Object::String(palette, lopdf::StringFormat::Hexadecimal),
105 ]);
106 // Separation with a Type 2 exponential tint transform. The alternate is
107 // DeviceRGB, not DeviceCMYK, so this measures the TINT TRANSFORM and not
108 // the colour-management policy divergence recorded in
109 // `refdiff_devicecmyk_diverges_because_the_reference_is_colour_managed`.
110 let tint = doc.add_object(Stream::new(
111 dictionary! {
112 "FunctionType" => 2,
113 "Domain" => vec![0.into(), 1.into()],
114 "C0" => vec![Object::Real(1.0), Object::Real(1.0), Object::Real(1.0)],
115 "C1" => vec![Object::Real(0.1), Object::Real(0.35), Object::Real(0.6)],
116 "N" => 1,
117 },
118 Vec::new(),
119 ));
120 let sep = Object::Array(vec![
121 Object::Name(b"Separation".to_vec()),
122 Object::Name(b"Spot".to_vec()),
123 Object::Name(b"DeviceRGB".to_vec()),
124 Object::Reference(tint),
125 ]);
126 let cs = dictionary! { "IX" => indexed, "SP" => sep };
127 let resources = dictionary! { "ColorSpace" => cs };
128 let ops = vec![
129 op("cs", vec![Object::Name(b"IX".to_vec())]),
130 op("sc", vec![Object::Integer(2)]),
131 op("re", vec![n(20.0), n(110.0), n(70.0), n(70.0)]),
132 op("f", vec![]),
133 op("cs", vec![Object::Name(b"IX".to_vec())]),
134 op("sc", vec![Object::Integer(1)]),
135 op("re", vec![n(110.0), n(110.0), n(70.0), n(70.0)]),
136 op("f", vec![]),
137 op("cs", vec![Object::Name(b"SP".to_vec())]),
138 op("sc", vec![n(1.0)]),
139 op("re", vec![n(20.0), n(20.0), n(70.0), n(70.0)]),
140 op("f", vec![]),
141 op("cs", vec![Object::Name(b"SP".to_vec())]),
142 op("sc", vec![n(0.5)]),
143 op("re", vec![n(110.0), n(20.0), n(70.0), n(70.0)]),
144 op("f", vec![]),
145 ];
146 let content = Content { operations: ops }.encode().expect("encode");
147 let _ = assemble(&mut doc, content, resources, dictionary! {});
148 let mut bytes = Vec::new();
149 doc.save_to(&mut bytes).expect("save");
150
151 let r = compare_page("indexed_separation", &bytes);
152 for (label, rect) in [
153 ("indexed_2_blue", [30.0, 120.0, 80.0, 170.0]),
154 ("indexed_1_green", [120.0, 120.0, 170.0, 170.0]),
155 ("separation_1.0", [30.0, 30.0, 80.0, 80.0]),
156 ("separation_0.5", [120.0, 30.0, 170.0, 80.0]),
157 ] {
158 let a = interior_mean(&r.ours, r.w, r.h, r.page_h, rect);
159 let b = interior_mean(&r.theirs, r.w, r.h, r.page_h, rect);
160 println!(" {label}: ours={a:?} theirs={b:?}");
161 for c in 0..3 {
162 assert!(
163 (a[c] - b[c]).abs() <= 4.0,
164 "{label} channel {c}: ours {} vs hayro {}",
165 a[c],
166 b[c]
167 );
168 }
169 }
170}
171
172/// All four function types, driven through Separation tint transforms so the
173/// answer lands in a flat, antialiasing-free patch of colour. This is the single
174/// highest-value entry in the corpus: `functions.rs` is 58 kB of formula, every
175/// line of it verified only against our own reading of §7.10.
176#[test]
177#[ignore]
178fn refdiff_all_four_function_types_via_separation() {
179 let mut doc = Document::with_version("1.7");
180
181 // Type 2: exponential, N = 2 so the exponent is actually exercised.
182 let f2 = doc.add_object(Stream::new(
183 dictionary! {
184 "FunctionType" => 2,
185 "Domain" => vec![0.into(), 1.into()],
186 "C0" => vec![Object::Real(0.0)],
187 "C1" => vec![Object::Real(1.0)],
188 "N" => 2,
189 },
190 Vec::new(),
191 ));
192 // Type 3: stitching two Type 2s, with a non-trivial /Encode.
193 let f2a = doc.add_object(Stream::new(
194 dictionary! {
195 "FunctionType" => 2, "Domain" => vec![0.into(), 1.into()],
196 "C0" => vec![Object::Real(0.0)], "C1" => vec![Object::Real(0.3)], "N" => 1,
197 },
198 Vec::new(),
199 ));
200 let f2b = doc.add_object(Stream::new(
201 dictionary! {
202 "FunctionType" => 2, "Domain" => vec![0.into(), 1.into()],
203 "C0" => vec![Object::Real(0.3)], "C1" => vec![Object::Real(1.0)], "N" => 1,
204 },
205 Vec::new(),
206 ));
207 let f3 = doc.add_object(Stream::new(
208 dictionary! {
209 "FunctionType" => 3,
210 "Domain" => vec![0.into(), 1.into()],
211 "Functions" => vec![Object::Reference(f2a), Object::Reference(f2b)],
212 "Bounds" => vec![Object::Real(0.4)],
213 "Encode" => vec![Object::Real(0.0), Object::Real(1.0), Object::Real(0.0), Object::Real(1.0)],
214 },
215 Vec::new(),
216 ));
217 // Type 0: sampled, 5 samples, 8 bpc, linear interpolation between them.
218 let f0 = doc.add_object(Stream::new(
219 dictionary! {
220 "FunctionType" => 0,
221 "Domain" => vec![0.into(), 1.into()],
222 "Range" => vec![0.into(), 1.into()],
223 "Size" => vec![5.into()],
224 "BitsPerSample" => 8,
225 },
226 vec![0u8, 32, 200, 220, 255],
227 ));
228 // Type 4: PostScript calculator.
229 let f4 = doc.add_object(Stream::new(
230 dictionary! {
231 "FunctionType" => 4,
232 "Domain" => vec![0.into(), 1.into()],
233 "Range" => vec![0.into(), 1.into()],
234 },
235 b"{ dup mul 0.5 mul 0.25 add }".to_vec(),
236 ));
237
238 let mut cs = Dictionary::new();
239 for (name, f) in [(&b"F2"[..], f2), (&b"F3"[..], f3), (&b"F0"[..], f0), (&b"F4"[..], f4)] {
240 cs.set(
241 String::from_utf8_lossy(name).to_string(),
242 Object::Array(vec![
243 Object::Name(b"Separation".to_vec()),
244 Object::Name(b"Spot".to_vec()),
245 Object::Name(b"DeviceGray".to_vec()),
246 Object::Reference(f),
247 ]),
248 );
249 }
250 // Four columns x two tint values, so eight independent evaluations.
251 let mut ops = Vec::new();
252 let tints = [0.35f64, 0.8];
253 for (col, name) in [&b"F2"[..], &b"F3"[..], &b"F0"[..], &b"F4"[..]].iter().enumerate() {
254 for (row, t) in tints.iter().enumerate() {
255 ops.push(op("cs", vec![Object::Name(name.to_vec())]));
256 ops.push(op("sc", vec![n(*t)]));
257 ops.push(op(
258 "re",
259 vec![n(10.0 + col as f64 * 48.0), n(10.0 + row as f64 * 96.0), n(40.0), n(88.0)],
260 ));
261 ops.push(op("f", vec![]));
262 }
263 }
264 let content = Content { operations: ops }.encode().expect("encode");
265 let _ = assemble(&mut doc, content, dictionary! { "ColorSpace" => cs }, dictionary! {});
266 let mut bytes = Vec::new();
267 doc.save_to(&mut bytes).expect("save");
268
269 let r = compare_page("functions", &bytes);
270 let mut worst = 0f64;
271 for (col, name) in ["Type2", "Type3", "Type0", "Type4"].iter().enumerate() {
272 for (row, t) in tints.iter().enumerate() {
273 let x = 15.0 + col as f64 * 48.0;
274 let y = 15.0 + row as f64 * 96.0;
275 let rect = [x, y, x + 30.0, y + 78.0];
276 let a = interior_mean(&r.ours, r.w, r.h, r.page_h, rect);
277 let b = interior_mean(&r.theirs, r.w, r.h, r.page_h, rect);
278 let d = (a[0] - b[0]).abs().max((a[1] - b[1]).abs()).max((a[2] - b[2]).abs());
279 println!(" {name} tint={t}: ours={:.1} theirs={:.1} delta={:.1}", a[0], b[0], d);
280 worst = worst.max(d);
281 assert!(d <= 4.0, "{name} at tint {t}: ours {:.1} vs hayro {:.1}", a[0], b[0]);
282 }
283 }
284 println!(" worst function delta = {worst:.2}");
285}
286
287/// Axial and radial shadings with `/Extend`. §8.7.4.5.3 and §8.7.4.5.4 are dense
288/// and the extend/`t` mapping is easy to read two ways.
289#[test]
290#[ignore]
291fn refdiff_axial_and_radial_shadings() {
292 let mut doc = Document::with_version("1.7");
293 let f = doc.add_object(Stream::new(
294 dictionary! {
295 "FunctionType" => 2, "Domain" => vec![0.into(), 1.into()],
296 "C0" => vec![Object::Real(1.0), Object::Real(0.0), Object::Real(0.0)],
297 "C1" => vec![Object::Real(0.0), Object::Real(0.0), Object::Real(1.0)],
298 "N" => 1,
299 },
300 Vec::new(),
301 ));
302 let axial = doc.add_object(dictionary! {
303 "ShadingType" => 2,
304 "ColorSpace" => Object::Name(b"DeviceRGB".to_vec()),
305 "Coords" => vec![Object::Real(50.0), Object::Real(0.0), Object::Real(150.0), Object::Real(0.0)],
306 "Function" => Object::Reference(f),
307 "Extend" => vec![Object::Boolean(true), Object::Boolean(true)],
308 });
309 let radial = doc.add_object(dictionary! {
310 "ShadingType" => 3,
311 "ColorSpace" => Object::Name(b"DeviceRGB".to_vec()),
312 "Coords" => vec![
313 Object::Real(100.0), Object::Real(100.0), Object::Real(10.0),
314 Object::Real(100.0), Object::Real(100.0), Object::Real(80.0),
315 ],
316 "Function" => Object::Reference(f),
317 "Extend" => vec![Object::Boolean(true), Object::Boolean(true)],
318 });
319
320 let ops_ax = vec![
321 op("q", vec![]),
322 op("re", vec![n(0.0), n(100.0), n(200.0), n(100.0)]),
323 op("W", vec![]),
324 op("n", vec![]),
325 op("sh", vec![Object::Name(b"AX".to_vec())]),
326 op("Q", vec![]),
327 op("q", vec![]),
328 op("re", vec![n(0.0), n(0.0), n(200.0), n(100.0)]),
329 op("W", vec![]),
330 op("n", vec![]),
331 op("sh", vec![Object::Name(b"RA".to_vec())]),
332 op("Q", vec![]),
333 ];
334 let res = dictionary! {
335 "Shading" => dictionary! { "AX" => Object::Reference(axial), "RA" => Object::Reference(radial) }
336 };
337 let content = Content { operations: ops_ax }.encode().expect("encode");
338 let _ = assemble(&mut doc, content, res, dictionary! {});
339 let mut bytes = Vec::new();
340 doc.save_to(&mut bytes).expect("save");
341
342 let r = compare_page("shadings", &bytes);
343 // Extend regions: left of x=50 must be pure C0, right of x=150 pure C1.
344 for (label, rect, want) in [
345 ("axial_extend_before", [5.0, 120.0, 40.0, 180.0], [255.0, 0.0, 0.0]),
346 ("axial_extend_after", [160.0, 120.0, 195.0, 180.0], [0.0, 0.0, 255.0]),
347 ] {
348 let a = interior_mean(&r.ours, r.w, r.h, r.page_h, rect);
349 let b = interior_mean(&r.theirs, r.w, r.h, r.page_h, rect);
350 println!(" {label}: ours={a:?} theirs={b:?} spec={want:?}");
351 for c in 0..3 {
352 assert!((a[c] - b[c]).abs() <= 4.0, "{label} channel {c}: {} vs {}", a[c], b[c]);
353 }
354 }
355}
356
357/// Image XObject placement, `/Decode` inversion, and an `/ImageMask` stencil.
358/// §8.9.5.2 (unit-square mapping, row 0 = v=1) and §8.9.6.2 (stencil polarity)
359/// are both easy to invert silently — a flipped image still "looks like an
360/// image".
361#[test]
362#[ignore]
363fn refdiff_image_placement_decode_and_stencil() {
364 let mut doc = Document::with_version("1.7");
365 // 2x2 RGB, deliberately asymmetric so a flip in either axis is visible.
366 let rgb = vec![255u8, 0, 0, 0, 255, 0, 0, 0, 255, 255, 255, 0];
367 let img = doc.add_object(Stream::new(
368 dictionary! {
369 "Type" => "XObject", "Subtype" => "Image",
370 "Width" => 2, "Height" => 2,
371 "ColorSpace" => Object::Name(b"DeviceRGB".to_vec()),
372 "BitsPerComponent" => 8,
373 },
374 rgb,
375 ));
376 // 8x8 stencil: left half set. 1 bit per row-padded row.
377 let stencil: Vec<u8> = (0..8).map(|_| 0xF0u8).collect();
378 let mask = doc.add_object(Stream::new(
379 dictionary! {
380 "Type" => "XObject", "Subtype" => "Image",
381 "Width" => 8, "Height" => 8,
382 "ImageMask" => Object::Boolean(true),
383 "BitsPerComponent" => 1,
384 },
385 stencil,
386 ));
387 let ops = vec![
388 op("q", vec![]),
389 op("cm", vec![n(80.0), n(0.0), n(0.0), n(80.0), n(20.0), n(100.0)]),
390 op("Do", vec![Object::Name(b"Im".to_vec())]),
391 op("Q", vec![]),
392 op("q", vec![]),
393 op("rg", vec![n(0.0), n(0.6), n(0.2)]),
394 op("cm", vec![n(80.0), n(0.0), n(0.0), n(80.0), n(20.0), n(10.0)]),
395 op("Do", vec![Object::Name(b"Ms".to_vec())]),
396 op("Q", vec![]),
397 ];
398 let res = dictionary! {
399 "XObject" => dictionary! {
400 "Im" => Object::Reference(img),
401 "Ms" => Object::Reference(mask),
402 }
403 };
404 let content = Content { operations: ops }.encode().expect("encode");
405 let _ = assemble(&mut doc, content, res, dictionary! {});
406 let mut bytes = Vec::new();
407 doc.save_to(&mut bytes).expect("save");
408
409 let r = compare_page("image_placement", &bytes);
410 // Quadrant colours pin the orientation: sample well inside each.
411 for (label, rect) in [
412 ("img_top_left", [30.0, 145.0, 55.0, 170.0]),
413 ("img_top_right", [105.0, 145.0, 130.0, 170.0]),
414 ("img_bottom_left", [30.0, 110.0, 55.0, 135.0]),
415 ("img_bottom_right", [105.0, 110.0, 130.0, 135.0]),
416 ("stencil_left_painted", [30.0, 20.0, 55.0, 80.0]),
417 ("stencil_right_clear", [65.0, 20.0, 90.0, 80.0]),
418 ] {
419 let a = interior_mean(&r.ours, r.w, r.h, r.page_h, rect);
420 let b = interior_mean(&r.theirs, r.w, r.h, r.page_h, rect);
421 println!(" {label}: ours={a:?} theirs={b:?}");
422 for c in 0..3 {
423 assert!((a[c] - b[c]).abs() <= 4.0, "{label} channel {c}: {} vs {}", a[c], b[c]);
424 }
425 }
426}