auto-palette 0.9.0

🎨 A Rust library that extracts prominent color palettes from images automatically.
Documentation
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
use std::borrow::Cow;
#[cfg(feature = "image")]
use std::path::Path;

#[cfg(feature = "image")]
use image::{DynamicImage, RgbImage, RgbaImage};

use crate::{
    color::{rgb_to_xyz, xyz_to_lab, Lab, D65},
    image::{error::ImageError, Pixel, RGBA_CHANNELS},
    math::normalize,
    Filter,
    FloatNumber,
    ImageResult,
};

/// The image data representing the pixel data of an image.
///
/// Each pixel is represented by 4 bytes in RGBA (Red, Green, Blue, Alpha) format.
/// The pixel data is stored in a linear array of bytes, where each pixel is represented by 4 bytes.
///
/// # Example
/// ```
/// #[cfg(feature = "image")]
/// {
///     use auto_palette::ImageData;
///
///     let pixels = [
///         255, 0, 0, 255, // Red
///         0, 255, 0, 255, // Green
///         0, 0, 255, 255, // Blue
///         0, 0, 0, 255, // Black
///     ];
///     let image_data = ImageData::new(2, 2, &pixels).unwrap();
///     assert_eq!(image_data.width(), 2);
///     assert_eq!(image_data.height(), 2);
///     assert_eq!(image_data.data(), &pixels);
/// }
/// ```
#[derive(Debug)]
pub struct ImageData<'a> {
    width: u32,
    height: u32,
    data: Cow<'a, [u8]>,
}

impl<'a> ImageData<'a> {
    /// Creates a new `ImageData` with the given width, height, and pixel data.
    ///
    /// # Arguments
    /// * `width` - The width of the image data.
    /// * `height` - The height of the image data.
    /// * `data` - The pixel data of the image data.
    ///
    /// # Returns
    /// The `ImageData` with the given width, height, and pixel data.
    ///
    /// # Errors
    /// Returns an error if the length of the pixel data is not equal to `width * height * 4`.
    pub fn new(width: u32, height: u32, data: &'a [u8]) -> ImageResult<Self> {
        let expected_length = (width * height) as usize * RGBA_CHANNELS;
        if data.len() != expected_length {
            return Err(ImageError::UnexpectedLength {
                expected: expected_length,
                actual: data.len(),
            });
        }

        Ok(Self {
            width,
            height,
            data: Cow::Borrowed(data),
        })
    }

    /// Loads the image data from the given path.
    /// The image data is loaded using the `image` crate.
    ///
    /// # Arguments
    /// * `path` - The path to the image file.
    ///
    /// # Returns
    /// The image data loaded from the given path.
    ///
    /// # Errors
    /// Returns an error if the image loading process fails.
    /// Returns an error if the color type of the image is not supported.
    #[cfg(feature = "image")]
    pub fn load<P>(path: P) -> ImageResult<Self>
    where
        P: AsRef<Path>,
    {
        let image = image::open(path).map_err(ImageError::from)?;
        Self::try_from(&image)
    }

    /// Checks whether the image data is empty.
    ///
    /// # Returns
    /// `true` if the image data is empty, `false` otherwise.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.data.is_empty()
    }

    /// Returns the width of the image data.
    ///
    /// # Returns
    /// The width of the image data.
    #[inline]
    #[must_use]
    pub fn width(&self) -> u32 {
        self.width
    }

    /// Returns the height of the image data.
    ///
    /// # Returns
    /// The height of the image data.
    #[inline]
    #[must_use]
    pub fn height(&self) -> u32 {
        self.height
    }

    /// Returns the area of the image data.
    ///
    /// # Returns
    /// The area of the image data.
    #[must_use]
    pub fn area(&self) -> usize {
        self.width as usize * self.height as usize
    }

    /// Returns the pixel data of the image data.
    ///
    /// Each pixel is represented by 4 bytes in RGBA (Red, Green, Blue, Alpha) format.
    ///
    /// # Returns
    /// The pixel data of the image data.
    #[must_use]
    pub fn data(&self) -> &[u8] {
        &self.data
    }

    /// Returns an iterator over the pixels of the image data.
    ///
    /// # Returns
    /// An iterator over the pixels of the image data.
    #[allow(dead_code)]
    pub(crate) fn pixels<'b, T>(&'b self) -> impl Iterator<Item = Pixel<T>> + 'b
    where
        T: FloatNumber + 'b,
    {
        self.data
            .chunks_exact(RGBA_CHANNELS)
            .enumerate()
            .map(move |(index, rgba)| self.chunk_to_pixel(index, rgba))
    }

    /// Returns an iterator over the pixels of the image data together with the result of applying the `filter`.
    ///
    /// # Type Parameters
    /// * `T` - The floating point type.
    /// * `F` - The filter type.
    ///
    /// # Arguments
    /// * `filter` - The filter to apply to the pixels.
    ///
    /// # Returns
    /// An iterator over the pixels of the image data and the result of applying the filter.
    pub(crate) fn pixels_with_filter<'b, T, F>(
        &'b self,
        filter: &'b F,
    ) -> impl Iterator<Item = (Pixel<T>, bool)> + 'b
    where
        T: FloatNumber + 'b,
        F: Filter,
    {
        self.data
            .chunks_exact(RGBA_CHANNELS)
            .enumerate()
            .map(move |(index, chunk)| {
                (
                    self.chunk_to_pixel::<T>(index, chunk),
                    filter.test(&[chunk[0], chunk[1], chunk[2], chunk[3]]),
                )
            })
    }

    /// Converts a chunk of pixel data to a pixel representation.
    ///
    /// # Type Parameters
    /// * `T` - The floating point type.
    ///
    /// # Arguments
    /// * `index` - The index of the pixel in the image data.
    /// * `chunk` - The chunk of pixel data.
    ///
    /// # Returns
    /// The pixel representation of the chunk of pixel data.
    #[inline(always)]
    #[must_use]
    fn chunk_to_pixel<T>(&self, index: usize, chunk: &[u8]) -> Pixel<T>
    where
        T: FloatNumber,
    {
        let (x, y, z) = rgb_to_xyz::<T>(chunk[0], chunk[1], chunk[2]);
        let (l, a, b) = xyz_to_lab::<T, D65>(x, y, z);

        let coord_x = T::from_usize((index % self.width as usize) + 1);
        let coord_y = T::from_usize((index / self.width as usize) + 1);

        let width_f = T::from_u32(self.width);
        let height_f = T::from_u32(self.height);

        [
            Lab::<T>::normalize_l(l),
            Lab::<T>::normalize_a(a),
            Lab::<T>::normalize_b(b),
            normalize(coord_x, T::zero(), width_f),
            normalize(coord_y, T::zero(), height_f),
        ]
    }
}

