maple-render-core 0.3.0

Core rendering and animation logic for maple templates
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
use image::{Rgba, RgbaImage};

#[derive(Debug, Clone, Copy, Default)]
pub struct Pixer {
    pub r: f32,
    pub g: f32,
    pub b: f32,
    pub a: f32,
}

impl Pixer {
    #[inline(always)]
    pub fn new() -> Self {
        Pixer::default()
    }

    #[inline(always)]
    pub fn from_rgba(pixel: &Rgba<u8>) -> Self {
        Pixer { r: pixel[0] as f32, g: pixel[1] as f32, b: pixel[2] as f32, a: pixel[3] as f32 }
    }

    #[inline(always)]
    pub fn to_rgba(&self) -> Rgba<u8> {
        Rgba([clamp_u8(self.r), clamp_u8(self.g), clamp_u8(self.b), clamp_u8(self.a)])
    }

    #[inline(always)]
    pub fn preblend(&mut self) {
        self.r *= self.a;
        self.g *= self.a;
        self.b *= self.a;
    }

    #[inline(always)]
    pub fn postblend(&mut self, scale: f32) {
        if scale > 0.0001 {
            self.r /= scale;
            self.g /= scale;
            self.b /= scale;
        } else {
            self.r = 0.0;
            self.g = 0.0;
            self.b = 0.0;
        }
    }

    pub fn add(&mut self, other: &Pixer) {
        self.r += other.r;
        self.g += other.g;
        self.b += other.b;
        self.a += other.a;
    }

    pub fn add_rgba(&mut self, pixel: &Rgba<u8>) {
        self.r += pixel[0] as f32;
        self.g += pixel[1] as f32;
        self.b += pixel[2] as f32;
        self.a += pixel[3] as f32;
    }

    pub fn scale(&mut self, factor: f32) {
        self.r *= factor;
        self.g *= factor;
        self.b *= factor;
        self.a *= factor;
    }

    pub fn div(&mut self, factor: f32) {
        if factor.abs() > 0.0001 {
            self.r /= factor;
            self.g /= factor;
            self.b /= factor;
            self.a /= factor;
        }
    }
}

impl std::ops::Add for Pixer {
    type Output = Pixer;

    fn add(self, other: Pixer) -> Pixer {
        Pixer { r: self.r + other.r, g: self.g + other.g, b: self.b + other.b, a: self.a + other.a }
    }
}

impl std::ops::AddAssign for Pixer {
    fn add_assign(&mut self, other: Pixer) {
        self.r += other.r;
        self.g += other.g;
        self.b += other.b;
        self.a += other.a;
    }
}

impl std::ops::Mul<f32> for Pixer {
    type Output = Pixer;

    fn mul(self, factor: f32) -> Pixer {
        Pixer { r: self.r * factor, g: self.g * factor, b: self.b * factor, a: self.a * factor }
    }
}

impl std::ops::Div<f32> for Pixer {
    type Output = Pixer;

    fn div(self, factor: f32) -> Pixer {
        if factor.abs() > 0.0001 {
            Pixer { r: self.r / factor, g: self.g / factor, b: self.b / factor, a: self.a / factor }
        } else {
            Pixer::new()
        }
    }
}

#[inline]
fn clamp_u8(v: f32) -> u8 {
    if v <= 0.0 {
        0
    } else if v >= 255.0 {
        255
    } else {
        v as u8
    }
}

#[inline(always)]
pub fn safe_pixel(img: &RgbaImage, x: i32, y: i32) -> Rgba<u8> {
    let w = img.width() as i32;
    let h = img.height() as i32;

    if x < 0 || y < 0 || x >= w || y >= h {
        Rgba([0, 0, 0, 0])
    } else {
        *img.get_pixel(x as u32, y as u32)
    }
}

