Skip to content

Commit d7ba22f

Browse files
elmarcoCBenoit
authored andcommitted
refactor(graphics): drop now unused yuv/rgb code
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
1 parent 5f1c440 commit d7ba22f

File tree

2 files changed

+4
-228
lines changed

2 files changed

+4
-228
lines changed

crates/ironrdp-graphics/src/color_conversion.rs

Lines changed: 0 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -23,84 +23,6 @@ pub fn ycbcr_to_bgra(input: YCbCrBuffer<'_>, output: &mut [u8]) -> io::Result<()
2323
rdp_yuv444_to_bgra(&planar, output, len).map_err(io::Error::other)
2424
}
2525

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-
10426
#[allow(clippy::too_many_arguments)]
10527
pub fn to_64x64_ycbcr_tile(
10628
input: &[u8],
@@ -147,10 +69,6 @@ pub fn rdp_16bit_to_rgb(color: u16) -> [u8; 3] {
14769
[r, g, b]
14870
}
14971

150-
fn clip(v: i32) -> u8 {
151-
v.clamp(0, 255) as u8
152-
}
153-
15472
#[derive(Debug)]
15573
pub struct YCbCrBuffer<'a> {
15674
pub y: &'a [i16],
@@ -191,79 +109,3 @@ pub struct Rgb {
191109
pub g: u8,
192110
pub b: u8,
193111
}
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-
}

crates/ironrdp-testsuite-core/tests/graphics/color_conversion.rs

Lines changed: 4 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,6 @@ fn to_64x64_ycbcr() {
1111
to_64x64_ycbcr_tile(&input, 1, 1, 4, PixelFormat::ABgr32, &mut y, &mut cb, &mut cr);
1212
}
1313

14-
#[test]
15-
fn ycbcr_from_rgb_works_for_zeros() {
16-
let rgb = Rgb { r: 0, g: 0, b: 0 };
17-
let expected = YCbCr { y: -4096, cb: 0, cr: 0 };
18-
19-
let actual = YCbCr::from(rgb);
20-
assert_eq!(expected, actual);
21-
}
22-
23-
#[test]
24-
fn ycbcr_from_rgb_works_for_max_values() {
25-
let rgb = Rgb { r: 255, g: 255, b: 255 };
26-
let expected = YCbCr { y: 4063, cb: 0, cr: 0 };
27-
28-
let actual = YCbCr::from(rgb);
29-
assert_eq!(expected, actual);
30-
}
31-
3214
#[ignore]
3315
#[test]
3416
fn rgb_to_ycbcr_converts_large_buffer() {
@@ -39,61 +21,13 @@ fn rgb_to_ycbcr_converts_large_buffer() {
3921
cr: YCBCR_BUFFER_CR.as_ref(),
4022
};
4123

42-
let mut y = vec![0; 4096];
43-
let mut cb = vec![0; 4096];
44-
let mut cr = vec![0; 4096];
45-
to_ycbcr(rgba, 64, 64, 64 * 4, PixelFormat::BgrA32, &mut y, &mut cb, &mut cr);
24+
let mut y = [0; 4096];
25+
let mut cb = [0; 4096];
26+
let mut cr = [0; 4096];
27+
to_64x64_ycbcr_tile(rgba, 64, 64, 64 * 4, PixelFormat::BgrA32, &mut y, &mut cb, &mut cr);
4628
assert_eq!(expected.y, y.as_slice());
4729
}
4830

49-
#[test]
50-
fn rgb_from_ycbcr_works_for_zeros() {
51-
let ycbcr = YCbCr { y: 0, cb: 0, cr: 0 };
52-
53-
let expected = Rgb { r: 128, g: 128, b: 128 };
54-
let actual = Rgb::from(ycbcr);
55-
assert_eq!(expected, actual);
56-
}
57-
58-
#[test]
59-
fn rgb_from_ycbcr_works_for_max_values() {
60-
let ycbcr = YCbCr {
61-
y: 32767,
62-
cb: 32767,
63-
cr: 32767,
64-
};
65-
66-
let expected = Rgb { r: 255, g: 68, b: 255 };
67-
let actual = Rgb::from(ycbcr);
68-
assert_eq!(expected, actual);
69-
}
70-
71-
#[test]
72-
fn rgb_from_ycbcr_works_for_min_values() {
73-
let ycbcr = YCbCr {
74-
y: -32768,
75-
cb: -32768,
76-
cr: -32768,
77-
};
78-
79-
let expected = Rgb { r: 0, g: 187, b: 0 };
80-
let actual = Rgb::from(ycbcr);
81-
assert_eq!(expected, actual);
82-
}
83-
84-
#[test]
85-
fn rgb_from_ycbcr_works_for_regular_values() {
86-
let ycbcr = YCbCr {
87-
y: 68,
88-
cb: 1710,
89-
cr: -2128,
90-
};
91-
92-
let expected = Rgb { r: 36, g: 159, b: 224 };
93-
let actual = Rgb::from(ycbcr);
94-
assert_eq!(expected, actual);
95-
}
96-
9731
#[test]
9832
fn ycbcr_to_rgb_converts_one_element_buffer() {
9933
let ycbcr = YCbCrBuffer {

0 commit comments

Comments
 (0)