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
use crate::models::{Color16, Led};

use super::Image;

#[derive(Debug, Default)]
pub struct Reducer {
    spec: Vec<LedSpec>,
    spec_width: u16,
    spec_height: u16,
}

#[derive(Debug)]
struct LedSpec {
    lxmin: u16,
    lxmax: u16,
    lymin: u16,
    lymax: u16,
}

impl LedSpec {
    pub fn new(spec: &Led, width: u16, height: u16, fwidth: f32, fheight: f32) -> Self {
        let lxmin = spec.hmin * fwidth;
        let lxmax = spec.hmax * fwidth;
        let lymin = spec.vmin * fheight;
        let lymax = spec.vmax * fheight;

        Self {
            lxmin: lxmin.floor() as u16,
            lxmax: (lxmax.ceil() as u16).min(width - 1),
            lymin: lymin.floor() as u16,
            lymax: (lymax.ceil() as u16).min(height - 1),
        }
    }
}

impl Reducer {
    pub fn reset(&mut self, width: u16, height: u16, leds: &[Led]) {
        self.spec_width = width;
        self.spec_height = height;

        let fwidth = width as f32;
        let fheight = height as f32;

        self.spec.clear();
        self.spec.reserve(leds.len());

        for spec in leds.iter() {
            self.spec
                .push(LedSpec::new(spec, width, height, fwidth, fheight));
        }
    }

    pub fn reduce(&mut self, image: &impl Image, leds: &[Led], color_data: &mut [Color16]) {
        let width = image.width();
        let height = image.height();

        if self.spec_width != width || self.spec_height != height || self.spec.len() != leds.len() {
            self.reset(width, height, leds);
        }

        for (spec, value) in self.spec.iter().zip(color_data.iter_mut()) {
            let mut r_acc = 0u64;
            let mut g_acc = 0u64;
            let mut b_acc = 0u64;
            let mut cnt = 0u64;

            for y in spec.lymin..=spec.lymax {
                for x in spec.lxmin..=spec.lxmax {
                    // Safety: x (resp. y) are necessarily in 0..width (resp. 0..height)
                    let rgb = unsafe { image.color_at_unchecked(x as _, y as _) };
                    let area = 255;

                    let (r, g, b) = rgb.into_components();
                    r_acc += (r as u64 * 255) * area;
                    g_acc += (g as u64 * 255) * area;
                    b_acc += (b as u64 * 255) * area;
                    cnt += area;
                }
            }

            *value = Color16::new(
                ((r_acc / cnt.max(1)) * 65535 / (255 * 255)).min(u16::MAX as _) as u16,
                ((g_acc / cnt.max(1)) * 65535 / (255 * 255)).min(u16::MAX as _) as u16,
                ((b_acc / cnt.max(1)) * 65535 / (255 * 255)).min(u16::MAX as _) as u16,
            );
        }
    }
}