Print: classify a freeform gradient's plate cells after their opacity

A translucent cell whose samples round to ink before its opacity is
applied, such as 0.4% cyan at 25% opacity, is written with no ink once
the opacity is applied, yet it was kept and painted paper over the ink
below. The cells are now classified from the samples the image writes
after opacity, and RGB cells in CMYK output from their converted inks.
This commit is contained in:
dotCooCoo
2026-11-20 23:55:44 -08:00
parent 5ea4753750
commit 6fb2028149
2 changed files with 54 additions and 3 deletions
+30 -3
View File
@@ -1597,9 +1597,36 @@ impl<'a> Scene<'a> {
(field.display_grid(area, cols, rows).flat_map(|(c, a)| [q(c[0]), q(c[1]), q(c[2]), q(a)]).collect(), 255)
};
let n = if in_inks { 5 } else { 4 };
// Overprinting on a plate: the cells whose samples put none of the ink down are left out,
// as clear pixels at Level 3 and outside a clip of the other cells at Level 2.
let blank: Option<Vec<bool>> = self.keep_below.then(|| samples.chunks(n).map(|s| s.iter().take(n - 1).all(|&v| v == paper)).collect());
// Overprinting on a plate: the cells whose samples put none of the ink down once their
// opacity is applied, as `encode_inks` and `encode_image` write them (RGB converted to
// CMYK in CMYK output), are left out, as clear pixels at Level 3 and outside a clip of the
// other cells at Level 2.
let written = |v: u8, a: u8| {
let (v, a) = (u32::from(v), u32::from(a));
((v * a + u32::from(paper) * (255 - a) + 127) / 255) as u8
};
let (cms, to_cmyk) = (self.cms.clone(), !in_inks && self.cmyk);
let intent = cms.settings().intent;
let mut converted: HashMap<[u8; 3], bool> = HashMap::new();
let blank: Option<Vec<bool>> = self.keep_below.then(|| {
samples
.chunks(n)
.map(|s| {
let a = s.last().copied().unwrap_or(0);
if a == 0 || in_inks {
return s.iter().take(n - 1).all(|&v| written(v, a) == paper);
}
let rgb = [0, 1, 2].map(|i| s.get(i).map_or(paper, |&v| written(v, a)));
if !to_cmyk {
return rgb == [paper; 3];
}
*converted.entry(rgb).or_insert_with(|| {
let inks = cms.srgb_to_cmyk(rgb.map(|x| f32::from(x) / 255.0), intent);
inks.iter().all(|&x| (unit(x) * 255.0).round() as u8 == 0)
})
})
.collect()
});
let blank = blank.filter(|b| b.contains(&true));
if blank.as_ref().is_some_and(|b| !b.contains(&false)) {
return;
+24
View File
@@ -1637,6 +1637,30 @@ fn a_repeated_field_clipped_on_a_plate_is_charged_its_reference_at_the_page_limi
assert_eq!((&page.setup, &page.body), (&full.setup, &full.body));
}
/// On a plate, an overprinting freeform gradient whose cells are translucent enough that their
/// opacity takes every sample to no ink (or to paper white in RGB) draws no image at either level.
#[test]
fn a_translucent_field_whose_samples_write_no_ink_on_a_plate_draws_no_image() {
let faint = [Color::cmyk(0.004, 0.0, 0.0, 0.0), Color::rgb(254.0 / 255.0, 254.0 / 255.0, 254.0 / 255.0)];
for color in faint {
let d = doc_with(|d| {
let mut g = GradientPaint::new(Gradient { kind: GradientKind::Freeform, ..Default::default() });
let point = |x: f64| FreeformPoint { opacity: 0.25, ..FreeformPoint::new(Point::new(x, 32.0), color) };
g.freeform = Some(Freeform { points: vec![point(12.0), point(52.0)], ..Default::default() });
let mut n = path(d, shapes::rectangle(Rect::new(0.0, 0.0, 64.0, 64.0)), Paint::Gradient(Box::new(g)), Paint::None, 0.0);
for it in &mut n.appearance.items {
*it.overprint_mut() = true;
}
vec![n]
});
for level in [Level::Three, Level::Two] {
let o = EpsOptions { level, ..opts(&d) };
let out = crate::scene::Scene::new(&d, &o).on_plate(true).run();
assert!(!out.body.contains("image"), "{color:?} at {level:?}: {}", out.body);
}
}
}
/// A 200 × 200 pt document holding 8 × 8 pixels of noise as image `pic` and a three-pixel gray ramp
/// as image `gray`, its first layer holding what `nodes` makes.
fn image_doc_with(nodes: impl FnOnce(&mut Document) -> Vec<Node>) -> Document {