Skip to content

feat: add Peak Signal-to-Noise Ratio (PSNR) to compression#1028

Merged
siriak merged 2 commits intoTheAlgorithms:masterfrom
AliAlimohammadi:add-psnr-compression
Mar 22, 2026
Merged

feat: add Peak Signal-to-Noise Ratio (PSNR) to compression#1028
siriak merged 2 commits intoTheAlgorithms:masterfrom
AliAlimohammadi:add-psnr-compression

Conversation

@AliAlimohammadi
Copy link
Contributor

Description

Adds a pure-Rust implementation of Peak Signal-to-Noise Ratio (PSNR), a standard metric for measuring the quality of a compressed or reconstructed image relative to its original.

First, the Mean Squared Error (MSE) is computed across all $n$ pixel values:

$$MSE = \frac{1}{n} \sum_{i=1}^{n} (o_i - c_i)^2$$

PSNR is then derived in decibels (dB) as:

$$PSNR = 20 \cdot \log_{10}\left(\frac{PIXEL_MAX}{\sqrt{MSE}}\right)$$

where $PIXEL_MAX = 255$ for an 8-bit image. Returns f64::INFINITY when the two images are identical ($MSE = 0$).

Reference: Peak signal-to-noise ratio — Wikipedia

Type of change

  • New feature (non-breaking change which adds functionality)

Checklist:

  • I ran cargo clippy --all -- -D warnings just before my last commit and fixed any issue that was found.
  • I ran cargo fmt just before my last commit.
  • I ran cargo test just before my last commit and all tests passed.
  • I added my algorithm to the corresponding mod.rs file within its own folder, and in any parent folder(s).
  • I added my algorithm to DIRECTORY.md with the correct link.
  • I checked CONTRIBUTING.md and my code follows its guidelines.

@codecov-commenter
Copy link

codecov-commenter commented Mar 17, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.06%. Comparing base (88a00a6) to head (f02db26).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1028   +/-   ##
=======================================
  Coverage   96.05%   96.06%           
=======================================
  Files         387      388    +1     
  Lines       29420    29468   +48     
=======================================
+ Hits        28259    28307   +48     
  Misses       1161     1161           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@AliAlimohammadi
Copy link
Contributor Author

@siriak, this is ready to be merged.

Note: CI is currently failing due to pre-existing Clippy warnings in binary_count_trailing_zeros.rs, transposition.rs, and prime_check.rs that are unrelated to this PR. Happy to fix these in a separate commit if the maintainers prefer.

Copy link
Member

@siriak siriak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks!

@siriak siriak merged commit 1c7738e into TheAlgorithms:master Mar 22, 2026
7 checks passed
@AliAlimohammadi AliAlimohammadi deleted the add-psnr-compression branch March 22, 2026 21:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants