1 /*
2  * 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 androidx.camera.integration.diagnose
18 
19 import android.graphics.Bitmap
20 import com.google.common.truth.Truth.assertThat
21 import java.io.BufferedReader
22 
23 const val JPEG_ENCODE_ERROR_TOLERANCE = 3
24 
25 /** Asserting bitmap color and dimension is as expected */
assertBitmapColorAndSizenull26 fun assertBitmapColorAndSize(bitmap: Bitmap, color: Int, width: Int, height: Int) {
27     for (x in 0 until bitmap.width) {
28         for (y in 0 until bitmap.height) {
29             val pixelColor = bitmap.getPixel(x, y)
30             // compare the RGB of the pixel to the given color
31             for (shift in 16 until 0 step 8) {
32                 val pixelRgb = (pixelColor shr shift) and 0xFF
33                 val rgb = (color shr shift) and 0xFF
34                 val rgbDifference = kotlin.math.abs(pixelRgb.minus(rgb))
35                 assertThat(rgbDifference).isLessThan(JPEG_ENCODE_ERROR_TOLERANCE)
36             }
37         }
38     }
39     assertThat(bitmap.width).isEqualTo(width)
40     assertThat(bitmap.height).isEqualTo(height)
41 }
42 
43 /** Reading and returning complete String from buffer */
readTextnull44 fun readText(br: BufferedReader): String {
45     var lines = StringBuilder()
46     while (br.ready()) {
47         lines.append(br.readLine())
48     }
49     return lines.toString()
50 }
51