oximedia-virtual 0.1.8

Virtual production and LED wall tools for OxiMedia
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
//! Pixel mapping for LED walls in virtual production.
//!
//! Provides tools for mapping rendered pixels to physical LED panel coordinates,
//! including HDR remapping, gamma correction, and per-panel calibration offsets.

#![allow(dead_code)]
#![allow(clippy::cast_precision_loss)]

/// An RGB pixel with floating-point components in linear light [0.0, ∞).
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct LinearPixel {
    /// Red channel.
    pub r: f32,
    /// Green channel.
    pub g: f32,
    /// Blue channel.
    pub b: f32,
}

impl LinearPixel {
    /// Create a new linear pixel.
    #[must_use]
    pub const fn new(r: f32, g: f32, b: f32) -> Self {
        Self { r, g, b }
    }

    /// Clamp all channels to [0.0, `max_nits`].
    #[must_use]
    pub fn clamp_nits(self, max_nits: f32) -> Self {
        Self {
            r: self.r.clamp(0.0, max_nits),
            g: self.g.clamp(0.0, max_nits),
            b: self.b.clamp(0.0, max_nits),
        }
    }

    /// Scale all channels by a factor.
    #[must_use]
    pub fn scale(self, factor: f32) -> Self {
        Self {
            r: self.r * factor,
            g: self.g * factor,
            b: self.b * factor,
        }
    }

    /// Convert to 8-bit sRGB (gamma-corrected) output.
    #[must_use]
    pub fn to_srgb8(self) -> [u8; 3] {
        let apply_gamma = |c: f32| -> u8 {
            let clamped = c.clamp(0.0, 1.0);
            let gamma = if clamped <= 0.003_130_8 {
                clamped * 12.92
            } else {
                1.055 * clamped.powf(1.0 / 2.4) - 0.055
            };
            (gamma * 255.0 + 0.5) as u8
        };
        [
            apply_gamma(self.r),
            apply_gamma(self.g),
            apply_gamma(self.b),
        ]
    }

    /// Convert to 10-bit output (values 0–1023).
    #[must_use]
    pub fn to_10bit(self) -> [u16; 3] {
        let conv = |c: f32| -> u16 { (c.clamp(0.0, 1.0) * 1023.0 + 0.5) as u16 };
        [conv(self.r), conv(self.g), conv(self.b)]
    }
}

/// A per-panel calibration offset that adjusts pixel values.
#[derive(Debug, Clone, Copy)]
pub struct PanelCalibrationOffset {
    /// Additive gain offset for red [−1.0, 1.0].
    pub r_gain: f32,
    /// Additive gain offset for green [−1.0, 1.0].
    pub g_gain: f32,
    /// Additive gain offset for blue [−1.0, 1.0].
    pub b_gain: f32,
    /// Brightness multiplier (1.0 = no change).
    pub brightness: f32,
}

impl PanelCalibrationOffset {
    /// Create a new calibration offset.
    #[must_use]
    pub fn new(r_gain: f32, g_gain: f32, b_gain: f32, brightness: f32) -> Self {
        Self {
            r_gain,
            g_gain,
            b_gain,
            brightness: brightness.max(0.0),
        }
    }

    /// Identity calibration (no change).
    #[must_use]
    pub fn identity() -> Self {
        Self::new(0.0, 0.0, 0.0, 1.0)
    }

    /// Apply calibration to a pixel.
    #[must_use]
    pub fn apply(&self, pixel: LinearPixel) -> LinearPixel {
        LinearPixel {
            r: ((pixel.r + self.r_gain) * self.brightness).max(0.0),
            g: ((pixel.g + self.g_gain) * self.brightness).max(0.0),
            b: ((pixel.b + self.b_gain) * self.brightness).max(0.0),
        }
    }
}

impl Default for PanelCalibrationOffset {
    fn default() -> Self {
        Self::identity()
    }
}

/// HDR tone-mapping mode for mapping scene-linear HDR to LED output range.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ToneMappingMode {
    /// No tone mapping — clip values above peak nits.
    Clip,
    /// Simple linear scale to fit peak nits.
    #[default]
    LinearScale,
    /// Reinhard global tone mapping.
    Reinhard,
    /// ACES filmic tone mapping approximation.
    AcesFilmic,
}

