agg-rust-azul 1.0.2

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
//! Perspective span interpolators.
//!
//! Port of `agg_span_interpolator_persp.h`.
//! Two variants for perspective coordinate interpolation:
//! - `SpanInterpolatorPerspExact` — uses exact perspective iterator
//! - `SpanInterpolatorPerspLerp` — uses linear interpolation with DDA

use crate::basics::{iround, uround};
use crate::dda_line::Dda2LineInterpolator;
use crate::span_interpolator_linear::SpanInterpolator;
use crate::trans_perspective::{PerspectiveIteratorX, TransPerspective};

/// Subpixel precision for perspective interpolation.
const SUBPIXEL_SHIFT: u32 = 8;
const SUBPIXEL_SCALE: i32 = 1 << SUBPIXEL_SHIFT;

/// Helper: compute local scale from perspective transform at a point.
/// Returns the scale factor as a subpixel-shifted integer, then right-shifted.
fn calc_scale(
    xt: f64,
    yt: f64,
    x_src: f64,
    y_src: f64,
    trans_inv: &TransPerspective,
    dx_offset: f64,
    dy_offset: f64,
) -> i32 {
    let mut dx = xt + dx_offset;
    let mut dy = yt + dy_offset;
    trans_inv.transform(&mut dx, &mut dy);
    dx -= x_src;
    dy -= y_src;
    (uround(SUBPIXEL_SCALE as f64 / (dx * dx + dy * dy).sqrt()) >> SUBPIXEL_SHIFT) as i32
}

// ============================================================================
// SpanInterpolatorPerspExact
// ============================================================================

/// Exact perspective span interpolator.
///
/// Port of C++ `span_interpolator_persp_exact<SubpixelShift>`.
/// Uses `PerspectiveIteratorX` for exact perspective division at each pixel.
/// Scale factors are linearly interpolated via DDA.
pub struct SpanInterpolatorPerspExact {
    trans_dir: TransPerspective,
    trans_inv: TransPerspective,
    iterator: PerspectiveIteratorX,
    scale_x: Dda2LineInterpolator,
    scale_y: Dda2LineInterpolator,
}

impl SpanInterpolatorPerspExact {
    pub fn new() -> Self {
        Self {
            trans_dir: TransPerspective::new(),
            trans_inv: TransPerspective::new(),
            iterator: PerspectiveIteratorX::default_new(),
            scale_x: Dda2LineInterpolator::new_forward(0, 0, 1),
            scale_y: Dda2LineInterpolator::new_forward(0, 0, 1),
        }
    }

    pub fn new_quad_to_quad(src: &[f64; 8], dst: &[f64; 8]) -> Self {
        let mut s = Self::new();
        s.quad_to_quad(src, dst);
        s
    }

    pub fn new_rect_to_quad(x1: f64, y1: f64, x2: f64, y2: f64, quad: &[f64; 8]) -> Self {
        let mut s = Self::new();
        s.rect_to_quad(x1, y1, x2, y2, quad);
        s
    }

    pub fn new_quad_to_rect(quad: &[f64; 8], x1: f64, y1: f64, x2: f64, y2: f64) -> Self {
        let mut s = Self::new();
        s.quad_to_rect(quad, x1, y1, x2, y2);
        s
    }

    pub fn quad_to_quad(&mut self, src: &[f64; 8], dst: &[f64; 8]) {
        self.trans_dir.quad_to_quad(src, dst);
        self.trans_inv.quad_to_quad(dst, src);
    }

    pub fn rect_to_quad(&mut self, x1: f64, y1: f64, x2: f64, y2: f64, quad: &[f64; 8]) {
        let src = [x1, y1, x2, y1, x2, y2, x1, y2];
        self.quad_to_quad(&src, quad);
    }

    pub fn quad_to_rect(&mut self, quad: &[f64; 8], x1: f64, y1: f64, x2: f64, y2: f64) {
        let dst = [x1, y1, x2, y1, x2, y2, x1, y2];
        self.quad_to_quad(quad, &dst);
    }

    pub fn is_valid(&self) -> bool {
        self.trans_dir.is_valid()
    }

    pub fn local_scale(&self, x: &mut i32, y: &mut i32) {
        *x = self.scale_x.y();
        *y = self.scale_y.y();
    }

