photon-rs 0.3.3

High-performance image processing library for native use and the web
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
#[cfg(test)]
mod test {

    use image::ImageBuffer;

    use crate::channels::*;
    use crate::colour_spaces::*;
    use crate::transform::{resample, seam_carve};
    use crate::PhotonImage;

    #[test]
    fn test_alter_red_channel() {
        let width = 4;
        let height = 4;
        // Create an image from a vec of pixels
        let raw_pix = vec![
            134, 122, 131, 255, 131, 131, 139, 255, 135, 134, 137, 255, 138, 134, 130,
            255, 126, 125, 119, 255, 131, 134, 129, 255, 137, 134, 132, 255, 130, 126,
            130, 255, 132, 125, 132, 255, 122, 142, 129, 255, 134, 135, 128, 255, 138,
            120, 125, 255, 125, 134, 110, 255, 121, 122, 137, 255, 141, 140, 141, 255,
            125, 144, 120, 255,
        ];

        let altered_r_channel_pix = vec![
            174, 122, 131, 255, 171, 131, 139, 255, 175, 134, 137, 255, 178, 134, 130,
            255, 166, 125, 119, 255, 171, 134, 129, 255, 177, 134, 132, 255, 170, 126,
            130, 255, 172, 125, 132, 255, 162, 142, 129, 255, 174, 135, 128, 255, 178,
            120, 125, 255, 165, 134, 110, 255, 161, 122, 137, 255, 181, 140, 141, 255,
            165, 144, 120, 255,
        ];

        let mut photon_image = PhotonImage::new(raw_pix, width, height);
        alter_red_channel(&mut photon_image, 40);
        assert_eq!(photon_image.raw_pixels, altered_r_channel_pix);
    }

    #[test]
    fn test_alter_blue_channel() {
        let width = 4;
        let height = 4;
        // Create an image from a vec of pixels
        let raw_pix = vec![
            134, 122, 131, 255, 131, 131, 139, 255, 135, 134, 137, 255, 138, 134, 130,
            255, 126, 125, 119, 255, 131, 134, 129, 255, 137, 134, 132, 255, 130, 126,
            130, 255, 132, 125, 132, 255, 122, 142, 129, 255, 134, 135, 128, 255, 138,
            120, 125, 255, 125, 134, 110, 255, 121, 122, 137, 255, 141, 140, 141, 255,
            125, 144, 120, 255,
        ];

        let altered_b_channel_pix = vec![
            134, 122, 171, 255, 131, 131, 179, 255, 135, 134, 177, 255, 138, 134, 170,
            255, 126, 125, 159, 255, 131, 134, 169, 255, 137, 134, 172, 255, 130, 126,
            170, 255, 132, 125, 172, 255, 122, 142, 169, 255, 134, 135, 168, 255, 138,
            120, 165, 255, 125, 134, 150, 255, 121, 122, 177, 255, 141, 140, 181, 255,
            125, 144, 160, 255,
        ];

        let mut photon_image = PhotonImage::new(raw_pix, width, height);
        alter_blue_channel(&mut photon_image, 40);
        assert_eq!(photon_image.raw_pixels, altered_b_channel_pix);
    }

    #[test]
    fn test_alter_green_channel() {
        let width = 4;
        let height = 4;
        // Create an image from a vec of pixels
        let raw_pix = vec![
            134, 122, 131, 255, 131, 131, 139, 255, 135, 134, 137, 255, 138, 134, 130,
            255, 126, 125, 119, 255, 131, 134, 129, 255, 137, 134, 132, 255, 130, 126,
            130, 255, 132, 125, 132, 255, 122, 142, 129, 255, 134, 135, 128, 255, 138,
            120, 125, 255, 125, 134, 110, 255, 121, 122, 137, 255, 141, 140, 141, 255,
            125, 144, 120, 255,
        ];

        let altered_g_channel_pix = vec![
            134, 162, 131, 255, 131, 171, 139, 255, 135, 174, 137, 255, 138, 174, 130,
            255, 126, 165, 119, 255, 131, 174, 129, 255, 137, 174, 132, 255, 130, 166,
            130, 255, 132, 165, 132, 255, 122, 182, 129, 255, 134, 175, 128, 255, 138,
            160, 125, 255, 125, 174, 110, 255, 121, 162, 137, 255, 141, 180, 141, 255,
            125, 184, 120, 255,
        ];

        let mut photon_image = PhotonImage::new(raw_pix, width, height);
        alter_green_channel(&mut photon_image, 40);
        assert_eq!(photon_image.raw_pixels, altered_g_channel_pix);
    }

