I'm capturing the screen using Windows DXGI Desktop Duplication API through a library and the pic_data contains raw pixel data of a frame represented in a vector of u8 BGRA format. I realized that my screen capture was slow due to the time it takes to convert pixel data to the correct color type. I was wondering how I could improve the following code to reduce the time it takes to complete the conversion operation.
Time taken for RGB conversion: 337.2225ms
let rgb_data: Vec<u8> = pic_data
.as_bgra()
.to_owned()
.iter()
.copied()
.flat_map(|pixel| [pixel.r, pixel.g, pixel.b])
.collect();
Time taken for RGB conversion: 113.4304ms
for rgba in pic_data.chunks(4) {
rgb_data.push(rgba[2]); // Red
rgb_data.push(rgba[1]); // Green
rgb_data.push(rgba[0]); // Blue
}