Parallel loading (#136)
* Parallel loading * Avoid vec allocation per pixel
This commit is contained in:
parent
df07d10a5c
commit
72393d06c9
1 changed files with 17 additions and 6 deletions
23
src/lib.rs
23
src/lib.rs
|
|
@ -687,8 +687,15 @@ fn optimize_png(png: &mut PngData, original_data: &[u8], opts: &Options) -> PngR
|
|||
}
|
||||
}
|
||||
|
||||
let old_png = image::load_from_memory_with_format(original_data, ImageFormat::PNG);
|
||||
let new_png = image::load_from_memory_with_format(&output, ImageFormat::PNG);
|
||||
let (old_png, new_png) = {
|
||||
let old_png = || image::load_from_memory_with_format(original_data, ImageFormat::PNG);
|
||||
let new_png = || image::load_from_memory_with_format(&output, ImageFormat::PNG);
|
||||
#[cfg(feature = "parallel")]
|
||||
let res = rayon::join(old_png, new_png);
|
||||
#[cfg(not(feature = "parallel"))]
|
||||
let res = (old_png(), new_png());
|
||||
res
|
||||
};
|
||||
|
||||
if let Ok(new_png) = new_png {
|
||||
if let Ok(old_png) = old_png {
|
||||
|
|
@ -898,11 +905,15 @@ fn copy_permissions(input_path: &Path, out_file: &File, verbosity: Option<u8>) {
|
|||
fn images_equal(old_png: &DynamicImage, new_png: &DynamicImage) -> bool {
|
||||
let a = old_png
|
||||
.pixels()
|
||||
.map(|x| x.2.channels().to_owned())
|
||||
.filter(|p| !(p.len() == 4 && p[3] == 0));
|
||||
.filter(|x| {
|
||||
let p = x.2.channels();
|
||||
!(p.len() == 4 && p[3] == 0)
|
||||
});
|
||||
let b = new_png
|
||||
.pixels()
|
||||
.map(|x| x.2.channels().to_owned())
|
||||
.filter(|p| !(p.len() == 4 && p[3] == 0));
|
||||
.filter(|x| {
|
||||
let p = x.2.channels();
|
||||
!(p.len() == 4 && p[3] == 0)
|
||||
});
|
||||
a.eq(b)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue