-
-
Notifications
You must be signed in to change notification settings - Fork 71
/
imagehash.go
299 lines (261 loc) · 6.43 KB
/
imagehash.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
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
// Copyright 2017 The goimagehash Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package goimagehash
import (
"encoding/binary"
"encoding/gob"
"encoding/hex"
"errors"
"fmt"
"io"
)
var errNoOther = errors.New("other should not be nil")
// Kind describes the kinds of hash.
type Kind int
// ImageHash is a struct of hash computation.
type ImageHash struct {
hash uint64
kind Kind
}
// ExtImageHash is a struct of big hash computation.
type ExtImageHash struct {
hash []uint64
kind Kind
bits int
}
const (
// Unknown is a enum value of the unknown hash.
Unknown Kind = iota
// AHash is a enum value of the average hash.
AHash
//PHash is a enum value of the perceptual hash.
PHash
// DHash is a enum value of the difference hash.
DHash
// WHash is a enum value of the wavelet hash.
WHash
)
// NewImageHash function creates a new image hash.
func NewImageHash(hash uint64, kind Kind) *ImageHash {
return &ImageHash{hash: hash, kind: kind}
}
// Bits method returns an actual hash bit size
func (h *ImageHash) Bits() int {
return 64
}
// Distance method returns a distance between two hashes.
func (h *ImageHash) Distance(other *ImageHash) (int, error) {
if other == nil {
return -1, errNoOther
}
if h.GetKind() != other.GetKind() {
return -1, errors.New("Image hashes's kind should be identical")
}
lhash := h.GetHash()
rhash := other.GetHash()
hamming := lhash ^ rhash
return popcnt(hamming), nil
}
// GetHash method returns a 64bits hash value.
func (h *ImageHash) GetHash() uint64 {
return h.hash
}
// GetKind method returns a kind of image hash.
func (h *ImageHash) GetKind() Kind {
return h.kind
}
func (h *ImageHash) leftShiftSet(idx int) {
h.hash |= 1 << uint(idx)
}
const strFmt = "%1s:%016x"
// Dump method writes a binary serialization into w io.Writer.
func (h *ImageHash) Dump(w io.Writer) error {
type D struct {
Hash uint64
Kind Kind
}
enc := gob.NewEncoder(w)
err := enc.Encode(D{Hash: h.hash, Kind: h.kind})
if err != nil {
return err
}
return nil
}
// LoadImageHash method loads a ImageHash from io.Reader.
func LoadImageHash(b io.Reader) (*ImageHash, error) {
type E struct {
Hash uint64
Kind Kind
}
var e E
dec := gob.NewDecoder(b)
err := dec.Decode(&e)
if err != nil {
return nil, err
}
return &ImageHash{hash: e.Hash, kind: e.Kind}, nil
}
// ImageHashFromString returns an image hash from a hex representation
//
// Deprecated: Use goimagehash.LoadImageHash instead.
func ImageHashFromString(s string) (*ImageHash, error) {
var kindStr string
var hash uint64
_, err := fmt.Sscanf(s, strFmt, &kindStr, &hash)
if err != nil {
return nil, errors.New("Couldn't parse string " + s)
}
kind := Unknown
switch kindStr {
case "a":
kind = AHash
case "p":
kind = PHash
case "d":
kind = DHash
case "w":
kind = WHash
}
return NewImageHash(hash, kind), nil
}
// ToString returns a hex representation of the hash
func (h *ImageHash) ToString() string {
kindStr := ""
switch h.kind {
case AHash:
kindStr = "a"
case PHash:
kindStr = "p"
case DHash:
kindStr = "d"
case WHash:
kindStr = "w"
}
return fmt.Sprintf(strFmt, kindStr, h.hash)
}
// NewExtImageHash function creates a new big hash
func NewExtImageHash(hash []uint64, kind Kind, bits int) *ExtImageHash {
return &ExtImageHash{hash: hash, kind: kind, bits: bits}
}
// Bits method returns an actual hash bit size
func (h *ExtImageHash) Bits() int {
return h.bits
}
// Distance method returns a distance between two big hashes
func (h *ExtImageHash) Distance(other *ExtImageHash) (int, error) {
if h.GetKind() != other.GetKind() {
return -1, errors.New("Extended Image hashes's kind should be identical")
}
if h.Bits() != other.Bits() {
msg := fmt.Sprintf("Extended image hash should has an identical bit size but got %v vs %v", h.Bits(), other.Bits())
return -1, errors.New(msg)
}
lHash := h.GetHash()
rHash := other.GetHash()
if len(lHash) != len(rHash) {
return -1, errors.New("Extended Image hashes's size should be identical")
}
distance := 0
for idx, lh := range lHash {
rh := rHash[idx]
hamming := lh ^ rh
distance += popcnt(hamming)
}
return distance, nil
}
// GetHash method returns a big hash value
func (h *ExtImageHash) GetHash() []uint64 {
return h.hash
}
// GetKind method returns a kind of big hash
func (h *ExtImageHash) GetKind() Kind {
return h.kind
}
// Dump method writes a binary serialization into w io.Writer.
func (h *ExtImageHash) Dump(w io.Writer) error {
type D struct {
Hash []uint64
Kind Kind
Bits int
}
enc := gob.NewEncoder(w)
err := enc.Encode(D{Hash: h.hash, Kind: h.kind, Bits: h.bits})
if err != nil {
return err
}
return nil
}
// LoadExtImageHash method loads a ExtImageHash from io.Reader.
func LoadExtImageHash(b io.Reader) (*ExtImageHash, error) {
type E struct {
Hash []uint64
Kind Kind
Bits int
}
var e E
dec := gob.NewDecoder(b)
err := dec.Decode(&e)
if err != nil {
return nil, err
}
return &ExtImageHash{hash: e.Hash, kind: e.Kind, bits: e.Bits}, nil
}
const extStrFmt = "%1s:%s"
// ExtImageHashFromString returns a big hash from a hex representation
//
// Deprecated: Use goimagehash.LoadExtImageHash instead.
func ExtImageHashFromString(s string) (*ExtImageHash, error) {
var kindStr string
var hashStr string
_, err := fmt.Sscanf(s, extStrFmt, &kindStr, &hashStr)
if err != nil {
return nil, errors.New("Couldn't parse string " + s)
}
hexBytes, err := hex.DecodeString(hashStr)
if err != nil {
return nil, err
}
var hash []uint64
lenOfByte := 8
for i := 0; i < len(hexBytes)/lenOfByte; i++ {
startIndex := i * lenOfByte
endIndex := startIndex + lenOfByte
hashUint64 := binary.BigEndian.Uint64(hexBytes[startIndex:endIndex])
hash = append(hash, hashUint64)
}
kind := Unknown
switch kindStr {
case "a":
kind = AHash
case "p":
kind = PHash
case "d":
kind = DHash
case "w":
kind = WHash
}
return NewExtImageHash(hash, kind, len(hash)*64), nil
}
// ToString returns a hex representation of big hash
func (h *ExtImageHash) ToString() string {
var hexBytes []byte
for _, hash := range h.hash {
hashBytes := make([]byte, 8)
binary.BigEndian.PutUint64(hashBytes, hash)
hexBytes = append(hexBytes, hashBytes...)
}
hexStr := hex.EncodeToString(hexBytes)
kindStr := ""
switch h.kind {
case AHash:
kindStr = "a"
case PHash:
kindStr = "p"
case DHash:
kindStr = "d"
case WHash:
kindStr = "w"
}
return fmt.Sprintf(extStrFmt, kindStr, hexStr)
}