blob: 819a6a4c1f67e06aa22dab3c688d3444565b3007 [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/**
143 * Return a constant string value representing the error code.
144 *
145 * Introduced in API 31.
146 *
147 * Pass the return value from an {@link AImageDecoder} method (e.g.
148 * {@link AImageDecoder_decodeImage}) for a text string representing the error
149 * code.
150 *
151 * Errors:
152 * - Returns null for a value out of range.
153 */
154const char* _Nullable AImageDecoder_resultToString(int)__INTRODUCED_IN(31);
155
Leon Scroggins III2f984942019-11-22 17:02:23 -0500156struct AImageDecoder;
157
158/**
159 * Opaque handle for decoding images.
160 *
Leon Scroggins III3b3700e2020-12-29 11:52:12 -0500161 * Introduced in API 30
162 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500163 * Create using one of the following:
164 * - {@link AImageDecoder_createFromAAsset}
165 * - {@link AImageDecoder_createFromFd}
166 * - {@link AImageDecoder_createFromBuffer}
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500167 *
168 * After creation, {@link AImageDecoder_getHeaderInfo} can be used to retrieve
169 * information about the encoded image. Other functions, like
170 * {@link AImageDecoder_setTargetSize}, can be used to specify how to decode, and
Leon Scroggins IIIac9f7492020-12-28 15:33:13 -0500171 * {@link AImageDecoder_decodeImage} will decode into client provided memory.
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500172 *
173 * {@link AImageDecoder} objects are NOT thread-safe, and should not be shared across
174 * threads.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500175 */
176typedef struct AImageDecoder AImageDecoder;
177
178/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500179 * Create a new {@link AImageDecoder} from an {@link AAsset}.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500180 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700181 * Available since API level 30.
182 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500183 * @param asset {@link AAsset} containing encoded image data. Client is still
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500184 * responsible for calling {@link AAsset_close} on it, which may be
185 * done after deleting the returned {@link AImageDecoder}.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500186 * @param outDecoder On success (i.e. return value is
187 * {@link ANDROID_IMAGE_DECODER_SUCCESS}), this will be set to
188 * a newly created {@link AImageDecoder}. Caller is
189 * responsible for calling {@link AImageDecoder_delete} on it.
190 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500191 * indicating the reason for the failure.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500192 *
193 * Errors:
194 * - {@link ANDROID_IMAGE_DECODER_INCOMPLETE}: The asset was truncated before
195 * reading the image header.
196 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: One of the parameters is
197 * null.
198 * - {@link ANDROID_IMAGE_DECODER_INVALID_INPUT}: There is an error in the
199 * header.
200 * - {@link ANDROID_IMAGE_DECODER_SEEK_ERROR}: The asset failed to seek.
201 * - {@link ANDROID_IMAGE_DECODER_INTERNAL_ERROR}: Some other error, like a
202 * failure to allocate memory.
203 * - {@link ANDROID_IMAGE_DECODER_UNSUPPORTED_FORMAT}: The format is not
204 * supported.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500205 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400206int AImageDecoder_createFromAAsset(struct AAsset* _Nonnull asset,
Leon Scroggins IIIb2ee00d2021-01-07 15:37:34 -0500207 AImageDecoder* _Nullable * _Nonnull outDecoder)
Leon Scroggins IIIa9f397b2020-01-27 12:42:56 -0500208 __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500209
210/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500211 * Create a new {@link AImageDecoder} from a file descriptor.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500212 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700213 * Available since API level 30.
214 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500215 * @param fd Seekable, readable, open file descriptor for encoded data.
216 * Client is still responsible for closing it, which may be done
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500217 * after deleting the returned {@link AImageDecoder}.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500218 * @param outDecoder On success (i.e. return value is
219 * {@link ANDROID_IMAGE_DECODER_SUCCESS}), this will be set to
220 * a newly created {@link AImageDecoder}. Caller is
221 * responsible for calling {@link AImageDecoder_delete} on it.
222 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500223 * indicating the reason for the failure.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500224 *
225 * Errors:
226 * - {@link ANDROID_IMAGE_DECODER_INCOMPLETE}: The file was truncated before
227 * reading the image header.
228 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The {@link AImageDecoder} is
229 * null, or |fd| does not represent a valid, seekable file descriptor.
230 * - {@link ANDROID_IMAGE_DECODER_INVALID_INPUT}: There is an error in the
231 * header.
232 * - {@link ANDROID_IMAGE_DECODER_SEEK_ERROR}: The descriptor failed to seek.
233 * - {@link ANDROID_IMAGE_DECODER_INTERNAL_ERROR}: Some other error, like a
234 * failure to allocate memory.
235 * - {@link ANDROID_IMAGE_DECODER_UNSUPPORTED_FORMAT}: The format is not
236 * supported.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500237 */
Leon Scroggins IIIb2ee00d2021-01-07 15:37:34 -0500238int AImageDecoder_createFromFd(int fd, AImageDecoder* _Nullable * _Nonnull outDecoder)
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400239 __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500240
241/**
242 * Create a new AImageDecoder from a buffer.
243 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700244 * Available since API level 30.
245 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500246 * @param buffer Pointer to encoded data. Must be valid for the entire time
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500247 * the {@link AImageDecoder} is used.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500248 * @param length Byte length of buffer.
249 * @param outDecoder On success (i.e. return value is
250 * {@link ANDROID_IMAGE_DECODER_SUCCESS}), this will be set to
251 * a newly created {@link AImageDecoder}. Caller is
252 * responsible for calling {@link AImageDecoder_delete} on it.
253 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500254 * indicating the reason for the failure.
255 *
256 * Errors:
257 * - {@link ANDROID_IMAGE_DECODER_INCOMPLETE}: The encoded image was truncated before
258 * reading the image header.
259 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: One of the parameters is
260 * invalid.
261 * - {@link ANDROID_IMAGE_DECODER_INVALID_INPUT}: There is an error in the
262 * header.
263 * - {@link ANDROID_IMAGE_DECODER_INTERNAL_ERROR}: Some other error, like a
264 * failure to allocate memory.
265 * - {@link ANDROID_IMAGE_DECODER_UNSUPPORTED_FORMAT}: The format is not
266 * supported.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500267 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400268int AImageDecoder_createFromBuffer(const void* _Nonnull buffer, size_t length,
Leon Scroggins IIIb2ee00d2021-01-07 15:37:34 -0500269 AImageDecoder* _Nullable * _Nonnull outDecoder)
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400270 __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500271
272/**
273 * Delete the AImageDecoder.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700274 *
275 * Available since API level 30.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500276 */
Leon Scroggins IIIb2ee00d2021-01-07 15:37:34 -0500277void AImageDecoder_delete(AImageDecoder* _Nullable decoder) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500278
279/**
280 * Choose the desired output format.
281 *
Leon Scroggins III55287002020-10-13 11:30:11 -0400282 * If the encoded image represents an animation, this must be called while on
283 * the first frame (e.g. before calling {@link AImageDecoder_advanceFrame} or
284 * after calling {@link AImageDecoder_rewind}).
285 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700286 * Available since API level 30.
287 *
Leon Scroggins III5d0445c2020-01-23 09:43:43 -0500288 * @param format {@link AndroidBitmapFormat} to use for the output.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500289 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
290 * indicating the reason for the failure. On failure, the
291 * {@link AImageDecoder} uses the format it was already planning
292 * to use (either its default or a previously successful setting
293 * from this function).
294 *
295 * Errors:
296 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The
297 * {@link AImageDecoder} is null or |format| does not correspond to an
298 * {@link AndroidBitmapFormat}.
299 * - {@link ANDROID_IMAGE_DECODER_INVALID_CONVERSION}: The
300 * {@link AndroidBitmapFormat} is incompatible with the image.
Leon Scroggins III55287002020-10-13 11:30:11 -0400301 * - {@link ANDROID_IMAGE_DECODER_INVALID_STATE}: The animation is not on
302 * the first frame.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500303 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400304int AImageDecoder_setAndroidBitmapFormat(AImageDecoder* _Nonnull decoder,
Leon Scroggins III2f984942019-11-22 17:02:23 -0500305 int32_t format) __INTRODUCED_IN(30);
306
Leon Scroggins III1be112f2020-01-15 04:26:44 -0500307/**
308 * Specify whether the output's pixels should be unpremultiplied.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500309 *
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500310 * By default, {@link AImageDecoder_decodeImage} will premultiply the pixels, if they have alpha.
311 * Pass true to this method to leave them unpremultiplied. This has no effect on an
Leon Scroggins III1be112f2020-01-15 04:26:44 -0500312 * opaque image.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500313 *
Leon Scroggins III55287002020-10-13 11:30:11 -0400314 * If the encoded image represents an animation, this must be called while on
315 * the first frame (e.g. before calling {@link AImageDecoder_advanceFrame} or
316 * after calling {@link AImageDecoder_rewind}).
317 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700318 * Available since API level 30.
319 *
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500320 * @param unpremultipliedRequired Pass true to leave the pixels unpremultiplied.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500321 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
322 * indicating the reason for the failure.
323 *
324 * Errors:
325 * - {@link ANDROID_IMAGE_DECODER_INVALID_CONVERSION}: Unpremultiplied is not
326 * possible due to an existing scale set by
327 * {@link AImageDecoder_setTargetSize}.
328 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The
329 * {@link AImageDecoder} is null.
Leon Scroggins III55287002020-10-13 11:30:11 -0400330 * - {@link ANDROID_IMAGE_DECODER_INVALID_STATE}: The animation is not on
331 * the first frame.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500332 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400333int AImageDecoder_setUnpremultipliedRequired(AImageDecoder* _Nonnull decoder,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500334 bool unpremultipliedRequired) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500335
336/**
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500337 * Choose the dataspace for the output.
338 *
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500339 * Ignored by {@link ANDROID_BITMAP_FORMAT_A_8}, which does not support
340 * an {@link ADataSpace}.
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500341 *
Leon Scroggins III55287002020-10-13 11:30:11 -0400342 * If the encoded image represents an animation, this must be called while on
343 * the first frame (e.g. before calling {@link AImageDecoder_advanceFrame} or
344 * after calling {@link AImageDecoder_rewind}).
345 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700346 * Available since API level 30.
347 *
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500348 * @param dataspace The {@link ADataSpace} to decode into. An ADataSpace
349 * specifies how to interpret the colors. By default,
350 * AImageDecoder will decode into the ADataSpace specified by
351 * {@link AImageDecoderHeaderInfo_getDataSpace}. If this
352 * parameter is set to a different ADataSpace, AImageDecoder
353 * will transform the output into the specified ADataSpace.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500354 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
355 * indicating the reason for the failure.
356 *
357 * Errors:
358 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The
359 * {@link AImageDecoder} is null or |dataspace| does not correspond to an
360 * {@link ADataSpace} value.
Leon Scroggins III55287002020-10-13 11:30:11 -0400361 * - {@link ANDROID_IMAGE_DECODER_INVALID_STATE}: The animation is not on
362 * the first frame.
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500363 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400364int AImageDecoder_setDataSpace(AImageDecoder* _Nonnull decoder, int32_t dataspace)
365 __INTRODUCED_IN(30);
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500366
367/**
Leon Scroggins III2f984942019-11-22 17:02:23 -0500368 * Specify the output size for a decoded image.
369 *
370 * Future calls to {@link AImageDecoder_decodeImage} will sample or scale the
371 * encoded image to reach the desired size. If a crop rect is set (via
372 * {@link AImageDecoder_setCrop}), it must be contained within the dimensions
373 * specified by width and height, and the output image will be the size of the
374 * crop rect.
375 *
Leon Scroggins III55287002020-10-13 11:30:11 -0400376 * If the encoded image represents an animation, this must be called while on
377 * the first frame (e.g. before calling {@link AImageDecoder_advanceFrame} or
378 * after calling {@link AImageDecoder_rewind}).
379 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700380 * Available since API level 30.
381 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500382 * @param width Width of the output (prior to cropping).
383 * This will affect future calls to
384 * {@link AImageDecoder_getMinimumStride}, which will now return
385 * a value based on this width.
386 * @param height Height of the output (prior to cropping).
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500387 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
388 * indicating the reason for the failure.
389 *
390 * Errors:
391 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The
392 * {@link AImageDecoder} is null.
393 * - {@link ANDROID_IMAGE_DECODER_INVALID_SCALE}: |width| or |height| is <= 0,
Leon Scroggins III55287002020-10-13 11:30:11 -0400394 * the size is too big, any existing crop is not contained by the new image
395 * dimensions, or the scale is incompatible with a previous call to
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500396 * {@link AImageDecoder_setUnpremultipliedRequired}(true).
Leon Scroggins III55287002020-10-13 11:30:11 -0400397 * - {@link ANDROID_IMAGE_DECODER_INVALID_STATE}: The animation is not on
398 * the first frame.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500399 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400400int AImageDecoder_setTargetSize(AImageDecoder* _Nonnull decoder, int32_t width,
401 int32_t height) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500402
Leon Scroggins IIIf27256b2020-01-19 21:13:04 -0500403/**
404 * Compute the dimensions to use for a given sampleSize.
405 *
406 * Although AImageDecoder can scale to an arbitrary target size (see
407 * {@link AImageDecoder_setTargetSize}), some sizes may be more efficient than
408 * others. This computes the most efficient target size to use to reach a
409 * particular sampleSize.
410 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700411 * Available since API level 30.
412 *
Leon Scroggins IIIf27256b2020-01-19 21:13:04 -0500413 * @param sampleSize A subsampling rate of the original image. Must be greater
414 * than or equal to 1. A sampleSize of 2 means to skip every
415 * other pixel/line, resulting in a width and height that are
416 * 1/2 of the original dimensions, with 1/4 the number of
417 * pixels.
418 * @param width Out parameter for the width sampled by sampleSize, and rounded
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500419 * in the direction that the decoder can do most efficiently.
Leon Scroggins IIIf27256b2020-01-19 21:13:04 -0500420 * @param height Out parameter for the height sampled by sampleSize, and rounded
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500421 * in the direction that the decoder can do most efficiently.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500422 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
423 * indicating the reason for the failure.
424 *
425 * Errors:
426 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The
427 * {@link AImageDecoder}, |width| or |height| is null or |sampleSize| is < 1.
Leon Scroggins IIIf27256b2020-01-19 21:13:04 -0500428 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400429int AImageDecoder_computeSampledSize(const AImageDecoder* _Nonnull decoder, int sampleSize,
430 int32_t* _Nonnull width, int32_t* _Nonnull height)
431 __INTRODUCED_IN(30);
Leon Scroggins III3b3700e2020-12-29 11:52:12 -0500432
Leon Scroggins III2f984942019-11-22 17:02:23 -0500433/**
434 * Specify how to crop the output after scaling (if any).
435 *
436 * Future calls to {@link AImageDecoder_decodeImage} will crop their output to
437 * the specified {@link ARect}. Clients will only need to allocate enough memory
438 * for the cropped ARect.
439 *
Leon Scroggins III55287002020-10-13 11:30:11 -0400440 * If the encoded image represents an animation, this must be called while on
441 * the first frame (e.g. before calling {@link AImageDecoder_advanceFrame} or
442 * after calling {@link AImageDecoder_rewind}).
443 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700444 * Available since API level 30.
445 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500446 * @param crop Rectangle describing a crop of the decode. It must be contained inside of
447 * the (possibly scaled, by {@link AImageDecoder_setTargetSize})
448 * image dimensions. This will affect future calls to
449 * {@link AImageDecoder_getMinimumStride}, which will now return a
450 * value based on the width of the crop. An empty ARect -
451 * specifically { 0, 0, 0, 0 } - may be used to remove the cropping
452 * behavior. Any other empty or unsorted ARects will result in
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500453 * returning {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500454 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
455 * indicating the reason for the failure.
456 *
457 * Errors:
458 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The
Leon Scroggins III55287002020-10-13 11:30:11 -0400459 * {@link AImageDecoder} is null, or the crop is not contained by the
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500460 * (possibly scaled) image dimensions.
Leon Scroggins III55287002020-10-13 11:30:11 -0400461 * - {@link ANDROID_IMAGE_DECODER_INVALID_STATE}: The animation is not on
462 * the first frame.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500463 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400464int AImageDecoder_setCrop(AImageDecoder* _Nonnull decoder, ARect crop) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500465
Leon Scroggins III2f984942019-11-22 17:02:23 -0500466struct AImageDecoderHeaderInfo;
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500467/**
Leon Scroggins III3b3700e2020-12-29 11:52:12 -0500468 * Opaque handle for representing information about the encoded image.
469 *
470 * Introduced in API 30
471 *
472 * Retrieved using {@link AImageDecoder_getHeaderInfo} and passed to methods
473 * like {@link AImageDecoderHeaderInfo_getWidth} and
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500474 * {@link AImageDecoderHeaderInfo_getHeight}.
475 */
Leon Scroggins III2f984942019-11-22 17:02:23 -0500476typedef struct AImageDecoderHeaderInfo AImageDecoderHeaderInfo;
477
478/**
479 * Return an opaque handle for reading header info.
480 *
481 * This is owned by the {@link AImageDecoder} and will be destroyed when the
482 * AImageDecoder is destroyed via {@link AImageDecoder_delete}.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700483 *
484 * Available since API level 30.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500485 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400486const AImageDecoderHeaderInfo* _Nonnull AImageDecoder_getHeaderInfo(
487 const AImageDecoder* _Nonnull decoder) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500488
489/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500490 * Report the native width of the encoded image. This is also the logical
491 * pixel width of the output, unless {@link AImageDecoder_setTargetSize} is
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500492 * used to choose a different size or {@link AImageDecoder_setCrop} is used to
493 * set a crop rect.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700494 *
495 * Available since API level 30.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500496 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400497int32_t AImageDecoderHeaderInfo_getWidth(const AImageDecoderHeaderInfo* _Nonnull)
498 __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500499
500/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500501 * Report the native height of the encoded image. This is also the logical
502 * pixel height of the output, unless {@link AImageDecoder_setTargetSize} is
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500503 * used to choose a different size or {@link AImageDecoder_setCrop} is used to
504 * set a crop rect.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700505 *
506 * Available since API level 30.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500507 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400508int32_t AImageDecoderHeaderInfo_getHeight(const AImageDecoderHeaderInfo* _Nonnull)
509 __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500510
511/**
512 * Report the mimeType of the encoded image.
513 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700514 * Available since API level 30.
515 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500516 * @return a string literal describing the mime type.
517 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400518const char* _Nonnull AImageDecoderHeaderInfo_getMimeType(
519 const AImageDecoderHeaderInfo* _Nonnull) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500520
521/**
Leon Scroggins III5d0445c2020-01-23 09:43:43 -0500522 * Report the {@link AndroidBitmapFormat} the AImageDecoder will decode to
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500523 * by default. {@link AImageDecoder} will try to choose one that is sensible
Leon Scroggins III2f984942019-11-22 17:02:23 -0500524 * for the image and the system. Note that this does not indicate the
525 * encoded format of the image.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700526 *
527 * Available since API level 30.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500528 */
Leon Scroggins III5d0445c2020-01-23 09:43:43 -0500529int32_t AImageDecoderHeaderInfo_getAndroidBitmapFormat(
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400530 const AImageDecoderHeaderInfo* _Nonnull) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500531
532/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500533 * Report how the {@link AImageDecoder} will handle alpha by default. If the image
Leon Scroggins III2f984942019-11-22 17:02:23 -0500534 * contains no alpha (according to its header), this will return
535 * {@link ANDROID_BITMAP_FLAGS_ALPHA_OPAQUE}. If the image may contain alpha,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500536 * this returns {@link ANDROID_BITMAP_FLAGS_ALPHA_PREMUL}, because
537 * {@link AImageDecoder_decodeImage} will premultiply pixels by default.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700538 *
539 * Available since API level 30.
Leon Scroggins IIIac9f7492020-12-28 15:33:13 -0500540 *
541 * Starting in API level 31, an AImageDecoder may contain multiple frames of an
542 * animation, but this method still only reports whether the first frame has
543 * alpha.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500544 */
545int AImageDecoderHeaderInfo_getAlphaFlags(
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400546 const AImageDecoderHeaderInfo* _Nonnull) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500547
548/**
Leon Scroggins III8cee65e2020-01-30 14:20:42 -0500549 * Report the dataspace the AImageDecoder will decode to by default.
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500550 *
Leon Scroggins III8cee65e2020-01-30 14:20:42 -0500551 * By default, {@link AImageDecoder_decodeImage} will not do any color
552 * conversion.
553 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700554 * Available since API level 30.
555 *
Leon Scroggins III8cee65e2020-01-30 14:20:42 -0500556 * @return The {@link ADataSpace} representing the way the colors
557 * are encoded (or {@link ADATASPACE_UNKNOWN} if there is not a
558 * corresponding ADataSpace). This specifies how to interpret the colors
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500559 * in the decoded image, unless {@link AImageDecoder_setDataSpace} is
560 * called to decode to a different ADataSpace.
561 *
562 * Note that ADataSpace only exposes a few values. This may return
Leon Scroggins III8cee65e2020-01-30 14:20:42 -0500563 * {@link ADATASPACE_UNKNOWN}, even for Named ColorSpaces, if they have
564 * no corresponding {@link ADataSpace}.
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500565 */
566int32_t AImageDecoderHeaderInfo_getDataSpace(
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400567 const AImageDecoderHeaderInfo* _Nonnull) __INTRODUCED_IN(30);
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500568
569/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500570 * Return the minimum stride that can be used in
571 * {@link AImageDecoder_decodeImage).
572 *
573 * This stride provides no padding, meaning it will be exactly equal to the
574 * width times the number of bytes per pixel for the {@link AndroidBitmapFormat}
575 * being used.
576 *
577 * If the output is scaled (via {@link AImageDecoder_setTargetSize}) and/or
578 * cropped (via {@link AImageDecoder_setCrop}), this takes those into account.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700579 *
580 * Available since API level 30.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500581 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400582size_t AImageDecoder_getMinimumStride(AImageDecoder* _Nonnull decoder) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500583
584/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500585 * Decode the image into pixels, using the settings of the {@link AImageDecoder}.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500586 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700587 * Available since API level 30.
588 *
Leon Scroggins III1b389712020-10-09 13:12:39 -0400589 * 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 -0500590 * animated image (i.e. GIF, WebP) using new APIs. Internally,
Leon Scroggins III55287002020-10-13 11:30:11 -0400591 * AImageDecoder keeps track of its "current frame" - that is, the frame that
592 * will be decoded by a call to AImageDecoder_decodeImage. At creation time, the
593 * current frame is always the first frame, and multiple calls to this method
594 * will each decode the first frame. {@link AImageDecoder_advanceFrame} advances
595 * the current frame to the following frame, so that future calls to this method
596 * will decode that frame. Some frames may update only part of the image. They
597 * may only update a sub-rectangle (see {@link
598 * AImageDecoderFrameInfo_getFrameRect}), or they may have alpha (see
599 * {@link AImageDecoderFrameInfo_hasAlphaWithinBounds}). In these cases, this
600 * method assumes that the prior frame is still residing in the |pixels| buffer,
601 * decodes only the new portion, and blends it with the buffer. Frames that change
602 * the entire |pixels| buffer are "independent", and do not require the prior
603 * frame to remain in the buffer. The first frame is always independent. A
604 * sophisticated client can use information from the {@link AImageDecoderFrameInfo}
605 * to determine whether other frames are independent, or what frames they rely on.
606 *
607 * If the current frame is marked {@link ANDROID_IMAGE_DECODER_DISPOSE_OP_PREVIOUS},
Leon Scroggins III78859b42021-01-13 11:48:34 -0500608 * AImageDecoder_decodeImage will store the |pixels| buffer prior to decoding
Leon Scroggins III55287002020-10-13 11:30:11 -0400609 * (note: this only happens for the first in a string of consecutive
610 * ANDROID_IMAGE_DECODER_DISPOSE_OP_PREVIOUS frames). After advancing to the
Leon Scroggins III78859b42021-01-13 11:48:34 -0500611 * following frame, AImageDecoder_decodeImage will restore that buffer prior to
612 * decoding that frame. This is the default behavior, but it can be disabled
613 * by passing false to {@link AImageDecoder_setInternallyHandleDisposePrevious}.
Leon Scroggins III55287002020-10-13 11:30:11 -0400614 *
615 * Ignoring timing information, display, etc, a client wishing to decode all
616 * frames of an animated image may conceptually use code like the following:
617 *
618 * while (true) {
619 * int result = AImageDecoder_decodeImage(decoder, pixels, stride, size);
620 * if (result != ANDROID_IMAGE_DECODER_SUCCESS) break;
621 *
622 * // Display or save the image in |pixels|, keeping the buffer intact for
623 * // AImageDecoder to decode the next frame correctly.
624 * Application_viewImage(pixels);
625 *
626 * result = AImageDecoder_advanceFrame(decoder);
627 * if (result != ANDROID_IMAGE_DECODER_SUCCESS) break;
628 * }
Leon Scroggins III1b389712020-10-09 13:12:39 -0400629 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500630 * @param decoder Opaque object representing the decoder.
631 * @param pixels On success, will be filled with the result
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500632 * of the decode. Must be large enough to hold |size| bytes.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500633 * @param stride Width in bytes of a single row. Must be at least
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500634 * {@link AImageDecoder_getMinimumStride} and a multiple of the
635 * bytes per pixel of the {@link AndroidBitmapFormat}.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500636 * @param size Size of the pixel buffer in bytes. Must be at least
637 * stride * (height - 1) +
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500638 * {@link AImageDecoder_getMinimumStride}.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500639 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
640 * indicating the reason for the failure.
641 *
642 * Errors:
643 * - {@link ANDROID_IMAGE_DECODER_INCOMPLETE}: The image was truncated. A
644 * partial image was decoded, and undecoded lines have been initialized to all
645 * zeroes.
646 * - {@link ANDROID_IMAGE_DECODER_ERROR}: The image contained an error. A
647 * partial image was decoded, and undecoded lines have been initialized to all
648 * zeroes.
649 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The {@link AImageDecoder} or
650 * |pixels| is null, the stride is not large enough or not pixel aligned, or
651 * |size| is not large enough.
652 * - {@link ANDROID_IMAGE_DECODER_SEEK_ERROR}: The asset or file descriptor
653 * failed to seek.
654 * - {@link ANDROID_IMAGE_DECODER_INTERNAL_ERROR}: Some other error, like a
655 * failure to allocate memory.
Leon Scroggins III55287002020-10-13 11:30:11 -0400656 * - {@link ANDROID_IMAGE_DECODER_FINISHED}: The input contains no
657 * more frames. No decoding occurred. The client must call
658 * {@link AImageDecoder_rewind} before calling
659 * {@link AImageDecoder_decodeImage} again.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500660 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400661int AImageDecoder_decodeImage(AImageDecoder* _Nonnull decoder,
662 void* _Nonnull pixels, size_t stride,
Leon Scroggins III2f984942019-11-22 17:02:23 -0500663 size_t size) __INTRODUCED_IN(30);
664
Leon Scroggins III1b389712020-10-09 13:12:39 -0400665/**
666 * Return true iff the image is animated - i.e. has multiple frames.
667 *
668 * Introduced in API 31.
669 *
Leon Scroggins III55287002020-10-13 11:30:11 -0400670 * A single frame GIF is considered to *not* be animated. This may require
671 * seeking past the first frame to verify whether there is a following frame.
Leon Scroggins III1b389712020-10-09 13:12:39 -0400672 *
673 * Errors:
674 * - returns false if |decoder| is null.
675 */
676bool AImageDecoder_isAnimated(AImageDecoder* _Nonnull decoder)
677 __INTRODUCED_IN(31);
678
Leon Scroggins III728a9ac2020-10-12 10:34:05 -0400679enum {
680 /*
681 * Reported by {@link AImageDecoder_getRepeatCount} if the
682 * animation should repeat forever.
Leon Scroggins III3b3700e2020-12-29 11:52:12 -0500683 *
684 * Introduced in API 31
Leon Scroggins III728a9ac2020-10-12 10:34:05 -0400685 */
686 ANDROID_IMAGE_DECODER_INFINITE = INT32_MAX,
687};
688
689/**
690 * Report how many times the animation should repeat.
691 *
692 * Introduced in API 31.
693 *
694 * This does not include the first play through. e.g. a repeat
695 * count of 4 means that each frame is played 5 times.
696 *
697 * {@link ANDROID_IMAGE_DECODER_INFINITE} means to repeat forever.
698 *
699 * This may require seeking.
700 *
701 * For non-animated formats, this returns 0. It may return non-zero for
702 * an image with only one frame (i.e. {@link AImageDecoder_isAnimated} returns
703 * false) if the encoded image contains a repeat count.
704 *
705 * @return Number of times to repeat on success or a value
706 * indicating the reason for the failure.
707 *
708 * Errors:
709 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The AImageDecoder
710 * is null.
711 */
Jiyong Parkf1f20902021-01-13 08:28:13 +0900712int32_t AImageDecoder_getRepeatCount(AImageDecoder* _Nonnull decoder)
Leon Scroggins III728a9ac2020-10-12 10:34:05 -0400713 __INTRODUCED_IN(31);
714
Leon Scroggins III55287002020-10-13 11:30:11 -0400715/**
716 * Advance to the next frame in the animation.
717 *
718 * Introduced in API 31.
719 *
720 * The AImageDecoder keeps track internally which frame it is ready to decode
721 * (the "current frame"). Initially it is set to decode the first frame, and
722 * each call to {@link AImageDecoder_decodeImage} will continue to decode
723 * the same frame until this method (or {@link AImageDecoder_rewind})
724 * is called.
725 *
726 * Note that this can be used to skip a frame without decoding it. But
727 * some frames depend on (i.e. blend with) prior frames, and
728 * AImageDecoder_decodeImage assumes that the prior frame is in the
729 * |pixels| buffer. In addition, AImageDecoder_decodeImage handles caching and
730 * restoring frames (see {@link ANDROID_IMAGE_DECODER_DISPOSE_OP_PREVIOUS}), so
731 * skipping frames in an image with such frames may not produce the correct
732 * results.
733 *
734 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
735 * indicating the reason for the failure.
736 *
737 * Errors:
738 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The AImageDecoder
739 * represents an image that is not animated (see
740 * {@link AImageDecoder_isAnimated}) or the AImageDecoder is null.
741 * - {@link ANDROID_IMAGE_DECODER_INCOMPLETE}: The input appears
742 * to be truncated. The client must call {@link AImageDecoder_rewind}
743 * before calling {@link AImageDecoder_decodeImage} again.
744 * - {@link ANDROID_IMAGE_DECODER_ERROR}: The input contains an error.
745 * The client must call {@link AImageDecoder_rewind} before
746 * calling {@link AImageDecoder_decodeImage} again.
747 * - {@link ANDROID_IMAGE_DECODER_FINISHED}: The input contains no
748 * more frames. The client must call {@link AImageDecoder_rewind}
749 * before calling {@link AImageDecoder_decodeImage} again.
750 */
751int AImageDecoder_advanceFrame(AImageDecoder* _Nonnull decoder)
752 __INTRODUCED_IN(31);
753
754/**
755 * Return to the beginning of the animation.
756 *
757 * Introduced in API 31.
758 *
759 * After this call, the AImageDecoder will be ready to decode the
760 * first frame of the animation. This can be called after reaching
761 * the end of the animation or an error or in the middle of the
762 * animation.
763 *
764 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
765 * indicating the reason for the failure.
766 *
767 * Errors:
768 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The AImageDecoder
769 * represents an image that is not animated (see
770 * {@link AImageDecoder_isAnimated}) or the AImageDecoder is
771 * null.
772 * - {@link ANDROID_IMAGE_DECODER_SEEK_ERROR}: The asset or file
773 * descriptor failed to seek.
774 */
775int AImageDecoder_rewind(AImageDecoder* _Nonnull decoder)
776 __INTRODUCED_IN(31);
777
Leon Scroggins IIIac9f7492020-12-28 15:33:13 -0500778struct AImageDecoderFrameInfo;
779
780/**
781 * Opaque handle to animation information about a single frame.
782 *
783 * Introduced in API 31
784 *
785 * The duration (retrieved with {@link AImageDecoderFrameInfo_getDuration}) is
786 * necessary for clients to display the animation at the proper speed. The other
787 * information is helpful for a client that wants to determine what frames are
788 * independent (or what frames they depend on), but is unnecessary for
789 * a simple client that wants to sequentially display all frames.
790 */
791typedef struct AImageDecoderFrameInfo AImageDecoderFrameInfo;
792
Leon Scroggins IIIac9f7492020-12-28 15:33:13 -0500793/**
794 * Create an uninitialized AImageDecoderFrameInfo.
795 *
796 * Introduced in API 31.
797 *
798 * This can be passed to {@link AImageDecoder_getFrameInfo} to fill
799 * in information about the current frame. It may be reused.
800 *
801 * Must be deleted with {@link AImageDecoderFrameInfo_delete}.
802 */
803AImageDecoderFrameInfo* _Nullable AImageDecoderFrameInfo_create()
804 __INTRODUCED_IN(31);
805
806/**
807 * Delete an AImageDecoderFrameInfo.
808 *
809 * Introduced in API 31.
810 */
811void AImageDecoderFrameInfo_delete(
812 AImageDecoderFrameInfo* _Nullable info) __INTRODUCED_IN(31);
813
814/**
815 * Fill |info| with information about the current frame.
816 *
817 * Introduced in API 31.
818 *
819 * Initially, this will return information about the first frame.
820 * {@link AImageDecoder_advanceFrame} and
821 * {@link AImageDecoder_rewind} can be used to change which frame
822 * is the current frame.
823 *
824 * If the image only has one frame, this will fill the {@link
825 * AImageDecoderFrameInfo} with the encoded info, if any, or reasonable
826 * defaults.
827 *
828 * @param decoder Opaque object representing the decoder.
829 * @param info Opaque object to hold frame information. On success, will be
830 * filled with information regarding the current frame.
831 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
832 * indicating the reason for the failure.
833 *
834 * Errors:
835 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: One of the parameters is null.
836 * - {@link ANDROID_IMAGE_DECODER_FINISHED}: The input contains no
837 * more frames. The client must call {@link AImageDecoder_rewind} to reset the
838 * current frame to a valid frame (0).
839 */
840int AImageDecoder_getFrameInfo(AImageDecoder* _Nonnull decoder,
841 AImageDecoderFrameInfo* _Nonnull info) __INTRODUCED_IN(31);
842
843/**
844 * Report the number of nanoseconds to show the current frame.
845 *
846 * Introduced in API 31.
847 *
848 * Errors:
849 * - returns 0 if |info| is null.
850 */
851int64_t AImageDecoderFrameInfo_getDuration(
852 const AImageDecoderFrameInfo* _Nonnull info) __INTRODUCED_IN(31);
853
854/**
855 * The rectangle of the image (within 0, 0,
856 * {@link AImageDecoder_getWidth}, {@link AImageDecoder_getHeight})
857 * updated by this frame.
858 *
859 * Introduced in API 31.
860 *
861 * Note that this is unaffected by calls to
862 * {@link AImageDecoder_setTargetSize} or
863 * {@link AImageDecoder_setCrop}.
864 *
865 * A frame may update only part of the image. This will always be
866 * contained by the image’s dimensions.
867 *
868 * This, along with other information in AImageDecoderFrameInfo,
869 * can be useful for determining whether a frame is independent, but
870 * the decoder handles blending frames, so a simple
871 * sequential client does not need this.
872 *
873 * Errors:
874 * - returns an empty ARect if |info| is null.
875 */
876ARect AImageDecoderFrameInfo_getFrameRect(
877 const AImageDecoderFrameInfo* _Nonnull info) __INTRODUCED_IN(31);
878
879/**
880 * Whether the new portion of this frame may contain alpha.
881 *
882 * Introduced in API 31.
883 *
884 * Note that this may differ from whether the composed frame has
885 * alpha. If this frame does not fill the entire image dimensions
886 * (see {@link AImageDecoderFrameInfo_getFrameRect}) or it blends
887 * with an opaque frame, for example, the composed frame’s alpha
888 * may not match. It is also conservative; for example, if a color
889 * index-based frame has a color with alpha but does not use it,
890 * this will still return true.
891 *
892 * This, along with other information in AImageDecoderFrameInfo,
893 * can be useful for determining whether a frame is independent, but
894 * the decoder handles blending frames, so a simple
895 * sequential client does not need this.
896 *
897 * Errors:
898 * - returns false if |info| is null.
899 */
900bool AImageDecoderFrameInfo_hasAlphaWithinBounds(
901 const AImageDecoderFrameInfo* _Nonnull info) __INTRODUCED_IN(31);
902
Leon Scroggins IIIac9f7492020-12-28 15:33:13 -0500903/**
904 * How a frame is “disposed” before showing the next one.
905 *
906 * Introduced in API 31.
907 *
908 * This, along with other information in AImageDecoderFrameInfo,
909 * can be useful for determining whether a frame is independent, but
910 * the decoder handles disposing of frames, so a simple
911 * sequential client does not need this.
912 */
913enum {
914 // No disposal. The following frame will be drawn directly
915 // on top of this one.
916 ANDROID_IMAGE_DECODER_DISPOSE_OP_NONE = 1,
Leon Scroggins III78859b42021-01-13 11:48:34 -0500917 // The frame’s rectangle is cleared to transparent (by AImageDecoder)
918 // before decoding the next frame.
Leon Scroggins IIIac9f7492020-12-28 15:33:13 -0500919 ANDROID_IMAGE_DECODER_DISPOSE_OP_BACKGROUND = 2,
Leon Scroggins III78859b42021-01-13 11:48:34 -0500920 // The frame’s rectangle is reverted to the prior frame before decoding
921 // the next frame. This is handled by AImageDecoder, unless
922 // {@link AImageDecoder_setInternallyHandleDisposePrevious} is set to false.
Leon Scroggins IIIac9f7492020-12-28 15:33:13 -0500923 ANDROID_IMAGE_DECODER_DISPOSE_OP_PREVIOUS = 3,
924};
925
Leon Scroggins IIIac9f7492020-12-28 15:33:13 -0500926/**
927 * Return how this frame is “disposed” before showing the next one.
928 *
929 * Introduced in API 31.
930 *
931 * This, along with other information in AImageDecoderFrameInfo,
932 * can be useful for determining whether a frame is independent, but
933 * the decoder handles disposing of frames, so a simple
934 * sequential client does not need this.
935 *
936 * @return one of:
937 * - {@link ANDROID_IMAGE_DECODER_DISPOSE_OP_NONE}
938 * - {@link ANDROID_IMAGE_DECODER_DISPOSE_OP_BACKGROUND}
939 * - {@link ANDROID_IMAGE_DECODER_DISPOSE_OP_PREVIOUS}
940 * Errors:
941 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER} if |info| is null.
942 */
943int32_t AImageDecoderFrameInfo_getDisposeOp(
944 const AImageDecoderFrameInfo* _Nonnull info) __INTRODUCED_IN(31);
945
Leon Scroggins IIIac9f7492020-12-28 15:33:13 -0500946/**
947 * How a frame is blended with the previous frame.
948 *
949 * Introduced in API 31.
950 *
951 * This, along with other information in AImageDecoderFrameInfo,
952 * can be useful for determining whether a frame is independent, but
953 * the decoder handles blending frames, so a simple
954 * sequential client does not need this.
955 */
956enum {
957 // This frame replaces existing content. This corresponds
958 // to webp’s “do not blend”.
959 ANDROID_IMAGE_DECODER_BLEND_OP_SRC = 1,
960 // This frame blends with the previous frame.
961 ANDROID_IMAGE_DECODER_BLEND_OP_SRC_OVER = 2,
962};
963
Leon Scroggins IIIac9f7492020-12-28 15:33:13 -0500964/**
965 * Return how this frame is blended with the previous frame.
966 *
967 * Introduced in API 31.
968 *
969 * This, along with other information in AImageDecoderFrameInfo,
970 * can be useful for determining whether a frame is independent, but
971 * the decoder handles blending frames, so a simple
972 * sequential client does not need this.
973 *
974 * @return one of:
975 * - {@link ANDROID_IMAGE_DECODER_BLEND_OP_SRC}
976 * - {@link ANDROID_IMAGE_DECODER_BLEND_OP_SRC_OVER}
977 * Errors:
978 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER} if |info| is null.
979 */
980int32_t AImageDecoderFrameInfo_getBlendOp(
981 const AImageDecoderFrameInfo* _Nonnull info)
982 __INTRODUCED_IN(31);
983
Leon Scroggins III78859b42021-01-13 11:48:34 -0500984/**
985 * Whether to have AImageDecoder store the frame prior to a
986 * frame marked {@link ANDROID_IMAGE_DECODER_DISPOSE_OP_PREVIOUS}.
987 *
988 * Introduced in API 31.
989 *
990 * The default is true. Many images will not have such a frame (it
991 * is not supported by WebP, and only some GIFs use it). But
992 * if frame i is ANDROID_IMAGE_DECODER_DISPOSE_OP_PREVIOUS, then i+1
993 * may depend on i-1. When this setting is true, AImageDecoder will
994 * defensively copy frame i-1 (i.e. the contents of |pixels| in
995 * {@link AImageDecoder_decodeImage}) into an internal buffer so that
996 * it can be used to decode i+1.
997 *
998 * AImageDecoder will only store a single frame, at the size specified
999 * by {@link AImageDecoder_setTargetSize} (or the original dimensions
1000 * if that method has not been called), and will discard it when it is
1001 * no longer necessary.
1002 *
1003 * A client that desires to manually store such frames may set this to
1004 * false, so that AImageDecoder does not need to store this extra
1005 * frame. Instead, when decoding the same
1006 * ANDROID_IMAGE_DECODER_DISPOSE_OP_PREVIOUS frame i, AImageDecoder
1007 * will decode directly into |pixels|, assuming the client stored i-1.
1008 * When asked to decode frame i+1, AImageDecoder will now assume that
1009 * the client provided i-1 in |pixels|.
1010 *
1011 * @param handleInternally Whether AImageDecoder will internally
1012 * handle ANDROID_IMAGE_DECODER_DISPOSE_OP_PREVIOUS
1013 * frames.
1014 */
1015void AImageDecoder_setInternallyHandleDisposePrevious(
1016 AImageDecoder* _Nonnull decoder, bool handleInternally)
1017 __INTRODUCED_IN(31);
1018
1019
Leon Scroggins III2f984942019-11-22 17:02:23 -05001020#ifdef __cplusplus
1021}
1022#endif
1023
1024#endif // ANDROID_IMAGE_DECODER_H
1025
1026/** @} */