Newbie question here - I have a simple function that takes a grey-scale image (I'm using the image
crate) and returns a resized grey-scale image.
fn shrink_image(img: image::GrayImage, block_size: u32) -> image::GrayImage {
let (width, height) = img.dimensions();
let (new_width, new_height) = (width / block_size, height / block_size);
resize(&img, new_width, new_height, FilterType::Lanczos3)
}
This works fine in my program, but it doesn't really have to only be working on grey-scale images, does it? So I wanted to make it more general so that it could take any image.
By looking at the docs for image
I figured that image::GreyImage
is just a type synonym for ImageBuffer<Luma<u8>, Vec<u8>>
and that, in turn, ImageBuffer
implements the GenericImageView
trait, which is where the dimensions
and resize
methods I used in the function are coming from.
So I tried modifying the function so it takes and return an object of type T
that implements GenericImageView
:
fn shrink_image<T: GenericImageView>(img: T, block_size: u32) -> T {
let (width, height) = img.dimensions();
let (new_width, new_height) = (width / block_size, height / block_size);
resize(&img, new_width, new_height, FilterType::Lanczos3)
}
But I get this compile error
error[E0308]: mismatched types
--> src/lib.rs:46:5
|
42 | fn shrink_image<T: GenericImageView>(img: T, block_size: u32, squeeze: u8) -> T {
| - expected this type parameter - expected `T` because of return type
...
46 | / resize(&img, new_width * squeeze as u32, new_height, FilterType::Lanczos3,
47 | | )
| |_____^ expected type parameter `T`, found `ImageBuffer<<... as GenericImageView>::Pixel, ...>`
|
= note: expected type parameter `T`
found struct `ImageBuffer<<T as GenericImageView>::Pixel, Vec<<<T as GenericImageView>::Pixel as Pixel>::Subpixel>>`
For more information about this error, try `rustc --explain E0308`.
What am I doing wrong?