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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
|
use std::fmt::Display;
use std::path::Path;
use std::sync::atomic::{AtomicU32, Ordering};
use image::io::Reader as ImageReader;
use image::{DynamicImage, ImageError, Rgb, RgbImage};
use crate::color::Color;
use crate::data_types::dimens::Dimens;
use crate::opengl::texture::PixelDataFormat;
use crate::util::builder;
static NEXT_ID: AtomicU32 = AtomicU32::new(0);
mod reexports
{
pub use crate::opengl::texture::{Filtering, Wrapping};
}
pub use reexports::*;
#[derive(Debug, Clone)]
pub struct Texture
{
id: Id,
image: DynamicImage,
pixel_data_format: PixelDataFormat,
dimensions: Dimens<u32>,
properties: Properties,
}
impl Texture
{
pub fn builder() -> Builder
{
Builder::default()
}
/// Opens a texture image.
///
/// # Errors
/// Will return `Err` if:
/// - Opening the image fails
/// - The image data is not 8-bit/color RGB
pub fn open(path: &Path) -> Result<Self, Error>
{
Self::builder().open(path)
}
#[must_use]
pub fn new_from_color(dimensions: &Dimens<u32>, color: &Color<u8>) -> Self
{
Self::builder().build_with_single_color(dimensions, color)
}
#[must_use]
pub fn id(&self) -> Id
{
self.id
}
#[must_use]
pub fn properties(&self) -> &Properties
{
&self.properties
}
pub fn properties_mut(&mut self) -> &mut Properties
{
&mut self.properties
}
#[must_use]
pub fn dimensions(&self) -> &Dimens<u32>
{
&self.dimensions
}
#[must_use]
pub fn pixel_data_format(&self) -> PixelDataFormat
{
self.pixel_data_format
}
#[must_use]
pub fn image(&self) -> &DynamicImage
{
&self.image
}
}
impl Drop for Texture
{
fn drop(&mut self)
{
NEXT_ID.fetch_sub(1, Ordering::Relaxed);
}
}
/// Texture builder.
#[derive(Debug, Default, Clone)]
pub struct Builder
{
properties: Properties,
}
impl Builder
{
pub fn properties(mut self, properties: Properties) -> Self
{
self.properties = properties;
self
}
/// Opens a image as a texture.
///
/// # Errors
/// Will return `Err` if:
/// - Opening the image fails
/// - Decoding the image fails
/// - The image data is in a unsupported format
pub fn open(&self, path: &(impl AsRef<Path> + ?Sized)) -> Result<Texture, Error>
{
let image = ImageReader::open(path)
.map_err(Error::OpenImageFailed)?
.decode()
.map_err(Error::DecodeImageFailed)?;
let pixel_data_format = match &image {
DynamicImage::ImageRgb8(_) => PixelDataFormat::Rgb8,
DynamicImage::ImageRgba8(_) => PixelDataFormat::Rgba8,
_ => {
return Err(Error::UnsupportedImageDataFormat);
}
};
let dimensions = Dimens {
width: image.width(),
height: image.height(),
};
let id = NEXT_ID.fetch_add(1, Ordering::Relaxed);
Ok(Texture {
id: Id::new(id),
image,
pixel_data_format,
dimensions,
properties: self.properties.clone(),
})
}
#[must_use]
pub fn build_with_single_color(
&self,
dimensions: &Dimens<u32>,
color: &Color<u8>,
) -> Texture
{
let image = RgbImage::from_pixel(
dimensions.width,
dimensions.height,
Rgb([color.red, color.green, color.blue]),
);
let id = NEXT_ID.fetch_add(1, Ordering::Relaxed);
Texture {
id: Id::new(id),
image: image.into(),
pixel_data_format: PixelDataFormat::Rgb8,
dimensions: *dimensions,
properties: self.properties.clone(),
}
}
}
/// Texture error.
#[derive(Debug, thiserror::Error)]
pub enum Error
{
#[error("Failed to open texture image")]
OpenImageFailed(#[source] std::io::Error),
#[error("Failed to decode texture image")]
DecodeImageFailed(#[source] ImageError),
#[error("Unsupported image data format")]
UnsupportedImageDataFormat,
}
builder! {
/// Texture properties
#[builder(name = PropertiesBuilder, derives=(Debug, Clone))]
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct Properties
{
pub wrap: Wrapping,
pub magnifying_filter: Filtering,
pub minifying_filter: Filtering,
}
}
impl Properties
{
pub fn builder() -> PropertiesBuilder
{
PropertiesBuilder::default()
}
}
impl Default for Properties
{
fn default() -> Self
{
Self {
wrap: Wrapping::Repeat,
magnifying_filter: Filtering::Linear,
minifying_filter: Filtering::Nearest,
}
}
}
impl Default for PropertiesBuilder
{
fn default() -> Self
{
Properties::default().into()
}
}
/// Texture ID.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Id
{
id: u32,
}
impl Id
{
fn new(id: u32) -> Self
{
Self { id }
}
}
impl Display for Id
{
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result
{
self.id.fmt(formatter)
}
}
|