image-rs / image

Encoding and decoding images in Rust
Apache License 2.0
4.76k stars 588 forks source link

DynamicImage:from SubImage<&DynamicImage>::to_image creates Rgba8 instead of L16 #2274

Open emirror-de opened 1 week ago

emirror-de commented 1 week ago

I am using the code below to transform a grayscale image from SubImage<&DynamicImage> to DynamicImage. Is there something I am missing during this conversion?

Expected

I would expect the new dest image to be L16 as well.

Actual behaviour

dest image is of Rgba8.

Reproduction steps

// source = SubImage<&DynamicImage>
debug!("{}", source.inner().color()); // outputs: L16
let dest = DynamicImage::from(source.to_image());
debug!("{}", dest.color()); // outputs: Rgba8
emirror-de commented 1 week ago

I am now using this workaround:

let tmp_img = source.to_image();
let mut dest = DynamicImage::new(tmp_img.width(), tmp_img.height(), img.inner().color());
dest.copy_from(&*source, 0, 0)?;

But this feels a bit complicated and suboptimal?

fintelia commented 6 days ago

This is related to https://github.com/image-rs/image/issues/1952. The DynamicImage enum implements GenericImage<Rgba<u8>> even though it really shouldn't

emirror-de commented 4 days ago

Ahh I see. Thanks for the hint.