agg-rust-azul 1.0.3

Pure Rust port of Anti-Grain Geometry (AGG) 2.6 - high quality 2D vector graphics rendering (azul fork)
Documentation
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
//! Gradient color lookup table.
//!
//! Port of `agg_gradient_lut.h` — builds a LUT (lookup table) from SVG-style
//! color stops. Used by `SpanGradient` to map gradient distances to colors.

use crate::basics::uround;
use crate::color::Rgba8;
use crate::dda_line::DdaLineInterpolator;

// ============================================================================
// ColorFunction trait
// ============================================================================

/// Trait for color lookup functions used by span_gradient.
///
/// Provides indexed access to a color palette of known size.
pub trait ColorFunction {
    type Color;

    fn size(&self) -> usize;
    fn get(&self, index: usize) -> Self::Color;
}

// ============================================================================
// ColorInterpolator — generic version using gradient() method
// ============================================================================

/// Generic color interpolator using the color type's `gradient()` method.
///
/// Port of C++ `color_interpolator<ColorT>` (generic template).
struct ColorInterpolatorGeneric<C> {
    c1: C,
    c2: C,
    len: u32,
    count: u32,
}

impl<C: Clone> ColorInterpolatorGeneric<C> {
    fn new(c1: &C, c2: &C, len: u32) -> Self {
        Self {
            c1: c1.clone(),
            c2: c2.clone(),
            len,
            count: 0,
        }
    }

    fn inc(&mut self) {
        self.count += 1;
    }
}

impl ColorInterpolatorGeneric<Rgba8> {
    fn color(&self) -> Rgba8 {
        self.c1
            .gradient(&self.c2, self.count as f64 / self.len as f64)
    }
}

// ============================================================================
// ColorInterpolatorRgba8 — fast DDA specialization for Rgba8
// ============================================================================

/// Fast RGBA8 color interpolator using 14-bit DDA interpolation.
///
/// Port of C++ `color_interpolator<rgba8>` specialization.
struct ColorInterpolatorRgba8 {
    r: DdaLineInterpolator<14, 0>,
    g: DdaLineInterpolator<14, 0>,
    b: DdaLineInterpolator<14, 0>,
    a: DdaLineInterpolator<14, 0>,
}

impl ColorInterpolatorRgba8 {
    fn new(c1: &Rgba8, c2: &Rgba8, len: u32) -> Self {
        Self {
            r: DdaLineInterpolator::new(c1.r as i32, c2.r as i32, len),
            g: DdaLineInterpolator::new(c1.g as i32, c2.g as i32, len),
            b: DdaLineInterpolator::new(c1.b as i32, c2.b as i32, len),
            a: DdaLineInterpolator::new(c1.a as i32, c2.a as i32, len),
        }
    }

    fn inc(&mut self) {
        self.r.inc();
        self.g.inc();
        self.b.inc();
        self.a.inc();
    }

    fn color(&self) -> Rgba8 {
        Rgba8::new(
            self.r.y() as u32,
            self.g.y() as u32,
            self.b.y() as u32,
            self.a.y() as u32,
        )
    }
}

// ============================================================================
// GradientLut
// ============================================================================

/// Color stop for gradient definition.
#[derive(Clone)]
struct ColorPoint {
    offset: f64,
    color: Rgba8,
}

impl ColorPoint {
    fn new(offset: f64, color: Rgba8) -> Self {
        Self {
            offset: offset.clamp(0.0, 1.0),
            color,
        }
    }
}

/// Gradient color lookup table.
///
/// Builds a 256-entry (or custom size) color LUT from SVG-style color stops.
/// Supports arbitrary numbers of stops at positions [0..1].
///
/// Port of C++ `gradient_lut<ColorInterpolator, ColorLutSize>`.
pub struct GradientLut {
    color_profile: Vec<ColorPoint>,
    color_lut: Vec<Rgba8>,
    lut_size: usize,
    use_fast_interpolator: bool,
}

