agg-rust 1.0.2

Pure Rust port of Anti-Grain Geometry (AGG) 2.6 - high quality 2D vector graphics rendering
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
//! Base renderer with clipping.
//!
//! Port of `agg_renderer_base.h` — wraps a pixel format with a clip rectangle,
//! ensuring all rendering operations are bounded within the visible area.

use crate::basics::{CoverType, RectI};
use crate::pixfmt_rgba::PixelFormat;

// ============================================================================
// RendererBase — clip-and-delegate renderer
// ============================================================================

/// Base renderer that clips all operations to a rectangle before delegating
/// to the underlying pixel format.
///
/// Port of C++ `renderer_base<PixelFormat>`.
pub struct RendererBase<PF: PixelFormat> {
    ren: PF,
    clip_box: RectI,
}

impl<PF: PixelFormat> RendererBase<PF> {
    /// Create a new renderer wrapping the given pixel format.
    /// The clip box is initialized to the full buffer extent.
    pub fn new(ren: PF) -> Self {
        let w = ren.width() as i32;
        let h = ren.height() as i32;
        Self {
            ren,
            clip_box: RectI::new(0, 0, w - 1, h - 1),
        }
    }

    pub fn width(&self) -> u32 {
        self.ren.width()
    }
    pub fn height(&self) -> u32 {
        self.ren.height()
    }

    /// Set the clip rectangle (will be intersected with the buffer bounds).
    pub fn clip_box_i(&mut self, x1: i32, y1: i32, x2: i32, y2: i32) -> bool {
        let mut cb = RectI::new(x1, y1, x2, y2);
        cb.normalize();
        if cb.clip(&RectI::new(
            0,
            0,
            self.ren.width() as i32 - 1,
            self.ren.height() as i32 - 1,
        )) {
            self.clip_box = cb;
            true
        } else {
            self.clip_box.x1 = 1;
            self.clip_box.y1 = 1;
            self.clip_box.x2 = 0;
            self.clip_box.y2 = 0;
            false
        }
    }

    /// Reset clipping to the full buffer or to nothing.
    pub fn reset_clipping(&mut self, visibility: bool) {
        if visibility {
            self.clip_box.x1 = 0;
            self.clip_box.y1 = 0;
            self.clip_box.x2 = self.ren.width() as i32 - 1;
            self.clip_box.y2 = self.ren.height() as i32 - 1;
        } else {
            self.clip_box.x1 = 1;
            self.clip_box.y1 = 1;
            self.clip_box.x2 = 0;
            self.clip_box.y2 = 0;
        }
    }

    pub fn clip_box(&self) -> &RectI {
        &self.clip_box
    }
    pub fn xmin(&self) -> i32 {
        self.clip_box.x1
    }
    pub fn ymin(&self) -> i32 {
        self.clip_box.y1
    }
    pub fn xmax(&self) -> i32 {
        self.clip_box.x2
    }
    pub fn ymax(&self) -> i32 {
        self.clip_box.y2
    }

    #[inline]
    pub fn inbox(&self, x: i32, y: i32) -> bool {
        x >= self.clip_box.x1
            && y >= self.clip_box.y1
            && x <= self.clip_box.x2
            && y <= self.clip_box.y2
    }

    /// Get a reference to the underlying pixel format.
    pub fn ren(&self) -> &PF {
        &self.ren
    }

    /// Get a mutable reference to the underlying pixel format.
    pub fn ren_mut(&mut self) -> &mut PF {
        &mut self.ren
    }

    // ========================================================================
    // Rendering operations (clip then delegate)
    // ========================================================================

    /// Clear the entire buffer to a solid color.
    pub fn clear(&mut self, c: &PF::ColorType) {
        let w = self.ren.width();
        if w > 0 {
            let h = self.ren.height();
            for y in 0..h as i32 {
                self.ren.copy_hline(0, y, w, c);
            }
        }
    }

