Skip to content

Commit a084351

Browse files
add tolerance to bitmap snapshotting
1 parent 315b050 commit a084351

File tree

6 files changed

+44
-15
lines changed

6 files changed

+44
-15
lines changed

snapshot/src/androidMain/kotlin/com/quickbird/snapshot/Bitmap+updatePixels.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ package com.quickbird.snapshot
22

33
import android.graphics.Bitmap
44

5-
internal fun Bitmap.updatePixels(update: (x: Int, y: Int, color: Int) -> Int) {
5+
internal fun Bitmap.updatePixels(update: (x: Int, y: Int, color: Color) -> Color) {
66
(0 until width).map { x ->
77
(0 until height).map { y ->
8-
val updated = update(x, y, getPixel(x, y))
9-
setPixel(x, y, updated)
8+
val updated = update(x, y, getPixel(x, y).color)
9+
setPixel(x, y, updated.value)
1010
}
1111
}
1212
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.quickbird.snapshot
2+
3+
import androidx.annotation.ColorInt
4+
5+
data class Color(@ColorInt val value: Int)
6+
7+
val @receiver:ColorInt Int.color
8+
get() = Color(this)
Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,49 @@
11
package com.quickbird.snapshot
22

33
import android.graphics.Bitmap
4-
import android.graphics.Color
4+
import android.graphics.Color as AndroidColor
55

6-
// TODO: Wrap Color
7-
fun Diffing.Companion.bitmap(colorDiffing: Diffing<Int>) = Diffing<Bitmap> { first, second ->
8-
if (first.asByteArray().contentEquals(second.asByteArray())) null
6+
fun Diffing.Companion.bitmap(
7+
colorDiffing: Diffing<Color>,
8+
tolerance: Double = 0.0
9+
) = Diffing<Bitmap> { first, second ->
10+
val difference = first differenceTo second
11+
12+
if (difference <= tolerance) null
913
else first.copy(first.config, true).apply {
1014
updatePixels { x, y, color ->
1115
if (x < second.width && y < second.height)
12-
colorDiffing(color, second.getPixel(x, y)) ?: color
16+
colorDiffing(color, second.getPixel(x, y).color) ?: color
1317
else color
1418
}
1519
}
1620
}
1721

1822
val Diffing.Companion.colorRed
19-
get() = Diffing<Int> { first, second ->
20-
if (first == second) null
21-
else Color.RED
22-
}
23+
get() = color(AndroidColor.RED.color)
24+
25+
fun Diffing.Companion.color(differenceColor: Color) = Diffing<Color> { first, second ->
26+
if (first == second) null
27+
else differenceColor
28+
}
29+
30+
val Diffing.Companion.colorMean
31+
get() = intMean.wrap(::Color, Color::value)
2332

2433
val Diffing.Companion.intMean
2534
get() = Diffing<Int> { first, second ->
2635
if (first == second) null
2736
else first / 2 + second / 2
2837
}
38+
39+
private infix fun Bitmap.differenceTo(other: Bitmap): Double {
40+
val thisBytes = this.asByteArray()
41+
val otherBytes = other.asByteArray()
42+
if (thisBytes == null || otherBytes == null || thisBytes.size != otherBytes.size) return 100.0
43+
44+
val differentBytes = thisBytes
45+
.zip(otherBytes, Byte::equals)
46+
.count { !it }
47+
48+
return differentBytes.toDouble() / thisBytes.size
49+
}

snapshot/src/androidMain/kotlin/com/quickbird/snapshot/Snapshotting+composeScreenshot.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import androidx.compose.ui.test.onRoot
99

1010
val Snapshotting.Companion.composeNodeScreenshot
1111
get() = Snapshotting(
12-
diffing = Diffing.bitmap(colorDiffing = Diffing.intMean),
12+
diffing = Diffing.bitmap(colorDiffing = Diffing.colorMean),
1313
snapshot = { node: SemanticsNodeInteraction -> node.captureToImage().asAndroidBitmap() }
1414
)
1515

snapshot/src/androidMain/kotlin/com/quickbird/snapshot/Snapshotting+viewScreenshot.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import android.view.View
66

77
val Snapshotting.Companion.viewScreenshot
88
get() = Snapshotting(
9-
diffing = Diffing.bitmap(colorDiffing = Diffing.intMean),
9+
diffing = Diffing.bitmap(colorDiffing = Diffing.colorMean),
1010
snapshot = { view: View ->
1111
Bitmap.createBitmap(view.width, view.height, Bitmap.Config.ARGB_8888).apply {
1212
view.draw(Canvas(this))

snapshot/src/jvmMain/kotlin/com/quickbird/snapshot/androidTransfer/AndroidSnapshotTestFiles.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ data class AndroidSnapshotTestFiles(
1111
private val directory: AndroidSnapshotDirectory,
1212
private val appBundle: String,
1313
private val diffFilePrefix: String = "diff",
14-
private val androidDirectory: String = "/data/data/$appBundle/files/snapshots",
14+
private val androidDirectory: String = "/data/data/$appBundle/files/snapshot",
1515
private val tmpAndroidDirectory: String = "/sdcard/snapshot_tmp",
1616
private val deviceIds: List<String> = listAdbDevices()
1717
) {

0 commit comments

Comments
 (0)