Skip to content

verify align hash output with imagehash#25

Merged
aastopher merged 2 commits intostagefrom
feature/hash_improvements
Feb 5, 2025
Merged

verify align hash output with imagehash#25
aastopher merged 2 commits intostagefrom
feature/hash_improvements

Conversation

@aastopher
Copy link
Owner

@aastopher aastopher commented Feb 5, 2025

aHash

  • Reversed Bit Order:
    Now uses 1 << (63 - i) so that the most significant bit corresponds to the first pixel, matching imagehash’s convention.

mHash

  • Reversed Bit Order:
    Bit order is reversed (using 1 << (63 - i)) to align with other hash types.
  • Optimized Median Calculation:
    Uses Rust’s select_nth_unstable to compute the median in O(n) time instead of a full sort.

dHash

  • Optimized Loop Structure:
    Retrieves the first pixel outside the inner loop (iterating from x = 1 to 8) to avoid redundant calls.

pHash

  • Refined DCT Processing:
    Applies the DCT row‑wise and then column‑wise in place using a fixed‑size temporary array.
  • DC Exclusion in Median Calculation:
    Extracts the top‑left 8×8 DCT coefficients and excludes the DC component when computing the median.
  • O(n) Median Computation:
    Uses select_nth_unstable_by to find median in O(n).
  • Reversed Bit Order:
    Final hash bits are reversed (using 1 << (63 - i)) for consistency with the other algorithms.
  • Result:
    Produces a pHash that is consistent with imagehash.

wHash (Wavelet Hash)

  • Reversed Bit Order:
    Now uses 1 << (63 - i) so that the most significant bit corresponds to the first pixel, matching imagehash’s convention.
  • Haar Transform via dwt Crate:
    Implement Haar transform functions from the dwt crate.
  • Zeroing DC Coefficient:
    Immediately zero out the DC coefficient after forward transform.
  • O(n) Median Calculation:
    Clones transformed pixel vector and uses select_nth_unstable_by to find median in O(n).
  • Hash Generation:
    Iterates over the reconstructed coefficients from the inverse Haar transform, setting bits based on the median, to conform to imagehash’s output.
  • Result:
    Produces a wHash that is consistent with imagehash.

Each change was made to improve performance, and ensure consistent bit ordering across all hash types, and simplify the implementations and align hash output with imagehash for consistency.

@deepsource-io
Copy link

deepsource-io bot commented Feb 5, 2025

Here's the code health analysis summary for commits 350018c..9306aea. View details on DeepSource ↗.

Analysis Summary

AnalyzerStatusSummaryLink
DeepSource Rust LogoRust✅ SuccessView Check ↗

💡 If you’re a repository administrator, you can configure the quality gates from the settings.

@aastopher aastopher changed the base branch from main to stage February 5, 2025 04:21
@codspeed-hq
Copy link

codspeed-hq bot commented Feb 5, 2025

CodSpeed Performance Report

Merging #25 will not alter performance

Comparing feature/hash_improvements (2f3247a) with stage (350018c)

Summary

✅ 10 untouched benchmarks

@aastopher aastopher merged commit f9e65e5 into stage Feb 5, 2025
1 check passed
@aastopher aastopher deleted the feature/hash_improvements branch February 11, 2025 04:09
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.

1 participant