-
|
I want to convert a fn convert(mut img: image::DynamicImage) -> Vec<u8> {
img.apply_color_space(
image::metadata::Cicp::SRGB, Default::default()
).unwrap();
let mut img = img.into_rgba8().into_vec();
for pixel in img.as_chunks_mut::<4>() {
let [r, g, b, a] = *pixel;
*pixel = [b, g, r, 0];
}
img
}However, in worst case it does three separate passes through the image. I wonder if there is a faster way of achieving what I want. Should I use some kind of As added difficulty, ideally I’d also like to avoid memory allocation if the buffer already uses four bytes per pixel. Even if the image uses To give more context as to what I’m doing, I’m converting an image into one which X display server understands. X defines pixel colour as red, green and blue masks though I’ve limited myself to support 8-bit per channel with bit shifts. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
|
Currently you'll want to use use image::{DynamicImage, RgbaImage};
fn convert(src: &DynamicImage, buffer: RgbaImage) -> Vec<u8> {
let mut target = DynamicImage::ImageRgba8(buffer);
target.set_color_space(image::metadata::Cicp::SRGB);
target.copy_from_color_space(src, Default::default()).unwrap();
// Note: we know this does not reallocate, it's `Rgba8`.
let mut img = target.into_rgba8().into_vec();
for pixel in img.as_chunks_mut::<4>().0 {
let [r, g, b, a] = *pixel;
*pixel = [b, g, r, 0];
}
img
}You can't fully avoid memory allocations with the You may have some luck in Notable the above does not address endianess concerns, if your target buffer can be |
Beta Was this translation helpful? Give feedback.
Currently you'll want to use
DynamicImage::copy_from_color_space. It does not change the color representation so you can construct aDynamicImageto be used as a target from an existing buffer, transform in a way that changes both color space and channel representation, and then take back the original buffer.