    #[test]
    fn test_swap_blue_green_channels() {
        let width = 4;
        let height = 4;
        // Create an image from a vec of pixels
        let raw_pix = vec![
            134, 122, 131, 255, 131, 131, 139, 255, 135, 134, 137, 255, 138, 134, 130,
            255, 126, 125, 119, 255, 131, 134, 129, 255, 137, 134, 132, 255, 130, 126,
            130, 255, 132, 125, 132, 255, 122, 142, 129, 255, 134, 135, 128, 255, 138,
            120, 125, 255, 125, 134, 110, 255, 121, 122, 137, 255, 141, 140, 141, 255,
            125, 144, 120, 255,
        ];

        let correct_pix = vec![
            134, 131, 122, 255, 131, 139, 131, 255, 135, 137, 134, 255, 138, 130, 134,
            255, 126, 119, 125, 255, 131, 129, 134, 255, 137, 132, 134, 255, 130, 130,
            126, 255, 132, 132, 125, 255, 122, 129, 142, 255, 134, 128, 135, 255, 138,
            125, 120, 255, 125, 110, 134, 255, 121, 137, 122, 255, 141, 141, 140, 255,
            125, 120, 144, 255,
        ];

        let mut photon_image = PhotonImage::new(raw_pix, width, height);
        swap_channels(&mut photon_image, 1, 2);
        assert_eq!(photon_image.raw_pixels, correct_pix);
    }

    #[test]
    fn test_swap_blue_red_channels() {
        let width = 4;
        let height = 4;
        // Create an image from a vec of pixels
        let raw_pix = vec![
            134, 122, 131, 255, 131, 131, 139, 255, 135, 134, 137, 255, 138, 134, 130,
            255, 126, 125, 119, 255, 131, 134, 129, 255, 137, 134, 132, 255, 130, 126,
            130, 255, 132, 125, 132, 255, 122, 142, 129, 255, 134, 135, 128, 255, 138,
            120, 125, 255, 125, 134, 110, 255, 121, 122, 137, 255, 141, 140, 141, 255,
            125, 144, 120, 255,
        ];

        let correct_pix = vec![
            131, 122, 134, 255, 139, 131, 131, 255, 137, 134, 135, 255, 130, 134, 138,
            255, 119, 125, 126, 255, 129, 134, 131, 255, 132, 134, 137, 255, 130, 126,
            130, 255, 132, 125, 132, 255, 129, 142, 122, 255, 128, 135, 134, 255, 125,
            120, 138, 255, 110, 134, 125, 255, 137, 122, 121, 255, 141, 140, 141, 255,
            120, 144, 125, 255,
        ];

        let mut photon_image = PhotonImage::new(raw_pix, width, height);
        swap_channels(&mut photon_image, 0, 2);
        assert_eq!(photon_image.raw_pixels, correct_pix);
    }

    #[test]
    fn test_swap_green_red_channels() {
        let width = 4;
        let height = 4;
        // Create an image from a vec of pixels
        let raw_pix = vec![
            134, 122, 131, 255, 131, 131, 139, 255, 135, 134, 137, 255, 138, 134, 130,
            255, 126, 125, 119, 255, 131, 134, 129, 255, 137, 134, 132, 255, 130, 126,
            130, 255, 132, 125, 132, 255, 122, 142, 129, 255, 134, 135, 128, 255, 138,
            120, 125, 255, 125, 134, 110, 255, 121, 122, 137, 255, 141, 140, 141, 255,
            125, 144, 120, 255,
        ];

        let correct_pix = vec![
            122, 134, 131, 255, 131, 131, 139, 255, 134, 135, 137, 255, 134, 138, 130,
            255, 125, 126, 119, 255, 134, 131, 129, 255, 134, 137, 132, 255, 126, 130,
            130, 255, 125, 132, 132, 255, 142, 122, 129, 255, 135, 134, 128, 255, 120,
            138, 125, 255, 134, 125, 110, 255, 122, 121, 137, 255, 140, 141, 141, 255,
            144, 125, 120, 255,
        ];

        let mut photon_image = PhotonImage::new(raw_pix, width, height);
        swap_channels(&mut photon_image, 1, 0);
        assert_eq!(photon_image.raw_pixels, correct_pix);
    }

