Compare commits

..

8 Commits
v3 ... master

Author SHA1 Message Date
dad3269c77 update to v4 2020-04-26 14:55:25 -04:00
bee8c91bb5
Update fastimagehash.go 2020-04-12 13:49:12 -04:00
b0e9d9b3a7
Update fastimagehash.go 2020-04-12 11:45:06 -04:00
11d151edcb
Update fastimagehash.go 2020-04-12 11:43:44 -04:00
bcfdf954b4
Update fastimagehash.go 2020-04-12 11:39:22 -04:00
569fe641b1
Update go.mod 2020-04-11 11:49:12 -04:00
4c7782f655
Update fastimagehash.go 2020-04-10 21:21:09 -04:00
8bf61096c8
Update go.mod 2020-04-10 21:16:11 -04:00
2 changed files with 23 additions and 76 deletions

View File

@ -16,13 +16,13 @@ import (
const char* Version = FASTIMAGEHASH_VERSION;
char *hash_to_hex_string_reversed_wr(void *h, int size) {
char *out = malloc(size * 2 + 1);
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 * 2 + 1);
char *out = malloc(size * size / 4 + 1);
hash_to_hex_string((uchar*)h, out, size);
return out;
}
@ -51,27 +51,17 @@ uchar *dhash_mem_wr(void *buf, size_t buf_len, int hash_size, int* ret) {
return out;
}
uchar *whash_mem_wr(void *buf, size_t buf_len, int hash_size, int img_scale, int* ret, _GoString_ go_wname) {
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, "haar");
*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, "db4");
*ret = whash_mem((uchar*)buf, buf_len, out, hash_size, img_scale, remove_max_ll, "db4");
}
return out;
}
multi_hash_t *multi_mem_wr(void* buf, size_t buf_len, int hash_size, int ph_highfreq_factor, int wh_img_scale, int* ret, _GoString_ go_wname) {
multi_hash_t *m = multi_hash_create(hash_size);
if (strncmp(_GoStringPtr(go_wname), "haar", 4) == 0) {
*ret = multi_hash_mem(buf, buf_len, m, hash_size, ph_highfreq_factor, wh_img_scale, "haar");
} else {
*ret = multi_hash_mem(buf, buf_len, m, hash_size, ph_highfreq_factor, wh_img_scale, "db4");
}
return m;
}
*/
import "C"
@ -84,29 +74,22 @@ const (
)
type Wave string
const (
Haar = "haar"
Daubechies = "db4"
)
type Hash struct {
Size int
Bytes []byte
}
type MultiHash struct {
PHash Hash
AHash Hash
DHash Hash
WHash Hash
MHash Hash
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))
goHash := C.GoBytes(unsafe.Pointer(hash), C.int(hashSize*hashSize/8))
C.free(unsafe.Pointer(hash))
return &Hash{
@ -121,33 +104,6 @@ func retHash(hash *C.uchar, hashSize int, ret C.int) (*Hash, Code) {
}, Code(ret)
}
func retMultiHash(m *C.multi_hash_t, hashSize int, ret C.int) (*MultiHash, Code) {
if ret == Ok {
goPHash := C.GoBytes(unsafe.Pointer(m.phash), C.int(hashSize))
goAHash := C.GoBytes(unsafe.Pointer(m.ahash), C.int(hashSize))
goDHash := C.GoBytes(unsafe.Pointer(m.dhash), C.int(hashSize))
goWHash := C.GoBytes(unsafe.Pointer(m.whash), C.int(hashSize))
goMHash := C.GoBytes(unsafe.Pointer(m.mhash), C.int(hashSize))
C.multi_hash_destroy(m)
return &MultiHash{
PHash: Hash{hashSize, goPHash},
AHash: Hash{hashSize, goAHash},
DHash: Hash{hashSize, goDHash},
WHash: Hash{hashSize, goWHash},
MHash: Hash{hashSize, goMHash},
}, Code(ret)
}
return &MultiHash{
PHash: Hash{hashSize, nil},
AHash: Hash{hashSize, nil},
DHash: Hash{hashSize, nil},
WHash: Hash{hashSize, nil},
MHash: Hash{hashSize, nil},
}, Code(ret)
}
func readAll(filepath string) ([]byte, error) {
f, err := os.Open(filepath)
if err != nil {
@ -231,31 +187,22 @@ func DHashMem(buf []byte, hashSize int) (*Hash, Code) {
return retHash(hash, hashSize, ret)
}
func WHashFile(filepath string, hashSize, imgScale int, wave Wave) (*Hash, Code) {
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, wave)
return WHashMem(bytes, hashSize, imgScale, removeMaxLL, wave)
}
func WHashMem(buf []byte, hashSize, imgScale int, wave Wave) (*Hash, Code) {
func WHashMem(buf []byte, hashSize, imgScale int, removeMaxLL bool, wave Wave) (*Hash, Code) {
var ret C.int
hash := C.whash_mem_wr(unsafe.Pointer(&buf[0]), C.size_t(len(buf)), C.int(hashSize), C.int(imgScale), &ret, string(wave))
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)
}
func MultiHashFile(filepath string, hashSize, phHighFreqFactor, whImgScale int, wave Wave) (*MultiHash, Code) {
bytes, err := readAll(filepath)
if err != nil {
return nil, ReadErr
}
return MultiHashMem(bytes, hashSize, phHighFreqFactor, whImgScale, wave)
}
func MultiHashMem(buf []byte, hashSize, phHighFreqFactor, whImgScale int, wave Wave) (*MultiHash, Code) {
var ret C.int
m := C.multi_mem_wr(unsafe.Pointer(&buf[0]), C.size_t(len(buf)), C.int(hashSize),
C.int(phHighFreqFactor), C.int(whImgScale), &ret, string(wave))
return retMultiHash(m, hashSize, ret)
}

2
go.mod
View File

@ -1,3 +1,3 @@
module github.com/simon987/fastimagehash-go
go 1.13
go 1.14