    pub fn transform(&self, x: &mut f64, y: &mut f64) {
        self.trans_dir.transform(x, y);
    }

    pub fn trans_dir(&self) -> &TransPerspective {
        &self.trans_dir
    }

    pub fn trans_inv(&self) -> &TransPerspective {
        &self.trans_inv
    }
}

impl SpanInterpolator for SpanInterpolatorPerspExact {
    fn begin(&mut self, x: f64, y: f64, len: u32) {
        self.iterator = self.trans_dir.begin(x, y, 1.0);
        let xt = self.iterator.x;
        let yt = self.iterator.y;

        let delta = 1.0 / SUBPIXEL_SCALE as f64;

        let sx1 = calc_scale(xt, yt, x, y, &self.trans_inv, delta, 0.0);
        let sy1 = calc_scale(xt, yt, x, y, &self.trans_inv, 0.0, delta);

        let x2 = x + len as f64;
        let mut xt2 = x2;
        let mut yt2 = y;
        self.trans_dir.transform(&mut xt2, &mut yt2);

        let sx2 = calc_scale(xt2, yt2, x2, y, &self.trans_inv, delta, 0.0);
        let sy2 = calc_scale(xt2, yt2, x2, y, &self.trans_inv, 0.0, delta);

        self.scale_x = Dda2LineInterpolator::new_forward(sx1, sx2, len as i32);
        self.scale_y = Dda2LineInterpolator::new_forward(sy1, sy2, len as i32);
    }

    fn next(&mut self) {
        self.iterator.next();
        self.scale_x.inc();
        self.scale_y.inc();
    }

    fn coordinates(&self, x: &mut i32, y: &mut i32) {
        *x = iround(self.iterator.x * SUBPIXEL_SCALE as f64);
        *y = iround(self.iterator.y * SUBPIXEL_SCALE as f64);
    }
}

// ============================================================================
// SpanInterpolatorPerspLerp
// ============================================================================

/// Linear-interpolation perspective span interpolator.
///
/// Port of C++ `span_interpolator_persp_lerp<SubpixelShift>`.
/// Transforms only the endpoints and linearly interpolates coordinates
/// between them using DDA. Faster but less accurate than the exact variant.
pub struct SpanInterpolatorPerspLerp {
    trans_dir: TransPerspective,
    trans_inv: TransPerspective,
    coord_x: Dda2LineInterpolator,
    coord_y: Dda2LineInterpolator,
    scale_x: Dda2LineInterpolator,
    scale_y: Dda2LineInterpolator,
}

impl SpanInterpolatorPerspLerp {
    pub fn new() -> Self {
        Self {
            trans_dir: TransPerspective::new(),
            trans_inv: TransPerspective::new(),
            coord_x: Dda2LineInterpolator::new_forward(0, 0, 1),
            coord_y: Dda2LineInterpolator::new_forward(0, 0, 1),
            scale_x: Dda2LineInterpolator::new_forward(0, 0, 1),
            scale_y: Dda2LineInterpolator::new_forward(0, 0, 1),
        }
    }

    pub fn new_quad_to_quad(src: &[f64; 8], dst: &[f64; 8]) -> Self {
        let mut s = Self::new();
        s.quad_to_quad(src, dst);
        s
    }

    pub fn new_rect_to_quad(x1: f64, y1: f64, x2: f64, y2: f64, quad: &[f64; 8]) -> Self {
        let mut s = Self::new();
        s.rect_to_quad(x1, y1, x2, y2, quad);
        s
    }

    pub fn new_quad_to_rect(quad: &[f64; 8], x1: f64, y1: f64, x2: f64, y2: f64) -> Self {
        let mut s = Self::new();
        s.quad_to_rect(quad, x1, y1, x2, y2);
        s
    }

    pub fn quad_to_quad(&mut self, src: &[f64; 8], dst: &[f64; 8]) {
        self.trans_dir.quad_to_quad(src, dst);
        self.trans_inv.quad_to_quad(dst, src);
    }

    pub fn rect_to_quad(&mut self, x1: f64, y1: f64, x2: f64, y2: f64, quad: &[f64; 8]) {
        let src = [x1, y1, x2, y1, x2, y2, x1, y2];
        self.quad_to_quad(&src, quad);
    }

