blob: d5345547849b707d72b8417409178eb8939493f2 [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
54#ifdef __cplusplus
55extern "C" {
56#endif
57
58struct AAsset;
Leon Scroggins III2f984942019-11-22 17:02:23 -050059
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050060/**
Leon Scroggins III3b3700e2020-12-29 11:52:12 -050061 * {@link AImageDecoder} functions result code.
62 *
63 * Introduced in API 30.
64 *
65 * Many functions will return this to indicate success
66 * ({@link ANDROID_IMAGE_DECODER_SUCCESS}) or the reason for the failure. On
67 * failure, any out-parameters should be considered uninitialized, except where
Leon Scroggins III9b9fb392020-12-03 16:55:36 -050068 * specified. Use {@link AImageDecoder_resultToString} for a readable
69 * version of the result code.
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050070 */
Leon Scroggins III2f984942019-11-22 17:02:23 -050071enum {
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050072 /**
73 * Decoding was successful and complete.
74 */
Leon Scroggins III2f984942019-11-22 17:02:23 -050075 ANDROID_IMAGE_DECODER_SUCCESS = 0,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050076 /**
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -050077 * The input is incomplete.
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050078 */
Leon Scroggins III2f984942019-11-22 17:02:23 -050079 ANDROID_IMAGE_DECODER_INCOMPLETE = -1,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050080 /**
81 * The input contained an error after decoding some lines.
82 */
Leon Scroggins III2f984942019-11-22 17:02:23 -050083 ANDROID_IMAGE_DECODER_ERROR = -2,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050084 /**
85 * Could not convert. For example, attempting to decode an image with
86 * alpha to an opaque format.
87 */
Leon Scroggins III2f984942019-11-22 17:02:23 -050088 ANDROID_IMAGE_DECODER_INVALID_CONVERSION = -3,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050089 /**
90 * The scale is invalid. It may have overflowed, or it may be incompatible
91 * with the current alpha setting.
92 */
Leon Scroggins III2f984942019-11-22 17:02:23 -050093 ANDROID_IMAGE_DECODER_INVALID_SCALE = -4,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050094 /**
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -050095 * Some other parameter is invalid.
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050096 */
Leon Scroggins III2f984942019-11-22 17:02:23 -050097 ANDROID_IMAGE_DECODER_BAD_PARAMETER = -5,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050098 /**
99 * Input was invalid before decoding any pixels.
100 */
Leon Scroggins III2f984942019-11-22 17:02:23 -0500101 ANDROID_IMAGE_DECODER_INVALID_INPUT = -6,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500102 /**
103 * A seek was required and it failed.
104 */
Leon Scroggins III2f984942019-11-22 17:02:23 -0500105 ANDROID_IMAGE_DECODER_SEEK_ERROR = -7,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500106 /**
107 * Some other error. For example, an internal allocation failed.
108 */
Leon Scroggins III2f984942019-11-22 17:02:23 -0500109 ANDROID_IMAGE_DECODER_INTERNAL_ERROR = -8,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500110 /**
111 * AImageDecoder did not recognize the format.
112 */
Leon Scroggins III55287002020-10-13 11:30:11 -0400113 ANDROID_IMAGE_DECODER_UNSUPPORTED_FORMAT = -9,
114 /**
115 * The animation has reached the end.
116 */
117 ANDROID_IMAGE_DECODER_FINISHED = -10,
118 /**
119 * This method cannot be called while the AImageDecoder is in its current
120 * state. For example, various setters (like {@link AImageDecoder_setTargetSize})
121 * can only be called while the AImageDecoder is set to decode the first
122 * frame of an animation. This ensures that any blending and/or restoring
123 * prior frames works correctly.
124 */
125 ANDROID_IMAGE_DECODER_INVALID_STATE = -11,
Leon Scroggins III2f984942019-11-22 17:02:23 -0500126};
127
Leon Scroggins III9b9fb392020-12-03 16:55:36 -0500128#if __ANDROID_API__ >= 31
129
130/**
131 * Return a constant string value representing the error code.
132 *
133 * Introduced in API 31.
134 *
135 * Pass the return value from an {@link AImageDecoder} method (e.g.
136 * {@link AImageDecoder_decodeImage}) for a text string representing the error
137 * code.
138 *
139 * Errors:
140 * - Returns null for a value out of range.
141 */
142const char* _Nullable AImageDecoder_resultToString(int)__INTRODUCED_IN(31);
143
144#endif // __ANDROID_API__ >= 31
145
Leon Scroggins III2f984942019-11-22 17:02:23 -0500146struct AImageDecoder;
147
148/**
149 * Opaque handle for decoding images.
150 *
Leon Scroggins III3b3700e2020-12-29 11:52:12 -0500151 * Introduced in API 30
152 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500153 * Create using one of the following:
154 * - {@link AImageDecoder_createFromAAsset}
155 * - {@link AImageDecoder_createFromFd}
156 * - {@link AImageDecoder_createFromBuffer}
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500157 *
158 * After creation, {@link AImageDecoder_getHeaderInfo} can be used to retrieve
159 * information about the encoded image. Other functions, like
160 * {@link AImageDecoder_setTargetSize}, can be used to specify how to decode, and
Leon Scroggins IIIac9f7492020-12-28 15:33:13 -0500161 * {@link AImageDecoder_decodeImage} will decode into client provided memory.
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500162 *
163 * {@link AImageDecoder} objects are NOT thread-safe, and should not be shared across
164 * threads.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500165 */
166typedef struct AImageDecoder AImageDecoder;
167
Leon Scroggins III3b3700e2020-12-29 11:52:12 -0500168#if __ANDROID_API__ >= 30
169
Leon Scroggins III2f984942019-11-22 17:02:23 -0500170/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500171 * Create a new {@link AImageDecoder} from an {@link AAsset}.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500172 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700173 * Available since API level 30.
174 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500175 * @param asset {@link AAsset} containing encoded image data. Client is still
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500176 * responsible for calling {@link AAsset_close} on it, which may be
177 * done after deleting the returned {@link AImageDecoder}.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500178 * @param outDecoder On success (i.e. return value is
179 * {@link ANDROID_IMAGE_DECODER_SUCCESS}), this will be set to
180 * a newly created {@link AImageDecoder}. Caller is
181 * responsible for calling {@link AImageDecoder_delete} on it.
182 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500183 * indicating the reason for the failure.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500184 *
185 * Errors:
186 * - {@link ANDROID_IMAGE_DECODER_INCOMPLETE}: The asset was truncated before
187 * reading the image header.
188 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: One of the parameters is
189 * null.
190 * - {@link ANDROID_IMAGE_DECODER_INVALID_INPUT}: There is an error in the
191 * header.
192 * - {@link ANDROID_IMAGE_DECODER_SEEK_ERROR}: The asset failed to seek.
193 * - {@link ANDROID_IMAGE_DECODER_INTERNAL_ERROR}: Some other error, like a
194 * failure to allocate memory.
195 * - {@link ANDROID_IMAGE_DECODER_UNSUPPORTED_FORMAT}: The format is not
196 * supported.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500197 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400198int AImageDecoder_createFromAAsset(struct AAsset* _Nonnull asset,
Leon Scroggins IIIb2ee00d2021-01-07 15:37:34 -0500199 AImageDecoder* _Nullable * _Nonnull outDecoder)
Leon Scroggins IIIa9f397b2020-01-27 12:42:56 -0500200 __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500201
202/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500203 * Create a new {@link AImageDecoder} from a file descriptor.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500204 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700205 * Available since API level 30.
206 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500207 * @param fd Seekable, readable, open file descriptor for encoded data.
208 * Client is still responsible for closing it, which may be done
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500209 * after deleting the returned {@link AImageDecoder}.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500210 * @param outDecoder On success (i.e. return value is
211 * {@link ANDROID_IMAGE_DECODER_SUCCESS}), this will be set to
212 * a newly created {@link AImageDecoder}. Caller is
213 * responsible for calling {@link AImageDecoder_delete} on it.
214 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500215 * indicating the reason for the failure.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500216 *
217 * Errors:
218 * - {@link ANDROID_IMAGE_DECODER_INCOMPLETE}: The file was truncated before
219 * reading the image header.
220 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The {@link AImageDecoder} is
221 * null, or |fd| does not represent a valid, seekable file descriptor.
222 * - {@link ANDROID_IMAGE_DECODER_INVALID_INPUT}: There is an error in the
223 * header.
224 * - {@link ANDROID_IMAGE_DECODER_SEEK_ERROR}: The descriptor failed to seek.
225 * - {@link ANDROID_IMAGE_DECODER_INTERNAL_ERROR}: Some other error, like a
226 * failure to allocate memory.
227 * - {@link ANDROID_IMAGE_DECODER_UNSUPPORTED_FORMAT}: The format is not
228 * supported.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500229 */
Leon Scroggins IIIb2ee00d2021-01-07 15:37:34 -0500230int AImageDecoder_createFromFd(int fd, AImageDecoder* _Nullable * _Nonnull outDecoder)
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400231 __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500232
233/**
234 * Create a new AImageDecoder from a buffer.
235 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700236 * Available since API level 30.
237 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500238 * @param buffer Pointer to encoded data. Must be valid for the entire time
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500239 * the {@link AImageDecoder} is used.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500240 * @param length Byte length of buffer.
241 * @param outDecoder On success (i.e. return value is
242 * {@link ANDROID_IMAGE_DECODER_SUCCESS}), this will be set to
243 * a newly created {@link AImageDecoder}. Caller is
244 * responsible for calling {@link AImageDecoder_delete} on it.
245 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500246 * indicating the reason for the failure.
247 *
248 * Errors:
249 * - {@link ANDROID_IMAGE_DECODER_INCOMPLETE}: The encoded image was truncated before
250 * reading the image header.
251 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: One of the parameters is
252 * invalid.
253 * - {@link ANDROID_IMAGE_DECODER_INVALID_INPUT}: There is an error in the
254 * header.
255 * - {@link ANDROID_IMAGE_DECODER_INTERNAL_ERROR}: Some other error, like a
256 * failure to allocate memory.
257 * - {@link ANDROID_IMAGE_DECODER_UNSUPPORTED_FORMAT}: The format is not
258 * supported.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500259 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400260int AImageDecoder_createFromBuffer(const void* _Nonnull buffer, size_t length,
Leon Scroggins IIIb2ee00d2021-01-07 15:37:34 -0500261 AImageDecoder* _Nullable * _Nonnull outDecoder)
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400262 __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500263
264/**
265 * Delete the AImageDecoder.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700266 *
267 * Available since API level 30.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500268 */
Leon Scroggins IIIb2ee00d2021-01-07 15:37:34 -0500269void AImageDecoder_delete(AImageDecoder* _Nullable decoder) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500270
271/**
272 * Choose the desired output format.
273 *
Leon Scroggins III55287002020-10-13 11:30:11 -0400274 * If the encoded image represents an animation, this must be called while on
275 * the first frame (e.g. before calling {@link AImageDecoder_advanceFrame} or
276 * after calling {@link AImageDecoder_rewind}).
277 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700278 * Available since API level 30.
279 *
Leon Scroggins III5d0445c2020-01-23 09:43:43 -0500280 * @param format {@link AndroidBitmapFormat} to use for the output.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500281 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
282 * indicating the reason for the failure. On failure, the
283 * {@link AImageDecoder} uses the format it was already planning
284 * to use (either its default or a previously successful setting
285 * from this function).
286 *
287 * Errors:
288 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The
289 * {@link AImageDecoder} is null or |format| does not correspond to an
290 * {@link AndroidBitmapFormat}.
291 * - {@link ANDROID_IMAGE_DECODER_INVALID_CONVERSION}: The
292 * {@link AndroidBitmapFormat} is incompatible with the image.
Leon Scroggins III55287002020-10-13 11:30:11 -0400293 * - {@link ANDROID_IMAGE_DECODER_INVALID_STATE}: The animation is not on
294 * the first frame.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500295 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400296int AImageDecoder_setAndroidBitmapFormat(AImageDecoder* _Nonnull decoder,
Leon Scroggins III2f984942019-11-22 17:02:23 -0500297 int32_t format) __INTRODUCED_IN(30);
298
Leon Scroggins III1be112f2020-01-15 04:26:44 -0500299/**
300 * Specify whether the output's pixels should be unpremultiplied.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500301 *
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500302 * By default, {@link AImageDecoder_decodeImage} will premultiply the pixels, if they have alpha.
303 * Pass true to this method to leave them unpremultiplied. This has no effect on an
Leon Scroggins III1be112f2020-01-15 04:26:44 -0500304 * opaque image.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500305 *
Leon Scroggins III55287002020-10-13 11:30:11 -0400306 * If the encoded image represents an animation, this must be called while on
307 * the first frame (e.g. before calling {@link AImageDecoder_advanceFrame} or
308 * after calling {@link AImageDecoder_rewind}).
309 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700310 * Available since API level 30.
311 *
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500312 * @param unpremultipliedRequired Pass true to leave the pixels unpremultiplied.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500313 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
314 * indicating the reason for the failure.
315 *
316 * Errors:
317 * - {@link ANDROID_IMAGE_DECODER_INVALID_CONVERSION}: Unpremultiplied is not
318 * possible due to an existing scale set by
319 * {@link AImageDecoder_setTargetSize}.
320 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The
321 * {@link AImageDecoder} is null.
Leon Scroggins III55287002020-10-13 11:30:11 -0400322 * - {@link ANDROID_IMAGE_DECODER_INVALID_STATE}: The animation is not on
323 * the first frame.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500324 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400325int AImageDecoder_setUnpremultipliedRequired(AImageDecoder* _Nonnull decoder,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500326 bool unpremultipliedRequired) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500327
328/**
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500329 * Choose the dataspace for the output.
330 *
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500331 * Ignored by {@link ANDROID_BITMAP_FORMAT_A_8}, which does not support
332 * an {@link ADataSpace}.
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500333 *
Leon Scroggins III55287002020-10-13 11:30:11 -0400334 * If the encoded image represents an animation, this must be called while on
335 * the first frame (e.g. before calling {@link AImageDecoder_advanceFrame} or
336 * after calling {@link AImageDecoder_rewind}).
337 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700338 * Available since API level 30.
339 *
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500340 * @param dataspace The {@link ADataSpace} to decode into. An ADataSpace
341 * specifies how to interpret the colors. By default,
342 * AImageDecoder will decode into the ADataSpace specified by
343 * {@link AImageDecoderHeaderInfo_getDataSpace}. If this
344 * parameter is set to a different ADataSpace, AImageDecoder
345 * will transform the output into the specified ADataSpace.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500346 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
347 * indicating the reason for the failure.
348 *
349 * Errors:
350 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The
351 * {@link AImageDecoder} is null or |dataspace| does not correspond to an
352 * {@link ADataSpace} value.
Leon Scroggins III55287002020-10-13 11:30:11 -0400353 * - {@link ANDROID_IMAGE_DECODER_INVALID_STATE}: The animation is not on
354 * the first frame.
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500355 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400356int AImageDecoder_setDataSpace(AImageDecoder* _Nonnull decoder, int32_t dataspace)
357 __INTRODUCED_IN(30);
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500358
359/**
Leon Scroggins III2f984942019-11-22 17:02:23 -0500360 * Specify the output size for a decoded image.
361 *
362 * Future calls to {@link AImageDecoder_decodeImage} will sample or scale the
363 * encoded image to reach the desired size. If a crop rect is set (via
364 * {@link AImageDecoder_setCrop}), it must be contained within the dimensions
365 * specified by width and height, and the output image will be the size of the
366 * crop rect.
367 *
Leon Scroggins III55287002020-10-13 11:30:11 -0400368 * If the encoded image represents an animation, this must be called while on
369 * the first frame (e.g. before calling {@link AImageDecoder_advanceFrame} or
370 * after calling {@link AImageDecoder_rewind}).
371 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700372 * Available since API level 30.
373 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500374 * @param width Width of the output (prior to cropping).
375 * This will affect future calls to
376 * {@link AImageDecoder_getMinimumStride}, which will now return
377 * a value based on this width.
378 * @param height Height of the output (prior to cropping).
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500379 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
380 * indicating the reason for the failure.
381 *
382 * Errors:
383 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The
384 * {@link AImageDecoder} is null.
385 * - {@link ANDROID_IMAGE_DECODER_INVALID_SCALE}: |width| or |height| is <= 0,
Leon Scroggins III55287002020-10-13 11:30:11 -0400386 * the size is too big, any existing crop is not contained by the new image
387 * dimensions, or the scale is incompatible with a previous call to
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500388 * {@link AImageDecoder_setUnpremultipliedRequired}(true).
Leon Scroggins III55287002020-10-13 11:30:11 -0400389 * - {@link ANDROID_IMAGE_DECODER_INVALID_STATE}: The animation is not on
390 * the first frame.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500391 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400392int AImageDecoder_setTargetSize(AImageDecoder* _Nonnull decoder, int32_t width,
393 int32_t height) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500394
Leon Scroggins IIIf27256b2020-01-19 21:13:04 -0500395/**
396 * Compute the dimensions to use for a given sampleSize.
397 *
398 * Although AImageDecoder can scale to an arbitrary target size (see
399 * {@link AImageDecoder_setTargetSize}), some sizes may be more efficient than
400 * others. This computes the most efficient target size to use to reach a
401 * particular sampleSize.
402 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700403 * Available since API level 30.
404 *
Leon Scroggins IIIf27256b2020-01-19 21:13:04 -0500405 * @param sampleSize A subsampling rate of the original image. Must be greater
406 * than or equal to 1. A sampleSize of 2 means to skip every
407 * other pixel/line, resulting in a width and height that are
408 * 1/2 of the original dimensions, with 1/4 the number of
409 * pixels.
410 * @param width Out parameter for the width sampled by sampleSize, and rounded
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500411 * in the direction that the decoder can do most efficiently.
Leon Scroggins IIIf27256b2020-01-19 21:13:04 -0500412 * @param height Out parameter for the height sampled by sampleSize, and rounded
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500413 * in the direction that the decoder can do most efficiently.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500414 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
415 * indicating the reason for the failure.
416 *
417 * Errors:
418 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The
419 * {@link AImageDecoder}, |width| or |height| is null or |sampleSize| is < 1.
Leon Scroggins IIIf27256b2020-01-19 21:13:04 -0500420 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400421int AImageDecoder_computeSampledSize(const AImageDecoder* _Nonnull decoder, int sampleSize,
422 int32_t* _Nonnull width, int32_t* _Nonnull height)
423 __INTRODUCED_IN(30);
Leon Scroggins III3b3700e2020-12-29 11:52:12 -0500424
Leon Scroggins III2f984942019-11-22 17:02:23 -0500425/**
426 * Specify how to crop the output after scaling (if any).
427 *
428 * Future calls to {@link AImageDecoder_decodeImage} will crop their output to
429 * the specified {@link ARect}. Clients will only need to allocate enough memory
430 * for the cropped ARect.
431 *
Leon Scroggins III55287002020-10-13 11:30:11 -0400432 * If the encoded image represents an animation, this must be called while on
433 * the first frame (e.g. before calling {@link AImageDecoder_advanceFrame} or
434 * after calling {@link AImageDecoder_rewind}).
435 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700436 * Available since API level 30.
437 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500438 * @param crop Rectangle describing a crop of the decode. It must be contained inside of
439 * the (possibly scaled, by {@link AImageDecoder_setTargetSize})
440 * image dimensions. This will affect future calls to
441 * {@link AImageDecoder_getMinimumStride}, which will now return a
442 * value based on the width of the crop. An empty ARect -
443 * specifically { 0, 0, 0, 0 } - may be used to remove the cropping
444 * behavior. Any other empty or unsorted ARects will result in
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500445 * returning {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500446 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
447 * indicating the reason for the failure.
448 *
449 * Errors:
450 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The
Leon Scroggins III55287002020-10-13 11:30:11 -0400451 * {@link AImageDecoder} is null, or the crop is not contained by the
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500452 * (possibly scaled) image dimensions.
Leon Scroggins III55287002020-10-13 11:30:11 -0400453 * - {@link ANDROID_IMAGE_DECODER_INVALID_STATE}: The animation is not on
454 * the first frame.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500455 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400456int AImageDecoder_setCrop(AImageDecoder* _Nonnull decoder, ARect crop) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500457
Leon Scroggins III3b3700e2020-12-29 11:52:12 -0500458#endif // __ANDROID_API__ >= 30
459
Leon Scroggins III2f984942019-11-22 17:02:23 -0500460struct AImageDecoderHeaderInfo;
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500461/**
Leon Scroggins III3b3700e2020-12-29 11:52:12 -0500462 * Opaque handle for representing information about the encoded image.
463 *
464 * Introduced in API 30
465 *
466 * Retrieved using {@link AImageDecoder_getHeaderInfo} and passed to methods
467 * like {@link AImageDecoderHeaderInfo_getWidth} and
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500468 * {@link AImageDecoderHeaderInfo_getHeight}.
469 */
Leon Scroggins III2f984942019-11-22 17:02:23 -0500470typedef struct AImageDecoderHeaderInfo AImageDecoderHeaderInfo;
471
Leon Scroggins III3b3700e2020-12-29 11:52:12 -0500472#if __ANDROID_API__ >= 30
473
Leon Scroggins III2f984942019-11-22 17:02:23 -0500474/**
475 * Return an opaque handle for reading header info.
476 *
477 * This is owned by the {@link AImageDecoder} and will be destroyed when the
478 * AImageDecoder is destroyed via {@link AImageDecoder_delete}.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700479 *
480 * Available since API level 30.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500481 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400482const AImageDecoderHeaderInfo* _Nonnull AImageDecoder_getHeaderInfo(
483 const AImageDecoder* _Nonnull decoder) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500484
485/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500486 * Report the native width of the encoded image. This is also the logical
487 * pixel width of the output, unless {@link AImageDecoder_setTargetSize} is
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500488 * used to choose a different size or {@link AImageDecoder_setCrop} is used to
489 * set a crop rect.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700490 *
491 * Available since API level 30.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500492 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400493int32_t AImageDecoderHeaderInfo_getWidth(const AImageDecoderHeaderInfo* _Nonnull)
494 __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500495
496/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500497 * Report the native height of the encoded image. This is also the logical
498 * pixel height of the output, unless {@link AImageDecoder_setTargetSize} is
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500499 * used to choose a different size or {@link AImageDecoder_setCrop} is used to
500 * set a crop rect.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700501 *
502 * Available since API level 30.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500503 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400504int32_t AImageDecoderHeaderInfo_getHeight(const AImageDecoderHeaderInfo* _Nonnull)
505 __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500506
507/**
508 * Report the mimeType of the encoded image.
509 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700510 * Available since API level 30.
511 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500512 * @return a string literal describing the mime type.
513 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400514const char* _Nonnull AImageDecoderHeaderInfo_getMimeType(
515 const AImageDecoderHeaderInfo* _Nonnull) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500516
517/**
Leon Scroggins III5d0445c2020-01-23 09:43:43 -0500518 * Report the {@link AndroidBitmapFormat} the AImageDecoder will decode to
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500519 * by default. {@link AImageDecoder} will try to choose one that is sensible
Leon Scroggins III2f984942019-11-22 17:02:23 -0500520 * for the image and the system. Note that this does not indicate the
521 * encoded format of the image.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700522 *
523 * Available since API level 30.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500524 */
Leon Scroggins III5d0445c2020-01-23 09:43:43 -0500525int32_t AImageDecoderHeaderInfo_getAndroidBitmapFormat(
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400526 const AImageDecoderHeaderInfo* _Nonnull) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500527
528/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500529 * Report how the {@link AImageDecoder} will handle alpha by default. If the image
Leon Scroggins III2f984942019-11-22 17:02:23 -0500530 * contains no alpha (according to its header), this will return
531 * {@link ANDROID_BITMAP_FLAGS_ALPHA_OPAQUE}. If the image may contain alpha,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500532 * this returns {@link ANDROID_BITMAP_FLAGS_ALPHA_PREMUL}, because
533 * {@link AImageDecoder_decodeImage} will premultiply pixels by default.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700534 *
535 * Available since API level 30.
Leon Scroggins IIIac9f7492020-12-28 15:33:13 -0500536 *
537 * Starting in API level 31, an AImageDecoder may contain multiple frames of an
538 * animation, but this method still only reports whether the first frame has
539 * alpha.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500540 */
541int AImageDecoderHeaderInfo_getAlphaFlags(
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400542 const AImageDecoderHeaderInfo* _Nonnull) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500543
544/**
Leon Scroggins III8cee65e2020-01-30 14:20:42 -0500545 * Report the dataspace the AImageDecoder will decode to by default.
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500546 *
Leon Scroggins III8cee65e2020-01-30 14:20:42 -0500547 * By default, {@link AImageDecoder_decodeImage} will not do any color
548 * conversion.
549 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700550 * Available since API level 30.
551 *
Leon Scroggins III8cee65e2020-01-30 14:20:42 -0500552 * @return The {@link ADataSpace} representing the way the colors
553 * are encoded (or {@link ADATASPACE_UNKNOWN} if there is not a
554 * corresponding ADataSpace). This specifies how to interpret the colors
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500555 * in the decoded image, unless {@link AImageDecoder_setDataSpace} is
556 * called to decode to a different ADataSpace.
557 *
558 * Note that ADataSpace only exposes a few values. This may return
Leon Scroggins III8cee65e2020-01-30 14:20:42 -0500559 * {@link ADATASPACE_UNKNOWN}, even for Named ColorSpaces, if they have
560 * no corresponding {@link ADataSpace}.
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500561 */
562int32_t AImageDecoderHeaderInfo_getDataSpace(
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400563 const AImageDecoderHeaderInfo* _Nonnull) __INTRODUCED_IN(30);
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500564
565/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500566 * Return the minimum stride that can be used in
567 * {@link AImageDecoder_decodeImage).
568 *
569 * This stride provides no padding, meaning it will be exactly equal to the
570 * width times the number of bytes per pixel for the {@link AndroidBitmapFormat}
571 * being used.
572 *
573 * If the output is scaled (via {@link AImageDecoder_setTargetSize}) and/or
574 * cropped (via {@link AImageDecoder_setCrop}), this takes those into account.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700575 *
576 * Available since API level 30.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500577 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400578size_t AImageDecoder_getMinimumStride(AImageDecoder* _Nonnull decoder) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500579
580/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500581 * Decode the image into pixels, using the settings of the {@link AImageDecoder}.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500582 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700583 * Available since API level 30.
584 *
Leon Scroggins III1b389712020-10-09 13:12:39 -0400585 * 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 -0500586 * animated image (i.e. GIF, WebP) using new APIs. Internally,
Leon Scroggins III55287002020-10-13 11:30:11 -0400587 * AImageDecoder keeps track of its "current frame" - that is, the frame that
588 * will be decoded by a call to AImageDecoder_decodeImage. At creation time, the
589 * current frame is always the first frame, and multiple calls to this method
590 * will each decode the first frame. {@link AImageDecoder_advanceFrame} advances
591 * the current frame to the following frame, so that future calls to this method
592 * will decode that frame. Some frames may update only part of the image. They
593 * may only update a sub-rectangle (see {@link
594 * AImageDecoderFrameInfo_getFrameRect}), or they may have alpha (see
595 * {@link AImageDecoderFrameInfo_hasAlphaWithinBounds}). In these cases, this
596 * method assumes that the prior frame is still residing in the |pixels| buffer,
597 * decodes only the new portion, and blends it with the buffer. Frames that change
598 * the entire |pixels| buffer are "independent", and do not require the prior
599 * frame to remain in the buffer. The first frame is always independent. A
600 * sophisticated client can use information from the {@link AImageDecoderFrameInfo}
601 * to determine whether other frames are independent, or what frames they rely on.
602 *
603 * If the current frame is marked {@link ANDROID_IMAGE_DECODER_DISPOSE_OP_PREVIOUS},
604 * AImageDecoder_decodeImage will cache the |pixels| buffer prior to decoding
605 * (note: this only happens for the first in a string of consecutive
606 * ANDROID_IMAGE_DECODER_DISPOSE_OP_PREVIOUS frames). After advancing to the
607 * following frame, AImageDecoder_decodeImage will restore the cached buffer
608 * prior to decoding that frame.
609 *
610 * Ignoring timing information, display, etc, a client wishing to decode all
611 * frames of an animated image may conceptually use code like the following:
612 *
613 * while (true) {
614 * int result = AImageDecoder_decodeImage(decoder, pixels, stride, size);
615 * if (result != ANDROID_IMAGE_DECODER_SUCCESS) break;
616 *
617 * // Display or save the image in |pixels|, keeping the buffer intact for
618 * // AImageDecoder to decode the next frame correctly.
619 * Application_viewImage(pixels);
620 *
621 * result = AImageDecoder_advanceFrame(decoder);
622 * if (result != ANDROID_IMAGE_DECODER_SUCCESS) break;
623 * }
Leon Scroggins III1b389712020-10-09 13:12:39 -0400624 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500625 * @param decoder Opaque object representing the decoder.
626 * @param pixels On success, will be filled with the result
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500627 * of the decode. Must be large enough to hold |size| bytes.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500628 * @param stride Width in bytes of a single row. Must be at least
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500629 * {@link AImageDecoder_getMinimumStride} and a multiple of the
630 * bytes per pixel of the {@link AndroidBitmapFormat}.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500631 * @param size Size of the pixel buffer in bytes. Must be at least
632 * stride * (height - 1) +
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500633 * {@link AImageDecoder_getMinimumStride}.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500634 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
635 * indicating the reason for the failure.
636 *
637 * Errors:
638 * - {@link ANDROID_IMAGE_DECODER_INCOMPLETE}: The image was truncated. A
639 * partial image was decoded, and undecoded lines have been initialized to all
640 * zeroes.
641 * - {@link ANDROID_IMAGE_DECODER_ERROR}: The image contained an error. A
642 * partial image was decoded, and undecoded lines have been initialized to all
643 * zeroes.
644 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The {@link AImageDecoder} or
645 * |pixels| is null, the stride is not large enough or not pixel aligned, or
646 * |size| is not large enough.
647 * - {@link ANDROID_IMAGE_DECODER_SEEK_ERROR}: The asset or file descriptor
648 * failed to seek.
649 * - {@link ANDROID_IMAGE_DECODER_INTERNAL_ERROR}: Some other error, like a
650 * failure to allocate memory.
Leon Scroggins III55287002020-10-13 11:30:11 -0400651 * - {@link ANDROID_IMAGE_DECODER_FINISHED}: The input contains no
652 * more frames. No decoding occurred. The client must call
653 * {@link AImageDecoder_rewind} before calling
654 * {@link AImageDecoder_decodeImage} again.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500655 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400656int AImageDecoder_decodeImage(AImageDecoder* _Nonnull decoder,
657 void* _Nonnull pixels, size_t stride,
Leon Scroggins III2f984942019-11-22 17:02:23 -0500658 size_t size) __INTRODUCED_IN(30);
659
660#endif // __ANDROID_API__ >= 30
661
Leon Scroggins III1b389712020-10-09 13:12:39 -0400662#if __ANDROID_API__ >= 31
663
664/**
665 * Return true iff the image is animated - i.e. has multiple frames.
666 *
667 * Introduced in API 31.
668 *
Leon Scroggins III55287002020-10-13 11:30:11 -0400669 * A single frame GIF is considered to *not* be animated. This may require
670 * seeking past the first frame to verify whether there is a following frame.
Leon Scroggins III1b389712020-10-09 13:12:39 -0400671 *
672 * Errors:
673 * - returns false if |decoder| is null.
674 */
675bool AImageDecoder_isAnimated(AImageDecoder* _Nonnull decoder)
676 __INTRODUCED_IN(31);
677
Leon Scroggins III3b3700e2020-12-29 11:52:12 -0500678#endif // __ANDROID_API__ >= 31
679
Leon Scroggins III728a9ac2020-10-12 10:34:05 -0400680enum {
681 /*
682 * Reported by {@link AImageDecoder_getRepeatCount} if the
683 * animation should repeat forever.
Leon Scroggins III3b3700e2020-12-29 11:52:12 -0500684 *
685 * Introduced in API 31
Leon Scroggins III728a9ac2020-10-12 10:34:05 -0400686 */
687 ANDROID_IMAGE_DECODER_INFINITE = INT32_MAX,
688};
689
Leon Scroggins III3b3700e2020-12-29 11:52:12 -0500690#if __ANDROID_API__ >= 31
691
Leon Scroggins III728a9ac2020-10-12 10:34:05 -0400692/**
693 * Report how many times the animation should repeat.
694 *
695 * Introduced in API 31.
696 *
697 * This does not include the first play through. e.g. a repeat
698 * count of 4 means that each frame is played 5 times.
699 *
700 * {@link ANDROID_IMAGE_DECODER_INFINITE} means to repeat forever.
701 *
702 * This may require seeking.
703 *
704 * For non-animated formats, this returns 0. It may return non-zero for
705 * an image with only one frame (i.e. {@link AImageDecoder_isAnimated} returns
706 * false) if the encoded image contains a repeat count.
707 *
708 * @return Number of times to repeat on success or a value
709 * indicating the reason for the failure.
710 *
711 * Errors:
712 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The AImageDecoder
713 * is null.
714 */
715int32_t AImageDecoder_getRepeatCount(AImageDecoder* _Nonnull decoder);
716 __INTRODUCED_IN(31);
717
Leon Scroggins III55287002020-10-13 11:30:11 -0400718/**
719 * Advance to the next frame in the animation.
720 *
721 * Introduced in API 31.
722 *
723 * The AImageDecoder keeps track internally which frame it is ready to decode
724 * (the "current frame"). Initially it is set to decode the first frame, and
725 * each call to {@link AImageDecoder_decodeImage} will continue to decode
726 * the same frame until this method (or {@link AImageDecoder_rewind})
727 * is called.
728 *
729 * Note that this can be used to skip a frame without decoding it. But
730 * some frames depend on (i.e. blend with) prior frames, and
731 * AImageDecoder_decodeImage assumes that the prior frame is in the
732 * |pixels| buffer. In addition, AImageDecoder_decodeImage handles caching and
733 * restoring frames (see {@link ANDROID_IMAGE_DECODER_DISPOSE_OP_PREVIOUS}), so
734 * skipping frames in an image with such frames may not produce the correct
735 * results.
736 *
737 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
738 * indicating the reason for the failure.
739 *
740 * Errors:
741 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The AImageDecoder
742 * represents an image that is not animated (see
743 * {@link AImageDecoder_isAnimated}) or the AImageDecoder is null.
744 * - {@link ANDROID_IMAGE_DECODER_INCOMPLETE}: The input appears
745 * to be truncated. The client must call {@link AImageDecoder_rewind}
746 * before calling {@link AImageDecoder_decodeImage} again.
747 * - {@link ANDROID_IMAGE_DECODER_ERROR}: The input contains an error.
748 * The client must call {@link AImageDecoder_rewind} before
749 * calling {@link AImageDecoder_decodeImage} again.
750 * - {@link ANDROID_IMAGE_DECODER_FINISHED}: The input contains no
751 * more frames. The client must call {@link AImageDecoder_rewind}
752 * before calling {@link AImageDecoder_decodeImage} again.
753 */
754int AImageDecoder_advanceFrame(AImageDecoder* _Nonnull decoder)
755 __INTRODUCED_IN(31);
756
757/**
758 * Return to the beginning of the animation.
759 *
760 * Introduced in API 31.
761 *
762 * After this call, the AImageDecoder will be ready to decode the
763 * first frame of the animation. This can be called after reaching
764 * the end of the animation or an error or in the middle of the
765 * animation.
766 *
767 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
768 * indicating the reason for the failure.
769 *
770 * Errors:
771 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The AImageDecoder
772 * represents an image that is not animated (see
773 * {@link AImageDecoder_isAnimated}) or the AImageDecoder is
774 * null.
775 * - {@link ANDROID_IMAGE_DECODER_SEEK_ERROR}: The asset or file
776 * descriptor failed to seek.
777 */
778int AImageDecoder_rewind(AImageDecoder* _Nonnull decoder)
779 __INTRODUCED_IN(31);
780
Leon Scroggins III1b389712020-10-09 13:12:39 -0400781#endif // __ANDROID_API__ >= 31
782
Leon Scroggins IIIac9f7492020-12-28 15:33:13 -0500783struct AImageDecoderFrameInfo;
784
785/**
786 * Opaque handle to animation information about a single frame.
787 *
788 * Introduced in API 31
789 *
790 * The duration (retrieved with {@link AImageDecoderFrameInfo_getDuration}) is
791 * necessary for clients to display the animation at the proper speed. The other
792 * information is helpful for a client that wants to determine what frames are
793 * independent (or what frames they depend on), but is unnecessary for
794 * a simple client that wants to sequentially display all frames.
795 */
796typedef struct AImageDecoderFrameInfo AImageDecoderFrameInfo;
797
798#if __ANDROID_API__ >= 31
799
800/**
801 * Create an uninitialized AImageDecoderFrameInfo.
802 *
803 * Introduced in API 31.
804 *
805 * This can be passed to {@link AImageDecoder_getFrameInfo} to fill
806 * in information about the current frame. It may be reused.
807 *
808 * Must be deleted with {@link AImageDecoderFrameInfo_delete}.
809 */
810AImageDecoderFrameInfo* _Nullable AImageDecoderFrameInfo_create()
811 __INTRODUCED_IN(31);
812
813/**
814 * Delete an AImageDecoderFrameInfo.
815 *
816 * Introduced in API 31.
817 */
818void AImageDecoderFrameInfo_delete(
819 AImageDecoderFrameInfo* _Nullable info) __INTRODUCED_IN(31);
820
821/**
822 * Fill |info| with information about the current frame.
823 *
824 * Introduced in API 31.
825 *
826 * Initially, this will return information about the first frame.
827 * {@link AImageDecoder_advanceFrame} and
828 * {@link AImageDecoder_rewind} can be used to change which frame
829 * is the current frame.
830 *
831 * If the image only has one frame, this will fill the {@link
832 * AImageDecoderFrameInfo} with the encoded info, if any, or reasonable
833 * defaults.
834 *
835 * @param decoder Opaque object representing the decoder.
836 * @param info Opaque object to hold frame information. On success, will be
837 * filled with information regarding the current frame.
838 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
839 * indicating the reason for the failure.
840 *
841 * Errors:
842 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: One of the parameters is null.
843 * - {@link ANDROID_IMAGE_DECODER_FINISHED}: The input contains no
844 * more frames. The client must call {@link AImageDecoder_rewind} to reset the
845 * current frame to a valid frame (0).
846 */
847int AImageDecoder_getFrameInfo(AImageDecoder* _Nonnull decoder,
848 AImageDecoderFrameInfo* _Nonnull info) __INTRODUCED_IN(31);
849
850/**
851 * Report the number of nanoseconds to show the current frame.
852 *
853 * Introduced in API 31.
854 *
855 * Errors:
856 * - returns 0 if |info| is null.
857 */
858int64_t AImageDecoderFrameInfo_getDuration(
859 const AImageDecoderFrameInfo* _Nonnull info) __INTRODUCED_IN(31);
860
861/**
862 * The rectangle of the image (within 0, 0,
863 * {@link AImageDecoder_getWidth}, {@link AImageDecoder_getHeight})
864 * updated by this frame.
865 *
866 * Introduced in API 31.
867 *
868 * Note that this is unaffected by calls to
869 * {@link AImageDecoder_setTargetSize} or
870 * {@link AImageDecoder_setCrop}.
871 *
872 * A frame may update only part of the image. This will always be
873 * contained by the image’s dimensions.
874 *
875 * This, along with other information in AImageDecoderFrameInfo,
876 * can be useful for determining whether a frame is independent, but
877 * the decoder handles blending frames, so a simple
878 * sequential client does not need this.
879 *
880 * Errors:
881 * - returns an empty ARect if |info| is null.
882 */
883ARect AImageDecoderFrameInfo_getFrameRect(
884 const AImageDecoderFrameInfo* _Nonnull info) __INTRODUCED_IN(31);
885
886/**
887 * Whether the new portion of this frame may contain alpha.
888 *
889 * Introduced in API 31.
890 *
891 * Note that this may differ from whether the composed frame has
892 * alpha. If this frame does not fill the entire image dimensions
893 * (see {@link AImageDecoderFrameInfo_getFrameRect}) or it blends
894 * with an opaque frame, for example, the composed frame’s alpha
895 * may not match. It is also conservative; for example, if a color
896 * index-based frame has a color with alpha but does not use it,
897 * this will still return true.
898 *
899 * This, along with other information in AImageDecoderFrameInfo,
900 * can be useful for determining whether a frame is independent, but
901 * the decoder handles blending frames, so a simple
902 * sequential client does not need this.
903 *
904 * Errors:
905 * - returns false if |info| is null.
906 */
907bool AImageDecoderFrameInfo_hasAlphaWithinBounds(
908 const AImageDecoderFrameInfo* _Nonnull info) __INTRODUCED_IN(31);
909
910#endif // __ANDROID_API__ >= 31
911
912/**
913 * How a frame is “disposed” before showing the next one.
914 *
915 * Introduced in API 31.
916 *
917 * This, along with other information in AImageDecoderFrameInfo,
918 * can be useful for determining whether a frame is independent, but
919 * the decoder handles disposing of frames, so a simple
920 * sequential client does not need this.
921 */
922enum {
923 // No disposal. The following frame will be drawn directly
924 // on top of this one.
925 ANDROID_IMAGE_DECODER_DISPOSE_OP_NONE = 1,
926 // The frame’s rectangle is cleared (by AImageDecoder) before
927 // decoding the next frame.
928 ANDROID_IMAGE_DECODER_DISPOSE_OP_BACKGROUND = 2,
929 // The frame’s rectangle is reverted (by AImageDecoder) to the
930 // prior frame before decoding the next frame.
931 ANDROID_IMAGE_DECODER_DISPOSE_OP_PREVIOUS = 3,
932};
933
934#if __ANDROID_API__ >= 31
935
936/**
937 * Return how this frame is “disposed” before showing the next one.
938 *
939 * Introduced in API 31.
940 *
941 * This, along with other information in AImageDecoderFrameInfo,
942 * can be useful for determining whether a frame is independent, but
943 * the decoder handles disposing of frames, so a simple
944 * sequential client does not need this.
945 *
946 * @return one of:
947 * - {@link ANDROID_IMAGE_DECODER_DISPOSE_OP_NONE}
948 * - {@link ANDROID_IMAGE_DECODER_DISPOSE_OP_BACKGROUND}
949 * - {@link ANDROID_IMAGE_DECODER_DISPOSE_OP_PREVIOUS}
950 * Errors:
951 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER} if |info| is null.
952 */
953int32_t AImageDecoderFrameInfo_getDisposeOp(
954 const AImageDecoderFrameInfo* _Nonnull info) __INTRODUCED_IN(31);
955
956#endif // __ANDROID_API__ >= 31
957
958/**
959 * How a frame is blended with the previous frame.
960 *
961 * Introduced in API 31.
962 *
963 * This, along with other information in AImageDecoderFrameInfo,
964 * can be useful for determining whether a frame is independent, but
965 * the decoder handles blending frames, so a simple
966 * sequential client does not need this.
967 */
968enum {
969 // This frame replaces existing content. This corresponds
970 // to webp’s “do not blend”.
971 ANDROID_IMAGE_DECODER_BLEND_OP_SRC = 1,
972 // This frame blends with the previous frame.
973 ANDROID_IMAGE_DECODER_BLEND_OP_SRC_OVER = 2,
974};
975
976#if __ANDROID_API__ >= 31
977
978/**
979 * Return how this frame is blended with the previous frame.
980 *
981 * Introduced in API 31.
982 *
983 * This, along with other information in AImageDecoderFrameInfo,
984 * can be useful for determining whether a frame is independent, but
985 * the decoder handles blending frames, so a simple
986 * sequential client does not need this.
987 *
988 * @return one of:
989 * - {@link ANDROID_IMAGE_DECODER_BLEND_OP_SRC}
990 * - {@link ANDROID_IMAGE_DECODER_BLEND_OP_SRC_OVER}
991 * Errors:
992 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER} if |info| is null.
993 */
994int32_t AImageDecoderFrameInfo_getBlendOp(
995 const AImageDecoderFrameInfo* _Nonnull info)
996 __INTRODUCED_IN(31);
997
998#endif // __ANDROID_API__ >= 31
999
Leon Scroggins III2f984942019-11-22 17:02:23 -05001000#ifdef __cplusplus
1001}
1002#endif
1003
1004#endif // ANDROID_IMAGE_DECODER_H
1005
1006/** @} */