    #[test]
    fn test_hsluv_bypass() {
        let width = 4;
        let height = 4;
        // Create an image from a vec of pixels
        let raw_pix = vec![
            134, 122, 131, 255, 131, 131, 139, 255, 135, 134, 137, 255, 138, 134, 130,
            255, 126, 125, 119, 255, 131, 134, 129, 255, 137, 134, 132, 255, 130, 126,
            130, 255, 132, 125, 132, 255, 122, 142, 129, 255, 134, 135, 128, 255, 138,
            120, 125, 255, 125, 134, 110, 255, 121, 122, 137, 255, 141, 140, 141, 255,
            125, 144, 120, 255,
        ];

        let correct_pix = vec![
            134, 122, 131, 255, 131, 131, 139, 255, 135, 134, 137, 255, 138, 134, 130,
            255, 126, 125, 119, 255, 131, 134, 129, 255, 137, 134, 132, 255, 130, 126,
            130, 255, 132, 125, 132, 255, 122, 142, 129, 255, 134, 135, 128, 255, 138,
            120, 125, 255, 125, 134, 110, 255, 121, 122, 137, 255, 141, 140, 141, 255,
            125, 144, 120, 255,
        ];

        let mut photon_image = PhotonImage::new(raw_pix, width, height);
        hue_rotate_hsluv(&mut photon_image, 0.0);
        let photon_result: ImageBuffer<image::Rgba<u8>, Vec<u8>> =
            ImageBuffer::from_raw(width, height, photon_image.get_raw_pixels())
                .expect("Test failed");
        let correct_image: ImageBuffer<image::Rgba<u8>, Vec<u8>> =
            ImageBuffer::from_raw(width, height, correct_pix).expect("Test failed");
        imageproc::assert_pixels_eq_within!(photon_result, correct_image, 1);
    }

    #[test]
    fn test_hsl_bypass() {
        let width = 4;
        let height = 4;
        // Create an image from a vec of pixels
        let raw_pix = vec![
            134, 122, 131, 255, 131, 131, 139, 255, 135, 134, 137, 255, 138, 134, 130,
            255, 126, 125, 119, 255, 131, 134, 129, 255, 137, 134, 132, 255, 130, 126,
            130, 255, 132, 125, 132, 255, 122, 142, 129, 255, 134, 135, 128, 255, 138,
            120, 125, 255, 125, 134, 110, 255, 121, 122, 137, 255, 141, 140, 141, 255,
            125, 144, 120, 255,
        ];

        let correct_pix = vec![
            134, 122, 131, 255, 131, 131, 139, 255, 135, 134, 137, 255, 138, 134, 130,
            255, 126, 125, 119, 255, 131, 134, 129, 255, 137, 134, 132, 255, 130, 126,
            130, 255, 132, 125, 132, 255, 122, 142, 129, 255, 134, 135, 128, 255, 138,
            120, 125, 255, 125, 134, 110, 255, 121, 122, 137, 255, 141, 140, 141, 255,
            125, 144, 120, 255,
        ];

        let mut photon_image = PhotonImage::new(raw_pix, width, height);
        hue_rotate_hsl(&mut photon_image, 0.0);
        let photon_result: ImageBuffer<image::Rgba<u8>, Vec<u8>> =
            ImageBuffer::from_raw(width, height, photon_image.get_raw_pixels())
                .expect("Test failed");
        let correct_image: ImageBuffer<image::Rgba<u8>, Vec<u8>> =
            ImageBuffer::from_raw(width, height, correct_pix).expect("Test failed");
        imageproc::assert_pixels_eq_within!(photon_result, correct_image, 1);
    }