/// Applies HDR tone mapping from scene-linear input to LED output range.
#[allow(dead_code)]
pub struct HdrToneMapper {
    /// Peak nits of the LED wall.
    peak_nits: f32,
    /// Tone mapping mode.
    mode: ToneMappingMode,
}

impl HdrToneMapper {
    /// Create a new tone mapper.
    #[must_use]
    pub fn new(peak_nits: f32, mode: ToneMappingMode) -> Self {
        Self { peak_nits, mode }
    }

    /// Map a scene-linear pixel to LED output range [0.0, 1.0].
    #[must_use]
    pub fn map(&self, pixel: LinearPixel) -> LinearPixel {
        match self.mode {
            ToneMappingMode::Clip => pixel.clamp_nits(self.peak_nits).scale(1.0 / self.peak_nits),
            ToneMappingMode::LinearScale => pixel.scale(1.0 / self.peak_nits).clamp_nits(1.0),
            ToneMappingMode::Reinhard => self.reinhard(pixel),
            ToneMappingMode::AcesFilmic => self.aces(pixel),
        }
    }

    fn reinhard(&self, pixel: LinearPixel) -> LinearPixel {
        let scale = 1.0 / self.peak_nits;
        let map = |c: f32| -> f32 {
            let x = c * scale;
            x / (1.0 + x)
        };
        LinearPixel::new(map(pixel.r), map(pixel.g), map(pixel.b))
    }

    fn aces(&self, pixel: LinearPixel) -> LinearPixel {
        // Narkowicz 2015 ACES approximation
        let scale = 1.0 / self.peak_nits;
        let map = |c: f32| -> f32 {
            let x = c * scale;
            let a = 2.51_f32;
            let b = 0.03_f32;
            let c_const = 2.43_f32;
            let d = 0.59_f32;
            let e = 0.14_f32;
            ((x * (a * x + b)) / (x * (c_const * x + d) + e)).clamp(0.0, 1.0)
        };
        LinearPixel::new(map(pixel.r), map(pixel.g), map(pixel.b))
    }

    /// Peak nits configured for this mapper.
    #[must_use]
    pub fn peak_nits(&self) -> f32 {
        self.peak_nits
    }

    /// Tone mapping mode.
    #[must_use]
    pub fn mode(&self) -> ToneMappingMode {
        self.mode
    }
}

/// Precomputed lookup table mapping every global pixel coordinate to its
/// (panel_idx, panel_col, panel_row) triple.
///
/// When the total pixel count fits within `memory_cap_pixels`, the LUT is
/// built eagerly so that subsequent lookups are O(1) array access.  For
/// very large walls it returns `None` from `build`, and the caller should
/// fall back to arithmetic (`PixelMapper::global_to_panel`).
pub struct GlobalPixelLut {
    /// Flat Vec of (panel_idx, panel_col, panel_row) — row-major order.
    entries: Vec<(u32, u32, u32)>,
    frame_width: u32,
    frame_height: u32,
    /// Maximum pixels before refusing to allocate.
    memory_cap_pixels: usize,
}

/// Default memory cap: 32 megapixels (≈ 384 MB at 12 bytes/entry).
pub const DEFAULT_MEMORY_CAP: usize = 32 * 1024 * 1024;

/// Minimal description of a panel needed to build the LUT.
#[derive(Debug, Clone, Copy)]
pub struct PanelDesc {
    /// Horizontal pixel offset of this panel within the global frame.
    pub x_offset: u32,
    /// Vertical pixel offset of this panel within the global frame.
    pub y_offset: u32,
    /// Width of this panel in pixels.
    pub width: u32,
    /// Height of this panel in pixels.
    pub height: u32,
}

impl GlobalPixelLut {
    /// Build the LUT from a slice of panels and the overall frame dimensions.
    ///
    /// Returns `None` when total pixel count exceeds `DEFAULT_MEMORY_CAP`.
    #[must_use]
    pub fn build(panels: &[PanelDesc], frame_width: u32, frame_height: u32) -> Option<Self> {
        Self::build_with_cap(panels, frame_width, frame_height, DEFAULT_MEMORY_CAP)
    }

