ori_core/image/
image.rs

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
use std::{
    fmt::Debug,
    hash::{Hash, Hasher},
    ops::Deref,
    sync::{Arc, Weak},
};

use crate::canvas::Color;

use super::ImageData;

/// Include an image.
///
/// Path is relative to the `CARGO_MANIFEST_DIR` environment variable.
#[macro_export]
#[cfg(feature = "image")]
macro_rules! include_image {
    ($path:literal) => {{
        static IMAGE: ::std::sync::OnceLock<$crate::image::Image> = ::std::sync::OnceLock::new();

        ::std::sync::OnceLock::get_or_init(&IMAGE, || {
            let bytes = <[::std::primitive::u8]>::to_vec(::std::include_bytes!(
                // use concat! to get the full path relative to the CARGO_MANIFEST_DIR
                ::std::concat!(::std::env!("CARGO_MANIFEST_DIR"), "/", $path)
            ));

            match $crate::image::Image::try_load_data(bytes) {
                ::std::result::Result::Ok(image) => image,
                ::std::result::Result::Err(err) => {
                    ::std::panic!("Failed to load image:{}: {}", $path, err);
                }
            }
        })
        .clone()
    }};
}

/// A unique identifier for an [`Image`].
///
/// The identifier is computed by hashing the image data.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct ImageId {
    pub(crate) hash: u64,
}

/// An clonable image.
#[derive(Clone, Debug)]
pub struct Image {
    id: ImageId,
    data: Arc<ImageData>,
}

impl Default for Image {
    fn default() -> Self {
        Self::from(ImageData::default())
    }
}

impl Image {
    /// Create a new image.
    ///
    /// # Panics
    /// - If `pixels.len()` is not equal to `width * height * 4`.
    pub fn new(pixels: Vec<u8>, width: u32, height: u32) -> Self {
        Self::from(ImageData::new(pixels, width, height))
    }

    /// Try to load an image from a file.
    #[cfg(feature = "image")]
    pub fn try_load_data(data: Vec<u8>) -> image::ImageResult<Self> {
        Ok(Self::from(ImageData::try_load_data(data)?))
    }

    /// Load an image from a file.
    #[cfg(feature = "image")]
    pub fn load_data(data: Vec<u8>) -> Self {
        Self::from(ImageData::load_data(data))
    }

    /// Try to load an image from a file.
    #[cfg(feature = "image")]
    pub fn try_load(path: impl AsRef<std::path::Path>) -> image::ImageResult<Self> {
        Ok(Self::from(ImageData::try_load(path)?))
    }

    /// Load an image from a file.
    #[cfg(feature = "image")]
    pub fn load(path: impl AsRef<std::path::Path>) -> Self {
        Self::from(ImageData::load(path))
    }

    /// Premultiply the image alpha, returning a new image.
    pub fn premultiplied(mut self) -> Self {
        self.multiply_alpha();
        self
    }

    /// Get the [`ImageId`].
    pub fn id(&self) -> ImageId {
        self.id
    }

    /// Modify the image data.
    pub fn modify(&mut self, f: impl FnOnce(&mut ImageData)) {
        f(Arc::make_mut(&mut self.data));
        self.id = self.data.compute_id();
    }

    /// Multiply the image with a color.
    pub fn color(&mut self, color: Color) {
        let [r, g, b, a] = color.to_rgba8();

        self.modify(|data| {
            for pixel in data.chunks_exact_mut(4) {
                pixel[0] = (pixel[0] as u16 * r as u16 / 255) as u8;
                pixel[1] = (pixel[1] as u16 * g as u16 / 255) as u8;
                pixel[2] = (pixel[2] as u16 * b as u16 / 255) as u8;
                pixel[3] = (pixel[3] as u16 * a as u16 / 255) as u8;
            }
        });
    }

    /// Premultiply the image alpha.
    pub fn multiply_alpha(&mut self) {
        self.modify(|data| {
            for pixel in data.chunks_exact_mut(4) {
                let alpha = pixel[3] as u16;

                pixel[0] = (pixel[0] as u16 * alpha / 255) as u8;
                pixel[1] = (pixel[1] as u16 * alpha / 255) as u8;
                pixel[2] = (pixel[2] as u16 * alpha / 255) as u8;
            }
        });
    }

    /// Downgrade the image to a weak reference.
    pub fn downgrade(&self) -> WeakImage {
        WeakImage {
            id: self.id,
            data: Arc::downgrade(&self.data),
        }
    }
}

impl Deref for Image {
    type Target = ImageData;

    fn deref(&self) -> &Self::Target {
        &self.data
    }
}

impl From<ImageData> for Image {
    fn from(value: ImageData) -> Self {
        let id = value.compute_id();
        let data = Arc::new(value);

        Self { id, data }
    }
}

impl PartialEq for Image {
    fn eq(&self, other: &Self) -> bool {
        self.id == other.id
    }
}

impl Eq for Image {}

impl Hash for Image {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.id.hash(state);
    }
}

/// A weak reference to an [`Image`].
#[derive(Clone, Debug)]
pub struct WeakImage {
    id: ImageId,
    data: Weak<ImageData>,
}

impl WeakImage {
    /// Get the [`ImageId`].
    pub fn id(&self) -> ImageId {
        self.id
    }

    /// Get the number of strong references to the image.
    pub fn strong_count(&self) -> usize {
        self.data.strong_count()
    }

    /// Get the number of weak references to the image.
    pub fn weak_count(&self) -> usize {
        self.data.weak_count()
    }
}

impl PartialEq for WeakImage {
    fn eq(&self, other: &Self) -> bool {
        self.id == other.id
    }
}

impl Eq for WeakImage {}

impl Hash for WeakImage {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.id.hash(state);
    }
}