impl GradientLut {
    /// Create a new gradient LUT with the specified size (default 256).
    pub fn new(lut_size: usize) -> Self {
        Self {
            color_profile: Vec::new(),
            color_lut: vec![Rgba8::default(); lut_size],
            lut_size,
            use_fast_interpolator: true,
        }
    }

    /// Create a new gradient LUT with default size of 256.
    pub fn new_default() -> Self {
        Self::new(256)
    }

    /// Set whether to use the fast DDA interpolator (default: true).
    pub fn set_use_fast_interpolator(&mut self, fast: bool) {
        self.use_fast_interpolator = fast;
    }

    /// Remove all color stops.
    pub fn remove_all(&mut self) {
        self.color_profile.clear();
    }

    /// Add a color stop at the given offset (clamped to [0..1]).
    pub fn add_color(&mut self, offset: f64, color: Rgba8) {
        self.color_profile.push(ColorPoint::new(offset, color));
    }

    /// Build the lookup table by interpolating between color stops.
    ///
    /// Must have at least 2 color stops. Stops are sorted by offset
    /// and duplicates are removed.
    pub fn build_lut(&mut self) {
        // Sort by offset
        self.color_profile
            .sort_by(|a, b| a.offset.partial_cmp(&b.offset).unwrap());
        // Remove duplicates (same offset)
        self.color_profile
            .dedup_by(|a, b| (a.offset - b.offset).abs() < 1e-10);

        if self.color_profile.len() < 2 {
            return;
        }

        let size = self.lut_size;
        let mut start = uround(self.color_profile[0].offset * size as f64) as usize;

        // Fill before first stop with first color
        let c = self.color_profile[0].color;
        for i in 0..start.min(size) {
            self.color_lut[i] = c;
        }

        // Interpolate between stops
        for i in 1..self.color_profile.len() {
            let end = uround(self.color_profile[i].offset * size as f64) as usize;
            // The loops below write entries `start..end` (that's `end - start` of them),
            // calling `color()` then `inc()` each time, so the LAST written entry uses
            // step index `end - start - 1`. A DdaLineInterpolator sized `count` only
            // reaches its end color after `count` steps, so to land the segment's end
            // color exactly on that last entry the interpolator must be sized
            // `end - start - 1`. The previous `end - start + 1` sized it two steps too
            // long, so every segment stopped ~2/255 short of its end color (a
            // black->white ramp ended at 253, not 255).
            let seg_len = if end > start { (end - start - 1).max(1) } else { 1 };

            if self.use_fast_interpolator {
                let mut ci = ColorInterpolatorRgba8::new(
                    &self.color_profile[i - 1].color,
                    &self.color_profile[i].color,
                    seg_len as u32,
                );
                while start < end && start < size {
                    self.color_lut[start] = ci.color();
                    ci.inc();
                    start += 1;
                }
            } else {
                let mut ci = ColorInterpolatorGeneric::new(
                    &self.color_profile[i - 1].color,
                    &self.color_profile[i].color,
                    seg_len as u32,
                );
                while start < end && start < size {
                    self.color_lut[start] = ci.color();
                    ci.inc();
                    start += 1;
                }
            }
        }

        // Fill after last stop with last color
        let c = self.color_profile.last().unwrap().color;
        let mut end = start;
        while end < size {
            self.color_lut[end] = c;
            end += 1;
        }
    }
}

impl ColorFunction for GradientLut {
    type Color = Rgba8;

    fn size(&self) -> usize {
        self.lut_size
    }

    fn get(&self, index: usize) -> Rgba8 {
        self.color_lut[index]
    }
}

// ============================================================================
// GradientLinearColor — simple 2-color linear interpolation
// ============================================================================

/// Simple 2-color linear gradient color function.
///
/// Interpolates between two colors based on index/size ratio.
///
/// Port of C++ `gradient_linear_color<ColorT>`.
pub struct GradientLinearColor {
    c1: Rgba8,
    c2: Rgba8,
    size: usize,
}

impl GradientLinearColor {
    pub fn new(c1: Rgba8, c2: Rgba8, size: usize) -> Self {
        Self { c1, c2, size }
    }

