blob: ee1eee2be67da88ba514fb8ad8ba15d1df8ded2a [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
Elliott Hughes08e6cf52021-02-08 12:37:53 -080054#if !defined(__INTRODUCED_IN)
55#define __INTRODUCED_IN(__api_level) /* nothing */
Leon Scroggins III726a5502021-01-08 14:25:31 -050056#endif
57
Leon Scroggins III2f984942019-11-22 17:02:23 -050058#ifdef __cplusplus
59extern "C" {
60#endif
61
62struct AAsset;
Leon Scroggins III2f984942019-11-22 17:02:23 -050063
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050064/**
Leon Scroggins III3b3700e2020-12-29 11:52:12 -050065 * {@link AImageDecoder} functions result code.
66 *
67 * Introduced in API 30.
68 *
69 * Many functions will return this to indicate success
70 * ({@link ANDROID_IMAGE_DECODER_SUCCESS}) or the reason for the failure. On
71 * failure, any out-parameters should be considered uninitialized, except where
Leon Scroggins III9b9fb392020-12-03 16:55:36 -050072 * specified. Use {@link AImageDecoder_resultToString} for a readable
73 * version of the result code.
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050074 */
Leon Scroggins III2f984942019-11-22 17:02:23 -050075enum {
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050076 /**
77 * Decoding was successful and complete.
78 */
Leon Scroggins III2f984942019-11-22 17:02:23 -050079 ANDROID_IMAGE_DECODER_SUCCESS = 0,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050080 /**
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -050081 * The input is incomplete.
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050082 */
Leon Scroggins III2f984942019-11-22 17:02:23 -050083 ANDROID_IMAGE_DECODER_INCOMPLETE = -1,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050084 /**
85 * The input contained an error after decoding some lines.
86 */
Leon Scroggins III2f984942019-11-22 17:02:23 -050087 ANDROID_IMAGE_DECODER_ERROR = -2,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050088 /**
89 * Could not convert. For example, attempting to decode an image with
90 * alpha to an opaque format.
91 */
Leon Scroggins III2f984942019-11-22 17:02:23 -050092 ANDROID_IMAGE_DECODER_INVALID_CONVERSION = -3,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050093 /**
94 * The scale is invalid. It may have overflowed, or it may be incompatible
95 * with the current alpha setting.
96 */
Leon Scroggins III2f984942019-11-22 17:02:23 -050097 ANDROID_IMAGE_DECODER_INVALID_SCALE = -4,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050098 /**
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -050099 * Some other parameter is invalid.
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500100 */
Leon Scroggins III2f984942019-11-22 17:02:23 -0500101 ANDROID_IMAGE_DECODER_BAD_PARAMETER = -5,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500102 /**
103 * Input was invalid before decoding any pixels.
104 */
Leon Scroggins III2f984942019-11-22 17:02:23 -0500105 ANDROID_IMAGE_DECODER_INVALID_INPUT = -6,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500106 /**
107 * A seek was required and it failed.
108 */
Leon Scroggins III2f984942019-11-22 17:02:23 -0500109 ANDROID_IMAGE_DECODER_SEEK_ERROR = -7,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500110 /**
111 * Some other error. For example, an internal allocation failed.
112 */
Leon Scroggins III2f984942019-11-22 17:02:23 -0500113 ANDROID_IMAGE_DECODER_INTERNAL_ERROR = -8,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500114 /**
115 * AImageDecoder did not recognize the format.
116 */
Leon Scroggins III55287002020-10-13 11:30:11 -0400117 ANDROID_IMAGE_DECODER_UNSUPPORTED_FORMAT = -9,
118 /**
119 * The animation has reached the end.
120 */
121 ANDROID_IMAGE_DECODER_FINISHED = -10,
122 /**
123 * This method cannot be called while the AImageDecoder is in its current
124 * state. For example, various setters (like {@link AImageDecoder_setTargetSize})
125 * can only be called while the AImageDecoder is set to decode the first
126 * frame of an animation. This ensures that any blending and/or restoring
127 * prior frames works correctly.
128 */
129 ANDROID_IMAGE_DECODER_INVALID_STATE = -11,
Leon Scroggins III2f984942019-11-22 17:02:23 -0500130};
131
Leon Scroggins III9b9fb392020-12-03 16:55:36 -0500132/**
133 * Return a constant string value representing the error code.
134 *
135 * Introduced in API 31.
136 *
137 * Pass the return value from an {@link AImageDecoder} method (e.g.
138 * {@link AImageDecoder_decodeImage}) for a text string representing the error
139 * code.
140 *
141 * Errors:
142 * - Returns null for a value out of range.
143 */
144const char* _Nullable AImageDecoder_resultToString(int)__INTRODUCED_IN(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
168/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500169 * Create a new {@link AImageDecoder} from an {@link AAsset}.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500170 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700171 * Available since API level 30.
172 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500173 * @param asset {@link AAsset} containing encoded image data. Client is still
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500174 * responsible for calling {@link AAsset_close} on it, which may be
175 * done after deleting the returned {@link AImageDecoder}.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500176 * @param outDecoder On success (i.e. return value is
177 * {@link ANDROID_IMAGE_DECODER_SUCCESS}), this will be set to
178 * a newly created {@link AImageDecoder}. Caller is
179 * responsible for calling {@link AImageDecoder_delete} on it.
180 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500181 * indicating the reason for the failure.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500182 *
183 * Errors:
184 * - {@link ANDROID_IMAGE_DECODER_INCOMPLETE}: The asset was truncated before
185 * reading the image header.
186 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: One of the parameters is
187 * null.
188 * - {@link ANDROID_IMAGE_DECODER_INVALID_INPUT}: There is an error in the
189 * header.
190 * - {@link ANDROID_IMAGE_DECODER_SEEK_ERROR}: The asset failed to seek.
191 * - {@link ANDROID_IMAGE_DECODER_INTERNAL_ERROR}: Some other error, like a
192 * failure to allocate memory.
193 * - {@link ANDROID_IMAGE_DECODER_UNSUPPORTED_FORMAT}: The format is not
194 * supported.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500195 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400196int AImageDecoder_createFromAAsset(struct AAsset* _Nonnull asset,
Leon Scroggins IIIb2ee00d2021-01-07 15:37:34 -0500197 AImageDecoder* _Nullable * _Nonnull outDecoder)
Leon Scroggins IIIa9f397b2020-01-27 12:42:56 -0500198 __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500199
200/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500201 * Create a new {@link AImageDecoder} from a file descriptor.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500202 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700203 * Available since API level 30.
204 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500205 * @param fd Seekable, readable, open file descriptor for encoded data.
206 * Client is still responsible for closing it, which may be done
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500207 * after deleting the returned {@link AImageDecoder}.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500208 * @param outDecoder On success (i.e. return value is
209 * {@link ANDROID_IMAGE_DECODER_SUCCESS}), this will be set to
210 * a newly created {@link AImageDecoder}. Caller is
211 * responsible for calling {@link AImageDecoder_delete} on it.
212 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500213 * indicating the reason for the failure.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500214 *
215 * Errors:
216 * - {@link ANDROID_IMAGE_DECODER_INCOMPLETE}: The file was truncated before
217 * reading the image header.
218 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The {@link AImageDecoder} is
219 * null, or |fd| does not represent a valid, seekable file descriptor.
220 * - {@link ANDROID_IMAGE_DECODER_INVALID_INPUT}: There is an error in the
221 * header.
222 * - {@link ANDROID_IMAGE_DECODER_SEEK_ERROR}: The descriptor failed to seek.
223 * - {@link ANDROID_IMAGE_DECODER_INTERNAL_ERROR}: Some other error, like a
224 * failure to allocate memory.
225 * - {@link ANDROID_IMAGE_DECODER_UNSUPPORTED_FORMAT}: The format is not
226 * supported.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500227 */
Leon Scroggins IIIb2ee00d2021-01-07 15:37:34 -0500228int AImageDecoder_createFromFd(int fd, AImageDecoder* _Nullable * _Nonnull outDecoder)
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400229 __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500230
231/**
232 * Create a new AImageDecoder from a buffer.
233 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700234 * Available since API level 30.
235 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500236 * @param buffer Pointer to encoded data. Must be valid for the entire time
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500237 * the {@link AImageDecoder} is used.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500238 * @param length Byte length of buffer.
239 * @param outDecoder On success (i.e. return value is
240 * {@link ANDROID_IMAGE_DECODER_SUCCESS}), this will be set to
241 * a newly created {@link AImageDecoder}. Caller is
242 * responsible for calling {@link AImageDecoder_delete} on it.
243 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500244 * indicating the reason for the failure.
245 *
246 * Errors:
247 * - {@link ANDROID_IMAGE_DECODER_INCOMPLETE}: The encoded image was truncated before
248 * reading the image header.
249 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: One of the parameters is
250 * invalid.
251 * - {@link ANDROID_IMAGE_DECODER_INVALID_INPUT}: There is an error in the
252 * header.
253 * - {@link ANDROID_IMAGE_DECODER_INTERNAL_ERROR}: Some other error, like a
254 * failure to allocate memory.
255 * - {@link ANDROID_IMAGE_DECODER_UNSUPPORTED_FORMAT}: The format is not
256 * supported.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500257 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400258int AImageDecoder_createFromBuffer(const void* _Nonnull buffer, size_t length,
Leon Scroggins IIIb2ee00d2021-01-07 15:37:34 -0500259 AImageDecoder* _Nullable * _Nonnull outDecoder)
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400260 __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500261
262/**
263 * Delete the AImageDecoder.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700264 *
265 * Available since API level 30.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500266 */
Leon Scroggins IIIb2ee00d2021-01-07 15:37:34 -0500267void AImageDecoder_delete(AImageDecoder* _Nullable decoder) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500268
269/**
270 * Choose the desired output format.
271 *
Leon Scroggins III55287002020-10-13 11:30:11 -0400272 * If the encoded image represents an animation, this must be called while on
273 * the first frame (e.g. before calling {@link AImageDecoder_advanceFrame} or
274 * after calling {@link AImageDecoder_rewind}).
275 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700276 * Available since API level 30.
277 *
Leon Scroggins III5d0445c2020-01-23 09:43:43 -0500278 * @param format {@link AndroidBitmapFormat} to use for the output.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500279 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
280 * indicating the reason for the failure. On failure, the
281 * {@link AImageDecoder} uses the format it was already planning
282 * to use (either its default or a previously successful setting
283 * from this function).
284 *
285 * Errors:
286 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The
287 * {@link AImageDecoder} is null or |format| does not correspond to an
288 * {@link AndroidBitmapFormat}.
289 * - {@link ANDROID_IMAGE_DECODER_INVALID_CONVERSION}: The
290 * {@link AndroidBitmapFormat} is incompatible with the image.
Leon Scroggins III55287002020-10-13 11:30:11 -0400291 * - {@link ANDROID_IMAGE_DECODER_INVALID_STATE}: The animation is not on
292 * the first frame.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500293 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400294int AImageDecoder_setAndroidBitmapFormat(AImageDecoder* _Nonnull decoder,
Leon Scroggins III2f984942019-11-22 17:02:23 -0500295 int32_t format) __INTRODUCED_IN(30);
296
Leon Scroggins III1be112f2020-01-15 04:26:44 -0500297/**
298 * Specify whether the output's pixels should be unpremultiplied.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500299 *
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500300 * By default, {@link AImageDecoder_decodeImage} will premultiply the pixels, if they have alpha.
301 * Pass true to this method to leave them unpremultiplied. This has no effect on an
Leon Scroggins III1be112f2020-01-15 04:26:44 -0500302 * opaque image.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500303 *
Leon Scroggins III55287002020-10-13 11:30:11 -0400304 * If the encoded image represents an animation, this must be called while on
305 * the first frame (e.g. before calling {@link AImageDecoder_advanceFrame} or
306 * after calling {@link AImageDecoder_rewind}).
307 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700308 * Available since API level 30.
309 *
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500310 * @param unpremultipliedRequired Pass true to leave the pixels unpremultiplied.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500311 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
312 * indicating the reason for the failure.
313 *
314 * Errors:
315 * - {@link ANDROID_IMAGE_DECODER_INVALID_CONVERSION}: Unpremultiplied is not
316 * possible due to an existing scale set by
317 * {@link AImageDecoder_setTargetSize}.
318 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The
319 * {@link AImageDecoder} is null.
Leon Scroggins III55287002020-10-13 11:30:11 -0400320 * - {@link ANDROID_IMAGE_DECODER_INVALID_STATE}: The animation is not on
321 * the first frame.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500322 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400323int AImageDecoder_setUnpremultipliedRequired(AImageDecoder* _Nonnull decoder,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500324 bool unpremultipliedRequired) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500325
326/**
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500327 * Choose the dataspace for the output.
328 *
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500329 * Ignored by {@link ANDROID_BITMAP_FORMAT_A_8}, which does not support
330 * an {@link ADataSpace}.
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500331 *
Leon Scroggins III55287002020-10-13 11:30:11 -0400332 * If the encoded image represents an animation, this must be called while on
333 * the first frame (e.g. before calling {@link AImageDecoder_advanceFrame} or
334 * after calling {@link AImageDecoder_rewind}).
335 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700336 * Available since API level 30.
337 *
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500338 * @param dataspace The {@link ADataSpace} to decode into. An ADataSpace
339 * specifies how to interpret the colors. By default,
340 * AImageDecoder will decode into the ADataSpace specified by
341 * {@link AImageDecoderHeaderInfo_getDataSpace}. If this
342 * parameter is set to a different ADataSpace, AImageDecoder
343 * will transform the output into the specified ADataSpace.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500344 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
345 * indicating the reason for the failure.
346 *
347 * Errors:
348 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The
349 * {@link AImageDecoder} is null or |dataspace| does not correspond to an
350 * {@link ADataSpace} value.
Leon Scroggins III55287002020-10-13 11:30:11 -0400351 * - {@link ANDROID_IMAGE_DECODER_INVALID_STATE}: The animation is not on
352 * the first frame.
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500353 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400354int AImageDecoder_setDataSpace(AImageDecoder* _Nonnull decoder, int32_t dataspace)
355 __INTRODUCED_IN(30);
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500356
357/**
Leon Scroggins III2f984942019-11-22 17:02:23 -0500358 * Specify the output size for a decoded image.
359 *
360 * Future calls to {@link AImageDecoder_decodeImage} will sample or scale the
361 * encoded image to reach the desired size. If a crop rect is set (via
362 * {@link AImageDecoder_setCrop}), it must be contained within the dimensions
363 * specified by width and height, and the output image will be the size of the
364 * crop rect.
365 *
Leon Scroggins III55287002020-10-13 11:30:11 -0400366 * If the encoded image represents an animation, this must be called while on
367 * the first frame (e.g. before calling {@link AImageDecoder_advanceFrame} or
368 * after calling {@link AImageDecoder_rewind}).
369 *
Leon Scroggins III159ab122021-02-24 15:32:42 -0500370 * It is strongly recommended to use setTargetSize only for downscaling, as it
371 * is often more efficient to scale-up when rendering than up-front due to
372 * reduced overall memory.
373 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700374 * Available since API level 30.
375 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500376 * @param width Width of the output (prior to cropping).
377 * This will affect future calls to
378 * {@link AImageDecoder_getMinimumStride}, which will now return
379 * a value based on this width.
380 * @param height Height of the output (prior to cropping).
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500381 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
382 * indicating the reason for the failure.
383 *
384 * Errors:
385 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The
386 * {@link AImageDecoder} is null.
387 * - {@link ANDROID_IMAGE_DECODER_INVALID_SCALE}: |width| or |height| is <= 0,
Leon Scroggins III55287002020-10-13 11:30:11 -0400388 * the size is too big, any existing crop is not contained by the new image
389 * dimensions, or the scale is incompatible with a previous call to
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500390 * {@link AImageDecoder_setUnpremultipliedRequired}(true).
Leon Scroggins III55287002020-10-13 11:30:11 -0400391 * - {@link ANDROID_IMAGE_DECODER_INVALID_STATE}: The animation is not on
392 * the first frame.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500393 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400394int AImageDecoder_setTargetSize(AImageDecoder* _Nonnull decoder, int32_t width,
395 int32_t height) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500396
Leon Scroggins IIIf27256b2020-01-19 21:13:04 -0500397/**
398 * Compute the dimensions to use for a given sampleSize.
399 *
400 * Although AImageDecoder can scale to an arbitrary target size (see
401 * {@link AImageDecoder_setTargetSize}), some sizes may be more efficient than
402 * others. This computes the most efficient target size to use to reach a
403 * particular sampleSize.
404 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700405 * Available since API level 30.
406 *
Leon Scroggins IIIf27256b2020-01-19 21:13:04 -0500407 * @param sampleSize A subsampling rate of the original image. Must be greater
408 * than or equal to 1. A sampleSize of 2 means to skip every
409 * other pixel/line, resulting in a width and height that are
410 * 1/2 of the original dimensions, with 1/4 the number of
411 * pixels.
412 * @param width Out parameter for the width 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 IIIf27256b2020-01-19 21:13:04 -0500414 * @param height Out parameter for the height sampled by sampleSize, and rounded
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500415 * in the direction that the decoder can do most efficiently.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500416 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
417 * indicating the reason for the failure.
418 *
419 * Errors:
420 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The
421 * {@link AImageDecoder}, |width| or |height| is null or |sampleSize| is < 1.
Leon Scroggins IIIf27256b2020-01-19 21:13:04 -0500422 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400423int AImageDecoder_computeSampledSize(const AImageDecoder* _Nonnull decoder, int sampleSize,
424 int32_t* _Nonnull width, int32_t* _Nonnull height)
425 __INTRODUCED_IN(30);
Leon Scroggins III3b3700e2020-12-29 11:52:12 -0500426
Leon Scroggins III2f984942019-11-22 17:02:23 -0500427/**
428 * Specify how to crop the output after scaling (if any).
429 *
430 * Future calls to {@link AImageDecoder_decodeImage} will crop their output to
431 * the specified {@link ARect}. Clients will only need to allocate enough memory
432 * for the cropped ARect.
433 *
Leon Scroggins III55287002020-10-13 11:30:11 -0400434 * If the encoded image represents an animation, this must be called while on
435 * the first frame (e.g. before calling {@link AImageDecoder_advanceFrame} or
436 * after calling {@link AImageDecoder_rewind}).
437 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700438 * Available since API level 30.
439 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500440 * @param crop Rectangle describing a crop of the decode. It must be contained inside of
441 * the (possibly scaled, by {@link AImageDecoder_setTargetSize})
442 * image dimensions. This will affect future calls to
443 * {@link AImageDecoder_getMinimumStride}, which will now return a
444 * value based on the width of the crop. An empty ARect -
445 * specifically { 0, 0, 0, 0 } - may be used to remove the cropping
446 * behavior. Any other empty or unsorted ARects will result in
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500447 * returning {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500448 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
449 * indicating the reason for the failure.
450 *
451 * Errors:
452 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The
Leon Scroggins III55287002020-10-13 11:30:11 -0400453 * {@link AImageDecoder} is null, or the crop is not contained by the
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500454 * (possibly scaled) image dimensions.
Leon Scroggins III55287002020-10-13 11:30:11 -0400455 * - {@link ANDROID_IMAGE_DECODER_INVALID_STATE}: The animation is not on
456 * the first frame.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500457 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400458int AImageDecoder_setCrop(AImageDecoder* _Nonnull decoder, ARect crop) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500459
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
472/**
473 * Return an opaque handle for reading header info.
474 *
475 * This is owned by the {@link AImageDecoder} and will be destroyed when the
476 * AImageDecoder is destroyed via {@link AImageDecoder_delete}.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700477 *
478 * Available since API level 30.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500479 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400480const AImageDecoderHeaderInfo* _Nonnull AImageDecoder_getHeaderInfo(
481 const AImageDecoder* _Nonnull decoder) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500482
483/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500484 * Report the native width of the encoded image. This is also the logical
485 * pixel width of the output, unless {@link AImageDecoder_setTargetSize} is
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500486 * used to choose a different size or {@link AImageDecoder_setCrop} is used to
487 * set a crop rect.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700488 *
489 * Available since API level 30.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500490 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400491int32_t AImageDecoderHeaderInfo_getWidth(const AImageDecoderHeaderInfo* _Nonnull)
492 __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500493
494/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500495 * Report the native height of the encoded image. This is also the logical
496 * pixel height of the output, unless {@link AImageDecoder_setTargetSize} is
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500497 * used to choose a different size or {@link AImageDecoder_setCrop} is used to
498 * set a crop rect.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700499 *
500 * Available since API level 30.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500501 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400502int32_t AImageDecoderHeaderInfo_getHeight(const AImageDecoderHeaderInfo* _Nonnull)
503 __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500504
505/**
506 * Report the mimeType of the encoded image.
507 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700508 * Available since API level 30.
509 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500510 * @return a string literal describing the mime type.
511 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400512const char* _Nonnull AImageDecoderHeaderInfo_getMimeType(
513 const AImageDecoderHeaderInfo* _Nonnull) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500514
515/**
Leon Scroggins III5d0445c2020-01-23 09:43:43 -0500516 * Report the {@link AndroidBitmapFormat} the AImageDecoder will decode to
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500517 * by default. {@link AImageDecoder} will try to choose one that is sensible
Leon Scroggins III2f984942019-11-22 17:02:23 -0500518 * for the image and the system. Note that this does not indicate the
519 * encoded format of the image.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700520 *
521 * Available since API level 30.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500522 */
Leon Scroggins III5d0445c2020-01-23 09:43:43 -0500523int32_t AImageDecoderHeaderInfo_getAndroidBitmapFormat(
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400524 const AImageDecoderHeaderInfo* _Nonnull) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500525
526/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500527 * Report how the {@link AImageDecoder} will handle alpha by default. If the image
Leon Scroggins III2f984942019-11-22 17:02:23 -0500528 * contains no alpha (according to its header), this will return
529 * {@link ANDROID_BITMAP_FLAGS_ALPHA_OPAQUE}. If the image may contain alpha,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500530 * this returns {@link ANDROID_BITMAP_FLAGS_ALPHA_PREMUL}, because
531 * {@link AImageDecoder_decodeImage} will premultiply pixels by default.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700532 *
533 * Available since API level 30.
Leon Scroggins IIIac9f7492020-12-28 15:33:13 -0500534 *
535 * Starting in API level 31, an AImageDecoder may contain multiple frames of an
536 * animation, but this method still only reports whether the first frame has
537 * alpha.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500538 */
539int AImageDecoderHeaderInfo_getAlphaFlags(
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400540 const AImageDecoderHeaderInfo* _Nonnull) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500541
542/**
Leon Scroggins III8cee65e2020-01-30 14:20:42 -0500543 * Report the dataspace the AImageDecoder will decode to by default.
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500544 *
Leon Scroggins III8cee65e2020-01-30 14:20:42 -0500545 * By default, {@link AImageDecoder_decodeImage} will not do any color
546 * conversion.
547 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700548 * Available since API level 30.
549 *
Leon Scroggins III8cee65e2020-01-30 14:20:42 -0500550 * @return The {@link ADataSpace} representing the way the colors
551 * are encoded (or {@link ADATASPACE_UNKNOWN} if there is not a
552 * corresponding ADataSpace). This specifies how to interpret the colors
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500553 * in the decoded image, unless {@link AImageDecoder_setDataSpace} is
554 * called to decode to a different ADataSpace.
555 *
556 * Note that ADataSpace only exposes a few values. This may return
Leon Scroggins III8cee65e2020-01-30 14:20:42 -0500557 * {@link ADATASPACE_UNKNOWN}, even for Named ColorSpaces, if they have
558 * no corresponding {@link ADataSpace}.
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500559 */
560int32_t AImageDecoderHeaderInfo_getDataSpace(
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400561 const AImageDecoderHeaderInfo* _Nonnull) __INTRODUCED_IN(30);
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500562
563/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500564 * Return the minimum stride that can be used in
565 * {@link AImageDecoder_decodeImage).
566 *
567 * This stride provides no padding, meaning it will be exactly equal to the
568 * width times the number of bytes per pixel for the {@link AndroidBitmapFormat}
569 * being used.
570 *
571 * If the output is scaled (via {@link AImageDecoder_setTargetSize}) and/or
572 * cropped (via {@link AImageDecoder_setCrop}), this takes those into account.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700573 *
574 * Available since API level 30.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500575 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400576size_t AImageDecoder_getMinimumStride(AImageDecoder* _Nonnull decoder) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500577
578/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500579 * Decode the image into pixels, using the settings of the {@link AImageDecoder}.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500580 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700581 * Available since API level 30.
582 *
Leon Scroggins III1b389712020-10-09 13:12:39 -0400583 * 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 -0500584 * animated image (i.e. GIF, WebP) using new APIs. Internally,
Leon Scroggins III55287002020-10-13 11:30:11 -0400585 * AImageDecoder keeps track of its "current frame" - that is, the frame that
586 * will be decoded by a call to AImageDecoder_decodeImage. At creation time, the
587 * current frame is always the first frame, and multiple calls to this method
588 * will each decode the first frame. {@link AImageDecoder_advanceFrame} advances
589 * the current frame to the following frame, so that future calls to this method
590 * will decode that frame. Some frames may update only part of the image. They
591 * may only update a sub-rectangle (see {@link
592 * AImageDecoderFrameInfo_getFrameRect}), or they may have alpha (see
593 * {@link AImageDecoderFrameInfo_hasAlphaWithinBounds}). In these cases, this
594 * method assumes that the prior frame is still residing in the |pixels| buffer,
595 * decodes only the new portion, and blends it with the buffer. Frames that change
596 * the entire |pixels| buffer are "independent", and do not require the prior
597 * frame to remain in the buffer. The first frame is always independent. A
598 * sophisticated client can use information from the {@link AImageDecoderFrameInfo}
599 * to determine whether other frames are independent, or what frames they rely on.
600 *
601 * If the current frame is marked {@link ANDROID_IMAGE_DECODER_DISPOSE_OP_PREVIOUS},
Leon Scroggins III78859b42021-01-13 11:48:34 -0500602 * AImageDecoder_decodeImage will store the |pixels| buffer prior to decoding
Leon Scroggins III55287002020-10-13 11:30:11 -0400603 * (note: this only happens for the first in a string of consecutive
604 * ANDROID_IMAGE_DECODER_DISPOSE_OP_PREVIOUS frames). After advancing to the
Leon Scroggins III78859b42021-01-13 11:48:34 -0500605 * following frame, AImageDecoder_decodeImage will restore that buffer prior to
606 * decoding that frame. This is the default behavior, but it can be disabled
607 * by passing false to {@link AImageDecoder_setInternallyHandleDisposePrevious}.
Leon Scroggins III55287002020-10-13 11:30:11 -0400608 *
609 * Ignoring timing information, display, etc, a client wishing to decode all
610 * frames of an animated image may conceptually use code like the following:
611 *
612 * while (true) {
613 * int result = AImageDecoder_decodeImage(decoder, pixels, stride, size);
614 * if (result != ANDROID_IMAGE_DECODER_SUCCESS) break;
615 *
616 * // Display or save the image in |pixels|, keeping the buffer intact for
617 * // AImageDecoder to decode the next frame correctly.
618 * Application_viewImage(pixels);
619 *
620 * result = AImageDecoder_advanceFrame(decoder);
621 * if (result != ANDROID_IMAGE_DECODER_SUCCESS) break;
622 * }
Leon Scroggins III1b389712020-10-09 13:12:39 -0400623 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500624 * @param decoder Opaque object representing the decoder.
625 * @param pixels On success, will be filled with the result
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500626 * of the decode. Must be large enough to hold |size| bytes.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500627 * @param stride Width in bytes of a single row. Must be at least
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500628 * {@link AImageDecoder_getMinimumStride} and a multiple of the
629 * bytes per pixel of the {@link AndroidBitmapFormat}.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500630 * @param size Size of the pixel buffer in bytes. Must be at least
631 * stride * (height - 1) +
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500632 * {@link AImageDecoder_getMinimumStride}.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500633 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
634 * indicating the reason for the failure.
635 *
636 * Errors:
637 * - {@link ANDROID_IMAGE_DECODER_INCOMPLETE}: The image was truncated. A
638 * partial image was decoded, and undecoded lines have been initialized to all
639 * zeroes.
640 * - {@link ANDROID_IMAGE_DECODER_ERROR}: The image contained an error. A
641 * partial image was decoded, and undecoded lines have been initialized to all
642 * zeroes.
643 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The {@link AImageDecoder} or
644 * |pixels| is null, the stride is not large enough or not pixel aligned, or
645 * |size| is not large enough.
646 * - {@link ANDROID_IMAGE_DECODER_SEEK_ERROR}: The asset or file descriptor
647 * failed to seek.
648 * - {@link ANDROID_IMAGE_DECODER_INTERNAL_ERROR}: Some other error, like a
649 * failure to allocate memory.
Leon Scroggins III55287002020-10-13 11:30:11 -0400650 * - {@link ANDROID_IMAGE_DECODER_FINISHED}: The input contains no
651 * more frames. No decoding occurred. The client must call
652 * {@link AImageDecoder_rewind} before calling
653 * {@link AImageDecoder_decodeImage} again.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500654 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400655int AImageDecoder_decodeImage(AImageDecoder* _Nonnull decoder,
656 void* _Nonnull pixels, size_t stride,
Leon Scroggins III2f984942019-11-22 17:02:23 -0500657 size_t size) __INTRODUCED_IN(30);
658
Leon Scroggins III1b389712020-10-09 13:12:39 -0400659/**
660 * Return true iff the image is animated - i.e. has multiple frames.
661 *
662 * Introduced in API 31.
663 *
Leon Scroggins III55287002020-10-13 11:30:11 -0400664 * A single frame GIF is considered to *not* be animated. This may require
665 * seeking past the first frame to verify whether there is a following frame.
Leon Scroggins III1b389712020-10-09 13:12:39 -0400666 *
667 * Errors:
668 * - returns false if |decoder| is null.
669 */
670bool AImageDecoder_isAnimated(AImageDecoder* _Nonnull decoder)
671 __INTRODUCED_IN(31);
672
Leon Scroggins III728a9ac2020-10-12 10:34:05 -0400673enum {
674 /*
675 * Reported by {@link AImageDecoder_getRepeatCount} if the
676 * animation should repeat forever.
Leon Scroggins III3b3700e2020-12-29 11:52:12 -0500677 *
678 * Introduced in API 31
Leon Scroggins III728a9ac2020-10-12 10:34:05 -0400679 */
680 ANDROID_IMAGE_DECODER_INFINITE = INT32_MAX,
681};
682
683/**
684 * Report how many times the animation should repeat.
685 *
686 * Introduced in API 31.
687 *
688 * This does not include the first play through. e.g. a repeat
689 * count of 4 means that each frame is played 5 times.
690 *
691 * {@link ANDROID_IMAGE_DECODER_INFINITE} means to repeat forever.
692 *
693 * This may require seeking.
694 *
695 * For non-animated formats, this returns 0. It may return non-zero for
696 * an image with only one frame (i.e. {@link AImageDecoder_isAnimated} returns
697 * false) if the encoded image contains a repeat count.
698 *
699 * @return Number of times to repeat on success or a value
700 * indicating the reason for the failure.
701 *
702 * Errors:
703 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The AImageDecoder
704 * is null.
705 */
Jiyong Parkf1f20902021-01-13 08:28:13 +0900706int32_t AImageDecoder_getRepeatCount(AImageDecoder* _Nonnull decoder)
Leon Scroggins III728a9ac2020-10-12 10:34:05 -0400707 __INTRODUCED_IN(31);
708
Leon Scroggins III55287002020-10-13 11:30:11 -0400709/**
710 * Advance to the next frame in the animation.
711 *
712 * Introduced in API 31.
713 *
714 * The AImageDecoder keeps track internally which frame it is ready to decode
715 * (the "current frame"). Initially it is set to decode the first frame, and
716 * each call to {@link AImageDecoder_decodeImage} will continue to decode
717 * the same frame until this method (or {@link AImageDecoder_rewind})
718 * is called.
719 *
720 * Note that this can be used to skip a frame without decoding it. But
721 * some frames depend on (i.e. blend with) prior frames, and
722 * AImageDecoder_decodeImage assumes that the prior frame is in the
723 * |pixels| buffer. In addition, AImageDecoder_decodeImage handles caching and
724 * restoring frames (see {@link ANDROID_IMAGE_DECODER_DISPOSE_OP_PREVIOUS}), so
725 * skipping frames in an image with such frames may not produce the correct
726 * results.
727 *
728 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
729 * indicating the reason for the failure.
730 *
731 * Errors:
732 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The AImageDecoder
733 * represents an image that is not animated (see
734 * {@link AImageDecoder_isAnimated}) or the AImageDecoder is null.
735 * - {@link ANDROID_IMAGE_DECODER_INCOMPLETE}: The input appears
736 * to be truncated. The client must call {@link AImageDecoder_rewind}
737 * before calling {@link AImageDecoder_decodeImage} again.
738 * - {@link ANDROID_IMAGE_DECODER_ERROR}: The input contains an error.
739 * The client must call {@link AImageDecoder_rewind} before
740 * calling {@link AImageDecoder_decodeImage} again.
741 * - {@link ANDROID_IMAGE_DECODER_FINISHED}: The input contains no
742 * more frames. The client must call {@link AImageDecoder_rewind}
743 * before calling {@link AImageDecoder_decodeImage} again.
744 */
745int AImageDecoder_advanceFrame(AImageDecoder* _Nonnull decoder)
746 __INTRODUCED_IN(31);
747
748/**
749 * Return to the beginning of the animation.
750 *
751 * Introduced in API 31.
752 *
753 * After this call, the AImageDecoder will be ready to decode the
754 * first frame of the animation. This can be called after reaching
755 * the end of the animation or an error or in the middle of the
756 * animation.
757 *
758 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
759 * indicating the reason for the failure.
760 *
761 * Errors:
762 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The AImageDecoder
763 * represents an image that is not animated (see
764 * {@link AImageDecoder_isAnimated}) or the AImageDecoder is
765 * null.
766 * - {@link ANDROID_IMAGE_DECODER_SEEK_ERROR}: The asset or file
767 * descriptor failed to seek.
768 */
769int AImageDecoder_rewind(AImageDecoder* _Nonnull decoder)
770 __INTRODUCED_IN(31);
771
Leon Scroggins IIIac9f7492020-12-28 15:33:13 -0500772struct AImageDecoderFrameInfo;
773
774/**
775 * Opaque handle to animation information about a single frame.
776 *
777 * Introduced in API 31
778 *
779 * The duration (retrieved with {@link AImageDecoderFrameInfo_getDuration}) is
780 * necessary for clients to display the animation at the proper speed. The other
781 * information is helpful for a client that wants to determine what frames are
782 * independent (or what frames they depend on), but is unnecessary for
783 * a simple client that wants to sequentially display all frames.
784 */
785typedef struct AImageDecoderFrameInfo AImageDecoderFrameInfo;
786
Leon Scroggins IIIac9f7492020-12-28 15:33:13 -0500787/**
788 * Create an uninitialized AImageDecoderFrameInfo.
789 *
790 * Introduced in API 31.
791 *
792 * This can be passed to {@link AImageDecoder_getFrameInfo} to fill
793 * in information about the current frame. It may be reused.
794 *
795 * Must be deleted with {@link AImageDecoderFrameInfo_delete}.
796 */
797AImageDecoderFrameInfo* _Nullable AImageDecoderFrameInfo_create()
798 __INTRODUCED_IN(31);
799
800/**
801 * Delete an AImageDecoderFrameInfo.
802 *
803 * Introduced in API 31.
804 */
805void AImageDecoderFrameInfo_delete(
806 AImageDecoderFrameInfo* _Nullable info) __INTRODUCED_IN(31);
807
808/**
809 * Fill |info| with information about the current frame.
810 *
811 * Introduced in API 31.
812 *
813 * Initially, this will return information about the first frame.
814 * {@link AImageDecoder_advanceFrame} and
815 * {@link AImageDecoder_rewind} can be used to change which frame
816 * is the current frame.
817 *
818 * If the image only has one frame, this will fill the {@link
819 * AImageDecoderFrameInfo} with the encoded info, if any, or reasonable
820 * defaults.
821 *
822 * @param decoder Opaque object representing the decoder.
823 * @param info Opaque object to hold frame information. On success, will be
824 * filled with information regarding the current frame.
825 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
826 * indicating the reason for the failure.
827 *
828 * Errors:
829 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: One of the parameters is null.
830 * - {@link ANDROID_IMAGE_DECODER_FINISHED}: The input contains no
831 * more frames. The client must call {@link AImageDecoder_rewind} to reset the
832 * current frame to a valid frame (0).
833 */
834int AImageDecoder_getFrameInfo(AImageDecoder* _Nonnull decoder,
835 AImageDecoderFrameInfo* _Nonnull info) __INTRODUCED_IN(31);
836
837/**
838 * Report the number of nanoseconds to show the current frame.
839 *
840 * Introduced in API 31.
841 *
842 * Errors:
843 * - returns 0 if |info| is null.
844 */
845int64_t AImageDecoderFrameInfo_getDuration(
846 const AImageDecoderFrameInfo* _Nonnull info) __INTRODUCED_IN(31);
847
848/**
849 * The rectangle of the image (within 0, 0,
850 * {@link AImageDecoder_getWidth}, {@link AImageDecoder_getHeight})
851 * updated by this frame.
852 *
853 * Introduced in API 31.
854 *
855 * Note that this is unaffected by calls to
856 * {@link AImageDecoder_setTargetSize} or
857 * {@link AImageDecoder_setCrop}.
858 *
859 * A frame may update only part of the image. This will always be
860 * contained by the image’s dimensions.
861 *
862 * This, along with other information in AImageDecoderFrameInfo,
863 * can be useful for determining whether a frame is independent, but
864 * the decoder handles blending frames, so a simple
865 * sequential client does not need this.
866 *
867 * Errors:
868 * - returns an empty ARect if |info| is null.
869 */
870ARect AImageDecoderFrameInfo_getFrameRect(
871 const AImageDecoderFrameInfo* _Nonnull info) __INTRODUCED_IN(31);
872
873/**
874 * Whether the new portion of this frame may contain alpha.
875 *
876 * Introduced in API 31.
877 *
878 * Note that this may differ from whether the composed frame has
879 * alpha. If this frame does not fill the entire image dimensions
880 * (see {@link AImageDecoderFrameInfo_getFrameRect}) or it blends
881 * with an opaque frame, for example, the composed frame’s alpha
882 * may not match. It is also conservative; for example, if a color
883 * index-based frame has a color with alpha but does not use it,
884 * this will still return true.
885 *
886 * This, along with other information in AImageDecoderFrameInfo,
887 * can be useful for determining whether a frame is independent, but
888 * the decoder handles blending frames, so a simple
889 * sequential client does not need this.
890 *
891 * Errors:
892 * - returns false if |info| is null.
893 */
894bool AImageDecoderFrameInfo_hasAlphaWithinBounds(
895 const AImageDecoderFrameInfo* _Nonnull info) __INTRODUCED_IN(31);
896
Leon Scroggins IIIac9f7492020-12-28 15:33:13 -0500897/**
898 * How a frame is “disposed” before showing the next one.
899 *
900 * Introduced in API 31.
901 *
902 * This, along with other information in AImageDecoderFrameInfo,
903 * can be useful for determining whether a frame is independent, but
904 * the decoder handles disposing of frames, so a simple
905 * sequential client does not need this.
906 */
907enum {
908 // No disposal. The following frame will be drawn directly
909 // on top of this one.
910 ANDROID_IMAGE_DECODER_DISPOSE_OP_NONE = 1,
Leon Scroggins III78859b42021-01-13 11:48:34 -0500911 // The frame’s rectangle is cleared to transparent (by AImageDecoder)
912 // before decoding the next frame.
Leon Scroggins IIIac9f7492020-12-28 15:33:13 -0500913 ANDROID_IMAGE_DECODER_DISPOSE_OP_BACKGROUND = 2,
Leon Scroggins III78859b42021-01-13 11:48:34 -0500914 // The frame’s rectangle is reverted to the prior frame before decoding
915 // the next frame. This is handled by AImageDecoder, unless
916 // {@link AImageDecoder_setInternallyHandleDisposePrevious} is set to false.
Leon Scroggins IIIac9f7492020-12-28 15:33:13 -0500917 ANDROID_IMAGE_DECODER_DISPOSE_OP_PREVIOUS = 3,
918};
919
Leon Scroggins IIIac9f7492020-12-28 15:33:13 -0500920/**
921 * Return how this frame is “disposed” before showing the next one.
922 *
923 * Introduced in API 31.
924 *
925 * This, along with other information in AImageDecoderFrameInfo,
926 * can be useful for determining whether a frame is independent, but
927 * the decoder handles disposing of frames, so a simple
928 * sequential client does not need this.
929 *
930 * @return one of:
931 * - {@link ANDROID_IMAGE_DECODER_DISPOSE_OP_NONE}
932 * - {@link ANDROID_IMAGE_DECODER_DISPOSE_OP_BACKGROUND}
933 * - {@link ANDROID_IMAGE_DECODER_DISPOSE_OP_PREVIOUS}
934 * Errors:
935 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER} if |info| is null.
936 */
937int32_t AImageDecoderFrameInfo_getDisposeOp(
938 const AImageDecoderFrameInfo* _Nonnull info) __INTRODUCED_IN(31);
939
Leon Scroggins IIIac9f7492020-12-28 15:33:13 -0500940/**
941 * How a frame is blended with the previous frame.
942 *
943 * Introduced in API 31.
944 *
945 * This, along with other information in AImageDecoderFrameInfo,
946 * can be useful for determining whether a frame is independent, but
947 * the decoder handles blending frames, so a simple
948 * sequential client does not need this.
949 */
950enum {
951 // This frame replaces existing content. This corresponds
952 // to webp’s “do not blend”.
953 ANDROID_IMAGE_DECODER_BLEND_OP_SRC = 1,
954 // This frame blends with the previous frame.
955 ANDROID_IMAGE_DECODER_BLEND_OP_SRC_OVER = 2,
956};
957
Leon Scroggins IIIac9f7492020-12-28 15:33:13 -0500958/**
959 * Return how this 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 *
968 * @return one of:
969 * - {@link ANDROID_IMAGE_DECODER_BLEND_OP_SRC}
970 * - {@link ANDROID_IMAGE_DECODER_BLEND_OP_SRC_OVER}
971 * Errors:
972 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER} if |info| is null.
973 */
974int32_t AImageDecoderFrameInfo_getBlendOp(
975 const AImageDecoderFrameInfo* _Nonnull info)
976 __INTRODUCED_IN(31);
977
Leon Scroggins III78859b42021-01-13 11:48:34 -0500978/**
979 * Whether to have AImageDecoder store the frame prior to a
980 * frame marked {@link ANDROID_IMAGE_DECODER_DISPOSE_OP_PREVIOUS}.
981 *
982 * Introduced in API 31.
983 *
984 * The default is true. Many images will not have such a frame (it
985 * is not supported by WebP, and only some GIFs use it). But
986 * if frame i is ANDROID_IMAGE_DECODER_DISPOSE_OP_PREVIOUS, then i+1
987 * may depend on i-1. When this setting is true, AImageDecoder will
988 * defensively copy frame i-1 (i.e. the contents of |pixels| in
989 * {@link AImageDecoder_decodeImage}) into an internal buffer so that
990 * it can be used to decode i+1.
991 *
992 * AImageDecoder will only store a single frame, at the size specified
993 * by {@link AImageDecoder_setTargetSize} (or the original dimensions
994 * if that method has not been called), and will discard it when it is
995 * no longer necessary.
996 *
997 * A client that desires to manually store such frames may set this to
998 * false, so that AImageDecoder does not need to store this extra
999 * frame. Instead, when decoding the same
1000 * ANDROID_IMAGE_DECODER_DISPOSE_OP_PREVIOUS frame i, AImageDecoder
1001 * will decode directly into |pixels|, assuming the client stored i-1.
1002 * When asked to decode frame i+1, AImageDecoder will now assume that
1003 * the client provided i-1 in |pixels|.
1004 *
1005 * @param handleInternally Whether AImageDecoder will internally
1006 * handle ANDROID_IMAGE_DECODER_DISPOSE_OP_PREVIOUS
1007 * frames.
1008 */
1009void AImageDecoder_setInternallyHandleDisposePrevious(
1010 AImageDecoder* _Nonnull decoder, bool handleInternally)
1011 __INTRODUCED_IN(31);
1012
1013
Leon Scroggins III2f984942019-11-22 17:02:23 -05001014#ifdef __cplusplus
1015}
1016#endif
1017
1018#endif // ANDROID_IMAGE_DECODER_H
1019
1020/** @} */