From f6b458cafa412b5851fc9d957451dc9f9f3e7333 Mon Sep 17 00:00:00 2001 From: Thomas Forgione Date: Thu, 29 Mar 2018 08:44:58 +0200 Subject: [PATCH] Panic if not same size --- src/lib.rs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 43919b6..c445cf6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,14 +1,15 @@ extern crate image; -use image::{Pixel, GenericImage, ImageResult, ImageError}; +use image::{Pixel, GenericImage, ImageResult}; -pub fn psnr, T: GenericImage>(img1: &T, img2: &T) -> ImageResult { +/// Compute a PSNR between two images. +/// +/// Will panic an error if the two images don't have the same size. +pub fn psnr, T: GenericImage>(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() { - return Err(ImageError::DimensionError); - } + assert_eq!(img1.dimensions(), img2.dimensions()); let mut mse = 0.0; @@ -31,11 +32,11 @@ pub fn psnr, T: GenericImage>(img1: &T, img2: &T) } 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 { let img1 = image::open(path1)?; let img2 = image::open(path2)?; - psnr(&img1, &img2) + Ok(psnr(&img1, &img2)) }