-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfastimagehash.go
208 lines (173 loc) · 5.11 KB
/
fastimagehash.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
package fastimagehash
import "C"
import (
"io/ioutil"
"os"
"unsafe"
)
/*
#cgo LDFLAGS: -L. -lfastimagehash
#include <fastimagehash.h>
#include <stdlib.h>
#include <string.h>
const char* Version = FASTIMAGEHASH_VERSION;
char *hash_to_hex_string_reversed_wr(void *h, int size) {
char *out = malloc(size * size / 4 + 1);
hash_to_hex_string_reversed((uchar*)h, out, size);
return out;
}
char *hash_to_hex_string_wr(void *h, int size) {
char *out = malloc(size * size / 4 + 1);
hash_to_hex_string((uchar*)h, out, size);
return out;
}
uchar *phash_mem_wr(void *buf, size_t buf_len, int hash_size, int highfreq_factor, int* ret) {
uchar *out = malloc(hash_size * hash_size / 8);
*ret = phash_mem((uchar*)buf, buf_len, out, hash_size, highfreq_factor);
return out;
}
uchar *ahash_mem_wr(void *buf, size_t buf_len, int hash_size, int* ret) {
uchar *out = malloc(hash_size * hash_size / 8);
*ret = ahash_mem((uchar*)buf, buf_len, out, hash_size);
return out;
}
uchar *mhash_mem_wr(void *buf, size_t buf_len, int hash_size, int* ret) {
uchar *out = malloc(hash_size * hash_size / 8);
*ret = mhash_mem((uchar*)buf, buf_len, out, hash_size);
return out;
}
uchar *dhash_mem_wr(void *buf, size_t buf_len, int hash_size, int* ret) {
uchar *out = malloc(hash_size * hash_size / 8);
*ret = dhash_mem((uchar*)buf, buf_len, out, hash_size);
return out;
}
uchar *whash_mem_wr(void *buf, size_t buf_len, int hash_size, int img_scale, int remove_max_ll, int* ret, _GoString_ go_wname) {
uchar *out = malloc(hash_size * hash_size / 8);
if (strncmp(_GoStringPtr(go_wname), "haar", 4) == 0) {
*ret = whash_mem((uchar*)buf, buf_len, out, hash_size, img_scale, remove_max_ll, "haar");
} else {
*ret = whash_mem((uchar*)buf, buf_len, out, hash_size, img_scale, remove_max_ll, "db4");
}
return out;
}
*/
import "C"
type Code int
const (
Ok = 0
ReadErr Code = -2
DecodeErr Code = -3
)
type Wave string
const (
Haar = "haar"
Daubechies = "db4"
)
type Hash struct {
Size int `json:"size"`
Bytes []byte `json:"bytes"`
}
var LibVersion = C.GoString(C.Version)
func retHash(hash *C.uchar, hashSize int, ret C.int) (*Hash, Code) {
if ret == Ok {
goHash := C.GoBytes(unsafe.Pointer(hash), C.int(hashSize*hashSize/8))
C.free(unsafe.Pointer(hash))
return &Hash{
Size: hashSize,
Bytes: goHash,
}, Code(ret)
}
return &Hash{
Size: hashSize,
Bytes: nil,
}, Code(ret)
}
func readAll(filepath string) ([]byte, error) {
f, err := os.Open(filepath)
if err != nil {
return nil, err
}
bytes, err := ioutil.ReadAll(f)
if err != nil {
return nil, err
}
return bytes, nil
}
func (h *Hash) ToHexStringReversed() (ret string) {
out := C.hash_to_hex_string_reversed_wr(unsafe.Pointer(&h.Bytes[0]), C.int(h.Size))
ret = C.GoString(out)
C.free(unsafe.Pointer(out))
return
}
func (h *Hash) ToHexString() (ret string) {
out := C.hash_to_hex_string_wr(unsafe.Pointer(&h.Bytes[0]), C.int(h.Size))
ret = C.GoString(out)
C.free(unsafe.Pointer(out))
return
}
func PHashFile(filepath string, hashSize, highFreqFactor int) (*Hash, Code) {
bytes, err := readAll(filepath)
if err != nil {
return nil, ReadErr
}
return PHashMem(bytes, hashSize, highFreqFactor)
}
func PHashMem(buf []byte, hashSize, highFreqFactor int) (*Hash, Code) {
var ret C.int
hash := C.phash_mem_wr(unsafe.Pointer(&buf[0]), C.size_t(len(buf)), C.int(hashSize), C.int(highFreqFactor), &ret)
return retHash(hash, hashSize, ret)
}
func AHashFile(filepath string, hashSize int) (*Hash, Code) {
bytes, err := readAll(filepath)
if err != nil {
return nil, ReadErr
}
return AHashMem(bytes, hashSize)
}
func AHashMem(buf []byte, hashSize int) (*Hash, Code) {
var ret C.int
hash := C.ahash_mem_wr(unsafe.Pointer(&buf[0]), C.size_t(len(buf)), C.int(hashSize), &ret)
return retHash(hash, hashSize, ret)
}
func MHashFile(filepath string, hashSize int) (*Hash, Code) {
bytes, err := readAll(filepath)
if err != nil {
return nil, ReadErr
}
return MHashMem(bytes, hashSize)
}
func MHashMem(buf []byte, hashSize int) (*Hash, Code) {
var ret C.int
hash := C.mhash_mem_wr(unsafe.Pointer(&buf[0]), C.size_t(len(buf)), C.int(hashSize), &ret)
return retHash(hash, hashSize, ret)
}
func DHashFile(filepath string, hashSize int) (*Hash, Code) {
bytes, err := readAll(filepath)
if err != nil {
return nil, ReadErr
}
return DHashMem(bytes, hashSize)
}
func DHashMem(buf []byte, hashSize int) (*Hash, Code) {
var ret C.int
hash := C.dhash_mem_wr(unsafe.Pointer(&buf[0]), C.size_t(len(buf)), C.int(hashSize), &ret)
return retHash(hash, hashSize, ret)
}
func WHashFile(filepath string, hashSize, imgScale int, removeMaxLL bool, wave Wave) (*Hash, Code) {
bytes, err := readAll(filepath)
if err != nil {
return nil, ReadErr
}
return WHashMem(bytes, hashSize, imgScale, removeMaxLL, wave)
}
func WHashMem(buf []byte, hashSize, imgScale int, removeMaxLL bool, wave Wave) (*Hash, Code) {
var ret C.int
var remove_max_ll C.int
if removeMaxLL {
remove_max_ll = 1
} else {
remove_max_ll = 0
}
hash := C.whash_mem_wr(unsafe.Pointer(&buf[0]), C.size_t(len(buf)), C.int(hashSize), C.int(imgScale), remove_max_ll, &ret, string(wave))
return retHash(hash, hashSize, ret)
}