blob: fcc0b9b4470f7e5cc928d9e63bf4b17733d9cc6e [file] [log] [blame]
Leon Scroggins III2f984942019-11-22 17:02:23 -05001/*
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 III97fea5f2020-01-30 12:18:20 -050018 * @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 IIIbb5ffd22020-02-06 11:45:16 -050023 * 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 III97fea5f2020-01-30 12:18:20 -050035 * 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 III2f984942019-11-22 17:02:23 -050039 * @{
40 */
41
42/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050043 * @file imagedecoder.h
44 * @brief API for decoding images.
Leon Scroggins III2f984942019-11-22 17:02:23 -050045 */
46
47#ifndef ANDROID_IMAGE_DECODER_H
48#define ANDROID_IMAGE_DECODER_H
49
50#include "bitmap.h"
Leon Scroggins IIIa9f397b2020-01-27 12:42:56 -050051#include <android/rect.h>
Leon Scroggins III2f984942019-11-22 17:02:23 -050052#include <stdint.h>
53
Leon Scroggins III726a5502021-01-08 14:25:31 -050054#ifndef __ANDROID__
55 // Value copied from 'bionic/libc/include/android/api-level.h' which is not available on
56 // non Android systems. It is set to 10000 which is same as __ANDROID_API_FUTURE__ value.
57 #ifndef __ANDROID_API__
58 #define __ANDROID_API__ 10000
59 #endif
60
61 // Value copied from 'bionic/libc/include/android/versioning.h' which is not available on
62 // non Android systems
63 #ifndef __INTRODUCED_IN
64 #define __INTRODUCED_IN(api_level)
65 #endif
66#endif
67
Leon Scroggins III2f984942019-11-22 17:02:23 -050068#ifdef __cplusplus
69extern "C" {
70#endif
71
72struct AAsset;
Leon Scroggins III2f984942019-11-22 17:02:23 -050073
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050074/**
Leon Scroggins III3b3700e2020-12-29 11:52:12 -050075 * {@link AImageDecoder} functions result code.
76 *
77 * Introduced in API 30.
78 *
79 * Many functions will return this to indicate success
80 * ({@link ANDROID_IMAGE_DECODER_SUCCESS}) or the reason for the failure. On
81 * failure, any out-parameters should be considered uninitialized, except where
Leon Scroggins III9b9fb392020-12-03 16:55:36 -050082 * specified. Use {@link AImageDecoder_resultToString} for a readable
83 * version of the result code.
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050084 */
Leon Scroggins III2f984942019-11-22 17:02:23 -050085enum {
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050086 /**
87 * Decoding was successful and complete.
88 */
Leon Scroggins III2f984942019-11-22 17:02:23 -050089 ANDROID_IMAGE_DECODER_SUCCESS = 0,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050090 /**
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -050091 * The input is incomplete.
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050092 */
Leon Scroggins III2f984942019-11-22 17:02:23 -050093 ANDROID_IMAGE_DECODER_INCOMPLETE = -1,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050094 /**
95 * The input contained an error after decoding some lines.
96 */
Leon Scroggins III2f984942019-11-22 17:02:23 -050097 ANDROID_IMAGE_DECODER_ERROR = -2,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050098 /**
99 * Could not convert. For example, attempting to decode an image with
100 * alpha to an opaque format.
101 */
Leon Scroggins III2f984942019-11-22 17:02:23 -0500102 ANDROID_IMAGE_DECODER_INVALID_CONVERSION = -3,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500103 /**
104 * The scale is invalid. It may have overflowed, or it may be incompatible
105 * with the current alpha setting.
106 */
Leon Scroggins III2f984942019-11-22 17:02:23 -0500107 ANDROID_IMAGE_DECODER_INVALID_SCALE = -4,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500108 /**
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500109 * Some other parameter is invalid.
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500110 */
Leon Scroggins III2f984942019-11-22 17:02:23 -0500111 ANDROID_IMAGE_DECODER_BAD_PARAMETER = -5,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500112 /**
113 * Input was invalid before decoding any pixels.
114 */
Leon Scroggins III2f984942019-11-22 17:02:23 -0500115 ANDROID_IMAGE_DECODER_INVALID_INPUT = -6,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500116 /**
117 * A seek was required and it failed.
118 */
Leon Scroggins III2f984942019-11-22 17:02:23 -0500119 ANDROID_IMAGE_DECODER_SEEK_ERROR = -7,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500120 /**
121 * Some other error. For example, an internal allocation failed.
122 */
Leon Scroggins III2f984942019-11-22 17:02:23 -0500123 ANDROID_IMAGE_DECODER_INTERNAL_ERROR = -8,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500124 /**
125 * AImageDecoder did not recognize the format.
126 */
Leon Scroggins III55287002020-10-13 11:30:11 -0400127 ANDROID_IMAGE_DECODER_UNSUPPORTED_FORMAT = -9,
128 /**
129 * The animation has reached the end.
130 */
131 ANDROID_IMAGE_DECODER_FINISHED = -10,
132 /**
133 * This method cannot be called while the AImageDecoder is in its current
134 * state. For example, various setters (like {@link AImageDecoder_setTargetSize})
135 * can only be called while the AImageDecoder is set to decode the first
136 * frame of an animation. This ensures that any blending and/or restoring
137 * prior frames works correctly.
138 */
139 ANDROID_IMAGE_DECODER_INVALID_STATE = -11,
Leon Scroggins III2f984942019-11-22 17:02:23 -0500140};
141
Leon Scroggins III9b9fb392020-12-03 16:55:36 -0500142#if __ANDROID_API__ >= 31
143
144/**
145 * Return a constant string value representing the error code.
146 *
147 * Introduced in API 31.
148 *
149 * Pass the return value from an {@link AImageDecoder} method (e.g.
150 * {@link AImageDecoder_decodeImage}) for a text string representing the error
151 * code.
152 *
153 * Errors:
154 * - Returns null for a value out of range.
155 */
156const char* _Nullable AImageDecoder_resultToString(int)__INTRODUCED_IN(31);
157
158#endif // __ANDROID_API__ >= 31
159
Leon Scroggins III2f984942019-11-22 17:02:23 -0500160struct AImageDecoder;
161
162/**
163 * Opaque handle for decoding images.
164 *
Leon Scroggins III3b3700e2020-12-29 11:52:12 -0500165 * Introduced in API 30
166 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500167 * Create using one of the following:
168 * - {@link AImageDecoder_createFromAAsset}
169 * - {@link AImageDecoder_createFromFd}
170 * - {@link AImageDecoder_createFromBuffer}
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500171 *
172 * After creation, {@link AImageDecoder_getHeaderInfo} can be used to retrieve
173 * information about the encoded image. Other functions, like
174 * {@link AImageDecoder_setTargetSize}, can be used to specify how to decode, and
Leon Scroggins IIIac9f7492020-12-28 15:33:13 -0500175 * {@link AImageDecoder_decodeImage} will decode into client provided memory.
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500176 *
177 * {@link AImageDecoder} objects are NOT thread-safe, and should not be shared across
178 * threads.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500179 */
180typedef struct AImageDecoder AImageDecoder;
181
Leon Scroggins III3b3700e2020-12-29 11:52:12 -0500182#if __ANDROID_API__ >= 30
183
Leon Scroggins III2f984942019-11-22 17:02:23 -0500184/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500185 * Create a new {@link AImageDecoder} from an {@link AAsset}.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500186 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700187 * Available since API level 30.
188 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500189 * @param asset {@link AAsset} containing encoded image data. Client is still
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500190 * responsible for calling {@link AAsset_close} on it, which may be
191 * done after deleting the returned {@link AImageDecoder}.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500192 * @param outDecoder On success (i.e. return value is
193 * {@link ANDROID_IMAGE_DECODER_SUCCESS}), this will be set to
194 * a newly created {@link AImageDecoder}. Caller is
195 * responsible for calling {@link AImageDecoder_delete} on it.
196 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500197 * indicating the reason for the failure.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500198 *
199 * Errors:
200 * - {@link ANDROID_IMAGE_DECODER_INCOMPLETE}: The asset was truncated before
201 * reading the image header.
202 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: One of the parameters is
203 * null.
204 * - {@link ANDROID_IMAGE_DECODER_INVALID_INPUT}: There is an error in the
205 * header.
206 * - {@link ANDROID_IMAGE_DECODER_SEEK_ERROR}: The asset failed to seek.
207 * - {@link ANDROID_IMAGE_DECODER_INTERNAL_ERROR}: Some other error, like a
208 * failure to allocate memory.
209 * - {@link ANDROID_IMAGE_DECODER_UNSUPPORTED_FORMAT}: The format is not
210 * supported.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500211 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400212int AImageDecoder_createFromAAsset(struct AAsset* _Nonnull asset,
Leon Scroggins IIIb2ee00d2021-01-07 15:37:34 -0500213 AImageDecoder* _Nullable * _Nonnull outDecoder)
Leon Scroggins IIIa9f397b2020-01-27 12:42:56 -0500214 __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500215
216/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500217 * Create a new {@link AImageDecoder} from a file descriptor.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500218 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700219 * Available since API level 30.
220 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500221 * @param fd Seekable, readable, open file descriptor for encoded data.
222 * Client is still responsible for closing it, which may be done
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500223 * after deleting the returned {@link AImageDecoder}.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500224 * @param outDecoder On success (i.e. return value is
225 * {@link ANDROID_IMAGE_DECODER_SUCCESS}), this will be set to
226 * a newly created {@link AImageDecoder}. Caller is
227 * responsible for calling {@link AImageDecoder_delete} on it.
228 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500229 * indicating the reason for the failure.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500230 *
231 * Errors:
232 * - {@link ANDROID_IMAGE_DECODER_INCOMPLETE}: The file was truncated before
233 * reading the image header.
234 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The {@link AImageDecoder} is
235 * null, or |fd| does not represent a valid, seekable file descriptor.
236 * - {@link ANDROID_IMAGE_DECODER_INVALID_INPUT}: There is an error in the
237 * header.
238 * - {@link ANDROID_IMAGE_DECODER_SEEK_ERROR}: The descriptor failed to seek.
239 * - {@link ANDROID_IMAGE_DECODER_INTERNAL_ERROR}: Some other error, like a
240 * failure to allocate memory.
241 * - {@link ANDROID_IMAGE_DECODER_UNSUPPORTED_FORMAT}: The format is not
242 * supported.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500243 */
Leon Scroggins IIIb2ee00d2021-01-07 15:37:34 -0500244int AImageDecoder_createFromFd(int fd, AImageDecoder* _Nullable * _Nonnull outDecoder)
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400245 __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500246
247/**
248 * Create a new AImageDecoder from a buffer.
249 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700250 * Available since API level 30.
251 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500252 * @param buffer Pointer to encoded data. Must be valid for the entire time
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500253 * the {@link AImageDecoder} is used.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500254 * @param length Byte length of buffer.
255 * @param outDecoder On success (i.e. return value is
256 * {@link ANDROID_IMAGE_DECODER_SUCCESS}), this will be set to
257 * a newly created {@link AImageDecoder}. Caller is
258 * responsible for calling {@link AImageDecoder_delete} on it.
259 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500260 * indicating the reason for the failure.
261 *
262 * Errors:
263 * - {@link ANDROID_IMAGE_DECODER_INCOMPLETE}: The encoded image was truncated before
264 * reading the image header.
265 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: One of the parameters is
266 * invalid.
267 * - {@link ANDROID_IMAGE_DECODER_INVALID_INPUT}: There is an error in the
268 * header.
269 * - {@link ANDROID_IMAGE_DECODER_INTERNAL_ERROR}: Some other error, like a
270 * failure to allocate memory.
271 * - {@link ANDROID_IMAGE_DECODER_UNSUPPORTED_FORMAT}: The format is not
272 * supported.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500273 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400274int AImageDecoder_createFromBuffer(const void* _Nonnull buffer, size_t length,
Leon Scroggins IIIb2ee00d2021-01-07 15:37:34 -0500275 AImageDecoder* _Nullable * _Nonnull outDecoder)
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400276 __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500277
278/**
279 * Delete the AImageDecoder.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700280 *
281 * Available since API level 30.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500282 */
Leon Scroggins IIIb2ee00d2021-01-07 15:37:34 -0500283void AImageDecoder_delete(AImageDecoder* _Nullable decoder) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500284
285/**
286 * Choose the desired output format.
287 *
Leon Scroggins III55287002020-10-13 11:30:11 -0400288 * If the encoded image represents an animation, this must be called while on
289 * the first frame (e.g. before calling {@link AImageDecoder_advanceFrame} or
290 * after calling {@link AImageDecoder_rewind}).
291 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700292 * Available since API level 30.
293 *
Leon Scroggins III5d0445c2020-01-23 09:43:43 -0500294 * @param format {@link AndroidBitmapFormat} to use for the output.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500295 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
296 * indicating the reason for the failure. On failure, the
297 * {@link AImageDecoder} uses the format it was already planning
298 * to use (either its default or a previously successful setting
299 * from this function).
300 *
301 * Errors:
302 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The
303 * {@link AImageDecoder} is null or |format| does not correspond to an
304 * {@link AndroidBitmapFormat}.
305 * - {@link ANDROID_IMAGE_DECODER_INVALID_CONVERSION}: The
306 * {@link AndroidBitmapFormat} is incompatible with the image.
Leon Scroggins III55287002020-10-13 11:30:11 -0400307 * - {@link ANDROID_IMAGE_DECODER_INVALID_STATE}: The animation is not on
308 * the first frame.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500309 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400310int AImageDecoder_setAndroidBitmapFormat(AImageDecoder* _Nonnull decoder,
Leon Scroggins III2f984942019-11-22 17:02:23 -0500311 int32_t format) __INTRODUCED_IN(30);
312
Leon Scroggins III1be112f2020-01-15 04:26:44 -0500313/**
314 * Specify whether the output's pixels should be unpremultiplied.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500315 *
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500316 * By default, {@link AImageDecoder_decodeImage} will premultiply the pixels, if they have alpha.
317 * Pass true to this method to leave them unpremultiplied. This has no effect on an
Leon Scroggins III1be112f2020-01-15 04:26:44 -0500318 * opaque image.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500319 *
Leon Scroggins III55287002020-10-13 11:30:11 -0400320 * If the encoded image represents an animation, this must be called while on
321 * the first frame (e.g. before calling {@link AImageDecoder_advanceFrame} or
322 * after calling {@link AImageDecoder_rewind}).
323 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700324 * Available since API level 30.
325 *
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500326 * @param unpremultipliedRequired Pass true to leave the pixels unpremultiplied.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500327 * @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_INVALID_CONVERSION}: Unpremultiplied is not
332 * possible due to an existing scale set by
333 * {@link AImageDecoder_setTargetSize}.
334 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The
335 * {@link AImageDecoder} is null.
Leon Scroggins III55287002020-10-13 11:30:11 -0400336 * - {@link ANDROID_IMAGE_DECODER_INVALID_STATE}: The animation is not on
337 * the first frame.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500338 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400339int AImageDecoder_setUnpremultipliedRequired(AImageDecoder* _Nonnull decoder,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500340 bool unpremultipliedRequired) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500341
342/**
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500343 * Choose the dataspace for the output.
344 *
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500345 * Ignored by {@link ANDROID_BITMAP_FORMAT_A_8}, which does not support
346 * an {@link ADataSpace}.
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500347 *
Leon Scroggins III55287002020-10-13 11:30:11 -0400348 * If the encoded image represents an animation, this must be called while on
349 * the first frame (e.g. before calling {@link AImageDecoder_advanceFrame} or
350 * after calling {@link AImageDecoder_rewind}).
351 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700352 * Available since API level 30.
353 *
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500354 * @param dataspace The {@link ADataSpace} to decode into. An ADataSpace
355 * specifies how to interpret the colors. By default,
356 * AImageDecoder will decode into the ADataSpace specified by
357 * {@link AImageDecoderHeaderInfo_getDataSpace}. If this
358 * parameter is set to a different ADataSpace, AImageDecoder
359 * will transform the output into the specified ADataSpace.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500360 * @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 or |dataspace| does not correspond to an
366 * {@link ADataSpace} value.
Leon Scroggins III55287002020-10-13 11:30:11 -0400367 * - {@link ANDROID_IMAGE_DECODER_INVALID_STATE}: The animation is not on
368 * the first frame.
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500369 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400370int AImageDecoder_setDataSpace(AImageDecoder* _Nonnull decoder, int32_t dataspace)
371 __INTRODUCED_IN(30);
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500372
373/**
Leon Scroggins III2f984942019-11-22 17:02:23 -0500374 * Specify the output size for a decoded image.
375 *
376 * Future calls to {@link AImageDecoder_decodeImage} will sample or scale the
377 * encoded image to reach the desired size. If a crop rect is set (via
378 * {@link AImageDecoder_setCrop}), it must be contained within the dimensions
379 * specified by width and height, and the output image will be the size of the
380 * crop rect.
381 *
Leon Scroggins III55287002020-10-13 11:30:11 -0400382 * If the encoded image represents an animation, this must be called while on
383 * the first frame (e.g. before calling {@link AImageDecoder_advanceFrame} or
384 * after calling {@link AImageDecoder_rewind}).
385 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700386 * Available since API level 30.
387 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500388 * @param width Width of the output (prior to cropping).
389 * This will affect future calls to
390 * {@link AImageDecoder_getMinimumStride}, which will now return
391 * a value based on this width.
392 * @param height Height of the output (prior to cropping).
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500393 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
394 * indicating the reason for the failure.
395 *
396 * Errors:
397 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The
398 * {@link AImageDecoder} is null.
399 * - {@link ANDROID_IMAGE_DECODER_INVALID_SCALE}: |width| or |height| is <= 0,
Leon Scroggins III55287002020-10-13 11:30:11 -0400400 * the size is too big, any existing crop is not contained by the new image
401 * dimensions, or the scale is incompatible with a previous call to
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500402 * {@link AImageDecoder_setUnpremultipliedRequired}(true).
Leon Scroggins III55287002020-10-13 11:30:11 -0400403 * - {@link ANDROID_IMAGE_DECODER_INVALID_STATE}: The animation is not on
404 * the first frame.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500405 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400406int AImageDecoder_setTargetSize(AImageDecoder* _Nonnull decoder, int32_t width,
407 int32_t height) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500408
Leon Scroggins IIIf27256b2020-01-19 21:13:04 -0500409/**
410 * Compute the dimensions to use for a given sampleSize.
411 *
412 * Although AImageDecoder can scale to an arbitrary target size (see
413 * {@link AImageDecoder_setTargetSize}), some sizes may be more efficient than
414 * others. This computes the most efficient target size to use to reach a
415 * particular sampleSize.
416 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700417 * Available since API level 30.
418 *
Leon Scroggins IIIf27256b2020-01-19 21:13:04 -0500419 * @param sampleSize A subsampling rate of the original image. Must be greater
420 * than or equal to 1. A sampleSize of 2 means to skip every
421 * other pixel/line, resulting in a width and height that are
422 * 1/2 of the original dimensions, with 1/4 the number of
423 * pixels.
424 * @param width Out parameter for the width sampled by sampleSize, and rounded
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500425 * in the direction that the decoder can do most efficiently.
Leon Scroggins IIIf27256b2020-01-19 21:13:04 -0500426 * @param height Out parameter for the height sampled by sampleSize, and rounded
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500427 * in the direction that the decoder can do most efficiently.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500428 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
429 * indicating the reason for the failure.
430 *
431 * Errors:
432 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The
433 * {@link AImageDecoder}, |width| or |height| is null or |sampleSize| is < 1.
Leon Scroggins IIIf27256b2020-01-19 21:13:04 -0500434 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400435int AImageDecoder_computeSampledSize(const AImageDecoder* _Nonnull decoder, int sampleSize,
436 int32_t* _Nonnull width, int32_t* _Nonnull height)
437 __INTRODUCED_IN(30);
Leon Scroggins III3b3700e2020-12-29 11:52:12 -0500438
Leon Scroggins III2f984942019-11-22 17:02:23 -0500439/**
440 * Specify how to crop the output after scaling (if any).
441 *
442 * Future calls to {@link AImageDecoder_decodeImage} will crop their output to
443 * the specified {@link ARect}. Clients will only need to allocate enough memory
444 * for the cropped ARect.
445 *
Leon Scroggins III55287002020-10-13 11:30:11 -0400446 * If the encoded image represents an animation, this must be called while on
447 * the first frame (e.g. before calling {@link AImageDecoder_advanceFrame} or
448 * after calling {@link AImageDecoder_rewind}).
449 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700450 * Available since API level 30.
451 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500452 * @param crop Rectangle describing a crop of the decode. It must be contained inside of
453 * the (possibly scaled, by {@link AImageDecoder_setTargetSize})
454 * image dimensions. This will affect future calls to
455 * {@link AImageDecoder_getMinimumStride}, which will now return a
456 * value based on the width of the crop. An empty ARect -
457 * specifically { 0, 0, 0, 0 } - may be used to remove the cropping
458 * behavior. Any other empty or unsorted ARects will result in
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500459 * returning {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500460 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
461 * indicating the reason for the failure.
462 *
463 * Errors:
464 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The
Leon Scroggins III55287002020-10-13 11:30:11 -0400465 * {@link AImageDecoder} is null, or the crop is not contained by the
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500466 * (possibly scaled) image dimensions.
Leon Scroggins III55287002020-10-13 11:30:11 -0400467 * - {@link ANDROID_IMAGE_DECODER_INVALID_STATE}: The animation is not on
468 * the first frame.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500469 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400470int AImageDecoder_setCrop(AImageDecoder* _Nonnull decoder, ARect crop) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500471
Leon Scroggins III3b3700e2020-12-29 11:52:12 -0500472#endif // __ANDROID_API__ >= 30
473
Leon Scroggins III2f984942019-11-22 17:02:23 -0500474struct AImageDecoderHeaderInfo;
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500475/**
Leon Scroggins III3b3700e2020-12-29 11:52:12 -0500476 * Opaque handle for representing information about the encoded image.
477 *
478 * Introduced in API 30
479 *
480 * Retrieved using {@link AImageDecoder_getHeaderInfo} and passed to methods
481 * like {@link AImageDecoderHeaderInfo_getWidth} and
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500482 * {@link AImageDecoderHeaderInfo_getHeight}.
483 */
Leon Scroggins III2f984942019-11-22 17:02:23 -0500484typedef struct AImageDecoderHeaderInfo AImageDecoderHeaderInfo;
485
Leon Scroggins III3b3700e2020-12-29 11:52:12 -0500486#if __ANDROID_API__ >= 30
487
Leon Scroggins III2f984942019-11-22 17:02:23 -0500488/**
489 * Return an opaque handle for reading header info.
490 *
491 * This is owned by the {@link AImageDecoder} and will be destroyed when the
492 * AImageDecoder is destroyed via {@link AImageDecoder_delete}.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700493 *
494 * Available since API level 30.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500495 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400496const AImageDecoderHeaderInfo* _Nonnull AImageDecoder_getHeaderInfo(
497 const AImageDecoder* _Nonnull decoder) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500498
499/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500500 * Report the native width of the encoded image. This is also the logical
501 * pixel width of the output, unless {@link AImageDecoder_setTargetSize} is
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500502 * used to choose a different size or {@link AImageDecoder_setCrop} is used to
503 * set a crop rect.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700504 *
505 * Available since API level 30.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500506 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400507int32_t AImageDecoderHeaderInfo_getWidth(const AImageDecoderHeaderInfo* _Nonnull)
508 __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500509
510/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500511 * Report the native height of the encoded image. This is also the logical
512 * pixel height of the output, unless {@link AImageDecoder_setTargetSize} is
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500513 * used to choose a different size or {@link AImageDecoder_setCrop} is used to
514 * set a crop rect.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700515 *
516 * Available since API level 30.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500517 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400518int32_t AImageDecoderHeaderInfo_getHeight(const AImageDecoderHeaderInfo* _Nonnull)
519 __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500520
521/**
522 * Report the mimeType of the encoded image.
523 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700524 * Available since API level 30.
525 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500526 * @return a string literal describing the mime type.
527 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400528const char* _Nonnull AImageDecoderHeaderInfo_getMimeType(
529 const AImageDecoderHeaderInfo* _Nonnull) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500530
531/**
Leon Scroggins III5d0445c2020-01-23 09:43:43 -0500532 * Report the {@link AndroidBitmapFormat} the AImageDecoder will decode to
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500533 * by default. {@link AImageDecoder} will try to choose one that is sensible
Leon Scroggins III2f984942019-11-22 17:02:23 -0500534 * for the image and the system. Note that this does not indicate the
535 * encoded format of the image.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700536 *
537 * Available since API level 30.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500538 */
Leon Scroggins III5d0445c2020-01-23 09:43:43 -0500539int32_t AImageDecoderHeaderInfo_getAndroidBitmapFormat(
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400540 const AImageDecoderHeaderInfo* _Nonnull) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500541
542/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500543 * Report how the {@link AImageDecoder} will handle alpha by default. If the image
Leon Scroggins III2f984942019-11-22 17:02:23 -0500544 * contains no alpha (according to its header), this will return
545 * {@link ANDROID_BITMAP_FLAGS_ALPHA_OPAQUE}. If the image may contain alpha,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500546 * this returns {@link ANDROID_BITMAP_FLAGS_ALPHA_PREMUL}, because
547 * {@link AImageDecoder_decodeImage} will premultiply pixels by default.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700548 *
549 * Available since API level 30.
Leon Scroggins IIIac9f7492020-12-28 15:33:13 -0500550 *
551 * Starting in API level 31, an AImageDecoder may contain multiple frames of an
552 * animation, but this method still only reports whether the first frame has
553 * alpha.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500554 */
555int AImageDecoderHeaderInfo_getAlphaFlags(
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400556 const AImageDecoderHeaderInfo* _Nonnull) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500557
558/**
Leon Scroggins III8cee65e2020-01-30 14:20:42 -0500559 * Report the dataspace the AImageDecoder will decode to by default.
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500560 *
Leon Scroggins III8cee65e2020-01-30 14:20:42 -0500561 * By default, {@link AImageDecoder_decodeImage} will not do any color
562 * conversion.
563 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700564 * Available since API level 30.
565 *
Leon Scroggins III8cee65e2020-01-30 14:20:42 -0500566 * @return The {@link ADataSpace} representing the way the colors
567 * are encoded (or {@link ADATASPACE_UNKNOWN} if there is not a
568 * corresponding ADataSpace). This specifies how to interpret the colors
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500569 * in the decoded image, unless {@link AImageDecoder_setDataSpace} is
570 * called to decode to a different ADataSpace.
571 *
572 * Note that ADataSpace only exposes a few values. This may return
Leon Scroggins III8cee65e2020-01-30 14:20:42 -0500573 * {@link ADATASPACE_UNKNOWN}, even for Named ColorSpaces, if they have
574 * no corresponding {@link ADataSpace}.
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500575 */
576int32_t AImageDecoderHeaderInfo_getDataSpace(
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400577 const AImageDecoderHeaderInfo* _Nonnull) __INTRODUCED_IN(30);
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500578
579/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500580 * Return the minimum stride that can be used in
581 * {@link AImageDecoder_decodeImage).
582 *
583 * This stride provides no padding, meaning it will be exactly equal to the
584 * width times the number of bytes per pixel for the {@link AndroidBitmapFormat}
585 * being used.
586 *
587 * If the output is scaled (via {@link AImageDecoder_setTargetSize}) and/or
588 * cropped (via {@link AImageDecoder_setCrop}), this takes those into account.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700589 *
590 * Available since API level 30.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500591 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400592size_t AImageDecoder_getMinimumStride(AImageDecoder* _Nonnull decoder) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500593
594/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500595 * Decode the image into pixels, using the settings of the {@link AImageDecoder}.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500596 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700597 * Available since API level 30.
598 *
Leon Scroggins III1b389712020-10-09 13:12:39 -0400599 * Starting in API level 31, it can be used to decode all of the frames of an
Leon Scroggins IIIec2cbe32021-01-12 12:59:21 -0500600 * animated image (i.e. GIF, WebP) using new APIs. Internally,
Leon Scroggins III55287002020-10-13 11:30:11 -0400601 * AImageDecoder keeps track of its "current frame" - that is, the frame that
602 * will be decoded by a call to AImageDecoder_decodeImage. At creation time, the
603 * current frame is always the first frame, and multiple calls to this method
604 * will each decode the first frame. {@link AImageDecoder_advanceFrame} advances
605 * the current frame to the following frame, so that future calls to this method
606 * will decode that frame. Some frames may update only part of the image. They
607 * may only update a sub-rectangle (see {@link
608 * AImageDecoderFrameInfo_getFrameRect}), or they may have alpha (see
609 * {@link AImageDecoderFrameInfo_hasAlphaWithinBounds}). In these cases, this
610 * method assumes that the prior frame is still residing in the |pixels| buffer,
611 * decodes only the new portion, and blends it with the buffer. Frames that change
612 * the entire |pixels| buffer are "independent", and do not require the prior
613 * frame to remain in the buffer. The first frame is always independent. A
614 * sophisticated client can use information from the {@link AImageDecoderFrameInfo}
615 * to determine whether other frames are independent, or what frames they rely on.
616 *
617 * If the current frame is marked {@link ANDROID_IMAGE_DECODER_DISPOSE_OP_PREVIOUS},
Leon Scroggins III78859b42021-01-13 11:48:34 -0500618 * AImageDecoder_decodeImage will store the |pixels| buffer prior to decoding
Leon Scroggins III55287002020-10-13 11:30:11 -0400619 * (note: this only happens for the first in a string of consecutive
620 * ANDROID_IMAGE_DECODER_DISPOSE_OP_PREVIOUS frames). After advancing to the
Leon Scroggins III78859b42021-01-13 11:48:34 -0500621 * following frame, AImageDecoder_decodeImage will restore that buffer prior to
622 * decoding that frame. This is the default behavior, but it can be disabled
623 * by passing false to {@link AImageDecoder_setInternallyHandleDisposePrevious}.
Leon Scroggins III55287002020-10-13 11:30:11 -0400624 *
625 * Ignoring timing information, display, etc, a client wishing to decode all
626 * frames of an animated image may conceptually use code like the following:
627 *
628 * while (true) {
629 * int result = AImageDecoder_decodeImage(decoder, pixels, stride, size);
630 * if (result != ANDROID_IMAGE_DECODER_SUCCESS) break;
631 *
632 * // Display or save the image in |pixels|, keeping the buffer intact for
633 * // AImageDecoder to decode the next frame correctly.
634 * Application_viewImage(pixels);
635 *
636 * result = AImageDecoder_advanceFrame(decoder);
637 * if (result != ANDROID_IMAGE_DECODER_SUCCESS) break;
638 * }
Leon Scroggins III1b389712020-10-09 13:12:39 -0400639 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500640 * @param decoder Opaque object representing the decoder.
641 * @param pixels On success, will be filled with the result
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500642 * of the decode. Must be large enough to hold |size| bytes.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500643 * @param stride Width in bytes of a single row. Must be at least
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500644 * {@link AImageDecoder_getMinimumStride} and a multiple of the
645 * bytes per pixel of the {@link AndroidBitmapFormat}.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500646 * @param size Size of the pixel buffer in bytes. Must be at least
647 * stride * (height - 1) +
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500648 * {@link AImageDecoder_getMinimumStride}.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500649 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
650 * indicating the reason for the failure.
651 *
652 * Errors:
653 * - {@link ANDROID_IMAGE_DECODER_INCOMPLETE}: The image was truncated. A
654 * partial image was decoded, and undecoded lines have been initialized to all
655 * zeroes.
656 * - {@link ANDROID_IMAGE_DECODER_ERROR}: The image contained an error. A
657 * partial image was decoded, and undecoded lines have been initialized to all
658 * zeroes.
659 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The {@link AImageDecoder} or
660 * |pixels| is null, the stride is not large enough or not pixel aligned, or
661 * |size| is not large enough.
662 * - {@link ANDROID_IMAGE_DECODER_SEEK_ERROR}: The asset or file descriptor
663 * failed to seek.
664 * - {@link ANDROID_IMAGE_DECODER_INTERNAL_ERROR}: Some other error, like a
665 * failure to allocate memory.
Leon Scroggins III55287002020-10-13 11:30:11 -0400666 * - {@link ANDROID_IMAGE_DECODER_FINISHED}: The input contains no
667 * more frames. No decoding occurred. The client must call
668 * {@link AImageDecoder_rewind} before calling
669 * {@link AImageDecoder_decodeImage} again.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500670 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400671int AImageDecoder_decodeImage(AImageDecoder* _Nonnull decoder,
672 void* _Nonnull pixels, size_t stride,
Leon Scroggins III2f984942019-11-22 17:02:23 -0500673 size_t size) __INTRODUCED_IN(30);
674
675#endif // __ANDROID_API__ >= 30
676
Leon Scroggins III1b389712020-10-09 13:12:39 -0400677#if __ANDROID_API__ >= 31
678
679/**
680 * Return true iff the image is animated - i.e. has multiple frames.
681 *
682 * Introduced in API 31.
683 *
Leon Scroggins III55287002020-10-13 11:30:11 -0400684 * A single frame GIF is considered to *not* be animated. This may require
685 * seeking past the first frame to verify whether there is a following frame.
Leon Scroggins III1b389712020-10-09 13:12:39 -0400686 *
687 * Errors:
688 * - returns false if |decoder| is null.
689 */
690bool AImageDecoder_isAnimated(AImageDecoder* _Nonnull decoder)
691 __INTRODUCED_IN(31);
692
Leon Scroggins III3b3700e2020-12-29 11:52:12 -0500693#endif // __ANDROID_API__ >= 31
694
Leon Scroggins III728a9ac2020-10-12 10:34:05 -0400695enum {
696 /*
697 * Reported by {@link AImageDecoder_getRepeatCount} if the
698 * animation should repeat forever.
Leon Scroggins III3b3700e2020-12-29 11:52:12 -0500699 *
700 * Introduced in API 31
Leon Scroggins III728a9ac2020-10-12 10:34:05 -0400701 */
702 ANDROID_IMAGE_DECODER_INFINITE = INT32_MAX,
703};
704
Leon Scroggins III3b3700e2020-12-29 11:52:12 -0500705#if __ANDROID_API__ >= 31
706
Leon Scroggins III728a9ac2020-10-12 10:34:05 -0400707/**
708 * Report how many times the animation should repeat.
709 *
710 * Introduced in API 31.
711 *
712 * This does not include the first play through. e.g. a repeat
713 * count of 4 means that each frame is played 5 times.
714 *
715 * {@link ANDROID_IMAGE_DECODER_INFINITE} means to repeat forever.
716 *
717 * This may require seeking.
718 *
719 * For non-animated formats, this returns 0. It may return non-zero for
720 * an image with only one frame (i.e. {@link AImageDecoder_isAnimated} returns
721 * false) if the encoded image contains a repeat count.
722 *
723 * @return Number of times to repeat on success or a value
724 * indicating the reason for the failure.
725 *
726 * Errors:
727 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The AImageDecoder
728 * is null.
729 */
730int32_t AImageDecoder_getRepeatCount(AImageDecoder* _Nonnull decoder);
731 __INTRODUCED_IN(31);
732
Leon Scroggins III55287002020-10-13 11:30:11 -0400733/**
734 * Advance to the next frame in the animation.
735 *
736 * Introduced in API 31.
737 *
738 * The AImageDecoder keeps track internally which frame it is ready to decode
739 * (the "current frame"). Initially it is set to decode the first frame, and
740 * each call to {@link AImageDecoder_decodeImage} will continue to decode
741 * the same frame until this method (or {@link AImageDecoder_rewind})
742 * is called.
743 *
744 * Note that this can be used to skip a frame without decoding it. But
745 * some frames depend on (i.e. blend with) prior frames, and
746 * AImageDecoder_decodeImage assumes that the prior frame is in the
747 * |pixels| buffer. In addition, AImageDecoder_decodeImage handles caching and
748 * restoring frames (see {@link ANDROID_IMAGE_DECODER_DISPOSE_OP_PREVIOUS}), so
749 * skipping frames in an image with such frames may not produce the correct
750 * results.
751 *
752 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
753 * indicating the reason for the failure.
754 *
755 * Errors:
756 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The AImageDecoder
757 * represents an image that is not animated (see
758 * {@link AImageDecoder_isAnimated}) or the AImageDecoder is null.
759 * - {@link ANDROID_IMAGE_DECODER_INCOMPLETE}: The input appears
760 * to be truncated. The client must call {@link AImageDecoder_rewind}
761 * before calling {@link AImageDecoder_decodeImage} again.
762 * - {@link ANDROID_IMAGE_DECODER_ERROR}: The input contains an error.
763 * The client must call {@link AImageDecoder_rewind} before
764 * calling {@link AImageDecoder_decodeImage} again.
765 * - {@link ANDROID_IMAGE_DECODER_FINISHED}: The input contains no
766 * more frames. The client must call {@link AImageDecoder_rewind}
767 * before calling {@link AImageDecoder_decodeImage} again.
768 */
769int AImageDecoder_advanceFrame(AImageDecoder* _Nonnull decoder)
770 __INTRODUCED_IN(31);
771
772/**
773 * Return to the beginning of the animation.
774 *
775 * Introduced in API 31.
776 *
777 * After this call, the AImageDecoder will be ready to decode the
778 * first frame of the animation. This can be called after reaching
779 * the end of the animation or an error or in the middle of the
780 * animation.
781 *
782 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
783 * indicating the reason for the failure.
784 *
785 * Errors:
786 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The AImageDecoder
787 * represents an image that is not animated (see
788 * {@link AImageDecoder_isAnimated}) or the AImageDecoder is
789 * null.
790 * - {@link ANDROID_IMAGE_DECODER_SEEK_ERROR}: The asset or file
791 * descriptor failed to seek.
792 */
793int AImageDecoder_rewind(AImageDecoder* _Nonnull decoder)
794 __INTRODUCED_IN(31);
795
Leon Scroggins III1b389712020-10-09 13:12:39 -0400796#endif // __ANDROID_API__ >= 31
797
Leon Scroggins IIIac9f7492020-12-28 15:33:13 -0500798struct AImageDecoderFrameInfo;
799
800/**
801 * Opaque handle to animation information about a single frame.
802 *
803 * Introduced in API 31
804 *
805 * The duration (retrieved with {@link AImageDecoderFrameInfo_getDuration}) is
806 * necessary for clients to display the animation at the proper speed. The other
807 * information is helpful for a client that wants to determine what frames are
808 * independent (or what frames they depend on), but is unnecessary for
809 * a simple client that wants to sequentially display all frames.
810 */
811typedef struct AImageDecoderFrameInfo AImageDecoderFrameInfo;
812
813#if __ANDROID_API__ >= 31
814
815/**
816 * Create an uninitialized AImageDecoderFrameInfo.
817 *
818 * Introduced in API 31.
819 *
820 * This can be passed to {@link AImageDecoder_getFrameInfo} to fill
821 * in information about the current frame. It may be reused.
822 *
823 * Must be deleted with {@link AImageDecoderFrameInfo_delete}.
824 */
825AImageDecoderFrameInfo* _Nullable AImageDecoderFrameInfo_create()
826 __INTRODUCED_IN(31);
827
828/**
829 * Delete an AImageDecoderFrameInfo.
830 *
831 * Introduced in API 31.
832 */
833void AImageDecoderFrameInfo_delete(
834 AImageDecoderFrameInfo* _Nullable info) __INTRODUCED_IN(31);
835
836/**
837 * Fill |info| with information about the current frame.
838 *
839 * Introduced in API 31.
840 *
841 * Initially, this will return information about the first frame.
842 * {@link AImageDecoder_advanceFrame} and
843 * {@link AImageDecoder_rewind} can be used to change which frame
844 * is the current frame.
845 *
846 * If the image only has one frame, this will fill the {@link
847 * AImageDecoderFrameInfo} with the encoded info, if any, or reasonable
848 * defaults.
849 *
850 * @param decoder Opaque object representing the decoder.
851 * @param info Opaque object to hold frame information. On success, will be
852 * filled with information regarding the current frame.
853 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
854 * indicating the reason for the failure.
855 *
856 * Errors:
857 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: One of the parameters is null.
858 * - {@link ANDROID_IMAGE_DECODER_FINISHED}: The input contains no
859 * more frames. The client must call {@link AImageDecoder_rewind} to reset the
860 * current frame to a valid frame (0).
861 */
862int AImageDecoder_getFrameInfo(AImageDecoder* _Nonnull decoder,
863 AImageDecoderFrameInfo* _Nonnull info) __INTRODUCED_IN(31);
864
865/**
866 * Report the number of nanoseconds to show the current frame.
867 *
868 * Introduced in API 31.
869 *
870 * Errors:
871 * - returns 0 if |info| is null.
872 */
873int64_t AImageDecoderFrameInfo_getDuration(
874 const AImageDecoderFrameInfo* _Nonnull info) __INTRODUCED_IN(31);
875
876/**
877 * The rectangle of the image (within 0, 0,
878 * {@link AImageDecoder_getWidth}, {@link AImageDecoder_getHeight})
879 * updated by this frame.
880 *
881 * Introduced in API 31.
882 *
883 * Note that this is unaffected by calls to
884 * {@link AImageDecoder_setTargetSize} or
885 * {@link AImageDecoder_setCrop}.
886 *
887 * A frame may update only part of the image. This will always be
888 * contained by the image’s dimensions.
889 *
890 * This, along with other information in AImageDecoderFrameInfo,
891 * can be useful for determining whether a frame is independent, but
892 * the decoder handles blending frames, so a simple
893 * sequential client does not need this.
894 *
895 * Errors:
896 * - returns an empty ARect if |info| is null.
897 */
898ARect AImageDecoderFrameInfo_getFrameRect(
899 const AImageDecoderFrameInfo* _Nonnull info) __INTRODUCED_IN(31);
900
901/**
902 * Whether the new portion of this frame may contain alpha.
903 *
904 * Introduced in API 31.
905 *
906 * Note that this may differ from whether the composed frame has
907 * alpha. If this frame does not fill the entire image dimensions
908 * (see {@link AImageDecoderFrameInfo_getFrameRect}) or it blends
909 * with an opaque frame, for example, the composed frame’s alpha
910 * may not match. It is also conservative; for example, if a color
911 * index-based frame has a color with alpha but does not use it,
912 * this will still return true.
913 *
914 * This, along with other information in AImageDecoderFrameInfo,
915 * can be useful for determining whether a frame is independent, but
916 * the decoder handles blending frames, so a simple
917 * sequential client does not need this.
918 *
919 * Errors:
920 * - returns false if |info| is null.
921 */
922bool AImageDecoderFrameInfo_hasAlphaWithinBounds(
923 const AImageDecoderFrameInfo* _Nonnull info) __INTRODUCED_IN(31);
924
925#endif // __ANDROID_API__ >= 31
926
927/**
928 * How a frame is “disposed” before showing the next one.
929 *
930 * Introduced in API 31.
931 *
932 * This, along with other information in AImageDecoderFrameInfo,
933 * can be useful for determining whether a frame is independent, but
934 * the decoder handles disposing of frames, so a simple
935 * sequential client does not need this.
936 */
937enum {
938 // No disposal. The following frame will be drawn directly
939 // on top of this one.
940 ANDROID_IMAGE_DECODER_DISPOSE_OP_NONE = 1,
Leon Scroggins III78859b42021-01-13 11:48:34 -0500941 // The frame’s rectangle is cleared to transparent (by AImageDecoder)
942 // before decoding the next frame.
Leon Scroggins IIIac9f7492020-12-28 15:33:13 -0500943 ANDROID_IMAGE_DECODER_DISPOSE_OP_BACKGROUND = 2,
Leon Scroggins III78859b42021-01-13 11:48:34 -0500944 // The frame’s rectangle is reverted to the prior frame before decoding
945 // the next frame. This is handled by AImageDecoder, unless
946 // {@link AImageDecoder_setInternallyHandleDisposePrevious} is set to false.
Leon Scroggins IIIac9f7492020-12-28 15:33:13 -0500947 ANDROID_IMAGE_DECODER_DISPOSE_OP_PREVIOUS = 3,
948};
949
950#if __ANDROID_API__ >= 31
951
952/**
953 * Return how this frame is “disposed” before showing the next one.
954 *
955 * Introduced in API 31.
956 *
957 * This, along with other information in AImageDecoderFrameInfo,
958 * can be useful for determining whether a frame is independent, but
959 * the decoder handles disposing of frames, so a simple
960 * sequential client does not need this.
961 *
962 * @return one of:
963 * - {@link ANDROID_IMAGE_DECODER_DISPOSE_OP_NONE}
964 * - {@link ANDROID_IMAGE_DECODER_DISPOSE_OP_BACKGROUND}
965 * - {@link ANDROID_IMAGE_DECODER_DISPOSE_OP_PREVIOUS}
966 * Errors:
967 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER} if |info| is null.
968 */
969int32_t AImageDecoderFrameInfo_getDisposeOp(
970 const AImageDecoderFrameInfo* _Nonnull info) __INTRODUCED_IN(31);
971
972#endif // __ANDROID_API__ >= 31
973
974/**
975 * How a frame is blended with the previous frame.
976 *
977 * Introduced in API 31.
978 *
979 * This, along with other information in AImageDecoderFrameInfo,
980 * can be useful for determining whether a frame is independent, but
981 * the decoder handles blending frames, so a simple
982 * sequential client does not need this.
983 */
984enum {
985 // This frame replaces existing content. This corresponds
986 // to webp’s “do not blend”.
987 ANDROID_IMAGE_DECODER_BLEND_OP_SRC = 1,
988 // This frame blends with the previous frame.
989 ANDROID_IMAGE_DECODER_BLEND_OP_SRC_OVER = 2,
990};
991
992#if __ANDROID_API__ >= 31
993
994/**
995 * Return how this frame is blended with the previous frame.
996 *
997 * Introduced in API 31.
998 *
999 * This, along with other information in AImageDecoderFrameInfo,
1000 * can be useful for determining whether a frame is independent, but
1001 * the decoder handles blending frames, so a simple
1002 * sequential client does not need this.
1003 *
1004 * @return one of:
1005 * - {@link ANDROID_IMAGE_DECODER_BLEND_OP_SRC}
1006 * - {@link ANDROID_IMAGE_DECODER_BLEND_OP_SRC_OVER}
1007 * Errors:
1008 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER} if |info| is null.
1009 */
1010int32_t AImageDecoderFrameInfo_getBlendOp(
1011 const AImageDecoderFrameInfo* _Nonnull info)
1012 __INTRODUCED_IN(31);
1013
Leon Scroggins III78859b42021-01-13 11:48:34 -05001014/**
1015 * Whether to have AImageDecoder store the frame prior to a
1016 * frame marked {@link ANDROID_IMAGE_DECODER_DISPOSE_OP_PREVIOUS}.
1017 *
1018 * Introduced in API 31.
1019 *
1020 * The default is true. Many images will not have such a frame (it
1021 * is not supported by WebP, and only some GIFs use it). But
1022 * if frame i is ANDROID_IMAGE_DECODER_DISPOSE_OP_PREVIOUS, then i+1
1023 * may depend on i-1. When this setting is true, AImageDecoder will
1024 * defensively copy frame i-1 (i.e. the contents of |pixels| in
1025 * {@link AImageDecoder_decodeImage}) into an internal buffer so that
1026 * it can be used to decode i+1.
1027 *
1028 * AImageDecoder will only store a single frame, at the size specified
1029 * by {@link AImageDecoder_setTargetSize} (or the original dimensions
1030 * if that method has not been called), and will discard it when it is
1031 * no longer necessary.
1032 *
1033 * A client that desires to manually store such frames may set this to
1034 * false, so that AImageDecoder does not need to store this extra
1035 * frame. Instead, when decoding the same
1036 * ANDROID_IMAGE_DECODER_DISPOSE_OP_PREVIOUS frame i, AImageDecoder
1037 * will decode directly into |pixels|, assuming the client stored i-1.
1038 * When asked to decode frame i+1, AImageDecoder will now assume that
1039 * the client provided i-1 in |pixels|.
1040 *
1041 * @param handleInternally Whether AImageDecoder will internally
1042 * handle ANDROID_IMAGE_DECODER_DISPOSE_OP_PREVIOUS
1043 * frames.
1044 */
1045void AImageDecoder_setInternallyHandleDisposePrevious(
1046 AImageDecoder* _Nonnull decoder, bool handleInternally)
1047 __INTRODUCED_IN(31);
1048
1049
Leon Scroggins IIIac9f7492020-12-28 15:33:13 -05001050#endif // __ANDROID_API__ >= 31
1051
Leon Scroggins III2f984942019-11-22 17:02:23 -05001052#ifdef __cplusplus
1053}
1054#endif
1055
1056#endif // ANDROID_IMAGE_DECODER_H
1057
1058/** @} */