Skip to content

Commit

Permalink
Merge pull request #15 from ansman/fix/14
Browse files Browse the repository at this point in the history
Fix a crash when comparing differently sized images
  • Loading branch information
rharter authored Feb 2, 2024
2 parents 957ada7 + e575a28 commit 807cccc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class SimpleImageComparator(
(b..t).forEach { offsetY ->
if (offsetX != x || offsetY != y) {
// If we're out of bounds for either of the images, return false
if (x >= minOf(left.width, right.width) || y >= minOf(left.height, right.height)) return false
if (offsetX >= minOf(left.width, right.width) || offsetY >= minOf(left.height, right.height)) return false

val c1 = left.getPixel(offsetX, offsetY)
val localDeltaThreshold = color.distance(c1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,14 @@ class ShiftComparisonTest {
assertEquals(comparator.hShift, 0)
assertEquals(comparator.vShift, 10)
}

@Test fun `returns DIFFERENT for different sized images`() {
val first = TestImage(width = 1080, height = 1080)
val second = TestImage(width = 1080, height = 540)
second.setPixel(second.width - 1, second.height - 1, Color(0f, 0f, 0f, 0f))

val comparator = SimpleImageComparator(hShift = 1, vShift = 1)
assertEquals(540 * 1080 + 1, comparator.compare(first, second).pixelDifferences)
assertEquals(540 * 1080, comparator.compare(second, first).pixelDifferences)
}
}

0 comments on commit 807cccc

Please sign in to comment.