1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//! `average-v1` and `difference-v1`, the integer image hashes.
use cratedownscale;
use crate::;
/// Hash an image with `average-v1`, the average hash.
///
/// The definition, frozen:
///
/// 1. Reduce the image to 8×8 with [`Luma::downscale_v1`].
/// 2. For each of the 64 pixels, row by row from the top left, the bit is set when
/// `pixel * 64 > sum of all 64 pixels`: when the pixel is strictly brighter than the mean, in
/// exact integer arithmetic.
///
/// The first bit is the most significant bit of the hash. A flat image hashes to all zeros.
///
/// ```
/// use legit_hashes::{Luma, average_v1};
///
/// // Dark on the left, light on the right...
/// let pixels: Vec<u8> = (0..64).map(|i| if i % 8 < 4 { 0 } else { 255 }).collect();
/// let hash = average_v1(Luma::new(8, 8, &pixels)?);
/// assert_eq!(hash.to_hex(), "0f0f0f0f0f0f0f0f");
/// # Ok::<(), legit_hashes::Error>(())
/// ```
/// Hash an image with `difference-v1`, the difference hash.
///
/// The definition, frozen:
///
/// 1. Reduce the image to 9 wide by 8 high with [`Luma::downscale_v1`].
/// 2. For each row from the top, and each column `col` from 0 to 7, the bit is set when
/// `pixel[row][col + 1] > pixel[row][col]`: when the right neighbour is strictly brighter.
///
/// The first bit is the most significant bit of the hash. A flat image hashes to all zeros.
///
/// ```
/// use legit_hashes::{Luma, difference_v1};
///
/// // Brighter to the right on every row...
/// let pixels: Vec<u8> = (0..72).map(|i| (i % 9 * 20) as u8).collect();
/// let hash = difference_v1(Luma::new(9, 8, &pixels)?);
/// assert_eq!(hash.to_hex(), "ffffffffffffffff");
/// # Ok::<(), legit_hashes::Error>(())
/// ```