/// Bilinear interpolation with alpha-weighted averaging.
///
/// Coordinates remain `f64` for sub-pixel accuracy (the heavy lifting is the
/// per-pixel color blend, which is performed in `f32` for throughput). The
/// `f64` geometry inputs are down-cast to `f32` when constructing the returned
/// `Pixer`.
#[inline(always)]
pub fn sample_linear(img: &RgbaImage, x: f64, y: f64) -> Pixer {
    let xx = x.floor() as i32;
    let yy = y.floor() as i32;
    let fx = x - xx as f64;
    let fy = y - yy as f64;

    let w00 = ((1.0 - fx) * (1.0 - fy)) as f32;
    let w10 = (fx * (1.0 - fy)) as f32;
    let w01 = ((1.0 - fx) * fy) as f32;
    let w11 = (fx * fy) as f32;

    let w = img.width() as i32;
    let h = img.height() as i32;

    if xx >= 0 && yy >= 0 && xx + 1 < w && yy + 1 < h {
        let raw = img.as_raw();
        let w_us = w as usize;
        let row_stride = w_us * 4;

        let row0 = yy as usize * row_stride;
        let row1 = row0 + row_stride;
        let col = xx as usize * 4;

        let i00 = row0 + col;
        let i10 = i00 + 4;
        let i01 = row1 + col;
        let i11 = i01 + 4;

        let a00 = raw[i00 + 3] as f32;
        let a10 = raw[i10 + 3] as f32;
        let a01 = raw[i01 + 3] as f32;
        let a11 = raw[i11 + 3] as f32;

        let aa = w00 * a00 + w10 * a10 + w01 * a01 + w11 * a11;
        let aa_safe = if aa < 0.0001 { 0.0001 } else { aa };

        return Pixer {
            r: (w00 * raw[i00] as f32 * a00
                + w10 * raw[i10] as f32 * a10
                + w01 * raw[i01] as f32 * a01
                + w11 * raw[i11] as f32 * a11)
                / aa_safe,
            g: (w00 * raw[i00 + 1] as f32 * a00
                + w10 * raw[i10 + 1] as f32 * a10
                + w01 * raw[i01 + 1] as f32 * a01
                + w11 * raw[i11 + 1] as f32 * a11)
                / aa_safe,
            b: (w00 * raw[i00 + 2] as f32 * a00
                + w10 * raw[i10 + 2] as f32 * a10
                + w01 * raw[i01 + 2] as f32 * a01
                + w11 * raw[i11 + 2] as f32 * a11)
                / aa_safe,
            a: aa,
        };
    }

    let p00 = safe_pixel(img, xx, yy);
    let p10 = safe_pixel(img, xx + 1, yy);
    let p01 = safe_pixel(img, xx, yy + 1);
    let p11 = safe_pixel(img, xx + 1, yy + 1);

    let a00 = p00[3] as f32;
    let a10 = p10[3] as f32;
    let a01 = p01[3] as f32;
    let a11 = p11[3] as f32;

    let aa = w00 * a00 + w10 * a10 + w01 * a01 + w11 * a11;
    let aa_safe = if aa < 0.0001 { 0.0001 } else { aa };

    Pixer {
        r: (w00 * p00[0] as f32 * a00
            + w10 * p10[0] as f32 * a10
            + w01 * p01[0] as f32 * a01
            + w11 * p11[0] as f32 * a11)
            / aa_safe,
        g: (w00 * p00[1] as f32 * a00
            + w10 * p10[1] as f32 * a10
            + w01 * p01[1] as f32 * a01
            + w11 * p11[1] as f32 * a11)
            / aa_safe,
        b: (w00 * p00[2] as f32 * a00
            + w10 * p10[2] as f32 * a10
            + w01 * p01[2] as f32 * a01
            + w11 * p11[2] as f32 * a11)
            / aa_safe,
        a: aa,
    }
}

