```  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
```
```//! # Pixel Art Rotation
//!
//! This library allows you to rotate pixel art using the [rotsprite](https://en.wikipedia.org/wiki/Pixel-art_scaling_algorithms#RotSprite) algorithm.

// Make the modules public for benchmarks but don't document it
#[doc(hidden)]
pub mod rotate;
#[doc(hidden)]
pub mod scale2x;

use thiserror::Error;

#[derive(Error, Debug, PartialEq)]
pub enum Error {
#[error("image size doesn't match with supplied width")]
ImageSizeMismatch,
}

/// Rotate a sprite based on any pixel format implementing the `Eq` and `Clone` traits.
///
/// Rotation is in degrees (0-360).
/// The size of the resulting vector will be bigger if the rotation isn't exactly 0.0, 90.0, 180.0 or 270.0 degrees.
/// The width and the height will be swapped at angles of 90.0 and 270.0.
pub fn rotsprite<P>(
buf: &[P],
empty_color: &P,
width: usize,
rotation: f64,
) -> Result<(usize, usize, Vec<P>), Error>
where
P: Eq + Clone,
{
// If there's no rotation we don't have to do anything
if rotation == 0.0 {
return Ok((width, buf.len() / width, buf.to_vec()));
}

let len = buf.len();
if len % width != 0 {
return Err(Error::ImageSizeMismatch);
}
let height = len / width;

// Upscale the image using the scale2x algorithm
// 2x
let (scaled_width, scaled_height, scaled) = scale2x::scale2x(buf, width, height);
// 4x
let (scaled_width, scaled_height, scaled) =
scale2x::scale2x(&scaled, scaled_width, scaled_height);
// 8x
let (scaled_width, scaled_height, scaled) =
scale2x::scale2x(&scaled, scaled_width, scaled_height);

// Rotate the image
let rotated = rotate::rotate(
&scaled,
empty_color,
scaled_width,
scaled_height,
rotation,
8,
);

Ok(rotated)
}

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

#[test]
fn rotation_equality() -> Result<(), Error> {
let buf = [1, 2, 3, 4, 5, 6].to_vec();
assert_eq!(
rotsprite(&buf, &0, 3, 45.0)?,
rotsprite(&buf, &0, 3, 45.0 + 360.0)?
);
assert_eq!(
rotsprite(&buf, &0, 3, 45.0)?,
rotsprite(&buf, &0, 3, 45.0 - 360.0)?
);
assert_eq!(
rotsprite(&buf, &0, 3, 12.0)?,
rotsprite(&buf, &0, 3, 12.0 - 360.0)?
);

Ok(())
}

#[test]
fn rotation_size() -> Result<(), Error> {
let buf = [1, 2, 3, 4, 5, 6].to_vec();
let (w, h, _) = rotsprite(&buf, &0, 3, 45.0)?;
assert_eq!(w, 4);
assert_eq!(h, 4);
let (w, h, _) = rotsprite(&buf, &0, 3, 90.0)?;
assert_eq!(w, 2);
assert_eq!(h, 3);
let (w, h, _) = rotsprite(&buf, &0, 3, 180.0)?;
assert_eq!(w, 3);
assert_eq!(h, 2);

Ok(())
}

#[test]
fn rotation_test() -> Result<(), Error> {
let buf = [1, 2, 3, 4, 5, 6].to_vec();
let (w, h, new) = rotsprite(&buf, &0, 3, 90.0)?;
assert_eq!(w, 2);
assert_eq!(h, 3);
assert_eq!(new, [4, 1, 5, 2, 6, 3]);

Ok(())
}

#[test]
fn no_rotation_test() -> Result<(), Error> {
let buf = [1, 0, 0, 1, 1, 0].to_vec();
let (w, h, new) = rotsprite(&buf, &-1, 2, 0.0)?;
assert_eq!(w, 2);
assert_eq!(h, 3);
assert_eq!(buf, new);

Ok(())
}

#[test]
fn size_mismatch_error_test() {
assert_eq!(
rotsprite(&[0, 0, 0, 0, 0], &-1, 2, 1.0).unwrap_err(),
Error::ImageSizeMismatch
);
}
}
```