oxihuman-export 0.2.1

Export pipeline for OxiHuman — glTF, COLLADA, STL, and streaming formats
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
// Copyright (C) 2026 COOLJAPAN OU (Team KitaSan)
// SPDX-License-Identifier: Apache-2.0
#![allow(dead_code)]

//! AV1 intra prediction modes for 4×4 blocks.
//!
//! The lossless backbone uses `DcPred` exclusively; all remaining modes exist
//! for the lossy path.  Directional modes (D45–D63) fall back to `DcPred`
//! because they are not exercised in the lossless backbone.

// ─────────────────────────────────────────────────────────────────────────────
// IntraMode enumeration
// ─────────────────────────────────────────────────────────────────────────────

/// AV1 intra prediction modes (4×4 block granularity).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum IntraMode {
    DcPred = 0,
    VPred = 1,
    HPred = 2,
    D45Pred = 3,
    D135Pred = 4,
    D117Pred = 5,
    D153Pred = 6,
    D207Pred = 7,
    D63Pred = 8,
    PaethPred = 9,
    SmoothPred = 10,
    SmoothVPred = 11,
    SmoothHPred = 12,
}

// ─────────────────────────────────────────────────────────────────────────────
// Internal helper: DC value computation
// ─────────────────────────────────────────────────────────────────────────────

