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 | /** |
Leon Scroggins III | 97fea5f | 2020-01-30 12:18:20 -0500 | [diff] [blame] | 18 | * @defgroup ImageDecoder |
| 19 | * |
| 20 | * Functions for converting encoded images into RGBA pixels. |
| 21 | * |
| 22 | * Similar to the Java counterpart android.graphics.ImageDecoder, it can be used |
Leon Scroggins III | bb5ffd2 | 2020-02-06 11:45:16 -0500 | [diff] [blame] | 23 | * to decode images in the following formats: |
| 24 | * - JPEG |
| 25 | * - PNG |
| 26 | * - GIF |
| 27 | * - WebP |
| 28 | * - BMP |
| 29 | * - ICO |
| 30 | * - WBMP |
| 31 | * - HEIF |
| 32 | * - Digital negatives (via the DNG SDK) |
| 33 | * <p>It has similar options for scaling, cropping, and choosing the output format. |
| 34 | * Unlike the Java API, which can create an android.graphics.Bitmap or |
Leon Scroggins III | 97fea5f | 2020-01-30 12:18:20 -0500 | [diff] [blame] | 35 | * android.graphics.drawable.Drawable object, AImageDecoder decodes directly |
| 36 | * into memory provided by the client. For more information, see the |
| 37 | * <a href="https://developer.android.com/ndk/guides/image-decoder">Image decoder</a> |
| 38 | * developer guide. |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 39 | * @{ |
| 40 | */ |
| 41 | |
| 42 | /** |
Leon Scroggins III | 97fea5f | 2020-01-30 12:18:20 -0500 | [diff] [blame] | 43 | * @file imagedecoder.h |
| 44 | * @brief API for decoding images. |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 45 | */ |
| 46 | |
| 47 | #ifndef ANDROID_IMAGE_DECODER_H |
| 48 | #define ANDROID_IMAGE_DECODER_H |
| 49 | |
| 50 | #include "bitmap.h" |
Leon Scroggins III | a9f397b | 2020-01-27 12:42:56 -0500 | [diff] [blame] | 51 | #include <android/rect.h> |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 52 | #include <stdint.h> |
| 53 | |
| 54 | #ifdef __cplusplus |
| 55 | extern "C" { |
| 56 | #endif |
| 57 | |
| 58 | struct AAsset; |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 59 | |
Leon Scroggins III | 97fea5f | 2020-01-30 12:18:20 -0500 | [diff] [blame] | 60 | /** |
Leon Scroggins III | 3b3700e | 2020-12-29 11:52:12 -0500 | [diff] [blame] | 61 | * {@link AImageDecoder} functions result code. |
| 62 | * |
| 63 | * Introduced in API 30. |
| 64 | * |
| 65 | * Many functions will return this to indicate success |
| 66 | * ({@link ANDROID_IMAGE_DECODER_SUCCESS}) or the reason for the failure. On |
| 67 | * failure, any out-parameters should be considered uninitialized, except where |
| 68 | * specified. |
Leon Scroggins III | 97fea5f | 2020-01-30 12:18:20 -0500 | [diff] [blame] | 69 | */ |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 70 | enum { |
Leon Scroggins III | 97fea5f | 2020-01-30 12:18:20 -0500 | [diff] [blame] | 71 | /** |
| 72 | * Decoding was successful and complete. |
| 73 | */ |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 74 | ANDROID_IMAGE_DECODER_SUCCESS = 0, |
Leon Scroggins III | 97fea5f | 2020-01-30 12:18:20 -0500 | [diff] [blame] | 75 | /** |
Leon Scroggins III | bb5ffd2 | 2020-02-06 11:45:16 -0500 | [diff] [blame] | 76 | * The input is incomplete. |
Leon Scroggins III | 97fea5f | 2020-01-30 12:18:20 -0500 | [diff] [blame] | 77 | */ |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 78 | ANDROID_IMAGE_DECODER_INCOMPLETE = -1, |
Leon Scroggins III | 97fea5f | 2020-01-30 12:18:20 -0500 | [diff] [blame] | 79 | /** |
| 80 | * The input contained an error after decoding some lines. |
| 81 | */ |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 82 | ANDROID_IMAGE_DECODER_ERROR = -2, |
Leon Scroggins III | 97fea5f | 2020-01-30 12:18:20 -0500 | [diff] [blame] | 83 | /** |
| 84 | * Could not convert. For example, attempting to decode an image with |
| 85 | * alpha to an opaque format. |
| 86 | */ |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 87 | ANDROID_IMAGE_DECODER_INVALID_CONVERSION = -3, |
Leon Scroggins III | 97fea5f | 2020-01-30 12:18:20 -0500 | [diff] [blame] | 88 | /** |
| 89 | * The scale is invalid. It may have overflowed, or it may be incompatible |
| 90 | * with the current alpha setting. |
| 91 | */ |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 92 | ANDROID_IMAGE_DECODER_INVALID_SCALE = -4, |
Leon Scroggins III | 97fea5f | 2020-01-30 12:18:20 -0500 | [diff] [blame] | 93 | /** |
Leon Scroggins III | bb5ffd2 | 2020-02-06 11:45:16 -0500 | [diff] [blame] | 94 | * Some other parameter is invalid. |
Leon Scroggins III | 97fea5f | 2020-01-30 12:18:20 -0500 | [diff] [blame] | 95 | */ |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 96 | ANDROID_IMAGE_DECODER_BAD_PARAMETER = -5, |
Leon Scroggins III | 97fea5f | 2020-01-30 12:18:20 -0500 | [diff] [blame] | 97 | /** |
| 98 | * Input was invalid before decoding any pixels. |
| 99 | */ |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 100 | ANDROID_IMAGE_DECODER_INVALID_INPUT = -6, |
Leon Scroggins III | 97fea5f | 2020-01-30 12:18:20 -0500 | [diff] [blame] | 101 | /** |
| 102 | * A seek was required and it failed. |
| 103 | */ |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 104 | ANDROID_IMAGE_DECODER_SEEK_ERROR = -7, |
Leon Scroggins III | 97fea5f | 2020-01-30 12:18:20 -0500 | [diff] [blame] | 105 | /** |
| 106 | * Some other error. For example, an internal allocation failed. |
| 107 | */ |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 108 | ANDROID_IMAGE_DECODER_INTERNAL_ERROR = -8, |
Leon Scroggins III | 97fea5f | 2020-01-30 12:18:20 -0500 | [diff] [blame] | 109 | /** |
| 110 | * AImageDecoder did not recognize the format. |
| 111 | */ |
Leon Scroggins III | 5528700 | 2020-10-13 11:30:11 -0400 | [diff] [blame^] | 112 | ANDROID_IMAGE_DECODER_UNSUPPORTED_FORMAT = -9, |
| 113 | /** |
| 114 | * The animation has reached the end. |
| 115 | */ |
| 116 | ANDROID_IMAGE_DECODER_FINISHED = -10, |
| 117 | /** |
| 118 | * This method cannot be called while the AImageDecoder is in its current |
| 119 | * state. For example, various setters (like {@link AImageDecoder_setTargetSize}) |
| 120 | * can only be called while the AImageDecoder is set to decode the first |
| 121 | * frame of an animation. This ensures that any blending and/or restoring |
| 122 | * prior frames works correctly. |
| 123 | */ |
| 124 | ANDROID_IMAGE_DECODER_INVALID_STATE = -11, |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | struct AImageDecoder; |
| 128 | |
| 129 | /** |
| 130 | * Opaque handle for decoding images. |
| 131 | * |
Leon Scroggins III | 3b3700e | 2020-12-29 11:52:12 -0500 | [diff] [blame] | 132 | * Introduced in API 30 |
| 133 | * |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 134 | * Create using one of the following: |
| 135 | * - {@link AImageDecoder_createFromAAsset} |
| 136 | * - {@link AImageDecoder_createFromFd} |
| 137 | * - {@link AImageDecoder_createFromBuffer} |
Leon Scroggins III | 97fea5f | 2020-01-30 12:18:20 -0500 | [diff] [blame] | 138 | * |
| 139 | * After creation, {@link AImageDecoder_getHeaderInfo} can be used to retrieve |
| 140 | * information about the encoded image. Other functions, like |
| 141 | * {@link AImageDecoder_setTargetSize}, can be used to specify how to decode, and |
| 142 | * {@link AImageDecoder_decode} will decode into client provided memory. |
| 143 | * |
| 144 | * {@link AImageDecoder} objects are NOT thread-safe, and should not be shared across |
| 145 | * threads. |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 146 | */ |
| 147 | typedef struct AImageDecoder AImageDecoder; |
| 148 | |
Leon Scroggins III | 3b3700e | 2020-12-29 11:52:12 -0500 | [diff] [blame] | 149 | #if __ANDROID_API__ >= 30 |
| 150 | |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 151 | /** |
Leon Scroggins III | 97fea5f | 2020-01-30 12:18:20 -0500 | [diff] [blame] | 152 | * Create a new {@link AImageDecoder} from an {@link AAsset}. |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 153 | * |
Elliott Hughes | 7be0e2d | 2020-06-02 13:05:04 -0700 | [diff] [blame] | 154 | * Available since API level 30. |
| 155 | * |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 156 | * @param asset {@link AAsset} containing encoded image data. Client is still |
Leon Scroggins III | 97fea5f | 2020-01-30 12:18:20 -0500 | [diff] [blame] | 157 | * responsible for calling {@link AAsset_close} on it, which may be |
| 158 | * done after deleting the returned {@link AImageDecoder}. |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 159 | * @param outDecoder On success (i.e. return value is |
| 160 | * {@link ANDROID_IMAGE_DECODER_SUCCESS}), this will be set to |
| 161 | * a newly created {@link AImageDecoder}. Caller is |
| 162 | * responsible for calling {@link AImageDecoder_delete} on it. |
| 163 | * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value |
Leon Scroggins III | 97fea5f | 2020-01-30 12:18:20 -0500 | [diff] [blame] | 164 | * indicating the reason for the failure. |
Leon Scroggins III | bb5ffd2 | 2020-02-06 11:45:16 -0500 | [diff] [blame] | 165 | * |
| 166 | * Errors: |
| 167 | * - {@link ANDROID_IMAGE_DECODER_INCOMPLETE}: The asset was truncated before |
| 168 | * reading the image header. |
| 169 | * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: One of the parameters is |
| 170 | * null. |
| 171 | * - {@link ANDROID_IMAGE_DECODER_INVALID_INPUT}: There is an error in the |
| 172 | * header. |
| 173 | * - {@link ANDROID_IMAGE_DECODER_SEEK_ERROR}: The asset failed to seek. |
| 174 | * - {@link ANDROID_IMAGE_DECODER_INTERNAL_ERROR}: Some other error, like a |
| 175 | * failure to allocate memory. |
| 176 | * - {@link ANDROID_IMAGE_DECODER_UNSUPPORTED_FORMAT}: The format is not |
| 177 | * supported. |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 178 | */ |
Leon Scroggins III | 4a8ecfe | 2020-10-07 10:53:24 -0400 | [diff] [blame] | 179 | int AImageDecoder_createFromAAsset(struct AAsset* _Nonnull asset, |
| 180 | AImageDecoder* _Nonnull * _Nonnull outDecoder) |
Leon Scroggins III | a9f397b | 2020-01-27 12:42:56 -0500 | [diff] [blame] | 181 | __INTRODUCED_IN(30); |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 182 | |
| 183 | /** |
Leon Scroggins III | 97fea5f | 2020-01-30 12:18:20 -0500 | [diff] [blame] | 184 | * Create a new {@link AImageDecoder} from a file descriptor. |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 185 | * |
Elliott Hughes | 7be0e2d | 2020-06-02 13:05:04 -0700 | [diff] [blame] | 186 | * Available since API level 30. |
| 187 | * |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 188 | * @param fd Seekable, readable, open file descriptor for encoded data. |
| 189 | * Client is still responsible for closing it, which may be done |
Leon Scroggins III | 97fea5f | 2020-01-30 12:18:20 -0500 | [diff] [blame] | 190 | * after deleting the returned {@link AImageDecoder}. |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 191 | * @param outDecoder On success (i.e. return value is |
| 192 | * {@link ANDROID_IMAGE_DECODER_SUCCESS}), this will be set to |
| 193 | * a newly created {@link AImageDecoder}. Caller is |
| 194 | * responsible for calling {@link AImageDecoder_delete} on it. |
| 195 | * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value |
Leon Scroggins III | 97fea5f | 2020-01-30 12:18:20 -0500 | [diff] [blame] | 196 | * indicating the reason for the failure. |
Leon Scroggins III | bb5ffd2 | 2020-02-06 11:45:16 -0500 | [diff] [blame] | 197 | * |
| 198 | * Errors: |
| 199 | * - {@link ANDROID_IMAGE_DECODER_INCOMPLETE}: The file was truncated before |
| 200 | * reading the image header. |
| 201 | * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The {@link AImageDecoder} is |
| 202 | * null, or |fd| does not represent a valid, seekable file descriptor. |
| 203 | * - {@link ANDROID_IMAGE_DECODER_INVALID_INPUT}: There is an error in the |
| 204 | * header. |
| 205 | * - {@link ANDROID_IMAGE_DECODER_SEEK_ERROR}: The descriptor failed to seek. |
| 206 | * - {@link ANDROID_IMAGE_DECODER_INTERNAL_ERROR}: Some other error, like a |
| 207 | * failure to allocate memory. |
| 208 | * - {@link ANDROID_IMAGE_DECODER_UNSUPPORTED_FORMAT}: The format is not |
| 209 | * supported. |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 210 | */ |
Leon Scroggins III | 4a8ecfe | 2020-10-07 10:53:24 -0400 | [diff] [blame] | 211 | int AImageDecoder_createFromFd(int fd, AImageDecoder* _Nonnull * _Nonnull outDecoder) |
| 212 | __INTRODUCED_IN(30); |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 213 | |
| 214 | /** |
| 215 | * Create a new AImageDecoder from a buffer. |
| 216 | * |
Elliott Hughes | 7be0e2d | 2020-06-02 13:05:04 -0700 | [diff] [blame] | 217 | * Available since API level 30. |
| 218 | * |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 219 | * @param buffer Pointer to encoded data. Must be valid for the entire time |
Leon Scroggins III | 97fea5f | 2020-01-30 12:18:20 -0500 | [diff] [blame] | 220 | * the {@link AImageDecoder} is used. |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 221 | * @param length Byte length of buffer. |
| 222 | * @param outDecoder On success (i.e. return value is |
| 223 | * {@link ANDROID_IMAGE_DECODER_SUCCESS}), this will be set to |
| 224 | * a newly created {@link AImageDecoder}. Caller is |
| 225 | * responsible for calling {@link AImageDecoder_delete} on it. |
| 226 | * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value |
Leon Scroggins III | bb5ffd2 | 2020-02-06 11:45:16 -0500 | [diff] [blame] | 227 | * indicating the reason for the failure. |
| 228 | * |
| 229 | * Errors: |
| 230 | * - {@link ANDROID_IMAGE_DECODER_INCOMPLETE}: The encoded image was truncated before |
| 231 | * reading the image header. |
| 232 | * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: One of the parameters is |
| 233 | * invalid. |
| 234 | * - {@link ANDROID_IMAGE_DECODER_INVALID_INPUT}: There is an error in the |
| 235 | * header. |
| 236 | * - {@link ANDROID_IMAGE_DECODER_INTERNAL_ERROR}: Some other error, like a |
| 237 | * failure to allocate memory. |
| 238 | * - {@link ANDROID_IMAGE_DECODER_UNSUPPORTED_FORMAT}: The format is not |
| 239 | * supported. |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 240 | */ |
Leon Scroggins III | 4a8ecfe | 2020-10-07 10:53:24 -0400 | [diff] [blame] | 241 | int AImageDecoder_createFromBuffer(const void* _Nonnull buffer, size_t length, |
| 242 | AImageDecoder* _Nonnull * _Nonnull outDecoder) |
| 243 | __INTRODUCED_IN(30); |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 244 | |
| 245 | /** |
| 246 | * Delete the AImageDecoder. |
Elliott Hughes | 7be0e2d | 2020-06-02 13:05:04 -0700 | [diff] [blame] | 247 | * |
| 248 | * Available since API level 30. |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 249 | */ |
Leon Scroggins III | 4a8ecfe | 2020-10-07 10:53:24 -0400 | [diff] [blame] | 250 | void AImageDecoder_delete(AImageDecoder* _Nonnull decoder) __INTRODUCED_IN(30); |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 251 | |
| 252 | /** |
| 253 | * Choose the desired output format. |
| 254 | * |
Leon Scroggins III | 5528700 | 2020-10-13 11:30:11 -0400 | [diff] [blame^] | 255 | * If the encoded image represents an animation, this must be called while on |
| 256 | * the first frame (e.g. before calling {@link AImageDecoder_advanceFrame} or |
| 257 | * after calling {@link AImageDecoder_rewind}). |
| 258 | * |
Elliott Hughes | 7be0e2d | 2020-06-02 13:05:04 -0700 | [diff] [blame] | 259 | * Available since API level 30. |
| 260 | * |
Leon Scroggins III | 5d0445c | 2020-01-23 09:43:43 -0500 | [diff] [blame] | 261 | * @param format {@link AndroidBitmapFormat} to use for the output. |
Leon Scroggins III | bb5ffd2 | 2020-02-06 11:45:16 -0500 | [diff] [blame] | 262 | * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value |
| 263 | * indicating the reason for the failure. On failure, the |
| 264 | * {@link AImageDecoder} uses the format it was already planning |
| 265 | * to use (either its default or a previously successful setting |
| 266 | * from this function). |
| 267 | * |
| 268 | * Errors: |
| 269 | * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The |
| 270 | * {@link AImageDecoder} is null or |format| does not correspond to an |
| 271 | * {@link AndroidBitmapFormat}. |
| 272 | * - {@link ANDROID_IMAGE_DECODER_INVALID_CONVERSION}: The |
| 273 | * {@link AndroidBitmapFormat} is incompatible with the image. |
Leon Scroggins III | 5528700 | 2020-10-13 11:30:11 -0400 | [diff] [blame^] | 274 | * - {@link ANDROID_IMAGE_DECODER_INVALID_STATE}: The animation is not on |
| 275 | * the first frame. |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 276 | */ |
Leon Scroggins III | 4a8ecfe | 2020-10-07 10:53:24 -0400 | [diff] [blame] | 277 | int AImageDecoder_setAndroidBitmapFormat(AImageDecoder* _Nonnull decoder, |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 278 | int32_t format) __INTRODUCED_IN(30); |
| 279 | |
Leon Scroggins III | 1be112f | 2020-01-15 04:26:44 -0500 | [diff] [blame] | 280 | /** |
| 281 | * Specify whether the output's pixels should be unpremultiplied. |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 282 | * |
Leon Scroggins III | 97fea5f | 2020-01-30 12:18:20 -0500 | [diff] [blame] | 283 | * By default, {@link AImageDecoder_decodeImage} will premultiply the pixels, if they have alpha. |
| 284 | * Pass true to this method to leave them unpremultiplied. This has no effect on an |
Leon Scroggins III | 1be112f | 2020-01-15 04:26:44 -0500 | [diff] [blame] | 285 | * opaque image. |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 286 | * |
Leon Scroggins III | 5528700 | 2020-10-13 11:30:11 -0400 | [diff] [blame^] | 287 | * If the encoded image represents an animation, this must be called while on |
| 288 | * the first frame (e.g. before calling {@link AImageDecoder_advanceFrame} or |
| 289 | * after calling {@link AImageDecoder_rewind}). |
| 290 | * |
Elliott Hughes | 7be0e2d | 2020-06-02 13:05:04 -0700 | [diff] [blame] | 291 | * Available since API level 30. |
| 292 | * |
Leon Scroggins III | 97fea5f | 2020-01-30 12:18:20 -0500 | [diff] [blame] | 293 | * @param unpremultipliedRequired Pass true to leave the pixels unpremultiplied. |
Leon Scroggins III | bb5ffd2 | 2020-02-06 11:45:16 -0500 | [diff] [blame] | 294 | * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value |
| 295 | * indicating the reason for the failure. |
| 296 | * |
| 297 | * Errors: |
| 298 | * - {@link ANDROID_IMAGE_DECODER_INVALID_CONVERSION}: Unpremultiplied is not |
| 299 | * possible due to an existing scale set by |
| 300 | * {@link AImageDecoder_setTargetSize}. |
| 301 | * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The |
| 302 | * {@link AImageDecoder} is null. |
Leon Scroggins III | 5528700 | 2020-10-13 11:30:11 -0400 | [diff] [blame^] | 303 | * - {@link ANDROID_IMAGE_DECODER_INVALID_STATE}: The animation is not on |
| 304 | * the first frame. |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 305 | */ |
Leon Scroggins III | 4a8ecfe | 2020-10-07 10:53:24 -0400 | [diff] [blame] | 306 | int AImageDecoder_setUnpremultipliedRequired(AImageDecoder* _Nonnull decoder, |
Leon Scroggins III | 97fea5f | 2020-01-30 12:18:20 -0500 | [diff] [blame] | 307 | bool unpremultipliedRequired) __INTRODUCED_IN(30); |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 308 | |
| 309 | /** |
Leon Scroggins III | 20d480c | 2020-01-15 15:32:59 -0500 | [diff] [blame] | 310 | * Choose the dataspace for the output. |
| 311 | * |
Leon Scroggins III | 97fea5f | 2020-01-30 12:18:20 -0500 | [diff] [blame] | 312 | * Ignored by {@link ANDROID_BITMAP_FORMAT_A_8}, which does not support |
| 313 | * an {@link ADataSpace}. |
Leon Scroggins III | 20d480c | 2020-01-15 15:32:59 -0500 | [diff] [blame] | 314 | * |
Leon Scroggins III | 5528700 | 2020-10-13 11:30:11 -0400 | [diff] [blame^] | 315 | * If the encoded image represents an animation, this must be called while on |
| 316 | * the first frame (e.g. before calling {@link AImageDecoder_advanceFrame} or |
| 317 | * after calling {@link AImageDecoder_rewind}). |
| 318 | * |
Elliott Hughes | 7be0e2d | 2020-06-02 13:05:04 -0700 | [diff] [blame] | 319 | * Available since API level 30. |
| 320 | * |
Leon Scroggins III | 20d480c | 2020-01-15 15:32:59 -0500 | [diff] [blame] | 321 | * @param dataspace The {@link ADataSpace} to decode into. An ADataSpace |
| 322 | * specifies how to interpret the colors. By default, |
| 323 | * AImageDecoder will decode into the ADataSpace specified by |
| 324 | * {@link AImageDecoderHeaderInfo_getDataSpace}. If this |
| 325 | * parameter is set to a different ADataSpace, AImageDecoder |
| 326 | * will transform the output into the specified ADataSpace. |
Leon Scroggins III | bb5ffd2 | 2020-02-06 11:45:16 -0500 | [diff] [blame] | 327 | * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value |
| 328 | * indicating the reason for the failure. |
| 329 | * |
| 330 | * Errors: |
| 331 | * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The |
| 332 | * {@link AImageDecoder} is null or |dataspace| does not correspond to an |
| 333 | * {@link ADataSpace} value. |
Leon Scroggins III | 5528700 | 2020-10-13 11:30:11 -0400 | [diff] [blame^] | 334 | * - {@link ANDROID_IMAGE_DECODER_INVALID_STATE}: The animation is not on |
| 335 | * the first frame. |
Leon Scroggins III | 20d480c | 2020-01-15 15:32:59 -0500 | [diff] [blame] | 336 | */ |
Leon Scroggins III | 4a8ecfe | 2020-10-07 10:53:24 -0400 | [diff] [blame] | 337 | int AImageDecoder_setDataSpace(AImageDecoder* _Nonnull decoder, int32_t dataspace) |
| 338 | __INTRODUCED_IN(30); |
Leon Scroggins III | 20d480c | 2020-01-15 15:32:59 -0500 | [diff] [blame] | 339 | |
| 340 | /** |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 341 | * Specify the output size for a decoded image. |
| 342 | * |
| 343 | * Future calls to {@link AImageDecoder_decodeImage} will sample or scale the |
| 344 | * encoded image to reach the desired size. If a crop rect is set (via |
| 345 | * {@link AImageDecoder_setCrop}), it must be contained within the dimensions |
| 346 | * specified by width and height, and the output image will be the size of the |
| 347 | * crop rect. |
| 348 | * |
Leon Scroggins III | 5528700 | 2020-10-13 11:30:11 -0400 | [diff] [blame^] | 349 | * If the encoded image represents an animation, this must be called while on |
| 350 | * the first frame (e.g. before calling {@link AImageDecoder_advanceFrame} or |
| 351 | * after calling {@link AImageDecoder_rewind}). |
| 352 | * |
Elliott Hughes | 7be0e2d | 2020-06-02 13:05:04 -0700 | [diff] [blame] | 353 | * Available since API level 30. |
| 354 | * |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 355 | * @param width Width of the output (prior to cropping). |
| 356 | * This will affect future calls to |
| 357 | * {@link AImageDecoder_getMinimumStride}, which will now return |
| 358 | * a value based on this width. |
| 359 | * @param height Height of the output (prior to cropping). |
Leon Scroggins III | bb5ffd2 | 2020-02-06 11:45:16 -0500 | [diff] [blame] | 360 | * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value |
| 361 | * indicating the reason for the failure. |
| 362 | * |
| 363 | * Errors: |
| 364 | * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The |
| 365 | * {@link AImageDecoder} is null. |
| 366 | * - {@link ANDROID_IMAGE_DECODER_INVALID_SCALE}: |width| or |height| is <= 0, |
Leon Scroggins III | 5528700 | 2020-10-13 11:30:11 -0400 | [diff] [blame^] | 367 | * the size is too big, any existing crop is not contained by the new image |
| 368 | * dimensions, or the scale is incompatible with a previous call to |
Leon Scroggins III | bb5ffd2 | 2020-02-06 11:45:16 -0500 | [diff] [blame] | 369 | * {@link AImageDecoder_setUnpremultipliedRequired}(true). |
Leon Scroggins III | 5528700 | 2020-10-13 11:30:11 -0400 | [diff] [blame^] | 370 | * - {@link ANDROID_IMAGE_DECODER_INVALID_STATE}: The animation is not on |
| 371 | * the first frame. |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 372 | */ |
Leon Scroggins III | 4a8ecfe | 2020-10-07 10:53:24 -0400 | [diff] [blame] | 373 | int AImageDecoder_setTargetSize(AImageDecoder* _Nonnull decoder, int32_t width, |
| 374 | int32_t height) __INTRODUCED_IN(30); |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 375 | |
Leon Scroggins III | f27256b | 2020-01-19 21:13:04 -0500 | [diff] [blame] | 376 | /** |
| 377 | * Compute the dimensions to use for a given sampleSize. |
| 378 | * |
| 379 | * Although AImageDecoder can scale to an arbitrary target size (see |
| 380 | * {@link AImageDecoder_setTargetSize}), some sizes may be more efficient than |
| 381 | * others. This computes the most efficient target size to use to reach a |
| 382 | * particular sampleSize. |
| 383 | * |
Elliott Hughes | 7be0e2d | 2020-06-02 13:05:04 -0700 | [diff] [blame] | 384 | * Available since API level 30. |
| 385 | * |
Leon Scroggins III | f27256b | 2020-01-19 21:13:04 -0500 | [diff] [blame] | 386 | * @param sampleSize A subsampling rate of the original image. Must be greater |
| 387 | * than or equal to 1. A sampleSize of 2 means to skip every |
| 388 | * other pixel/line, resulting in a width and height that are |
| 389 | * 1/2 of the original dimensions, with 1/4 the number of |
| 390 | * pixels. |
| 391 | * @param width Out parameter for the width sampled by sampleSize, and rounded |
Leon Scroggins III | 97fea5f | 2020-01-30 12:18:20 -0500 | [diff] [blame] | 392 | * in the direction that the decoder can do most efficiently. |
Leon Scroggins III | f27256b | 2020-01-19 21:13:04 -0500 | [diff] [blame] | 393 | * @param height Out parameter for the height sampled by sampleSize, and rounded |
Leon Scroggins III | 97fea5f | 2020-01-30 12:18:20 -0500 | [diff] [blame] | 394 | * in the direction that the decoder can do most efficiently. |
Leon Scroggins III | bb5ffd2 | 2020-02-06 11:45:16 -0500 | [diff] [blame] | 395 | * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value |
| 396 | * indicating the reason for the failure. |
| 397 | * |
| 398 | * Errors: |
| 399 | * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The |
| 400 | * {@link AImageDecoder}, |width| or |height| is null or |sampleSize| is < 1. |
Leon Scroggins III | f27256b | 2020-01-19 21:13:04 -0500 | [diff] [blame] | 401 | */ |
Leon Scroggins III | 4a8ecfe | 2020-10-07 10:53:24 -0400 | [diff] [blame] | 402 | int AImageDecoder_computeSampledSize(const AImageDecoder* _Nonnull decoder, int sampleSize, |
| 403 | int32_t* _Nonnull width, int32_t* _Nonnull height) |
| 404 | __INTRODUCED_IN(30); |
Leon Scroggins III | 3b3700e | 2020-12-29 11:52:12 -0500 | [diff] [blame] | 405 | |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 406 | /** |
| 407 | * Specify how to crop the output after scaling (if any). |
| 408 | * |
| 409 | * Future calls to {@link AImageDecoder_decodeImage} will crop their output to |
| 410 | * the specified {@link ARect}. Clients will only need to allocate enough memory |
| 411 | * for the cropped ARect. |
| 412 | * |
Leon Scroggins III | 5528700 | 2020-10-13 11:30:11 -0400 | [diff] [blame^] | 413 | * If the encoded image represents an animation, this must be called while on |
| 414 | * the first frame (e.g. before calling {@link AImageDecoder_advanceFrame} or |
| 415 | * after calling {@link AImageDecoder_rewind}). |
| 416 | * |
Elliott Hughes | 7be0e2d | 2020-06-02 13:05:04 -0700 | [diff] [blame] | 417 | * Available since API level 30. |
| 418 | * |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 419 | * @param crop Rectangle describing a crop of the decode. It must be contained inside of |
| 420 | * the (possibly scaled, by {@link AImageDecoder_setTargetSize}) |
| 421 | * image dimensions. This will affect future calls to |
| 422 | * {@link AImageDecoder_getMinimumStride}, which will now return a |
| 423 | * value based on the width of the crop. An empty ARect - |
| 424 | * specifically { 0, 0, 0, 0 } - may be used to remove the cropping |
| 425 | * behavior. Any other empty or unsorted ARects will result in |
Leon Scroggins III | 97fea5f | 2020-01-30 12:18:20 -0500 | [diff] [blame] | 426 | * returning {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}. |
Leon Scroggins III | bb5ffd2 | 2020-02-06 11:45:16 -0500 | [diff] [blame] | 427 | * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value |
| 428 | * indicating the reason for the failure. |
| 429 | * |
| 430 | * Errors: |
| 431 | * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The |
Leon Scroggins III | 5528700 | 2020-10-13 11:30:11 -0400 | [diff] [blame^] | 432 | * {@link AImageDecoder} is null, or the crop is not contained by the |
Leon Scroggins III | bb5ffd2 | 2020-02-06 11:45:16 -0500 | [diff] [blame] | 433 | * (possibly scaled) image dimensions. |
Leon Scroggins III | 5528700 | 2020-10-13 11:30:11 -0400 | [diff] [blame^] | 434 | * - {@link ANDROID_IMAGE_DECODER_INVALID_STATE}: The animation is not on |
| 435 | * the first frame. |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 436 | */ |
Leon Scroggins III | 4a8ecfe | 2020-10-07 10:53:24 -0400 | [diff] [blame] | 437 | int AImageDecoder_setCrop(AImageDecoder* _Nonnull decoder, ARect crop) __INTRODUCED_IN(30); |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 438 | |
Leon Scroggins III | 3b3700e | 2020-12-29 11:52:12 -0500 | [diff] [blame] | 439 | #endif // __ANDROID_API__ >= 30 |
| 440 | |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 441 | struct AImageDecoderHeaderInfo; |
Leon Scroggins III | 97fea5f | 2020-01-30 12:18:20 -0500 | [diff] [blame] | 442 | /** |
Leon Scroggins III | 3b3700e | 2020-12-29 11:52:12 -0500 | [diff] [blame] | 443 | * Opaque handle for representing information about the encoded image. |
| 444 | * |
| 445 | * Introduced in API 30 |
| 446 | * |
| 447 | * Retrieved using {@link AImageDecoder_getHeaderInfo} and passed to methods |
| 448 | * like {@link AImageDecoderHeaderInfo_getWidth} and |
Leon Scroggins III | 97fea5f | 2020-01-30 12:18:20 -0500 | [diff] [blame] | 449 | * {@link AImageDecoderHeaderInfo_getHeight}. |
| 450 | */ |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 451 | typedef struct AImageDecoderHeaderInfo AImageDecoderHeaderInfo; |
| 452 | |
Leon Scroggins III | 3b3700e | 2020-12-29 11:52:12 -0500 | [diff] [blame] | 453 | #if __ANDROID_API__ >= 30 |
| 454 | |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 455 | /** |
| 456 | * Return an opaque handle for reading header info. |
| 457 | * |
| 458 | * This is owned by the {@link AImageDecoder} and will be destroyed when the |
| 459 | * AImageDecoder is destroyed via {@link AImageDecoder_delete}. |
Elliott Hughes | 7be0e2d | 2020-06-02 13:05:04 -0700 | [diff] [blame] | 460 | * |
| 461 | * Available since API level 30. |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 462 | */ |
Leon Scroggins III | 4a8ecfe | 2020-10-07 10:53:24 -0400 | [diff] [blame] | 463 | const AImageDecoderHeaderInfo* _Nonnull AImageDecoder_getHeaderInfo( |
| 464 | const AImageDecoder* _Nonnull decoder) __INTRODUCED_IN(30); |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 465 | |
| 466 | /** |
Leon Scroggins III | 97fea5f | 2020-01-30 12:18:20 -0500 | [diff] [blame] | 467 | * Report the native width of the encoded image. This is also the logical |
| 468 | * pixel width of the output, unless {@link AImageDecoder_setTargetSize} is |
Leon Scroggins III | bb5ffd2 | 2020-02-06 11:45:16 -0500 | [diff] [blame] | 469 | * used to choose a different size or {@link AImageDecoder_setCrop} is used to |
| 470 | * set a crop rect. |
Elliott Hughes | 7be0e2d | 2020-06-02 13:05:04 -0700 | [diff] [blame] | 471 | * |
| 472 | * Available since API level 30. |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 473 | */ |
Leon Scroggins III | 4a8ecfe | 2020-10-07 10:53:24 -0400 | [diff] [blame] | 474 | int32_t AImageDecoderHeaderInfo_getWidth(const AImageDecoderHeaderInfo* _Nonnull) |
| 475 | __INTRODUCED_IN(30); |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 476 | |
| 477 | /** |
Leon Scroggins III | 97fea5f | 2020-01-30 12:18:20 -0500 | [diff] [blame] | 478 | * Report the native height of the encoded image. This is also the logical |
| 479 | * pixel height of the output, unless {@link AImageDecoder_setTargetSize} is |
Leon Scroggins III | bb5ffd2 | 2020-02-06 11:45:16 -0500 | [diff] [blame] | 480 | * used to choose a different size or {@link AImageDecoder_setCrop} is used to |
| 481 | * set a crop rect. |
Elliott Hughes | 7be0e2d | 2020-06-02 13:05:04 -0700 | [diff] [blame] | 482 | * |
| 483 | * Available since API level 30. |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 484 | */ |
Leon Scroggins III | 4a8ecfe | 2020-10-07 10:53:24 -0400 | [diff] [blame] | 485 | int32_t AImageDecoderHeaderInfo_getHeight(const AImageDecoderHeaderInfo* _Nonnull) |
| 486 | __INTRODUCED_IN(30); |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 487 | |
| 488 | /** |
| 489 | * Report the mimeType of the encoded image. |
| 490 | * |
Elliott Hughes | 7be0e2d | 2020-06-02 13:05:04 -0700 | [diff] [blame] | 491 | * Available since API level 30. |
| 492 | * |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 493 | * @return a string literal describing the mime type. |
| 494 | */ |
Leon Scroggins III | 4a8ecfe | 2020-10-07 10:53:24 -0400 | [diff] [blame] | 495 | const char* _Nonnull AImageDecoderHeaderInfo_getMimeType( |
| 496 | const AImageDecoderHeaderInfo* _Nonnull) __INTRODUCED_IN(30); |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 497 | |
| 498 | /** |
Leon Scroggins III | 5d0445c | 2020-01-23 09:43:43 -0500 | [diff] [blame] | 499 | * Report the {@link AndroidBitmapFormat} the AImageDecoder will decode to |
Leon Scroggins III | 97fea5f | 2020-01-30 12:18:20 -0500 | [diff] [blame] | 500 | * by default. {@link AImageDecoder} will try to choose one that is sensible |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 501 | * for the image and the system. Note that this does not indicate the |
| 502 | * encoded format of the image. |
Elliott Hughes | 7be0e2d | 2020-06-02 13:05:04 -0700 | [diff] [blame] | 503 | * |
| 504 | * Available since API level 30. |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 505 | */ |
Leon Scroggins III | 5d0445c | 2020-01-23 09:43:43 -0500 | [diff] [blame] | 506 | int32_t AImageDecoderHeaderInfo_getAndroidBitmapFormat( |
Leon Scroggins III | 4a8ecfe | 2020-10-07 10:53:24 -0400 | [diff] [blame] | 507 | const AImageDecoderHeaderInfo* _Nonnull) __INTRODUCED_IN(30); |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 508 | |
| 509 | /** |
Leon Scroggins III | 97fea5f | 2020-01-30 12:18:20 -0500 | [diff] [blame] | 510 | * Report how the {@link AImageDecoder} will handle alpha by default. If the image |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 511 | * contains no alpha (according to its header), this will return |
| 512 | * {@link ANDROID_BITMAP_FLAGS_ALPHA_OPAQUE}. If the image may contain alpha, |
Leon Scroggins III | 97fea5f | 2020-01-30 12:18:20 -0500 | [diff] [blame] | 513 | * this returns {@link ANDROID_BITMAP_FLAGS_ALPHA_PREMUL}, because |
| 514 | * {@link AImageDecoder_decodeImage} will premultiply pixels by default. |
Elliott Hughes | 7be0e2d | 2020-06-02 13:05:04 -0700 | [diff] [blame] | 515 | * |
| 516 | * Available since API level 30. |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 517 | */ |
| 518 | int AImageDecoderHeaderInfo_getAlphaFlags( |
Leon Scroggins III | 4a8ecfe | 2020-10-07 10:53:24 -0400 | [diff] [blame] | 519 | const AImageDecoderHeaderInfo* _Nonnull) __INTRODUCED_IN(30); |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 520 | |
| 521 | /** |
Leon Scroggins III | 8cee65e | 2020-01-30 14:20:42 -0500 | [diff] [blame] | 522 | * Report the dataspace the AImageDecoder will decode to by default. |
Leon Scroggins III | 20d480c | 2020-01-15 15:32:59 -0500 | [diff] [blame] | 523 | * |
Leon Scroggins III | 8cee65e | 2020-01-30 14:20:42 -0500 | [diff] [blame] | 524 | * By default, {@link AImageDecoder_decodeImage} will not do any color |
| 525 | * conversion. |
| 526 | * |
Elliott Hughes | 7be0e2d | 2020-06-02 13:05:04 -0700 | [diff] [blame] | 527 | * Available since API level 30. |
| 528 | * |
Leon Scroggins III | 8cee65e | 2020-01-30 14:20:42 -0500 | [diff] [blame] | 529 | * @return The {@link ADataSpace} representing the way the colors |
| 530 | * are encoded (or {@link ADATASPACE_UNKNOWN} if there is not a |
| 531 | * corresponding ADataSpace). This specifies how to interpret the colors |
Leon Scroggins III | 20d480c | 2020-01-15 15:32:59 -0500 | [diff] [blame] | 532 | * in the decoded image, unless {@link AImageDecoder_setDataSpace} is |
| 533 | * called to decode to a different ADataSpace. |
| 534 | * |
| 535 | * Note that ADataSpace only exposes a few values. This may return |
Leon Scroggins III | 8cee65e | 2020-01-30 14:20:42 -0500 | [diff] [blame] | 536 | * {@link ADATASPACE_UNKNOWN}, even for Named ColorSpaces, if they have |
| 537 | * no corresponding {@link ADataSpace}. |
Leon Scroggins III | 20d480c | 2020-01-15 15:32:59 -0500 | [diff] [blame] | 538 | */ |
| 539 | int32_t AImageDecoderHeaderInfo_getDataSpace( |
Leon Scroggins III | 4a8ecfe | 2020-10-07 10:53:24 -0400 | [diff] [blame] | 540 | const AImageDecoderHeaderInfo* _Nonnull) __INTRODUCED_IN(30); |
Leon Scroggins III | 20d480c | 2020-01-15 15:32:59 -0500 | [diff] [blame] | 541 | |
| 542 | /** |
Leon Scroggins III | 97fea5f | 2020-01-30 12:18:20 -0500 | [diff] [blame] | 543 | * Return the minimum stride that can be used in |
| 544 | * {@link AImageDecoder_decodeImage). |
| 545 | * |
| 546 | * This stride provides no padding, meaning it will be exactly equal to the |
| 547 | * width times the number of bytes per pixel for the {@link AndroidBitmapFormat} |
| 548 | * being used. |
| 549 | * |
| 550 | * If the output is scaled (via {@link AImageDecoder_setTargetSize}) and/or |
| 551 | * cropped (via {@link AImageDecoder_setCrop}), this takes those into account. |
Elliott Hughes | 7be0e2d | 2020-06-02 13:05:04 -0700 | [diff] [blame] | 552 | * |
| 553 | * Available since API level 30. |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 554 | */ |
Leon Scroggins III | 4a8ecfe | 2020-10-07 10:53:24 -0400 | [diff] [blame] | 555 | size_t AImageDecoder_getMinimumStride(AImageDecoder* _Nonnull decoder) __INTRODUCED_IN(30); |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 556 | |
| 557 | /** |
Leon Scroggins III | 97fea5f | 2020-01-30 12:18:20 -0500 | [diff] [blame] | 558 | * Decode the image into pixels, using the settings of the {@link AImageDecoder}. |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 559 | * |
Elliott Hughes | 7be0e2d | 2020-06-02 13:05:04 -0700 | [diff] [blame] | 560 | * Available since API level 30. |
| 561 | * |
Leon Scroggins III | 1b38971 | 2020-10-09 13:12:39 -0400 | [diff] [blame] | 562 | * Starting in API level 31, it can be used to decode all of the frames of an |
Leon Scroggins III | 5528700 | 2020-10-13 11:30:11 -0400 | [diff] [blame^] | 563 | * animated image (i.e. GIF, WebP, HEIF) using new APIs. Internally, |
| 564 | * AImageDecoder keeps track of its "current frame" - that is, the frame that |
| 565 | * will be decoded by a call to AImageDecoder_decodeImage. At creation time, the |
| 566 | * current frame is always the first frame, and multiple calls to this method |
| 567 | * will each decode the first frame. {@link AImageDecoder_advanceFrame} advances |
| 568 | * the current frame to the following frame, so that future calls to this method |
| 569 | * will decode that frame. Some frames may update only part of the image. They |
| 570 | * may only update a sub-rectangle (see {@link |
| 571 | * AImageDecoderFrameInfo_getFrameRect}), or they may have alpha (see |
| 572 | * {@link AImageDecoderFrameInfo_hasAlphaWithinBounds}). In these cases, this |
| 573 | * method assumes that the prior frame is still residing in the |pixels| buffer, |
| 574 | * decodes only the new portion, and blends it with the buffer. Frames that change |
| 575 | * the entire |pixels| buffer are "independent", and do not require the prior |
| 576 | * frame to remain in the buffer. The first frame is always independent. A |
| 577 | * sophisticated client can use information from the {@link AImageDecoderFrameInfo} |
| 578 | * to determine whether other frames are independent, or what frames they rely on. |
| 579 | * |
| 580 | * If the current frame is marked {@link ANDROID_IMAGE_DECODER_DISPOSE_OP_PREVIOUS}, |
| 581 | * AImageDecoder_decodeImage will cache the |pixels| buffer prior to decoding |
| 582 | * (note: this only happens for the first in a string of consecutive |
| 583 | * ANDROID_IMAGE_DECODER_DISPOSE_OP_PREVIOUS frames). After advancing to the |
| 584 | * following frame, AImageDecoder_decodeImage will restore the cached buffer |
| 585 | * prior to decoding that frame. |
| 586 | * |
| 587 | * Ignoring timing information, display, etc, a client wishing to decode all |
| 588 | * frames of an animated image may conceptually use code like the following: |
| 589 | * |
| 590 | * while (true) { |
| 591 | * int result = AImageDecoder_decodeImage(decoder, pixels, stride, size); |
| 592 | * if (result != ANDROID_IMAGE_DECODER_SUCCESS) break; |
| 593 | * |
| 594 | * // Display or save the image in |pixels|, keeping the buffer intact for |
| 595 | * // AImageDecoder to decode the next frame correctly. |
| 596 | * Application_viewImage(pixels); |
| 597 | * |
| 598 | * result = AImageDecoder_advanceFrame(decoder); |
| 599 | * if (result != ANDROID_IMAGE_DECODER_SUCCESS) break; |
| 600 | * } |
Leon Scroggins III | 1b38971 | 2020-10-09 13:12:39 -0400 | [diff] [blame] | 601 | * |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 602 | * @param decoder Opaque object representing the decoder. |
| 603 | * @param pixels On success, will be filled with the result |
Leon Scroggins III | 97fea5f | 2020-01-30 12:18:20 -0500 | [diff] [blame] | 604 | * of the decode. Must be large enough to hold |size| bytes. |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 605 | * @param stride Width in bytes of a single row. Must be at least |
Leon Scroggins III | 97fea5f | 2020-01-30 12:18:20 -0500 | [diff] [blame] | 606 | * {@link AImageDecoder_getMinimumStride} and a multiple of the |
| 607 | * bytes per pixel of the {@link AndroidBitmapFormat}. |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 608 | * @param size Size of the pixel buffer in bytes. Must be at least |
| 609 | * stride * (height - 1) + |
Leon Scroggins III | 97fea5f | 2020-01-30 12:18:20 -0500 | [diff] [blame] | 610 | * {@link AImageDecoder_getMinimumStride}. |
Leon Scroggins III | bb5ffd2 | 2020-02-06 11:45:16 -0500 | [diff] [blame] | 611 | * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value |
| 612 | * indicating the reason for the failure. |
| 613 | * |
| 614 | * Errors: |
| 615 | * - {@link ANDROID_IMAGE_DECODER_INCOMPLETE}: The image was truncated. A |
| 616 | * partial image was decoded, and undecoded lines have been initialized to all |
| 617 | * zeroes. |
| 618 | * - {@link ANDROID_IMAGE_DECODER_ERROR}: The image contained an error. A |
| 619 | * partial image was decoded, and undecoded lines have been initialized to all |
| 620 | * zeroes. |
| 621 | * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The {@link AImageDecoder} or |
| 622 | * |pixels| is null, the stride is not large enough or not pixel aligned, or |
| 623 | * |size| is not large enough. |
| 624 | * - {@link ANDROID_IMAGE_DECODER_SEEK_ERROR}: The asset or file descriptor |
| 625 | * failed to seek. |
| 626 | * - {@link ANDROID_IMAGE_DECODER_INTERNAL_ERROR}: Some other error, like a |
| 627 | * failure to allocate memory. |
Leon Scroggins III | 5528700 | 2020-10-13 11:30:11 -0400 | [diff] [blame^] | 628 | * - {@link ANDROID_IMAGE_DECODER_FINISHED}: The input contains no |
| 629 | * more frames. No decoding occurred. The client must call |
| 630 | * {@link AImageDecoder_rewind} before calling |
| 631 | * {@link AImageDecoder_decodeImage} again. |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 632 | */ |
Leon Scroggins III | 4a8ecfe | 2020-10-07 10:53:24 -0400 | [diff] [blame] | 633 | int AImageDecoder_decodeImage(AImageDecoder* _Nonnull decoder, |
| 634 | void* _Nonnull pixels, size_t stride, |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 635 | size_t size) __INTRODUCED_IN(30); |
| 636 | |
| 637 | #endif // __ANDROID_API__ >= 30 |
| 638 | |
Leon Scroggins III | 1b38971 | 2020-10-09 13:12:39 -0400 | [diff] [blame] | 639 | #if __ANDROID_API__ >= 31 |
| 640 | |
| 641 | /** |
| 642 | * Return true iff the image is animated - i.e. has multiple frames. |
| 643 | * |
| 644 | * Introduced in API 31. |
| 645 | * |
Leon Scroggins III | 5528700 | 2020-10-13 11:30:11 -0400 | [diff] [blame^] | 646 | * A single frame GIF is considered to *not* be animated. This may require |
| 647 | * seeking past the first frame to verify whether there is a following frame. |
Leon Scroggins III | 1b38971 | 2020-10-09 13:12:39 -0400 | [diff] [blame] | 648 | * |
| 649 | * Errors: |
| 650 | * - returns false if |decoder| is null. |
| 651 | */ |
| 652 | bool AImageDecoder_isAnimated(AImageDecoder* _Nonnull decoder) |
| 653 | __INTRODUCED_IN(31); |
| 654 | |
Leon Scroggins III | 3b3700e | 2020-12-29 11:52:12 -0500 | [diff] [blame] | 655 | #endif // __ANDROID_API__ >= 31 |
| 656 | |
Leon Scroggins III | 728a9ac | 2020-10-12 10:34:05 -0400 | [diff] [blame] | 657 | enum { |
| 658 | /* |
| 659 | * Reported by {@link AImageDecoder_getRepeatCount} if the |
| 660 | * animation should repeat forever. |
Leon Scroggins III | 3b3700e | 2020-12-29 11:52:12 -0500 | [diff] [blame] | 661 | * |
| 662 | * Introduced in API 31 |
Leon Scroggins III | 728a9ac | 2020-10-12 10:34:05 -0400 | [diff] [blame] | 663 | */ |
| 664 | ANDROID_IMAGE_DECODER_INFINITE = INT32_MAX, |
| 665 | }; |
| 666 | |
Leon Scroggins III | 3b3700e | 2020-12-29 11:52:12 -0500 | [diff] [blame] | 667 | #if __ANDROID_API__ >= 31 |
| 668 | |
Leon Scroggins III | 728a9ac | 2020-10-12 10:34:05 -0400 | [diff] [blame] | 669 | /** |
| 670 | * Report how many times the animation should repeat. |
| 671 | * |
| 672 | * Introduced in API 31. |
| 673 | * |
| 674 | * This does not include the first play through. e.g. a repeat |
| 675 | * count of 4 means that each frame is played 5 times. |
| 676 | * |
| 677 | * {@link ANDROID_IMAGE_DECODER_INFINITE} means to repeat forever. |
| 678 | * |
| 679 | * This may require seeking. |
| 680 | * |
| 681 | * For non-animated formats, this returns 0. It may return non-zero for |
| 682 | * an image with only one frame (i.e. {@link AImageDecoder_isAnimated} returns |
| 683 | * false) if the encoded image contains a repeat count. |
| 684 | * |
| 685 | * @return Number of times to repeat on success or a value |
| 686 | * indicating the reason for the failure. |
| 687 | * |
| 688 | * Errors: |
| 689 | * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The AImageDecoder |
| 690 | * is null. |
| 691 | */ |
| 692 | int32_t AImageDecoder_getRepeatCount(AImageDecoder* _Nonnull decoder); |
| 693 | __INTRODUCED_IN(31); |
| 694 | |
Leon Scroggins III | 5528700 | 2020-10-13 11:30:11 -0400 | [diff] [blame^] | 695 | /** |
| 696 | * Advance to the next frame in the animation. |
| 697 | * |
| 698 | * Introduced in API 31. |
| 699 | * |
| 700 | * The AImageDecoder keeps track internally which frame it is ready to decode |
| 701 | * (the "current frame"). Initially it is set to decode the first frame, and |
| 702 | * each call to {@link AImageDecoder_decodeImage} will continue to decode |
| 703 | * the same frame until this method (or {@link AImageDecoder_rewind}) |
| 704 | * is called. |
| 705 | * |
| 706 | * Note that this can be used to skip a frame without decoding it. But |
| 707 | * some frames depend on (i.e. blend with) prior frames, and |
| 708 | * AImageDecoder_decodeImage assumes that the prior frame is in the |
| 709 | * |pixels| buffer. In addition, AImageDecoder_decodeImage handles caching and |
| 710 | * restoring frames (see {@link ANDROID_IMAGE_DECODER_DISPOSE_OP_PREVIOUS}), so |
| 711 | * skipping frames in an image with such frames may not produce the correct |
| 712 | * results. |
| 713 | * |
| 714 | * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value |
| 715 | * indicating the reason for the failure. |
| 716 | * |
| 717 | * Errors: |
| 718 | * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The AImageDecoder |
| 719 | * represents an image that is not animated (see |
| 720 | * {@link AImageDecoder_isAnimated}) or the AImageDecoder is null. |
| 721 | * - {@link ANDROID_IMAGE_DECODER_INCOMPLETE}: The input appears |
| 722 | * to be truncated. The client must call {@link AImageDecoder_rewind} |
| 723 | * before calling {@link AImageDecoder_decodeImage} again. |
| 724 | * - {@link ANDROID_IMAGE_DECODER_ERROR}: The input contains an error. |
| 725 | * The client must call {@link AImageDecoder_rewind} before |
| 726 | * calling {@link AImageDecoder_decodeImage} again. |
| 727 | * - {@link ANDROID_IMAGE_DECODER_FINISHED}: The input contains no |
| 728 | * more frames. The client must call {@link AImageDecoder_rewind} |
| 729 | * before calling {@link AImageDecoder_decodeImage} again. |
| 730 | */ |
| 731 | int AImageDecoder_advanceFrame(AImageDecoder* _Nonnull decoder) |
| 732 | __INTRODUCED_IN(31); |
| 733 | |
| 734 | /** |
| 735 | * Return to the beginning of the animation. |
| 736 | * |
| 737 | * Introduced in API 31. |
| 738 | * |
| 739 | * After this call, the AImageDecoder will be ready to decode the |
| 740 | * first frame of the animation. This can be called after reaching |
| 741 | * the end of the animation or an error or in the middle of the |
| 742 | * animation. |
| 743 | * |
| 744 | * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value |
| 745 | * indicating the reason for the failure. |
| 746 | * |
| 747 | * Errors: |
| 748 | * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The AImageDecoder |
| 749 | * represents an image that is not animated (see |
| 750 | * {@link AImageDecoder_isAnimated}) or the AImageDecoder is |
| 751 | * null. |
| 752 | * - {@link ANDROID_IMAGE_DECODER_SEEK_ERROR}: The asset or file |
| 753 | * descriptor failed to seek. |
| 754 | */ |
| 755 | int AImageDecoder_rewind(AImageDecoder* _Nonnull decoder) |
| 756 | __INTRODUCED_IN(31); |
| 757 | |
Leon Scroggins III | 1b38971 | 2020-10-09 13:12:39 -0400 | [diff] [blame] | 758 | #endif // __ANDROID_API__ >= 31 |
| 759 | |
Leon Scroggins III | 2f98494 | 2019-11-22 17:02:23 -0500 | [diff] [blame] | 760 | #ifdef __cplusplus |
| 761 | } |
| 762 | #endif |
| 763 | |
| 764 | #endif // ANDROID_IMAGE_DECODER_H |
| 765 | |
| 766 | /** @} */ |