Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
spirali committed Jan 9, 2025
1 parent cf29792 commit 13a5a5f
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 35 deletions.
Binary file added tests/left/same.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/right/same.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
43 changes: 25 additions & 18 deletions v2/kompari/src/dirdiff.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::fs::File;
use std::path::{Path, PathBuf};
use crate::fsutils::{list_image_dir_names, load_image};
use crate::imgdiff::{compare_images, ImageDifference};
Expand All @@ -12,6 +11,7 @@ pub struct DirDiffConfig {
}

impl DirDiffConfig {

pub fn new(left_path: PathBuf, right_path: PathBuf) -> Self {
DirDiffConfig {
left_path,
Expand All @@ -31,23 +31,21 @@ impl DirDiffConfig {
let mut diffs: Vec<_> = pairs
.into_iter()
.filter_map(|pair| {
let diff_result = compute_pair_diff(&pair);
let image_diff = compute_pair_diff(&pair);
if self.ignore_left_missing && !matches!(image_diff, Err(ref e) if e.is_left_missing()) {
return None
}
if self.ignore_right_missing && !matches!(image_diff, Err(ref e) if e.is_right_missing()) {
return None
}
Some(PairResult {
title: pair.title,
left: pair.left,
right: pair.right,
diff_result,
image_diff,
})
})
.collect();

if self.ignore_left_missing {
diffs.retain(|pair| !matches!(pair.left_info, ImageInfoResult::Missing));
}

if self.ignore_right_missing {
diffs.retain(|pair| !matches!(pair.right_info, ImageInfoResult::Missing));
}
Ok(DirDiff { diffs })
}

Expand Down Expand Up @@ -84,6 +82,14 @@ impl LeftRightError {
LeftRightError::Left(_) => None,
}
}

pub fn is_left_missing(&self) -> bool {
self.left().map(|e| matches!(e, crate::Error::FileNotFound(_))).unwrap_or(false)
}

pub fn is_right_missing(&self) -> bool {
self.right().map(|e| matches!(e, crate::Error::FileNotFound(_))).unwrap_or(false)
}
}

pub struct PairResult {
Expand All @@ -99,11 +105,13 @@ pub struct DirDiff {
}

impl DirDiff {

pub fn results(&self) -> &[PairResult] {
&self.diffs
}
}


pub struct Pair {
struct Pair {
pub title: String,
pub left: PathBuf,
pub right: PathBuf,
Expand Down Expand Up @@ -136,12 +144,11 @@ pub(crate) fn pairs_from_paths(
.map(|name| {
let left = left_path.join(&name);
let right = right_path.join(&name);
Pair::new(
name.into_string()
.unwrap_or_else(|name| name.to_string_lossy().into_owned()),
Pair {
title: name.into_string().unwrap_or_else( | name| name.to_string_lossy().into_owned()),
left,
right,
)
}
})
.collect())
}
Expand All @@ -156,5 +163,5 @@ fn compute_pair_diff(pair: &Pair) -> Result<ImageDifference, LeftRightError> {
(Ok(_), Err(e)) => return Err(LeftRightError::Right(e)),
(Err(e1), Err(e2)) => return Err(LeftRightError::Both(e1, e2)),
};
compare_images(&left_image, &right_image)
Ok(compare_images(&left_image, &right_image))
}
6 changes: 4 additions & 2 deletions v2/kompari/src/fsutils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

use std::ffi::{OsStr, OsString};
use std::path::{Path, PathBuf};
use image::RgbImage;

// Copyright 2024 the Kompari Authors
// SPDX-License-Identifier: Apache-2.0 OR MIT

pub(crate) fn list_image_dir(
dir_path: &Path,
Expand Down Expand Up @@ -37,5 +39,5 @@ pub(crate) fn load_image(path: &Path) -> crate::Result<crate::Image> {
if !path.is_file() {
return Err(crate::Error::FileNotFound(path.to_path_buf()))
}
Ok(image::ImageReader::open(path)?.decode()?.into_rgb8())
Ok(image::ImageReader::open(path)?.decode()?.into_rgba8())
}
20 changes: 10 additions & 10 deletions v2/kompari/src/imgdiff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,26 @@ pub enum ImageDifference {
},
}

pub fn compare_images(old: &Image, new: &Image) -> Difference {
if old.width() != new.width() || old.height() != new.height() {
return Difference::SizeMismatch;
pub fn compare_images(left: &Image, right: &Image) -> ImageDifference {
if left.width() != right.width() || left.height() != right.height() {
return ImageDifference::SizeMismatch;
}

let n_different_pixels: u64 = old
let n_different_pixels: u64 = left
.pixels()
.zip(new.pixels())
.zip(right.pixels())
.map(|(pl, pr)| if pl == pr { 0 } else { 1 })
.sum();

if n_different_pixels == 0 {
return Difference::None;
return ImageDifference::None;
}

let mut distance_sum: u64 = 0;

let diff_image_data: Vec<u8> = old
let diff_image_data: Vec<u8> = left
.pixels()
.zip(new.pixels())
.zip(right.pixels())
.flat_map(|(&p1, &p2)| {
let total_distance = pixel_distance(p1, p2);
distance_sum += total_distance;
Expand All @@ -53,10 +53,10 @@ pub fn compare_images(old: &Image, new: &Image) -> Difference {
})
.collect();

let diff_image = Image::from_vec(old.width(), old.height(), diff_image_data)
let diff_image = Image::from_vec(left.width(), left.height(), diff_image_data)
.expect("Same number of pixels as left and right, which have the same dimensions");

Difference::Content {
ImageDifference::Content {
n_different_pixels,
distance_sum,
diff_image,
Expand Down
9 changes: 4 additions & 5 deletions v2/kompari/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ mod imgdiff;
mod dirdiff;
mod fsutils;

#[cfg(test)]
mod tests;

/// The image type used throughout Kompari.
pub type Image = image::RgbaImage;

Expand All @@ -45,8 +48,4 @@ pub enum Error {
GenericError(String),
}

pub type Result<T> = std::result::Result<T, Error>;


#[cfg(test)]
mod tests {}
pub type Result<T> = std::result::Result<T, Error>;
20 changes: 20 additions & 0 deletions v2/kompari/src/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2024 the Kompari Authors
// SPDX-License-Identifier: Apache-2.0 OR MIT

use std::path::Path;
use crate::dirdiff::DirDiffConfig;

#[test]
pub fn test_compare_dir() {
let test_dir = Path::new(env!("CARGO_MANIFEST_DIR"))
.parent()
.unwrap()
.parent().unwrap().join("tests");
let left = test_dir.join("left");
let right = test_dir.join("right");

let diff = DirDiffConfig::new(left, right).create_diff().unwrap();
let res = diff.results();
assert_eq!(res.len(), 1);

}

0 comments on commit 13a5a5f

Please sign in to comment.