    /// Copy a single pixel (clipped).
    pub fn copy_pixel(&mut self, x: i32, y: i32, c: &PF::ColorType) {
        if self.inbox(x, y) {
            self.ren.copy_pixel(x, y, c);
        }
    }

    /// Blend a single pixel (clipped).
    pub fn blend_pixel(&mut self, x: i32, y: i32, c: &PF::ColorType, cover: CoverType) {
        if self.inbox(x, y) {
            self.ren.blend_pixel(x, y, c, cover);
        }
    }

    /// Get the pixel at (x, y), or default if outside clip.
    pub fn pixel(&self, x: i32, y: i32) -> PF::ColorType
    where
        PF::ColorType: Default,
    {
        if self.inbox(x, y) {
            self.ren.pixel(x, y)
        } else {
            PF::ColorType::default()
        }
    }

    /// Copy a horizontal line (clipped). x1, x2 are inclusive endpoints.
    pub fn copy_hline(&mut self, mut x1: i32, y: i32, mut x2: i32, c: &PF::ColorType) {
        if x1 > x2 {
            std::mem::swap(&mut x1, &mut x2);
        }
        if y > self.ymax() || y < self.ymin() || x1 > self.xmax() || x2 < self.xmin() {
            return;
        }
        x1 = x1.max(self.xmin());
        x2 = x2.min(self.xmax());
        self.ren.copy_hline(x1, y, (x2 - x1 + 1) as u32, c);
    }

    /// Blend a horizontal line (clipped). x1, x2 are inclusive endpoints.
    pub fn blend_hline(
        &mut self,
        mut x1: i32,
        y: i32,
        mut x2: i32,
        c: &PF::ColorType,
        cover: CoverType,
    ) {
        if x1 > x2 {
            std::mem::swap(&mut x1, &mut x2);
        }
        if y > self.ymax() || y < self.ymin() || x1 > self.xmax() || x2 < self.xmin() {
            return;
        }
        x1 = x1.max(self.xmin());
        x2 = x2.min(self.xmax());
        self.ren.blend_hline(x1, y, (x2 - x1 + 1) as u32, c, cover);
    }

    /// Blend a vertical line (clipped). y1, y2 are inclusive endpoints.
    pub fn blend_vline(
        &mut self,
        x: i32,
        mut y1: i32,
        mut y2: i32,
        c: &PF::ColorType,
        cover: CoverType,
    ) {
        if y1 > y2 {
            std::mem::swap(&mut y1, &mut y2);
        }
        if x > self.xmax() || x < self.xmin() || y1 > self.ymax() || y2 < self.ymin() {
            return;
        }
        y1 = y1.max(self.ymin());
        y2 = y2.min(self.ymax());
        for y in y1..=y2 {
            self.ren.blend_pixel(x, y, c, cover);
        }
    }

    /// Blend a filled rectangle (clipped).
    pub fn blend_bar(
        &mut self,
        x1: i32,
        y1: i32,
        x2: i32,
        y2: i32,
        c: &PF::ColorType,
        cover: CoverType,
    ) {
        let mut rc = crate::basics::RectI::new(x1, y1, x2, y2);
        rc.normalize();
        if !rc.clip(&self.clip_box) {
            return;
        }
        for y in rc.y1..=rc.y2 {
            self.ren
                .blend_hline(rc.x1, y, (rc.x2 - rc.x1 + 1) as u32, c, cover);
        }
    }

    /// Blend a solid horizontal span with per-pixel coverage (clipped).
    pub fn blend_solid_hspan(
        &mut self,
        mut x: i32,
        y: i32,
        mut len: i32,
        c: &PF::ColorType,
        covers: &[CoverType],
    ) {
        if y > self.ymax() || y < self.ymin() {
            return;
        }

        let mut covers_offset = 0usize;
        if x < self.xmin() {
            let d = self.xmin() - x;
            len -= d;
            if len <= 0 {
                return;
            }
            covers_offset += d as usize;
            x = self.xmin();
        }
        if x + len > self.xmax() + 1 {
            len = self.xmax() - x + 1;
            if len <= 0 {
                return;
            }
        }
        self.ren
            .blend_solid_hspan(x, y, len as u32, c, &covers[covers_offset..]);
    }

