1 /*
2  * Copyright 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 androidx.compose.ui.graphics
18 
19 import androidx.annotation.IntRange
20 
21 /**
22  * Result of a pixel read operation. This contains the [ImageBitmap] pixel information represented
23  * as a 1 dimensional array of values that supports queries of pixel values based on the 2
24  * dimensional coordinates of the corresponding [ImageBitmap] this was obtained from
25  *
26  * @sample androidx.compose.ui.graphics.samples.ImageBitmapReadPixelsSample
27  * @param buffer IntArray where pixel information is stored as an ARGB value packed into an Int
28  * @param bufferOffset first index in the buffer where pixel information for the [ImageBitmap] is
29  *   stored
30  * @param width Width of the subsection of the [ImageBitmap] this buffer represents
31  * @param height Height of the subsection of the [ImageBitmap] this buffer represents
32  * @param stride Number of entries to skip between rows
33  * @see ImageBitmap.readPixels
34  * @See ImageBitmap.toPixelMap
35  */
36 class PixelMap(
37     val buffer: IntArray,
38     val width: Int,
39     val height: Int,
40     val bufferOffset: Int,
41     val stride: Int
42 ) {
43     /**
44      * Obtain the color of the pixel at the given coordinate.
45      *
46      * @param x the horizontal pixel coordinate, minimum 1
47      * @param y the vertical pixel coordinate, minimum 1
48      */
getnull49     operator fun get(@IntRange(from = 0) x: Int, @IntRange(from = 0) y: Int): Color =
50         Color(buffer[bufferOffset + y * stride + x])
51 }
52