-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdq.go
More file actions
344 lines (280 loc) · 8.43 KB
/
pdq.go
File metadata and controls
344 lines (280 loc) · 8.43 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// Reimplementation of https://github.com/facebook/ThreatExchange/blob/main/pdq in Golang
//
// For reference, please see https://github.com/facebook/ThreatExchange/blob/main/hashing/hashing.pdf
//
// Function names are similar or the same as those in the reference C++ implementation, and
// any questions about implementation should reference that code.
package pdq
import (
"errors"
"fmt"
"image"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"math"
"os"
"time"
_ "golang.org/x/image/bmp"
_ "golang.org/x/image/tiff"
_ "golang.org/x/image/webp"
)
// HashResult contains the output of a PDQ hash operation
type HashResult struct {
Hash string
Quality int
ImageHeightTimesWidth int
HashDuration time.Duration
}
// Various constants pulled from the reference implementation
const (
LumaFromRCoeff = 0.299
LumaFromGCoeff = 0.587
LumaFromBCoeff = 0.114
PdqNumJaroszXYPasses = 2
DownsampleDims = 512
MinHashableDim = 5
)
var (
ErrInvalidFile = errors.New("invalid input file name")
)
// SEE: https://github.com/facebook/ThreatExchange/blob/main/pdq/cpp/hashing/pdqhashing.cpp#L42
var dctMatrix64 []float32
func init() {
const numRows = 16
const numCols = 64
matrixScaleFactor := math.Sqrt(2.0 / float64(numCols))
dctMatrix64 = make([]float32, numRows*numCols)
for i := range numRows {
for j := range numCols {
dctMatrix64[i*numCols+j] = float32(matrixScaleFactor * math.Cos((math.Pi/2.0/float64(numCols))*float64(i+1)*float64(2*j+1)))
}
}
}
// HashFromImage generates a PDQ hash from an image.Image
// The image should idealy be pre-resizes to 512x512 or smaller for performance reasons.
// SEE: https://github.com/facebook/ThreatExchange/blob/main/hashing/hashing.pdf, "More on Downsampling"
// Returns a HashResult containing the hash and a quality score between 0 and 100.
// Please reference the evaluation data for selecting a good quality score. From hashing.pdf:
// "Confident-match distances are up to the system designer, of course, but 30, 20, or less has been found to
// produce good results on evaluation data."
func HashFromImage(img image.Image) (*HashResult, error) {
bounds := img.Bounds()
size := bounds.Size()
imageHeightTimesWidth := size.Y * size.X
luma, numRows, numCols := loadFloatLumaFromImage(img)
fullBuffer2 := make([]float32, numRows*numCols)
hashStart := time.Now()
hash, quality := hash256FromFloatLuma(luma, fullBuffer2, numRows, numCols)
hashTime := time.Since(hashStart)
return &HashResult{
Hash: hash,
Quality: quality,
ImageHeightTimesWidth: imageHeightTimesWidth,
HashDuration: hashTime,
}, nil
}
// Opens a file at the specified file and uses image.Image to decode the image. Returns the result of
// HashFromImage. This is a convenience wrapper around HashFromImage that handles the IO and decoding for you.
// Ideally, you should call HashFromImage on your own with a 512x512 or smaller image that you have resized
// yourself. This function is provided only to match the reference implementation.
func HashFromFile(filename string) (*HashResult, error) {
if filename == "" {
return nil, ErrInvalidFile
}
file, err := os.Open(filename)
if err != nil {
return nil, fmt.Errorf("failed to open file for hashing: %w", err)
}
defer file.Close()
img, _, err := image.Decode(file)
if err != nil {
return nil, fmt.Errorf("failed to decode image: %w", err)
}
return HashFromImage(img)
}
func loadFloatLumaFromImage(img image.Image) ([]float32, int, int) {
bounds := img.Bounds()
numRows := bounds.Dy()
numCols := bounds.Dx()
luma := make([]float32, numRows*numCols)
for row := range numRows {
for col := range numCols {
// purposefully discarding alpha
r, g, b, _ := img.At(bounds.Min.X+col, bounds.Min.Y+row).RGBA()
r8 := float32(r >> 8)
g8 := float32(g >> 8)
b8 := float32(b >> 8)
luma[row*numCols+col] = LumaFromRCoeff*r8 + LumaFromGCoeff*g8 + LumaFromBCoeff*b8
}
}
return luma, numRows, numCols
}
// SEE: https://github.com/facebook/ThreatExchange/blob/main/pdq/cpp/hashing/pdqhashing.cpp#L127
func hash256FromFloatLuma(
fullBuffer1 []float32,
fullBuffer2 []float32,
numRows, numCols int,
) (string, int) {
// from reference impl, do not return a hash for images taht are too small
if numRows < MinHashableDim || numCols < MinHashableDim {
return "", 0
}
buffer64x64 := make([]float32, 64*64)
buffer16x64 := make([]float32, 16*64)
buffer16x16 := make([]float32, 16*16)
quality := float256FromFloatLuma(fullBuffer1, fullBuffer2, numRows, numCols, buffer64x64, buffer16x64, buffer16x16)
hash := convertBufferToHash(buffer16x16)
return hash, quality
}
const hexChars = "0123456789abcdef"
func convertBufferToHash(buffer16x16 []float32) string {
median := torben(buffer16x16)
words := make([]uint16, 16)
for i := range 16 {
for j := range 16 {
if buffer16x16[i*16+j] > median {
bitIndex := i*16 + j
wordIndex := bitIndex / 16
bitInWord := bitIndex % 16
words[wordIndex] |= 1 << bitInWord
}
}
}
result := make([]byte, 64)
for i := range 16 {
word := words[15-i]
offset := i * 4
result[offset+0] = hexChars[word>>12]
result[offset+1] = hexChars[(word>>8)&0xF]
result[offset+2] = hexChars[(word>>4)&0xF]
result[offset+3] = hexChars[word&0xF]
}
return string(result)
}
// SEE: https://github.com/facebook/ThreatExchange/blob/main/pdq/cpp/hashing/pdqhashing.cpp#L158
func float256FromFloatLuma(
fullBuffer1 []float32,
fullBuffer2 []float32,
numRows, numCols int,
buffer64x64 []float32,
buffer16x64 []float32,
buffer16x16 []float32,
) int {
if numRows == 64 && numCols == 64 {
copy(buffer64x64, fullBuffer1)
} else {
windowSizeAlongRows := computeJaroszFilterWindowSize(numCols, 64)
windowSizeAlongCols := computeJaroszFilterWindowSize(numRows, 64)
jaroszFilterFloat(fullBuffer1, fullBuffer2, numRows, numCols, windowSizeAlongRows, windowSizeAlongCols, PdqNumJaroszXYPasses)
decimateFloat(fullBuffer1, numRows, numCols, buffer64x64, 64, 64)
}
quality := imageDomainQualityMetric(buffer64x64)
dct64To16(buffer64x64, buffer16x64, buffer16x16)
return quality
}
// SEE: https://github.com/facebook/ThreatExchange/blob/main/pdq/cpp/hashing/pdqhashing.cpp#L318
func imageDomainQualityMetric(buffer64x64 []float32) int {
gradientSum := 0
for i := range 63 {
for j := range 64 {
u := buffer64x64[i*64+j]
v := buffer64x64[(i+1)*64+j]
d := int(math.Abs(float64((u - v) * 100 / 255)))
gradientSum += d
}
}
for i := range 64 {
for j := range 63 {
u := buffer64x64[i*64+j]
v := buffer64x64[i*64+j+1]
d := int(math.Abs(float64((u - v) * 100 / 255)))
gradientSum += d
}
}
quality := min(gradientSum/90, 100)
return quality
}
// SEE: https://github.com/facebook/ThreatExchange/blob/main/pdq/cpp/hashing/pdqhashing.cpp#L355
func dct64To16(A []float32, T []float32, B []float32) {
for i := range 16 {
dctRow := dctMatrix64[i*64:]
for j := range 64 {
var sum0, sum1, sum2, sum3 float32
for k := 0; k < 64; k += 4 {
sum0 += dctRow[k] * A[k*64+j]
sum1 += dctRow[k+1] * A[(k+1)*64+j]
sum2 += dctRow[k+2] * A[(k+2)*64+j]
sum3 += dctRow[k+3] * A[(k+3)*64+j]
}
T[i*64+j] = sum0 + sum1 + sum2 + sum3
}
}
for i := range 16 {
tRow := T[i*64:]
for j := range 16 {
dctRow := dctMatrix64[j*64:]
var sum0, sum1, sum2, sum3 float32
for k := 0; k < 64; k += 4 {
sum0 += tRow[k] * dctRow[k]
sum1 += tRow[k+1] * dctRow[k+1]
sum2 += tRow[k+2] * dctRow[k+2]
sum3 += tRow[k+3] * dctRow[k+3]
}
B[i*16+j] = sum0 + sum1 + sum2 + sum3
}
}
}
// SEE: https://github.com/facebook/ThreatExchange/blob/main/pdq/cpp/hashing/torben.cpp
func torben(m []float32) float32 {
n := len(m)
if n == 0 {
return 0
}
min, max := m[0], m[0]
for i := 1; i < n; i++ {
if m[i] < min {
min = m[i]
}
if m[i] > max {
max = m[i]
}
}
var guess, maxltguess, mingtguess float32
var less, greater, equal int
for {
guess = (min + max) / 2
less, greater, equal = 0, 0, 0
maxltguess = min
mingtguess = max
for i := range n {
if m[i] < guess {
less++
if m[i] > maxltguess {
maxltguess = m[i]
}
} else if m[i] > guess {
greater++
if m[i] < mingtguess {
mingtguess = m[i]
}
} else {
equal++
}
}
if less <= (n+1)/2 && greater <= (n+1)/2 {
break
} else if less > greater {
max = maxltguess
} else {
min = mingtguess
}
}
if less >= (n+1)/2 {
return maxltguess
} else if less+equal >= (n+1)/2 {
return guess
} else {
return mingtguess
}
}