type Source = fn(usize, usize, usize, usize) -> (usize, usize);
#[derive(Clone, Debug, PartialEq, Eq)]
pub(super) struct Canvas {
pub(super) width: usize,
pub(super) height: usize,
pub(super) channels: usize,
pub(super) bytes: Vec<u8>,
}
impl Canvas {
pub(super) fn oriented(self, orientation: u64) -> Self {
let (w, h) = (self.width, self.height);
let (out_w, out_h, source): (usize, usize, Source) = match orientation {
2 => (w, h, |x, y, w, _| (w - 1 - x, y)),
3 => (w, h, |x, y, w, h| (w - 1 - x, h - 1 - y)),
4 => (w, h, |x, y, _, h| (x, h - 1 - y)),
5 => (h, w, |x, y, _, _| (y, x)),
6 => (h, w, |x, y, _, h| (y, h - 1 - x)),
7 => (h, w, |x, y, w, h| (w - 1 - y, h - 1 - x)),
8 => (h, w, |x, y, w, _| (w - 1 - y, x)),
_ => return self,
};
let channels = self.channels;
let mut bytes = Vec::with_capacity(self.bytes.len());
for y in 0..out_h {
for x in 0..out_w {
let (sx, sy) = source(x, y, w, h);
let at = (sy * w + sx) * channels;
bytes.extend_from_slice(&self.bytes[at..at + channels]);
}
}
Self {
width: out_w,
height: out_h,
channels,
bytes,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn canvas() -> Canvas {
Canvas {
width: 3,
height: 2,
channels: 1,
bytes: vec![1, 2, 3, 4, 5, 6],
}
}
#[test]
fn every_orientation() {
let cases: [(u64, (usize, usize), [u8; 6]); 8] = [
(1, (3, 2), [1, 2, 3, 4, 5, 6]),
(2, (3, 2), [3, 2, 1, 6, 5, 4]),
(3, (3, 2), [6, 5, 4, 3, 2, 1]),
(4, (3, 2), [4, 5, 6, 1, 2, 3]),
(5, (2, 3), [1, 4, 2, 5, 3, 6]),
(6, (2, 3), [4, 1, 5, 2, 6, 3]),
(7, (2, 3), [6, 3, 5, 2, 4, 1]),
(8, (2, 3), [3, 6, 2, 5, 1, 4]),
];
for (orientation, size, bytes) in cases {
let turned = canvas().oriented(orientation);
assert_eq!((turned.width, turned.height), size, "{orientation}");
assert_eq!(turned.bytes, bytes, "{orientation}");
}
}
}