| 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 | * | 
|  | 136 | * @param format AndroidBitmapFormat to use | 
|  | 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 | /** | 
|  | 162 | * Specify the output size for a decoded image. | 
|  | 163 | * | 
|  | 164 | * Future calls to {@link AImageDecoder_decodeImage} will sample or scale the | 
|  | 165 | * encoded image to reach the desired size. If a crop rect is set (via | 
|  | 166 | * {@link AImageDecoder_setCrop}), it must be contained within the dimensions | 
|  | 167 | * specified by width and height, and the output image will be the size of the | 
|  | 168 | * crop rect. | 
|  | 169 | * | 
|  | 170 | * @param width Width of the output (prior to cropping). | 
|  | 171 | *              This will affect future calls to | 
|  | 172 | *              {@link AImageDecoder_getMinimumStride}, which will now return | 
|  | 173 | *              a value based on this width. | 
|  | 174 | * @param height Height of the output (prior to cropping). | 
|  | 175 | * @return - {@link ANDROID_IMAGE_DECODER_SUCCESS} on success | 
|  | 176 | *         - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER} if the AImageDecoder | 
|  | 177 | *           pointer is null, width or height is <= 0, or any existing crop is | 
|  | 178 | *           not contained by the image dimensions. | 
|  | 179 | */ | 
|  | 180 | int AImageDecoder_setTargetSize(AImageDecoder*, int width, int height) __INTRODUCED_IN(30); | 
|  | 181 |  | 
|  | 182 | /** | 
|  | 183 | * Specify how to crop the output after scaling (if any). | 
|  | 184 | * | 
|  | 185 | * Future calls to {@link AImageDecoder_decodeImage} will crop their output to | 
|  | 186 | * the specified {@link ARect}. Clients will only need to allocate enough memory | 
|  | 187 | * for the cropped ARect. | 
|  | 188 | * | 
|  | 189 | * @param crop Rectangle describing a crop of the decode. It must be contained inside of | 
|  | 190 | *             the (possibly scaled, by {@link AImageDecoder_setTargetSize}) | 
|  | 191 | *             image dimensions. This will affect future calls to | 
|  | 192 | *             {@link AImageDecoder_getMinimumStride}, which will now return a | 
|  | 193 | *             value based on the width of the crop. An empty ARect - | 
|  | 194 | *             specifically { 0, 0, 0, 0 } - may be used to remove the cropping | 
|  | 195 | *             behavior. Any other empty or unsorted ARects will result in | 
|  | 196 | *             returning ANDROID_IMAGE_DECODER_BAD_PARAMETER. | 
|  | 197 | * @return - {@link ANDROID_IMAGE_DECODER_SUCCESS} on success | 
|  | 198 | *         - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER} if the AImageDecoder | 
|  | 199 | *           pointer is null or the crop is not contained by the image | 
|  | 200 | *           dimensions. | 
|  | 201 | */ | 
|  | 202 | int AImageDecoder_setCrop(AImageDecoder*, ARect crop) __INTRODUCED_IN(30); | 
|  | 203 |  | 
|  | 204 | /** | 
|  | 205 | * Opaque handle for reading header info. | 
|  | 206 | */ | 
|  | 207 | struct AImageDecoderHeaderInfo; | 
|  | 208 | typedef struct AImageDecoderHeaderInfo AImageDecoderHeaderInfo; | 
|  | 209 |  | 
|  | 210 | /** | 
|  | 211 | * Return an opaque handle for reading header info. | 
|  | 212 | * | 
|  | 213 | * This is owned by the {@link AImageDecoder} and will be destroyed when the | 
|  | 214 | * AImageDecoder is destroyed via {@link AImageDecoder_delete}. | 
|  | 215 | */ | 
|  | 216 | const AImageDecoderHeaderInfo* AImageDecoder_getHeaderInfo( | 
|  | 217 | const AImageDecoder*) __INTRODUCED_IN(30); | 
|  | 218 |  | 
|  | 219 | /** | 
|  | 220 | * Report the native width of the encoded image. | 
|  | 221 | */ | 
|  | 222 | int32_t AImageDecoderHeaderInfo_getWidth(const AImageDecoderHeaderInfo*) __INTRODUCED_IN(30); | 
|  | 223 |  | 
|  | 224 | /** | 
|  | 225 | * Report the native height of the encoded image. | 
|  | 226 | */ | 
|  | 227 | int32_t AImageDecoderHeaderInfo_getHeight(const AImageDecoderHeaderInfo*) __INTRODUCED_IN(30); | 
|  | 228 |  | 
|  | 229 | /** | 
|  | 230 | * Report the mimeType of the encoded image. | 
|  | 231 | * | 
|  | 232 | * @return a string literal describing the mime type. | 
|  | 233 | */ | 
|  | 234 | const char* AImageDecoderHeaderInfo_getMimeType( | 
|  | 235 | const AImageDecoderHeaderInfo*) __INTRODUCED_IN(30); | 
|  | 236 |  | 
|  | 237 | /** | 
|  | 238 | * Report whether the encoded image represents an animation. | 
|  | 239 | */ | 
|  | 240 | bool AImageDecoderHeaderInfo_isAnimated( | 
|  | 241 | const AImageDecoderHeaderInfo*) __INTRODUCED_IN(30); | 
|  | 242 |  | 
|  | 243 | /** | 
|  | 244 | * Report the AndroidBitmapFormat the AImageDecoder will decode to | 
|  | 245 | * by default. AImageDecoder will try to choose one that is sensible | 
|  | 246 | * for the image and the system. Note that this does not indicate the | 
|  | 247 | * encoded format of the image. | 
|  | 248 | */ | 
|  | 249 | AndroidBitmapFormat AImageDecoderHeaderInfo_getAndroidBitmapFormat( | 
|  | 250 | const AImageDecoderHeaderInfo*) __INTRODUCED_IN(30); | 
|  | 251 |  | 
|  | 252 | /** | 
|  | 253 | * Report how the AImageDecoder will handle alpha by default. If the image | 
|  | 254 | * contains no alpha (according to its header), this will return | 
|  | 255 | * {@link ANDROID_BITMAP_FLAGS_ALPHA_OPAQUE}. If the image may contain alpha, | 
|  | 256 | * this returns {@link ANDROID_BITMAP_FLAGS_ALPHA_PREMUL}. | 
|  | 257 | * | 
|  | 258 | * For animated images only the opacity of the first frame is reported. | 
|  | 259 | */ | 
|  | 260 | int AImageDecoderHeaderInfo_getAlphaFlags( | 
|  | 261 | const AImageDecoderHeaderInfo*) __INTRODUCED_IN(30); | 
|  | 262 |  | 
|  | 263 | /** | 
|  | 264 | * Return the minimum stride that can be used, taking the specified | 
|  | 265 | * (or default) (possibly scaled) width, crop rect and | 
|  | 266 | * {@link AndroidBitmapFormat} into account. | 
|  | 267 | */ | 
|  | 268 | size_t AImageDecoder_getMinimumStride(AImageDecoder*) __INTRODUCED_IN(30); | 
|  | 269 |  | 
|  | 270 | /** | 
|  | 271 | * Decode the image into pixels, using the settings of the AImageDecoder. | 
|  | 272 | * | 
|  | 273 | * @param decoder Opaque object representing the decoder. | 
|  | 274 | * @param pixels On success, will be filled with the result | 
|  | 275 | *               of the decode. Must be large enough to fit |size| bytes. | 
|  | 276 | * @param stride Width in bytes of a single row. Must be at least | 
|  | 277 | *               {@link AImageDecoder_getMinimumStride}. | 
|  | 278 | * @param size Size of the pixel buffer in bytes. Must be at least | 
|  | 279 | *             stride * (height - 1) + | 
|  | 280 | *             {@link AImageDecoder_getMinimumStride}. | 
|  | 281 | * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success, or an error code | 
|  | 282 | *         from the same enum describing the failure. | 
|  | 283 | */ | 
|  | 284 | int AImageDecoder_decodeImage(AImageDecoder* decoder, | 
|  | 285 | void* pixels, size_t stride, | 
|  | 286 | size_t size) __INTRODUCED_IN(30); | 
|  | 287 |  | 
|  | 288 | #endif // __ANDROID_API__ >= 30 | 
|  | 289 |  | 
|  | 290 | #ifdef __cplusplus | 
|  | 291 | } | 
|  | 292 | #endif | 
|  | 293 |  | 
|  | 294 | #endif // ANDROID_IMAGE_DECODER_H | 
|  | 295 |  | 
|  | 296 | /** @} */ |