    /// Blend a solid vertical span with per-pixel coverage (clipped).
    pub fn blend_solid_vspan(
        &mut self,
        x: i32,
        mut y: i32,
        mut len: i32,
        c: &PF::ColorType,
        covers: &[CoverType],
    ) {
        if x > self.xmax() || x < self.xmin() {
            return;
        }

        let mut covers_offset = 0usize;
        if y < self.ymin() {
            let d = self.ymin() - y;
            len -= d;
            if len <= 0 {
                return;
            }
            covers_offset += d as usize;
            y = self.ymin();
        }
        if y + len > self.ymax() + 1 {
            len = self.ymax() - y + 1;
            if len <= 0 {
                return;
            }
        }
        for i in 0..len as usize {
            self.ren
                .blend_pixel(x, y + i as i32, c, covers[covers_offset + i]);
        }
    }

    /// Blend a horizontal span with per-pixel colors (clipped).
    ///
    /// If `covers` is non-empty, each pixel uses its corresponding coverage.
    /// If `covers` is empty, all pixels use the uniform `cover` value.
    pub fn blend_color_hspan(
        &mut self,
        mut x: i32,
        y: i32,
        mut len: i32,
        colors: &[PF::ColorType],
        covers: &[CoverType],
        cover: CoverType,
    ) {
        if y > self.ymax() || y < self.ymin() {
            return;
        }

        let mut colors_offset = 0usize;
        let mut covers_offset = 0usize;
        if x < self.xmin() {
            let d = (self.xmin() - x) as usize;
            len -= d as i32;
            if len <= 0 {
                return;
            }
            if !covers.is_empty() {
                covers_offset += d;
            }
            colors_offset += d;
            x = self.xmin();
        }
        if x + len > self.xmax() + 1 {
            len = self.xmax() - x + 1;
            if len <= 0 {
                return;
            }
        }
        self.ren.blend_color_hspan(
            x,
            y,
            len as u32,
            &colors[colors_offset..],
            if covers.is_empty() {
                &[]
            } else {
                &covers[covers_offset..]
            },
            cover,
        );
    }

    /// Blend a vertical span with per-pixel colors (clipped).
    ///
    /// If `covers` is non-empty, each pixel uses its corresponding coverage.
    /// If `covers` is empty, all pixels use the uniform `cover` value.
    ///
    /// Port of C++ `renderer_base::blend_color_vspan`.
    pub fn blend_color_vspan(
        &mut self,
        x: i32,
        mut y: i32,
        mut len: i32,
        colors: &[PF::ColorType],
        covers: &[CoverType],
        cover: CoverType,
    ) {
        if x > self.xmax() || x < self.xmin() {
            return;
        }

        let mut colors_offset = 0usize;
        let mut covers_offset = 0usize;
        if y < self.ymin() {
            let d = (self.ymin() - y) as usize;
            len -= d as i32;
            if len <= 0 {
                return;
            }
            if !covers.is_empty() {
                covers_offset += d;
            }
            colors_offset += d;
            y = self.ymin();
        }
        if y + len > self.ymax() + 1 {
            len = self.ymax() - y + 1;
            if len <= 0 {
                return;
            }
        }
        // Iterate vertically, blending one pixel per row.
        for i in 0..len as usize {
            let c = if covers.is_empty() { cover } else { covers[covers_offset + i] };
            self.ren.blend_pixel(x, y + i as i32, &colors[colors_offset + i], c);
        }
    }
}

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

#[cfg(test)]
mod tests {
    use super::*;
    use crate::color::Rgba8;
    use crate::pixfmt_rgba::PixfmtRgba32;
    use crate::rendering_buffer::RowAccessor;

    const BPP: usize = 4;

