2022-11-17 16:08:11 +11:00
|
|
|
use std::path::Path;
|
|
|
|
|
|
|
|
pub struct Image {
|
|
|
|
pub bytes: Vec<u8>,
|
2022-11-26 18:38:15 +11:00
|
|
|
pub size: Size<u32>,
|
|
|
|
pub pitch: usize,
|
2022-11-17 16:08:11 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Image {
|
2022-11-22 10:19:43 +11:00
|
|
|
pub fn load(path: impl AsRef<Path>) -> Result<Self, ImageError> {
|
2022-11-21 18:13:22 +11:00
|
|
|
let image = image::open(path.as_ref())?.flipv().to_rgba8();
|
|
|
|
|
2022-11-17 16:08:11 +11:00
|
|
|
let height = image.height();
|
|
|
|
let width = image.width();
|
2022-11-26 18:38:15 +11:00
|
|
|
let pitch = image.sample_layout().height_stride.max(
|
|
|
|
image.sample_layout().width_stride
|
|
|
|
);
|
2022-11-22 08:21:50 +11:00
|
|
|
|
2022-11-17 16:08:11 +11:00
|
|
|
Ok(Image {
|
2022-11-26 18:38:15 +11:00
|
|
|
bytes: image.into_raw(),
|
|
|
|
pitch,
|
2022-11-24 17:37:16 +11:00
|
|
|
size: Size {
|
|
|
|
height,
|
|
|
|
width,
|
|
|
|
}
|
2022-11-17 16:08:11 +11:00
|
|
|
})
|
|
|
|
}
|
2022-11-22 08:21:50 +11:00
|
|
|
}
|
2022-11-22 09:44:38 +11:00
|
|
|
|
2022-11-24 17:37:16 +11:00
|
|
|
pub use image::ImageError as ImageError;
|
|
|
|
use crate::Size;
|