Converting DynamicImage into an ImageBuffer<P, _> #2072
-
Hi there, I'm creating a function that aims to translate a given DynamicImage to an image buffer with type // A simplified version of an Image struct my code uses
pub struct Image<P: Pixel, U: image::GenericImage<Pixel = P>> {
underlying: U,
}
pub trait FromWithFormat<T> {
fn from_with_format(t: T, format: ImageFormat) -> Self;
}
impl<'a, P> FromWithFormat<&'a [u8]> for Image<P, ImageBuffer<P, Vec<P::Subpixel>>>
where
P: Pixel,
{
fn from_with_format(bytes: &'a [u8], format: ImageFormat) -> Self {
let dyn_image = image::load_from_memory_with_format(&bytes, format).unwrap();
let image: ImageBuffer<P, Vec<P::Subpixel>> = todo!(); // Some sort of macro that uses ConvertBuffer
Self { underlying: image }
}
} I looked into making a macro that does this similar to how the crate uses the |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
What are you trying to use your The core problem is that you can pretty much only apply operations that have been defined as required methods on the relevant traits. Which of course requires code changes within the image crate itself. In the case of color conversion, this is four methods on Pixel and a bunch more machinery around conversion between different primitive types. I've so far been hesitant to stabilize and publicly expose the rest of the fully generic color conversion API because it felt very messy and not especially useful without being able to do much other generic image processing. |
Beta Was this translation helpful? Give feedback.
-
Funny, I needed the same thing and just opened #2076. Doesnt allow arbitrary Pixel types but you can add a constraint |
Beta Was this translation helpful? Give feedback.
Hey, thanks for getting back to me. I ended up brute forcing it with macros, which isn't a very elegant solution but it works for now.
Do you have plans to make that part of the API that we discussed public in the future? Thanks 👍