Skip to content

Commit 748ce2b

Browse files
committed
feat: two images can be compared based on the number of matched pixels
1 parent 10ee77c commit 748ce2b

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

  • fxgl-core/src/main/kotlin/com/almasb/fxgl/texture

fxgl-core/src/main/kotlin/com/almasb/fxgl/texture/Images.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,4 +679,25 @@ fun interpolateIntermediateImages(images: List<Image>, numFramesBetweenImages: I
679679
result += images.last()
680680

681681
return result
682+
}
683+
684+
/**
685+
* The returned value of 0 means images do not share a single pixel (x, y, color are checked).
686+
* The value of 1 means images are identical.
687+
* If images have different sizes, 0 is returned.
688+
*
689+
* @return an accuracy value [0..1] (a ratio)
690+
* representing the number of matched pixels over the number of total pixels
691+
*/
692+
fun Image.compareStrict(other: Image): Double {
693+
if (this.width != other.width || this.height != other.height)
694+
return 0.0
695+
696+
val pixels0 = toPixels(this)
697+
val pixels1 = toPixels(other)
698+
699+
val matched = pixels0.zip(pixels1)
700+
.count { (p0, p1) -> p0.color == p1.color }
701+
702+
return matched.toDouble() / pixels0.size
682703
}

0 commit comments

Comments
 (0)