#[cfg(feature = "image")]
impl TryFrom<&DynamicImage> for ImageData<'_> {
    type Error = ImageError;

    fn try_from(image: &DynamicImage) -> Result<Self, Self::Error> {
        match image {
            DynamicImage::ImageRgb8(image) => Ok(Self::from(image)),
            DynamicImage::ImageRgba8(image) => Ok(Self::from(image)),
            _ => Err(ImageError::UnsupportedFormat),
        }
    }
}

#[cfg(feature = "image")]
impl From<&RgbImage> for ImageData<'_> {
    fn from(image: &RgbImage) -> Self {
        let (width, height) = image.dimensions();
        let size = (width * height) as usize;
        let data = image.pixels().fold(
            Vec::with_capacity(size * RGBA_CHANNELS),
            |mut pixels, pixel| {
                pixels.extend_from_slice(&[pixel[0], pixel[1], pixel[2], 255]);
                pixels
            },
        );
        Self {
            width,
            height,
            data: data.into(),
        }
    }
}

#[cfg(feature = "image")]
impl From<&RgbaImage> for ImageData<'_> {
    fn from(image: &RgbaImage) -> Self {
        let (width, height) = image.dimensions();
        let data = image.to_vec();
        Self {
            width,
            height,
            data: data.into(),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{assert_approx_eq, Rgba};

    #[test]
    fn test_new() {
        // Arrange
        let pixels = [
            255, 255, 255, 255, // White
            255, 255, 255, 255, // White
            255, 255, 255, 255, // White
            255, 255, 255, 255, // White
        ];

        // Act
        let actual = ImageData::new(2, 2, &pixels);

        // Assert
        assert!(actual.is_ok());

        let image_data = actual.unwrap();
        assert!(!image_data.is_empty());
        assert_eq!(image_data.width(), 2);
        assert_eq!(image_data.height(), 2);
        assert_eq!(image_data.area(), 4);
        assert_eq!(image_data.data(), &pixels);
    }

    #[test]
    fn test_new_empty_data() {
        // Arrange
        let pixels = [];

        // Act
        let actual = ImageData::new(0, 0, &pixels);

        // Assert
        assert!(actual.is_ok());

        let image_data = actual.unwrap();
        assert!(image_data.is_empty());
        assert_eq!(image_data.width(), 0);
        assert_eq!(image_data.height(), 0);
        assert_eq!(image_data.area(), 0);
        assert_eq!(image_data.data(), &pixels);
    }

    #[test]
    fn test_new_unexpected_length() {
        // Arrange
        let pixels = [255, 255, 255, 255];

        // Act
        let actual = ImageData::new(2, 2, &pixels);

        // Assert
        assert!(actual.is_err());

        let error = actual.unwrap_err();
        assert_eq!(
            error.to_string(),
            "Unexpected data length - expected 16, got 4"
        );
    }

    #[cfg(feature = "image")]
    #[test]
    fn test_load_supported_format() {
        // Act
        let actual = ImageData::load("../../gfx/parrots_rgba8.png");

        // Assert
        assert!(actual.is_ok());

        let image_data = actual.unwrap();
        assert!(!image_data.is_empty());
        assert_eq!(image_data.width(), 150);
        assert_eq!(image_data.height(), 150);
        assert_eq!(image_data.area(), 150 * 150);
        assert_eq!(image_data.data().len(), 150 * 150 * 4);
    }

    #[cfg(feature = "image")]
    #[test]
    fn test_load_unsupported_format() {
        // Act
        let actual = ImageData::load("../../gfx/parrots_la16.png");

        // Assert
        assert!(actual.is_err());

        let error = actual.unwrap_err();
        assert_eq!(error.to_string(), "Unsupported image format or color type");
    }

    #[cfg(all(feature = "image", not(target_os = "windows")))]
    #[test]
    fn test_load_unknown_path() {
        // Act
        let actual = ImageData::load("../../gfx/unknown.png");

        // Assert
        assert!(actual.is_err());

        let error = actual.unwrap_err();
        assert_eq!(
            error.to_string(),
            "Failed to load image from file: No such file or directory (os error 2)"
        );
    }

    #[cfg(all(feature = "image", target_os = "windows"))]
    #[test]
    fn test_load_unknown_path_windows() {
        // Act
        let actual = ImageData::load("../../gfx/unknown.png");

        // Assert
        assert!(actual.is_err());

        let error = actual.unwrap_err();
        assert_eq!(
            error.to_string(),
            "Failed to load image from file: The system cannot find the file specified. (os error 2)"
        );
    }

    #[cfg(all(feature = "image", not(target_os = "windows")))]
    #[test]
    fn test_load_invalid_file() {
        // Act
        let actual = ImageData::load("../../gfx/colors/invalid.jpg");

        // Assert
        assert!(actual.is_err());

        let error = actual.unwrap_err();
        assert_eq!(
            error.to_string(),
            "Failed to load image from file: No such file or directory (os error 2)"
        );
    }

    #[cfg(all(feature = "image", target_os = "windows"))]
    #[test]
    fn test_load_invalid_file_windows() {
        // Act
        let actual = ImageData::load("../../gfx/colors/invalid.jpg");

        // Assert
        assert!(actual.is_err());

        let error = actual.unwrap_err();
        assert_eq!(
            error.to_string(),
            "Failed to load image from file: The system cannot find the file specified. (os error 2)"
        );
    }

    #[test]
    fn test_pixels_iter() {
        // Arrange
        let pixels = [
            255, 0, 0, 255, // Red
            0, 0, 0, 0, // Transparent
            255, 255, 0, 255, // Yellow
            0, 0, 0, 0, // Transparent
        ];
        let image_data = ImageData::new(2, 2, &pixels).unwrap();

        // Act
        let actual: Vec<_> = image_data.pixels::<f64>().collect();

        // Assert
        assert_eq!(actual.len(), 4);

        let pixel = actual[0];
        assert_approx_eq!(pixel[0], 0.532371);
        assert_approx_eq!(pixel[1], 0.816032);
        assert_approx_eq!(pixel[2], 0.765488);
        assert_approx_eq!(pixel[3], 0.5);
        assert_approx_eq!(pixel[4], 0.5);

        let pixel = actual[1];
        assert_approx_eq!(pixel[0], 0.0);
        assert_approx_eq!(pixel[1], 0.501960);
        assert_approx_eq!(pixel[2], 0.501960);
        assert_approx_eq!(pixel[3], 1.0);
        assert_approx_eq!(pixel[4], 0.5);

        let pixel = actual[2];
        assert_approx_eq!(pixel[0], 0.971385);
        assert_approx_eq!(pixel[1], 0.417402);
        assert_approx_eq!(pixel[2], 0.872457);
        assert_approx_eq!(pixel[3], 0.5);
        assert_approx_eq!(pixel[4], 1.0);

        let pixel = actual[3];
        assert_approx_eq!(pixel[0], 0.0);
        assert_approx_eq!(pixel[1], 0.501960);
        assert_approx_eq!(pixel[2], 0.501960);
        assert_approx_eq!(pixel[3], 1.0);
        assert_approx_eq!(pixel[4], 1.0);
    }

    #[test]
    fn test_pixels_with_filter() {
        // Arrange
        let data = [
            255, 0, 0, 255, // Red
            0, 0, 0, 0, // Transparent
            255, 255, 0, 255, // Yellow
            0, 0, 0, 0, // Transparent
        ];
        let image_data = ImageData::new(2, 2, &data).unwrap();

        // Act
        let (pixels, mask) = image_data
            .pixels_with_filter::<f64, _>(&|rgba: &Rgba| rgba[3] != 0)
            .fold(
                (Vec::new(), Vec::new()),
                |(mut pixels, mut mask), (pixel, m)| {
                    pixels.push(pixel);
                    mask.push(m);
                    (pixels, mask)
                },
            );

        // Assert
        assert_eq!(pixels.len(), 4);
        assert_eq!(mask.len(), 4);
        assert_eq!(mask, vec![true, false, true, false]);
    }
}