fast image operations
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
//! Manages the affine image transformations.
use crate::{Image, cloner::ImageCloner};

impl<const CHANNELS: usize> ImageCloner<'_, CHANNELS> {
    /// Flip an image vertically.
    /// ```
    /// # use fimg::Image;
    /// let a = Image::<_, 1>::build(2,2).buf(vec![21,42,90,01]);
    /// assert_eq!(a.cloner().flip_v().take_buffer(), [90,01,21,42]);
    /// ```
    #[must_use = "function does not modify the original image"]
    pub fn flip_v(&self) -> Image<Vec<u8>, CHANNELS> {
        let mut out = self.uninit();
        for y in 0..self.height() {
            for x in 0..self.width() {
                // SAFETY: looping over self, all ok (could be safe versions, bounds would be elided)
                let p = unsafe { self.pixel(x, y) };
                // SAFETY: looping over self.
                unsafe { out.write(p, (x, self.height() - y - 1)) };
            }
        }
        // SAFETY: init
        unsafe { out.assume_init() }
    }

    /// Flip an image horizontally
    /// ```
    /// # use fimg::Image;
    /// let a = Image::<_,1>::build(2,2).buf(vec![90,01,21,42]);
    /// assert_eq!(a.cloner().flip_h().take_buffer(), [01,90,42,21]);
    /// ```
    #[must_use = "function does not modify the original image"]
    pub fn flip_h(&self) -> Image<Vec<u8>, CHANNELS> {
        let mut out = self.uninit();
        for y in 0..self.height() {
            for x in 0..self.width() {
                // SAFETY: looping over self, all ok
                let p = unsafe { self.pixel(x, y) };
                // SAFETY: looping over self, all ok
                unsafe { out.write(p, (self.width() - x - 1, y)) };
            }
        }
        // SAFETY: init
        unsafe { out.assume_init() }
    }
}

impl<const CHANNELS: usize, T: AsMut<[u8]> + AsRef<[u8]>> Image<T, CHANNELS> {
    /// Flip an image vertically.
    pub fn flip_v(&mut self) {
        for y in 0..self.height() / 2 {
            for x in 0..self.width() {
                let y2 = self.height() - y - 1;
                #[allow(clippy::multiple_unsafe_ops_per_block)]
                // SAFETY: within bounds
                unsafe {
                    self.swap_pixel((x, y2), (x, y))
                }
            }
        }
    }

    /// Flip an image horizontally.
    pub fn flip_h(&mut self) {
        for y in 0..self.height() {
            for x in 0..self.width() / 2 {
                let x2 = self.width() - x - 1;
                #[allow(clippy::multiple_unsafe_ops_per_block)]
                // SAFETY: bounded
                unsafe {
                    self.swap_pixel((x2, y), (x, y))
                }
            }
        }
    }
}

impl<const CHANNELS: usize> ImageCloner<'_, CHANNELS> {
    /// Rotate an image 180 degrees clockwise.
    ///
    /// ```
    /// # use fimg::Image;
    /// let a = Image::<_,1>::build(2,2).buf(vec![00,01,02,10]);
    /// assert_eq!(a.cloner().rot_180().take_buffer(), vec![10,02,01,00]);
    /// ```
    #[must_use = "function does not modify the original image"]
    pub fn rot_180(&self) -> Image<Vec<u8>, CHANNELS> {
        let s = (self.width() * self.height()) as usize;
        let mut v: Vec<[u8; CHANNELS]> = Vec::with_capacity(s);
        for (x, y) in self.chunked().rev().zip(&mut v.spare_capacity_mut()[..]) {
            y.write(*x);
        }
        // SAFETY: we just wrote the right amount
        unsafe { v.set_len(s) };
        Image::build(self.width(), self.height()).buf(v.into_flattened())
    }

    /// Rotate an image 90 degrees clockwise.
    /// # Safety
    ///
    /// UB if the image is not square
    #[must_use = "function does not modify the original image"]
    pub unsafe fn rot_90(&self) -> Image<Vec<u8>, CHANNELS> {
        // SAFETY: yep
        let mut out = unsafe { transpose_out(self) };
        // SAFETY: sqar
        unsafe { crev(out.as_mut()) };
        out
    }

    /// Rotate an image 270 degrees clockwise, or 90 degrees anti clockwise.
    /// # Safety
    ///
    /// UB if the image is not square
    #[must_use = "function does not modify the original image"]
    pub unsafe fn rot_270(&self) -> Image<Vec<u8>, CHANNELS> {
        // SAFETY: yep
        let mut out = unsafe { transpose_out(self) };
        out.flip_v();
        out
    }
}

