From 0d9946dc311394ea0de202209560777ea81196a7 Mon Sep 17 00:00:00 2001 From: Thomas Forgione Date: Fri, 29 Jun 2018 14:19:23 +0200 Subject: [PATCH] Added psnr from mse --- Cargo.lock | 1 + Cargo.toml | 1 + src/lib.rs | 17 +++++++++++++++-- 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d99689e..889f5e9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -182,6 +182,7 @@ name = "psnr" version = "0.1.0" dependencies = [ "image 0.17.0 (registry+https://github.com/rust-lang/crates.io-index)", + "lazy_static 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 4a89f8e..f7e8d75 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,7 @@ authors = ["Thomas Forgione "] [dependencies] image = "*" +lazy_static = "*" [[bin]] name = "psnr" diff --git a/src/lib.rs b/src/lib.rs index d30aed8..e26c913 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,14 @@ +#[macro_use] +extern crate lazy_static; extern crate image; use image::{Pixel, GenericImage, ImageResult, Rgba}; +lazy_static! { + /// This values is 20 * log10(255) + pub static ref TWENTY_LOG10_MAX: f64 = 20.0 * (std::u8::MAX as f64).log10(); +} + /// Compute a MSE between an image and its color. /// /// Will panic an error if the two images don't have the same size. @@ -58,9 +65,15 @@ pub fn mse, T: GenericImage>(img1: &T, img2: &T) mse / (img1.dimensions().0 * img1.dimensions().1 * 3) as f64 } +/// Compute the PSNR from the MSE. +/// Returns 10 * log10(255^2) - 10 * log10(mse) +pub fn psnr_from_mse(mse: f64) -> f64 { + *TWENTY_LOG10_MAX - 10.0 * mse.log10() + +} + pub fn psnr, T: GenericImage>(img1: &T, img2: &T) -> f64 { - let twenty_log10_max: f64 = 20.0 * (std::u8::MAX as f64).log10(); - twenty_log10_max - 10.0 * mse(img1, img2).log10() + *TWENTY_LOG10_MAX - 10.0 * mse(img1, img2).log10() } pub fn psnr_files(path1: &str, path2: &str) -> ImageResult {