1
- package com . github . poslegm . scalaphash
1
+ package scalaphash
2
2
3
3
import java .awt .image .BufferedImage
4
4
5
- trait PHashAlgebra {
5
+ import scala .util .control .NonFatal
6
+
7
+ object PHash {
6
8
type DCTHash = Long
7
9
type MarrHash = Array [Int ]
8
10
type RadialHash = Array [Int ]
@@ -13,21 +15,23 @@ trait PHashAlgebra {
13
15
* @param image image for hashing
14
16
* @return 64-bit hash value or exception
15
17
* */
16
- def dctHash (image : BufferedImage ): Either [Throwable , DCTHash ]
18
+ def dctHash (image : BufferedImage ): Either [Throwable , DCTHash ] =
19
+ try Right (unsafeDctHash(image))
20
+ catch { case NonFatal (e) => Left (e) }
17
21
18
22
/**
19
23
* Computes DCT hash value of image
20
24
* (http://www.phash.org/docs/pubs/thesis_zauner.pdf / page 21)
21
25
* @param image image for hashing
22
26
* @return 64-bit hash value
23
27
* */
24
- def unsafeDctHash (image : BufferedImage ): DCTHash
28
+ def unsafeDctHash (image : BufferedImage ): DCTHash = PHashInternal .unsafeDctHash(image)
25
29
26
30
/**
27
31
* Computes distance between two DCT hashes
28
32
* Less is better
29
33
* */
30
- def dctHashDistance (hash1 : DCTHash , hash2 : DCTHash ): Long
34
+ def dctHashDistance (hash1 : DCTHash , hash2 : DCTHash ): Long = PHashInternal .dctHashDistance(hash1, hash2)
31
35
32
36
/**
33
37
* Computes Marr hash value of image
@@ -37,7 +41,9 @@ trait PHashAlgebra {
37
41
* @param level coefficient for correlation kernel
38
42
* @return hash as int array or exception
39
43
* */
40
- def marrHash (image : BufferedImage , alpha : Int = 2 , level : Int = 1 ): Either [Throwable , MarrHash ]
44
+ def marrHash (image : BufferedImage , alpha : Int = 2 , level : Int = 1 ): Either [Throwable , MarrHash ] =
45
+ try Right (unsafeMarrHash(image, alpha, level))
46
+ catch { case NonFatal (e) => Left (e) }
41
47
42
48
/**
43
49
* Computes Marr hash value of image
@@ -47,13 +53,14 @@ trait PHashAlgebra {
47
53
* @param level coefficient for correlation kernel
48
54
* @return hash as int array
49
55
* */
50
- def unsafeMarrHash (image : BufferedImage , alpha : Int = 2 , level : Int = 1 ): MarrHash
56
+ def unsafeMarrHash (image : BufferedImage , alpha : Int = 2 , level : Int = 1 ): MarrHash =
57
+ PHashInternal .unsafeMarrHash(image, alpha, level)
51
58
52
59
/**
53
60
* Computes distance between two Marr hashes
54
61
* Less is better
55
62
* */
56
- def marrHashDistance (hash1 : MarrHash , hash2 : MarrHash ): Option [Double ]
63
+ def marrHashDistance (hash1 : MarrHash , hash2 : MarrHash ): Option [Double ] = PHashInternal .marrHashDistance(hash1, hash2)
57
64
58
65
/**
59
66
* Computes Radial hash value of image
@@ -62,7 +69,9 @@ trait PHashAlgebra {
62
69
* @param projectionsCount number of projections to compute
63
70
* @return hash as int array or exception
64
71
* */
65
- def radialHash (image : BufferedImage , projectionsCount : Int = 180 ): Either [Throwable , RadialHash ]
72
+ def radialHash (image : BufferedImage , projectionsCount : Int = 180 ): Either [Throwable , RadialHash ] =
73
+ try Right (unsafeRadialHash(image, projectionsCount))
74
+ catch { case NonFatal (e) => Left (e) }
66
75
67
76
/**
68
77
* Computes Radial hash value of image
@@ -71,11 +80,12 @@ trait PHashAlgebra {
71
80
* @param projectionsCount number of projections to compute
72
81
* @return hash as int array
73
82
* */
74
- def unsafeRadialHash (image : BufferedImage , projectionsCount : Int = 180 ): RadialHash
83
+ def unsafeRadialHash (image : BufferedImage , projectionsCount : Int = 180 ): RadialHash =
84
+ PHashInternal .unsafeRadialHash(image, projectionsCount)
75
85
76
86
/**
77
87
* Computes distance between two Radial hashes
78
88
* More is better
79
89
* */
80
- def radialHashDistance (hash1 : RadialHash , hash2 : RadialHash ): Double
90
+ def radialHashDistance (hash1 : RadialHash , hash2 : RadialHash ): Double = PHashInternal .radialHashDistance(hash1, hash2)
81
91
}
0 commit comments