impl<const CHANNELS: usize, T: AsMut<[u8]> + AsRef<[u8]>> Image<T, CHANNELS> {
    /// Rotate an image 180 degrees clockwise.
    pub fn rot_180(&mut self) {
        self.flatten_mut().reverse();
    }

    /// Rotate an image 90 degrees clockwise.
    /// # Safety
    ///
    /// UB if the image is not square
    #[inline]
    pub unsafe fn rot_90(&mut self) {
        // This is done by first flipping
        self.flip_v();
        // Then transposing the image, as to not allocate.
        // SAFETY: caller ensures square
        unsafe { transpose(self) };
    }

    /// Rotate an image 270 degrees clockwise, or 90 degrees anti clockwise.
    /// # Safety
    ///
    /// UB if the image is not square
    #[inline]
    pub unsafe fn rot_270(&mut self) {
        self.flip_h();
        // SAFETY: caller ensures squareness
        unsafe { transpose(self) };
    }
}

/// Reverse columns of square image
/// # Safety
///
/// UB if supplied image not square
unsafe fn crev<const CHANNELS: usize, T: AsMut<[u8]> + AsRef<[u8]>>(mut img: Image<T, CHANNELS>) {
    debug_assert_eq!(img.width(), img.height());
    let size = img.width() as usize;
    let b = img.flatten_mut();
    for i in 0..size {
        let mut start = 0;
        let mut end = size - 1;
        while start < end {
            // SAFETY: hmm
            unsafe { b.swap_unchecked(i * size + start, i * size + end) };
            start += 1;
            end -= 1;
        }
    }
}

/// Transpose a square image out of place
/// # Safety
///
/// UB if provided image rectangular
unsafe fn transpose_out<const CHANNELS: usize>(
    i: &ImageCloner<'_, CHANNELS>,
) -> Image<Vec<u8>, CHANNELS> {
    let mut out = i.alloc();
    // SAFETY: yep
    unsafe {
        mattr::transpose(
            i.flatten(),
            out.flatten_mut(),
            i.height() as usize,
            i.width() as usize,
        )
    };
    out
}

/// Transpose a square image
/// # Safety
///
/// UB if supplied image rectangular
unsafe fn transpose<const CHANNELS: usize, T: AsMut<[u8]> + AsRef<[u8]>>(
    img: &mut Image<T, CHANNELS>,
) {
    debug_assert_eq!(img.width(), img.height());
    if img.width().is_power_of_two() {
        // SAFETY: caller guarantees
        unsafe { transpose_diag(img, 0, img.width() as usize) };
    } else {
        // SAFETY: caller guarantees
        unsafe { transpose_non_power_of_two(img) };
    }
}

/// Transpose a square (non power of two) image.
///
/// # Safety
///
/// UB if image not square
unsafe fn transpose_non_power_of_two<const CHANNELS: usize, T: AsMut<[u8]> + AsRef<[u8]>>(
    img: &mut Image<T, CHANNELS>,
) {
    debug_assert_eq!(img.width(), img.height());
    let size = img.width() as usize;
    let b = img.flatten_mut();
    for i in 0..size {
        for j in i..size {
            // SAFETY: caller ensures squarity
            unsafe { b.swap_unchecked(i * size + j, j * size + i) };
        }
    }
}

/// break it down until
const TILE: usize = 4;
/// # Safety
///
/// be careful
unsafe fn transpose_tile<const CHANNELS: usize, T: AsMut<[u8]> + AsRef<[u8]>>(
    img: &mut Image<T, CHANNELS>,
    row: usize,
    col: usize,
    size: usize,
) {
    if size > TILE {
        #[allow(
            clippy::multiple_unsafe_ops_per_block,
            clippy::undocumented_unsafe_blocks
        )]
        unsafe {
            // top left
            transpose_tile(img, row, col, size / 2);
            // top right
            transpose_tile(img, row, col + size / 2, size / 2);
            // bottom left
            transpose_tile(img, row + size / 2, col, size / 2);
            // bottom right
            transpose_tile(img, row + size / 2, col + size / 2, size / 2);
        }
    } else {
        let s = img.width() as usize;
        let b = img.flatten_mut();
        for i in 0..size {
            for j in 0..size {
                // SAFETY: this should be okay if we careful
                unsafe { b.swap_unchecked((row + i) * s + (col + j), (col + j) * s + (row + i)) };
            }
        }
    }
}

