Skip to main content

figif_core/hashers/
blockhash.rs

1//! Block hash implementation.
2
3use crate::traits::FrameHasher;
4use image::RgbaImage;
5use img_hash::{HashAlg, HasherConfig, ImageHash};
6
7/// Block hash for perceptual image comparison.
8///
9/// Block hash divides the image into blocks and computes a hash
10/// based on the average brightness of each block compared to
11/// the overall image average.
12///
13/// # Algorithm
14///
15/// 1. Divide image into NxN blocks
16/// 2. Compute average brightness for each block
17/// 3. Compute overall image average
18/// 4. Set bit to 1 if block average > image average, else 0
19///
20/// # Example
21///
22/// ```ignore
23/// use figif_core::hashers::BlockHasher;
24/// use figif_core::traits::FrameHasher;
25///
26/// let hasher = BlockHasher::new();
27/// let hash = hasher.hash_frame(&image);
28/// ```
29#[derive(Debug, Clone)]
30pub struct BlockHasher {
31    hash_width: u32,
32    hash_height: u32,
33}
34
35impl Default for BlockHasher {
36    fn default() -> Self {
37        Self::new()
38    }
39}
40
41impl BlockHasher {
42    /// Create a new block hasher with default 8x8 hash size.
43    pub fn new() -> Self {
44        Self::with_size(8, 8)
45    }
46
47    /// Create a block hasher with custom hash dimensions.
48    ///
49    /// The hash size determines the number of blocks (width * height bits).
50    pub fn with_size(width: u32, height: u32) -> Self {
51        Self {
52            hash_width: width,
53            hash_height: height,
54        }
55    }
56
57    /// Get the hash width.
58    pub fn hash_width(&self) -> u32 {
59        self.hash_width
60    }
61
62    /// Get the hash height.
63    pub fn hash_height(&self) -> u32 {
64        self.hash_height
65    }
66
67    /// Get the total hash bits.
68    pub fn hash_bits(&self) -> u32 {
69        self.hash_width * self.hash_height
70    }
71
72    fn build_hasher(&self) -> img_hash::Hasher {
73        HasherConfig::new()
74            .hash_alg(HashAlg::Blockhash)
75            .hash_size(self.hash_width, self.hash_height)
76            .to_hasher()
77    }
78}
79
80impl FrameHasher for BlockHasher {
81    type Hash = ImageHash;
82
83    fn hash_frame(&self, image: &RgbaImage) -> Self::Hash {
84        let hasher = self.build_hasher();
85        // Convert to img_hash's image type via raw pixel conversion
86        let (width, height) = image.dimensions();
87        let raw = image.as_raw().clone();
88        let img_hash_image: img_hash::image::RgbaImage =
89            img_hash::image::ImageBuffer::from_raw(width, height, raw)
90                .expect("image dimensions should match");
91        let dynamic = img_hash::image::DynamicImage::ImageRgba8(img_hash_image);
92        hasher.hash_image(&dynamic)
93    }
94
95    fn distance(&self, a: &Self::Hash, b: &Self::Hash) -> u32 {
96        a.dist(b)
97    }
98
99    fn name(&self) -> &'static str {
100        "blockhash"
101    }
102
103    fn suggested_threshold(&self) -> u32 {
104        // Block hash is fairly tolerant
105        let base_bits = 64;
106        let actual_bits = self.hash_bits();
107        (6 * actual_bits / base_bits).max(4)
108    }
109}