    pub fn colors(&mut self, c1: Rgba8, c2: Rgba8) {
        self.c1 = c1;
        self.c2 = c2;
    }
}

impl ColorFunction for GradientLinearColor {
    type Color = Rgba8;

    fn size(&self) -> usize {
        self.size
    }

    fn get(&self, index: usize) -> Rgba8 {
        self.c1
            .gradient(&self.c2, index as f64 / (self.size - 1).max(1) as f64)
    }
}

// ============================================================================
// Tests
// ============================================================================

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_gradient_lut_new() {
        let lut = GradientLut::new_default();
        assert_eq!(lut.size(), 256);
    }

    #[test]
    fn test_gradient_lut_two_stops() {
        let mut lut = GradientLut::new_default();
        lut.add_color(0.0, Rgba8::new(255, 0, 0, 255));
        lut.add_color(1.0, Rgba8::new(0, 0, 255, 255));
        lut.build_lut();

        // First should be exactly red
        let c0 = lut.get(0);
        assert_eq!(c0.r, 255);
        assert_eq!(c0.b, 0);

        // Last entry must land EXACTLY on the end color (the interpolator is now sized
        // so the ramp reaches its endpoint instead of stopping ~2/255 short).
        let c255 = lut.get(255);
        assert_eq!(c255.r, 0, "c255.r={}", c255.r);
        assert_eq!(c255.b, 255, "c255.b={}", c255.b);

        // Middle should be roughly equal
        let c128 = lut.get(128);
        assert!(c128.r > 50 && c128.r < 200, "Mid r={}", c128.r);
        assert!(c128.b > 50 && c128.b < 200, "Mid b={}", c128.b);
    }

    /// Regression test for the gradient-LUT interpolator length off-by-one.
    ///
    /// `build_lut` fills entries `start..end` (that's `end - start` of them), writing the
    /// interpolator's current color then stepping it once per entry — so the LAST entry
    /// is written at step index `end - start - 1`. A `DdaLineInterpolator` reaches its end
    /// color only after `count` steps, so `count` must be `end - start - 1` for that last
    /// entry to land on the end color.
    ///
    /// The interpolator used to be sized `end - start + 1`. For a single black→white
    /// segment over the whole 256-entry table (`start = 0`, `end = 256`) that made the
    /// last entry land at step 255 of a 257-step ramp:
    ///
    ///     255 * 255 / 257 ≈ 253
    ///
    /// so `get(255)` came back as (253, 253, 253) instead of pure white — every
    /// multi-stop gradient's endpoint was tinted ~2/255 toward the start color. With the
    /// interpolator sized `end - start - 1`, a segment whose length divides the color
    /// range (here 255 over 255 steps) reaches the endpoint exactly.
    #[test]
    fn gradient_lut_two_stop_ramp_reaches_its_end_color_exactly() {
        let mut lut = GradientLut::new_default();
        lut.add_color(0.0, Rgba8::new(0, 0, 0, 255)); // black
        lut.add_color(1.0, Rgba8::new(255, 255, 255, 255)); // white
        lut.build_lut();

        // Start is pure black.
        let first = lut.get(0);
        assert_eq!((first.r, first.g, first.b), (0, 0, 0), "first = {first:?}");

        // End is pure white. Pre-fix this was ~(253, 253, 253) — the bug this guards.
        let last = lut.get(255);
        assert_eq!(
            (last.r, last.g, last.b),
            (255, 255, 255),
            "the ramp must reach pure white; got {last:?}",
        );
    }

    #[test]
    fn test_gradient_lut_three_stops() {
        let mut lut = GradientLut::new_default();
        lut.add_color(0.0, Rgba8::new(255, 0, 0, 255));
        lut.add_color(0.5, Rgba8::new(0, 255, 0, 255));
        lut.add_color(1.0, Rgba8::new(0, 0, 255, 255));
        lut.build_lut();

        // Start: exactly red
        assert_eq!(lut.get(0).r, 255);
        // End: blue within 1 LSB. Here the last segment spans 127 entries over a range
        // of 255, which integer DDA can't divide exactly, so it lands at 254 — but that
        // is the residual rounding error, not the old ~2/255 endpoint shortfall.
        assert!(lut.get(255).b >= 254, "last.b={}", lut.get(255).b);
        // Middle: should be mostly green
        let mid = lut.get(128);
        assert!(mid.g > 128, "Mid green={}", mid.g);
    }

    #[test]
    fn test_gradient_lut_remove_all() {
        let mut lut = GradientLut::new_default();
        lut.add_color(0.0, Rgba8::new(255, 0, 0, 255));
        lut.remove_all();
        lut.add_color(0.0, Rgba8::new(0, 255, 0, 255));
        lut.add_color(1.0, Rgba8::new(0, 255, 0, 255));
        lut.build_lut();
        assert_eq!(lut.get(0).g, 255);
    }

    #[test]
    fn test_gradient_lut_generic_interpolator() {
        let mut lut = GradientLut::new_default();
        lut.set_use_fast_interpolator(false);
        lut.add_color(0.0, Rgba8::new(255, 0, 0, 255));
        lut.add_color(1.0, Rgba8::new(0, 0, 255, 255));
        lut.build_lut();

        let c0 = lut.get(0);
        assert_eq!(c0.r, 255);
        let c255 = lut.get(255);
        assert!(c255.b >= 252, "c255.b={}", c255.b);
    }

    #[test]
    fn test_gradient_lut_custom_size() {
        let mut lut = GradientLut::new(64);
        lut.add_color(0.0, Rgba8::new(0, 0, 0, 255));
        lut.add_color(1.0, Rgba8::new(255, 255, 255, 255));
        lut.build_lut();
        assert_eq!(lut.size(), 64);
        assert_eq!(lut.get(0).r, 0);
        assert!(lut.get(63).r >= 244, "last.r={}", lut.get(63).r);
    }

    #[test]
    fn test_gradient_linear_color() {
        let gc = GradientLinearColor::new(
            Rgba8::new(0, 0, 0, 255),
            Rgba8::new(255, 255, 255, 255),
            256,
        );
        assert_eq!(gc.size(), 256);

        let c0 = gc.get(0);
        assert_eq!(c0.r, 0);

        let c255 = gc.get(255);
        assert_eq!(c255.r, 255);

        let c128 = gc.get(128);
        assert!(c128.r > 100 && c128.r < 160, "c128.r={}", c128.r);
    }

    #[test]
    fn test_gradient_lut_unsorted_stops() {
        let mut lut = GradientLut::new_default();
        lut.add_color(1.0, Rgba8::new(0, 0, 255, 255));
        lut.add_color(0.0, Rgba8::new(255, 0, 0, 255));
        lut.build_lut();

        // Should still work — stops are sorted internally
        assert_eq!(lut.get(0).r, 255);
        assert!(lut.get(255).b >= 252, "last.b={}", lut.get(255).b);
    }

    #[test]
    fn test_color_interpolator_rgba8_fast() {
        let c1 = Rgba8::new(0, 0, 0, 255);
        let c2 = Rgba8::new(255, 255, 255, 255);
        let mut ci = ColorInterpolatorRgba8::new(&c1, &c2, 10);

        let first = ci.color();
        assert_eq!(first.r, 0);

        for _ in 0..10 {
            ci.inc();
        }
        let last = ci.color();
        assert_eq!(last.r, 255);
    }

    #[test]
    fn test_gradient_linear_color_set_colors() {
        let mut gc = GradientLinearColor::new(
            Rgba8::new(0, 0, 0, 255),
            Rgba8::new(255, 255, 255, 255),
            256,
        );
        gc.colors(Rgba8::new(255, 0, 0, 255), Rgba8::new(0, 255, 0, 255));
        assert_eq!(gc.get(0).r, 255);
        assert_eq!(gc.get(0).g, 0);
        assert_eq!(gc.get(255).r, 0);
        assert_eq!(gc.get(255).g, 255);
    }
}