Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GenericImage::copy_from can't be used with RGB, only with RGBa #1952

Open
libbkmz opened this issue Jun 26, 2023 · 1 comment · May be fixed by #2344
Open

GenericImage::copy_from can't be used with RGB, only with RGBa #1952

libbkmz opened this issue Jun 26, 2023 · 1 comment · May be fixed by #2344
Labels
kind: API missing or awkward public APIs

Comments

@libbkmz
Copy link

libbkmz commented Jun 26, 2023

I'm trying to create DynamicImage from FlatSamples, which I got from another library.

use image::GenericImage;

fn main() {
    let w = 640;
    let h = 480;
    let bits_per_pixel = 3;

    let buffer = image::FlatSamples {
        samples: vec![],
        layout: image::flat::SampleLayout::row_major_packed(bits_per_pixel, w, h),
        color_hint: None,
    };

    let view = buffer.as_view::<image::Rgb<u8>>().unwrap();

    let mut dyn_img = image::DynamicImage::new_rgb8(w, h);
    let _ = dyn_img.copy_from(&view, 0, 0);
}

Playground link

This is the compilation error output:

error[[E0271]](https://doc.rust-lang.org/stable/error_codes/E0271.html): type mismatch resolving `<View<&[u8], Rgb<u8>> as GenericImageView>::Pixel == Rgba<u8>`
    --> src/main.rs:17:31
     |
17   |     let _ = dyn_img.copy_from(&view, 0, 0);
     |                     --------- ^^^^^ expected `Rgba<u8>`, found `Rgb<u8>`
     |                     |
     |                     required by a bound introduced by this call
     |
     = note: expected struct `Rgba<u8>`
                found struct `Rgb<u8>`

How can I create DynamicImage without an Alpha channel?

@fintelia
Copy link
Contributor

fintelia commented Jul 7, 2023

Construct an RgbImage and then make the DynamicImage from that:

let mut img = image::RgbImage::new(w, h);
img.copy_from(&view, 0, 0).unwrap();
let dyn_img = DynamicImage::ImageRgb8(img);

The fact that DynamicImage has a copy_from method at all is a bug IMO, because as you observed it only works for Rgba8 and not any other color type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: API missing or awkward public APIs
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants