@@ -23,84 +23,6 @@ pub fn ycbcr_to_bgra(input: YCbCrBuffer<'_>, output: &mut [u8]) -> io::Result<()
23
23
rdp_yuv444_to_bgra ( & planar, output, len) . map_err ( io:: Error :: other)
24
24
}
25
25
26
- fn iter_to_ycbcr < ' a , I , C > ( input : I , y : & mut [ i16 ] , cb : & mut [ i16 ] , cr : & mut [ i16 ] , conv : C )
27
- where
28
- I : ExactSizeIterator < Item = & ' a [ u8 ] > ,
29
- C : Fn ( & [ u8 ] ) -> Rgb ,
30
- {
31
- for ( i, pixel) in input. into_iter ( ) . enumerate ( ) {
32
- let pixel = YCbCr :: from ( conv ( pixel) ) ;
33
-
34
- y[ i] = pixel. y ;
35
- cb[ i] = pixel. cb ;
36
- cr[ i] = pixel. cr ;
37
- }
38
- }
39
-
40
- fn xrgb_to_rgb ( pixel : & [ u8 ] ) -> Rgb {
41
- Rgb {
42
- r : pixel[ 1 ] ,
43
- g : pixel[ 2 ] ,
44
- b : pixel[ 3 ] ,
45
- }
46
- }
47
-
48
- fn xbgr_to_rgb ( pixel : & [ u8 ] ) -> Rgb {
49
- Rgb {
50
- b : pixel[ 1 ] ,
51
- g : pixel[ 2 ] ,
52
- r : pixel[ 3 ] ,
53
- }
54
- }
55
-
56
- fn bgrx_to_rgb ( pixel : & [ u8 ] ) -> Rgb {
57
- Rgb {
58
- b : pixel[ 0 ] ,
59
- g : pixel[ 1 ] ,
60
- r : pixel[ 2 ] ,
61
- }
62
- }
63
-
64
- fn rgbx_to_rgb ( pixel : & [ u8 ] ) -> Rgb {
65
- Rgb {
66
- r : pixel[ 0 ] ,
67
- g : pixel[ 1 ] ,
68
- b : pixel[ 2 ] ,
69
- }
70
- }
71
-
72
- const fn pixel_format_to_rgb_fn ( format : PixelFormat ) -> fn ( & [ u8 ] ) -> Rgb {
73
- match format {
74
- PixelFormat :: ARgb32 | PixelFormat :: XRgb32 => xrgb_to_rgb,
75
- PixelFormat :: ABgr32 | PixelFormat :: XBgr32 => xbgr_to_rgb,
76
- PixelFormat :: BgrA32 | PixelFormat :: BgrX32 => bgrx_to_rgb,
77
- PixelFormat :: RgbA32 | PixelFormat :: RgbX32 => rgbx_to_rgb,
78
- }
79
- }
80
-
81
- #[ allow( clippy:: too_many_arguments) ]
82
- pub fn to_ycbcr (
83
- mut input : & [ u8 ] ,
84
- width : usize ,
85
- height : usize ,
86
- stride : usize ,
87
- format : PixelFormat ,
88
- mut y : & mut [ i16 ] ,
89
- mut cb : & mut [ i16 ] ,
90
- mut cr : & mut [ i16 ] ,
91
- ) {
92
- let to_rgb = pixel_format_to_rgb_fn ( format) ;
93
- let bpp = format. bytes_per_pixel ( ) as usize ;
94
-
95
- for _ in 0 ..height {
96
- iter_to_ycbcr ( input[ ..width * bpp] . chunks_exact ( bpp) , y, cb, cr, to_rgb) ;
97
- input = & input[ stride..] ;
98
- y = & mut y[ width..] ;
99
- cb = & mut cb[ width..] ;
100
- cr = & mut cr[ width..] ;
101
- }
102
- }
103
-
104
26
#[ allow( clippy:: too_many_arguments) ]
105
27
pub fn to_64x64_ycbcr_tile (
106
28
input : & [ u8 ] ,
@@ -147,10 +69,6 @@ pub fn rdp_16bit_to_rgb(color: u16) -> [u8; 3] {
147
69
[ r, g, b]
148
70
}
149
71
150
- fn clip ( v : i32 ) -> u8 {
151
- v. clamp ( 0 , 255 ) as u8
152
- }
153
-
154
72
#[ derive( Debug ) ]
155
73
pub struct YCbCrBuffer < ' a > {
156
74
pub y : & ' a [ i16 ] ,
@@ -191,79 +109,3 @@ pub struct Rgb {
191
109
pub g : u8 ,
192
110
pub b : u8 ,
193
111
}
194
-
195
- impl From < YCbCr > for Rgb {
196
- fn from ( YCbCr { y, cb, cr } : YCbCr ) -> Self {
197
- #![ allow( clippy:: similar_names) ] // It’s hard to find better names here.
198
-
199
- // We scale the factors by << 16 into 32-bit integers in order to
200
- // avoid slower floating point multiplications. Since the final
201
- // result needs to be scaled by >> 5 we will extract only the
202
- // upper 11 bits (>> 21) from the final sum.
203
- // Hence we also have to scale the other terms of the sum by << 16.
204
- const DIVISOR : f32 = ( 1 << 16 ) as f32 ;
205
-
206
- let y = i32:: from ( y) ;
207
- let cb = i32:: from ( cb) ;
208
- let cr = i32:: from ( cr) ;
209
-
210
- let yy = ( y + 4096 ) << 16 ;
211
- let cr_r = cr. overflowing_mul ( ( 1.402_525 * DIVISOR ) as i32 ) . 0 ;
212
- let cb_g = cb. overflowing_mul ( ( 0.343_730 * DIVISOR ) as i32 ) . 0 ;
213
- let cr_g = cr. overflowing_mul ( ( 0.714_401 * DIVISOR ) as i32 ) . 0 ;
214
- let cb_b = cb. overflowing_mul ( ( 1.769_905 * DIVISOR ) as i32 ) . 0 ;
215
- let cr_b = cb. overflowing_mul ( ( 0.000_013 * DIVISOR ) as i32 ) . 0 ;
216
-
217
- let r = clip ( ( yy. overflowing_add ( cr_r) . 0 ) >> 21 ) ;
218
- let g = clip ( ( yy. overflowing_sub ( cb_g) . 0 . overflowing_sub ( cr_g) . 0 ) >> 21 ) ;
219
- let b = clip ( ( yy. overflowing_add ( cb_b) . 0 . overflowing_add ( cr_b) . 0 ) >> 21 ) ;
220
-
221
- Self { r, g, b }
222
- }
223
- }
224
-
225
- impl From < Rgb > for YCbCr {
226
- fn from ( Rgb { r, g, b } : Rgb ) -> Self {
227
- #![ allow( clippy:: similar_names) ] // It’s hard to find better names here.
228
-
229
- // We scale the factors by << 15 into 32-bit integers in order
230
- // to avoid slower floating point multiplications. Since the
231
- // terms need to be scaled by << 5 we simply scale the final
232
- // sum by >> 10
233
- const DIVISOR : f32 = ( 1 << 15 ) as f32 ;
234
- const Y_R : i32 = ( 0.299 * DIVISOR ) as i32 ;
235
- const Y_G : i32 = ( 0.587 * DIVISOR ) as i32 ;
236
- const Y_B : i32 = ( 0.114 * DIVISOR ) as i32 ;
237
- const CB_R : i32 = ( 0.168_935 * DIVISOR ) as i32 ;
238
- const CB_G : i32 = ( 0.331_665 * DIVISOR ) as i32 ;
239
- const CB_B : i32 = ( 0.500_59 * DIVISOR ) as i32 ;
240
- const CR_R : i32 = ( 0.499_813 * DIVISOR ) as i32 ;
241
- const CR_G : i32 = ( 0.418_531 * DIVISOR ) as i32 ;
242
- const CR_B : i32 = ( 0.081_282 * DIVISOR ) as i32 ;
243
-
244
- let r = i32:: from ( r) ;
245
- let g = i32:: from ( g) ;
246
- let b = i32:: from ( b) ;
247
-
248
- let y_r = r. overflowing_mul ( Y_R ) . 0 ;
249
- let y_g = g. overflowing_mul ( Y_G ) . 0 ;
250
- let y_b = b. overflowing_mul ( Y_B ) . 0 ;
251
- let y = y_r. overflowing_add ( y_g) . 0 . overflowing_add ( y_b) . 0 >> 10 ;
252
-
253
- let cb_r = r. overflowing_mul ( CB_R ) . 0 ;
254
- let cb_g = g. overflowing_mul ( CB_G ) . 0 ;
255
- let cb_b = b. overflowing_mul ( CB_B ) . 0 ;
256
- let cb = cb_b. overflowing_sub ( cb_g) . 0 . overflowing_sub ( cb_r) . 0 >> 10 ;
257
-
258
- let cr_r = r. overflowing_mul ( CR_R ) . 0 ;
259
- let cr_g = g. overflowing_mul ( CR_G ) . 0 ;
260
- let cr_b = b. overflowing_mul ( CR_B ) . 0 ;
261
- let cr = cr_r. overflowing_sub ( cr_g) . 0 . overflowing_sub ( cr_b) . 0 >> 10 ;
262
-
263
- Self {
264
- y : ( y - 4096 ) . clamp ( -4096 , 4095 ) as i16 ,
265
- cb : cb. clamp ( -4096 , 4095 ) as i16 ,
266
- cr : cr. clamp ( -4096 , 4095 ) as i16 ,
267
- }
268
- }
269
- }
0 commit comments