Skip to content

Commit

Permalink
feat: tests now required before release
Browse files Browse the repository at this point in the history
  • Loading branch information
AskAlexSharov committed Feb 23, 2019
1 parent 5cd5109 commit e4fc29b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 20 deletions.
20 changes: 4 additions & 16 deletions avg/distance.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package avg

import (
"context"
"math"

"github.com/AskAlexSharov/imgdiff/loader"
"github.com/Nr90/imgsim"
Expand All @@ -16,28 +17,15 @@ func Distance(ctx context.Context, fileName1, fileName2 string) (int, error) {
if r1.Err != nil {
return 0, r1.Err
}

ahash1 := imgsim.AverageHash(r1.Img)

r2 := <-ch
if r2.Err != nil {
return 0, r2.Err
}
ahash2 := imgsim.AverageHash(r2.Img)
percents := float64(imgsim.Distance(ahash1, ahash2)) / 64 * 100 // because 64 bit hash

return imgsim.Distance(ahash1, ahash2) % 64, nil // because 64 bit hash
return int(math.Round(percents)), nil
}

//func Difference(img1, img2 image.Image) int {
// // Hashing
// ahash1 := imgsim.AverageHash(img1)
// ahash2 := imgsim.AverageHash(img2)
//
// dhash1 := imgsim.DifferenceHash(img1)
// dhash2 := imgsim.DifferenceHash(img2)
//
// // distance
// avgDistance := imgsim.Distance(ahash1, ahash2)
// diffDistance := imgsim.Distance(dhash1, dhash2)
//
// return (avgDistance + diffDistance) % 128
//}
14 changes: 10 additions & 4 deletions avg/distance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ func Test_distance(t *testing.T) {
wantDistance: 0,
},
{
name: "Original vs Damaged - 1%",
name: "Original vs Damaged - 2%",
file1: "./test-png-original.png",
file2: "https://raw.githubusercontent.com/AskAlexSharov/imgdiff/master/avg/test-png-damaged.png",
wantDistance: 1,
wantDistance: 2,
},
{
name: "Scaled Down vs Damaged - 1%",
name: "Scaled Down vs Damaged - 2%",
file1: "https://raw.githubusercontent.com/AskAlexSharov/imgdiff/master/avg/test-png-scaled-down.png",
file2: "./test-png-damaged.png",
wantDistance: 1,
wantDistance: 2,
},
}
for _, tc := range tests {
Expand All @@ -45,3 +45,9 @@ func Test_distance(t *testing.T) {
})
}
}

//func BenchmarkHello(b *testing.B) {
// for i := 0; i < b.N; i++ {
// Distance(context.Background(), "./test-png-original.png", "./test-png-scaled-down.png")
// }
//}

0 comments on commit e4fc29b

Please sign in to comment.