Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

perf(compose): prepare gradient stops outside pixel loops (#103)

MikeeId3a6855367d1
diff --git a/crates/compose/src/effects.rs b/crates/compose/src/effects.rs
index 44866490f..e483d53b1 100644
--- a/crates/compose/src/effects.rs
+++ b/crates/compose/src/effects.rs
@@ -446,9 +446,25 @@ pub fn gradient_t(style: GradientStyle, angle: f32, scale: f32, reverse: bool, o
/// Samples colour and opacity stops at `t`.
pub fn sample_gradient(g: &Gradient, t: f32) -> [f32; 4] {
- let color = sample_stops(&g.stops.iter().map(|(p, c)| (*p, rgb(c))).collect::<Vec<_>>(), t);
- let alpha = if g.opacity_stops.is_empty() { 1.0 } else { sample_stops(&g.opacity_stops.iter().map(|(p, a)| (*p, [*a; 3])).collect::<Vec<_>>(), t)[0] };
- [color[0], color[1], color[2], alpha]
+ PreparedGradient::new(g).sample(t)
+}
+
+// Prepared only for one paint call: CMYK conversions must use that call's active profile.
+struct PreparedGradient {
+ color: Vec<(f32, [f32; 3])>,
+ opacity: Vec<(f32, [f32; 3])>,
+}
+
+impl PreparedGradient {
+ fn new(g: &Gradient) -> Self {
+ Self { color: g.stops.iter().map(|(p, c)| (*p, rgb(c))).collect(), opacity: g.opacity_stops.iter().map(|(p, a)| (*p, [*a; 3])).collect() }
+ }
+
+ fn sample(&self, t: f32) -> [f32; 4] {
+ let color = sample_stops(&self.color, t);
+ let alpha = if self.opacity.is_empty() { 1.0 } else { sample_stops(&self.opacity, t)[0] };
+ [color[0], color[1], color[2], alpha]
+ }
}
fn sample_stops(stops: &[(f32, [f32; 3])], t: f32) -> [f32; 3] {
@@ -578,13 +594,14 @@ fn paint_fx(dst: &mut Buffer, m: &Map, p: &FxPaint, shape_bounds: Rect, anchor:
match p {
FxPaint::Color(c) => paint_color(dst, m, rgb(c), blend, opacity),
FxPaint::Gradient(g) => {
+ let prepared = PreparedGradient::new(g);
let w = big.width() as usize;
paint(
dst,
m,
|i| {
let (x, y) = ((big.x0 + (i % w) as i32) as f32 + 0.5, (big.y0 + (i / w) as i32) as f32 + 0.5);
- sample_gradient(g, gradient_t(g.style, g.angle, g.scale, g.reverse, g.offset, shape_bounds, x, y))
+ prepared.sample(gradient_t(g.style, g.angle, g.scale, g.reverse, g.offset, shape_bounds, x, y))
},
blend,
opacity,
diff --git a/crates/compose/src/lib.rs b/crates/compose/src/lib.rs
index 493e16262..54e2b6aa1 100644
--- a/crates/compose/src/lib.rs
+++ b/crates/compose/src/lib.rs
@@ -595,13 +595,21 @@ fn render_fill(f: &Fill, rect: Rect, canvas: Rect, patterns: &[Pattern]) -> Buff
Buffer::filled(rect, [rgb[0], rgb[1], rgb[2], c.alpha])
}
Fill::Gradient { stops, angle, scale, style, reverse } => {
+ // Convert once per render call, while its document CMYK profile is active.
+ let stops: Vec<_> = stops
+ .iter()
+ .map(|(p, c)| {
+ let rgb = c.to_rgb();
+ (*p, [rgb[0], rgb[1], rgb[2], c.alpha])
+ })
+ .collect();
// Gradient geometry relative to the layer's frame, independent of the render rect.
let mut b = Buffer::transparent(rect);
for y in rect.y0..rect.y1 {
for x in rect.x0..rect.x1 {
let t = effects::gradient_t(*style, *angle, *scale, *reverse, (0.0, 0.0), canvas, x as f32 + 0.5, y as f32 + 0.5);
let i = ((y - rect.y0) as usize) * rect.width() as usize + (x - rect.x0) as usize;
- b.px[i] = sample_stops(stops, t);
+ b.px[i] = sample_stops(&stops, t);
}
}
b
@@ -614,27 +622,23 @@ fn render_fill(f: &Fill, rect: Rect, canvas: Rect, patterns: &[Pattern]) -> Buff
}
}
-fn sample_stops(stops: &[(f32, photocraft_color::Color)], t: f32) -> [f32; 4] {
- let conv = |c: &photocraft_color::Color| {
- let r = c.to_rgb();
- [r[0], r[1], r[2], c.alpha]
- };
+fn sample_stops(stops: &[(f32, [f32; 4])], t: f32) -> [f32; 4] {
match stops {
[] => [0.0; 4],
- [only] => conv(&only.1),
+ [only] => only.1,
_ => {
if t <= stops[0].0 {
- return conv(&stops[0].1);
+ return stops[0].1;
}
for w in stops.windows(2) {
let (a, b) = (&w[0], &w[1]);
if t <= b.0 {
let k = if b.0 > a.0 { (t - a.0) / (b.0 - a.0) } else { 0.0 };
- let (ca, cb) = (conv(&a.1), conv(&b.1));
+ let (ca, cb) = (a.1, b.1);
return std::array::from_fn(|i| ca[i] + (cb[i] - ca[i]) * k);
}
}
- conv(&stops[stops.len() - 1].1)
+ stops[stops.len() - 1].1
}
}
}