From 85ad2fa252e7b7b4a5498d00d2597a15c66636b2 Mon Sep 17 00:00:00 2001 From: Robert Garrett Date: Tue, 7 May 2024 09:23:29 -0500 Subject: [PATCH] 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. --- src/main.rs | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/src/main.rs b/src/main.rs index 6474ec7..1face9d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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 = 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 = 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 = [