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
//! Prepared decoder rendering. Samples retain absolute coordinates and their
//! original order, including when a trial renders only a sparse set of blocks.

use super::{
    CHANNEL_WEIGHT, PixelBox, Point, QuantizedSpline, Y_TO_B, Y_TO_X, adjusted_quant, blob_weight,
    blob_window, sample_curve,
};
use crate::encoding_context::EncodingContext;
use crate::image::Image3F;

pub(crate) struct Sample {
    pub(crate) position: Point<f32>,
    pub(crate) color: [f32; 3],
    pub(crate) inv_sigma: f32,
    pub(crate) amp: f32,
    /// Half-open pixel window.
    window: (usize, usize, usize, usize),
}

impl Sample {
    #[inline]
    fn row(&self, kernel: RenderRowFn, y: usize, x0: usize, rows: [&mut [f32]; 3], sign: f32) {
        kernel(self, y as f32 - self.position.y, x0, rows, self.amp * sign);
    }

    #[inline]
    pub(crate) fn scalar_row(&self, dy: f32, x0: usize, rows: [&mut [f32]; 3], amp: f32) {
        let [rx, ry, rb] = rows;
        for (x, ((rx, ry), rb)) in (x0..).zip(rx.iter_mut().zip(ry).zip(rb)) {
            let li = blob_weight(x as f32 - self.position.x, dy, self.inv_sigma, amp);
            *rx += self.color[0] * li;
            *ry += self.color[1] * li;
            *rb += self.color[2] * li;
        }
    }
}

pub(crate) type RenderRowFn = fn(&Sample, f32, usize, [&mut [f32]; 3], f32);

pub(crate) fn select_render_row_fn() -> RenderRowFn {
    #[cfg(all(target_arch = "aarch64", feature = "neon"))]
    return |sample, dy, x0, rows, amp| unsafe {
        crate::neon::spline_render_row_neon(sample, dy, x0, rows, amp)
    };
    #[cfg(all(target_arch = "x86_64", feature = "avx"))]
    if std::is_x86_feature_detected!("avx2") && std::is_x86_feature_detected!("fma") {
        return |sample, dy, x0, rows, amp| unsafe {
            crate::avx::spline_render_row_avx2(sample, dy, x0, rows, amp)
        };
    }
    #[allow(unreachable_code)]
    Sample::scalar_row
}

pub(super) struct RenderPlan {
    samples: Vec<Sample>,
    pub(super) bounds: Option<PixelBox>,
    width: usize,
    row: RenderRowFn,
}

impl RenderPlan {
    pub(super) fn new(
        ctx: &EncodingContext,
        sp: &QuantizedSpline,
        adjust: i32,
        w: usize,
        h: usize,
    ) -> Self {
        let inv_quant = 1.0 / adjusted_quant(adjust);
        let mut dct = [[0f32; 4]; 32];
        for (i, row) in dct.iter_mut().enumerate() {
            let f = if i == 0 {
                std::f32::consts::FRAC_1_SQRT_2
            } else {
                1.0
            };
            for ((v, channel), weight) in row.iter_mut().zip(&sp.dct).zip(CHANNEL_WEIGHT) {
                *v = channel[i] as f32 * f * weight * inv_quant;
            }
            row[0] += Y_TO_X * row[1];
            row[2] += Y_TO_B * row[1];
            for v in row {
                *v *= std::f32::consts::SQRT_2;
            }
        }
        let (samples, arc) = sample_curve(&sp.points);
        let mut plan = Self {
            samples: Vec::new(),
            bounds: None,
            width: w,
            row: ctx.spline_render_row,
        };
        if arc <= 0.0 {
            return plan;
        }
        plan.samples.reserve(samples.len());
        let continuous_idct = ctx.spline_continuous_idct;
        let r = 0.1f32.ln() * 5.0;
        for (k, sample) in samples.iter().enumerate() {
            let Point { x: cx, y: cy } = sample.position;
            let mult = sample.multiplier;
            let t = 31.0 * (k as f32 / arc).min(1.0);
            let [x, y, b, sigma] = continuous_idct(&dct, t);
            let color = [x, y, b];
            if !(sigma.is_finite() && sigma != 0.0 && (1.0 / sigma).is_finite()) {
                continue;
            }
            let mut max_color = 0.01f32;
            for c in color {
                max_color = max_color.max((c * mult).abs());
            }
            let reach = (-2.0 * sigma * sigma * (r - max_color.ln())).sqrt();
            let (x0, x1, y0, y1) = blob_window(cx, cy, reach, w, h);
            if x0 >= x1 || y0 >= y1 {
                continue;
            }
            plan.samples.push(Sample {
                position: sample.position,
                color,
                inv_sigma: 1.0 / sigma,
                amp: 0.25 * sigma * mult,
                window: (x0, x1, y0, y1),
            });
            plan.bounds = Some(match plan.bounds {
                None => (x0, y0, x1 - 1, y1 - 1),
                Some(b) => (b.0.min(x0), b.1.min(y0), b.2.max(x1 - 1), b.3.max(y1 - 1)),
            });
        }
        plan
    }

