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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//! `blockhash-v1`, the block mean value hash.
use crate::;
/// Blocks a side: 16 × 16 blocks, one bit each.
const GRID: usize = 16;
/// Blocks in a band of four rows, thresholded against their own median.
const BAND: usize = GRID * GRID / 4;
/// What a fully transparent pixel counts as: white, `255 + 255 + 255`.
const TRANSPARENT: u64 = 765;
/// Hash an image with `blockhash-v1`, the block mean value hash.
///
/// The definition, frozen, is the "precise" method of [blockhash](https://github.com/commonsmachinery/blockhash)
/// at 16 blocks a side, as its Python port, [blockhash-python](https://github.com/commonsmachinery/blockhash-python)
/// at commit `f28b741a1f6bfff25763ae42dc2190051079998c`, computes it: `blockhash(image, 16)`. That
/// implements the method of Bian Yang, Fan Gu and Xiamu Niu. Given the same RGBA pixels, this
/// function gives the bits blockhash-python gives. The image isn't resized first.
///
/// 1. A pixel's value is `R + G + B`, from 0 to 765; a pixel with alpha 0 counts as 765, white,
/// whatever its colour. Any other alpha is ignored.
/// 2. Cut the image into 16 × 16 blocks, each `width / 16` pixels wide and `height / 16` high,
/// which needn't be whole numbers. Along a side of `n` pixels, pixel `p`, counting from 0, goes
/// to the blocks this way:
/// - When 16 divides `n`, all of it goes to block `16p / n`, in integer division.
/// - Otherwise, let `r = (p + 1) mod (n / 16)`, and `f` its fractional part. When `r` is at
/// least 1, or `p` is the last pixel, all of it goes to block `⌊16p / n⌋`. Otherwise `1 - f`
/// of it goes to block `⌊16p / n⌋` and `f` to block `⌈16p / n⌉`.
///
/// A pixel's share of a block is its share along the width times its share along the height.
/// On a side of 16 pixels or more this is the area of the pixel that lies in the block. On a
/// shorter side, where a block is narrower than a pixel, it isn't: some blocks get nothing.
/// 3. A block's value is the sum of the values of its pixels, each times its share.
/// 4. Split the 256 blocks, taken row by row, into four bands of four rows. In each band, let `m`
/// be the median of its 64 block values, the mean of the two middle ones, and
/// `half = width · height · 1.5`, the value of a block of pixels whose channels are all 128.
/// A block's bit is set when its value `v` is above `m`, or when `|v - m| < 1` and `m > half`.
/// So blocks equal to a median at or below mid-grey stay clear, and those equal to a brighter
/// median are set, which keeps a mostly black or mostly white image from hashing to all zeros.
/// 5. The bits run in block order, row by row from the top left, the first in the most significant
/// bit of the first byte, as blockhash prints them.
///
/// The reference computes the shares in `f64`. Each share is a multiple of 1/16 along each side,
/// so each product of a value and two shares is a multiple of 1/256 below 1,024, and every sum and
/// median it forms stays below `2^53 / 256`: `f64` holds them all exactly. This function computes
/// the same quantities times 256, in `u64`, so its comparisons are the reference's, ties included,
/// and every platform computes the same hash.
///
/// Each band has exactly 32 of its 64 bits set when its block values are distinct and no value
/// other than one above the median lies within 1 of it. A band of equal values is all clear or all
/// set; a flat image hashes to all zeros unless it's brighter than mid-grey.
///
/// The C blockhash agrees except at a tie: it sums in `f32`, and for a size 16 doesn't divide it
/// truncates the block area to an integer before it computes `half`.
///
/// ```
/// use legit_hashes::{Algorithm, Rgba, RgbaBuf, blockhash_v1};
///
/// // A white square on black, 16 pixels a side in the middle of 32: the bits of the middle
/// // eight blocks of the middle eight rows are set...
/// let rgb: Vec<u8> = (0..32 * 32)
/// .flat_map(|i| if (8..24).contains(&(i % 32)) && (8..24).contains(&(i / 32)) { [255; 3] } else { [0; 3] })
/// .collect();
/// let hash = blockhash_v1(RgbaBuf::from_rgb8(32, 32, &rgb)?.view());
/// assert_eq!(hash.algorithm(), Algorithm::BlockhashV1);
/// let middle = "0ff0".repeat(8);
/// assert_eq!(hash.to_hex(), format!("{}{middle}{}", "0".repeat(16), "0".repeat(16)));
///
/// // ...and transparent pixels count as white.
/// let clear = blockhash_v1(Rgba::new(16, 16, &[0; 16 * 16 * 4])?);
/// assert_eq!(clear.to_hex(), "f".repeat(64));
/// # Ok::<(), legit_hashes::Error>(())
/// ```
/// Where one pixel along a side goes: its share of each of two blocks, in sixteenths. Both blocks
/// are the same when the pixel lies within one.
/// Step 2 for each pixel along a side of `side` pixels.