imgref
imgref copied to clipboard
Interop with the "image" crate
I'm using the image crate to load images, and I was wondering: is there a method to easily and quickly convert a loaded RgbaImage into an Img? Right now I'm doing it manually like this:
pub fn image_to_imgref(img: &RgbaImage) -> Img<Vec<rgb::RGBA<u8>>> {
Img::new(
img.pixels()
.map(|p| rgb::RGBA::new(p.0[0], p.0[1], p.0[2], p.0[3]))
.collect::<Vec<_>>(),
img.width() as usize,
img.height() as usize,
)
}
I was wondering if it is possible to do this in a one-liner. I was also wondering if it was possible to do it the other way around...
I'm not sure. There's into_vec() on ImageBuffer (RgbaImage is an alias of ImageBuffer), which will give you Vec<u8>, but then you need to make it Vec<RGBA>. Rust doesn't have safe methods for that yet.
You could easily borrow the buffer to make Img<&[RGBA]>:
use rgb::FromSlice;
ImgRef::new(img.as_rgba(), img.width(), img.height())