    pub(super) fn draw(&self, image: &mut Image3F, sign: f32) {
        for sample in &self.samples {
            let (x0, x1, y0, y1) = sample.window;
            for y in y0..y1 {
                let [rx, ry, rb] = image.all_plane_rows_mut(y);
                sample.row(
                    self.row,
                    y,
                    x0,
                    [&mut rx[x0..x1], &mut ry[x0..x1], &mut rb[x0..x1]],
                    sign,
                );
            }
        }
    }

    pub(super) fn tiled(self) -> TiledSpline {
        let blocks_w = self.width.div_ceil(8);
        let mut coverage = Vec::new();
        for (index, sample) in self.samples.iter().enumerate() {
            let (x0, x1, y0, y1) = sample.window;
            for by in y0 / 8..=(y1 - 1) / 8 {
                for bx in x0 / 8..=(x1 - 1) / 8 {
                    coverage.push((by * blocks_w + bx, index));
                }
            }
        }
        if let Some((x0, y0, x1, y1)) = self.bounds {
            let bx0 = x0 / 8;
            let by0 = y0 / 8;
            let bw = x1 / 8 - bx0 + 1;
            let bh = y1 / 8 - by0 + 1;
            // Bound the dense counters by the existing coverage allocation.
            if bw * bh <= coverage.len() && coverage.len() >= 256 {
                let mut offsets = vec![0usize; bw * bh];
                for &(cell, _) in &coverage {
                    let local = (cell / blocks_w - by0) * bw + cell % blocks_w - bx0;
                    offsets[local] += 1;
                }
                let mut tiles = Vec::new();
                let mut end = 0;
                for (local, count) in offsets.iter_mut().enumerate() {
                    let start = end;
                    end += *count;
                    if *count != 0 {
                        tiles.push(Tile {
                            cell: (local / bw + by0) * blocks_w + local % bw + bx0,
                            start,
                            end,
                        });
                    }
                    *count = start;
                }
                let mut sample_indices = vec![0usize; coverage.len()];
                // Coverage was emitted in sample order: stable scatter keeps
                // exactly the original sample order within every tile.
                for (cell, index) in coverage {
                    let local = (cell / blocks_w - by0) * bw + cell % blocks_w - bx0;
                    sample_indices[offsets[local]] = index;
                    offsets[local] += 1;
                }
                return TiledSpline {
                    plan: self,
                    tiles,
                    sample_indices,
                };
            }
        }
        // The sample index is a secondary key: each pixel sees additions in
        // decoder order even though different blocks can be rendered separately.
        coverage.sort_unstable();
        let mut tiles: Vec<Tile> = Vec::new();
        let mut sample_indices = Vec::with_capacity(coverage.len());
        for (cell, index) in coverage {
            if tiles.last().is_none_or(|tile| tile.cell != cell) {
                tiles.push(Tile {
                    cell,
                    start: sample_indices.len(),
                    end: sample_indices.len(),
                });
            }
            sample_indices.push(index);
            tiles.last_mut().unwrap().end += 1;
        }
        TiledSpline {
            plan: self,
            tiles,
            sample_indices,
        }
    }
}

pub(super) struct Tile {
    pub(super) cell: usize,
    start: usize,
    end: usize,
}

pub(super) struct TiledSpline {
    plan: RenderPlan,
    pub(super) tiles: Vec<Tile>,
    sample_indices: Vec<usize>,
}

impl TiledSpline {
    pub(super) fn bounds(&self) -> Option<PixelBox> {
        self.plan.bounds
    }

