// Regression tests for dithering methods. This file ensures that changes made to how the // algorithms are implemented does not affect the actual algorithm. This is done with // 'known-good' sample images (see `samples/`). If changes to the algorithms are made, // these tests will fail, and that is to be expected. In that case the reference images // will need to be updated as well. use anyhow::Result; use image::{ImageReader, RgbImage}; use pi_frame_server::dither::{DitherMethod, DitheredImage}; use pi_frame_server::eink::Palette; fn compare_original(sample_file: &str, reference_file: &str, method: &DitherMethod) -> Result<()> { let image: RgbImage = ImageReader::open(format!("samples/{sample_file}"))? .decode()? .into_rgb8(); // process let mut method = method.get_ditherer(); let mut result = DitheredImage::new( image.width(), image.height(), Palette::Default.value().to_vec(), ); method.dither(&image, &mut result); let expected = ImageReader::open(format!("tests/known_samples/{reference_file}"))? .decode()? .into_rgb8() .into_raw(); let output = result.into_rgbimage().into_raw(); assert_eq!(output, expected); Ok(()) } #[test] fn sample_0_atkinson() -> Result<()> { compare_original( "sample_0.tiff", "sample_0_atkinson.tiff", &DitherMethod::Atkinson, ) } #[test] fn sample_1_atkinson() -> Result<()> { compare_original( "sample_1.tiff", "sample_1_atkinson.tiff", &DitherMethod::Atkinson, ) } #[test] fn sample_1_floydsteinberg() -> Result<()> { compare_original( "sample_1.tiff", "sample_1_floydsteinberg.tiff", &DitherMethod::FloydSteinberg, ) }