    #[test]
    fn test_hsv_bypass() {
        let width = 4;
        let height = 4;
        // Create an image from a vec of pixels
        let raw_pix = vec![
            134, 122, 131, 255, 131, 131, 139, 255, 135, 134, 137, 255, 138, 134, 130,
            255, 126, 125, 119, 255, 131, 134, 129, 255, 137, 134, 132, 255, 130, 126,
            130, 255, 132, 125, 132, 255, 122, 142, 129, 255, 134, 135, 128, 255, 138,
            120, 125, 255, 125, 134, 110, 255, 121, 122, 137, 255, 141, 140, 141, 255,
            125, 144, 120, 255,
        ];

        let correct_pix = vec![
            134, 122, 131, 255, 131, 131, 139, 255, 135, 134, 137, 255, 138, 134, 130,
            255, 126, 125, 119, 255, 131, 134, 129, 255, 137, 134, 132, 255, 130, 126,
            130, 255, 132, 125, 132, 255, 122, 142, 129, 255, 134, 135, 128, 255, 138,
            120, 125, 255, 125, 134, 110, 255, 121, 122, 137, 255, 141, 140, 141, 255,
            125, 144, 120, 255,
        ];

        let mut photon_image = PhotonImage::new(raw_pix, width, height);
        hue_rotate_hsv(&mut photon_image, 0.0);
        let photon_result: ImageBuffer<image::Rgba<u8>, Vec<u8>> =
            ImageBuffer::from_raw(width, height, photon_image.get_raw_pixels())
                .expect("Test failed");
        let correct_image: ImageBuffer<image::Rgba<u8>, Vec<u8>> =
            ImageBuffer::from_raw(width, height, correct_pix).expect("Test failed");
        imageproc::assert_pixels_eq_within!(photon_result, correct_image, 1);
    }

    #[test]
    fn test_lch_bypass() {
        let width = 4;
        let height = 4;
        // Create an image from a vec of pixels
        let raw_pix = vec![
            134, 122, 131, 255, 131, 131, 139, 255, 135, 134, 137, 255, 138, 134, 130,
            255, 126, 125, 119, 255, 131, 134, 129, 255, 137, 134, 132, 255, 130, 126,
            130, 255, 132, 125, 132, 255, 122, 142, 129, 255, 134, 135, 128, 255, 138,
            120, 125, 255, 125, 134, 110, 255, 121, 122, 137, 255, 141, 140, 141, 255,
            125, 144, 120, 255,
        ];

        let correct_pix: Vec<u8> = vec![
            134, 122, 131, 255, 131, 131, 139, 255, 135, 134, 137, 255, 138, 134, 130,
            255, 126, 125, 119, 255, 131, 134, 129, 255, 137, 134, 132, 255, 130, 126,
            130, 255, 132, 125, 132, 255, 122, 142, 129, 255, 134, 135, 128, 255, 138,
            120, 125, 255, 125, 134, 110, 255, 121, 122, 137, 255, 141, 140, 141, 255,
            125, 144, 120, 255,
        ];

        let mut photon_image = PhotonImage::new(raw_pix, width, height);
        hue_rotate_lch(&mut photon_image, 0.0);
        let photon_result: ImageBuffer<image::Rgba<u8>, Vec<u8>> =
            ImageBuffer::from_raw(width, height, photon_image.get_raw_pixels())
                .expect("Test failed");
        let correct_image: ImageBuffer<image::Rgba<u8>, Vec<u8>> =
            ImageBuffer::from_raw(width, height, correct_pix).expect("Test failed");
        imageproc::assert_pixels_eq_within!(photon_result, correct_image, 1);
    }

