Compare images without allocating memory (#107)
This commit is contained in:
parent
5570a24781
commit
3e5b61e0c8
1 changed files with 9 additions and 6 deletions
15
src/lib.rs
15
src/lib.rs
|
|
@ -583,7 +583,7 @@ fn optimize_png(
|
||||||
|
|
||||||
if let Ok(new_png) = new_png {
|
if let Ok(new_png) = new_png {
|
||||||
if let Ok(old_png) = old_png {
|
if let Ok(old_png) = old_png {
|
||||||
if image_to_pixel_array(&old_png) == image_to_pixel_array(&new_png) {
|
if images_equal(&old_png, &new_png) {
|
||||||
return Ok(output);
|
return Ok(output);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -737,10 +737,13 @@ fn copy_permissions(input_path: &Path, out_file: &File, verbosity: Option<u8>) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn image_to_pixel_array(image: &DynamicImage) -> Vec<Vec<u8>> {
|
/// Compares images pixel by pixel for equivalent content
|
||||||
image
|
fn images_equal(old_png: &DynamicImage, new_png: &DynamicImage) -> bool {
|
||||||
.pixels()
|
let a = old_png.pixels()
|
||||||
.map(|x| x.2.channels().to_owned())
|
.map(|x| x.2.channels().to_owned())
|
||||||
.filter(|p| !(p.len() == 4 && p[3] == 0))
|
.filter(|p| !(p.len() == 4 && p[3] == 0));
|
||||||
.collect()
|
let b = new_png.pixels()
|
||||||
|
.map(|x| x.2.channels().to_owned())
|
||||||
|
.filter(|p| !(p.len() == 4 && p[3] == 0));
|
||||||
|
a.eq(b)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue