Home | History | Annotate | Download | only in bitmapverifiers
      1 /*
      2  * Copyright (C) 2014 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 package android.uirendering.cts.bitmapverifiers;
     17 
     18 import android.graphics.Bitmap;
     19 import android.graphics.Color;
     20 import androidx.annotation.ColorInt;
     21 import android.uirendering.cts.util.CompareUtils;
     22 import android.util.Log;
     23 
     24 /**
     25  * This class looks at every pixel in a given bitmap and verifies that it is correct.
     26  */
     27 public abstract class PerPixelBitmapVerifier extends BitmapVerifier {
     28     private static final String TAG = "PerPixelBitmapVerifer";
     29     public static final int DEFAULT_THRESHOLD = 48;
     30 
     31     // total color difference tolerated without the pixel failing
     32     private int mColorTolerance;
     33 
     34     // portion of bitmap allowed to fail pixel check
     35     private float mSpatialTolerance;
     36 
     37     public PerPixelBitmapVerifier() {
     38         this(DEFAULT_THRESHOLD, 0);
     39     }
     40 
     41     public PerPixelBitmapVerifier(int colorTolerance) {
     42         this(colorTolerance, 0);
     43     }
     44 
     45     public PerPixelBitmapVerifier(int colorTolerance, float spatialTolerance) {
     46         mColorTolerance = colorTolerance;
     47         mSpatialTolerance = spatialTolerance;
     48     }
     49 
     50     @ColorInt
     51     protected int getExpectedColor(int x, int y) {
     52         return Color.WHITE;
     53     }
     54 
     55     public boolean verify(int[] bitmap, int offset, int stride, int width, int height) {
     56         int failures = 0;
     57         int[] differenceMap = new int[bitmap.length];
     58         for (int y = 0 ; y < height ; y++) {
     59             for (int x = 0 ; x < width ; x++) {
     60                 int index = indexFromXAndY(x, y, stride, offset);
     61                 if (!verifyPixel(x, y, bitmap[index])) {
     62                     if (failures < 50) {
     63                         Log.d(TAG, "Expected : " + Integer.toHexString(getExpectedColor(x, y))
     64                                 + " received : " + Integer.toHexString(bitmap[index])
     65                                 + " at position (" + x + "," + y + ")");
     66                     }
     67                     failures++;
     68                     differenceMap[index] = FAIL_COLOR;
     69                 } else {
     70                     differenceMap[index] = PASS_COLOR;
     71                 }
     72             }
     73         }
     74         int toleratedFailures = (int) (mSpatialTolerance * width * height);
     75         boolean success = failures <= toleratedFailures;
     76         Log.d(TAG, failures + " failures observed out of "
     77                 + toleratedFailures + " tolerated failures");
     78         if (!success) {
     79             mDifferenceBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
     80             mDifferenceBitmap.setPixels(differenceMap, offset, stride, 0, 0, width, height);
     81         }
     82         return success;
     83     }
     84 
     85 
     86     protected boolean verifyPixel(int x, int y, int observedColor) {
     87         int expectedColor = getExpectedColor(x, y);
     88         return CompareUtils.verifyPixelWithThreshold(observedColor, expectedColor, mColorTolerance);
     89     }
     90 }
     91