OP_DIFF and OP_LUMA roll over and under test cases

I wasn't explicitly testing the wrapping behavior of the diff and luma
op codes.
This commit is contained in:
2024-05-07 09:23:29 -05:00
parent ac4d88eb15
commit 85ad2fa252

View File

@@ -336,6 +336,44 @@ mod test {
assert_eq!(result, expected);
}
#[test]
fn decoder_unpack_diff_rollover() {
let init_pixel = PixelRGBA::new(255, 255, 255, 255);
let compressed = [
(QOI_OP_DIFF | 0b0011_1111), // +1s
(QOI_OP_DIFF | 0b0011_1111), // +1s
(QOI_OP_DIFF | 0b0010_1010), // +0s, could have been an index or a run, probably
];
let expected = [
PixelRGBA::new(0, 0, 0, 255), // 255 rollover to 0
PixelRGBA::new(1, 1, 1, 255), // +1s
PixelRGBA::new(1, 1, 1, 255), // holds at 1s
];
let decoder = Decoder::new_with_previous_pixel(compressed.into_iter(), init_pixel);
let result: Vec<PixelRGBA> = decoder.collect();
assert_eq!(result, expected);
}
#[test]
fn decoder_unpack_diff_rollunder() {
let init_pixel = PixelRGBA::new(0, 0, 0, 255);
let compressed = [
(QOI_OP_DIFF | 0b0001_0101), // -1s
(QOI_OP_DIFF | 0b0001_0101), // -1s
(QOI_OP_DIFF | 0b0010_1010), // 0s
];
let expected = [
PixelRGBA::new(255, 255, 255, 255),
PixelRGBA::new(254, 254, 254, 255),
PixelRGBA::new(254, 254, 254, 255),
];
let decoder = Decoder::new_with_previous_pixel(compressed.into_iter(), init_pixel);
let result: Vec<PixelRGBA> = decoder.collect();
assert_eq!(result, expected);
}
#[test]
fn decoder_unpack_luma() {
// red and blue diffs are relative to the green channel as (dr - dg) and (db - dg)
@@ -364,6 +402,32 @@ mod test {
assert_eq!(result, expected);
}
#[test]
fn decoder_unpack_luma_rollover() {
let init_pixel = PixelRGBA::new(255, 255, 255, 255);
let compressed = [
(QOI_OP_LUMA | 0b0011_1111), (0b1111_1111), // Diff (31, 7, 7) -> Pix (38, 31, 38)
];
let expected = PixelRGBA::new(37, 30, 37, 255);
let mut decoder = Decoder::new_with_previous_pixel(compressed.into_iter(), init_pixel);
let result = decoder.next().expect("Oops, didn't get a Pixel back from the Decoder");
assert_eq!(result, expected);
}
#[test]
fn decoder_unpack_luma_rollunder() {
let init_pixel = PixelRGBA::new(0, 0, 0, 255);
let compressed = [
(QOI_OP_LUMA | 0b0001_0011), (0b1100_0011) // Diff(-13, 4, -5) -> Pix (-9, -13, -18)
];
let expected = PixelRGBA::new(247, 243, 238, 255);
let mut decoder = Decoder::new_with_previous_pixel(compressed.into_iter(), init_pixel);
let result = decoder.next().expect("Oops, didn't get a Pixel back from the Decoder");
assert_eq!(result, expected);
}
#[test]
fn decoder_unpack_run() {
let compressed = [