# Bitmap

## Summary

| ### Enumerations ||
|---|---|
| `https://developer.android.com/ndk/reference/group/bitmap#group___bitmap_1ga39fca1837c5ce7715cbf571669660c13{ https://developer.android.com/ndk/reference/group/bitmap#group___bitmap_1gga39fca1837c5ce7715cbf571669660c13a07f71cf5c5d4950ac9813ae4bbf6d076 = 0, https://developer.android.com/ndk/reference/group/bitmap#group___bitmap_1gga39fca1837c5ce7715cbf571669660c13acf7205d1a348d867c63ac2885ce01374 = -1, https://developer.android.com/ndk/reference/group/bitmap#group___bitmap_1gga39fca1837c5ce7715cbf571669660c13a6b099b9533c38729a6c305f2fe93f98d = -2, https://developer.android.com/ndk/reference/group/bitmap#group___bitmap_1gga39fca1837c5ce7715cbf571669660c13a512f5b95b6b57e78d65502c06391f990 = -3 }` | enumAndroidBitmap functions result code. |
| `https://developer.android.com/ndk/reference/group/bitmap#group___bitmap_1gaaf8fd5f0e57d456151c951e0f3715fc4{ https://developer.android.com/ndk/reference/group/bitmap#group___bitmap_1ggaaf8fd5f0e57d456151c951e0f3715fc4a262bdd405b14647013af4f6a445188ca = 0, https://developer.android.com/ndk/reference/group/bitmap#group___bitmap_1ggaaf8fd5f0e57d456151c951e0f3715fc4af63df9ce342932ad1d117996cb3366f0 = 1, https://developer.android.com/ndk/reference/group/bitmap#group___bitmap_1ggaaf8fd5f0e57d456151c951e0f3715fc4af8bea8df5143a01f0971ffc876a7f0f6 = 2, https://developer.android.com/ndk/reference/group/bitmap#group___bitmap_1ggaaf8fd5f0e57d456151c951e0f3715fc4ad72c78fb3f1b6a1e3347067949a980a2 = 0x3, https://developer.android.com/ndk/reference/group/bitmap#group___bitmap_1ggaaf8fd5f0e57d456151c951e0f3715fc4aa85ebc360591b247ffc7ded9fa78b06e = 0 }` | enumBitmap alpha format. |
| `https://developer.android.com/ndk/reference/group/bitmap#group___bitmap_1ga94798fdadfbf49a7c658ace669a1d310{ https://developer.android.com/ndk/reference/group/bitmap#group___bitmap_1gga94798fdadfbf49a7c658ace669a1d310a9e3768ee25282777c2e7f241914bd286 = 1 << 31 }` | enum |
| `https://developer.android.com/ndk/reference/group/bitmap#group___bitmap_1ga09a014e3f2cbfed53d3f1351bc46bbd8{ https://developer.android.com/ndk/reference/group/bitmap#group___bitmap_1gga09a014e3f2cbfed53d3f1351bc46bbd8a291cb1c855abc486cb91803c3f89ac8c = 0, https://developer.android.com/ndk/reference/group/bitmap#group___bitmap_1gga09a014e3f2cbfed53d3f1351bc46bbd8a0cbe2c88e21bd3c2ef96146a6de8a457 = 1, https://developer.android.com/ndk/reference/group/bitmap#group___bitmap_1gga09a014e3f2cbfed53d3f1351bc46bbd8a2ddf8bae34b552039cf9955c63084998 = 3, https://developer.android.com/ndk/reference/group/bitmap#group___bitmap_1gga09a014e3f2cbfed53d3f1351bc46bbd8a9995f38054524b96080fb55d5c0a0801 = 4 }` | enumSpecifies the formats that can be compressed to with [AndroidBitmap_compress](https://developer.android.com/ndk/reference/group/bitmap#group___bitmap_1gaa875b278d134ae32a98af06b10837801). |
| `https://developer.android.com/ndk/reference/group/bitmap#group___bitmap_1gaea286a2d4c61ae2abb02b51500499f13{ https://developer.android.com/ndk/reference/group/bitmap#group___bitmap_1ggaea286a2d4c61ae2abb02b51500499f13ac6f0378ea5cfefd9abee2596af5a9021 = 0, https://developer.android.com/ndk/reference/group/bitmap#group___bitmap_1ggaea286a2d4c61ae2abb02b51500499f13ab92ae96ceea06aa534583beadba84057 = 1, https://developer.android.com/ndk/reference/group/bitmap#group___bitmap_1ggaea286a2d4c61ae2abb02b51500499f13a11b32e10d6db28fae70ec3590cb9ee91 = 4, https://developer.android.com/ndk/reference/group/bitmap#group___bitmap_1ggaea286a2d4c61ae2abb02b51500499f13adc2ede06eafe20439271cb8137dc7528 = 7, https://developer.android.com/ndk/reference/group/bitmap#group___bitmap_1ggaea286a2d4c61ae2abb02b51500499f13ad29996be25f8f88c96e016a1da5c4bca = 8, https://developer.android.com/ndk/reference/group/bitmap#group___bitmap_1ggaea286a2d4c61ae2abb02b51500499f13a7df3845005da3e2d18b1f260b9eb625d = 9, https://developer.android.com/ndk/reference/group/bitmap#group___bitmap_1ggaea286a2d4c61ae2abb02b51500499f13a2b4adecbd16166549df31e95f6522a0a = 10 }` | enumBitmap pixel format. |

| ### Typedefs ||
|---|---|
| `https://developer.android.com/ndk/reference/group/bitmap#group___bitmap_1ga837cf80de697c99501d88bfe2326cfd4` | typedef `struct AHardwareBuffer` |
| `https://developer.android.com/ndk/reference/group/bitmap#group___bitmap_1ga3464b40f55139365beb5eed04d7481b9)(void *userContext, const void *data, size_t size)` | typedef `bool(*` User-defined function for writing the output of compression. |

| ### Functions ||
|---|---|
| `https://developer.android.com/ndk/reference/group/bitmap#group___bitmap_1gaa875b278d134ae32a98af06b10837801(const https://developer.android.com/ndk/reference/struct/android-bitmap-info#struct_android_bitmap_info *info, int32_t dataspace, const void *pixels, int32_t format, int32_t quality, void *userContext, https://developer.android.com/ndk/reference/group/bitmap#group___bitmap_1ga3464b40f55139365beb5eed04d7481b9 fn)` | `int` Compress `pixels` as described by `info`. |
| `https://developer.android.com/ndk/reference/group/bitmap#group___bitmap_1ga505a67fbad08320570cc933b25358213(JNIEnv *env, jobject jbitmap)` | `int32_t` Given a java bitmap object, return its [ADataSpace](https://developer.android.com/ndk/reference/group/a-data-space#group___a_data_space). |
| `https://developer.android.com/ndk/reference/group/bitmap#group___bitmap_1ga48bcbad710fee8f6d656fd65ce588bbe(JNIEnv *env, jobject bitmap, https://developer.android.com/ndk/reference/group/a-hardware-buffer#group___a_hardware_buffer_1ga837cf80de697c99501d88bfe2326cfd4 **outBuffer)` | `int` Retrieve the native object associated with a HARDWARE Bitmap. |
| `https://developer.android.com/ndk/reference/group/bitmap#group___bitmap_1ga80292ee39d8a675928e38849742b54bf(JNIEnv *env, jobject jbitmap, https://developer.android.com/ndk/reference/struct/android-bitmap-info#struct_android_bitmap_info *info)` | `int` Given a java bitmap object, fill out the [AndroidBitmapInfo](https://developer.android.com/ndk/reference/struct/android-bitmap-info#struct_android_bitmap_info) struct for it. |
| `https://developer.android.com/ndk/reference/group/bitmap#group___bitmap_1ga2908d42fa4db286c34b7f8c11f29206f(JNIEnv *env, jobject jbitmap, void **addrPtr)` | `int` Given a java bitmap object, attempt to lock the pixel address. |
| `https://developer.android.com/ndk/reference/group/bitmap#group___bitmap_1ga4aca91f37baddd42d0051dca8179d4ed(JNIEnv *env, jobject jbitmap)` | `int` Call this to balance a successful call to AndroidBitmap_lockPixels. |

| ### Structs ||
|---|---|
| [AndroidBitmapInfo](https://developer.android.com/ndk/reference/struct/android-bitmap-info) | Bitmap info, see [AndroidBitmap_getInfo()](https://developer.android.com/ndk/reference/group/bitmap#group___bitmap_1ga80292ee39d8a675928e38849742b54bf). |

## Enumerations

### Anonymous Enum 22

Declared in `android/bitmap.h`

```text
 Anonymous Enum 22
```
AndroidBitmap functions result code.

| Properties ||
|---|---|
| `ANDROID_BITMAP_RESULT_ALLOCATION_FAILED` | Allocation failed. |
| `ANDROID_BITMAP_RESULT_BAD_PARAMETER` | Bad parameter. |
| `ANDROID_BITMAP_RESULT_JNI_EXCEPTION` | JNI exception occured. |
| `ANDROID_BITMAP_RESULT_SUCCESS` | Operation was successful. |

### Anonymous Enum 23

Declared in `android/bitmap.h`

```text
 Anonymous Enum 23
```
Bitmap alpha format.

| Properties ||
|---|---|
| `ANDROID_BITMAP_FLAGS_ALPHA_MASK` | Bit mask for AndroidBitmapFormat.flags to isolate the alpha. |
| `ANDROID_BITMAP_FLAGS_ALPHA_OPAQUE` | Pixels are opaque. |
| `ANDROID_BITMAP_FLAGS_ALPHA_PREMUL` | Pixel components are premultiplied by alpha. |
| `ANDROID_BITMAP_FLAGS_ALPHA_SHIFT` | Shift for AndroidBitmapFormat.flags to isolate the alpha. |
| `ANDROID_BITMAP_FLAGS_ALPHA_UNPREMUL` | Pixel components are independent of alpha. |

### Anonymous Enum 24

Declared in `android/bitmap.h`

```text
 Anonymous Enum 24
```

| Properties ||
|---|---|
| `ANDROID_BITMAP_FLAGS_IS_HARDWARE` | If this bit is set in [AndroidBitmapInfo.flags](https://developer.android.com/ndk/reference/struct/android-bitmap-info#struct_android_bitmap_info_1a6c37b0fb4db50a168001ecb25e75aa1c), the Bitmap uses the HARDWARE Config, and its [AHardwareBuffer](https://developer.android.com/ndk/reference/group/a-hardware-buffer#group___a_hardware_buffer) can be retrieved via [AndroidBitmap_getHardwareBuffer](https://developer.android.com/ndk/reference/group/bitmap#group___bitmap_1ga48bcbad710fee8f6d656fd65ce588bbe). |

### AndroidBitmapCompressFormat

Declared in `android/bitmap.h`

```text
 AndroidBitmapCompressFormat
```
Specifies the formats that can be compressed to with [AndroidBitmap_compress](https://developer.android.com/ndk/reference/group/bitmap#group___bitmap_1gaa875b278d134ae32a98af06b10837801).

| Properties ||
|---|---|
| `ANDROID_BITMAP_COMPRESS_FORMAT_JPEG` | Compress to the JPEG format. quality of 0 means compress for the smallest size. 100 means compress for max visual quality. |
| `ANDROID_BITMAP_COMPRESS_FORMAT_PNG` | Compress to the PNG format. PNG is lossless, so quality is ignored. |
| `ANDROID_BITMAP_COMPRESS_FORMAT_WEBP_LOSSLESS` | Compress to the WEBP lossless format. quality refers to how much effort to put into compression. A value of 0 means to compress quickly, resulting in a relatively large file size. 100 means to spend more time compressing, resulting in a smaller file. |
| `ANDROID_BITMAP_COMPRESS_FORMAT_WEBP_LOSSY` | Compress to the WEBP lossy format. quality of 0 means compress for the smallest size. 100 means compress for max visual quality. |

### AndroidBitmapFormat

Declared in `android/bitmap.h`

```text
 AndroidBitmapFormat
```
Bitmap pixel format.

| Properties ||
|---|---|
| `ANDROID_BITMAP_FORMAT_A_8` | Alpha: 8 bits. |
| `ANDROID_BITMAP_FORMAT_NONE` | No format. |
| `ANDROID_BITMAP_FORMAT_RGBA_1010102` | Red: 10 bits, Green: 10 bits, Blue: 10 bits, Alpha: 2 bits. |
| `ANDROID_BITMAP_FORMAT_RGBA_4444` | Deprecated in API level 13. Because of the poor quality of this configuration, it is advised to use ARGB_8888 instead. |
| `ANDROID_BITMAP_FORMAT_RGBA_8888` | Red: 8 bits, Green: 8 bits, Blue: 8 bits, Alpha: 8 bits. |
| `ANDROID_BITMAP_FORMAT_RGBA_F16` | Each component is stored as a half float. |
| `ANDROID_BITMAP_FORMAT_RGB_565` | Red: 5 bits, Green: 6 bits, Blue: 5 bits. |

## Typedefs

### AHardwareBuffer

Declared in `android/bitmap.h`

```text
struct AHardwareBuffer AHardwareBuffer
```

### AndroidBitmap_CompressWriteFunc

Declared in `android/bitmap.h`

```gdscript
bool(* AndroidBitmap_CompressWriteFunc)(void *userContext, const void *data, size_t size)
```
User-defined function for writing the output of compression.

Available since API level 30.

<br />

| Details ||
|---|---|
| Parameters | |---|---| | `userContext` | Pointer to user-defined data passed to [AndroidBitmap_compress](https://developer.android.com/ndk/reference/group/bitmap#group___bitmap_1gaa875b278d134ae32a98af06b10837801). | | `data` | Compressed data of `size` bytes to write. | | `size` | Length in bytes of data to write. | |
| **Returns** | Whether the operation succeeded. |

## Functions

### AndroidBitmap_compress

Declared in `android/bitmap.h`

```gdscript
int AndroidBitmap_compress(
  const AndroidBitmapInfo *info,
  int32_t dataspace,
  const void *pixels,
  int32_t format,
  int32_t quality,
  void *userContext,
  AndroidBitmap_CompressWriteFunc fn
)
```
Compress `pixels` as described by `info`.

Available since API level 30.

<br />

| Details ||
|---|---|
| Parameters | |---|---| | `info` | Description of the pixels to compress. | | `dataspace` | [ADataSpace](https://developer.android.com/ndk/reference/group/a-data-space#group___a_data_space) describing the color space of the pixels. | | `pixels` | Pointer to pixels to compress. | | `format` | [AndroidBitmapCompressFormat](https://developer.android.com/ndk/reference/group/bitmap#group___bitmap_1ga09a014e3f2cbfed53d3f1351bc46bbd8) to compress to. | | `quality` | Hint to the compressor, 0-100. The value is interpreted differently depending on the [AndroidBitmapCompressFormat](https://developer.android.com/ndk/reference/group/bitmap#group___bitmap_1ga09a014e3f2cbfed53d3f1351bc46bbd8). | | `userContext` | User-defined data which will be passed to the supplied [AndroidBitmap_CompressWriteFunc](https://developer.android.com/ndk/reference/group/bitmap#group___bitmap_1ga3464b40f55139365beb5eed04d7481b9) each time it is called. May be null. | | `fn` | Function that writes the compressed data. Will be called each time the compressor has compressed more data that is ready to be written. May be called more than once for each call to this method. May not be null. | |
| **Returns** | AndroidBitmap functions result code. |

### AndroidBitmap_getDataSpace

Declared in `android/bitmap.h`

```scdoc
int32_t AndroidBitmap_getDataSpace(
  JNIEnv *env,
  jobject jbitmap
)
```
Given a java bitmap object, return its [ADataSpace](https://developer.android.com/ndk/reference/group/a-data-space#group___a_data_space).

Note that [ADataSpace](https://developer.android.com/ndk/reference/group/a-data-space#group___a_data_space) only exposes a few values. This may return [ADATASPACE_UNKNOWN](https://developer.android.com/ndk/reference/group/a-data-space#group___a_data_space_1gga31c2db141423db19c32a3176e7a91adea1ed631bf9863a3744507ddc5bdd1e66a), even for Named ColorSpaces, if they have no corresponding ADataSpace.

Available since API level 30.

### AndroidBitmap_getHardwareBuffer

Declared in `android/bitmap.h`

```scdoc
int AndroidBitmap_getHardwareBuffer(
  JNIEnv *env,
  jobject bitmap,
  https://developer.android.com/ndk/reference/group/a-hardware-buffer#group___a_hardware_buffer_1ga837cf80de697c99501d88bfe2326cfd4 **outBuffer
)
```
Retrieve the native object associated with a HARDWARE Bitmap.

Client must not modify it while a Bitmap is wrapping it.

Available since API level 30.

<br />

| Details ||
|---|---|
| Parameters | |---|---| | `env` | Handle to the JNI environment pointer. | | `bitmap` | Handle to an android.graphics.Bitmap. | | `outBuffer` | On success, is set to a pointer to the [AHardwareBuffer](https://developer.android.com/ndk/reference/group/a-hardware-buffer#group___a_hardware_buffer) associated with bitmap. This acquires a reference on the buffer, and the client must call [AHardwareBuffer_release](https://developer.android.com/ndk/reference/group/a-hardware-buffer#group___a_hardware_buffer_1ga671cf3ec426591b6c8b895591667f6e6) when finished with it. | |
| **Returns** | AndroidBitmap functions result code. [ANDROID_BITMAP_RESULT_BAD_PARAMETER](https://developer.android.com/ndk/reference/group/bitmap#group___bitmap_1gga39fca1837c5ce7715cbf571669660c13acf7205d1a348d867c63ac2885ce01374) if bitmap is not a HARDWARE Bitmap. |

### AndroidBitmap_getInfo

Declared in `android/bitmap.h`

```scdoc
int AndroidBitmap_getInfo(
  JNIEnv *env,
  jobject jbitmap,
  AndroidBitmapInfo *info
)
```
Given a java bitmap object, fill out the [AndroidBitmapInfo](https://developer.android.com/ndk/reference/struct/android-bitmap-info#struct_android_bitmap_info) struct for it.

If the call fails, the info parameter will be ignored.

### AndroidBitmap_lockPixels

Declared in `android/bitmap.h`

```carbon
int AndroidBitmap_lockPixels(
  JNIEnv *env,
  jobject jbitmap,
  void **addrPtr
)
```
Given a java bitmap object, attempt to lock the pixel address.

Locking will ensure that the memory for the pixels will not move until the unlockPixels call, and ensure that, if the pixels had been previously purged, they will have been restored.

If this call succeeds, it must be balanced by a call to AndroidBitmap_unlockPixels, after which time the address of the pixels should no longer be used.

If this succeeds, \*addrPtr will be set to the pixel address. If the call fails, addrPtr will be ignored.

### AndroidBitmap_unlockPixels

Declared in `android/bitmap.h`

```scdoc
int AndroidBitmap_unlockPixels(
  JNIEnv *env,
  jobject jbitmap
)
```
Call this to balance a successful call to AndroidBitmap_lockPixels.