Distance calculates the distance between two hashes Returns error if hash types don't match
(other *ImageHash)
| 79 | // Distance calculates the distance between two hashes |
| 80 | // Returns error if hash types don't match |
| 81 | func (h *ImageHash) Distance(other *ImageHash) (float32, error) { |
| 82 | if h.hashType != other.hashType { |
| 83 | return 0, fmt.Errorf("cannot compare hash type %d with %d", h.hashType, other.hashType) |
| 84 | } |
| 85 | |
| 86 | switch h.hashType { |
| 87 | case HashTypeSHA256: |
| 88 | if h.sha256Hash == other.sha256Hash { |
| 89 | return 0, nil |
| 90 | } |
| 91 | return 1, nil |
| 92 | |
| 93 | case HashTypeDct: |
| 94 | return h.dctHashDistance(other), nil |
| 95 | |
| 96 | default: |
| 97 | return 0, fmt.Errorf("unsupported hash type: %d", h.hashType) |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | // Dump writes the hash to a writer |
| 102 | // Format: [1 byte: type][variable: hash data] |