Debian Codegraph @

.cargo/vendor/qoi/tests/test_chunks.rs

raw
1 mod common;
2
3 use bytemuck::{cast_slice, Pod};
4
5 use qoi::consts::{
6 QOI_HEADER_SIZE, QOI_OP_DIFF, QOI_OP_INDEX, QOI_OP_LUMA, QOI_OP_RGB, QOI_OP_RGBA, QOI_OP_RUN,
7 QOI_PADDING_SIZE,
8 };
9 use qoi::{decode_to_vec, encode_to_vec};
10
11 use self::common::hash;
12
13 fn test_chunk<P, E, const N: usize>(pixels: P, expected: E)
14 where
15 P: AsRef<[[u8; N]]>,
16 E: AsRef<[u8]>,
17 [u8; N]: Pod,
18 {
19 let pixels = pixels.as_ref();
20 let expected = expected.as_ref();
21 let pixels_raw = cast_slice::<_, u8>(pixels);
22 let encoded = encode_to_vec(pixels_raw, pixels.len() as _, 1).unwrap();
23 let decoded = decode_to_vec(&encoded).unwrap().1;
24 assert_eq!(pixels_raw, decoded.as_slice(), "roundtrip failed (encoded={:?}))", encoded);
25 assert!(encoded.len() >= expected.len() + QOI_HEADER_SIZE + QOI_PADDING_SIZE);
26 assert_eq!(&encoded[QOI_HEADER_SIZE..][..expected.len()], expected);
27 }
28
29 #[test]
30 fn test_encode_rgb_3ch() {
31 test_chunk([[11, 121, 231]], [QOI_OP_RGB, 11, 121, 231]);
32 }
33
34 #[test]
35 fn test_encode_rgb_4ch() {
36 test_chunk([[11, 121, 231, 0xff]], [QOI_OP_RGB, 11, 121, 231]);
37 }
38
39 #[test]
40 fn test_encode_rgba() {
41 test_chunk([[11, 121, 231, 55]], [QOI_OP_RGBA, 11, 121, 231, 55]);
42 }
43
44 #[test]
45 fn test_encode_run_start_len1to62_3ch() {
46 for n in 1..=62 {
47 let mut v = vec![[0, 0, 0]; n];
48 v.push([11, 22, 33]);
49 test_chunk(v, [QOI_OP_RUN | (n as u8 - 1), QOI_OP_RGB]);
50 }
51 }
52
53 #[test]
54 fn test_encode_run_start_len1to62_4ch() {
55 for n in 1..=62 {
56 let mut v = vec![[0, 0, 0, 0xff]; n];
57 v.push([11, 22, 33, 44]);
58 test_chunk(v, [QOI_OP_RUN | (n as u8 - 1), QOI_OP_RGBA]);
59 }
60 }
61
62 #[test]
63 fn test_encode_run_start_63to124_3ch() {
64 for n in 63..=124 {
65 let mut v = vec![[0, 0, 0]; n];
66 v.push([11, 22, 33]);
67 test_chunk(v, [QOI_OP_RUN | 61, QOI_OP_RUN | (n as u8 - 63), QOI_OP_RGB]);
68 }
69 }
70
71 #[test]
72 fn test_encode_run_start_len63to124_4ch() {
73 for n in 63..=124 {
74 let mut v = vec![[0, 0, 0, 0xff]; n];
75 v.push([11, 22, 33, 44]);
76 test_chunk(v, [QOI_OP_RUN | 61, QOI_OP_RUN | (n as u8 - 63), QOI_OP_RGBA]);
77 }
78 }
79
80 #[test]
81 fn test_encode_run_end_3ch() {
82 let px = [11, 33, 55];
83 test_chunk(
84 [[1, 99, 2], px, px, px],
85 [QOI_OP_RGB, 1, 99, 2, QOI_OP_RGB, px[0], px[1], px[2], QOI_OP_RUN | 1],
86 );
87 }
88
89 #[test]
90 fn test_encode_run_end_4ch() {
91 let px = [11, 33, 55, 77];
92 test_chunk(
93 [[1, 99, 2, 3], px, px, px],
94 [QOI_OP_RGBA, 1, 99, 2, 3, QOI_OP_RGBA, px[0], px[1], px[2], px[3], QOI_OP_RUN | 1],
95 );
96 }
97
98 #[test]
99 fn test_encode_run_mid_3ch() {
100 let px = [11, 33, 55];
101 test_chunk(
102 [[1, 99, 2], px, px, px, [1, 2, 3]],
103 [QOI_OP_RGB, 1, 99, 2, QOI_OP_RGB, px[0], px[1], px[2], QOI_OP_RUN | 1],
104 );
105 }
106
107 #[test]
108 fn test_encode_run_mid_4ch() {
109 let px = [11, 33, 55, 77];
110 test_chunk(
111 [[1, 99, 2, 3], px, px, px, [1, 2, 3, 4]],
112 [QOI_OP_RGBA, 1, 99, 2, 3, QOI_OP_RGBA, px[0], px[1], px[2], px[3], QOI_OP_RUN | 1],
113 );
114 }
115
116 #[test]
117 fn test_encode_index_3ch() {
118 let px = [101, 102, 103];
119 test_chunk(
120 [px, [1, 2, 3], px],
121 [QOI_OP_RGB, 101, 102, 103, QOI_OP_RGB, 1, 2, 3, QOI_OP_INDEX | hash(px)],
122 );
123 }
124
125 #[test]
126 fn test_encode_index_4ch() {
127 let px = [101, 102, 103, 104];
128 test_chunk(
129 [px, [1, 2, 3, 4], px],
130 [QOI_OP_RGBA, 101, 102, 103, 104, QOI_OP_RGBA, 1, 2, 3, 4, QOI_OP_INDEX | hash(px)],
131 );
132 }
133
134 #[test]
135 fn test_encode_index_zero_3ch() {
136 let px = [0, 0, 0];
137 test_chunk([[101, 102, 103], px], [QOI_OP_RGB, 101, 102, 103, QOI_OP_RGB, 0, 0, 0]);
138 }
139
140 #[test]
141 fn test_encode_index_zero_0x00_4ch() {
142 let px = [0, 0, 0, 0];
143 test_chunk(
144 [[101, 102, 103, 104], px],
145 [QOI_OP_RGBA, 101, 102, 103, 104, QOI_OP_INDEX | hash(px)],
146 );
147 }
148
149 #[test]
150 fn test_encode_index_zero_0xff_4ch() {
151 let px = [0, 0, 0, 0xff];
152 test_chunk(
153 [[101, 102, 103, 104], px],
154 [QOI_OP_RGBA, 101, 102, 103, 104, QOI_OP_RGBA, 0, 0, 0, 0xff],
155 );
156 }
157
158 #[test]
159 fn test_encode_diff() {
160 for x in 0..8_u8 {
161 let x = [x.wrapping_sub(5), x.wrapping_sub(4), x.wrapping_sub(3)];
162 for dr in 0..3 {
163 for dg in 0..3 {
164 for db in 0..3 {
165 if dr != 2 || dg != 2 || db != 2 {
166 let r = x[0].wrapping_add(dr).wrapping_sub(2);
167 let g = x[1].wrapping_add(dg).wrapping_sub(2);
168 let b = x[2].wrapping_add(db).wrapping_sub(2);
169 let d = QOI_OP_DIFF | dr << 4 | dg << 2 | db;
170 test_chunk(
171 [[1, 99, 2], x, [r, g, b]],
172 [QOI_OP_RGB, 1, 99, 2, QOI_OP_RGB, x[0], x[1], x[2], d],
173 );
174 test_chunk(
175 [[1, 99, 2, 0xff], [x[0], x[1], x[2], 9], [r, g, b, 9]],
176 [QOI_OP_RGB, 1, 99, 2, QOI_OP_RGBA, x[0], x[1], x[2], 9, d],
177 );
178 }
179 }
180 }
181 }
182 }
183 }
184
185 #[test]
186 fn test_encode_luma() {
187 for x in (0..200_u8).step_by(4) {
188 let x = [x.wrapping_mul(3), x.wrapping_sub(5), x.wrapping_sub(7)];
189 for dr_g in (0..16).step_by(4) {
190 for dg in (0..64).step_by(8) {
191 for db_g in (0..16).step_by(4) {
192 if dr_g != 8 || dg != 32 || db_g != 8 {
193 let r = x[0].wrapping_add(dr_g).wrapping_add(dg).wrapping_sub(40);
194 let g = x[1].wrapping_add(dg).wrapping_sub(32);
195 let b = x[2].wrapping_add(db_g).wrapping_add(dg).wrapping_sub(40);
196 let d1 = QOI_OP_LUMA | dg;
197 let d2 = (dr_g << 4) | db_g;
198 test_chunk(
199 [[1, 99, 2], x, [r, g, b]],
200 [QOI_OP_RGB, 1, 99, 2, QOI_OP_RGB, x[0], x[1], x[2], d1, d2],
201 );
202 test_chunk(
203 [[1, 99, 2, 0xff], [x[0], x[1], x[2], 9], [r, g, b, 9]],
204 [QOI_OP_RGB, 1, 99, 2, QOI_OP_RGBA, x[0], x[1], x[2], 9, d1, d2],
205 );
206 }
207 }
208 }
209 }
210 }
211 }