Support for Images without Alpha Channel?
wyhinton opened this issue · comments
As it stands ImageData requires an alpha channel. I think to new users it would be intuitive that you specify some kind of depth for the Image data you're trying to set, for example Rgb8, Rgba8, La8
.
As it stands trying to set a simple .jpg to the clipboard with return an slice out of range error:
let mut ctx = arboard::Clipboard::new().unwrap();
let img = ImageReader::open("imgs/glitch_1.jpg").unwrap().decode().unwrap();
let (w,h) = img.clone().into_rgb8().dimensions();
let bytes = img.as_bytes();
let img_data = arboard::ImageData{ width: w as usize, height: h as usize, bytes: bytes.into()};
ctx.set_image(img_data).unwrap();
///for a 400x400 JPEG
thread 'main' panicked at 'range start index 638400 out of range for slice of length 480000',
If providing an alpha channel is a requirement of the OS, then maybe the readme/examples/docs could mention this, and provide some strategies for working around it, given that many people would assume support for common compression formats.
I think to new users it would be intuitive that you specify some kind of depth for the Image data you're trying to set, for example
Rgb8, Rgba8, La8
.
It seems simpler to me if there's only one way to use it.
The example you showed should work if do it like this:
let mut ctx = arboard::Clipboard::new().unwrap();
let img = ImageReader::open("imgs/glitch_1.jpg").unwrap().decode().unwrap();
let rgba8 = img.into_rgba8(); // Of course you can use `img.clone().into_rgba8()` if needed
let (w,h) = rgba8.dimensions();
let bytes = rgba8.into_raw();
let img_data = arboard::ImageData{ width: w as usize, height: h as usize, bytes: bytes.into()};
ctx.set_image(img_data).unwrap();
If providing an alpha channel is a requirement of the OS, then maybe the readme/examples/docs could mention this
Providing an alpha channel is a requirement of the ImageData
and as far as I can tell, the documentation is clear about this.
I should probably add an example for how to use the image
crate together with this, but otherwise I think this issue can be closed. What do you think?