diff --git a/data/blur.py b/data/blur.py new file mode 100644 index 0000000..82d455b --- /dev/null +++ b/data/blur.py @@ -0,0 +1,16 @@ +# Blur the test images using the official blurhash-python package. +# The results are used as reference images in the tests of blurhash-rs. + +import blurhash +from os import listdir +from PIL import Image + +images = [f for f in listdir(".") if "png" in f and not "blurred" in f] + +for path in images: + with Image.open(path) as image: + hash = blurhash.encode(image, x_components=4, y_components=3) + width, height = image.size + + result = blurhash.decode(hash, width, height, mode=blurhash.PixelMode.RGBA) + result.save(path.split(".")[0] + "_blurred.png") diff --git a/data/octocat_blurred.png b/data/octocat_blurred.png new file mode 100644 index 0000000..f84003f Binary files /dev/null and b/data/octocat_blurred.png differ diff --git a/data/out.png b/data/out.png deleted file mode 100644 index 4094d81..0000000 Binary files a/data/out.png and /dev/null differ diff --git a/data/wikipedia_logo.png b/data/wikipedia_logo.png new file mode 100644 index 0000000..9608b05 Binary files /dev/null and b/data/wikipedia_logo.png differ diff --git a/data/wikipedia_logo.svg b/data/wikipedia_logo.svg deleted file mode 100644 index dc32f98..0000000 --- a/data/wikipedia_logo.svg +++ /dev/null @@ -1 +0,0 @@ -]>Wikipedia logo version 2 \ No newline at end of file diff --git a/data/wikipedia_logo_blurred.png b/data/wikipedia_logo_blurred.png new file mode 100644 index 0000000..5d0447b Binary files /dev/null and b/data/wikipedia_logo_blurred.png differ diff --git a/src/lib.rs b/src/lib.rs index 553dd42..baa8a7a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -368,7 +368,6 @@ pub fn decode_pixbuf( #[cfg(test)] mod tests { use super::*; - use image::{save_buffer, ColorType::Rgba8}; use image::{EncodableLayout, GenericImageView}; #[test] @@ -378,7 +377,6 @@ mod tests { let blurhash = encode(4, 3, width, height, img.to_rgba8().as_bytes()).unwrap(); let img = decode(&blurhash, width, height, 1.0).unwrap(); - save_buffer("data/out.png", &img, width, height, Rgba8).unwrap(); assert_eq!(img[0..5], [1, 1, 1, 255, 1]); } @@ -399,7 +397,6 @@ mod tests { let (width, height) = img.dimensions(); let blurhash = encode(4, 3, width, height, img.to_rgba8().as_bytes()).unwrap(); - // assert_eq!(blurhash, "LFL4=pI8%foujXofRkWC.loyH?V{"); assert_eq!(blurhash, "LNAdAqj[00aymkj[TKay9}ay-Sj["); } @@ -439,7 +436,7 @@ mod tests { #[cfg(feature = "gdk-pixbuf")] fn decode_blurhash_pixbuf() { use std::convert::TryInto; - let img = gdk_pixbuf::Pixbuf::from_file("data/wikipedia_logo.svg").unwrap(); + let img = gdk_pixbuf::Pixbuf::from_file("data/wikipedia_logo.png").unwrap(); let blurhash = encode_pixbuf(4, 3, &img).unwrap(); let img = decode_pixbuf( @@ -450,8 +447,44 @@ mod tests { ) .unwrap(); - let bytes = img.save_to_bufferv("png", &[]).unwrap(); + let target = image::open("data/wikipedia_logo_blurred.png").unwrap(); + assert_image_data_approximately_equal(&img.read_pixel_bytes(), target.as_bytes()) + } + + #[cfg(feature = "gdk-pixbuf")] + fn assert_image_data_approximately_equal(result: &[u8], target: &[u8]) { + const MAX_AVERAGE_ERROR: usize = 1; + const MAX_PEAK_ERROR: usize = 8; + + assert_eq!( + result.len(), + target.len(), + "images do not have the same shape: {} vs {}", + result.len(), + target.len() + ); + + let mut aggregated_error: usize = 0; + let mut peak_error = 0; + for (r, t) in result.iter().zip(target) { + let error = (*r as isize - *t as isize).abs() as usize; + aggregated_error += error; + peak_error = peak_error.max(error); + } - assert_eq!(bytes[1000..1005], [77, 210, 4, 80, 15]); + let average_error = aggregated_error / result.len(); + + assert!( + average_error <= MAX_AVERAGE_ERROR, + "images do not look similar. average error {} > {}", + average_error, + MAX_AVERAGE_ERROR + ); + assert!( + peak_error <= MAX_PEAK_ERROR, + "images do not look similar. peak error {} > {}", + peak_error, + MAX_PEAK_ERROR, + ); } }