PixelMap


Result of a pixel read operation. This contains the ImageBitmap pixel information represented as a 1 dimensional array of values that supports queries of pixel values based on the 2 dimensional coordinates of the corresponding ImageBitmap this was obtained from

import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.PixelMap

val imageBitmap = createImageBitmap()

val buffer = IntArray(20 * 10)
imageBitmap.readPixels(buffer = buffer, startX = 8, startY = 9, width = 20, height = 10)

val pixelmap = PixelMap(buffer = buffer, width = 20, height = 10, stride = 20, bufferOffset = 0)

// create a histogram to count the number of occurrences of a color within the specified
// subsection of the provided ImageBitmap
val histogram = HashMap<Color, Int>()
for (x in 0 until pixelmap.width) {
    for (y in 0 until pixelmap.height) {
        val color = pixelmap[x, y]
        val colorCount = histogram[color] ?: 0
        histogram[color] = (colorCount + 1)
    }
}

Summary

Public constructors

PixelMap(
    buffer: IntArray,
    width: Int,
    height: Int,
    bufferOffset: Int,
    stride: Int
)
Cmn

Public functions

operator Color
get(x: @IntRange(from = 0) Int, y: @IntRange(from = 0) Int)

Obtain the color of the pixel at the given coordinate.

Cmn

Public properties

IntArray

IntArray where pixel information is stored as an ARGB value packed into an Int

Cmn
Int

first index in the buffer where pixel information for the ImageBitmap is stored

Cmn
Int

Height of the subsection of the ImageBitmap this buffer represents

Cmn
Int

Number of entries to skip between rows

Cmn
Int

Width of the subsection of the ImageBitmap this buffer represents

Cmn

Public constructors

PixelMap

PixelMap(
    buffer: IntArray,
    width: Int,
    height: Int,
    bufferOffset: Int,
    stride: Int
)
Parameters
buffer: IntArray

IntArray where pixel information is stored as an ARGB value packed into an Int

width: Int

Width of the subsection of the ImageBitmap this buffer represents

height: Int

Height of the subsection of the ImageBitmap this buffer represents

bufferOffset: Int

first index in the buffer where pixel information for the ImageBitmap is stored

stride: Int

Number of entries to skip between rows

Public functions

get

operator fun get(x: @IntRange(from = 0) Int, y: @IntRange(from = 0) Int): Color

Obtain the color of the pixel at the given coordinate.

Parameters
x: @IntRange(from = 0) Int

the horizontal pixel coordinate, minimum 1

y: @IntRange(from = 0) Int

the vertical pixel coordinate, minimum 1

Public properties

buffer

val bufferIntArray

IntArray where pixel information is stored as an ARGB value packed into an Int

bufferOffset

val bufferOffsetInt

first index in the buffer where pixel information for the ImageBitmap is stored

height

val heightInt

Height of the subsection of the ImageBitmap this buffer represents

stride

val strideInt

Number of entries to skip between rows

width

val widthInt

Width of the subsection of the ImageBitmap this buffer represents