Search code examples
imagerustrust-piston

function or associated item not found for image::ImageBuffer::from_vec()


I'm using Piston's image crate, with this code:

use image::{Rgb, ImageBuffer, Pixel};

let image = Vec::<Rgb<u8>>::new();

let image_buffer = ImageBuffer::<Rgb<u8>, Vec<Rgb<u8>>>::from_vec(
    width, height
    image,
).unwrap();

However I get this error:

error[E0599]: no function or associated item named `from_vec` found for type `image::ImageBuffer<image::Rgb<u8>, std::vec::Vec<image::Rgb<u8>>>` in the current scope
   --> src/main.rs:348:21
    |
348 |     let image_buffer = ImageBuffer::<Rgb<u8>, Vec<Rgb<u8>>>::from_vec(
    |                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ function or associated item not found in `image::ImageBuffer<image::Rgb<u8>, std::vec::Vec<image::Rgb<u8>>>`

I can't work out why. It's clearly in the documentation, and the types seem right as far as I can tell.


Solution

  • Expanding a bit: In the example above, we have a ImageBuffer::<Rgb<u8>, Vec<Rgb<u8>>. And ImageBuffer provides two implementations of from_vec, depending on its type parameters:

    impl<P, Container> ImageBuffer<P, Container>
    where
        P: Pixel<Subpixel = u8> + 'static,
        Container: Deref<Target = [u8]>, 
    
    impl<P: Pixel + 'static> ImageBuffer<P, Vec<P::Subpixel>>
    where
        P::Subpixel: 'static, 
    

    Neither of these worked here because the Container parameter type in ImageBuffer<Rgb<u8>, Vec<Rgb<u8>> is a vector of Rgb<u8> values. It will dereference to a slice of [Rgb<u8>], making it incompatible with the first implementation, and the second one expects a vector of subpixel values (<P as Pixel>::Subpixel) rather than actual pixel values (Rgb<u8>). This is generally what the ImageBuffer type in this crate expects as its pixel data container.

    Working example:

    extern crate image;
    
    use image::{ImageBuffer, Pixel, Rgb};
    
    fn main() {
        let width = 64;
        let height = 64;
        let image = vec![0x7F_u8; width as usize * height as usize * 3];
    
        let image_buffer =
            ImageBuffer::<Rgb<u8>, Vec<u8>>::from_vec(width, height, image).unwrap();
    }
    

    Playground