Change validation to a debug assert (#481)
This commit is contained in:
parent
a3b104a2ed
commit
91e29144b0
1 changed files with 25 additions and 21 deletions
46
src/lib.rs
46
src/lib.rs
|
|
@ -657,28 +657,9 @@ fn optimize_png(
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
let (old_png, new_png) = rayon::join(
|
debug_assert!(validate_output(&output, original_data));
|
||||||
|| load_png_image_from_memory(original_data),
|
|
||||||
|| load_png_image_from_memory(&output),
|
|
||||||
);
|
|
||||||
|
|
||||||
if let Ok(new_png) = new_png {
|
Ok(output)
|
||||||
if let Ok(old_png) = old_png {
|
|
||||||
if images_equal(&old_png, &new_png) {
|
|
||||||
return Ok(output);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// The original image might be invalid if, for example, there is a CRC error,
|
|
||||||
// and we set fix_errors to true. In that case, all we can do is check that the
|
|
||||||
// new image is decodable.
|
|
||||||
return Ok(output);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
error!(
|
|
||||||
"The resulting image is corrupted and will not be outputted.\nThis is a bug! Please report it at https://github.com/shssoichiro/oxipng/issues"
|
|
||||||
);
|
|
||||||
Err(PngError::new("The resulting image is corrupted"))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn perform_reductions(
|
fn perform_reductions(
|
||||||
|
|
@ -1061,6 +1042,29 @@ fn copy_times(input_path_meta: &Metadata, out_path: &Path) -> PngResult<()> {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Validate that the output png data still matches the original image
|
||||||
|
fn validate_output(output: &[u8], original_data: &[u8]) -> bool {
|
||||||
|
let (old_png, new_png) = rayon::join(
|
||||||
|
|| load_png_image_from_memory(original_data),
|
||||||
|
|| load_png_image_from_memory(output),
|
||||||
|
);
|
||||||
|
|
||||||
|
match (new_png, old_png) {
|
||||||
|
(Err(new_err), _) => {
|
||||||
|
error!("Failed to read output image for validation: {}", new_err);
|
||||||
|
false
|
||||||
|
}
|
||||||
|
(_, Err(old_err)) => {
|
||||||
|
// The original image might be invalid if, for example, there is a CRC error,
|
||||||
|
// and we set fix_errors to true. In that case, all we can do is check that the
|
||||||
|
// new image is decodable.
|
||||||
|
warn!("Failed to read input image for validation: {}", old_err);
|
||||||
|
true
|
||||||
|
}
|
||||||
|
(Ok(new_png), Ok(old_png)) => images_equal(&old_png, &new_png),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Loads a PNG image from memory to a [DynamicImage]
|
/// Loads a PNG image from memory to a [DynamicImage]
|
||||||
fn load_png_image_from_memory(png_data: &[u8]) -> Result<DynamicImage, image::ImageError> {
|
fn load_png_image_from_memory(png_data: &[u8]) -> Result<DynamicImage, image::ImageError> {
|
||||||
let mut reader = image::io::Reader::new(Cursor::new(png_data));
|
let mut reader = image::io::Reader::new(Cursor::new(png_data));
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue