Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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 {
TXT,
#[default]
PNG,
}
// impl Default for PaletteFormat {
// fn default() -> Self {
// Self::PNG
// }
// }
#[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)
}