1 /* 2 * Copyright (C) 2020 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 android.uirendering.cts.bitmapverifiers; 18 19 import android.graphics.Color; 20 21 public class BlurPixelVerifier extends BitmapVerifier { 22 23 private final int mDstColor; 24 private final int mSrcColor; 25 26 /** 27 * Create a BitmapVerifier that compares pixel values relative to the 28 * provided source and destination colors. Pixels closer to the center of 29 * the test bitmap are expected to match closer to the source color, while pixels 30 * on the exterior of the test bitmap are expected to match the destination 31 * color more closely 32 */ BlurPixelVerifier(int srcColor, int dstColor)33 public BlurPixelVerifier(int srcColor, int dstColor) { 34 mSrcColor = srcColor; 35 mDstColor = dstColor; 36 } 37 38 @Override verify(int[] bitmap, int offset, int stride, int width, int height)39 public boolean verify(int[] bitmap, int offset, int stride, int width, int height) { 40 41 float dstRedChannel = Color.red(mDstColor); 42 float dstGreenChannel = Color.green(mDstColor); 43 float dstBlueChannel = Color.blue(mDstColor); 44 45 float srcRedChannel = Color.red(mSrcColor); 46 float srcGreenChannel = Color.green(mSrcColor); 47 float srcBlueChannel = Color.blue(mSrcColor); 48 49 // Calculate the largest rgb color difference between the source and destination 50 // colors 51 double maxDifference = Math.pow(srcRedChannel - dstRedChannel, 2.0f) 52 + Math.pow(srcGreenChannel - dstGreenChannel, 2.0f) 53 + Math.pow(srcBlueChannel - dstBlueChannel, 2.0f); 54 55 // Calculate the maximum distance between pixels to the center of the test image 56 double maxPixelDistance = 57 Math.sqrt(Math.pow(width / 2.0, 2.0) + Math.pow(height / 2.0, 2.0)); 58 59 // Additional tolerance applied to comparisons 60 float threshold = .05f; 61 for (int x = 0; x < width; x++) { 62 for (int y = 0; y < height; y++) { 63 double pixelDistance = Math.sqrt(Math.pow(x - width / 2.0, 2.0) 64 + Math.pow(y - height / 2.0, 2.0)); 65 // Calculate the threshold of the destination color expected based on the 66 // pixels position relative to the center 67 double dstPercentage = pixelDistance / maxPixelDistance + threshold; 68 69 int pixelColor = bitmap[indexFromXAndY(x, y, stride, offset)]; 70 double pixelRedChannel = Color.red(pixelColor); 71 double pixelGreenChannel = Color.green(pixelColor); 72 double pixelBlueChannel = Color.blue(pixelColor); 73 // Compare the RGB color distance between the current pixel and the destination 74 // color 75 double dstDistance = Math.sqrt(Math.pow(pixelRedChannel - dstRedChannel, 2.0) 76 + Math.pow(pixelGreenChannel - dstGreenChannel, 2.0) 77 + Math.pow(pixelBlueChannel - dstBlueChannel, 2.0)); 78 79 // Compare the RGB color distance between the current pixel and the source 80 // color 81 double srcDistance = Math.sqrt(Math.pow(pixelRedChannel - srcRedChannel, 2.0) 82 + Math.pow(pixelGreenChannel - srcGreenChannel, 2.0) 83 + Math.pow(pixelBlueChannel - srcBlueChannel, 2.0)); 84 85 // calculate the ratio between the destination color to the current pixel 86 // color relative to the maximum distance between source and destination colors 87 // If this value exceeds the threshold expected for the pixel distance from 88 // center then we are rendering an unexpected color 89 double dstFraction = dstDistance / maxDifference; 90 if (dstFraction > dstPercentage) { 91 return false; 92 } 93 94 // similarly compute the ratio between the source color to the current pixel 95 // color relative to the maximum distance between source and destination colors 96 // If this value exceeds the threshold expected for the pixel distance from 97 // center then we are rendering an unexpected source color 98 double srcFraction = srcDistance / maxDifference; 99 if (srcFraction > dstPercentage) { 100 return false; 101 } 102 } 103 } 104 return true; 105 } 106 } 107