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
use ordered_float::NotNan;
#[cfg(feature = "use_serde")]
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::sync::Arc;
#[cfg(feature = "use_serde")]
fn deserialize_notnan<'de, D>(deserializer: D) -> Result<NotNan<f32>, D::Error>
where
D: Deserializer<'de>,
{
let value = f32::deserialize(deserializer)?;
NotNan::new(value).map_err(|e| serde::de::Error::custom(format!("{:?}", e)))
}
#[cfg(feature = "use_serde")]
#[cfg_attr(feature = "cargo-clippy", allow(clippy::trivially_copy_pass_by_ref))]
fn serialize_notnan<S>(value: &NotNan<f32>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
value.into_inner().serialize(serializer)
}
#[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TextureCoordinate {
#[cfg_attr(
feature = "use_serde",
serde(
deserialize_with = "deserialize_notnan",
serialize_with = "serialize_notnan"
)
)]
pub x: NotNan<f32>,
#[cfg_attr(
feature = "use_serde",
serde(
deserialize_with = "deserialize_notnan",
serialize_with = "serialize_notnan"
)
)]
pub y: NotNan<f32>,
}
impl TextureCoordinate {
pub fn new(x: NotNan<f32>, y: NotNan<f32>) -> Self {
Self { x, y }
}
pub fn new_f32(x: f32, y: f32) -> Self {
let x = NotNan::new(x).unwrap();
let y = NotNan::new(y).unwrap();
Self::new(x, y)
}
}
#[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ImageCell {
top_left: TextureCoordinate,
bottom_right: TextureCoordinate,
data: Arc<ImageData>,
}
impl ImageCell {
pub fn new(
top_left: TextureCoordinate,
bottom_right: TextureCoordinate,
data: Arc<ImageData>,
) -> Self {
Self {
top_left,
bottom_right,
data,
}
}
pub fn top_left(&self) -> TextureCoordinate {
self.top_left
}
pub fn bottom_right(&self) -> TextureCoordinate {
self.bottom_right
}
pub fn image_data(&self) -> &Arc<ImageData> {
&self.data
}
}
static IMAGE_ID: ::std::sync::atomic::AtomicUsize = ::std::sync::atomic::AtomicUsize::new(0);
#[cfg_attr(feature = "use_serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ImageData {
id: usize,
data: Box<[u8]>,
}
impl ImageData {
pub fn with_raw_data(data: Box<[u8]>) -> Self {
let id = IMAGE_ID.fetch_add(1, ::std::sync::atomic::Ordering::Relaxed);
Self { id, data }
}
#[inline]
pub fn data(&self) -> &[u8] {
&self.data
}
#[inline]
pub fn id(&self) -> usize {
self.id
}
}