    fn make_renderer(w: u32, h: u32) -> (Vec<u8>, RowAccessor) {
        let stride = (w * BPP as u32) as i32;
        let buf = vec![0u8; (h * w * BPP as u32) as usize];
        let mut ra = RowAccessor::new();
        unsafe {
            ra.attach(buf.as_ptr() as *mut u8, w, h, stride);
        }
        (buf, ra)
    }

    #[test]
    fn test_new() {
        let (_buf, mut ra) = make_renderer(100, 80);
        let pf = PixfmtRgba32::new(&mut ra);
        let ren = RendererBase::new(pf);
        assert_eq!(ren.width(), 100);
        assert_eq!(ren.height(), 80);
        assert_eq!(ren.xmin(), 0);
        assert_eq!(ren.ymin(), 0);
        assert_eq!(ren.xmax(), 99);
        assert_eq!(ren.ymax(), 79);
    }

    #[test]
    fn test_clear() {
        let (_buf, mut ra) = make_renderer(10, 10);
        let pf = PixfmtRgba32::new(&mut ra);
        let mut ren = RendererBase::new(pf);
        let white = Rgba8::new(255, 255, 255, 255);
        ren.clear(&white);
        let p = ren.ren().pixel(5, 5);
        assert_eq!(p.r, 255);
        assert_eq!(p.a, 255);
    }

    #[test]
    fn test_copy_pixel_clipped() {
        let (_buf, mut ra) = make_renderer(10, 10);
        let pf = PixfmtRgba32::new(&mut ra);
        let mut ren = RendererBase::new(pf);
        let red = Rgba8::new(255, 0, 0, 255);
        // Inside clip box
        ren.copy_pixel(5, 5, &red);
        assert_eq!(ren.ren().pixel(5, 5).r, 255);
        // Outside clip box — should be silently ignored
        ren.copy_pixel(-1, 5, &red);
        ren.copy_pixel(100, 5, &red);
    }

    #[test]
    fn test_blend_hline_clipped() {
        let (_buf, mut ra) = make_renderer(20, 10);
        let pf = PixfmtRgba32::new(&mut ra);
        let mut ren = RendererBase::new(pf);
        let green = Rgba8::new(0, 255, 0, 255);
        // Line extends beyond right edge
        ren.blend_hline(15, 5, 25, &green, 255);
        // Pixels within bounds should be drawn
        assert_eq!(ren.ren().pixel(15, 5).g, 255);
        assert_eq!(ren.ren().pixel(19, 5).g, 255);
    }

    #[test]
    fn test_clip_box() {
        let (_buf, mut ra) = make_renderer(100, 100);
        let pf = PixfmtRgba32::new(&mut ra);
        let mut ren = RendererBase::new(pf);
        assert!(ren.clip_box_i(10, 10, 50, 50));
        assert_eq!(ren.xmin(), 10);
        assert_eq!(ren.ymin(), 10);
        assert_eq!(ren.xmax(), 50);
        assert_eq!(ren.ymax(), 50);
    }

    #[test]
    fn test_clip_box_invalid() {
        let (_buf, mut ra) = make_renderer(100, 100);
        let pf = PixfmtRgba32::new(&mut ra);
        let mut ren = RendererBase::new(pf);
        // Clip box entirely outside buffer
        assert!(!ren.clip_box_i(200, 200, 300, 300));
    }

    #[test]
    fn test_blend_solid_hspan_clipped() {
        let (_buf, mut ra) = make_renderer(20, 10);
        let pf = PixfmtRgba32::new(&mut ra);
        let mut ren = RendererBase::new(pf);
        let blue = Rgba8::new(0, 0, 255, 255);
        // Span starts before clip box left edge
        let covers = vec![255u8; 10];
        ren.blend_solid_hspan(-3, 5, 10, &blue, &covers);
        // First 3 pixels should be clipped, pixels 0..6 should be drawn
        assert_eq!(ren.ren().pixel(0, 5).b, 255);
        assert_eq!(ren.ren().pixel(6, 5).b, 255);
    }

