|
@ -11,17 +11,22 @@ object DHash extends PerceptualHasher with Logging { |
|
|
val width = imageData.length |
|
|
val width = imageData.length |
|
|
val height = imageData(0).length |
|
|
val height = imageData(0).length |
|
|
debug(s"Image data size: ${width}x${height}") |
|
|
debug(s"Image data size: ${width}x${height}") |
|
|
var hash = 0L |
|
|
|
|
|
for (row <- 0 until width) { |
|
|
|
|
|
var previousPixel = imageData(row)(0) |
|
|
|
|
|
var previousLocation = (row, 0) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//calculate dhash |
|
|
|
|
|
var hash = 0 |
|
|
|
|
|
var previousPixel = imageData(height-1)(width-1) |
|
|
|
|
|
var previousLocation = (height-1, width-1) |
|
|
|
|
|
if (height % 2 == 0) { |
|
|
|
|
|
previousPixel = imageData(height-1)(0) |
|
|
|
|
|
previousLocation = (height-1, 0) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
for (row <- 0 until height by 2) { |
|
|
//process each column |
|
|
//process each column |
|
|
for (col <- 0 until height) { |
|
|
|
|
|
debug(s"previousPixel: $previousPixel previousLocation: $previousLocation") |
|
|
|
|
|
//println(f"Column: $col%d") |
|
|
|
|
|
|
|
|
for (col <- 0 until width by 1) { |
|
|
hash <<= 1 |
|
|
hash <<= 1 |
|
|
val pixel = imageData(row)(col) |
|
|
val pixel = imageData(row)(col) |
|
|
|
|
|
//debug(s"previousPixel: $previousPixel currentPixel: $pixel previousLocation: $previousLocation currentLocation: (${row},${col})") |
|
|
//binary or the current bit based on whether the value |
|
|
//binary or the current bit based on whether the value |
|
|
//of the current pixel is greater or equal to the previous pixel |
|
|
//of the current pixel is greater or equal to the previous pixel |
|
|
if (pixel >= previousPixel) hash |= 1 else hash |= 0 |
|
|
if (pixel >= previousPixel) hash |= 1 else hash |= 0 |
|
@ -29,7 +34,24 @@ object DHash extends PerceptualHasher with Logging { |
|
|
previousPixel = pixel |
|
|
previousPixel = pixel |
|
|
previousLocation = (row, col) |
|
|
previousLocation = (row, col) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if ((row +1) < width) { |
|
|
|
|
|
val nextRow = row + 1 |
|
|
|
|
|
//process each column |
|
|
|
|
|
for (col <- (width - 1) to 0 by -1) { |
|
|
|
|
|
hash <<= 1 |
|
|
|
|
|
val pixel = imageData(nextRow)(col) |
|
|
|
|
|
//debug(s"previousPixel: $previousPixel currentPixel: $pixel previousLocation: $previousLocation currentLocation: (${nextRow},${col})") |
|
|
|
|
|
//binary or the current bit based on whether the value |
|
|
|
|
|
//of the current pixel is greater or equal to the previous pixel |
|
|
|
|
|
if (pixel >= previousPixel) hash |= 1 else hash |= 0 |
|
|
|
|
|
//debug(s"hash: $hash") |
|
|
|
|
|
previousPixel = pixel |
|
|
|
|
|
previousLocation = (nextRow, col) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
debug(s"Computed Hash: $hash from ${width * height} pixels") |
|
|
hash |
|
|
hash |
|
|
} |
|
|
} |
|
|
} |
|
|
} |