2024-07-31 02:46:15 +00:00
|
|
|
// 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, 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(())
|
|
|
|
}
|
2024-07-31 02:53:58 +00:00
|
|
|
|
2024-07-31 02:46:15 +00:00
|
|
|
#[test]
|
2024-07-31 02:53:58 +00:00
|
|
|
fn sample_0_atkinson() -> Result<()> {
|
2024-07-31 02:46:15 +00:00
|
|
|
compare_original(
|
|
|
|
"sample_0.tiff",
|
|
|
|
"sample_0_atkinson.tiff",
|
|
|
|
&DitherMethod::Atkinson,
|
2024-07-31 02:53:58 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
#[test]
|
|
|
|
fn sample_1_atkinson() -> Result<()> {
|
2024-07-31 02:46:15 +00:00
|
|
|
compare_original(
|
|
|
|
"sample_1.tiff",
|
|
|
|
"sample_1_atkinson.tiff",
|
|
|
|
&DitherMethod::Atkinson,
|
2024-07-31 02:53:58 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn sample_1_floydsteinberg() -> Result<()> {
|
2024-07-31 02:46:15 +00:00
|
|
|
compare_original(
|
|
|
|
"sample_1.tiff",
|
|
|
|
"sample_1_floydsteinberg.tiff",
|
|
|
|
&DitherMethod::FloydSteinberg,
|
2024-07-31 02:53:58 +00:00
|
|
|
)
|
2024-07-31 02:46:15 +00:00
|
|
|
}
|