agb/agb-image-converter/src/image_loader.rs

58 lines
1.5 KiB
Rust
Raw Normal View History

use std::{ffi::OsStr, path};
2021-04-20 09:40:07 +10:00
use image::{DynamicImage, GenericImageView};
2021-04-20 09:40:07 +10:00
use crate::colour::Colour;
#[derive(Clone)]
2021-04-20 09:40:07 +10:00
pub(crate) struct Image {
pub width: usize,
pub height: usize,
colour_data: Vec<Colour>,
}
impl Image {
pub fn load_from_file(image_path: &path::Path) -> Self {
2023-04-15 06:16:12 +10:00
let img = if image_path.extension() == Some(OsStr::new("aseprite")) {
let ase =
asefile::AsepriteFile::read_file(image_path).expect("failed to read aseprite file");
DynamicImage::ImageRgba8(ase.frame(0).image())
} else {
image::open(image_path).expect("Expected image to exist")
};
2022-02-23 07:16:34 +11:00
Self::load_from_dyn_image(img)
}
pub fn load_from_dyn_image(img: image::DynamicImage) -> Self {
2021-04-20 09:40:07 +10:00
let (width, height) = img.dimensions();
let width = width as usize;
let height = height as usize;
let mut colour_data = Vec::with_capacity(width * height);
for (_, _, pixel) in img.pixels() {
2022-05-23 04:23:29 +10:00
colour_data.push(Colour::from_rgb(pixel[0], pixel[1], pixel[2], pixel[3]));
2021-04-20 09:40:07 +10:00
}
Image {
width,
height,
colour_data,
}
}
pub fn from_colour_data(colour_data: Vec<Colour>) -> Self {
Self {
height: colour_data.len() / 8,
colour_data,
width: 8,
}
}
2021-04-20 09:40:07 +10:00
pub fn colour(&self, x: usize, y: usize) -> Colour {
self.colour_data[x + y * self.width]
}
}