    #[test]
    fn test_seam_carve() {
        let width = 4_u32;
        let height = 4_u32;
        // Create an image from a vec of pixels
        let raw_pix: Vec<u8> = vec![
            134, 122, 131, 255, 131, 131, 139, 255, 135, 134, 137, 255, 138, 134, 130,
            255, 126, 125, 119, 255, 131, 134, 129, 255, 137, 134, 132, 255, 130, 126,
            130, 255, 132, 125, 132, 255, 122, 142, 129, 255, 134, 135, 128, 255, 138,
            120, 125, 255, 125, 134, 110, 255, 121, 122, 137, 255, 141, 140, 141, 255,
            125, 144, 120, 255,
        ];

        let correct_pix: Vec<u8> = vec![
            132, 125, 132, 255, 131, 134, 129, 255, 134, 135, 128, 255, 125, 134, 110,
            255, 121, 122, 137, 255, 125, 144, 120, 255,
        ];

        let photon_image: PhotonImage = PhotonImage::new(raw_pix.clone(), width, height);
        {
            // Original image
            assert_eq!(photon_image.get_width(), width);
            assert_eq!(photon_image.get_height(), height);
        }
        {
            // Un-carved image
            // Will return the same image
            let result: PhotonImage = seam_carve(&photon_image, 100_u32, 100_u32);
            assert_eq!(result.get_width(), width);
            assert_eq!(result.get_height(), height);
            assert_eq!(result.get_raw_pixels(), raw_pix);
        }
        {
            // Carved Image, from 4x4 --> 3x2
            let new_w = 3_u32;
            let new_h = 2_u32;
            let result: PhotonImage = seam_carve(&photon_image, new_w, new_h);
            assert_eq!(result.get_width(), new_w);
            assert_eq!(result.get_height(), new_h);
            assert_eq!(result.get_raw_pixels(), correct_pix);
        }
    }

    #[test]
    fn test_resample() {
        let width = 320;
        let height = 240;
        let channels = 4;
        // Create an image from a vec of pixels
        let total_size = width * height * channels;
        let raw_pix: Vec<u8> = std::iter::repeat(127)
            .take(total_size as usize)
            .collect::<Vec<_>>();

        let photon_image: PhotonImage = PhotonImage::new(raw_pix.clone(), width, height);
        {
            // Resample to the same size.
            // Will return the same image.
            let result: PhotonImage =
                resample(&photon_image, width as usize, height as usize);
            assert_eq!(result.get_width(), width);
            assert_eq!(result.get_height(), height);
            assert_eq!(result.get_raw_pixels(), raw_pix);
        }
        {
            // Upsample width and upsample height.
            let new_w: usize = 640;
            let new_h: usize = 480;
            let channels = 4;
            let new_size = new_w * new_h * channels;
            let correct_pix: Vec<u8> =
                std::iter::repeat(127).take(new_size).collect::<Vec<_>>();
            let result: PhotonImage = resample(&photon_image, new_w, new_h);
            assert_eq!(result.get_width(), new_w as u32);
            assert_eq!(result.get_height(), new_h as u32);
            assert_eq!(result.get_raw_pixels(), correct_pix);
        }
        {
            // Downsample width and downsample height.
            let new_w: usize = 160;
            let new_h: usize = 120;
            let channels = 4;
            let new_size = new_w * new_h * channels;
            let correct_pix: Vec<u8> =
                std::iter::repeat(127).take(new_size).collect::<Vec<_>>();
            let result: PhotonImage = resample(&photon_image, new_w, new_h);
            assert_eq!(result.get_width(), new_w as u32);
            assert_eq!(result.get_height(), new_h as u32);
            assert_eq!(result.get_raw_pixels(), correct_pix);
        }
        {
            // Downsample width and upsample height.
            let new_w: usize = 160;
            let new_h: usize = 320;
            let channels = 4;
            let new_size = new_w * new_h * channels;
            let correct_pix: Vec<u8> =
                std::iter::repeat(127).take(new_size).collect::<Vec<_>>();
            let result: PhotonImage = resample(&photon_image, new_w, new_h);
            assert_eq!(result.get_width(), new_w as u32);
            assert_eq!(result.get_height(), new_h as u32);
            assert_eq!(result.get_raw_pixels(), correct_pix);
        }
        {
            // Upsample width and downsample height.
            let new_w: usize = 320;
            let new_h: usize = 120;
            let channels = 4;
            let new_size = new_w * new_h * channels;
            let correct_pix: Vec<u8> =
                std::iter::repeat(127).take(new_size).collect::<Vec<_>>();
            let result: PhotonImage = resample(&photon_image, new_w, new_h);
            assert_eq!(result.get_width(), new_w as u32);
            assert_eq!(result.get_height(), new_h as u32);
            assert_eq!(result.get_raw_pixels(), correct_pix);
        }
    }
}