-
Notifications
You must be signed in to change notification settings - Fork 697
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
6
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 3 commits
Commits
Show all changes
6 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 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,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")); | ||
|
|
||
| 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(), | ||
| })) | ||
|
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(); | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.