Repository navigation
Add reference tests for image operations #2877
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
RunDevelopment
wants to merge
9
commits into
image-rs:main
Choose a base branch
from
RunDevelopment:imageops-test
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
fe3af28
Add reference tests for image operations
RunDevelopment 3715540
Fix test for when `png` isn't available
RunDevelopment d15c965
Merge branch 'main' into imageops-test
RunDevelopment f9b74a8
Use OnceLock for images
RunDevelopment da33738
Remove LazyLock
RunDevelopment be9679f
Add struct for trials
RunDevelopment 0ed099d
Merge branch 'main' into imageops-test
RunDevelopment 1e9e047
Update for `filter3x3`'s abyss policy
RunDevelopment 253a350
More explicit no-png handling
RunDevelopment File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| use image::{DynamicImage, GenericImageView, GrayImage, RgbImage, RgbaImage}; | ||
| use std::{ | ||
| path::{Path, PathBuf}, | ||
| sync::OnceLock, | ||
| }; | ||
|
|
||
| use libtest_mimic::{Arguments, Trial}; | ||
|
|
||
| fn main() -> std::process::ExitCode { | ||
| 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), | ||
| 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, | ||
| ) { | ||
| if !cfg!(feature = "png") { | ||
| self.trials | ||
| .push(Trial::test(name.as_ref(), move || Ok(())).with_ignored_flag(true)); | ||
| return; | ||
| } | ||
|
|
||
| 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 = if cfg!(feature = "png") { | ||
| image::open(path).unwrap() | ||
| } else { | ||
| DynamicImage::new_rgb8(8, 8) | ||
| }; | ||
|
RunDevelopment marked this conversation as resolved.
Resolved
|
||
|
|
||
| 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(); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.There was a problem hiding this comment.
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!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough.