You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
110 lines
3.7 KiB
110 lines
3.7 KiB
package com.sothr.imagetools.hash
|
|
|
|
import java.io.File
|
|
import javax.imageio.ImageIO
|
|
|
|
import com.sothr.imagetools.hash.`type`.DHash
|
|
|
|
import scala.collection.mutable
|
|
|
|
class HashServiceDHashTest extends HashServiceBaseTest {
|
|
|
|
def dhashTestCase(filePath: String): Long = {
|
|
val sample = new File(filePath)
|
|
val image = ImageIO.read(sample)
|
|
HashService.getDhash(dhashSetting, image)
|
|
}
|
|
|
|
test("Confirm Largest DHash Output ") {
|
|
val testData: Array[Array[Int]] = Array(
|
|
Array(1, 2, 3, 4, 5, 6, 7, 8),
|
|
Array(16, 15, 14, 13, 12, 11, 10, 9),
|
|
Array(17, 18, 19, 20, 21, 22, 23, 24),
|
|
Array(32, 31, 30, 29, 28, 27, 26, 25),
|
|
Array(33, 34, 35, 36, 37, 38, 39, 40),
|
|
Array(48, 47, 46, 45, 44, 43, 42, 41),
|
|
Array(49, 50, 51, 52, 53, 54, 55, 56),
|
|
Array(64, 63, 62, 61, 60, 59, 58, 57))
|
|
val hash = DHash.getHash(testData)
|
|
debug(s"Hash of test array: $hash")
|
|
assert(hash == Long.MaxValue)
|
|
}
|
|
|
|
test("Calculate DHash Large Sample Image 1") {
|
|
debug("Starting 'Calculate DHash Large Sample Image 1' test")
|
|
val sample = new File(LargeSampleImage1)
|
|
debug(s"Testing File: ${sample.getAbsolutePath} exists: ${sample.exists}")
|
|
val image = ImageIO.read(sample)
|
|
debug(s"Image: width: ${image.getWidth} height: ${image.getHeight}")
|
|
val hash = HashService.getDhash(dhashSetting, image)
|
|
debug(s"Testing that $hash = 4004374827879799635L")
|
|
assert(hash == 4004374827879799635L)
|
|
}
|
|
|
|
test("Calculate DHash Medium Sample Image 1") {
|
|
debug("Starting 'Calculate DHash Medium Sample Image 1' test")
|
|
val sample = new File(MediumSampleImage1)
|
|
debug(s"Testing File: ${sample.getAbsolutePath} exists: ${sample.exists}")
|
|
val image = ImageIO.read(sample)
|
|
debug(s"Image: width: ${image.getWidth} height: ${image.getHeight}")
|
|
val hash = HashService.getDhash(dhashSetting, image)
|
|
debug(s"Testing that $hash = 4004374827879799635L")
|
|
assert(hash == 4004374827879799635L)
|
|
}
|
|
|
|
test("Calculate DHash Small Sample Image 1") {
|
|
debug("Starting 'Calculate DHash Small Sample Image 1' test")
|
|
val sample = new File(SmallSampleImage1)
|
|
debug(s"Testing File: ${sample.getAbsolutePath} exists: ${sample.exists}")
|
|
val image = ImageIO.read(sample)
|
|
debug(s"Image: width: ${image.getWidth} height: ${image.getHeight}")
|
|
val hash = HashService.getDhash(dhashSetting, image)
|
|
debug(s"Testing that $hash = 4004383623972821843L")
|
|
assert(hash == 4004383623972821843L)
|
|
}
|
|
|
|
test("DHash Of Large, Medium, And Small Sample 1 Must Be Similar") {
|
|
val largeHash = dhashTestCase(LargeSampleImage1)
|
|
val mediumHash = dhashTestCase(MediumSampleImage1)
|
|
val smallHash = dhashTestCase(SmallSampleImage1)
|
|
assert(HashService.areHashSimilar(dhashSetting, largeHash, mediumHash))
|
|
assert(HashService.areHashSimilar(dhashSetting, largeHash, smallHash))
|
|
assert(HashService.areHashSimilar(dhashSetting, mediumHash, smallHash))
|
|
}
|
|
|
|
test("Benchmark DHash") {
|
|
info("Benchmarking DHash")
|
|
info("DHash Large Image 3684x2736")
|
|
val time = new mutable.MutableList[Long]()
|
|
for (_ <- 0 until benchmarkRuns) {
|
|
time += getTime {
|
|
dhashTestCase(LargeSampleImage1)
|
|
}
|
|
}
|
|
|
|
val largeMean = getMean(time.toArray[Long])
|
|
info(s"The mean time of ${time.size} tests for large was: $largeMean ms")
|
|
time.clear()
|
|
info("DHash Medium Image 1824x1368")
|
|
for (_ <- 0 until benchmarkRuns) {
|
|
time += getTime {
|
|
dhashTestCase(MediumSampleImage1)
|
|
}
|
|
}
|
|
|
|
val mediumMean = getMean(time.toArray[Long])
|
|
info(s"The mean time of ${time.size} tests for medium was: $mediumMean ms")
|
|
time.clear()
|
|
info("DHash Small Image 912x684")
|
|
for (_ <- 0 until benchmarkRuns) {
|
|
time += getTime {
|
|
dhashTestCase(SmallSampleImage1)
|
|
}
|
|
}
|
|
|
|
val smallMean = getMean(time.toArray[Long])
|
|
info(s"The mean time of ${time.size} tests for small was: $smallMean ms")
|
|
time.clear()
|
|
assert(true)
|
|
}
|
|
}
|