{% setvar book_path %}/reference/androidx/_book.yaml{% endsetvar %} {% include "_shared/_reference-head-tags.html" %}

PixelMap

public final class 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)
    }
}
See also
readPixels
toPixelMap

Summary

Public constructors

PixelMap(
    @NonNull int[] buffer,
    int width,
    int height,
    int bufferOffset,
    int stride
)

Public methods

final @NonNull Color
get(int x, int y)

Obtain the color of the pixel at the given coordinate.

final @NonNull int[]

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

final int

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

final int

Height of the subsection of the ImageBitmap this buffer represents

final int

Number of entries to skip between rows

final int

Width of the subsection of the ImageBitmap this buffer represents

Public constructors

PixelMap

public PixelMap(
    @NonNull int[] buffer,
    int width,
    int height,
    int bufferOffset,
    int stride
)
Parameters
@NonNull int[] buffer

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

int width

Width of the subsection of the ImageBitmap this buffer represents

int height

Height of the subsection of the ImageBitmap this buffer represents

int bufferOffset

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

int stride

Number of entries to skip between rows

Public methods

get

public final @NonNull Color get(int x, int y)

Obtain the color of the pixel at the given coordinate.

Parameters
int x

the horizontal pixel coordinate, minimum 1

int y

the vertical pixel coordinate, minimum 1

getBuffer

public final @NonNull int[] getBuffer()

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

getBufferOffset

public final int getBufferOffset()

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

getHeight

public final int getHeight()

Height of the subsection of the ImageBitmap this buffer represents

getStride

public final int getStride()

Number of entries to skip between rows

getWidth

public final int getWidth()

Width of the subsection of the ImageBitmap this buffer represents