blob: 27be9c84b1784b89157e7f314dc6d23b70a844a8 [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
68 * specified.
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050069 */
Leon Scroggins III2f984942019-11-22 17:02:23 -050070enum {
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050071 /**
72 * Decoding was successful and complete.
73 */
Leon Scroggins III2f984942019-11-22 17:02:23 -050074 ANDROID_IMAGE_DECODER_SUCCESS = 0,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050075 /**
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -050076 * The input is incomplete.
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050077 */
Leon Scroggins III2f984942019-11-22 17:02:23 -050078 ANDROID_IMAGE_DECODER_INCOMPLETE = -1,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050079 /**
80 * The input contained an error after decoding some lines.
81 */
Leon Scroggins III2f984942019-11-22 17:02:23 -050082 ANDROID_IMAGE_DECODER_ERROR = -2,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050083 /**
84 * Could not convert. For example, attempting to decode an image with
85 * alpha to an opaque format.
86 */
Leon Scroggins III2f984942019-11-22 17:02:23 -050087 ANDROID_IMAGE_DECODER_INVALID_CONVERSION = -3,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050088 /**
89 * The scale is invalid. It may have overflowed, or it may be incompatible
90 * with the current alpha setting.
91 */
Leon Scroggins III2f984942019-11-22 17:02:23 -050092 ANDROID_IMAGE_DECODER_INVALID_SCALE = -4,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050093 /**
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -050094 * Some other parameter is invalid.
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050095 */
Leon Scroggins III2f984942019-11-22 17:02:23 -050096 ANDROID_IMAGE_DECODER_BAD_PARAMETER = -5,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050097 /**
98 * Input was invalid before decoding any pixels.
99 */
Leon Scroggins III2f984942019-11-22 17:02:23 -0500100 ANDROID_IMAGE_DECODER_INVALID_INPUT = -6,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500101 /**
102 * A seek was required and it failed.
103 */
Leon Scroggins III2f984942019-11-22 17:02:23 -0500104 ANDROID_IMAGE_DECODER_SEEK_ERROR = -7,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500105 /**
106 * Some other error. For example, an internal allocation failed.
107 */
Leon Scroggins III2f984942019-11-22 17:02:23 -0500108 ANDROID_IMAGE_DECODER_INTERNAL_ERROR = -8,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500109 /**
110 * AImageDecoder did not recognize the format.
111 */
Leon Scroggins III55287002020-10-13 11:30:11 -0400112 ANDROID_IMAGE_DECODER_UNSUPPORTED_FORMAT = -9,
113 /**
114 * The animation has reached the end.
115 */
116 ANDROID_IMAGE_DECODER_FINISHED = -10,
117 /**
118 * This method cannot be called while the AImageDecoder is in its current
119 * state. For example, various setters (like {@link AImageDecoder_setTargetSize})
120 * can only be called while the AImageDecoder is set to decode the first
121 * frame of an animation. This ensures that any blending and/or restoring
122 * prior frames works correctly.
123 */
124 ANDROID_IMAGE_DECODER_INVALID_STATE = -11,
Leon Scroggins III2f984942019-11-22 17:02:23 -0500125};
126
127struct AImageDecoder;
128
129/**
130 * Opaque handle for decoding images.
131 *
Leon Scroggins III3b3700e2020-12-29 11:52:12 -0500132 * Introduced in API 30
133 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500134 * Create using one of the following:
135 * - {@link AImageDecoder_createFromAAsset}
136 * - {@link AImageDecoder_createFromFd}
137 * - {@link AImageDecoder_createFromBuffer}
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500138 *
139 * After creation, {@link AImageDecoder_getHeaderInfo} can be used to retrieve
140 * information about the encoded image. Other functions, like
141 * {@link AImageDecoder_setTargetSize}, can be used to specify how to decode, and
Leon Scroggins IIIac9f7492020-12-28 15:33:13 -0500142 * {@link AImageDecoder_decodeImage} will decode into client provided memory.
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500143 *
144 * {@link AImageDecoder} objects are NOT thread-safe, and should not be shared across
145 * threads.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500146 */
147typedef struct AImageDecoder AImageDecoder;
148
Leon Scroggins III3b3700e2020-12-29 11:52:12 -0500149#if __ANDROID_API__ >= 30
150
Leon Scroggins III2f984942019-11-22 17:02:23 -0500151/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500152 * Create a new {@link AImageDecoder} from an {@link AAsset}.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500153 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700154 * Available since API level 30.
155 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500156 * @param asset {@link AAsset} containing encoded image data. Client is still
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500157 * responsible for calling {@link AAsset_close} on it, which may be
158 * done after deleting the returned {@link AImageDecoder}.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500159 * @param outDecoder On success (i.e. return value is
160 * {@link ANDROID_IMAGE_DECODER_SUCCESS}), this will be set to
161 * a newly created {@link AImageDecoder}. Caller is
162 * responsible for calling {@link AImageDecoder_delete} on it.
163 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500164 * indicating the reason for the failure.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500165 *
166 * Errors:
167 * - {@link ANDROID_IMAGE_DECODER_INCOMPLETE}: The asset was truncated before
168 * reading the image header.
169 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: One of the parameters is
170 * null.
171 * - {@link ANDROID_IMAGE_DECODER_INVALID_INPUT}: There is an error in the
172 * header.
173 * - {@link ANDROID_IMAGE_DECODER_SEEK_ERROR}: The asset failed to seek.
174 * - {@link ANDROID_IMAGE_DECODER_INTERNAL_ERROR}: Some other error, like a
175 * failure to allocate memory.
176 * - {@link ANDROID_IMAGE_DECODER_UNSUPPORTED_FORMAT}: The format is not
177 * supported.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500178 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400179int AImageDecoder_createFromAAsset(struct AAsset* _Nonnull asset,
180 AImageDecoder* _Nonnull * _Nonnull outDecoder)
Leon Scroggins IIIa9f397b2020-01-27 12:42:56 -0500181 __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500182
183/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500184 * Create a new {@link AImageDecoder} from a file descriptor.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500185 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700186 * Available since API level 30.
187 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500188 * @param fd Seekable, readable, open file descriptor for encoded data.
189 * Client is still responsible for closing it, which may be done
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500190 * after deleting the returned {@link AImageDecoder}.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500191 * @param outDecoder On success (i.e. return value is
192 * {@link ANDROID_IMAGE_DECODER_SUCCESS}), this will be set to
193 * a newly created {@link AImageDecoder}. Caller is
194 * responsible for calling {@link AImageDecoder_delete} on it.
195 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500196 * indicating the reason for the failure.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500197 *
198 * Errors:
199 * - {@link ANDROID_IMAGE_DECODER_INCOMPLETE}: The file was truncated before
200 * reading the image header.
201 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The {@link AImageDecoder} is
202 * null, or |fd| does not represent a valid, seekable file descriptor.
203 * - {@link ANDROID_IMAGE_DECODER_INVALID_INPUT}: There is an error in the
204 * header.
205 * - {@link ANDROID_IMAGE_DECODER_SEEK_ERROR}: The descriptor failed to seek.
206 * - {@link ANDROID_IMAGE_DECODER_INTERNAL_ERROR}: Some other error, like a
207 * failure to allocate memory.
208 * - {@link ANDROID_IMAGE_DECODER_UNSUPPORTED_FORMAT}: The format is not
209 * supported.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500210 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400211int AImageDecoder_createFromFd(int fd, AImageDecoder* _Nonnull * _Nonnull outDecoder)
212 __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500213
214/**
215 * Create a new AImageDecoder from a buffer.
216 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700217 * Available since API level 30.
218 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500219 * @param buffer Pointer to encoded data. Must be valid for the entire time
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500220 * the {@link AImageDecoder} is used.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500221 * @param length Byte length of buffer.
222 * @param outDecoder On success (i.e. return value is
223 * {@link ANDROID_IMAGE_DECODER_SUCCESS}), this will be set to
224 * a newly created {@link AImageDecoder}. Caller is
225 * responsible for calling {@link AImageDecoder_delete} on it.
226 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500227 * indicating the reason for the failure.
228 *
229 * Errors:
230 * - {@link ANDROID_IMAGE_DECODER_INCOMPLETE}: The encoded image was truncated before
231 * reading the image header.
232 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: One of the parameters is
233 * invalid.
234 * - {@link ANDROID_IMAGE_DECODER_INVALID_INPUT}: There is an error in the
235 * header.
236 * - {@link ANDROID_IMAGE_DECODER_INTERNAL_ERROR}: Some other error, like a
237 * failure to allocate memory.
238 * - {@link ANDROID_IMAGE_DECODER_UNSUPPORTED_FORMAT}: The format is not
239 * supported.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500240 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400241int AImageDecoder_createFromBuffer(const void* _Nonnull buffer, size_t length,
242 AImageDecoder* _Nonnull * _Nonnull outDecoder)
243 __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500244
245/**
246 * Delete the AImageDecoder.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700247 *
248 * Available since API level 30.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500249 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400250void AImageDecoder_delete(AImageDecoder* _Nonnull decoder) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500251
252/**
253 * Choose the desired output format.
254 *
Leon Scroggins III55287002020-10-13 11:30:11 -0400255 * If the encoded image represents an animation, this must be called while on
256 * the first frame (e.g. before calling {@link AImageDecoder_advanceFrame} or
257 * after calling {@link AImageDecoder_rewind}).
258 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700259 * Available since API level 30.
260 *
Leon Scroggins III5d0445c2020-01-23 09:43:43 -0500261 * @param format {@link AndroidBitmapFormat} to use for the output.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500262 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
263 * indicating the reason for the failure. On failure, the
264 * {@link AImageDecoder} uses the format it was already planning
265 * to use (either its default or a previously successful setting
266 * from this function).
267 *
268 * Errors:
269 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The
270 * {@link AImageDecoder} is null or |format| does not correspond to an
271 * {@link AndroidBitmapFormat}.
272 * - {@link ANDROID_IMAGE_DECODER_INVALID_CONVERSION}: The
273 * {@link AndroidBitmapFormat} is incompatible with the image.
Leon Scroggins III55287002020-10-13 11:30:11 -0400274 * - {@link ANDROID_IMAGE_DECODER_INVALID_STATE}: The animation is not on
275 * the first frame.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500276 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400277int AImageDecoder_setAndroidBitmapFormat(AImageDecoder* _Nonnull decoder,
Leon Scroggins III2f984942019-11-22 17:02:23 -0500278 int32_t format) __INTRODUCED_IN(30);
279
Leon Scroggins III1be112f2020-01-15 04:26:44 -0500280/**
281 * Specify whether the output's pixels should be unpremultiplied.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500282 *
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500283 * By default, {@link AImageDecoder_decodeImage} will premultiply the pixels, if they have alpha.
284 * Pass true to this method to leave them unpremultiplied. This has no effect on an
Leon Scroggins III1be112f2020-01-15 04:26:44 -0500285 * opaque image.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500286 *
Leon Scroggins III55287002020-10-13 11:30:11 -0400287 * If the encoded image represents an animation, this must be called while on
288 * the first frame (e.g. before calling {@link AImageDecoder_advanceFrame} or
289 * after calling {@link AImageDecoder_rewind}).
290 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700291 * Available since API level 30.
292 *
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500293 * @param unpremultipliedRequired Pass true to leave the pixels unpremultiplied.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500294 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
295 * indicating the reason for the failure.
296 *
297 * Errors:
298 * - {@link ANDROID_IMAGE_DECODER_INVALID_CONVERSION}: Unpremultiplied is not
299 * possible due to an existing scale set by
300 * {@link AImageDecoder_setTargetSize}.
301 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The
302 * {@link AImageDecoder} is null.
Leon Scroggins III55287002020-10-13 11:30:11 -0400303 * - {@link ANDROID_IMAGE_DECODER_INVALID_STATE}: The animation is not on
304 * the first frame.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500305 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400306int AImageDecoder_setUnpremultipliedRequired(AImageDecoder* _Nonnull decoder,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500307 bool unpremultipliedRequired) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500308
309/**
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500310 * Choose the dataspace for the output.
311 *
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500312 * Ignored by {@link ANDROID_BITMAP_FORMAT_A_8}, which does not support
313 * an {@link ADataSpace}.
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500314 *
Leon Scroggins III55287002020-10-13 11:30:11 -0400315 * If the encoded image represents an animation, this must be called while on
316 * the first frame (e.g. before calling {@link AImageDecoder_advanceFrame} or
317 * after calling {@link AImageDecoder_rewind}).
318 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700319 * Available since API level 30.
320 *
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500321 * @param dataspace The {@link ADataSpace} to decode into. An ADataSpace
322 * specifies how to interpret the colors. By default,
323 * AImageDecoder will decode into the ADataSpace specified by
324 * {@link AImageDecoderHeaderInfo_getDataSpace}. If this
325 * parameter is set to a different ADataSpace, AImageDecoder
326 * will transform the output into the specified ADataSpace.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500327 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
328 * indicating the reason for the failure.
329 *
330 * Errors:
331 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The
332 * {@link AImageDecoder} is null or |dataspace| does not correspond to an
333 * {@link ADataSpace} value.
Leon Scroggins III55287002020-10-13 11:30:11 -0400334 * - {@link ANDROID_IMAGE_DECODER_INVALID_STATE}: The animation is not on
335 * the first frame.
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500336 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400337int AImageDecoder_setDataSpace(AImageDecoder* _Nonnull decoder, int32_t dataspace)
338 __INTRODUCED_IN(30);
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500339
340/**
Leon Scroggins III2f984942019-11-22 17:02:23 -0500341 * Specify the output size for a decoded image.
342 *
343 * Future calls to {@link AImageDecoder_decodeImage} will sample or scale the
344 * encoded image to reach the desired size. If a crop rect is set (via
345 * {@link AImageDecoder_setCrop}), it must be contained within the dimensions
346 * specified by width and height, and the output image will be the size of the
347 * crop rect.
348 *
Leon Scroggins III55287002020-10-13 11:30:11 -0400349 * If the encoded image represents an animation, this must be called while on
350 * the first frame (e.g. before calling {@link AImageDecoder_advanceFrame} or
351 * after calling {@link AImageDecoder_rewind}).
352 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700353 * Available since API level 30.
354 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500355 * @param width Width of the output (prior to cropping).
356 * This will affect future calls to
357 * {@link AImageDecoder_getMinimumStride}, which will now return
358 * a value based on this width.
359 * @param height Height of the output (prior to cropping).
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500360 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
361 * indicating the reason for the failure.
362 *
363 * Errors:
364 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The
365 * {@link AImageDecoder} is null.
366 * - {@link ANDROID_IMAGE_DECODER_INVALID_SCALE}: |width| or |height| is <= 0,
Leon Scroggins III55287002020-10-13 11:30:11 -0400367 * the size is too big, any existing crop is not contained by the new image
368 * dimensions, or the scale is incompatible with a previous call to
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500369 * {@link AImageDecoder_setUnpremultipliedRequired}(true).
Leon Scroggins III55287002020-10-13 11:30:11 -0400370 * - {@link ANDROID_IMAGE_DECODER_INVALID_STATE}: The animation is not on
371 * the first frame.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500372 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400373int AImageDecoder_setTargetSize(AImageDecoder* _Nonnull decoder, int32_t width,
374 int32_t height) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500375
Leon Scroggins IIIf27256b2020-01-19 21:13:04 -0500376/**
377 * Compute the dimensions to use for a given sampleSize.
378 *
379 * Although AImageDecoder can scale to an arbitrary target size (see
380 * {@link AImageDecoder_setTargetSize}), some sizes may be more efficient than
381 * others. This computes the most efficient target size to use to reach a
382 * particular sampleSize.
383 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700384 * Available since API level 30.
385 *
Leon Scroggins IIIf27256b2020-01-19 21:13:04 -0500386 * @param sampleSize A subsampling rate of the original image. Must be greater
387 * than or equal to 1. A sampleSize of 2 means to skip every
388 * other pixel/line, resulting in a width and height that are
389 * 1/2 of the original dimensions, with 1/4 the number of
390 * pixels.
391 * @param width Out parameter for the width sampled by sampleSize, and rounded
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500392 * in the direction that the decoder can do most efficiently.
Leon Scroggins IIIf27256b2020-01-19 21:13:04 -0500393 * @param height Out parameter for the height sampled by sampleSize, and rounded
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500394 * in the direction that the decoder can do most efficiently.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500395 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
396 * indicating the reason for the failure.
397 *
398 * Errors:
399 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The
400 * {@link AImageDecoder}, |width| or |height| is null or |sampleSize| is < 1.
Leon Scroggins IIIf27256b2020-01-19 21:13:04 -0500401 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400402int AImageDecoder_computeSampledSize(const AImageDecoder* _Nonnull decoder, int sampleSize,
403 int32_t* _Nonnull width, int32_t* _Nonnull height)
404 __INTRODUCED_IN(30);
Leon Scroggins III3b3700e2020-12-29 11:52:12 -0500405
Leon Scroggins III2f984942019-11-22 17:02:23 -0500406/**
407 * Specify how to crop the output after scaling (if any).
408 *
409 * Future calls to {@link AImageDecoder_decodeImage} will crop their output to
410 * the specified {@link ARect}. Clients will only need to allocate enough memory
411 * for the cropped ARect.
412 *
Leon Scroggins III55287002020-10-13 11:30:11 -0400413 * If the encoded image represents an animation, this must be called while on
414 * the first frame (e.g. before calling {@link AImageDecoder_advanceFrame} or
415 * after calling {@link AImageDecoder_rewind}).
416 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700417 * Available since API level 30.
418 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500419 * @param crop Rectangle describing a crop of the decode. It must be contained inside of
420 * the (possibly scaled, by {@link AImageDecoder_setTargetSize})
421 * image dimensions. This will affect future calls to
422 * {@link AImageDecoder_getMinimumStride}, which will now return a
423 * value based on the width of the crop. An empty ARect -
424 * specifically { 0, 0, 0, 0 } - may be used to remove the cropping
425 * behavior. Any other empty or unsorted ARects will result in
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500426 * returning {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500427 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
428 * indicating the reason for the failure.
429 *
430 * Errors:
431 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The
Leon Scroggins III55287002020-10-13 11:30:11 -0400432 * {@link AImageDecoder} is null, or the crop is not contained by the
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500433 * (possibly scaled) image dimensions.
Leon Scroggins III55287002020-10-13 11:30:11 -0400434 * - {@link ANDROID_IMAGE_DECODER_INVALID_STATE}: The animation is not on
435 * the first frame.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500436 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400437int AImageDecoder_setCrop(AImageDecoder* _Nonnull decoder, ARect crop) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500438
Leon Scroggins III3b3700e2020-12-29 11:52:12 -0500439#endif // __ANDROID_API__ >= 30
440
Leon Scroggins III2f984942019-11-22 17:02:23 -0500441struct AImageDecoderHeaderInfo;
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500442/**
Leon Scroggins III3b3700e2020-12-29 11:52:12 -0500443 * Opaque handle for representing information about the encoded image.
444 *
445 * Introduced in API 30
446 *
447 * Retrieved using {@link AImageDecoder_getHeaderInfo} and passed to methods
448 * like {@link AImageDecoderHeaderInfo_getWidth} and
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500449 * {@link AImageDecoderHeaderInfo_getHeight}.
450 */
Leon Scroggins III2f984942019-11-22 17:02:23 -0500451typedef struct AImageDecoderHeaderInfo AImageDecoderHeaderInfo;
452
Leon Scroggins III3b3700e2020-12-29 11:52:12 -0500453#if __ANDROID_API__ >= 30
454
Leon Scroggins III2f984942019-11-22 17:02:23 -0500455/**
456 * Return an opaque handle for reading header info.
457 *
458 * This is owned by the {@link AImageDecoder} and will be destroyed when the
459 * AImageDecoder is destroyed via {@link AImageDecoder_delete}.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700460 *
461 * Available since API level 30.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500462 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400463const AImageDecoderHeaderInfo* _Nonnull AImageDecoder_getHeaderInfo(
464 const AImageDecoder* _Nonnull decoder) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500465
466/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500467 * Report the native width of the encoded image. This is also the logical
468 * pixel width of the output, unless {@link AImageDecoder_setTargetSize} is
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500469 * used to choose a different size or {@link AImageDecoder_setCrop} is used to
470 * set a crop rect.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700471 *
472 * Available since API level 30.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500473 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400474int32_t AImageDecoderHeaderInfo_getWidth(const AImageDecoderHeaderInfo* _Nonnull)
475 __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500476
477/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500478 * Report the native height of the encoded image. This is also the logical
479 * pixel height of the output, unless {@link AImageDecoder_setTargetSize} is
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500480 * used to choose a different size or {@link AImageDecoder_setCrop} is used to
481 * set a crop rect.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700482 *
483 * Available since API level 30.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500484 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400485int32_t AImageDecoderHeaderInfo_getHeight(const AImageDecoderHeaderInfo* _Nonnull)
486 __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500487
488/**
489 * Report the mimeType of the encoded image.
490 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700491 * Available since API level 30.
492 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500493 * @return a string literal describing the mime type.
494 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400495const char* _Nonnull AImageDecoderHeaderInfo_getMimeType(
496 const AImageDecoderHeaderInfo* _Nonnull) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500497
498/**
Leon Scroggins III5d0445c2020-01-23 09:43:43 -0500499 * Report the {@link AndroidBitmapFormat} the AImageDecoder will decode to
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500500 * by default. {@link AImageDecoder} will try to choose one that is sensible
Leon Scroggins III2f984942019-11-22 17:02:23 -0500501 * for the image and the system. Note that this does not indicate the
502 * encoded format of the image.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700503 *
504 * Available since API level 30.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500505 */
Leon Scroggins III5d0445c2020-01-23 09:43:43 -0500506int32_t AImageDecoderHeaderInfo_getAndroidBitmapFormat(
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400507 const AImageDecoderHeaderInfo* _Nonnull) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500508
509/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500510 * Report how the {@link AImageDecoder} will handle alpha by default. If the image
Leon Scroggins III2f984942019-11-22 17:02:23 -0500511 * contains no alpha (according to its header), this will return
512 * {@link ANDROID_BITMAP_FLAGS_ALPHA_OPAQUE}. If the image may contain alpha,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500513 * this returns {@link ANDROID_BITMAP_FLAGS_ALPHA_PREMUL}, because
514 * {@link AImageDecoder_decodeImage} will premultiply pixels by default.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700515 *
516 * Available since API level 30.
Leon Scroggins IIIac9f7492020-12-28 15:33:13 -0500517 *
518 * Starting in API level 31, an AImageDecoder may contain multiple frames of an
519 * animation, but this method still only reports whether the first frame has
520 * alpha.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500521 */
522int AImageDecoderHeaderInfo_getAlphaFlags(
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400523 const AImageDecoderHeaderInfo* _Nonnull) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500524
525/**
Leon Scroggins III8cee65e2020-01-30 14:20:42 -0500526 * Report the dataspace the AImageDecoder will decode to by default.
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500527 *
Leon Scroggins III8cee65e2020-01-30 14:20:42 -0500528 * By default, {@link AImageDecoder_decodeImage} will not do any color
529 * conversion.
530 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700531 * Available since API level 30.
532 *
Leon Scroggins III8cee65e2020-01-30 14:20:42 -0500533 * @return The {@link ADataSpace} representing the way the colors
534 * are encoded (or {@link ADATASPACE_UNKNOWN} if there is not a
535 * corresponding ADataSpace). This specifies how to interpret the colors
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500536 * in the decoded image, unless {@link AImageDecoder_setDataSpace} is
537 * called to decode to a different ADataSpace.
538 *
539 * Note that ADataSpace only exposes a few values. This may return
Leon Scroggins III8cee65e2020-01-30 14:20:42 -0500540 * {@link ADATASPACE_UNKNOWN}, even for Named ColorSpaces, if they have
541 * no corresponding {@link ADataSpace}.
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500542 */
543int32_t AImageDecoderHeaderInfo_getDataSpace(
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400544 const AImageDecoderHeaderInfo* _Nonnull) __INTRODUCED_IN(30);
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500545
546/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500547 * Return the minimum stride that can be used in
548 * {@link AImageDecoder_decodeImage).
549 *
550 * This stride provides no padding, meaning it will be exactly equal to the
551 * width times the number of bytes per pixel for the {@link AndroidBitmapFormat}
552 * being used.
553 *
554 * If the output is scaled (via {@link AImageDecoder_setTargetSize}) and/or
555 * cropped (via {@link AImageDecoder_setCrop}), this takes those into account.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700556 *
557 * Available since API level 30.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500558 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400559size_t AImageDecoder_getMinimumStride(AImageDecoder* _Nonnull decoder) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500560
561/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500562 * Decode the image into pixels, using the settings of the {@link AImageDecoder}.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500563 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700564 * Available since API level 30.
565 *
Leon Scroggins III1b389712020-10-09 13:12:39 -0400566 * Starting in API level 31, it can be used to decode all of the frames of an
Leon Scroggins III55287002020-10-13 11:30:11 -0400567 * animated image (i.e. GIF, WebP, HEIF) using new APIs. Internally,
568 * AImageDecoder keeps track of its "current frame" - that is, the frame that
569 * will be decoded by a call to AImageDecoder_decodeImage. At creation time, the
570 * current frame is always the first frame, and multiple calls to this method
571 * will each decode the first frame. {@link AImageDecoder_advanceFrame} advances
572 * the current frame to the following frame, so that future calls to this method
573 * will decode that frame. Some frames may update only part of the image. They
574 * may only update a sub-rectangle (see {@link
575 * AImageDecoderFrameInfo_getFrameRect}), or they may have alpha (see
576 * {@link AImageDecoderFrameInfo_hasAlphaWithinBounds}). In these cases, this
577 * method assumes that the prior frame is still residing in the |pixels| buffer,
578 * decodes only the new portion, and blends it with the buffer. Frames that change
579 * the entire |pixels| buffer are "independent", and do not require the prior
580 * frame to remain in the buffer. The first frame is always independent. A
581 * sophisticated client can use information from the {@link AImageDecoderFrameInfo}
582 * to determine whether other frames are independent, or what frames they rely on.
583 *
584 * If the current frame is marked {@link ANDROID_IMAGE_DECODER_DISPOSE_OP_PREVIOUS},
585 * AImageDecoder_decodeImage will cache the |pixels| buffer prior to decoding
586 * (note: this only happens for the first in a string of consecutive
587 * ANDROID_IMAGE_DECODER_DISPOSE_OP_PREVIOUS frames). After advancing to the
588 * following frame, AImageDecoder_decodeImage will restore the cached buffer
589 * prior to decoding that frame.
590 *
591 * Ignoring timing information, display, etc, a client wishing to decode all
592 * frames of an animated image may conceptually use code like the following:
593 *
594 * while (true) {
595 * int result = AImageDecoder_decodeImage(decoder, pixels, stride, size);
596 * if (result != ANDROID_IMAGE_DECODER_SUCCESS) break;
597 *
598 * // Display or save the image in |pixels|, keeping the buffer intact for
599 * // AImageDecoder to decode the next frame correctly.
600 * Application_viewImage(pixels);
601 *
602 * result = AImageDecoder_advanceFrame(decoder);
603 * if (result != ANDROID_IMAGE_DECODER_SUCCESS) break;
604 * }
Leon Scroggins III1b389712020-10-09 13:12:39 -0400605 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500606 * @param decoder Opaque object representing the decoder.
607 * @param pixels On success, will be filled with the result
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500608 * of the decode. Must be large enough to hold |size| bytes.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500609 * @param stride Width in bytes of a single row. Must be at least
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500610 * {@link AImageDecoder_getMinimumStride} and a multiple of the
611 * bytes per pixel of the {@link AndroidBitmapFormat}.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500612 * @param size Size of the pixel buffer in bytes. Must be at least
613 * stride * (height - 1) +
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500614 * {@link AImageDecoder_getMinimumStride}.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500615 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
616 * indicating the reason for the failure.
617 *
618 * Errors:
619 * - {@link ANDROID_IMAGE_DECODER_INCOMPLETE}: The image was truncated. A
620 * partial image was decoded, and undecoded lines have been initialized to all
621 * zeroes.
622 * - {@link ANDROID_IMAGE_DECODER_ERROR}: The image contained an error. A
623 * partial image was decoded, and undecoded lines have been initialized to all
624 * zeroes.
625 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The {@link AImageDecoder} or
626 * |pixels| is null, the stride is not large enough or not pixel aligned, or
627 * |size| is not large enough.
628 * - {@link ANDROID_IMAGE_DECODER_SEEK_ERROR}: The asset or file descriptor
629 * failed to seek.
630 * - {@link ANDROID_IMAGE_DECODER_INTERNAL_ERROR}: Some other error, like a
631 * failure to allocate memory.
Leon Scroggins III55287002020-10-13 11:30:11 -0400632 * - {@link ANDROID_IMAGE_DECODER_FINISHED}: The input contains no
633 * more frames. No decoding occurred. The client must call
634 * {@link AImageDecoder_rewind} before calling
635 * {@link AImageDecoder_decodeImage} again.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500636 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400637int AImageDecoder_decodeImage(AImageDecoder* _Nonnull decoder,
638 void* _Nonnull pixels, size_t stride,
Leon Scroggins III2f984942019-11-22 17:02:23 -0500639 size_t size) __INTRODUCED_IN(30);
640
641#endif // __ANDROID_API__ >= 30
642
Leon Scroggins III1b389712020-10-09 13:12:39 -0400643#if __ANDROID_API__ >= 31
644
645/**
646 * Return true iff the image is animated - i.e. has multiple frames.
647 *
648 * Introduced in API 31.
649 *
Leon Scroggins III55287002020-10-13 11:30:11 -0400650 * A single frame GIF is considered to *not* be animated. This may require
651 * seeking past the first frame to verify whether there is a following frame.
Leon Scroggins III1b389712020-10-09 13:12:39 -0400652 *
653 * Errors:
654 * - returns false if |decoder| is null.
655 */
656bool AImageDecoder_isAnimated(AImageDecoder* _Nonnull decoder)
657 __INTRODUCED_IN(31);
658
Leon Scroggins III3b3700e2020-12-29 11:52:12 -0500659#endif // __ANDROID_API__ >= 31
660
Leon Scroggins III728a9ac2020-10-12 10:34:05 -0400661enum {
662 /*
663 * Reported by {@link AImageDecoder_getRepeatCount} if the
664 * animation should repeat forever.
Leon Scroggins III3b3700e2020-12-29 11:52:12 -0500665 *
666 * Introduced in API 31
Leon Scroggins III728a9ac2020-10-12 10:34:05 -0400667 */
668 ANDROID_IMAGE_DECODER_INFINITE = INT32_MAX,
669};
670
Leon Scroggins III3b3700e2020-12-29 11:52:12 -0500671#if __ANDROID_API__ >= 31
672
Leon Scroggins III728a9ac2020-10-12 10:34:05 -0400673/**
674 * Report how many times the animation should repeat.
675 *
676 * Introduced in API 31.
677 *
678 * This does not include the first play through. e.g. a repeat
679 * count of 4 means that each frame is played 5 times.
680 *
681 * {@link ANDROID_IMAGE_DECODER_INFINITE} means to repeat forever.
682 *
683 * This may require seeking.
684 *
685 * For non-animated formats, this returns 0. It may return non-zero for
686 * an image with only one frame (i.e. {@link AImageDecoder_isAnimated} returns
687 * false) if the encoded image contains a repeat count.
688 *
689 * @return Number of times to repeat on success or a value
690 * indicating the reason for the failure.
691 *
692 * Errors:
693 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The AImageDecoder
694 * is null.
695 */
696int32_t AImageDecoder_getRepeatCount(AImageDecoder* _Nonnull decoder);
697 __INTRODUCED_IN(31);
698
Leon Scroggins III55287002020-10-13 11:30:11 -0400699/**
700 * Advance to the next frame in the animation.
701 *
702 * Introduced in API 31.
703 *
704 * The AImageDecoder keeps track internally which frame it is ready to decode
705 * (the "current frame"). Initially it is set to decode the first frame, and
706 * each call to {@link AImageDecoder_decodeImage} will continue to decode
707 * the same frame until this method (or {@link AImageDecoder_rewind})
708 * is called.
709 *
710 * Note that this can be used to skip a frame without decoding it. But
711 * some frames depend on (i.e. blend with) prior frames, and
712 * AImageDecoder_decodeImage assumes that the prior frame is in the
713 * |pixels| buffer. In addition, AImageDecoder_decodeImage handles caching and
714 * restoring frames (see {@link ANDROID_IMAGE_DECODER_DISPOSE_OP_PREVIOUS}), so
715 * skipping frames in an image with such frames may not produce the correct
716 * results.
717 *
718 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
719 * indicating the reason for the failure.
720 *
721 * Errors:
722 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The AImageDecoder
723 * represents an image that is not animated (see
724 * {@link AImageDecoder_isAnimated}) or the AImageDecoder is null.
725 * - {@link ANDROID_IMAGE_DECODER_INCOMPLETE}: The input appears
726 * to be truncated. The client must call {@link AImageDecoder_rewind}
727 * before calling {@link AImageDecoder_decodeImage} again.
728 * - {@link ANDROID_IMAGE_DECODER_ERROR}: The input contains an error.
729 * The client must call {@link AImageDecoder_rewind} before
730 * calling {@link AImageDecoder_decodeImage} again.
731 * - {@link ANDROID_IMAGE_DECODER_FINISHED}: The input contains no
732 * more frames. The client must call {@link AImageDecoder_rewind}
733 * before calling {@link AImageDecoder_decodeImage} again.
734 */
735int AImageDecoder_advanceFrame(AImageDecoder* _Nonnull decoder)
736 __INTRODUCED_IN(31);
737
738/**
739 * Return to the beginning of the animation.
740 *
741 * Introduced in API 31.
742 *
743 * After this call, the AImageDecoder will be ready to decode the
744 * first frame of the animation. This can be called after reaching
745 * the end of the animation or an error or in the middle of the
746 * animation.
747 *
748 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
749 * indicating the reason for the failure.
750 *
751 * Errors:
752 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The AImageDecoder
753 * represents an image that is not animated (see
754 * {@link AImageDecoder_isAnimated}) or the AImageDecoder is
755 * null.
756 * - {@link ANDROID_IMAGE_DECODER_SEEK_ERROR}: The asset or file
757 * descriptor failed to seek.
758 */
759int AImageDecoder_rewind(AImageDecoder* _Nonnull decoder)
760 __INTRODUCED_IN(31);
761
Leon Scroggins III1b389712020-10-09 13:12:39 -0400762#endif // __ANDROID_API__ >= 31
763
Leon Scroggins IIIac9f7492020-12-28 15:33:13 -0500764struct AImageDecoderFrameInfo;
765
766/**
767 * Opaque handle to animation information about a single frame.
768 *
769 * Introduced in API 31
770 *
771 * The duration (retrieved with {@link AImageDecoderFrameInfo_getDuration}) is
772 * necessary for clients to display the animation at the proper speed. The other
773 * information is helpful for a client that wants to determine what frames are
774 * independent (or what frames they depend on), but is unnecessary for
775 * a simple client that wants to sequentially display all frames.
776 */
777typedef struct AImageDecoderFrameInfo AImageDecoderFrameInfo;
778
779#if __ANDROID_API__ >= 31
780
781/**
782 * Create an uninitialized AImageDecoderFrameInfo.
783 *
784 * Introduced in API 31.
785 *
786 * This can be passed to {@link AImageDecoder_getFrameInfo} to fill
787 * in information about the current frame. It may be reused.
788 *
789 * Must be deleted with {@link AImageDecoderFrameInfo_delete}.
790 */
791AImageDecoderFrameInfo* _Nullable AImageDecoderFrameInfo_create()
792 __INTRODUCED_IN(31);
793
794/**
795 * Delete an AImageDecoderFrameInfo.
796 *
797 * Introduced in API 31.
798 */
799void AImageDecoderFrameInfo_delete(
800 AImageDecoderFrameInfo* _Nullable info) __INTRODUCED_IN(31);
801
802/**
803 * Fill |info| with information about the current frame.
804 *
805 * Introduced in API 31.
806 *
807 * Initially, this will return information about the first frame.
808 * {@link AImageDecoder_advanceFrame} and
809 * {@link AImageDecoder_rewind} can be used to change which frame
810 * is the current frame.
811 *
812 * If the image only has one frame, this will fill the {@link
813 * AImageDecoderFrameInfo} with the encoded info, if any, or reasonable
814 * defaults.
815 *
816 * @param decoder Opaque object representing the decoder.
817 * @param info Opaque object to hold frame information. On success, will be
818 * filled with information regarding the current frame.
819 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
820 * indicating the reason for the failure.
821 *
822 * Errors:
823 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: One of the parameters is null.
824 * - {@link ANDROID_IMAGE_DECODER_FINISHED}: The input contains no
825 * more frames. The client must call {@link AImageDecoder_rewind} to reset the
826 * current frame to a valid frame (0).
827 */
828int AImageDecoder_getFrameInfo(AImageDecoder* _Nonnull decoder,
829 AImageDecoderFrameInfo* _Nonnull info) __INTRODUCED_IN(31);
830
831/**
832 * Report the number of nanoseconds to show the current frame.
833 *
834 * Introduced in API 31.
835 *
836 * Errors:
837 * - returns 0 if |info| is null.
838 */
839int64_t AImageDecoderFrameInfo_getDuration(
840 const AImageDecoderFrameInfo* _Nonnull info) __INTRODUCED_IN(31);
841
842/**
843 * The rectangle of the image (within 0, 0,
844 * {@link AImageDecoder_getWidth}, {@link AImageDecoder_getHeight})
845 * updated by this frame.
846 *
847 * Introduced in API 31.
848 *
849 * Note that this is unaffected by calls to
850 * {@link AImageDecoder_setTargetSize} or
851 * {@link AImageDecoder_setCrop}.
852 *
853 * A frame may update only part of the image. This will always be
854 * contained by the image’s dimensions.
855 *
856 * This, along with other information in AImageDecoderFrameInfo,
857 * can be useful for determining whether a frame is independent, but
858 * the decoder handles blending frames, so a simple
859 * sequential client does not need this.
860 *
861 * Errors:
862 * - returns an empty ARect if |info| is null.
863 */
864ARect AImageDecoderFrameInfo_getFrameRect(
865 const AImageDecoderFrameInfo* _Nonnull info) __INTRODUCED_IN(31);
866
867/**
868 * Whether the new portion of this frame may contain alpha.
869 *
870 * Introduced in API 31.
871 *
872 * Note that this may differ from whether the composed frame has
873 * alpha. If this frame does not fill the entire image dimensions
874 * (see {@link AImageDecoderFrameInfo_getFrameRect}) or it blends
875 * with an opaque frame, for example, the composed frame’s alpha
876 * may not match. It is also conservative; for example, if a color
877 * index-based frame has a color with alpha but does not use it,
878 * this will still return true.
879 *
880 * This, along with other information in AImageDecoderFrameInfo,
881 * can be useful for determining whether a frame is independent, but
882 * the decoder handles blending frames, so a simple
883 * sequential client does not need this.
884 *
885 * Errors:
886 * - returns false if |info| is null.
887 */
888bool AImageDecoderFrameInfo_hasAlphaWithinBounds(
889 const AImageDecoderFrameInfo* _Nonnull info) __INTRODUCED_IN(31);
890
891#endif // __ANDROID_API__ >= 31
892
893/**
894 * How a frame is “disposed” before showing the next one.
895 *
896 * Introduced in API 31.
897 *
898 * This, along with other information in AImageDecoderFrameInfo,
899 * can be useful for determining whether a frame is independent, but
900 * the decoder handles disposing of frames, so a simple
901 * sequential client does not need this.
902 */
903enum {
904 // No disposal. The following frame will be drawn directly
905 // on top of this one.
906 ANDROID_IMAGE_DECODER_DISPOSE_OP_NONE = 1,
907 // The frame’s rectangle is cleared (by AImageDecoder) before
908 // decoding the next frame.
909 ANDROID_IMAGE_DECODER_DISPOSE_OP_BACKGROUND = 2,
910 // The frame’s rectangle is reverted (by AImageDecoder) to the
911 // prior frame before decoding the next frame.
912 ANDROID_IMAGE_DECODER_DISPOSE_OP_PREVIOUS = 3,
913};
914
915#if __ANDROID_API__ >= 31
916
917/**
918 * Return how this frame is “disposed” before showing the next one.
919 *
920 * Introduced in API 31.
921 *
922 * This, along with other information in AImageDecoderFrameInfo,
923 * can be useful for determining whether a frame is independent, but
924 * the decoder handles disposing of frames, so a simple
925 * sequential client does not need this.
926 *
927 * @return one of:
928 * - {@link ANDROID_IMAGE_DECODER_DISPOSE_OP_NONE}
929 * - {@link ANDROID_IMAGE_DECODER_DISPOSE_OP_BACKGROUND}
930 * - {@link ANDROID_IMAGE_DECODER_DISPOSE_OP_PREVIOUS}
931 * Errors:
932 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER} if |info| is null.
933 */
934int32_t AImageDecoderFrameInfo_getDisposeOp(
935 const AImageDecoderFrameInfo* _Nonnull info) __INTRODUCED_IN(31);
936
937#endif // __ANDROID_API__ >= 31
938
939/**
940 * How a frame is blended with the previous frame.
941 *
942 * Introduced in API 31.
943 *
944 * This, along with other information in AImageDecoderFrameInfo,
945 * can be useful for determining whether a frame is independent, but
946 * the decoder handles blending frames, so a simple
947 * sequential client does not need this.
948 */
949enum {
950 // This frame replaces existing content. This corresponds
951 // to webp’s “do not blend”.
952 ANDROID_IMAGE_DECODER_BLEND_OP_SRC = 1,
953 // This frame blends with the previous frame.
954 ANDROID_IMAGE_DECODER_BLEND_OP_SRC_OVER = 2,
955};
956
957#if __ANDROID_API__ >= 31
958
959/**
960 * Return how this frame is blended with the previous frame.
961 *
962 * Introduced in API 31.
963 *
964 * This, along with other information in AImageDecoderFrameInfo,
965 * can be useful for determining whether a frame is independent, but
966 * the decoder handles blending frames, so a simple
967 * sequential client does not need this.
968 *
969 * @return one of:
970 * - {@link ANDROID_IMAGE_DECODER_BLEND_OP_SRC}
971 * - {@link ANDROID_IMAGE_DECODER_BLEND_OP_SRC_OVER}
972 * Errors:
973 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER} if |info| is null.
974 */
975int32_t AImageDecoderFrameInfo_getBlendOp(
976 const AImageDecoderFrameInfo* _Nonnull info)
977 __INTRODUCED_IN(31);
978
979#endif // __ANDROID_API__ >= 31
980
Leon Scroggins III2f984942019-11-22 17:02:23 -0500981#ifdef __cplusplus
982}
983#endif
984
985#endif // ANDROID_IMAGE_DECODER_H
986
987/** @} */