Panic if not same size

This commit is contained in:
Thomas Forgione 2018-03-29 08:44:58 +02:00
parent 64a9d5402d
commit f6b458cafa
No known key found for this signature in database
GPG Key ID: C75CD416BD1FFCE1
1 changed files with 9 additions and 8 deletions

View File

@ -1,14 +1,15 @@
extern crate image; extern crate image;
use image::{Pixel, GenericImage, ImageResult, ImageError}; use image::{Pixel, GenericImage, ImageResult};
pub fn psnr<P: Pixel<Subpixel=u8>, T: GenericImage<Pixel=P>>(img1: &T, img2: &T) -> ImageResult<f64> { /// Compute a PSNR between two images.
///
/// Will panic an error if the two images don't have the same size.
pub fn psnr<P: Pixel<Subpixel=u8>, T: GenericImage<Pixel=P>>(img1: &T, img2: &T) -> f64 {
let twenty_log10_max: f64 = 20.0 * (std::u32::MAX as f64).log10(); let twenty_log10_max: f64 = 20.0 * (std::u8::MAX as f64).log10();
if img1.dimensions() != img2.dimensions() { assert_eq!(img1.dimensions(), img2.dimensions());
return Err(ImageError::DimensionError);
}
let mut mse = 0.0; let mut mse = 0.0;
@ -31,11 +32,11 @@ pub fn psnr<P: Pixel<Subpixel=u8>, T: GenericImage<Pixel=P>>(img1: &T, img2: &T)
} }
mse /= (img1.dimensions().0 * img1.dimensions().1 * 3) as f64; mse /= (img1.dimensions().0 * img1.dimensions().1 * 3) as f64;
Ok(twenty_log10_max - 10.0 * mse.log10()) twenty_log10_max - 10.0 * mse.log10()
} }
pub fn psnr_files(path1: &str, path2: &str) -> ImageResult<f64> { pub fn psnr_files(path1: &str, path2: &str) -> ImageResult<f64> {
let img1 = image::open(path1)?; let img1 = image::open(path1)?;
let img2 = image::open(path2)?; let img2 = image::open(path2)?;
psnr(&img1, &img2) Ok(psnr(&img1, &img2))
} }