/// Bilinear interpolation in premultiplied-alpha space.
///
/// The sampled compositor combines several neighboring samples before
/// converting back to straight alpha. Keeping RGB premultiplied here avoids a
/// divide followed immediately by a multiply for every tap.
#[inline(always)]
pub(crate) fn sample_linear_premultiplied(img: &RgbaImage, x: f64, y: f64) -> Pixer {
    let xx = x.floor() as i32;
    let yy = y.floor() as i32;
    let fx = x - xx as f64;
    let fy = y - yy as f64;

    let w00 = ((1.0 - fx) * (1.0 - fy)) as f32;
    let w10 = (fx * (1.0 - fy)) as f32;
    let w01 = ((1.0 - fx) * fy) as f32;
    let w11 = (fx * fy) as f32;

    let w = img.width() as i32;
    let h = img.height() as i32;

    if xx >= 0 && yy >= 0 && xx + 1 < w && yy + 1 < h {
        let raw = img.as_raw();
        let row_stride = w as usize * 4;

        let i00 = yy as usize * row_stride + xx as usize * 4;
        let i10 = i00 + 4;
        let i01 = i00 + row_stride;
        let i11 = i01 + 4;

        let a00 = raw[i00 + 3] as f32;
        let a10 = raw[i10 + 3] as f32;
        let a01 = raw[i01 + 3] as f32;
        let a11 = raw[i11 + 3] as f32;

        return Pixer {
            r: w00 * raw[i00] as f32 * a00
                + w10 * raw[i10] as f32 * a10
                + w01 * raw[i01] as f32 * a01
                + w11 * raw[i11] as f32 * a11,
            g: w00 * raw[i00 + 1] as f32 * a00
                + w10 * raw[i10 + 1] as f32 * a10
                + w01 * raw[i01 + 1] as f32 * a01
                + w11 * raw[i11 + 1] as f32 * a11,
            b: w00 * raw[i00 + 2] as f32 * a00
                + w10 * raw[i10 + 2] as f32 * a10
                + w01 * raw[i01 + 2] as f32 * a01
                + w11 * raw[i11 + 2] as f32 * a11,
            a: w00 * a00 + w10 * a10 + w01 * a01 + w11 * a11,
        };
    }

    let p00 = safe_pixel(img, xx, yy);
    let p10 = safe_pixel(img, xx + 1, yy);
    let p01 = safe_pixel(img, xx, yy + 1);
    let p11 = safe_pixel(img, xx + 1, yy + 1);

    let a00 = p00[3] as f32;
    let a10 = p10[3] as f32;
    let a01 = p01[3] as f32;
    let a11 = p11[3] as f32;

    Pixer {
        r: w00 * p00[0] as f32 * a00
            + w10 * p10[0] as f32 * a10
            + w01 * p01[0] as f32 * a01
            + w11 * p11[0] as f32 * a11,
        g: w00 * p00[1] as f32 * a00
            + w10 * p10[1] as f32 * a10
            + w01 * p01[1] as f32 * a01
            + w11 * p11[1] as f32 * a11,
        b: w00 * p00[2] as f32 * a00
            + w10 * p10[2] as f32 * a10
            + w01 * p01[2] as f32 * a01
            + w11 * p11[2] as f32 * a11,
        a: w00 * a00 + w10 * a10 + w01 * a01 + w11 * a11,
    }
}

/// Bilinear interpolation for an image known to contain only opaque pixels.
///
/// This preserves the general sampler's floating-point operation order while
/// replacing four alpha loads with the known value `255`.
#[inline(always)]
pub(crate) fn sample_linear_opaque(img: &RgbaImage, x: f64, y: f64) -> Pixer {
    let xx = x.floor() as i32;
    let yy = y.floor() as i32;
    let fx = x - xx as f64;
    let fy = y - yy as f64;

    let w00 = ((1.0 - fx) * (1.0 - fy)) as f32;
    let w10 = (fx * (1.0 - fy)) as f32;
    let w01 = ((1.0 - fx) * fy) as f32;
    let w11 = (fx * fy) as f32;

    let w = img.width() as i32;
    let h = img.height() as i32;

    if xx >= 0 && yy >= 0 && xx + 1 < w && yy + 1 < h {
        let raw = img.as_raw();
        let row_stride = w as usize * 4;

        let i00 = yy as usize * row_stride + xx as usize * 4;
        let i10 = i00 + 4;
        let i01 = i00 + row_stride;
        let i11 = i01 + 4;

        let alpha = 255.0;

        return Pixer {
            r: w00 * raw[i00] as f32 * alpha
                + w10 * raw[i10] as f32 * alpha
                + w01 * raw[i01] as f32 * alpha
                + w11 * raw[i11] as f32 * alpha,
            g: w00 * raw[i00 + 1] as f32 * alpha
                + w10 * raw[i10 + 1] as f32 * alpha
                + w01 * raw[i01 + 1] as f32 * alpha
                + w11 * raw[i11 + 1] as f32 * alpha,
            b: w00 * raw[i00 + 2] as f32 * alpha
                + w10 * raw[i10 + 2] as f32 * alpha
                + w01 * raw[i01 + 2] as f32 * alpha
                + w11 * raw[i11 + 2] as f32 * alpha,
            a: w00 * alpha + w10 * alpha + w01 * alpha + w11 * alpha,
        };
    }

    let p00 = safe_pixel(img, xx, yy);
    let p10 = safe_pixel(img, xx + 1, yy);
    let p01 = safe_pixel(img, xx, yy + 1);
    let p11 = safe_pixel(img, xx + 1, yy + 1);
    let a00 = p00[3] as f32;
    let a10 = p10[3] as f32;
    let a01 = p01[3] as f32;
    let a11 = p11[3] as f32;

    Pixer {
        r: w00 * p00[0] as f32 * a00
            + w10 * p10[0] as f32 * a10
            + w01 * p01[0] as f32 * a01
            + w11 * p11[0] as f32 * a11,
        g: w00 * p00[1] as f32 * a00
            + w10 * p10[1] as f32 * a10
            + w01 * p01[1] as f32 * a01
            + w11 * p11[1] as f32 * a11,
        b: w00 * p00[2] as f32 * a00
            + w10 * p10[2] as f32 * a10
            + w01 * p01[2] as f32 * a01
            + w11 * p11[2] as f32 * a11,
        a: w00 * a00 + w10 * a10 + w01 * a01 + w11 * a11,
    }
}

pub fn sample_weakly(img: &RgbaImage, x: f64, y: f64) -> Pixer {
    Pixer::from_rgba(&safe_pixel(img, x as i32, y as i32))
}

#[inline(always)]
pub fn distance(x1: f64, y1: f64, x2: f64, y2: f64) -> f64 {
    ((x1 - x2).powi(2) + (y1 - y2).powi(2)).sqrt()
}

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

    #[test]
    fn test_pixer_operations() {
        let mut p = Pixer { r: 100.0, g: 150.0, b: 200.0, a: 255.0 };
        p.scale(0.5);
        assert_eq!(p.r, 50.0);
        assert_eq!(p.g, 75.0);
        assert_eq!(p.b, 100.0);
    }

    #[test]
    fn test_clamp_u8() {
        assert_eq!(clamp_u8(-10.0), 0);
        assert_eq!(clamp_u8(128.0), 128);
        assert_eq!(clamp_u8(300.0), 255);
    }

    #[test]
    fn opaque_sampler_matches_general_premultiplied_sampler() {
        let mut image = RgbaImage::new(3, 2);
        for (i, pixel) in image.pixels_mut().enumerate() {
            let value = u8::try_from(i * 31).expect("test value fits in u8");
            *pixel = Rgba([value, value.wrapping_add(17), value.wrapping_add(83), 255]);
        }

        for (x, y) in [
            (-1.25, -0.5),
            (-0.25, 0.25),
            (0.0, 0.0),
            (0.3, 0.7),
            (1.5, 0.25),
            (2.0, 1.0),
            (2.75, 1.75),
            (4.0, 4.0),
        ] {
            let general = sample_linear_premultiplied(&image, x, y);
            let opaque = sample_linear_opaque(&image, x, y);
            assert_eq!(general.r.to_bits(), opaque.r.to_bits());
            assert_eq!(general.g.to_bits(), opaque.g.to_bits());
            assert_eq!(general.b.to_bits(), opaque.b.to_bits());
            assert_eq!(general.a.to_bits(), opaque.a.to_bits());
        }
    }
}