Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ path = "tests/reference_images.rs"
name = "reference_images"
harness = false

[[test]]
path = "tests/imageops_reference.rs"
name = "imageops_reference"
harness = false

# because of https://github.com/image-rs/image/pull/2583
# TODO: remove when shipping the next major release after 0.25
[package.metadata.cargo-semver-checks.lints]
Expand Down
Binary file added tests/assets/bw-edge.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/assets/cat.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/assets/noise.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/imageops/blur/bw-edge blur sigma=0.1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/imageops/blur/bw-edge blur sigma=0.5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/imageops/blur/bw-edge blur sigma=1.0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/imageops/blur/bw-edge blur sigma=1.5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/imageops/blur/bw-edge blur sigma=10.0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/imageops/blur/bw-edge blur sigma=2.0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/imageops/blur/bw-edge blur sigma=5.0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/imageops/blur/bw-edge blur sigma=50.0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/imageops/blur/bw-edge fast_blur sigma=0.1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/imageops/blur/bw-edge fast_blur sigma=0.5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/imageops/blur/bw-edge fast_blur sigma=1.0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/imageops/blur/bw-edge fast_blur sigma=1.5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/imageops/blur/bw-edge fast_blur sigma=10.0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/imageops/blur/bw-edge fast_blur sigma=2.0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/imageops/blur/bw-edge fast_blur sigma=5.0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/imageops/blur/bw-edge fast_blur sigma=50.0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/imageops/blur/noise blur sigma=0.1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/imageops/blur/noise blur sigma=0.5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/imageops/blur/noise blur sigma=1.0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/imageops/blur/noise blur sigma=1.5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/imageops/blur/noise blur sigma=10.0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/imageops/blur/noise blur sigma=2.0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/imageops/blur/noise blur sigma=5.0.png
Binary file added tests/imageops/blur/noise blur sigma=50.0.png
Binary file added tests/imageops/blur/noise fast_blur sigma=0.1.png
Binary file added tests/imageops/blur/noise fast_blur sigma=0.5.png
Binary file added tests/imageops/blur/noise fast_blur sigma=1.0.png
Binary file added tests/imageops/blur/noise fast_blur sigma=1.5.png
Binary file added tests/imageops/blur/noise fast_blur sigma=10.0.png
Binary file added tests/imageops/blur/noise fast_blur sigma=2.0.png
Binary file added tests/imageops/blur/noise fast_blur sigma=5.0.png
Binary file added tests/imageops/blur/noise fast_blur sigma=50.0.png
Binary file added tests/imageops/filter3x3 box blur.png
Binary file added tests/imageops/filter3x3 laplace.png
Binary file added tests/imageops/filter3x3 sharpen.png
Binary file added tests/imageops/huerotate angle=-45.png
Binary file added tests/imageops/huerotate angle=0.png
Binary file added tests/imageops/huerotate angle=180.png
Binary file added tests/imageops/huerotate angle=30.png
Binary file added tests/imageops/huerotate grayscale.png
Binary file added tests/imageops/invert.png
151 changes: 151 additions & 0 deletions tests/imageops_reference.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
use image::{DynamicImage, GenericImageView, GrayImage, RgbImage, RgbaImage};
use std::{
path::{Path, PathBuf},
sync::LazyLock,
};

use libtest_mimic::{Arguments, Trial};