    #[test]
    fn test_blend_solid_hspan_fully_clipped() {
        let (_buf, mut ra) = make_renderer(20, 10);
        let pf = PixfmtRgba32::new(&mut ra);
        let mut ren = RendererBase::new(pf);
        let red = Rgba8::new(255, 0, 0, 255);
        let covers = [255u8; 5];
        // Span entirely above clip box
        ren.blend_solid_hspan(5, -1, 5, &red, &covers);
        // Nothing should be drawn
        assert_eq!(ren.ren().pixel(5, 0).r, 0);
    }

    #[test]
    fn test_inbox() {
        let (_buf, mut ra) = make_renderer(10, 10);
        let pf = PixfmtRgba32::new(&mut ra);
        let ren = RendererBase::new(pf);
        assert!(ren.inbox(0, 0));
        assert!(ren.inbox(9, 9));
        assert!(!ren.inbox(-1, 0));
        assert!(!ren.inbox(10, 0));
        assert!(!ren.inbox(0, 10));
    }

    #[test]
    fn test_reset_clipping() {
        let (_buf, mut ra) = make_renderer(100, 100);
        let pf = PixfmtRgba32::new(&mut ra);
        let mut ren = RendererBase::new(pf);
        ren.clip_box_i(10, 10, 50, 50);
        ren.reset_clipping(true);
        assert_eq!(ren.xmin(), 0);
        assert_eq!(ren.ymin(), 0);
        assert_eq!(ren.xmax(), 99);
        assert_eq!(ren.ymax(), 99);

        ren.reset_clipping(false);
        assert!(!ren.inbox(0, 0));
    }

    #[test]
    fn test_blend_color_hspan() {
        let (_buf, mut ra) = make_renderer(20, 10);
        let pf = PixfmtRgba32::new(&mut ra);
        let mut ren = RendererBase::new(pf);
        let colors = [
            Rgba8::new(255, 0, 0, 255),
            Rgba8::new(0, 255, 0, 255),
            Rgba8::new(0, 0, 255, 255),
        ];
        ren.blend_color_hspan(5, 3, 3, &colors, &[], 255);
        let p0 = ren.ren().pixel(5, 3);
        assert_eq!(p0.r, 255);
        let p1 = ren.ren().pixel(6, 3);
        assert_eq!(p1.g, 255);
        let p2 = ren.ren().pixel(7, 3);
        assert_eq!(p2.b, 255);
    }

    #[test]
    fn test_blend_color_hspan_clipped_left() {
        let (_buf, mut ra) = make_renderer(20, 10);
        let pf = PixfmtRgba32::new(&mut ra);
        let mut ren = RendererBase::new(pf);
        let colors = [
            Rgba8::new(255, 0, 0, 255),
            Rgba8::new(0, 255, 0, 255),
            Rgba8::new(0, 0, 255, 255),
        ];
        // Starts at x=-1, so first color is clipped
        ren.blend_color_hspan(-1, 3, 3, &colors, &[], 255);
        // colors[0] (red) at x=-1 → clipped
        // colors[1] (green) at x=0
        let p0 = ren.ren().pixel(0, 3);
        assert_eq!(p0.g, 255);
        // colors[2] (blue) at x=1
        let p1 = ren.ren().pixel(1, 3);
        assert_eq!(p1.b, 255);
    }

    #[test]
    fn test_blend_color_hspan_clipped_y() {
        let (_buf, mut ra) = make_renderer(20, 10);
        let pf = PixfmtRgba32::new(&mut ra);
        let mut ren = RendererBase::new(pf);
        let colors = [Rgba8::new(255, 0, 0, 255)];
        // y=-1 → fully clipped
        ren.blend_color_hspan(5, -1, 1, &colors, &[], 255);
        let p = ren.ren().pixel(5, 0);
        assert_eq!(p.r, 0);
    }
}