Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
11 changes: 8 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,19 @@ path = "benches/imageops.rs"
name = "imageops"
harness = false

[[bench]]
path = "benches/bgra_to_rgba.rs"
name = "bgra_to_rgba"
harness = false

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

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

# because of https://github.com/image-rs/image/pull/2583
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
162 changes: 162 additions & 0 deletions tests/imageops_reference.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
use image::{DynamicImage, GenericImageView, GrayImage, RgbImage, RgbaImage};
use std::{
path::{Path, PathBuf},
sync::OnceLock,
};

use libtest_mimic::{Arguments, Trial};

fn main() -> std::process::ExitCode {
if !cfg!(feature = "png") {
// We can read neither inputs not references, so there is nothing to test.
eprintln!("The 'png' feature is not enabled. Skipping tests.");
return std::process::ExitCode::SUCCESS;
}

let tests_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests");
let assets_dir = tests_dir.join("assets");
let reference_dir = tests_dir.join("imageops");

static IMAGES: OnceLock<[Image; 3]> = OnceLock::new();
let [bw_edge, noise, cat] = IMAGES.get_or_init(|| {
[
Image::open(&assets_dir.join("bw-edge.png")),
Image::open(&assets_dir.join("noise.png")),
Image::open(&assets_dir.join("cat.png")),
]
});

let mut trials = ImageTrials::new(reference_dir);

// 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] {
trials.add(
format!("blur/{} blur sigma={sigma:.1}", image.name),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having filenames with spaces in them does make command line analysis of the files somewhat more awkward in general. (You cannot as easily do tricks like ls -l tests/imageops/blur/* | cut -w -f 5,9 | sort -n, which is a bit silly of an example.) But this is only a weak preference of mine.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If your tools can't handle valid file paths, you need better tools!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough.

move || {
image::imageops::blur_advanced(
&image.rgb,
image::imageops::GaussianBlurParameters::new_from_sigma(sigma),
)
},
);
trials.add(
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] {
trials.add(format!("huerotate angle={angle}"), move || {
image::imageops::huerotate(&cat.rgba, angle)
});
}
trials.add("huerotate grayscale", move || {
image::imageops::huerotate(&cat.gray, 180)
});

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

// filter3x3
trials.add("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])
});
trials.add("filter3x3 box blur", move || {
image::imageops::filter3x3(&cat.rgb, &[1.0 / 9.0; 9])
});
trials.add("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.trials).exit_code()
}

struct ImageTrials {
trials: Vec<Trial>,
reference_dir: PathBuf,
}
impl ImageTrials {
fn new(reference_dir: PathBuf) -> Self {
Self {
trials: Vec::new(),
reference_dir,
}
}
fn add<I: Into<DynamicImage>>(
&mut self,
name: impl AsRef<str>,
f: impl FnOnce() -> I + Send + 'static,
) {
let path = self.reference_dir.join(format!("{}.png", name.as_ref()));
self.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) -> Self {
let name = path.file_stem().unwrap().to_str().unwrap().to_string();

let image = image::open(path).unwrap();

Self {
name,
rgba: image.to_rgba8(),
rgb: image.to_rgb8(),
gray: image.to_luma8(),
}
}
}

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