# ImageBitmap

Artifact: [androidx.compose.ui:ui-graphics](https://developer.android.com/jetpack/androidx/releases/compose-ui) [View Source](https://cs.android.com/search?q=file:androidx/compose/ui/graphics/ImageBitmap.kt+class:androidx.compose.ui.graphics.ImageBitmap) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-ui#1.0.0) Common/AllAndroid/JVM
- Cmn

  ```
  interface ImageBitmap
  ```

*** ** * ** ***

Graphics object that represents a 2 dimensional array of pixel information represented as ARGB values

## Summary

| ### Nested types |
|---|
| `object https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ImageBitmap.Companion` Provide an empty companion object to hang platform-specific companion extensions onto. |

| ### Public functions |
|---|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ImageBitmap#prepareToDraw()()` Builds caches associated with the ImageBitmap that are used for drawing it. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ImageBitmap#readPixels(kotlin.IntArray,kotlin.Int,kotlin.Int,kotlin.Int,kotlin.Int,kotlin.Int,kotlin.Int)( buffer: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int-array/index.html, startX: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, startY: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, width: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, height: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, bufferOffset: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, stride: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html )` Copies the pixel data within the ImageBitmap into the given array. | Cmn |

| ### Public properties |
|---|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/colorspace/ColorSpace` | `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ImageBitmap#colorSpace()` ColorSpace the Image renders in | Cmn |
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ImageBitmapConfig` | `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ImageBitmap#config()` Returns the current configuration of this Image, either: | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ImageBitmap#hasAlpha()` Determines whether or not the ImageBitmap contains an alpha channel | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ImageBitmap#height()` The number of image pixels along the ImageBitmap's vertical axis. | Cmn |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ImageBitmap#width()` The number of image pixels along the ImageBitmap's horizontal axis. | Cmn |

| ### Extension functions |
|---|---|---|
| `https://developer.android.com/reference/android/graphics/Bitmap.html` | `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ImageBitmap.https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ImageBitmap#(androidx.compose.ui.graphics.ImageBitmap).asAndroidBitmap()()` | android |
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/PixelMap` | `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ImageBitmap.https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ImageBitmap#(androidx.compose.ui.graphics.ImageBitmap).toPixelMap(kotlin.Int,kotlin.Int,kotlin.Int,kotlin.Int,kotlin.IntArray,kotlin.Int,kotlin.Int)( startX: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, startY: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, width: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, height: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, buffer: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int-array/index.html, bufferOffset: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, stride: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html )` Convenience method to extract pixel information from the given ImageBitmap into a `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/PixelMap` that supports for querying pixel information based on | Cmn |

| ### Extension properties |
|---|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/remote/creation/compose/state/RemoteImageBitmap` | `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ImageBitmap.https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ImageBitmap#(androidx.compose.ui.graphics.ImageBitmap).rb()` Extension property to convert a `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ImageBitmap` to a `https://developer.android.com/reference/kotlin/androidx/compose/remote/creation/compose/state/RemoteImageBitmap`. | android |

## Public functions

### prepareToDraw

Cmn Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-ui#1.0.0)

```
fun prepareToDraw(): Unit
```

Builds caches associated with the ImageBitmap that are used for drawing it. This method can be used as a signal to upload textures to the GPU to eventually be rendered

### readPixels

Cmn Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-ui#1.0.0)

```
fun readPixels(
    buffer: IntArray,
    startX: Int = 0,
    startY: Int = 0,
    width: Int = this.width,
    height: Int = this.height,
    bufferOffset: Int = 0,
    stride: Int = width
): Unit
```

Copies the pixel data within the ImageBitmap into the given array. Each value is represented as ARGB values packed into an Int. The stride parameter allows the caller to allow for gaps in the returned pixels array between rows. For normal packed, results, the stride value is equivalent to the width of the `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ImageBitmap`. The returned colors are non-premultiplied ARGB values in the `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/colorspace/ColorSpaces#Srgb()` color space.

Note this method can block so it is recommended to not invoke this method in performance critical code paths

```kotlin
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)
    }
}
```

| Parameters |
|---|---|
| `buffer: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int-array/index.html` | The array to store the `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ImageBitmap`'s colors. By default this allocates an `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int-array/index.html` large enough to store all the pixel information. Consumers of this API are advised to use the smallest `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int-array/index.html` necessary to extract relevant pixel information, that is the 2 dimensional area of the section of the `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ImageBitmap` to be queried. |
| `startX: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html = 0` | The x-coordinate of the first pixel to read from the `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ImageBitmap` |
| `startY: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html = 0` | The y-coordinate of the first pixel to read from the `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ImageBitmap` |
| `width: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html = this.width` | The number of pixels to read from each row |
| `height: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html = this.height` | The number of rows to read |
| `bufferOffset: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html = 0` | The first index to write into the buffer array, this defaults to 0 |
| `stride: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html = width` | The number of entries in `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ImageBitmap#readPixels(kotlin.IntArray,kotlin.Int,kotlin.Int,kotlin.Int,kotlin.Int,kotlin.Int,kotlin.Int)` to skip between rows (must be \>= `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ImageBitmap#readPixels(kotlin.IntArray,kotlin.Int,kotlin.Int,kotlin.Int,kotlin.Int,kotlin.Int,kotlin.Int)` |

## Public properties

### colorSpace

Cmn Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-ui#1.0.0)

```
val colorSpace: ColorSpace
```

ColorSpace the Image renders in

### config

Cmn Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-ui#1.0.0)

```
val config: ImageBitmapConfig
```

Returns the current configuration of this Image, either:

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ImageBitmapConfig#Argb8888()` |   |
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ImageBitmapConfig#Rgb565()` |   |
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ImageBitmapConfig#Alpha8()` |   |
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ImageBitmapConfig#Gpu()` |   |

### hasAlpha

Cmn Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-ui#1.0.0)

```
val hasAlpha: Boolean
```

Determines whether or not the ImageBitmap contains an alpha channel

### height

Cmn Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-ui#1.0.0)

```
val height: Int
```

The number of image pixels along the ImageBitmap's vertical axis.

### width

Cmn Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/compose-ui#1.0.0)

```
val width: Int
```

The number of image pixels along the ImageBitmap's horizontal axis.

## Extension functions

### ImageBitmap.asAndroidBitmap

android Artifact: [androidx.compose.ui:ui-graphics](https://developer.android.com/jetpack/androidx/releases/compose-ui) [View Source](https://cs.android.com/search?q=file:androidx/compose/ui/graphics/AndroidImageBitmap.android.kt+function:asAndroidBitmap)

```
fun ImageBitmap.asAndroidBitmap(): Bitmap
```

| Throws |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unsupported-operation-exception/index.html` | if this `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ImageBitmap` is not backed by an android.graphics.Bitmap |

### ImageBitmap.toPixelMap

Cmn Artifact: [androidx.compose.ui:ui-graphics](https://developer.android.com/jetpack/androidx/releases/compose-ui) [View Source](https://cs.android.com/search?q=file:androidx/compose/ui/graphics/ImageBitmap.kt+function:toPixelMap)

```
fun ImageBitmap.toPixelMap(
    startX: Int = 0,
    startY: Int = 0,
    width: Int = this.width,
    height: Int = this.height,
    buffer: IntArray = IntArray(width * height),
    bufferOffset: Int = 0,
    stride: Int = width
): PixelMap
```

Convenience method to extract pixel information from the given ImageBitmap into a `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/PixelMap` that supports for querying pixel information based on

Note this method can block so it is recommended to not invoke this method in performance critical code paths

```kotlin
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.PixelMap
import androidx.compose.ui.graphics.toPixelMap

val imageBitmap = createImageBitmap()

// Sample a 3 by 2 subsection of the given ImageBitmap
// starting at the coordinate (48, 49)
val pixelmap = imageBitmap.toPixelMap(startX = 48, startY = 49, width = 3, height = 2)

// 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)
    }
}
```

| Parameters |
|---|---|
| `startX: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html = 0` | The x-coordinate of the first pixel to read from the `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ImageBitmap` |
| `startY: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html = 0` | The y-coordinate of the first pixel to read from the `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ImageBitmap` |
| `width: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html = this.width` | The number of pixels to read from each row |
| `height: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html = this.height` | The number of rows to read |
| `buffer: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int-array/index.html = IntArray(width * height)` | The array to store the `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ImageBitmap`'s colors. By default this allocates an `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int-array/index.html` large enough to store all the pixel information. Consumers of this API are advised to use the smallest `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int-array/index.html` necessary to extract relevant pixel information |
| `bufferOffset: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html = 0` | The first index to write into the buffer array, this defaults to 0 |
| `stride: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html = width` | The number of entries in `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/package-summary#(androidx.compose.ui.graphics.ImageBitmap).toPixelMap(kotlin.Int,kotlin.Int,kotlin.Int,kotlin.Int,kotlin.IntArray,kotlin.Int,kotlin.Int)` to skip between rows (must be \>= `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/package-summary#(androidx.compose.ui.graphics.ImageBitmap).toPixelMap(kotlin.Int,kotlin.Int,kotlin.Int,kotlin.Int,kotlin.IntArray,kotlin.Int,kotlin.Int)` |

| See also |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ImageBitmap#readPixels(kotlin.IntArray,kotlin.Int,kotlin.Int,kotlin.Int,kotlin.Int,kotlin.Int,kotlin.Int)` |   |

## Extension properties

### ImageBitmap.rb

android Artifact: [androidx.compose.remote:remote-creation-compose](https://developer.android.com/jetpack/androidx/releases/compose-remote) [View Source](https://cs.android.com/search?q=file:androidx/compose/remote/creation/compose/state/RemoteImageBitmap.kt+symbol:rb) Added in [1.0.0-alpha23](https://developer.android.com/jetpack/androidx/releases/compose-remote#1.0.0-alpha23)

```
val ImageBitmap.rb: RemoteImageBitmap
```

Extension property to convert a `https://developer.android.com/reference/kotlin/androidx/compose/ui/graphics/ImageBitmap` to a `https://developer.android.com/reference/kotlin/androidx/compose/remote/creation/compose/state/RemoteImageBitmap`.