diff --git a/README.md b/README.md index 722197d..e811d42 100644 --- a/README.md +++ b/README.md @@ -11,9 +11,8 @@ for `fastimagehash-go` to compile. package main import ( - "github.com/simon987/fastimagehash-go" "fmt" -) + "github.com/simon987/fastimagehash-go" ) func main() { hash, ret := fastimagehash.PHashFile("/path/to/image.jpg", 8, 4) diff --git a/fastimagehash.go b/fastimagehash.go index 71feb56..7fbdcd1 100644 --- a/fastimagehash.go +++ b/fastimagehash.go @@ -78,27 +78,43 @@ type MultiHash struct { } func retHash(hash *C.uchar, hashSize int, ret C.int) (*Hash, Code) { - goHash := C.GoBytes(unsafe.Pointer(hash), C.int(hashSize)) - C.free(unsafe.Pointer(hash)) + if ret == Ok { + goHash := C.GoBytes(unsafe.Pointer(hash), C.int(hashSize)) + C.free(unsafe.Pointer(hash)) + + return &Hash{ + Size: hashSize, + Bytes: goHash, + }, Code(ret) + } return &Hash{ Size: hashSize, - Bytes: goHash, + Bytes: nil, }, Code(ret) } func retMultiHash(m *C.multi_hash_t, hashSize int, ret C.int) (*MultiHash, Code) { - 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)) - C.multi_hash_destroy(m) + 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)) + C.multi_hash_destroy(m) + + return &MultiHash{ + PHash: Hash{hashSize, goPHash}, + AHash: Hash{hashSize, goAHash}, + DHash: Hash{hashSize, goDHash}, + WHash: Hash{hashSize, goWHash}, + }, Code(ret) + } return &MultiHash{ - PHash: Hash{hashSize, goPHash}, - AHash: Hash{hashSize, goAHash}, - DHash: Hash{hashSize, goDHash}, - WHash: Hash{hashSize, goWHash}, + PHash: Hash{hashSize, nil}, + AHash: Hash{hashSize,nil }, + DHash: Hash{hashSize,nil }, + WHash: Hash{hashSize,nil }, }, Code(ret) }