-
Notifications
You must be signed in to change notification settings - Fork 0
/
difference.go
63 lines (53 loc) · 1.65 KB
/
difference.go
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
package ish
import (
"image"
"github.com/steakknife/hamming"
)
// DifferenceHash implements the difference hash perceptual hashing algorithm
// described by Dr. Neal Krawetz.
type DifferenceHash struct {
width int
height int
}
// NewDifferenceHash returns a new instance of DifferenceHash.
func NewDifferenceHash(width, height int) *DifferenceHash {
if height <= 0 || width <= 0 {
panic("Width and height must be positive")
}
if height&(height-1) != 0 || width&(width-1) != 0 {
panic("Width and height must be powers of two")
}
return &DifferenceHash{width, height}
}
// Hash computes the difference hash of an image by shrinking it and comparing
// the relative brightness of pixels.
func (dh *DifferenceHash) Hash(img image.Image) ([]byte, error) {
// Prepare the image by converting it to grayscale and resizing it, if
// either or both of these operations have been performed, the cost of
// these calls is negligible.
ig := Grayscale(img)
is, err := Resize(ig, dh.width+1, dh.height)
if err != nil {
return nil, err
}
// Compute the difference hash by scanning the image horizontally and
// comparing the brightness of pixels.
hash := make([]byte, dh.Length())
for y := 0; y < dh.height; y++ {
for x := 0; x < dh.width; x++ {
i := uint(y*dh.width + x)
if is.Pix[i] < is.Pix[i+1] {
hash[i>>3] |= 1 << (i & 7)
}
}
}
return hash, nil
}
// Length returns the length of the difference hash in bytes.
func (dh *DifferenceHash) Length() int {
return (dh.width * dh.height) / 8
}
// Distance returns the hamming distance between two difference hashes.
func (dh *DifferenceHash) Distance(ha, hb []byte) int {
return hamming.Bytes(ha, hb)
}