    /// Build with an explicit memory cap.
    ///
    /// Returns `None` when total pixel count exceeds `memory_cap_pixels`.
    #[must_use]
    pub fn build_with_cap(
        panels: &[PanelDesc],
        frame_width: u32,
        frame_height: u32,
        memory_cap_pixels: usize,
    ) -> Option<Self> {
        let total = frame_width as usize * frame_height as usize;
        if total > memory_cap_pixels {
            return None;
        }

        // Fill entries with a sentinel meaning "no panel" (u32::MAX).
        let sentinel = (u32::MAX, u32::MAX, u32::MAX);
        let mut entries = vec![sentinel; total];

        for (idx, panel) in panels.iter().enumerate() {
            let panel_idx = idx as u32;
            for row in 0..panel.height {
                for col in 0..panel.width {
                    let gx = panel.x_offset + col;
                    let gy = panel.y_offset + row;
                    if gx < frame_width && gy < frame_height {
                        let gi = gy as usize * frame_width as usize + gx as usize;
                        entries[gi] = (panel_idx, col, row);
                    }
                }
            }
        }

        Some(Self {
            entries,
            frame_width,
            frame_height,
            memory_cap_pixels,
        })
    }

    /// Look up a global pixel coordinate.
    ///
    /// Returns `None` when the coordinate is out of range or lies in an
    /// uncovered region between panels.
    #[must_use]
    pub fn lookup(&self, global_x: u32, global_y: u32) -> Option<(u32, u32, u32)> {
        if global_x >= self.frame_width || global_y >= self.frame_height {
            return None;
        }
        let idx = global_y as usize * self.frame_width as usize + global_x as usize;
        let entry = self.entries[idx];
        if entry.0 == u32::MAX {
            None
        } else {
            Some(entry)
        }
    }

    /// Frame width this LUT was built for.
    #[must_use]
    pub fn frame_width(&self) -> u32 {
        self.frame_width
    }

    /// Frame height this LUT was built for.
    #[must_use]
    pub fn frame_height(&self) -> u32 {
        self.frame_height
    }

    /// Memory cap (pixels) used to gate construction.
    #[must_use]
    pub fn memory_cap_pixels(&self) -> usize {
        self.memory_cap_pixels
    }

    /// Number of entries in the LUT (frame_width × frame_height).
    #[must_use]
    pub fn entry_count(&self) -> usize {
        self.entries.len()
    }
}

/// Maps a 2D buffer of pixels to panel-local pixel buffers.
///
/// When the wall fits within `DEFAULT_MEMORY_CAP` pixels, a `GlobalPixelLut`
/// is built at construction time and used for O(1) lookups.  For larger walls
/// the arithmetic path is used instead.
pub struct PixelMapper {
    /// Total wall width in pixels.
    wall_width: u32,
    /// Total wall height in pixels.
    wall_height: u32,
    /// Width of each panel tile.
    tile_width: u32,
    /// Height of each panel tile.
    tile_height: u32,
    /// Optional precomputed LUT (available when wall fits in memory cap).
    lut: Option<GlobalPixelLut>,
}

impl PixelMapper {
    /// Create a new pixel mapper.
    ///
    /// A `GlobalPixelLut` is built automatically when the total pixel count
    /// is within `DEFAULT_MEMORY_CAP`.  For larger walls the LUT is omitted
    /// and arithmetic is used instead.
    #[must_use]
    pub fn new(wall_width: u32, wall_height: u32, tile_width: u32, tile_height: u32) -> Self {
        let lut = if tile_width > 0 && tile_height > 0 {
            // Build a uniform-tile panel descriptor list.
            let cols = wall_width.div_ceil(tile_width);
            let rows = wall_height.div_ceil(tile_height);
            let mut panels: Vec<PanelDesc> = Vec::with_capacity((cols * rows) as usize);
            for row in 0..rows {
                for col in 0..cols {
                    let x_offset = col * tile_width;
                    let y_offset = row * tile_height;
                    let w = tile_width.min(wall_width.saturating_sub(x_offset));
                    let h = tile_height.min(wall_height.saturating_sub(y_offset));
                    panels.push(PanelDesc {
                        x_offset,
                        y_offset,
                        width: w,
                        height: h,
                    });
                }
            }
            GlobalPixelLut::build(&panels, wall_width, wall_height)
        } else {
            None
        };

        Self {
            wall_width,
            wall_height,
            tile_width,
            tile_height,
            lut,
        }
    }

