Skip to content

Commit 08373d9

Browse files
authored
Merge pull request #8 from poslegm/api-refactoring
Package name simplified
2 parents f716362 + e13eb05 commit 08373d9

File tree

11 files changed

+44
-47
lines changed

11 files changed

+44
-47
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ My library implements three Perceptual Hashing algorithms: Radial Hash, DCT hash
1414

1515
```scala
1616
resolvers += "Sonatype OSS Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots"
17-
libraryDependencies += "com.github.poslegm" %% "scala-phash" % "1.1.1"
17+
libraryDependencies += "com.github.poslegm" %% "scala-phash" % "1.2.0"
1818
```
1919

2020
#### API
@@ -25,12 +25,12 @@ There is three functions for each hashing algorithm. Let's consider them by exam
2525

2626
Similar functions written for Marr and Radial Hash algorithms.
2727

28-
All public api with scaladocs decsribed in trait [`PHashAlgebra`](https://github.com/poslegm/scala-phash/blob/master/src/main/scala/com/github/poslegm/scalaphash/PHashAlgebra.scala).
28+
All public api with scaladocs decsribed in object [`PHash`](https://github.com/poslegm/scala-phash/blob/master/src/main/scala/com/github/poslegm/scalaphash/PHash.scala).
2929

3030
#### Example
3131

3232
```scala
33-
import com.github.poslegm.scalaphash.PHash._
33+
import scalaphash.PHash._
3434
import javax.imageio.ImageIO
3535

3636
val img1 = ImageIO.read(new File("img1.jpg"))

build.sbt

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name := "scala-phash"
22

33
organization := "com.github.poslegm"
44

5-
version := "1.1.1"
5+
version := "1.2.0"
66

77
scalaVersion := "2.12.6"
88
crossScalaVersions := Seq("2.11.8", "2.12.1", scalaVersion.value)

src/main/scala/com/github/poslegm/scalaphash/MathUtils.scala renamed to src/main/scala/scalaphash/MathUtils.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.github.poslegm.scalaphash
1+
package scalaphash
22

33
private[scalaphash] object MathUtils {
44
type FloatMatrix = Array[Array[Float]]
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
package com.github.poslegm.scalaphash
1+
package scalaphash
22

33
import java.awt.image.BufferedImage
44

5-
trait PHashAlgebra {
5+
import scala.util.control.NonFatal
6+
7+
object PHash {
68
type DCTHash = Long
79
type MarrHash = Array[Int]
810
type RadialHash = Array[Int]
@@ -13,21 +15,23 @@ trait PHashAlgebra {
1315
* @param image image for hashing
1416
* @return 64-bit hash value or exception
1517
* */
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) }
1721

1822
/**
1923
* Computes DCT hash value of image
2024
* (http://www.phash.org/docs/pubs/thesis_zauner.pdf / page 21)
2125
* @param image image for hashing
2226
* @return 64-bit hash value
2327
* */
24-
def unsafeDctHash(image: BufferedImage): DCTHash
28+
def unsafeDctHash(image: BufferedImage): DCTHash = PHashInternal.unsafeDctHash(image)
2529

2630
/**
2731
* Computes distance between two DCT hashes
2832
* Less is better
2933
* */
30-
def dctHashDistance(hash1: DCTHash, hash2: DCTHash): Long
34+
def dctHashDistance(hash1: DCTHash, hash2: DCTHash): Long = PHashInternal.dctHashDistance(hash1, hash2)
3135

3236
/**
3337
* Computes Marr hash value of image
@@ -37,7 +41,9 @@ trait PHashAlgebra {
3741
* @param level coefficient for correlation kernel
3842
* @return hash as int array or exception
3943
* */
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) }
4147

4248
/**
4349
* Computes Marr hash value of image
@@ -47,13 +53,14 @@ trait PHashAlgebra {
4753
* @param level coefficient for correlation kernel
4854
* @return hash as int array
4955
* */
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)
5158

5259
/**
5360
* Computes distance between two Marr hashes
5461
* Less is better
5562
* */
56-
def marrHashDistance(hash1: MarrHash, hash2: MarrHash): Option[Double]
63+
def marrHashDistance(hash1: MarrHash, hash2: MarrHash): Option[Double] = PHashInternal.marrHashDistance(hash1, hash2)
5764

5865
/**
5966
* Computes Radial hash value of image
@@ -62,7 +69,9 @@ trait PHashAlgebra {
6269
* @param projectionsCount number of projections to compute
6370
* @return hash as int array or exception
6471
* */
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) }
6675

6776
/**
6877
* Computes Radial hash value of image
@@ -71,11 +80,12 @@ trait PHashAlgebra {
7180
* @param projectionsCount number of projections to compute
7281
* @return hash as int array
7382
* */
74-
def unsafeRadialHash(image: BufferedImage, projectionsCount: Int = 180): RadialHash
83+
def unsafeRadialHash(image: BufferedImage, projectionsCount: Int = 180): RadialHash =
84+
PHashInternal.unsafeRadialHash(image, projectionsCount)
7585

7686
/**
7787
* Computes distance between two Radial hashes
7888
* More is better
7989
* */
80-
def radialHashDistance(hash1: RadialHash, hash2: RadialHash): Double
90+
def radialHashDistance(hash1: RadialHash, hash2: RadialHash): Double = PHashInternal.radialHashDistance(hash1, hash2)
8191
}

src/main/scala/com/github/poslegm/scalaphash/PHash.scala renamed to src/main/scala/scalaphash/PhashInternal.scala

+10-23
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
1-
package com.github.poslegm.scalaphash
1+
package scalaphash
22

33
import java.awt.image.BufferedImage
44

5-
import com.github.poslegm.scalaphash.MathUtils._
5+
import scalaphash.MathUtils._
6+
import scalaphash.PHash._
67

78
import scala.annotation.tailrec
89
import scala.collection.mutable.ArrayBuffer
9-
import scala.util.control.NonFatal
1010

11-
object PHash extends PHashAlgebra {
11+
private[scalaphash] object PHashInternal {
1212
private lazy val dctMatrix = createDctMatrix(32)
1313
private lazy val dctMatrixTransposed = dctMatrix.transpose
1414

15-
override def dctHash(image: BufferedImage): Either[Throwable, DCTHash] =
16-
try Right(unsafeDctHash(image))
17-
catch { case NonFatal(e) => Left(e) }
18-
19-
override def unsafeDctHash(image: BufferedImage): DCTHash = {
15+
def unsafeDctHash(image: BufferedImage): DCTHash = {
2016
val processedImage = PixelMatrix(image).makeGrayScale().makeConvolved()
2117
val matrix = processedImage.resize(32, 32).toMatrix
2218
val dctImage = dctMatrix * matrix * dctMatrixTransposed
@@ -29,7 +25,7 @@ object PHash extends PHashAlgebra {
2925
}
3026
}
3127

32-
override def dctHashDistance(hash1: DCTHash, hash2: DCTHash): Long = {
28+
def dctHashDistance(hash1: DCTHash, hash2: DCTHash): Long = {
3329
var x = hash1 ^ hash2
3430
val m1 = 0x5555555555555555L
3531
val m2 = 0x3333333333333333L
@@ -41,12 +37,7 @@ object PHash extends PHashAlgebra {
4137
(x * h01) >> 56
4238
}
4339

44-
override 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) }
47-
48-
override def unsafeMarrHash(image: BufferedImage, alpha: Int = 2, level: Int = 1): MarrHash = {
49-
40+
def unsafeMarrHash(image: BufferedImage, alpha: Int, level: Int): MarrHash = {
5041
val processed = PixelMatrix(image).makeGrayScale().makeBlurred().resize(512, 512).equalize(256)
5142

5243
val kernel = createMarrKernel(alpha, level)
@@ -88,7 +79,7 @@ object PHash extends PHashAlgebra {
8879
hash.toArray
8980
}
9081

91-
override def marrHashDistance(hash1: MarrHash, hash2: MarrHash): Option[Double] =
82+
def marrHashDistance(hash1: MarrHash, hash2: MarrHash): Option[Double] =
9283
if (hash1.length != hash2.length || hash1.isEmpty) {
9384
None
9485
} else {
@@ -99,11 +90,7 @@ object PHash extends PHashAlgebra {
9990
Some(distance / maxBitsCount)
10091
}
10192

102-
override def radialHash(image: BufferedImage, projectionsCount: Int = 180): Either[Throwable, RadialHash] =
103-
try Right(unsafeRadialHash(image, projectionsCount))
104-
catch { case NonFatal(e) => Left(e) }
105-
106-
override def unsafeRadialHash(image: BufferedImage, projectionsCount: Int = 180): RadialHash = {
93+
def unsafeRadialHash(image: BufferedImage, projectionsCount: Int): RadialHash = {
10794
val grayscaled = if (image.getColorModel.getColorSpace.getNumComponents >= 3) {
10895
PixelMatrix(image).makeGrayScale()
10996
} else {
@@ -116,7 +103,7 @@ object PHash extends PHashAlgebra {
116103
calculateRadialHash(features)
117104
}
118105

119-
override def radialHashDistance(hash1: RadialHash, hash2: RadialHash): Double = {
106+
def radialHashDistance(hash1: RadialHash, hash2: RadialHash): Double = {
120107
val meanX: Double = hash1.sum / hash2.length
121108
val meanY: Double = hash2.sum / hash2.length
122109
var max = 0.0

src/main/scala/com/github/poslegm/scalaphash/PixelMatrix.scala renamed to src/main/scala/scalaphash/PixelMatrix.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.github.poslegm.scalaphash
1+
package scalaphash
22

33
import java.awt.image._
44
import java.awt.RenderingHints

src/main/scala/com/github/poslegm/scalaphash/RadialProjections.scala renamed to src/main/scala/scalaphash/RadialProjections.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.github.poslegm.scalaphash
1+
package scalaphash
22

33
/**
44
* Utilitary class for projections computing (Radial Hash algorithm)

src/test/scala/com/github/poslegm/scalaphash/DCTHashTest.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.github.poslegm.scalaphash
1+
package scalaphash
22

33
import java.io.File
44
import javax.imageio.ImageIO
@@ -18,7 +18,7 @@ class DCTHashTest extends FlatSpec with Matchers with PrivateMethodTester {
1818
).map(_.map(_.toFloat))
1919

2020
val createDctMatrix = PrivateMethod[Array[Array[Float]]]('createDctMatrix)
21-
val matrix = PHash invokePrivate createDctMatrix(6)
21+
val matrix = PHashInternal invokePrivate createDctMatrix(6)
2222
matrix.length shouldEqual canonical.length
2323
matrix(0).length shouldEqual canonical(0).length
2424

src/test/scala/com/github/poslegm/scalaphash/ImageUtilsTest.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.github.poslegm.scalaphash
1+
package scalaphash
22

33
import java.io.File
44
import javax.imageio.ImageIO

src/test/scala/com/github/poslegm/scalaphash/MarrHashTest.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.github.poslegm.scalaphash
1+
package scalaphash
22

33
import java.io.File
44
import javax.imageio.ImageIO
@@ -25,7 +25,7 @@ class MarrHashTest extends FlatSpec with Matchers with PrivateMethodTester {
2525
).map(_.map(_.toFloat))
2626

2727
val createMarrKernel = PrivateMethod[Array[Array[Float]]]('createMarrKernel)
28-
val matrix = PHash invokePrivate createMarrKernel(1, 1)
28+
val matrix = PHashInternal invokePrivate createMarrKernel(1, 1)
2929
matrix.length shouldEqual canonical.length
3030
matrix.indices.foreach(i => matrix(i).length shouldEqual canonical(i).length)
3131

src/test/scala/com/github/poslegm/scalaphash/RadialHashTest.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.github.poslegm.scalaphash
1+
package scalaphash
22

33
import java.io.File
44
import javax.imageio.ImageIO

0 commit comments

Comments
 (0)