Skip to content

Commit

Permalink
feat: two images can be compared based on the number of matched pixels
Browse files Browse the repository at this point in the history
  • Loading branch information
AlmasB committed Aug 23, 2024
1 parent 10ee77c commit 748ce2b
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions fxgl-core/src/main/kotlin/com/almasb/fxgl/texture/Images.kt
Original file line number Diff line number Diff line change
Expand Up @@ -679,4 +679,25 @@ fun interpolateIntermediateImages(images: List<Image>, numFramesBetweenImages: I
result += images.last()

return result
}

/**
* The returned value of 0 means images do not share a single pixel (x, y, color are checked).
* The value of 1 means images are identical.
* If images have different sizes, 0 is returned.
*
* @return an accuracy value [0..1] (a ratio)
* representing the number of matched pixels over the number of total pixels
*/
fun Image.compareStrict(other: Image): Double {
if (this.width != other.width || this.height != other.height)
return 0.0

val pixels0 = toPixels(this)
val pixels1 = toPixels(other)

val matched = pixels0.zip(pixels1)
.count { (p0, p1) -> p0.color == p1.color }

return matched.toDouble() / pixels0.size
}

0 comments on commit 748ce2b

Please sign in to comment.