Compare commits

..

No commits in common. "master" and "v3" have entirely different histories.
master ... v3

2 changed files with 76 additions and 23 deletions

View File

@ -16,13 +16,13 @@ import (
const char* Version = FASTIMAGEHASH_VERSION; const char* Version = FASTIMAGEHASH_VERSION;
char *hash_to_hex_string_reversed_wr(void *h, int size) { char *hash_to_hex_string_reversed_wr(void *h, int size) {
char *out = malloc(size * size / 4 + 1); char *out = malloc(size * 2 + 1);
hash_to_hex_string_reversed((uchar*)h, out, size); hash_to_hex_string_reversed((uchar*)h, out, size);
return out; return out;
} }
char *hash_to_hex_string_wr(void *h, int size) { char *hash_to_hex_string_wr(void *h, int size) {
char *out = malloc(size * size / 4 + 1); char *out = malloc(size * 2 + 1);
hash_to_hex_string((uchar*)h, out, size); hash_to_hex_string((uchar*)h, out, size);
return out; return out;
} }
@ -51,45 +51,62 @@ uchar *dhash_mem_wr(void *buf, size_t buf_len, int hash_size, int* ret) {
return out; 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 *whash_mem_wr(void *buf, size_t buf_len, int hash_size, int img_scale, int* ret, _GoString_ go_wname) {
uchar *out = malloc(hash_size * hash_size / 8); uchar *out = malloc(hash_size * hash_size / 8);
if (strncmp(_GoStringPtr(go_wname), "haar", 4) == 0) { if (strncmp(_GoStringPtr(go_wname), "haar", 4) == 0) {
*ret = whash_mem((uchar*)buf, buf_len, out, hash_size, img_scale, remove_max_ll, "haar"); *ret = whash_mem((uchar*)buf, buf_len, out, hash_size, img_scale, "haar");
} else { } else {
*ret = whash_mem((uchar*)buf, buf_len, out, hash_size, img_scale, remove_max_ll, "db4"); *ret = whash_mem((uchar*)buf, buf_len, out, hash_size, img_scale, "db4");
} }
return out; 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" import "C"
type Code int type Code int
const ( const (
Ok = 0 Ok = 0
ReadErr Code = -2 ReadErr Code = -2
DecodeErr Code = -3 DecodeErr Code = -3
) )
type Wave string type Wave string
const ( const (
Haar = "haar" Haar = "haar"
Daubechies = "db4" Daubechies = "db4"
) )
type Hash struct { type Hash struct {
Size int `json:"size"` Size int
Bytes []byte `json:"bytes"` Bytes []byte
}
type MultiHash struct {
PHash Hash
AHash Hash
DHash Hash
WHash Hash
MHash Hash
} }
var LibVersion = C.GoString(C.Version) var LibVersion = C.GoString(C.Version)
func retHash(hash *C.uchar, hashSize int, ret C.int) (*Hash, Code) { func retHash(hash *C.uchar, hashSize int, ret C.int) (*Hash, Code) {
if ret == Ok { if ret == Ok {
goHash := C.GoBytes(unsafe.Pointer(hash), C.int(hashSize*hashSize/8)) goHash := C.GoBytes(unsafe.Pointer(hash), C.int(hashSize))
C.free(unsafe.Pointer(hash)) C.free(unsafe.Pointer(hash))
return &Hash{ return &Hash{
@ -104,6 +121,33 @@ func retHash(hash *C.uchar, hashSize int, ret C.int) (*Hash, Code) {
}, Code(ret) }, 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) { func readAll(filepath string) ([]byte, error) {
f, err := os.Open(filepath) f, err := os.Open(filepath)
if err != nil { if err != nil {
@ -187,22 +231,31 @@ func DHashMem(buf []byte, hashSize int) (*Hash, Code) {
return retHash(hash, hashSize, ret) return retHash(hash, hashSize, ret)
} }
func WHashFile(filepath string, hashSize, imgScale int, removeMaxLL bool, wave Wave) (*Hash, Code) { func WHashFile(filepath string, hashSize, imgScale int, wave Wave) (*Hash, Code) {
bytes, err := readAll(filepath) bytes, err := readAll(filepath)
if err != nil { if err != nil {
return nil, ReadErr return nil, ReadErr
} }
return WHashMem(bytes, hashSize, imgScale, removeMaxLL, wave) return WHashMem(bytes, hashSize, imgScale, wave)
} }
func WHashMem(buf []byte, hashSize, imgScale int, removeMaxLL bool, wave Wave) (*Hash, Code) { func WHashMem(buf []byte, hashSize, imgScale int, wave Wave) (*Hash, Code) {
var ret C.int var ret C.int
var remove_max_ll 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))
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) 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 module github.com/simon987/fastimagehash-go
go 1.14 go 1.13