    pub fn quad_to_rect(&mut self, quad: &[f64; 8], x1: f64, y1: f64, x2: f64, y2: f64) {
        let dst = [x1, y1, x2, y1, x2, y2, x1, y2];
        self.quad_to_quad(quad, &dst);
    }

    pub fn is_valid(&self) -> bool {
        self.trans_dir.is_valid()
    }

    pub fn local_scale(&self, x: &mut i32, y: &mut i32) {
        *x = self.scale_x.y();
        *y = self.scale_y.y();
    }

    pub fn transform(&self, x: &mut f64, y: &mut f64) {
        self.trans_dir.transform(x, y);
    }

    pub fn trans_dir(&self) -> &TransPerspective {
        &self.trans_dir
    }

    pub fn trans_inv(&self) -> &TransPerspective {
        &self.trans_inv
    }
}

impl SpanInterpolator for SpanInterpolatorPerspLerp {
    fn begin(&mut self, x: f64, y: f64, len: u32) {
        // Transform start point
        let mut xt = x;
        let mut yt = y;
        self.trans_dir.transform(&mut xt, &mut yt);
        let x1 = iround(xt * SUBPIXEL_SCALE as f64);
        let y1 = iround(yt * SUBPIXEL_SCALE as f64);

        let delta = 1.0 / SUBPIXEL_SCALE as f64;

        let sx1 = calc_scale(xt, yt, x, y, &self.trans_inv, delta, 0.0);
        let sy1 = calc_scale(xt, yt, x, y, &self.trans_inv, 0.0, delta);

        // Transform end point
        let x_end = x + len as f64;
        let mut xt2 = x_end;
        let mut yt2 = y;
        self.trans_dir.transform(&mut xt2, &mut yt2);
        let x2 = iround(xt2 * SUBPIXEL_SCALE as f64);
        let y2 = iround(yt2 * SUBPIXEL_SCALE as f64);

        let sx2 = calc_scale(xt2, yt2, x_end, y, &self.trans_inv, delta, 0.0);
        let sy2 = calc_scale(xt2, yt2, x_end, y, &self.trans_inv, 0.0, delta);

        self.coord_x = Dda2LineInterpolator::new_forward(x1, x2, len as i32);
        self.coord_y = Dda2LineInterpolator::new_forward(y1, y2, len as i32);
        self.scale_x = Dda2LineInterpolator::new_forward(sx1, sx2, len as i32);
        self.scale_y = Dda2LineInterpolator::new_forward(sy1, sy2, len as i32);
    }

    fn next(&mut self) {
        self.coord_x.inc();
        self.coord_y.inc();
        self.scale_x.inc();
        self.scale_y.inc();
    }

