How to ensure that an image::ImageBuffer implements image::GenericImage?


#1

I am working on drawing lines on an imagebuffer using the imageproc crate. Drawing a line requires the trait bound “GenericImage” from the image crate, which makes sense.

I am using an ImageBuffer<Rgb<u8>, Vec<u8>>, which satisfies the where clause in the docs, so GenericImage should be implemented. But I think I am misunderstanding, because I am getting the error:

 draw_line_segment(&rendered_strokes_buffer, (stroke.start.x as f32, stroke.start.y as f32), (stroke.end.x as f32, stroke.end.y as f32), stroke.color);
   |             ^^^^^^^^^^^^^^^^^ the trait `image::image::GenericImage` is not implemented for `image::ImageBuffer<image::Rgb<u8>, std::vec::Vec<u8>>`

How would I implement this trait? I saw someone recommend trying to “re-borrow”, via &*, but it didn’t work and I don’t really understand that approach anyway.

Thanks for reading!


#2

It is implemented when I tried it. Can you reproduce the error you are seeing in a short, self-contained, compilable example?

Here is what I tried (playground):

extern crate image;
use image::{GenericImage, ImageBuffer, Rgb};

fn main() {
    fn assert_generic_image<T: GenericImage>() {}
    assert_generic_image::<ImageBuffer<Rgb<u8>, Vec<u8>>>();
}

One possible explanation is you are using crates that have dependencies on different versions of image, so image 0.15’s GenericImage is not implemented for image 0.16’s ImageBuffer or vice versa. Check cargo tree -d to track that down.


#3

Unfortunately, I cannot create a short example because the crate I am using, imageproc, is not in the playpen.

However, I did just check Cargo.lock and it turns out that I am indeed using both version 0.15 and version 0.16 of image. The issue is now fixed! Is there any way for cargo to throw an error or warning if I accidentally am using two versions of the same crate?


#4

This is tracked in rust-lang/rust#22750.