1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//! Where a one-input function stops being affine.
use super::{Exponential, Kind, PdfFunction, Sampled, Stitching, clip};
impl PdfFunction {
/// The inputs between which every output is affine in the one input `t`,
/// sorted, with the `/Domain` ends first and last; at most `cap` of them.
///
/// A type 0 breaks at each sample position (its `/Encode` mapped back
/// onto `/Domain`), a type 2 with `/N` 0 or 1 only at the domain ends, a
/// type 3 at its `/Bounds` and at each sub-function's breaks mapped
/// through that sub-function's `/Encode`. Wherever a `/Range` clips an
/// output, the point it starts or stops clipping is a break too. A
/// function can jump at a break (a type 3 bound); outside the domain it
/// is constant.
///
/// `None` when no such list exists or it is longer than `cap`: more than
/// one input, a type 2 with any other `/N`, a type 4, or a type 3 with
/// such a sub-function. A caller that needs an exact answer takes `None`
/// as "sample it some other way".
#[must_use]
pub fn affine_breaks(&self, cap: usize) -> Option<Vec<f64>> {
let &[[d0, d1]] = self.domain.as_slice() else {
return None;
};
let mut breaks = match &self.kind {
Kind::Sampled(s) => s.breaks(d0, d1, cap)?,
Kind::Exponential(e) => e.breaks()?,
Kind::Stitching(s) => s.breaks(d0, d1, cap)?,
Kind::PostScript(_) => return None,
};
breaks.extend([d0, d1]);
breaks.retain(|t| t.is_finite() && (d0..=d1).contains(t));
sort_dedup(&mut breaks);
if let Some(range) = self.range.as_deref() {
let crossings = self.crossings(&breaks, range)?;
breaks.extend(crossings);
sort_dedup(&mut breaks);
}
(breaks.len() <= cap).then_some(breaks)
}
/// [`affine_breaks`](Self::affine_breaks) for a consumer that also
/// clamps output `i` to `bounds[i]` (a colour space's component range):
/// adds each input where an output crosses one of those bounds, so the
/// clamped outputs are affine between neighbouring breaks too. Outputs
/// past `bounds.len()` are not clamped.
///
/// `None` as for `affine_breaks`, or when the list grows past `cap`.
#[must_use]
pub fn affine_breaks_clamped(&self, cap: usize, bounds: &[[f64; 2]]) -> Option<Vec<f64>> {
let mut breaks = self.affine_breaks(cap)?;
let crossings = self.crossings(&breaks, bounds)?;
breaks.extend(crossings);
sort_dedup(&mut breaks);
(breaks.len() <= cap).then_some(breaks)
}
/// Where an output, affine between neighbouring `breaks` before the
/// `/Range` clip, crosses one of `bounds`. A bound the clip keeps the
/// output from reaching adds a harmless extra break.
fn crossings(&self, breaks: &[f64], bounds: &[[f64; 2]]) -> Option<Vec<f64>> {
let mut out = Vec::new();
for (&a, &b) in breaks.iter().zip(breaks.iter().skip(1)) {
let (fa, fb) = (self.unclipped(a, b)?, self.unclipped(b, a)?);
for ((&ya, &yb), &[lo, hi]) in fa.iter().zip(&fb).zip(bounds) {
for edge in [lo, hi] {
if (ya - edge) * (yb - edge) < 0.0 {
out.push(a + (b - a) * (edge - ya) / (yb - ya));
}
}
}
}
Some(out)
}
/// The outputs at `t` before the `/Range` clip, taken from the side of
/// `t` facing `toward` (a type 3 can jump at `t`).
fn unclipped(&self, t: f64, toward: f64) -> Option<Vec<f64>> {
let [d0, d1] = *self.domain.first()?;
let x = clip(
if toward < t {
t.next_down().max(toward)
} else {
t
},
d0,
d1,
);
let mut out = Vec::new();
match &self.kind {
Kind::Sampled(s) => s.eval(&self.domain, &[x], &mut out).ok()?,
Kind::Exponential(e) => e.eval(&[x], &mut out),
Kind::Stitching(s) => s.eval(&self.domain, &[x], &mut out).ok()?,
Kind::PostScript(_) => return None,
}
Some(out)
}
}
impl Sampled {
/// Each sample position mapped back from `/Encode` onto `[d0, d1]`.
fn breaks(&self, d0: f64, d1: f64, cap: usize) -> Option<Vec<f64>> {
let (&[size], &[[e0, e1]]) = (self.size.as_slice(), self.encode.as_slice()) else {
return None;
};
if size > cap {
return None;
}
if e1 == e0 || d1 <= d0 {
return Some(Vec::new());
}
#[allow(clippy::cast_precision_loss)] // size <= cap, a table length
let at = |i: usize| d0 + (i as f64 - e0) * (d1 - d0) / (e1 - e0);
Some((0..size).map(at).collect())
}
}
impl Exponential {
/// Affine only for `/N` 0 or 1.
fn breaks(&self) -> Option<Vec<f64>> {
(self.n == 0.0 || self.n == 1.0).then(Vec::new)
}
}
impl Stitching {
/// `/Bounds`, plus each sub-function's breaks mapped onto its
/// sub-domain.
fn breaks(&self, d0: f64, d1: f64, cap: usize) -> Option<Vec<f64>> {
let starts = std::iter::once(d0).chain(self.bounds.iter().copied());
let ends = self.bounds.iter().copied().chain(std::iter::once(d1));
let mut out: Vec<f64> = self.bounds.clone();
for (((lo, hi), &[a, b]), f) in starts.zip(ends).zip(&self.encode).zip(&self.functions) {
out.extend([lo, hi]);
if b == a || hi <= lo {
continue;
}
for c in f.affine_breaks(cap)? {
out.push(lo + (c - a) * (hi - lo) / (b - a));
}
if out.len() > cap.saturating_mul(4) {
return None;
}
}
Some(out)
}
}
fn sort_dedup(v: &mut Vec<f64>) {
v.sort_by(f64::total_cmp);
v.dedup();
}