    fn coordinates(&self, x: &mut i32, y: &mut i32) {
        *x = self.coord_x.y();
        *y = self.coord_y.y();
    }
}

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

    #[test]
    fn test_exact_identity() {
        let src = [0.0, 0.0, 100.0, 0.0, 100.0, 100.0, 0.0, 100.0];
        let dst = [0.0, 0.0, 100.0, 0.0, 100.0, 100.0, 0.0, 100.0];
        let mut interp = SpanInterpolatorPerspExact::new_quad_to_quad(&src, &dst);
        assert!(interp.is_valid());

        interp.begin(50.0, 50.0, 10);
        let (mut x, mut y) = (0, 0);
        interp.coordinates(&mut x, &mut y);
        // Should be approximately (50*256, 50*256) = (12800, 12800)
        assert!((x - 12800).abs() < 5, "x={x}");
        assert!((y - 12800).abs() < 5, "y={y}");
    }

    #[test]
    fn test_exact_next_advances() {
        let src = [0.0, 0.0, 100.0, 0.0, 100.0, 100.0, 0.0, 100.0];
        let dst = [0.0, 0.0, 100.0, 0.0, 100.0, 100.0, 0.0, 100.0];
        let mut interp = SpanInterpolatorPerspExact::new_quad_to_quad(&src, &dst);

        interp.begin(0.0, 50.0, 10);
        let (mut x1, mut y1) = (0, 0);
        interp.coordinates(&mut x1, &mut y1);

        interp.next();
        let (mut x2, mut y2) = (0, 0);
        interp.coordinates(&mut x2, &mut y2);

        // x should advance by roughly 256 (1 pixel in subpixel coords)
        assert!(x2 > x1, "x2={x2} should be > x1={x1}");
        assert!((x2 - x1 - 256).abs() < 5, "step={}", x2 - x1);
    }

    #[test]
    fn test_lerp_identity() {
        let src = [0.0, 0.0, 100.0, 0.0, 100.0, 100.0, 0.0, 100.0];
        let dst = [0.0, 0.0, 100.0, 0.0, 100.0, 100.0, 0.0, 100.0];
        let mut interp = SpanInterpolatorPerspLerp::new_quad_to_quad(&src, &dst);
        assert!(interp.is_valid());

        interp.begin(50.0, 50.0, 10);
        let (mut x, mut y) = (0, 0);
        interp.coordinates(&mut x, &mut y);
        assert!((x - 12800).abs() < 5, "x={x}");
        assert!((y - 12800).abs() < 5, "y={y}");
    }

    #[test]
    fn test_lerp_next_advances() {
        let src = [0.0, 0.0, 100.0, 0.0, 100.0, 100.0, 0.0, 100.0];
        let dst = [0.0, 0.0, 100.0, 0.0, 100.0, 100.0, 0.0, 100.0];
        let mut interp = SpanInterpolatorPerspLerp::new_quad_to_quad(&src, &dst);

        interp.begin(0.0, 50.0, 10);
        let (mut x1, mut y1) = (0, 0);
        interp.coordinates(&mut x1, &mut y1);

        interp.next();
        let (mut x2, mut y2) = (0, 0);
        interp.coordinates(&mut x2, &mut y2);

        assert!(x2 > x1, "x2={x2} should be > x1={x1}");
        assert!((x2 - x1 - 256).abs() < 5, "step={}", x2 - x1);
    }

    #[test]
    fn test_exact_rect_to_quad() {
        let quad = [10.0, 10.0, 110.0, 10.0, 110.0, 110.0, 10.0, 110.0];
        let mut interp = SpanInterpolatorPerspExact::new_rect_to_quad(0.0, 0.0, 100.0, 100.0, &quad);
        assert!(interp.is_valid());

        // Point (0,0) in rect space → (10,10) in quad space
        interp.begin(0.0, 0.0, 1);
        let (mut x, mut y) = (0, 0);
        interp.coordinates(&mut x, &mut y);
        assert!((x - 10 * 256).abs() < 5, "x={x}");
        assert!((y - 10 * 256).abs() < 5, "y={y}");
    }

    #[test]
    fn test_lerp_rect_to_quad() {
        let quad = [10.0, 10.0, 110.0, 10.0, 110.0, 110.0, 10.0, 110.0];
        let mut interp = SpanInterpolatorPerspLerp::new_rect_to_quad(0.0, 0.0, 100.0, 100.0, &quad);
        assert!(interp.is_valid());

        interp.begin(0.0, 0.0, 1);
        let (mut x, mut y) = (0, 0);
        interp.coordinates(&mut x, &mut y);
        assert!((x - 10 * 256).abs() < 5, "x={x}");
        assert!((y - 10 * 256).abs() < 5, "y={y}");
    }

    #[test]
    fn test_exact_and_lerp_agree_on_identity() {
        let src = [0.0, 0.0, 200.0, 0.0, 200.0, 200.0, 0.0, 200.0];
        let dst = src;

        let mut exact = SpanInterpolatorPerspExact::new_quad_to_quad(&src, &dst);
        let mut lerp = SpanInterpolatorPerspLerp::new_quad_to_quad(&src, &dst);

        exact.begin(10.0, 10.0, 5);
        lerp.begin(10.0, 10.0, 5);

        for _ in 0..5 {
            let (mut ex, mut ey) = (0, 0);
            let (mut lx, mut ly) = (0, 0);
            exact.coordinates(&mut ex, &mut ey);
            lerp.coordinates(&mut lx, &mut ly);
            assert!((ex - lx).abs() < 3, "ex={ex} lx={lx}");
            assert!((ey - ly).abs() < 3, "ey={ey} ly={ly}");

            exact.next();
            lerp.next();
        }
    }
}