Skip to content

Commit 5ff97e0

Browse files
committed
Add some encoder tests
Signed-off-by: Marc-André Lureau <marcandre.lureau@redhat.com>
1 parent 742a571 commit 5ff97e0

File tree

1 file changed

+74
-1
lines changed

1 file changed

+74
-1
lines changed

tests/test_misc.rs

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use qoi::{
44
consts::{QOI_OP_INDEX, QOI_OP_RGB, QOI_OP_RUN},
5-
decode_to_vec, Channels, ColorSpace, Decoder, Header, Result,
5+
decode_to_vec, Channels, ColorSpace, Decoder, Error, Header, RawChannels, Result,
66
};
77

88
const ONE_PIXEL_QOI_IMAGE: [u8; 23] = [
@@ -80,3 +80,76 @@ fn test_big_endian() {
8080
// so we can see it in the CI logs
8181
assert_eq!(u16::to_be_bytes(1), [0, 1]);
8282
}
83+
84+
#[test]
85+
fn test_new_encoder() {
86+
let arr3 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]; // 2 * 2 * 3
87+
let arr4 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; // 2 * 2 * 4
88+
89+
let enc = qoi::Encoder::new(&arr3, 2, 2).unwrap();
90+
assert_eq!(enc.channels(), Channels::Rgb);
91+
92+
let enc = qoi::Encoder::new(&arr4, 2, 2).unwrap();
93+
assert_eq!(enc.channels(), Channels::Rgba);
94+
95+
assert!(matches!(
96+
qoi::Encoder::new(&arr3, 3, 3),
97+
Err(Error::InvalidImageLength { size: 12, width: 3, height: 3 })
98+
));
99+
100+
assert!(matches!(qoi::Encoder::new(&arr3, 1, 1), Err(Error::InvalidChannels { channels: 12 })));
101+
102+
let enc = qoi::Encoder::new_raw(&arr3, 2, 2, 2 * 3, RawChannels::Bgr).unwrap();
103+
assert_eq!(enc.channels(), Channels::Rgb);
104+
let qoi = enc.encode_to_vec().unwrap();
105+
let (_header, res) = decode_to_vec(qoi).unwrap();
106+
assert_eq!(res, [2, 1, 0, 5, 4, 3, 8, 7, 6, 11, 10, 9]);
107+
108+
let enc = qoi::Encoder::new_raw(&arr4, 2, 2, 2 * 4, RawChannels::Rgba).unwrap();
109+
assert_eq!(enc.channels(), Channels::Rgba);
110+
let qoi = enc.encode_to_vec().unwrap();
111+
let (_header, res) = decode_to_vec(qoi).unwrap();
112+
assert_eq!(res, arr4);
113+
114+
let enc = qoi::Encoder::new_raw(&arr4, 2, 2, 2 * 4, RawChannels::Bgra).unwrap();
115+
assert_eq!(enc.channels(), Channels::Rgba);
116+
let qoi = enc.encode_to_vec().unwrap();
117+
let (_header, res) = decode_to_vec(qoi).unwrap();
118+
assert_eq!(res, [2, 1, 0, 3, 6, 5, 4, 7, 10, 9, 8, 11, 14, 13, 12, 15]);
119+
120+
let enc = qoi::Encoder::new_raw(&arr4, 2, 2, 2 * 4, RawChannels::Rgbx).unwrap();
121+
assert_eq!(enc.channels(), Channels::Rgb);
122+
let qoi = enc.encode_to_vec().unwrap();
123+
let (_header, res) = decode_to_vec(qoi).unwrap();
124+
assert_eq!(res, [0, 1, 2, 4, 5, 6, 8, 9, 10, 12, 13, 14]);
125+
126+
let enc = qoi::Encoder::new_raw(&arr4, 2, 2, 2 * 4, RawChannels::Xrgb).unwrap();
127+
assert_eq!(enc.channels(), Channels::Rgb);
128+
let qoi = enc.encode_to_vec().unwrap();
129+
let (_header, res) = decode_to_vec(qoi).unwrap();
130+
assert_eq!(res, [1, 2, 3, 5, 6, 7, 9, 10, 11, 13, 14, 15]);
131+
132+
let enc = qoi::Encoder::new_raw(&arr4, 2, 2, 2 * 4, RawChannels::Bgra).unwrap();
133+
assert_eq!(enc.channels(), Channels::Rgba);
134+
let qoi = enc.encode_to_vec().unwrap();
135+
let (_header, res) = decode_to_vec(qoi).unwrap();
136+
assert_eq!(res, [2, 1, 0, 3, 6, 5, 4, 7, 10, 9, 8, 11, 14, 13, 12, 15]);
137+
138+
let enc = qoi::Encoder::new_raw(&arr4, 2, 2, 2 * 4, RawChannels::Abgr).unwrap();
139+
assert_eq!(enc.channels(), Channels::Rgba);
140+
let qoi = enc.encode_to_vec().unwrap();
141+
let (_header, res) = decode_to_vec(qoi).unwrap();
142+
assert_eq!(res, [3, 2, 1, 0, 7, 6, 5, 4, 11, 10, 9, 8, 15, 14, 13, 12]);
143+
144+
let enc = qoi::Encoder::new_raw(&arr4, 2, 2, 2 * 4, RawChannels::Bgrx).unwrap();
145+
assert_eq!(enc.channels(), Channels::Rgb);
146+
let qoi = enc.encode_to_vec().unwrap();
147+
let (_header, res) = decode_to_vec(qoi).unwrap();
148+
assert_eq!(res, [2, 1, 0, 6, 5, 4, 10, 9, 8, 14, 13, 12]);
149+
150+
let enc = qoi::Encoder::new_raw(&arr4, 2, 2, 2 * 4, RawChannels::Xbgr).unwrap();
151+
assert_eq!(enc.channels(), Channels::Rgb);
152+
let qoi = enc.encode_to_vec().unwrap();
153+
let (_header, res) = decode_to_vec(qoi).unwrap();
154+
assert_eq!(res, [3, 2, 1, 7, 6, 5, 11, 10, 9, 15, 14, 13]);
155+
}

0 commit comments

Comments
 (0)