-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhash.go
More file actions
129 lines (112 loc) · 2.46 KB
/
Copy pathhash.go
File metadata and controls
129 lines (112 loc) · 2.46 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
package gosnap
import (
"image"
"math"
"math/big"
"math/bits"
"strings"
)
var Zero = Hash{value: big.NewInt(0)}
type Hash struct {
value *big.Int
}
func (h Hash) String() string {
return h.value.Text(62)
}
func HashString(text string) Hash {
return hashString(text)
}
func hashString(text string) Hash {
b := big.NewInt(0)
b.SetString(text, 62)
return Hash{value: b}
}
func (h Hash) Equal(other Hash, distance int) bool {
_, eq := h.equal(other, distance)
return eq
}
func (h Hash) equal(other Hash, distance int) (z Hash, equal bool) {
z = h.Xor(other)
equal = z.onesCount() <= distance
return
}
func (h Hash) onesCount() int {
var n = 0
for _, word := range h.value.Bits() {
n += bits.OnesCount(uint(word))
}
return n
}
func (h Hash) Xor(other Hash) Hash {
xor := big.NewInt(0).Xor(h.value, other.value)
return Hash{value: xor}
}
func (h Hash) Or(other Hash) Hash {
or := big.NewInt(0).Or(h.value, other.value)
return Hash{value: or}
}
func MakeGrayAndHash(img image.Image, bits uint) (*image.Gray, Hash) {
px := int(math.Sqrt(float64(bits)))
gray := grayScale(img, px, px)
return gray, Hash{value: grayToBigInt(gray)}
}
func MakeHash(img image.Image, bits uint) Hash {
_, hash := MakeGrayAndHash(img, bits)
return hash
}
func (h Hash) MarshalJSON() ([]byte, error) {
if h.value == nil {
return []byte("null"), nil
}
return []byte(`"` + h.String() + `"`), nil
}
func (h *Hash) UnmarshalJSON(b []byte) (err error) {
if string(b) == "null" {
return nil
}
str := strings.Trim(string(b), `"`)
*h = hashString(str)
return nil
}
func (h *Hash) SquareString(sq int) string {
s := strings.Builder{}
pow := (sq * sq) - sq
sqso := sq - 1
for n := 0; n < pow; n++ {
if h.value.Bit(n) == 1 {
s.WriteByte('1')
} else {
s.WriteByte('0')
}
if (n+1)%sqso == 0 {
s.WriteByte('\n')
}
}
return s.String()
}
// hash.Equal(others1.Hash.Or(others2.Hash), distance)
func (h *Hash) EqualUnion(others []Hash, distance int) bool {
maxBits := len(h.value.Bits())
for i := range others {
if l := len(others[i].value.Bits()); l > maxBits {
maxBits = l
}
}
currentDistance := 0
for i := 0; i < maxBits; i++ {
var or, val big.Word
for j := 0; j < len(others); j++ {
if i < len(others[j].value.Bits()) {
or |= others[j].value.Bits()[i]
}
}
if i < len(h.value.Bits()) {
val = h.value.Bits()[i]
}
currentDistance += bits.OnesCount(uint(val) ^ uint(or))
if currentDistance > distance {
return false
}
}
return true
}