1
1
use crate :: {
2
2
color:: { color_to16, ChannelAdjustments , ChannelAdjustmentsBuilder } ,
3
- image:: prelude:: * ,
3
+ image:: { prelude:: * , Reducer } ,
4
4
models:: { Color , Color16 , InstanceConfig , Leds } ,
5
5
} ;
6
6
@@ -16,6 +16,7 @@ pub struct Core {
16
16
channel_adjustments : ChannelAdjustments ,
17
17
smoothing : Smoothing ,
18
18
notified_inconsistent_led_data : bool ,
19
+ reducer : Reducer ,
19
20
}
20
21
21
22
impl Core {
@@ -35,6 +36,7 @@ impl Core {
35
36
channel_adjustments,
36
37
smoothing,
37
38
notified_inconsistent_led_data : false ,
39
+ reducer : Default :: default ( ) ,
38
40
}
39
41
}
40
42
@@ -47,62 +49,15 @@ impl Core {
47
49
self . black_border_detector . process ( image) ;
48
50
let black_border = self . black_border_detector . current_border ( ) ;
49
51
50
- // Update the 16-bit color data from the LED ranges and the image
52
+ // Crop the image using a view
51
53
let image = {
52
54
let ( x, y) = black_border. get_ranges ( image. width ( ) , image. height ( ) ) ;
53
55
image. wrap ( x, y)
54
56
} ;
55
57
56
- let width = image. width ( ) as f32 ;
57
- let height = image. height ( ) as f32 ;
58
- for ( spec, value) in self . leds . leds . iter ( ) . zip ( self . color_data . iter_mut ( ) ) {
59
- let mut r_acc = 0u64 ;
60
- let mut g_acc = 0u64 ;
61
- let mut b_acc = 0u64 ;
62
- let mut cnt = 0u64 ;
63
-
64
- // TODO: Fixed point arithmetic
65
- let lxmin = spec. hmin * width;
66
- let lxmax = spec. hmax * width;
67
- let lymin = spec. vmin * height;
68
- let lymax = spec. vmax * height;
69
-
70
- for y in lymin. floor ( ) as u32 ..=( lymax. ceil ( ) as u32 ) . min ( image. height ( ) - 1 ) {
71
- let y_area = if ( y as f32 ) < lymin {
72
- ( 255. * ( 1. - lymin. fract ( ) ) ) as u64
73
- } else if ( y + 1 ) as f32 > lymax {
74
- ( 255. * lymax. fract ( ) ) as u64
75
- } else {
76
- 255
77
- } ;
78
-
79
- for x in lxmin. floor ( ) as u32 ..=( lxmax. ceil ( ) as u32 ) . min ( image. width ( ) - 1 ) {
80
- if let Some ( rgb) = image. color_at ( x, y) {
81
- let x_area = if ( x as f32 ) < lxmin {
82
- ( 255. * ( 1. - lxmin. fract ( ) ) ) as u64
83
- } else if ( x + 1 ) as f32 > lxmax {
84
- ( 255. * lxmax. fract ( ) ) as u64
85
- } else {
86
- 255
87
- } ;
88
-
89
- let area = x_area * y_area / 255 ;
90
-
91
- let ( r, g, b) = rgb. into_components ( ) ;
92
- r_acc += ( r as u64 * 255 ) * area;
93
- g_acc += ( g as u64 * 255 ) * area;
94
- b_acc += ( b as u64 * 255 ) * area;
95
- cnt += area;
96
- }
97
- }
98
- }
99
-
100
- * value = Color16 :: new (
101
- ( r_acc / cnt. max ( 1 ) ) . max ( 0 ) . min ( u16:: MAX as _ ) as u16 ,
102
- ( g_acc / cnt. max ( 1 ) ) . max ( 0 ) . min ( u16:: MAX as _ ) as u16 ,
103
- ( b_acc / cnt. max ( 1 ) ) . max ( 0 ) . min ( u16:: MAX as _ ) as u16 ,
104
- ) ;
105
- }
58
+ // Update the 16-bit color data from the LED ranges and the image
59
+ self . reducer
60
+ . reduce ( & image, & self . leds . leds [ ..] , & mut self . color_data ) ;
106
61
}
107
62
108
63
fn handle_led_colors ( & mut self , led_colors : & [ Color ] ) {
0 commit comments