hashcompute: Fix bit set index (#22)

This commit is contained in:
Dong-hee Na 2019-03-16 17:22:57 +09:00 committed by GitHub
parent 5f00903fec
commit 58a4aa88ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 13 deletions

View File

@ -30,7 +30,7 @@ func AverageHash(img image.Image) (*ImageHash, error) {
for idx, p := range flattens { for idx, p := range flattens {
if p > avg { if p > avg {
ahash.Set(idx) ahash.leftShiftSet(len(flattens) - idx - 1)
} }
} }
@ -52,7 +52,7 @@ func DifferenceHash(img image.Image) (*ImageHash, error) {
for i := 0; i < len(pixels); i++ { for i := 0; i < len(pixels); i++ {
for j := 0; j < len(pixels[i])-1; j++ { for j := 0; j < len(pixels[i])-1; j++ {
if pixels[i][j] < pixels[i][j+1] { if pixels[i][j] < pixels[i][j+1] {
dhash.Set(idx) dhash.leftShiftSet(64 - idx - 1)
} }
idx++ idx++
} }
@ -78,7 +78,7 @@ func PerceptionHash(img image.Image) (*ImageHash, error) {
for idx, p := range flattens { for idx, p := range flattens {
if p > median { if p > median {
phash.Set(idx) phash.leftShiftSet(len(flattens) - idx - 1)
} }
} }
return phash, nil return phash, nil
@ -101,8 +101,8 @@ func PerceptionHashExtend(img image.Image, hashSize int) (*ExtImageHash, error)
lenOfUnit := 64 lenOfUnit := 64
phash := make([]uint64, imgSize/lenOfUnit) phash := make([]uint64, imgSize/lenOfUnit)
for idx, p := range flattens { for idx, p := range flattens {
indexOfArray := (imgSize - 1 - idx) / lenOfUnit indexOfArray := idx / lenOfUnit
indexOfBit := idx % lenOfUnit indexOfBit := lenOfUnit - idx%lenOfUnit - 1
if p > median { if p > median {
phash[indexOfArray] |= 1 << uint(indexOfBit) phash[indexOfArray] |= 1 << uint(indexOfBit)
} }
@ -127,8 +127,8 @@ func AverageHashExtend(img image.Image, hashSize int) (*ExtImageHash, error) {
lenOfUnit := 64 lenOfUnit := 64
ahash := make([]uint64, imgSize/lenOfUnit) ahash := make([]uint64, imgSize/lenOfUnit)
for idx, p := range flattens { for idx, p := range flattens {
indexOfArray := (imgSize - 1 - idx) / lenOfUnit indexOfArray := idx / lenOfUnit
indexOfBit := idx % lenOfUnit indexOfBit := lenOfUnit - idx%lenOfUnit - 1
if p > avg { if p > avg {
ahash[indexOfArray] |= 1 << uint(indexOfBit) ahash[indexOfArray] |= 1 << uint(indexOfBit)
} }
@ -153,8 +153,8 @@ func DifferenceHashExtend(img image.Image, hashSize int) (*ExtImageHash, error)
idx := 0 idx := 0
for i := 0; i < len(pixels); i++ { for i := 0; i < len(pixels); i++ {
for j := 0; j < len(pixels[i])-1; j++ { for j := 0; j < len(pixels[i])-1; j++ {
indexOfArray := (imgSize - 1 - idx) / lenOfUnit indexOfArray := idx / lenOfUnit
indexOfBit := idx % lenOfUnit indexOfBit := lenOfUnit - idx%lenOfUnit - 1
if pixels[i][j] < pixels[i][j+1] { if pixels[i][j] < pixels[i][j+1] {
dhash[indexOfArray] |= 1 << uint(indexOfBit) dhash[indexOfArray] |= 1 << uint(indexOfBit)
} }

View File

@ -67,8 +67,7 @@ func (h *ImageHash) GetKind() Kind {
return h.kind return h.kind
} }
// Set method sets a bit of index. func (h *ImageHash) leftShiftSet(idx int) {
func (h *ImageHash) Set(idx int) {
h.hash |= 1 << uint(idx) h.hash |= 1 << uint(idx)
} }

View File

@ -34,13 +34,13 @@ func TestNewImageHash(t *testing.T) {
for i := 0; i < len(data1); i++ { for i := 0; i < len(data1); i++ {
if data1[i] == 1 { if data1[i] == 1 {
hash1.Set(i) hash1.leftShiftSet(i)
} }
} }
for i := 0; i < len(data2); i++ { for i := 0; i < len(data2); i++ {
if data2[i] == 1 { if data2[i] == 1 {
hash2.Set(i) hash2.leftShiftSet(i)
} }
} }