Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | |
| 17 | /** |
| 18 | * @addtogroup ImageDecoder |
| 19 | * @{ |
| 20 | */ |
| 21 | |
| 22 | /** |
| 23 | * @file imageDecoder.h |
| 24 | */ |
| 25 | |
| 26 | #ifndef ANDROID_IMAGE_DECODER_H |
| 27 | #define ANDROID_IMAGE_DECODER_H |
| 28 | |
| 29 | #include "bitmap.h" |
| 30 | |
| 31 | #include <stdint.h> |
| 32 | |
| 33 | #ifdef __cplusplus |
| 34 | extern "C" { |
| 35 | #endif |
| 36 | |
| 37 | struct AAsset; |
| 38 | struct ARect; |
| 39 | |
| 40 | #if __ANDROID_API__ >= 30 |
| 41 | |
| 42 | /** AImageDecoder functions result code. */ |
| 43 | enum { |
| 44 | // Decoding was successful and complete. |
| 45 | ANDROID_IMAGE_DECODER_SUCCESS = 0, |
| 46 | // The input was incomplete. In decodeImage, this means a partial |
| 47 | // image was decoded. Undecoded lines are all zeroes. |
| 48 | // In AImageDecoder_create*, no AImageDecoder was created. |
| 49 | ANDROID_IMAGE_DECODER_INCOMPLETE = -1, |
| 50 | // The input contained an error after decoding some lines. Similar to |
| 51 | // INCOMPLETE, above. |
| 52 | ANDROID_IMAGE_DECODER_ERROR = -2, |
| 53 | // Could not convert, e.g. attempting to decode an image with |
| 54 | // alpha to an opaque format. |
| 55 | ANDROID_IMAGE_DECODER_INVALID_CONVERSION = -3, |
| 56 | // The scale is invalid. It may have overflowed, or it may be incompatible |
| 57 | // with the current alpha setting. |
| 58 | ANDROID_IMAGE_DECODER_INVALID_SCALE = -4, |
| 59 | // Some other parameter was bad (e.g. pixels) |
| 60 | ANDROID_IMAGE_DECODER_BAD_PARAMETER = -5, |
| 61 | // Input was invalid i.e. broken before decoding any pixels. |
| 62 | ANDROID_IMAGE_DECODER_INVALID_INPUT = -6, |
| 63 | // A seek was required, and failed. |
| 64 | ANDROID_IMAGE_DECODER_SEEK_ERROR = -7, |
| 65 | // Some other error (e.g. OOM) |
| 66 | ANDROID_IMAGE_DECODER_INTERNAL_ERROR = -8, |
| 67 | // We did not recognize the format |
| 68 | ANDROID_IMAGE_DECODER_UNSUPPORTED_FORMAT = -9 |
| 69 | }; |
| 70 | |
| 71 | struct AImageDecoder; |
| 72 | |
| 73 | /** |
| 74 | * Opaque handle for decoding images. |
| 75 | * |
| 76 | * Create using one of the following: |
| 77 | * - {@link AImageDecoder_createFromAAsset} |
| 78 | * - {@link AImageDecoder_createFromFd} |
| 79 | * - {@link AImageDecoder_createFromBuffer} |
| 80 | */ |
| 81 | typedef struct AImageDecoder AImageDecoder; |
| 82 | |
| 83 | /** |
| 84 | * Create a new AImageDecoder from an AAsset. |
| 85 | * |
| 86 | * @param asset {@link AAsset} containing encoded image data. Client is still |
| 87 | * responsible for calling {@link AAsset_close} on it. |
| 88 | * @param outDecoder On success (i.e. return value is |
| 89 | * {@link ANDROID_IMAGE_DECODER_SUCCESS}), this will be set to |
| 90 | * a newly created {@link AImageDecoder}. Caller is |
| 91 | * responsible for calling {@link AImageDecoder_delete} on it. |
| 92 | * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value |
| 93 | * indicating reason for the failure. |
| 94 | */ |
| 95 | int AImageDecoder_createFromAAsset(AAsset* asset, AImageDecoder** outDecoder) __INTRODUCED_IN(30); |
| 96 | |
| 97 | /** |
| 98 | * Create a new AImageDecoder from a file descriptor. |
| 99 | * |
| 100 | * @param fd Seekable, readable, open file descriptor for encoded data. |
| 101 | * Client is still responsible for closing it, which may be done |
| 102 | * *after* deleting the returned AImageDecoder. |
| 103 | * @param outDecoder On success (i.e. return value is |
| 104 | * {@link ANDROID_IMAGE_DECODER_SUCCESS}), this will be set to |
| 105 | * a newly created {@link AImageDecoder}. Caller is |
| 106 | * responsible for calling {@link AImageDecoder_delete} on it. |
| 107 | * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value |
| 108 | * indicating reason for the failure. |
| 109 | */ |
| 110 | int AImageDecoder_createFromFd(int fd, AImageDecoder** outDecoder) __INTRODUCED_IN(30); |
| 111 | |
| 112 | /** |
| 113 | * Create a new AImageDecoder from a buffer. |
| 114 | * |
| 115 | * @param buffer Pointer to encoded data. Must be valid for the entire time |
| 116 | * the AImageDecoder is used. |
| 117 | * @param length Byte length of buffer. |
| 118 | * @param outDecoder On success (i.e. return value is |
| 119 | * {@link ANDROID_IMAGE_DECODER_SUCCESS}), this will be set to |
| 120 | * a newly created {@link AImageDecoder}. Caller is |
| 121 | * responsible for calling {@link AImageDecoder_delete} on it. |
| 122 | * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value |
| 123 | * indicating reason for the failure. |
| 124 | */ |
| 125 | int AImageDecoder_createFromBuffer(const void* buffer, size_t length, |
| 126 | AImageDecoder** outDecoder) __INTRODUCED_IN(30); |
| 127 | |
| 128 | /** |
| 129 | * Delete the AImageDecoder. |
| 130 | */ |
| 131 | void AImageDecoder_delete(AImageDecoder* decoder) __INTRODUCED_IN(30); |
| 132 | |
| 133 | /** |
| 134 | * Choose the desired output format. |
| 135 | * |
Leon Scroggins III | 5d0445c | 2020-01-23 09:43:43 -0500 | [diff] [blame^] | 136 | * @param format {@link AndroidBitmapFormat} to use for the output. |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 137 | * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} if the format is compatible |
| 138 | * with the image and {@link ANDROID_IMAGE_DECODER_INVALID_CONVERSION} |
| 139 | * otherwise. In the latter case, the AImageDecoder uses the |
| 140 | * format it was already planning to use (either its default |
| 141 | * or a previously successful setting from this function). |
| 142 | */ |
| 143 | int AImageDecoder_setAndroidBitmapFormat(AImageDecoder*, |
| 144 | int32_t format) __INTRODUCED_IN(30); |
| 145 | |
Leon Scroggins III | 1be112f | 2020-01-15 04:26:44 -0500 | [diff] [blame] | 146 | /** |
| 147 | * Specify whether the output's pixels should be unpremultiplied. |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 148 | * |
Leon Scroggins III | 1be112f | 2020-01-15 04:26:44 -0500 | [diff] [blame] | 149 | * By default, the decoder will premultiply the pixels, if they have alpha. Pass |
| 150 | * false to this method to leave them unpremultiplied. This has no effect on an |
| 151 | * opaque image. |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 152 | * |
Leon Scroggins III | 1be112f | 2020-01-15 04:26:44 -0500 | [diff] [blame] | 153 | * @param required Pass true to leave the pixels unpremultiplied. |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 154 | * @return - {@link ANDROID_IMAGE_DECODER_SUCCESS} on success |
| 155 | * - {@link ANDROID_IMAGE_DECODER_INVALID_CONVERSION} if the conversion |
| 156 | * is not possible |
| 157 | * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER} for bad parameters |
| 158 | */ |
Leon Scroggins III | 1be112f | 2020-01-15 04:26:44 -0500 | [diff] [blame] | 159 | int AImageDecoder_setUnpremultipliedRequired(AImageDecoder*, bool required) __INTRODUCED_IN(30); |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 160 | |
| 161 | /** |
Leon Scroggins III | 20d480c | 2020-01-15 15:32:59 -0500 | [diff] [blame] | 162 | * Choose the dataspace for the output. |
| 163 | * |
| 164 | * Not supported for {@link ANDROID_BITMAP_FORMAT_A_8}, which does not support |
| 165 | * an ADataSpace. |
| 166 | * |
| 167 | * @param dataspace The {@link ADataSpace} to decode into. An ADataSpace |
| 168 | * specifies how to interpret the colors. By default, |
| 169 | * AImageDecoder will decode into the ADataSpace specified by |
| 170 | * {@link AImageDecoderHeaderInfo_getDataSpace}. If this |
| 171 | * parameter is set to a different ADataSpace, AImageDecoder |
| 172 | * will transform the output into the specified ADataSpace. |
| 173 | * @return - {@link ANDROID_IMAGE_DECODER_SUCCESS} on success |
| 174 | * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER} for a null |
| 175 | * AImageDecoder or an integer that does not correspond to an |
| 176 | * ADataSpace value. |
| 177 | */ |
| 178 | int AImageDecoder_setDataSpace(AImageDecoder*, int32_t dataspace) __INTRODUCED_IN(30); |
| 179 | |
| 180 | /** |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 181 | * Specify the output size for a decoded image. |
| 182 | * |
| 183 | * Future calls to {@link AImageDecoder_decodeImage} will sample or scale the |
| 184 | * encoded image to reach the desired size. If a crop rect is set (via |
| 185 | * {@link AImageDecoder_setCrop}), it must be contained within the dimensions |
| 186 | * specified by width and height, and the output image will be the size of the |
| 187 | * crop rect. |
| 188 | * |
| 189 | * @param width Width of the output (prior to cropping). |
| 190 | * This will affect future calls to |
| 191 | * {@link AImageDecoder_getMinimumStride}, which will now return |
| 192 | * a value based on this width. |
| 193 | * @param height Height of the output (prior to cropping). |
| 194 | * @return - {@link ANDROID_IMAGE_DECODER_SUCCESS} on success |
| 195 | * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER} if the AImageDecoder |
| 196 | * pointer is null, width or height is <= 0, or any existing crop is |
| 197 | * not contained by the image dimensions. |
| 198 | */ |
Leon Scroggins III | 5d0445c | 2020-01-23 09:43:43 -0500 | [diff] [blame^] | 199 | int AImageDecoder_setTargetSize(AImageDecoder*, int32_t width, int32_t height) __INTRODUCED_IN(30); |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 200 | |
Leon Scroggins III | f27256b | 2020-01-19 21:13:04 -0500 | [diff] [blame] | 201 | |
| 202 | /** |
| 203 | * Compute the dimensions to use for a given sampleSize. |
| 204 | * |
| 205 | * Although AImageDecoder can scale to an arbitrary target size (see |
| 206 | * {@link AImageDecoder_setTargetSize}), some sizes may be more efficient than |
| 207 | * others. This computes the most efficient target size to use to reach a |
| 208 | * particular sampleSize. |
| 209 | * |
| 210 | * @param sampleSize A subsampling rate of the original image. Must be greater |
| 211 | * than or equal to 1. A sampleSize of 2 means to skip every |
| 212 | * other pixel/line, resulting in a width and height that are |
| 213 | * 1/2 of the original dimensions, with 1/4 the number of |
| 214 | * pixels. |
| 215 | * @param width Out parameter for the width sampled by sampleSize, and rounded |
| 216 | * direction that the decoder can do most efficiently. |
| 217 | * @param height Out parameter for the height sampled by sampleSize, and rounded |
| 218 | * direction that the decoder can do most efficiently. |
| 219 | * @return ANDROID_IMAGE_DECODER result code. |
| 220 | */ |
| 221 | int AImageDecoder_computeSampledSize(const AImageDecoder*, int sampleSize, |
Leon Scroggins III | 5d0445c | 2020-01-23 09:43:43 -0500 | [diff] [blame^] | 222 | int32_t* width, int32_t* height) __INTRODUCED_IN(30); |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 223 | /** |
| 224 | * Specify how to crop the output after scaling (if any). |
| 225 | * |
| 226 | * Future calls to {@link AImageDecoder_decodeImage} will crop their output to |
| 227 | * the specified {@link ARect}. Clients will only need to allocate enough memory |
| 228 | * for the cropped ARect. |
| 229 | * |
| 230 | * @param crop Rectangle describing a crop of the decode. It must be contained inside of |
| 231 | * the (possibly scaled, by {@link AImageDecoder_setTargetSize}) |
| 232 | * image dimensions. This will affect future calls to |
| 233 | * {@link AImageDecoder_getMinimumStride}, which will now return a |
| 234 | * value based on the width of the crop. An empty ARect - |
| 235 | * specifically { 0, 0, 0, 0 } - may be used to remove the cropping |
| 236 | * behavior. Any other empty or unsorted ARects will result in |
| 237 | * returning ANDROID_IMAGE_DECODER_BAD_PARAMETER. |
| 238 | * @return - {@link ANDROID_IMAGE_DECODER_SUCCESS} on success |
| 239 | * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER} if the AImageDecoder |
| 240 | * pointer is null or the crop is not contained by the image |
| 241 | * dimensions. |
| 242 | */ |
| 243 | int AImageDecoder_setCrop(AImageDecoder*, ARect crop) __INTRODUCED_IN(30); |
| 244 | |
| 245 | /** |
| 246 | * Opaque handle for reading header info. |
| 247 | */ |
| 248 | struct AImageDecoderHeaderInfo; |
| 249 | typedef struct AImageDecoderHeaderInfo AImageDecoderHeaderInfo; |
| 250 | |
| 251 | /** |
| 252 | * Return an opaque handle for reading header info. |
| 253 | * |
| 254 | * This is owned by the {@link AImageDecoder} and will be destroyed when the |
| 255 | * AImageDecoder is destroyed via {@link AImageDecoder_delete}. |
| 256 | */ |
| 257 | const AImageDecoderHeaderInfo* AImageDecoder_getHeaderInfo( |
| 258 | const AImageDecoder*) __INTRODUCED_IN(30); |
| 259 | |
| 260 | /** |
| 261 | * Report the native width of the encoded image. |
| 262 | */ |
| 263 | int32_t AImageDecoderHeaderInfo_getWidth(const AImageDecoderHeaderInfo*) __INTRODUCED_IN(30); |
| 264 | |
| 265 | /** |
| 266 | * Report the native height of the encoded image. |
| 267 | */ |
| 268 | int32_t AImageDecoderHeaderInfo_getHeight(const AImageDecoderHeaderInfo*) __INTRODUCED_IN(30); |
| 269 | |
| 270 | /** |
| 271 | * Report the mimeType of the encoded image. |
| 272 | * |
| 273 | * @return a string literal describing the mime type. |
| 274 | */ |
| 275 | const char* AImageDecoderHeaderInfo_getMimeType( |
| 276 | const AImageDecoderHeaderInfo*) __INTRODUCED_IN(30); |
| 277 | |
| 278 | /** |
Leon Scroggins III | 5d0445c | 2020-01-23 09:43:43 -0500 | [diff] [blame^] | 279 | * Report the {@link AndroidBitmapFormat} the AImageDecoder will decode to |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 280 | * by default. AImageDecoder will try to choose one that is sensible |
| 281 | * for the image and the system. Note that this does not indicate the |
| 282 | * encoded format of the image. |
| 283 | */ |
Leon Scroggins III | 5d0445c | 2020-01-23 09:43:43 -0500 | [diff] [blame^] | 284 | int32_t AImageDecoderHeaderInfo_getAndroidBitmapFormat( |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 285 | const AImageDecoderHeaderInfo*) __INTRODUCED_IN(30); |
| 286 | |
| 287 | /** |
| 288 | * Report how the AImageDecoder will handle alpha by default. If the image |
| 289 | * contains no alpha (according to its header), this will return |
| 290 | * {@link ANDROID_BITMAP_FLAGS_ALPHA_OPAQUE}. If the image may contain alpha, |
| 291 | * this returns {@link ANDROID_BITMAP_FLAGS_ALPHA_PREMUL}. |
| 292 | * |
| 293 | * For animated images only the opacity of the first frame is reported. |
| 294 | */ |
| 295 | int AImageDecoderHeaderInfo_getAlphaFlags( |
| 296 | const AImageDecoderHeaderInfo*) __INTRODUCED_IN(30); |
| 297 | |
| 298 | /** |
Leon Scroggins III | 20d480c | 2020-01-15 15:32:59 -0500 | [diff] [blame] | 299 | * Report the dataspace the AImageDecoder will decode to by default. |
| 300 | * AImageDecoder will try to choose one that is sensible for the |
| 301 | * image and the system. Note that this may not exactly match the ICC |
| 302 | * profile (or other color information) stored in the encoded image. |
| 303 | * |
| 304 | * @return The {@link ADataSpace} most closely representing the way the colors |
| 305 | * are encoded (or {@link ADATASPACE_UNKNOWN} if there is not an |
| 306 | * approximate ADataSpace). This specifies how to interpret the colors |
| 307 | * in the decoded image, unless {@link AImageDecoder_setDataSpace} is |
| 308 | * called to decode to a different ADataSpace. |
| 309 | * |
| 310 | * Note that ADataSpace only exposes a few values. This may return |
| 311 | * ADATASPACE_UNKNOWN, even for Named ColorSpaces, if they have no |
| 312 | * corresponding ADataSpace. |
| 313 | */ |
| 314 | int32_t AImageDecoderHeaderInfo_getDataSpace( |
| 315 | const AImageDecoderHeaderInfo*) __INTRODUCED_IN(30); |
| 316 | |
| 317 | /** |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 318 | * Return the minimum stride that can be used, taking the specified |
| 319 | * (or default) (possibly scaled) width, crop rect and |
| 320 | * {@link AndroidBitmapFormat} into account. |
| 321 | */ |
| 322 | size_t AImageDecoder_getMinimumStride(AImageDecoder*) __INTRODUCED_IN(30); |
| 323 | |
| 324 | /** |
| 325 | * Decode the image into pixels, using the settings of the AImageDecoder. |
| 326 | * |
| 327 | * @param decoder Opaque object representing the decoder. |
| 328 | * @param pixels On success, will be filled with the result |
| 329 | * of the decode. Must be large enough to fit |size| bytes. |
| 330 | * @param stride Width in bytes of a single row. Must be at least |
| 331 | * {@link AImageDecoder_getMinimumStride}. |
| 332 | * @param size Size of the pixel buffer in bytes. Must be at least |
| 333 | * stride * (height - 1) + |
| 334 | * {@link AImageDecoder_getMinimumStride}. |
| 335 | * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success, or an error code |
| 336 | * from the same enum describing the failure. |
| 337 | */ |
| 338 | int AImageDecoder_decodeImage(AImageDecoder* decoder, |
| 339 | void* pixels, size_t stride, |
| 340 | size_t size) __INTRODUCED_IN(30); |
| 341 | |
| 342 | #endif // __ANDROID_API__ >= 30 |
| 343 | |
| 344 | #ifdef __cplusplus |
| 345 | } |
| 346 | #endif |
| 347 | |
| 348 | #endif // ANDROID_IMAGE_DECODER_H |
| 349 | |
| 350 | /** @} */ |