Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 | |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 17 | /** |
| 18 | * @addtogroup Bitmap |
| 19 | * @{ |
| 20 | */ |
| 21 | |
| 22 | /** |
| 23 | * @file bitmap.h |
| 24 | */ |
| 25 | |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 26 | #ifndef ANDROID_BITMAP_H |
| 27 | #define ANDROID_BITMAP_H |
| 28 | |
Leon Scroggins III | 7f2d5b6 | 2019-12-13 14:57:07 -0500 | [diff] [blame^] | 29 | #include <stdbool.h> |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 30 | #include <stdint.h> |
| 31 | #include <jni.h> |
| 32 | |
| 33 | #ifdef __cplusplus |
| 34 | extern "C" { |
| 35 | #endif |
| 36 | |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 37 | /** AndroidBitmap functions result code. */ |
| 38 | enum { |
| 39 | /** Operation was successful. */ |
| 40 | ANDROID_BITMAP_RESULT_SUCCESS = 0, |
| 41 | /** Bad parameter. */ |
| 42 | ANDROID_BITMAP_RESULT_BAD_PARAMETER = -1, |
| 43 | /** JNI exception occured. */ |
| 44 | ANDROID_BITMAP_RESULT_JNI_EXCEPTION = -2, |
| 45 | /** Allocation failed. */ |
| 46 | ANDROID_BITMAP_RESULT_ALLOCATION_FAILED = -3, |
| 47 | }; |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 48 | |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 49 | /** Backward compatibility: this macro used to be misspelled. */ |
Andrew Hsieh | 370980c | 2012-12-17 08:01:36 +0800 | [diff] [blame] | 50 | #define ANDROID_BITMAP_RESUT_SUCCESS ANDROID_BITMAP_RESULT_SUCCESS |
| 51 | |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 52 | /** Bitmap pixel format. */ |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 53 | enum AndroidBitmapFormat { |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 54 | /** No format. */ |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 55 | ANDROID_BITMAP_FORMAT_NONE = 0, |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 56 | /** Red: 8 bits, Green: 8 bits, Blue: 8 bits, Alpha: 8 bits. **/ |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 57 | ANDROID_BITMAP_FORMAT_RGBA_8888 = 1, |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 58 | /** Red: 5 bits, Green: 6 bits, Blue: 5 bits. **/ |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 59 | ANDROID_BITMAP_FORMAT_RGB_565 = 4, |
Quddus Chong | 4a1a45b | 2017-02-08 10:38:21 -0800 | [diff] [blame] | 60 | /** Deprecated in API level 13. Because of the poor quality of this configuration, it is advised to use ARGB_8888 instead. **/ |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 61 | ANDROID_BITMAP_FORMAT_RGBA_4444 = 7, |
Quddus Chong | 4a1a45b | 2017-02-08 10:38:21 -0800 | [diff] [blame] | 62 | /** Alpha: 8 bits. */ |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 63 | ANDROID_BITMAP_FORMAT_A_8 = 8, |
Leon Scroggins III | f73f5f2 | 2019-08-12 13:43:55 -0400 | [diff] [blame] | 64 | /** Each component is stored as a half float. **/ |
| 65 | ANDROID_BITMAP_FORMAT_RGBA_F16 = 9, |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
Leon Scroggins III | 0dd622e | 2019-08-23 15:44:21 -0400 | [diff] [blame] | 68 | /** Bitmap alpha format */ |
| 69 | enum { |
| 70 | /** Pixel components are premultiplied by alpha. */ |
| 71 | ANDROID_BITMAP_FLAGS_ALPHA_PREMUL = 0, |
| 72 | /** Pixels are opaque. */ |
| 73 | ANDROID_BITMAP_FLAGS_ALPHA_OPAQUE = 1, |
| 74 | /** Pixel components are independent of alpha. */ |
| 75 | ANDROID_BITMAP_FLAGS_ALPHA_UNPREMUL = 2, |
| 76 | /** Bit mask for AndroidBitmapFormat.flags to isolate the alpha. */ |
| 77 | ANDROID_BITMAP_FLAGS_ALPHA_MASK = 0x3, |
| 78 | /** Shift for AndroidBitmapFormat.flags to isolate the alpha. */ |
| 79 | ANDROID_BITMAP_FLAGS_ALPHA_SHIFT = 0, |
| 80 | }; |
| 81 | |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 82 | /** Bitmap info, see AndroidBitmap_getInfo(). */ |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 83 | typedef struct { |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 84 | /** The bitmap width in pixels. */ |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 85 | uint32_t width; |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 86 | /** The bitmap height in pixels. */ |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 87 | uint32_t height; |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 88 | /** The number of byte per row. */ |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 89 | uint32_t stride; |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 90 | /** The bitmap pixel format. See {@link AndroidBitmapFormat} */ |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 91 | int32_t format; |
Leon Scroggins III | 0dd622e | 2019-08-23 15:44:21 -0400 | [diff] [blame] | 92 | /** Two bits are used to encode alpha. Use ANDROID_BITMAP_FLAGS_ALPHA_MASK |
| 93 | * and ANDROID_BITMAP_FLAGS_ALPHA_SHIFT to retrieve them. */ |
| 94 | uint32_t flags; |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 95 | } AndroidBitmapInfo; |
| 96 | |
| 97 | /** |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 98 | * Given a java bitmap object, fill out the AndroidBitmapInfo struct for it. |
| 99 | * If the call fails, the info parameter will be ignored. |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 100 | */ |
| 101 | int AndroidBitmap_getInfo(JNIEnv* env, jobject jbitmap, |
| 102 | AndroidBitmapInfo* info); |
| 103 | |
Leon Scroggins III | 3dd6455 | 2020-01-10 13:41:44 -0500 | [diff] [blame] | 104 | #if __ANDROID_API__ >= 30 |
| 105 | |
| 106 | /** |
| 107 | * Given a java bitmap object, return its ADataSpace. |
| 108 | * |
| 109 | * Note that ADataSpace only exposes a few values. This may return |
| 110 | * ADATASPACE_UNKNOWN, even for Named ColorSpaces, if they have no |
| 111 | * corresponding ADataSpace. |
| 112 | */ |
| 113 | int32_t AndroidBitmap_getDataSpace(JNIEnv* env, jobject jbitmap) __INTRODUCED_IN(30); |
| 114 | |
| 115 | #endif // __ANDROID_API__ >= 30 |
| 116 | |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 117 | /** |
| 118 | * Given a java bitmap object, attempt to lock the pixel address. |
| 119 | * Locking will ensure that the memory for the pixels will not move |
| 120 | * until the unlockPixels call, and ensure that, if the pixels had been |
| 121 | * previously purged, they will have been restored. |
| 122 | * |
| 123 | * If this call succeeds, it must be balanced by a call to |
| 124 | * AndroidBitmap_unlockPixels, after which time the address of the pixels should |
| 125 | * no longer be used. |
| 126 | * |
| 127 | * If this succeeds, *addrPtr will be set to the pixel address. If the call |
| 128 | * fails, addrPtr will be ignored. |
| 129 | */ |
| 130 | int AndroidBitmap_lockPixels(JNIEnv* env, jobject jbitmap, void** addrPtr); |
| 131 | |
| 132 | /** |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 133 | * Call this to balance a successful call to AndroidBitmap_lockPixels. |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 134 | */ |
| 135 | int AndroidBitmap_unlockPixels(JNIEnv* env, jobject jbitmap); |
| 136 | |
Leon Scroggins III | 7f2d5b6 | 2019-12-13 14:57:07 -0500 | [diff] [blame^] | 137 | #if __ANDROID_API__ >= 30 |
| 138 | |
| 139 | // Note: these values match android.graphics.Bitmap#compressFormat. |
| 140 | |
| 141 | /** |
| 142 | * Specifies the formats that can be compressed to with |
| 143 | * {@link AndroidBitmap_compress}. |
| 144 | */ |
| 145 | enum AndroidBitmapCompressFormat { |
| 146 | /** |
| 147 | * Compress to the JPEG format. quality of 0 means |
| 148 | * compress for the smallest size. 100 means compress for max |
| 149 | * visual quality. |
| 150 | */ |
| 151 | ANDROID_BITMAP_COMPRESS_FORMAT_JPEG = 0, |
| 152 | /** |
| 153 | * Compress to the PNG format. PNG is lossless, so quality is |
| 154 | * ignored. |
| 155 | */ |
| 156 | ANDROID_BITMAP_COMPRESS_FORMAT_PNG = 1, |
| 157 | /** |
| 158 | * Compress to the WEBP lossy format. quality of 0 means |
| 159 | * compress for the smallest size. 100 means compress for max |
| 160 | * visual quality. |
| 161 | */ |
| 162 | ANDROID_BITMAP_COMPRESS_FORMAT_WEBP_LOSSY = 3, |
| 163 | /** |
| 164 | * Compress to the WEBP lossless format. quality refers to how |
| 165 | * much effort to put into compression. A value of 0 means to |
| 166 | * compress quickly, resulting in a relatively large file size. |
| 167 | * 100 means to spend more time compressing, resulting in a |
| 168 | * smaller file. |
| 169 | */ |
| 170 | ANDROID_BITMAP_COMPRESS_FORMAT_WEBP_LOSSLESS = 4, |
| 171 | }; |
| 172 | |
| 173 | /** |
| 174 | * User-defined function for writing the output of compression. |
| 175 | * |
| 176 | * @param userContext Pointer to user-defined data passed to |
| 177 | * {@link AndroidBitmap_compress}. |
| 178 | * @param data Compressed data of |size| bytes to write. |
| 179 | * @param size Length in bytes of data to write. |
| 180 | * @return Whether the operation succeeded. |
| 181 | */ |
| 182 | typedef bool (*AndroidBitmap_compress_write_fn)(void* userContext, |
| 183 | const void* data, |
| 184 | size_t size) __INTRODUCED_IN(30); |
| 185 | |
| 186 | /** |
| 187 | * Compress |pixels| as described by |info|. |
| 188 | * |
| 189 | * @param info Description of the pixels to compress. |
| 190 | * @param dataspace {@link ADataSpace} describing the color space of the |
| 191 | * pixels. |
| 192 | * @param pixels Pointer to pixels to compress. |
| 193 | * @param format (@link AndroidBitmapCompressFormat} to compress to. |
| 194 | * @param quality Hint to the compressor, 0-100. The value is interpreted |
| 195 | * differently depending on the |
| 196 | * {@link AndroidBitmapCompressFormat}. |
| 197 | * @param userContext User-defined data which will be passed to the supplied |
| 198 | * {@link AndroidBitmap_compress_write_fn} each time it is |
| 199 | * called. May be null. |
| 200 | * @parm fn Function that writes the compressed data. Will be called each time |
| 201 | * the compressor has compressed more data that is ready to be |
| 202 | * written. May be called more than once for each call to this method. |
| 203 | * May not be null. |
| 204 | * @return AndroidBitmap functions result code. |
| 205 | */ |
| 206 | int AndroidBitmap_compress(const AndroidBitmapInfo* info, |
| 207 | int32_t dataspace, |
| 208 | const void* pixels, |
| 209 | int32_t format, int32_t quality, |
| 210 | void* userContext, |
| 211 | AndroidBitmap_compress_write_fn fn) __INTRODUCED_IN(30); |
| 212 | |
| 213 | #endif // __ANDROID_API__ >= 30 |
| 214 | |
Mathias Agopian | e1c61d3 | 2012-03-23 14:19:36 -0700 | [diff] [blame] | 215 | #ifdef __cplusplus |
| 216 | } |
| 217 | #endif |
| 218 | |
| 219 | #endif |
Johan Euphrosine | bf6d5e0 | 2015-03-27 17:15:43 -0700 | [diff] [blame] | 220 | |
| 221 | /** @} */ |