Newer
Older
use std::path::Path;
use clap::ArgEnum;
use image::io::Reader;
use image::{ImageError, ImageFormat, RgbaImage};
use serde::{Deserialize, Serialize};
use thiserror::Error;
#[derive(
Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ArgEnum, Debug, Serialize, Deserialize, Default,
)]
pub enum PaletteFormat {
}
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ArgEnum, Debug)]
pub enum Format {
Png,
Jpg,
Gif,
Ico,
Tga,
Tiff,
Bmp,
}
impl Format {
pub fn as_image_format(&self) -> ImageFormat {
use Format::*;
match self {
Png => ImageFormat::Png,
Jpg => ImageFormat::Jpeg,
Gif => ImageFormat::Gif,
Ico => ImageFormat::Ico,
Tga => ImageFormat::Tga,
Tiff => ImageFormat::Tiff,
Bmp => ImageFormat::Bmp,
}
}
}
#[derive(Error, Debug)]
pub enum ImageLoadingError {
#[error("Failed to load image data; {0}")]
IoError(#[from] std::io::Error),
#[error("Failed to process image; {0}")]
ImageError(#[from] ImageError),
}
pub fn load_image<T: AsRef<Path>>(
path: T,
format: Option<Format>,
) -> anyhow::Result<RgbaImage, ImageLoadingError> {
let mut image = Reader::open(path)?;
let file = match format {
Some(format) => {
image.set_format(format.as_image_format());
image.decode()?.into_rgba8()
}
None => image.with_guessed_format()?.decode()?.into_rgba8(),
};
Ok(file)
}