    pub(super) fn draw_tile(&self, tile: &Tile, pixels: &mut [[f32; 64]; 3], sign: f32) {
        let blocks_w = self.plan.width.div_ceil(8);
        let (tx, ty) = (tile.cell % blocks_w * 8, tile.cell / blocks_w * 8);
        for &index in &self.sample_indices[tile.start..tile.end] {
            let sample = &self.plan.samples[index];
            let (sx0, sx1, sy0, sy1) = sample.window;
            let (x0, x1) = (sx0.max(tx), sx1.min(tx + 8));
            for y in sy0.max(ty)..sy1.min(ty + 8) {
                let start = (y - ty) * 8 + x0 - tx;
                let end = start + x1 - x0;
                let [rx, ry, rb] = &mut *pixels;
                sample.row(
                    self.plan.row,
                    y,
                    x0,
                    [
                        &mut rx[start..end],
                        &mut ry[start..end],
                        &mut rb[start..end],
                    ],
                    sign,
                );
            }
        }
    }
}

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

    #[test]
    fn render_row_matches_scalar_for_all_alignments_and_tails() {
        let ctx = EncodingContext::default();
        for x0 in [
            3,
            16_777_215,
            16_777_217,
            crate::encode_image::MAX_DIMENSION - 32,
        ] {
            for inv_sigma in [-1.7, 0.31] {
                for dy in [-0.0, 2.375] {
                    let sample = Sample {
                        position: Point::new(x0 as f32 + 0.35, 0.0),
                        color: [-0.013, 0.071, 0.017],
                        inv_sigma,
                        amp: -0.12,
                        window: (0, 0, 0, 0),
                    };
                    for start in 0..8 {
                        for len in 0..=25 {
                            let end = start + len;
                            let mut expected: [[f32; 40]; 3] = std::array::from_fn(|c| {
                                std::array::from_fn(|i| (i * 19 + c * 7) as f32 * 0.003 - 0.3)
                            });
                            let mut actual = expected;
                            let [rx, ry, rb] = &mut expected;
                            sample.scalar_row(
                                dy,
                                x0,
                                [
                                    &mut rx[start..end],
                                    &mut ry[start..end],
                                    &mut rb[start..end],
                                ],
                                sample.amp,
                            );
                            let [rx, ry, rb] = &mut actual;
                            (ctx.spline_render_row)(
                                &sample,
                                dy,
                                x0,
                                [
                                    &mut rx[start..end],
                                    &mut ry[start..end],
                                    &mut rb[start..end],
                                ],
                                sample.amp,
                            );
                            for (a, b) in actual.iter().flatten().zip(expected.iter().flatten()) {
                                assert_eq!(
                                    a.to_bits(),
                                    b.to_bits(),
                                    "x0={x0}, start={start}, len={len}, inv_sigma={inv_sigma}, dy={dy}"
                                );
                            }
                        }
                    }
                }
            }
        }
    }

    #[test]
    fn tile_binning_preserves_sorted_coverage_and_sample_order() {
        let ctx = EncodingContext::default();
        // Exercise the threshold, a nonzero tile origin, clipped edge tiles,
        // empty cells inside dense bins, and a large sparse bounding rectangle.
        for (count, sparse) in [
            (0, false),
            (255, false),
            (256, false),
            (257, false),
            (257, true),
        ] {
            let width = 263usize;
            let blocks_w = width.div_ceil(8);
            let mut plan = RenderPlan {
                samples: Vec::new(),
                bounds: None,
                width,
                row: ctx.spline_render_row,
            };
            for i in 0..count {
                let (x, y) = if i % 2 == 0 {
                    if sparse { (8, 8) } else { (240, 32) }
                } else if sparse {
                    (256, 576)
                } else {
                    (256, 48)
                };
                let (x1, y1) = ((x + 8).min(width), y + 1);
                plan.samples.push(Sample {
                    position: Point::new(x as f32, y as f32),
                    color: [1.0; 3],
                    inv_sigma: 1.0,
                    amp: 1.0,
                    window: (x, x1, y, y1),
                });
                plan.bounds = Some(match plan.bounds {
                    None => (x, y, x1 - 1, y1 - 1),
                    Some(b) => (b.0.min(x), b.1.min(y), b.2.max(x1 - 1), b.3.max(y1 - 1)),
                });
            }
            // The original representation is sorted by (image block, sample).
            let mut expected = Vec::new();
            for (index, sample) in plan.samples.iter().enumerate() {
                let (x0, x1, y0, y1) = sample.window;
                for by in y0 / 8..=(y1 - 1) / 8 {
                    for bx in x0 / 8..=(x1 - 1) / 8 {
                        expected.push((by * blocks_w + bx, index));
                    }
                }
            }
            expected.sort_unstable();
            if let Some((x0, y0, x1, y1)) = plan.bounds {
                let cells = (x1 / 8 - x0 / 8 + 1) * (y1 / 8 - y0 / 8 + 1);
                assert_eq!(cells > expected.len(), sparse);
            }
            let tiled = plan.tiled();
            let mut actual = Vec::new();
            let mut end = 0;
            for tile in &tiled.tiles {
                assert_eq!(tile.start, end);
                assert!(tile.start < tile.end);
                actual.extend(
                    tiled.sample_indices[tile.start..tile.end]
                        .iter()
                        .map(|&i| (tile.cell, i)),
                );
                end = tile.end;
            }
            assert_eq!(end, tiled.sample_indices.len());
            assert_eq!(actual, expected, "count={count}, sparse={sparse}");
        }
    }

    #[test]
    fn prepared_render_matches_original_sample_arithmetic() {
        let kernels = crate::encoding_context::EncodingContext::default();
        for (w, h) in [(37, 29), (129, 87)] {
            for width in [0, 1, 4, -3] {
                let mut sp = QuantizedSpline {
                    points: vec![
                        Point::new(-2, 1),
                        Point::new(12, 23),
                        Point::new(w as i32 - 1, h as i32 - 2),
                    ],
                    dct: [[0; 32]; 4],
                };
                sp.dct[0][0] = -11;
                sp.dct[1][0] = 7;
                sp.dct[1][2] = -3;
                sp.dct[2][1] = 9;
                sp.dct[3][0] = width;
                sp.dct[3][3] = if width == 0 { 0 } else { 1 };
                for sign in [-1.0, 1.0] {
                    let mut original = Image3F::new(w, h);
                    for c in 0..3 {
                        for y in 0..h {
                            for (x, value) in original.plane_row_mut(c, y).iter_mut().enumerate() {
                                *value = ((x * 19 + y * 31 + c * 7) % 83) as f32 * 0.001 - 0.04;
                            }
                        }
                    }
                    let mut prepared = original.clone();
                    let bounds = reference_render(&sp, QUANT_ADJUST, &mut original, sign);
                    let plan = RenderPlan::new(&kernels, &sp, QUANT_ADJUST, w, h);
                    plan.draw(&mut prepared, sign);
                    assert_eq!(plan.bounds, bounds);
                    for c in 0..3 {
                        assert!(
                            original
                                .plane_data(c)
                                .iter()
                                .zip(prepared.plane_data(c))
                                .all(|(a, b)| a.to_bits() == b.to_bits())
                        );
                    }
                }
            }
        }
    }
    fn reference_render(
        sp: &QuantizedSpline,
        adjust: i32,
        xyb: &mut Image3F,
        sign: f32,
    ) -> Option<PixelBox> {
        let (w, h) = (xyb.xsize(), xyb.ysize());
        let inv_quant = 1.0 / adjusted_quant(adjust);
        // Interleave channels so the continuous IDCT can accumulate four SIMD lanes.
        let mut dct = [[0f32; 4]; 32];
        for (i, row) in dct.iter_mut().enumerate() {
            let f = if i == 0 {
                std::f32::consts::FRAC_1_SQRT_2
            } else {
                1.0
            };
            for ((v, channel), weight) in row.iter_mut().zip(&sp.dct).zip(CHANNEL_WEIGHT) {
                *v = channel[i] as f32 * f * weight * inv_quant;
            }
            row[0] += Y_TO_X * row[1];
            row[2] += Y_TO_B * row[1];
            for v in row {
                *v *= std::f32::consts::SQRT_2;
            }
        }
        let (samples, arc) = sample_curve(&sp.points);
        if arc <= 0.0 {
            return None;
        }
        let continuous_idct = super::super::transform::continuous_idct_scalar;
        let mut touched: Option<PixelBox> = None;
        for (k, sample) in samples.iter().enumerate() {
            let Point { x: cx, y: cy } = sample.position;
            let mult = sample.multiplier;
            let t = 31.0 * (k as f32 / arc).min(1.0);
            let [x, y, b, sigma] = continuous_idct(&dct, t);
            let color = [x, y, b];
            if !(sigma.is_finite() && sigma != 0.0 && (1.0 / sigma).is_finite()) {
                continue;
            }
            let mut max_color = 0.01f32;
            for c in color {
                max_color = max_color.max((c * mult).abs());
            }
            let reach = (-2.0 * sigma * sigma * (0.1f32.ln() * 5.0 - max_color.ln())).sqrt();
            let (x0, x1, y0, y1) = blob_window(cx, cy, reach, w, h);
            if x0 >= x1 || y0 >= y1 {
                continue;
            }
            let (inv_sigma, amp) = (1.0 / sigma, 0.25 * sigma * mult * sign);
            for y in y0..y1 {
                let dy = y as f32 - cy;
                let [rx, ry, rb] = xyb.all_plane_rows_mut(y);
                for (x, ((rx, ry), rb)) in (x0..x1).zip(
                    rx[x0..x1]
                        .iter_mut()
                        .zip(&mut ry[x0..x1])
                        .zip(&mut rb[x0..x1]),
                ) {
                    let li = blob_weight(x as f32 - cx, dy, inv_sigma, amp);
                    *rx += color[0] * li;
                    *ry += color[1] * li;
                    *rb += color[2] * li;
                }
            }
            touched = Some(match touched {
                None => (x0, y0, x1 - 1, y1 - 1),
                Some(b) => (b.0.min(x0), b.1.min(y0), b.2.max(x1 - 1), b.3.max(y1 - 1)),
            });
        }
        touched
    }
}