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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
use std::fs::File;
use std::io::BufReader;
use std::path::Path;
use image_rs::GenericImageView as _;
use crate::asset::{Assets, Submitter as AssetSubmitter};
use crate::color::Color;
use crate::data_types::dimens::Dimens;
use crate::builder;
#[derive(Debug)]
pub struct Image
{
inner: image_rs::DynamicImage,
}
impl Image
{
pub fn open(path: impl AsRef<Path>) -> Result<Self, Error>
{
let buffered_reader =
BufReader::new(File::open(&path).map_err(Error::ReadFailed)?);
let image_reader = image_rs::io::Reader::with_format(
buffered_reader,
image_rs::ImageFormat::from_path(path)
.map_err(|_| Error::UnsupportedFormat)?,
);
Ok(Self {
inner: image_reader
.decode()
.map_err(|err| Error::DecodeFailed(DecodeError(err)))?,
})
}
pub fn from_color(dimens: impl Into<Dimens<u32>>, color: impl Into<Color<u8>>)
-> Self
{
let dimens: Dimens<u32> = dimens.into();
let color: Color<u8> = color.into();
Self {
inner: image_rs::RgbImage::from_pixel(
dimens.width,
dimens.height,
image_rs::Rgb([color.red, color.green, color.blue]),
)
.into(),
}
}
pub fn dimensions(&self) -> Dimens<u32>
{
self.inner.dimensions().into()
}
pub fn color_type(&self) -> ColorType
{
self.inner.color().into()
}
pub fn as_bytes(&self) -> &[u8]
{
self.inner.as_bytes()
}
}
builder! {
#[builder(name = SettingsBuilder, derives=(Debug, Clone))]
#[derive(Debug, Default, Clone)]
#[non_exhaustive]
pub struct Settings {
}
}
impl Settings
{
pub fn builder() -> SettingsBuilder
{
SettingsBuilder::default()
}
}
impl Default for SettingsBuilder
{
fn default() -> Self
{
Settings::default().into()
}
}
/// An enumeration over supported color types and bit depths
#[derive(Copy, PartialEq, Eq, Debug, Clone, Hash)]
#[non_exhaustive]
pub enum ColorType
{
/// Pixel is 8-bit luminance
L8,
/// Pixel is 8-bit luminance with an alpha channel
La8,
/// Pixel contains 8-bit R, G and B channels
Rgb8,
/// Pixel is 8-bit RGB with an alpha channel
Rgba8,
/// Pixel is 16-bit luminance
L16,
/// Pixel is 16-bit luminance with an alpha channel
La16,
/// Pixel is 16-bit RGB
Rgb16,
/// Pixel is 16-bit RGBA
Rgba16,
/// Pixel is 32-bit float RGB
Rgb32F,
/// Pixel is 32-bit float RGBA
Rgba32F,
}
impl From<image_rs::ColorType> for ColorType
{
fn from(color_type: image_rs::ColorType) -> Self
{
match color_type {
image_rs::ColorType::L8 => Self::L8,
image_rs::ColorType::La8 => Self::La8,
image_rs::ColorType::Rgb8 => Self::Rgb8,
image_rs::ColorType::Rgba8 => Self::Rgba8,
image_rs::ColorType::L16 => Self::L16,
image_rs::ColorType::La16 => Self::La16,
image_rs::ColorType::Rgb16 => Self::Rgb16,
image_rs::ColorType::Rgba16 => Self::Rgba16,
image_rs::ColorType::Rgb32F => Self::Rgb32F,
image_rs::ColorType::Rgba32F => Self::Rgba32F,
_ => {
panic!("Unrecognized image_rs::ColorType variant");
}
}
}
}
pub fn set_asset_importers(assets: &mut Assets)
{
assets.set_importer::<_, _>(["png", "jpg"], import_asset);
}
#[derive(Debug, thiserror::Error)]
pub enum Error
{
#[error("Failed to read image file")]
ReadFailed(#[source] std::io::Error),
#[error("Failed to decode image")]
DecodeFailed(DecodeError),
#[error("Unsupported image format")]
UnsupportedFormat,
}
#[derive(Debug, thiserror::Error)]
#[error(transparent)]
pub struct DecodeError(image_rs::ImageError);
fn import_asset(
asset_submitter: &mut AssetSubmitter<'_>,
path: &Path,
_settings: Option<&'_ Settings>,
) -> Result<(), Error>
{
asset_submitter.submit_store::<Image>(Image::open(path)?);
Ok(())
}
|