/// Compute the DC fill value from whichever borders are available.
///
/// If both `top` and `left` are present the average is taken over all 8
/// border pixels; if only one is present the average is taken over its 4
/// pixels; if neither is present 128 is returned.
fn dc_value(top: Option<&[u8]>, left: Option<&[u8]>) -> u8 {
    match (top, left) {
        (None, None) => 128u8,
        (Some(t), None) => {
            let sum: u32 = t[..4].iter().map(|&x| x as u32).sum();
            ((sum + 2) / 4) as u8
        }
        (None, Some(l)) => {
            let sum: u32 = l[..4].iter().map(|&x| x as u32).sum();
            ((sum + 2) / 4) as u8
        }
        (Some(t), Some(l)) => {
            let sum: u32 = t[..4].iter().chain(l[..4].iter()).map(|&x| x as u32).sum();
            ((sum + 4) / 8) as u8
        }
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// DC prediction
// ─────────────────────────────────────────────────────────────────────────────

fn predict_dc(top: Option<&[u8]>, left: Option<&[u8]>) -> [u8; 16] {
    let dc = dc_value(top, left);
    [dc; 16]
}

// ─────────────────────────────────────────────────────────────────────────────
// Vertical prediction (V_PRED)
// ─────────────────────────────────────────────────────────────────────────────

/// Copy the top row downward into all 4 rows.  Falls back to DcPred if no top
/// row is available.
fn predict_v(top: Option<&[u8]>, left: Option<&[u8]>) -> [u8; 16] {
    match top {
        None => predict_dc(None, left),
        Some(t) => {
            let mut out = [0u8; 16];
            for r in 0..4 {
                out[r * 4] = t[0];
                out[r * 4 + 1] = t[1];
                out[r * 4 + 2] = t[2];
                out[r * 4 + 3] = t[3];
            }
            out
        }
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Horizontal prediction (H_PRED)
// ─────────────────────────────────────────────────────────────────────────────

/// Copy the left column rightward into each row.  Falls back to DcPred if no
/// left column is available.
fn predict_h(top: Option<&[u8]>, left: Option<&[u8]>) -> [u8; 16] {
    match left {
        None => predict_dc(top, None),
        Some(l) => {
            let mut out = [0u8; 16];
            for r in 0..4 {
                let pix = l[r];
                out[r * 4] = pix;
                out[r * 4 + 1] = pix;
                out[r * 4 + 2] = pix;
                out[r * 4 + 3] = pix;
            }
            out
        }
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Paeth prediction
// ─────────────────────────────────────────────────────────────────────────────

/// Standard Paeth predictor for one pixel.
///
/// `a` = left neighbour, `b` = top neighbour, `c` = top-left neighbour.
#[inline(always)]
fn paeth_predictor(a: u8, b: u8, c: u8) -> u8 {
    let a = a as i32;
    let b = b as i32;
    let c = c as i32;
    let p = a + b - c;
    let pa = (p - a).abs();
    let pb = (p - b).abs();
    let pc = (p - c).abs();
    if pa <= pb && pa <= pc {
        a as u8
    } else if pb <= pc {
        b as u8
    } else {
        c as u8
    }
}

/// AV1 Paeth / smooth-hybrid intra prediction.
///
/// Requires both top row and left column plus the top-left pixel.  Falls back
/// to DcPred at any image boundary.
fn predict_paeth(top: Option<&[u8]>, left: Option<&[u8]>) -> [u8; 16] {
    match (top, left) {
        (Some(t), Some(l)) => {
            // top-left pixel is t[-1]; in AV1 convention we treat it as the
            // average of t[0] and l[0] when the true (-1,-1) pixel is unknown.
            // For an intra block the top-left corner is typically available as
            // the last pixel of the row above to the left; we approximate it
            // with the average of the two available border pixels.
            let top_left = (t[0] as u32 + l[0] as u32).div_ceil(2) as u8;
            let mut out = [0u8; 16];
            for r in 0..4usize {
                for c in 0..4usize {
                    out[r * 4 + c] = paeth_predictor(l[r], t[c], top_left);
                }
            }
            out
        }
        // Boundary fallback.
        _ => predict_dc(top, left),
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Smooth prediction
// ─────────────────────────────────────────────────────────────────────────────

/// AV1-inspired bilinear smooth prediction.
///
/// Each pixel is a weighted blend of the top border and the bottom-right
/// corner (= left[3], the last left-column pixel):
///
/// ```text
/// result[r][c] = ( top[c] * (4 − r)  +  left[3] * r  +  2 ) / 4
/// ```
///
/// Falls back to DcPred when a required border is absent.
fn predict_smooth(top: Option<&[u8]>, left: Option<&[u8]>) -> [u8; 16] {
    match (top, left) {
        (Some(t), Some(l)) => {
            let bottom_right = l[3] as u32;
            let mut out = [0u8; 16];
            for r in 0..4usize {
                let wr = r as u32;          // weight for bottom-right
                let wt = 4 - wr;            // weight for top
                for c in 0..4usize {
                    let blended = (t[c] as u32 * wt + bottom_right * wr + 2) / 4;
                    out[r * 4 + c] = blended.min(255) as u8;
                }
            }
            out
        }
        _ => predict_dc(top, left),
    }
}

/// Smooth-V prediction: blend vertically between top row and bottom-left corner.
///
/// Each column c uses the top pixel `t[c]` and the bottom-left corner `l[3]`.
/// Falls back to DcPred when a required border is absent.
fn predict_smooth_v(top: Option<&[u8]>, left: Option<&[u8]>) -> [u8; 16] {
    match (top, left) {
        (Some(t), Some(l)) => {
            let bl = l[3] as u32;
            let mut out = [0u8; 16];
            for r in 0..4usize {
                let wr = r as u32;
                let wt = 4 - wr;
                for c in 0..4usize {
                    let blended = (t[c] as u32 * wt + bl * wr + 2) / 4;
                    out[r * 4 + c] = blended.min(255) as u8;
                }
            }
            out
        }
        _ => predict_dc(top, left),
    }
}

/// Smooth-H prediction: blend horizontally between left column and top-right corner.
///
/// Each row r uses the left pixel `l[r]` and the top-right corner `t[3]`.
/// Falls back to DcPred when a required border is absent.
fn predict_smooth_h(top: Option<&[u8]>, left: Option<&[u8]>) -> [u8; 16] {
    match (top, left) {
        (Some(t), Some(l)) => {
            let tr = t[3] as u32;
            let mut out = [0u8; 16];
            for r in 0..4usize {
                let lr = l[r] as u32;
                for c in 0..4usize {
                    let wl = (4 - c) as u32;
                    let wr = c as u32;
                    let blended = (lr * wl + tr * wr + 2) / 4;
                    out[r * 4 + c] = blended.min(255) as u8;
                }
            }
            out
        }
        _ => predict_dc(top, left),
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Public predict_4x4
// ─────────────────────────────────────────────────────────────────────────────

/// Predict a 4×4 block from optional top row and left column.
///
/// # Parameters
/// * `mode`  — Intra prediction mode to apply.
/// * `top`   — Slice of ≥4 pixels from the row above (None at the top image edge).
/// * `left`  — Slice of ≥4 pixels from the column to the left (None at left edge).
///
/// # Returns
/// A 16-element, row-major `[u8; 16]` predicted block.
pub fn predict_4x4(mode: IntraMode, top: Option<&[u8]>, left: Option<&[u8]>) -> [u8; 16] {
    match mode {
        IntraMode::DcPred => predict_dc(top, left),
        IntraMode::VPred => predict_v(top, left),
        IntraMode::HPred => predict_h(top, left),
        IntraMode::PaethPred => predict_paeth(top, left),
        IntraMode::SmoothPred => predict_smooth(top, left),
        IntraMode::SmoothVPred => predict_smooth_v(top, left),
        IntraMode::SmoothHPred => predict_smooth_h(top, left),
        // Directional modes: not exercised in the lossless backbone.
        // Fall back to DcPred for safety.
        IntraMode::D45Pred
        | IntraMode::D135Pred
        | IntraMode::D117Pred
        | IntraMode::D153Pred
        | IntraMode::D207Pred
        | IntraMode::D63Pred => predict_dc(top, left),
    }
}

// ─────────────────────────────────────────────────────────────────────────────
// Tests
// ─────────────────────────────────────────────────────────────────────────────

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

    // ── DcPred ───────────────────────────────────────────────────────────────

    #[test]
    fn dc_pred_no_borders_is_128() {
        let out = predict_4x4(IntraMode::DcPred, None, None);
        assert_eq!(out, [128u8; 16], "DcPred with no borders must fill with 128");
    }

    #[test]
    fn dc_pred_top_only() {
        let top = [200u8, 200, 200, 200];
        let out = predict_4x4(IntraMode::DcPred, Some(&top), None);
        // average of [200;4] = 200
        assert_eq!(out, [200u8; 16]);
    }

    #[test]
    fn dc_pred_left_only() {
        let left = [100u8, 100, 100, 100];
        let out = predict_4x4(IntraMode::DcPred, None, Some(&left));
        assert_eq!(out, [100u8; 16]);
    }

    #[test]
    fn dc_pred_both_borders() {
        let top = [100u8; 4];
        let left = [60u8; 4];
        // sum = 100*4 + 60*4 = 640; average with bias = (640 + 4) / 8 = 80
        let out = predict_4x4(IntraMode::DcPred, Some(&top), Some(&left));
        assert_eq!(out, [80u8; 16]);
    }

    #[test]
    fn dc_pred_rounding() {
        // sum = 7*8 = 56; (56 + 4) / 8 = 7 (no rounding needed)
        let top = [7u8; 4];
        let left = [7u8; 4];
        let out = predict_4x4(IntraMode::DcPred, Some(&top), Some(&left));
        assert_eq!(out[0], 7);
    }

    // ── VPred ────────────────────────────────────────────────────────────────

    #[test]
    fn v_pred_copies_top_row() {
        let top = [10u8, 20, 30, 40];
        let left = [0u8; 4];
        let out = predict_4x4(IntraMode::VPred, Some(&top), Some(&left));
        for r in 0..4 {
            for c in 0..4 {
                assert_eq!(
                    out[r * 4 + c], top[c],
                    "VPred pixel at ({r},{c}) must equal top[{c}]={}", top[c]
                );
            }
        }
    }

    #[test]
    fn v_pred_no_top_falls_back_to_dc() {
        let left = [80u8; 4];
        let dc_out = predict_4x4(IntraMode::DcPred, None, Some(&left));
        let v_out = predict_4x4(IntraMode::VPred, None, Some(&left));
        assert_eq!(dc_out, v_out, "VPred with no top must fall back to DcPred");
    }

    // ── HPred ────────────────────────────────────────────────────────────────

    #[test]
    fn h_pred_copies_left_column() {
        let top = [0u8; 4];
        let left = [50u8, 60, 70, 80];
        let out = predict_4x4(IntraMode::HPred, Some(&top), Some(&left));
        for r in 0..4 {
            for c in 0..4 {
                assert_eq!(
                    out[r * 4 + c], left[r],
                    "HPred pixel at ({r},{c}) must equal left[{r}]={}", left[r]
                );
            }
        }
    }

    #[test]
    fn h_pred_no_left_falls_back_to_dc() {
        let top = [90u8; 4];
        let dc_out = predict_4x4(IntraMode::DcPred, Some(&top), None);
        let h_out = predict_4x4(IntraMode::HPred, Some(&top), None);
        assert_eq!(dc_out, h_out, "HPred with no left must fall back to DcPred");
    }

    // ── PaethPred ────────────────────────────────────────────────────────────

    #[test]
    fn paeth_pred_flat_borders_equals_border_value() {
        let top = [100u8; 4];
        let left = [100u8; 4];
        let out = predict_4x4(IntraMode::PaethPred, Some(&top), Some(&left));
        // With flat borders and top_left = 100, paeth should return 100 everywhere.
        assert_eq!(out, [100u8; 16]);
    }

    #[test]
    fn paeth_pred_no_borders_fallback() {
        let out = predict_4x4(IntraMode::PaethPred, None, None);
        assert_eq!(out, [128u8; 16]);
    }

    #[test]
    fn paeth_pred_values_in_range() {
        let top = [30u8, 60, 90, 120];
        let left = [40u8, 80, 120, 160];
        let out = predict_4x4(IntraMode::PaethPred, Some(&top), Some(&left));
        for &px in &out {
            // Paeth must return one of the three input pixels.
            let _ = px; // px is u8, always ≤ 255; existence check is the invariant
        }
        assert!(!out.is_empty(), "Paeth prediction must produce 16 pixels");
    }

    // ── SmoothPred ───────────────────────────────────────────────────────────

    #[test]
    fn smooth_pred_top_row_is_top_border() {
        // At r=0 the weight for bottom-right is 0, so result must equal top[c].
        let top = [10u8, 20, 30, 40];
        let left = [0u8, 50, 100, 200];
        let out = predict_4x4(IntraMode::SmoothPred, Some(&top), Some(&left));
        for c in 0..4 {
            // r=0: (top[c]*4 + left[3]*0 + 2) / 4 = top[c] (no rounding artefact)
            assert_eq!(out[c], top[c], "smooth row 0 col {c} must equal top[{c}]");
        }
    }

    #[test]
    fn smooth_pred_no_borders_fallback() {
        let out = predict_4x4(IntraMode::SmoothPred, None, None);
        assert_eq!(out, [128u8; 16]);
    }

    // ── General validity ─────────────────────────────────────────────────────

    #[test]
    fn all_modes_produce_valid_pixels() {
        let top = [100u8, 110, 120, 130];
        let left = [90u8, 95, 100, 105];
        let modes = [
            IntraMode::DcPred,
            IntraMode::VPred,
            IntraMode::HPred,
            IntraMode::D45Pred,
            IntraMode::D135Pred,
            IntraMode::D117Pred,
            IntraMode::D153Pred,
            IntraMode::D207Pred,
            IntraMode::D63Pred,
            IntraMode::PaethPred,
            IntraMode::SmoothPred,
            IntraMode::SmoothVPred,
            IntraMode::SmoothHPred,
        ];
        for mode in modes {
            let out = predict_4x4(mode, Some(&top), Some(&left));
            for &px in &out {
                // Pixel values must fit in u8 — the assert on ≤ 255 is trivially true
                // for u8, but we check the cast path is lossless.
                assert!(px as u32 <= 255, "pixel overflow in mode {mode:?}");
            }
        }
    }

    #[test]
    fn all_modes_no_border_do_not_panic() {
        let modes = [
            IntraMode::DcPred,
            IntraMode::VPred,
            IntraMode::HPred,
            IntraMode::D45Pred,
            IntraMode::D135Pred,
            IntraMode::D117Pred,
            IntraMode::D153Pred,
            IntraMode::D207Pred,
            IntraMode::D63Pred,
            IntraMode::PaethPred,
            IntraMode::SmoothPred,
            IntraMode::SmoothVPred,
            IntraMode::SmoothHPred,
        ];
        for mode in modes {
            let _ = predict_4x4(mode, None, None);
        }
    }
}