    /// Number of panel columns.
    #[must_use]
    pub fn panel_cols(&self) -> u32 {
        if self.tile_width == 0 {
            return 0;
        }
        self.wall_width.div_ceil(self.tile_width)
    }

    /// Number of panel rows.
    #[must_use]
    pub fn panel_rows(&self) -> u32 {
        if self.tile_height == 0 {
            return 0;
        }
        self.wall_height.div_ceil(self.tile_height)
    }

    /// Convert a global pixel coordinate to (col, row, `local_x`, `local_y`).
    ///
    /// Uses the precomputed LUT when available, otherwise falls back to
    /// arithmetic division.
    #[must_use]
    pub fn global_to_panel(&self, gx: u32, gy: u32) -> Option<(u32, u32, u32, u32)> {
        if self.tile_width == 0 || self.tile_height == 0 {
            return None;
        }
        if gx >= self.wall_width || gy >= self.wall_height {
            return None;
        }

        // Fast path: LUT available.
        if let Some(ref lut) = self.lut {
            // LUT entry is (panel_idx_in_col_row_order, panel_col, panel_row).
            // panel_idx = row * num_cols + col  →  col = idx % num_cols
            if let Some((pidx, local_x, local_y)) = lut.lookup(gx, gy) {
                let num_cols = self.panel_cols();
                let col = pidx % num_cols;
                let row = pidx / num_cols;
                return Some((col, row, local_x, local_y));
            }
            return None;
        }

        // Arithmetic fallback.
        let col = gx / self.tile_width;
        let row = gy / self.tile_height;
        let local_x = gx % self.tile_width;
        let local_y = gy % self.tile_height;
        Some((col, row, local_x, local_y))
    }

    /// Convert (col, row, `local_x`, `local_y`) back to global coordinates.
    #[must_use]
    pub fn panel_to_global(&self, col: u32, row: u32, local_x: u32, local_y: u32) -> (u32, u32) {
        (
            col * self.tile_width + local_x,
            row * self.tile_height + local_y,
        )
    }

    /// Wall resolution.
    #[must_use]
    pub fn resolution(&self) -> (u32, u32) {
        (self.wall_width, self.wall_height)
    }