/// # Safety
///
/// be careful
unsafe fn transpose_diag<const CHANNELS: usize, T: AsMut<[u8]> + AsRef<[u8]>>(
    img: &mut Image<T, CHANNELS>,
    pos: usize,
    size: usize,
) {
    if size > TILE {
        #[allow(
            clippy::multiple_unsafe_ops_per_block,
            clippy::undocumented_unsafe_blocks
        )]
        unsafe {
            transpose_diag(img, pos, size / 2);
            transpose_tile(img, pos, pos + size / 2, size / 2);
            transpose_diag(img, pos + size / 2, size / 2);
        }
    } else {
        let s = img.width() as usize;
        let b = img.flatten_mut();
        for i in 1..size {
            for j in 0..i {
                // SAFETY: this is fine unless pos / size is out of bounds, which it cant be
                unsafe { b.swap_unchecked((pos + i) * s + (pos + j), (pos + j) * s + (pos + i)) };
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::img;

    #[test]
    fn transp() {
        #[rustfmt::skip]
        let mut i = Image::<_, 1>::build(8, 8).buf(vec![
            0, 0, 1, 1, 0, 0, 1, 1,
            0, 1, 0, 1, 1, 0, 1, 1,
            0, 1, 1, 0, 1, 0, 1, 1,
            0, 1, 1, 1, 0, 0, 1, 1,
            0, 1, 1, 1, 1, 0, 1, 1,
            0, 1, 1, 1, 1, 0, 0, 1,
            0, 1, 1, 1, 1, 0, 1, 0,
            0, 0, 1, 1, 1, 0, 1, 1,
        ]);
        unsafe { transpose(&mut i.as_mut()) };
        #[rustfmt::skip]
        assert_eq!(i.take_buffer(), vec![
            0, 0, 0, 0, 0, 0, 0, 0,
            0, 1, 1, 1, 1, 1, 1, 0,
            1, 0, 1, 1, 1, 1, 1, 1,
            1, 1, 0, 1, 1, 1, 1, 1,
            0, 1, 1, 0, 1, 1, 1, 1,
            0, 0, 0, 0, 0, 0, 0, 0,
            1, 1, 1, 1, 1, 0, 1, 1,
            1, 1, 1, 1, 1, 1, 0, 1
        ]);
    }

    #[test]
    fn transp9() {
        #[rustfmt::skip]
        let mut i = Image::<_, 1>::build(9, 9).buf(vec![
            0, 0, 1, 1, 0, 0, 1, 1, 0,
            0, 1, 0, 1, 1, 0, 1, 1, 1,
            0, 1, 1, 0, 1, 0, 1, 1, 0,
            0, 1, 1, 1, 0, 0, 1, 1, 0,
            0, 1, 1, 1, 1, 0, 1, 1, 1,
            0, 1, 1, 1, 1, 0, 0, 1, 1,
            0, 1, 1, 1, 1, 0, 1, 0, 1,
            0, 0, 1, 1, 1, 0, 1, 1, 0,
            1, 1, 1, 0, 1, 1, 0, 1, 0,
        ]);
        unsafe { transpose(&mut i.as_mut()) };
        #[rustfmt::skip]
        assert_eq!(i.take_buffer(), vec![
            0, 0, 0, 0, 0, 0, 0, 0, 1,
            0, 1, 1, 1, 1, 1, 1, 0, 1,
            1, 0, 1, 1, 1, 1, 1, 1, 1,
            1, 1, 0, 1, 1, 1, 1, 1, 0,
            0, 1, 1, 0, 1, 1, 1, 1, 1,
            0, 0, 0, 0, 0, 0, 0, 0, 1,
            1, 1, 1, 1, 1, 0, 1, 1, 0,
            1, 1, 1, 1, 1, 1, 0, 1, 1,
            0, 1, 0, 0, 1, 1, 1, 0, 0
        ]);
    }

    #[test]
    fn rotate_90() {
        let mut from = img![
            [00, 01]
            [02, 10]
        ];
        unsafe { from.rot_90() };
        assert_eq!(
            from,
            img![
                [02, 00]
                [10, 01]
            ]
        );
    }

    #[test]
    fn rotate_180() {
        let mut from = img![
            [00, 01]
            [02, 10]
        ];
        from.rot_180();
        assert_eq!(
            from,
            img![
                [10, 02]
                [01, 00]
            ]
        );
    }

    #[test]
    fn rotate_270() {
        let mut from = img![
            [00, 01]
            [20, 10]
        ];
        unsafe { from.rot_270() };
        assert_eq!(
            from,
            img![
                [01, 10]
                [00, 20]
            ]
        );
    }

    #[test]
    fn flip_vertical() {
        let mut from = img![
            [90, 01]
            [21, 42]
        ];
        from.flip_v();
        assert_eq!(
            from,
            img![
                [21, 42]
                [90, 01]
            ]
        );
    }
    #[test]
    fn flip_horizontal() {
        let mut from = img![
            [90, 01]
            [21, 42]
        ];
        from.flip_h();
        assert_eq!(
            from,
            img![
                [01, 90]
                [42, 21]
            ]
        );
    }
}