Add json marshaling/unmarshaling
This commit is contained in:
parent
003cba6e1b
commit
33ff96e45f
4
go.mod
4
go.mod
@ -8,8 +8,8 @@ require (
|
||||
github.com/disintegration/imaging v1.6.2
|
||||
github.com/gen2brain/avif v0.3.1
|
||||
github.com/spakin/netpbm v1.3.0
|
||||
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8
|
||||
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8
|
||||
golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa
|
||||
golang.org/x/image v0.19.0
|
||||
)
|
||||
|
||||
require (
|
||||
|
7
go.sum
7
go.sum
@ -8,10 +8,11 @@ github.com/spakin/netpbm v1.3.0 h1:eDX7VvrkN5sHXW0luZXRA4AKDlLmu0E5sNxJ7VSTwxc=
|
||||
github.com/spakin/netpbm v1.3.0/go.mod h1:Q+ep6vNv1G44qSWp0wt3Y9o1m/QXjmaXZIFC0PMVpq0=
|
||||
github.com/tetratelabs/wazero v1.7.1 h1:QtSfd6KLc41DIMpDYlJdoMc6k7QTN246DM2+n2Y/Dx8=
|
||||
github.com/tetratelabs/wazero v1.7.1/go.mod h1:ytl6Zuh20R/eROuyDaGPkp82O9C/DJfXAwJfQ3X6/7Y=
|
||||
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8 h1:aAcj0Da7eBAtrTp03QXWvm88pSyOt+UgdZw2BFZ+lEw=
|
||||
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8/go.mod h1:CQ1k9gNrJ50XIzaKCRR2hssIjF07kZFEiieALBM/ARQ=
|
||||
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 h1:hVwzHzIUGRjiF7EcUjqNxk3NCfkPxbDKRdnNE1Rpg0U=
|
||||
golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa h1:ELnwvuAXPNtPk1TJRuGkI9fDTwym6AYBu0qzT8AcHdI=
|
||||
golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa/go.mod h1:akd2r19cwCdwSwWeIdzYQGa/EZZyqcOdwWiwj5L5eKQ=
|
||||
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
|
||||
golang.org/x/image v0.19.0 h1:D9FX4QWkLfkeqaC62SonffIIuYdOk/UE2XKUBgRIBIQ=
|
||||
golang.org/x/image v0.19.0/go.mod h1:y0zrRqlQRWQ5PXaYCOMLTW2fpsxZ8Qh9I/ohnInJEys=
|
||||
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
|
||||
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
|
41
imagehash.go
41
imagehash.go
@ -8,6 +8,7 @@ import (
|
||||
"encoding/binary"
|
||||
"encoding/gob"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
@ -36,19 +37,55 @@ const (
|
||||
Unknown Kind = iota
|
||||
// AHash is a enum value of the average hash.
|
||||
AHash
|
||||
// PHash is a enum value of the perceptual hash.
|
||||
PHash
|
||||
// DHash is a enum value of the difference hash.
|
||||
DHash
|
||||
// PHash is a enum value of the perceptual hash.
|
||||
PHash
|
||||
// WHash is a enum value of the wavelet hash.
|
||||
WHash
|
||||
)
|
||||
|
||||
var kindNames = map[Kind]string{
|
||||
Unknown: "Unknown",
|
||||
AHash: "ahash",
|
||||
DHash: "dhash",
|
||||
PHash: "phash",
|
||||
WHash: "whash",
|
||||
}
|
||||
|
||||
var nameKinds = map[string]Kind{
|
||||
"Unknown": Unknown,
|
||||
"ahash": AHash,
|
||||
"dhash": DHash,
|
||||
"phash": PHash,
|
||||
"whash": WHash,
|
||||
}
|
||||
|
||||
// NewImageHash function creates a new image hash.
|
||||
func NewImageHash(hash uint64, kind Kind) *ImageHash {
|
||||
return &ImageHash{hash: hash, kind: kind}
|
||||
}
|
||||
|
||||
func (k Kind) String() string {
|
||||
if name, ok := kindNames[k]; ok {
|
||||
return name
|
||||
}
|
||||
return "Unknown"
|
||||
}
|
||||
func (k *Kind) UnmarshalJSON(b []byte) error {
|
||||
var s string
|
||||
if err := json.Unmarshal(b, &s); err != nil {
|
||||
return err
|
||||
}
|
||||
*k = nameKinds[s]
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (k *Kind) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(k.String())
|
||||
}
|
||||
|
||||
// Bits method returns an actual hash bit size
|
||||
func (h *ImageHash) Bits() int {
|
||||
return 64
|
||||
|
Loading…
Reference in New Issue
Block a user