    /// Returns a reference to the precomputed LUT, if available.
    #[must_use]
    pub fn lut(&self) -> Option<&GlobalPixelLut> {
        self.lut.as_ref()
    }
}

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

    #[test]
    fn test_linear_pixel_clamp_nits() {
        let px = LinearPixel::new(2000.0, 500.0, -10.0);
        let clamped = px.clamp_nits(1000.0);
        assert_eq!(clamped.r, 1000.0);
        assert_eq!(clamped.g, 500.0);
        assert_eq!(clamped.b, 0.0);
    }

    #[test]
    fn test_linear_pixel_scale() {
        let px = LinearPixel::new(1.0, 0.5, 0.25);
        let scaled = px.scale(2.0);
        assert!((scaled.r - 2.0).abs() < 1e-6);
        assert!((scaled.g - 1.0).abs() < 1e-6);
    }

    #[test]
    fn test_linear_pixel_to_srgb8_black() {
        let px = LinearPixel::new(0.0, 0.0, 0.0);
        assert_eq!(px.to_srgb8(), [0, 0, 0]);
    }

    #[test]
    fn test_linear_pixel_to_srgb8_white() {
        let px = LinearPixel::new(1.0, 1.0, 1.0);
        assert_eq!(px.to_srgb8(), [255, 255, 255]);
    }

    #[test]
    fn test_linear_pixel_to_10bit_white() {
        let px = LinearPixel::new(1.0, 1.0, 1.0);
        assert_eq!(px.to_10bit(), [1023, 1023, 1023]);
    }

    #[test]
    fn test_linear_pixel_to_10bit_black() {
        let px = LinearPixel::new(0.0, 0.0, 0.0);
        assert_eq!(px.to_10bit(), [0, 0, 0]);
    }

    #[test]
    fn test_calibration_identity() {
        let cal = PanelCalibrationOffset::identity();
        let px = LinearPixel::new(0.5, 0.5, 0.5);
        let out = cal.apply(px);
        assert!((out.r - 0.5).abs() < 1e-6);
    }

    #[test]
    fn test_calibration_brightness() {
        let cal = PanelCalibrationOffset::new(0.0, 0.0, 0.0, 0.5);
        let px = LinearPixel::new(1.0, 1.0, 1.0);
        let out = cal.apply(px);
        assert!((out.r - 0.5).abs() < 1e-6);
    }

    #[test]
    fn test_calibration_gain_offset() {
        let cal = PanelCalibrationOffset::new(0.1, -0.1, 0.0, 1.0);
        let px = LinearPixel::new(0.5, 0.5, 0.5);
        let out = cal.apply(px);
        assert!((out.r - 0.6).abs() < 1e-5);
        assert!((out.g - 0.4).abs() < 1e-5);
    }

    #[test]
    fn test_calibration_no_negative_output() {
        let cal = PanelCalibrationOffset::new(-1.0, -1.0, -1.0, 1.0);
        let px = LinearPixel::new(0.0, 0.0, 0.0);
        let out = cal.apply(px);
        assert!(out.r >= 0.0);
        assert!(out.g >= 0.0);
        assert!(out.b >= 0.0);
    }

    #[test]
    fn test_tone_mapping_clip() {
        let mapper = HdrToneMapper::new(1000.0, ToneMappingMode::Clip);
        let px = LinearPixel::new(500.0, 1500.0, 0.0);
        let out = mapper.map(px);
        assert!((out.r - 0.5).abs() < 1e-5);
        assert!((out.g - 1.0).abs() < 1e-5);
    }

    #[test]
    fn test_tone_mapping_linear_scale() {
        let mapper = HdrToneMapper::new(1000.0, ToneMappingMode::LinearScale);
        let px = LinearPixel::new(500.0, 1000.0, 0.0);
        let out = mapper.map(px);
        assert!((out.r - 0.5).abs() < 1e-5);
        assert!((out.g - 1.0).abs() < 1e-5);
    }

    #[test]
    fn test_tone_mapping_reinhard_positive() {
        let mapper = HdrToneMapper::new(1000.0, ToneMappingMode::Reinhard);
        let px = LinearPixel::new(500.0, 500.0, 500.0);
        let out = mapper.map(px);
        // Reinhard: x/(1+x) where x = 500/1000 = 0.5 → 0.5/1.5 ≈ 0.333
        assert!(out.r > 0.0 && out.r < 1.0);
    }

    #[test]
    fn test_tone_mapping_aces_positive() {
        let mapper = HdrToneMapper::new(1000.0, ToneMappingMode::AcesFilmic);
        let px = LinearPixel::new(100.0, 500.0, 1000.0);
        let out = mapper.map(px);
        assert!(out.r >= 0.0 && out.r <= 1.0);
        assert!(out.g >= 0.0 && out.g <= 1.0);
        assert!(out.b >= 0.0 && out.b <= 1.0);
    }

    #[test]
    fn test_pixel_mapper_panel_cols_rows() {
        let mapper = PixelMapper::new(1920, 1080, 256, 128);
        // ceil(1920/256) = 8, ceil(1080/128) = 9 (since 1080 = 8*128 + 56)
        assert_eq!(mapper.panel_cols(), 8);
        assert_eq!(mapper.panel_rows(), 9);
    }

    #[test]
    fn test_pixel_mapper_global_to_panel() {
        let mapper = PixelMapper::new(512, 256, 256, 128);
        let result = mapper.global_to_panel(300, 150);
        assert!(result.is_some());
        let (col, row, lx, ly) = result.expect("should succeed in test");
        assert_eq!(col, 1);
        assert_eq!(row, 1);
        assert_eq!(lx, 300 - 256);
        assert_eq!(ly, 150 - 128);
    }

    #[test]
    fn test_pixel_mapper_global_to_panel_out_of_bounds() {
        let mapper = PixelMapper::new(512, 256, 256, 128);
        assert!(mapper.global_to_panel(512, 0).is_none());
        assert!(mapper.global_to_panel(0, 256).is_none());
    }

    #[test]
    fn test_pixel_mapper_panel_to_global_roundtrip() {
        let mapper = PixelMapper::new(1024, 512, 256, 128);
        let (gx, gy) = mapper.panel_to_global(2, 1, 50, 30);
        let result = mapper.global_to_panel(gx, gy);
        assert!(result.is_some());
        let (col, row, lx, ly) = result.expect("should succeed in test");
        assert_eq!((col, row, lx, ly), (2, 1, 50, 30));
    }
}