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

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

functions_part4.rs73 lines · 3.3 KiBRaw
1#[cfg(test)]
2mod tests_cont {
3 use super::*;
4
5 // A non-monotonic /TR must be carried faithfully; only the FIRST output component is
6 // used, per 11.6.5.2's one-in/one-out requirement.
7 #[test]
8 fn transfer_lut_uses_only_the_first_output() {
9 let f = PdfFunction::Exponential {
10 domain: [0.0, 1.0],
11 range: Vec::new(),
12 c0: vec![1.0, 0.0, 0.0],
13 c1: vec![0.0, 1.0, 1.0],
14 n: 1.0,
15 };
16 let lut = f.to_lut256();
17 assert_eq!(lut[0], 255, "first component starts at 1.0");
18 assert_eq!(lut[255], 0, "and ends at 0.0");
19 }
20
21 // SEAM TEST: dictionary -> `PdfFunction::parse` -> output arity -> `eval_cs_to_rgb`
22 // for a Separation. Neither side of this join was witnessed: my own arity tests stop
23 // at `out.len()`, and color.rs's Separation tests hand-BUILD a `PdfFunction` rather
24 // than parsing one, so nothing exercised parse feeding a real colour conversion.
25 //
26 // It matters because the failure is silent and total. §8.6.6.4's tint transform must
27 // yield `cs_kind_ncomp(alt)` components; when it yields fewer, `eval_cs_to_rgb` falls
28 // back to a subtractive grey ramp. So an arity regression in Type 2 parsing does not
29 // produce a slightly wrong colour — every spot colour on the page turns grey, which
30 // is 7.10's "function bugs show up as wrong Separation colours" in its worst form.
31 #[test]
32 fn a_parsed_tint_transform_drives_a_separation_to_a_real_colour() {
33 let mut doc = Document::with_version("1.7");
34 // Type 2 over DeviceCMYK: t=1 -> (0, 1, 1, 0), i.e. red.
35 let good = doc.add_object(dictionary! {
36 "FunctionType" => 2,
37 "Domain" => vec![0.into(), 1.into()],
38 "C0" => vec![0.into(), 0.into(), 0.into(), 0.into()],
39 "C1" => vec![0.into(), 1.into(), 1.into(), 0.into()],
40 "N" => 1,
41 });
42 let cs = CsKind::Separation {
43 name: b"Spot".to_vec(),
44 alt: Box::new(CsKind::DeviceCMYK),
45 tint_fn: PdfFunction::parse(&doc, &Object::Reference(good)),
46 };
47 let res = HashMap::new();
48 let argb = eval_cs_to_rgb(&doc, &cs, &[1.0], &res).expect("separation resolves");
49 let (r, g, b) = ((argb >> 16) & 0xFF, (argb >> 8) & 0xFF, argb & 0xFF);
50 assert!(r > 200 && g < 60 && b < 60, "full tint is red, got #{r:02x}{g:02x}{b:02x}");
51
52 // The same Separation whose tint transform parses to the WRONG arity takes the
53 // grey-ramp fallback. Asserting the two differ pins the seam without this test
54 // needing to know the fallback's formula.
55 let short = doc.add_object(dictionary! {
56 "FunctionType" => 2,
57 "Domain" => vec![0.into(), 1.into()],
58 "C0" => vec![0.into()],
59 "C1" => vec![1.into()],
60 "N" => 1,
61 });
62 let cs_short = CsKind::Separation {
63 name: b"Spot".to_vec(),
64 alt: Box::new(CsKind::DeviceCMYK),
65 tint_fn: PdfFunction::parse(&doc, &Object::Reference(short)),
66 };
67 let fallback = eval_cs_to_rgb(&doc, &cs_short, &[1.0], &res).expect("fallback resolves");
68 assert_ne!(
69 argb, fallback,
70 "a correctly-parsed 4-component tint transform must not land on the grey ramp"
71 );
72 }
73}