1 /* <lambda>null2 * Copyright 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package platform.test.screenshot.matchers 18 19 import android.graphics.Bitmap 20 import android.graphics.Color 21 import android.graphics.Rect 22 import kotlin.collections.List 23 import platform.test.screenshot.proto.ScreenshotResultProto 24 25 /** Bitmap matching that does an exact comparison of pixels between bitmaps. */ 26 class PixelPerfectMatcher : BitmapMatcher() { 27 28 override fun compareBitmaps( 29 expected: IntArray, 30 given: IntArray, 31 width: Int, 32 height: Int, 33 regions: List<Rect> 34 ): MatchResult { 35 check(expected.size == given.size) 36 37 val filter = getFilter(width, height, regions) 38 var different = 0 39 var same = 0 40 var ignored = 0 41 42 val diffArray = lazy { IntArray(width * height) { Color.TRANSPARENT } } 43 44 expected.indices.forEach { index -> 45 when { 46 !filter[index] -> ignored++ 47 expected[index] == given[index] -> same++ 48 else -> diffArray.value[index] = Color.MAGENTA.also { different++ } 49 } 50 } 51 52 val stats = 53 ScreenshotResultProto.DiffResult.ComparisonStatistics.newBuilder() 54 .setNumberPixelsCompared(width * height) 55 .setNumberPixelsIdentical(same) 56 .setNumberPixelsDifferent(different) 57 .setNumberPixelsIgnored(ignored) 58 .build() 59 60 return if (different > 0) { 61 val diff = Bitmap.createBitmap(diffArray.value, width, height, Bitmap.Config.ARGB_8888) 62 MatchResult(matches = false, diff = diff, comparisonStatistics = stats) 63 } else { 64 MatchResult(matches = true, diff = null, comparisonStatistics = stats) 65 } 66 } 67 } 68