static TESTS_DIR: LazyLock<PathBuf> =
LazyLock::new(|| PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests"));
static ASSETS: LazyLock<PathBuf> = LazyLock::new(|| TESTS_DIR.join("assets"));
static REFERENCE: LazyLock<PathBuf> = LazyLock::new(|| TESTS_DIR.join("imageops"));
Comment thread
RunDevelopment marked this conversation as resolved.
Outdated

fn main() -> std::process::ExitCode {
let mut trials = Vec::new();

let bw_edge = Image::open(&ASSETS.join("bw-edge.png"));
let noise = Image::open(&ASSETS.join("noise.png"));
let cat = Image::open(&ASSETS.join("cat.png"));

// blur & fast_blur with various sigmas
for image in [bw_edge, noise] {
for sigma in [0.1, 0.5, 1.0, 1.5, 2.0, 5.0, 10.0, 50.0] {
add_trial(
&mut trials,
format!("blur/{} blur sigma={sigma:.1}", image.name),
move || {
image::imageops::blur_advanced(
&image.rgb,
image::imageops::GaussianBlurParameters::new_from_sigma(sigma),
)
},
);
add_trial(
&mut trials,
format!("blur/{} fast_blur sigma={sigma:.1}", image.name),
move || image::imageops::fast_blur(&image.rgb, sigma),
);
}
}

// huerotate with various angles
for angle in [0, 30, 180, -45] {
add_trial(&mut trials, format!("huerotate angle={angle}"), move || {
image::imageops::huerotate(&cat.rgba, angle)
});
}
add_trial(&mut trials, "huerotate grayscale", move || {
image::imageops::huerotate(&cat.gray, 180)
});

// invert
add_trial(&mut trials, "invert", move || {
let mut img = cat.rgba.clone();
image::imageops::invert(&mut img);
img
});

// filter3x3
add_trial(&mut trials, "filter3x3 laplace", move || {
image::imageops::filter3x3(&cat.rgb, &[1.0, 1.0, 1.0, 1.0, -8.0, 1.0, 1.0, 1.0, 1.0])
});
add_trial(&mut trials, "filter3x3 box blur", move || {
image::imageops::filter3x3(&cat.rgb, &[1.0 / 9.0; 9])
});
add_trial(&mut trials, "filter3x3 sharpen", move || {
image::imageops::filter3x3(&cat.rgb, &[0.0, -0.5, 0.0, -0.5, 3.0, -0.5, 0.0, -0.5, 0.0])
});

let args = Arguments::from_args();
libtest_mimic::run(&args, trials).exit_code()
}

fn add_trial<I: Into<DynamicImage>>(
trials: &mut Vec<Trial>,
name: impl AsRef<str>,
f: impl FnOnce() -> I + Send + 'static,
) {
if !cfg!(feature = "png") {
trials.push(Trial::test(name.as_ref(), move || Ok(())).with_ignored_flag(true));
return;
}

let path = REFERENCE.join(format!("{}.png", name.as_ref()));
trials.push(Trial::test(name.as_ref(), move || {
let image = f().into();
compare_to_output(&path, image);
Ok(())
}));
}

struct Image {
name: String,
rgba: RgbaImage,
rgb: RgbImage,
gray: GrayImage,
}
impl Image {
fn open(path: &Path) -> &'static Self {
let name = path.file_stem().unwrap().to_str().unwrap().to_string();

let image = if cfg!(feature = "png") {
image::open(path).unwrap()
} else {
DynamicImage::new_rgb8(8, 8)
};

Box::leak(Box::new(Self {
name,
rgba: image.to_rgba8(),
rgb: image.to_rgb8(),
gray: image.to_luma8(),
}))
Comment thread
RunDevelopment marked this conversation as resolved.
Outdated
}
}

fn compare_to_output(path: &Path, image: DynamicImage) {
let name = path.file_stem().unwrap().to_str().unwrap();

if !path.exists() {
#[cfg(feature = "png")]
save_png(path, image);
panic!("Saved output for {name} to {path:?}. Please verify it is correct and commit it.");
}

let reference = image::open(path).unwrap();
assert_eq!(
image.dimensions(),
reference.dimensions(),
"Output dimensions differ for {name}"
);
assert_eq!(
image.as_bytes(),
reference.as_bytes(),
"Output pixel data differs for {name}"
);
}
#[cfg(feature = "png")]
fn save_png(path: &Path, image: DynamicImage) {
use image::codecs::png::{CompressionType, FilterType, PngEncoder};

std::fs::create_dir_all(path.parent().unwrap()).unwrap();

image
.write_with_encoder(PngEncoder::new_with_quality(
std::io::BufWriter::new(std::fs::File::create(path).unwrap()),
CompressionType::Best,
FilterType::Adaptive,
))
.unwrap();
}
Loading