Skip to content
This repository has been archived by the owner on May 16, 2022. It is now read-only.

Commit

Permalink
Add ability to tune hashing algorithm and image resolution
Browse files Browse the repository at this point in the history
Closes #2

Signed-off-by: Igor Shishkin <me@teran.ru>
  • Loading branch information
teran committed Sep 11, 2019
1 parent 927c386 commit f27c60f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 8 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/teran/imgsum
go 1.13

require (
github.com/Soreil/arw v0.0.0-20180907190942-c71045f5319b
github.com/soreil/arw v0.0.0-20180907190942-c71045f5319b
github.com/bamiaux/rez v0.0.0-20170731184118-29f4463c688b
github.com/brett-lempereur/ish v0.0.0-20161214150457-bbdc45bcf55d
github.com/gonum/blas v0.0.0-20181208220705-f22b278b28ac
Expand Down
29 changes: 29 additions & 0 deletions image/hasher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package image

import (
"fmt"

"github.com/brett-lempereur/ish"
)

// HashType type to represent the list of hashers available
type HashType string

var (
// HashTypeAvg reflects AverageHash
HashTypeAvg HashType = "avg"

// HashTypeDiff reflects DiffrenceHash
HashTypeDiff HashType = "diff"
)

func getHasher(kind HashType, res int) (ish.PerceptualHash, error) {
switch kind {
case HashTypeAvg:
return ish.NewAverageHash(res, res), nil
case HashTypeDiff:
return ish.NewDifferenceHash(res, res), nil
default:
return nil, fmt.Errorf("Unknown hasher: %s", kind)
}
}
7 changes: 3 additions & 4 deletions image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import (
"os"
"regexp"

"github.com/Soreil/arw"
"github.com/brett-lempereur/ish"
"github.com/nf/cr2"
"github.com/soreil/arw"
)

var (
Expand Down Expand Up @@ -117,8 +116,8 @@ func (i *Image) openSonyRaw() error {

// Hexdigest produces image hash and returns it as string
// actually the string is SHA256 from the hasher's value
func (i *Image) Hexdigest() (string, error) {
hasher := ish.NewAverageHash(1024, 1024)
func (i *Image) Hexdigest(hashKind HashType, res int) (string, error) {
hasher, err := getHasher(hashKind, res)
dh, err := hasher.Hash(i.image)
if err != nil {
return "", err
Expand Down
12 changes: 9 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ var (
date = "0000-00-00T00:00:00Z"
)

func calculate(file string) error {
func calculate(file string, hashKind image.HashType, res int) error {
i, err := image.NewImage(file)
if err != nil {
fmt.Fprintln(os.Stderr, file, err.Error())
wg.Done()
return err
}

h, err := i.Hexdigest()
h, err := i.Hexdigest(hashKind, res)
if err != nil {
fmt.Fprintln(os.Stderr, file, err.Error())
wg.Done()
Expand Down Expand Up @@ -117,6 +117,10 @@ func main() {
fmt.Printf(" Read file list from stdin as a JSON({'files':['file1', 'file2']}) and calculate their hash\n")
fmt.Printf(" -json-output\n")
fmt.Printf(" Return duplicates as a JSON(useful for IPC)\n")
fmt.Printf(" -hash-kind avg|diff")
fmt.Printf(" Allows to set hash function: average or difference (avg by default)")
fmt.Printf(" -hash-resolution 1024")
fmt.Printf(" Allows to set the (squared) image resolution to pass to hashing function (1024 by default)")
fmt.Printf(" -version\n")
fmt.Printf(" Print imgsum version\n\n")
fmt.Printf("Examples:\n")
Expand All @@ -132,6 +136,8 @@ func main() {
json_input := flag.Bool("json-input", false, "")
json_output := flag.Bool("json-output", false, "")
versionFlag := flag.Bool("version", false, "")
hashKind := flag.String("hash-kind", "avg", "")
hashResolution := flag.Int("hash-resolution", 1024, "")

flag.Parse()
if flag.NArg() < 1 && !*json_input && !*versionFlag {
Expand Down Expand Up @@ -168,7 +174,7 @@ func main() {
filename := files[file]
wg.Add(1)
go func() {
calculate(filename)
calculate(filename, image.HashType(*hashKind), *hashResolution)
defer func() {
<-sem
}()
Expand Down

0 comments on commit f27c60f

Please sign in to comment.