blob: cac67d4f8fa6a77bad959d1f4ee37e2340a7294e [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 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700370 * Available since API level 30.
371 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500372 * @param width Width of the output (prior to cropping).
373 * This will affect future calls to
374 * {@link AImageDecoder_getMinimumStride}, which will now return
375 * a value based on this width.
376 * @param height Height of the output (prior to cropping).
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500377 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
378 * indicating the reason for the failure.
379 *
380 * Errors:
381 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The
382 * {@link AImageDecoder} is null.
383 * - {@link ANDROID_IMAGE_DECODER_INVALID_SCALE}: |width| or |height| is <= 0,
Leon Scroggins III55287002020-10-13 11:30:11 -0400384 * the size is too big, any existing crop is not contained by the new image
385 * dimensions, or the scale is incompatible with a previous call to
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500386 * {@link AImageDecoder_setUnpremultipliedRequired}(true).
Leon Scroggins III55287002020-10-13 11:30:11 -0400387 * - {@link ANDROID_IMAGE_DECODER_INVALID_STATE}: The animation is not on
388 * the first frame.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500389 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400390int AImageDecoder_setTargetSize(AImageDecoder* _Nonnull decoder, int32_t width,
391 int32_t height) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500392
Leon Scroggins IIIf27256b2020-01-19 21:13:04 -0500393/**
394 * Compute the dimensions to use for a given sampleSize.
395 *
396 * Although AImageDecoder can scale to an arbitrary target size (see
397 * {@link AImageDecoder_setTargetSize}), some sizes may be more efficient than
398 * others. This computes the most efficient target size to use to reach a
399 * particular sampleSize.
400 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700401 * Available since API level 30.
402 *
Leon Scroggins IIIf27256b2020-01-19 21:13:04 -0500403 * @param sampleSize A subsampling rate of the original image. Must be greater
404 * than or equal to 1. A sampleSize of 2 means to skip every
405 * other pixel/line, resulting in a width and height that are
406 * 1/2 of the original dimensions, with 1/4 the number of
407 * pixels.
408 * @param width Out parameter for the width sampled by sampleSize, and rounded
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500409 * in the direction that the decoder can do most efficiently.
Leon Scroggins IIIf27256b2020-01-19 21:13:04 -0500410 * @param height Out parameter for the height sampled by sampleSize, and rounded
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500411 * in the direction that the decoder can do most efficiently.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500412 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
413 * indicating the reason for the failure.
414 *
415 * Errors:
416 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The
417 * {@link AImageDecoder}, |width| or |height| is null or |sampleSize| is < 1.
Leon Scroggins IIIf27256b2020-01-19 21:13:04 -0500418 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400419int AImageDecoder_computeSampledSize(const AImageDecoder* _Nonnull decoder, int sampleSize,
420 int32_t* _Nonnull width, int32_t* _Nonnull height)
421 __INTRODUCED_IN(30);
Leon Scroggins III3b3700e2020-12-29 11:52:12 -0500422
Leon Scroggins III2f984942019-11-22 17:02:23 -0500423/**
424 * Specify how to crop the output after scaling (if any).
425 *
426 * Future calls to {@link AImageDecoder_decodeImage} will crop their output to
427 * the specified {@link ARect}. Clients will only need to allocate enough memory
428 * for the cropped ARect.
429 *
Leon Scroggins III55287002020-10-13 11:30:11 -0400430 * If the encoded image represents an animation, this must be called while on
431 * the first frame (e.g. before calling {@link AImageDecoder_advanceFrame} or
432 * after calling {@link AImageDecoder_rewind}).
433 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700434 * Available since API level 30.
435 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500436 * @param crop Rectangle describing a crop of the decode. It must be contained inside of
437 * the (possibly scaled, by {@link AImageDecoder_setTargetSize})
438 * image dimensions. This will affect future calls to
439 * {@link AImageDecoder_getMinimumStride}, which will now return a
440 * value based on the width of the crop. An empty ARect -
441 * specifically { 0, 0, 0, 0 } - may be used to remove the cropping
442 * behavior. Any other empty or unsorted ARects will result in
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500443 * returning {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500444 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
445 * indicating the reason for the failure.
446 *
447 * Errors:
448 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The
Leon Scroggins III55287002020-10-13 11:30:11 -0400449 * {@link AImageDecoder} is null, or the crop is not contained by the
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500450 * (possibly scaled) image dimensions.
Leon Scroggins III55287002020-10-13 11:30:11 -0400451 * - {@link ANDROID_IMAGE_DECODER_INVALID_STATE}: The animation is not on
452 * the first frame.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500453 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400454int AImageDecoder_setCrop(AImageDecoder* _Nonnull decoder, ARect crop) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500455
Leon Scroggins III2f984942019-11-22 17:02:23 -0500456struct AImageDecoderHeaderInfo;
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500457/**
Leon Scroggins III3b3700e2020-12-29 11:52:12 -0500458 * Opaque handle for representing information about the encoded image.
459 *
460 * Introduced in API 30
461 *
462 * Retrieved using {@link AImageDecoder_getHeaderInfo} and passed to methods
463 * like {@link AImageDecoderHeaderInfo_getWidth} and
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500464 * {@link AImageDecoderHeaderInfo_getHeight}.
465 */
Leon Scroggins III2f984942019-11-22 17:02:23 -0500466typedef struct AImageDecoderHeaderInfo AImageDecoderHeaderInfo;
467
468/**
469 * Return an opaque handle for reading header info.
470 *
471 * This is owned by the {@link AImageDecoder} and will be destroyed when the
472 * AImageDecoder is destroyed via {@link AImageDecoder_delete}.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700473 *
474 * Available since API level 30.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500475 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400476const AImageDecoderHeaderInfo* _Nonnull AImageDecoder_getHeaderInfo(
477 const AImageDecoder* _Nonnull decoder) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500478
479/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500480 * Report the native width of the encoded image. This is also the logical
481 * pixel width of the output, unless {@link AImageDecoder_setTargetSize} is
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500482 * used to choose a different size or {@link AImageDecoder_setCrop} is used to
483 * set a crop rect.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700484 *
485 * Available since API level 30.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500486 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400487int32_t AImageDecoderHeaderInfo_getWidth(const AImageDecoderHeaderInfo* _Nonnull)
488 __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500489
490/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500491 * Report the native height of the encoded image. This is also the logical
492 * pixel height of the output, unless {@link AImageDecoder_setTargetSize} is
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500493 * used to choose a different size or {@link AImageDecoder_setCrop} is used to
494 * set a crop rect.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700495 *
496 * Available since API level 30.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500497 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400498int32_t AImageDecoderHeaderInfo_getHeight(const AImageDecoderHeaderInfo* _Nonnull)
499 __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500500
501/**
502 * Report the mimeType of the encoded image.
503 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700504 * Available since API level 30.
505 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500506 * @return a string literal describing the mime type.
507 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400508const char* _Nonnull AImageDecoderHeaderInfo_getMimeType(
509 const AImageDecoderHeaderInfo* _Nonnull) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500510
511/**
Leon Scroggins III5d0445c2020-01-23 09:43:43 -0500512 * Report the {@link AndroidBitmapFormat} the AImageDecoder will decode to
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500513 * by default. {@link AImageDecoder} will try to choose one that is sensible
Leon Scroggins III2f984942019-11-22 17:02:23 -0500514 * for the image and the system. Note that this does not indicate the
515 * encoded format of the image.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700516 *
517 * Available since API level 30.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500518 */
Leon Scroggins III5d0445c2020-01-23 09:43:43 -0500519int32_t AImageDecoderHeaderInfo_getAndroidBitmapFormat(
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400520 const AImageDecoderHeaderInfo* _Nonnull) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500521
522/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500523 * Report how the {@link AImageDecoder} will handle alpha by default. If the image
Leon Scroggins III2f984942019-11-22 17:02:23 -0500524 * contains no alpha (according to its header), this will return
525 * {@link ANDROID_BITMAP_FLAGS_ALPHA_OPAQUE}. If the image may contain alpha,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500526 * this returns {@link ANDROID_BITMAP_FLAGS_ALPHA_PREMUL}, because
527 * {@link AImageDecoder_decodeImage} will premultiply pixels by default.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700528 *
529 * Available since API level 30.
Leon Scroggins IIIac9f7492020-12-28 15:33:13 -0500530 *
531 * Starting in API level 31, an AImageDecoder may contain multiple frames of an
532 * animation, but this method still only reports whether the first frame has
533 * alpha.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500534 */
535int AImageDecoderHeaderInfo_getAlphaFlags(
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400536 const AImageDecoderHeaderInfo* _Nonnull) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500537
538/**
Leon Scroggins III8cee65e2020-01-30 14:20:42 -0500539 * Report the dataspace the AImageDecoder will decode to by default.
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500540 *
Leon Scroggins III8cee65e2020-01-30 14:20:42 -0500541 * By default, {@link AImageDecoder_decodeImage} will not do any color
542 * conversion.
543 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700544 * Available since API level 30.
545 *
Leon Scroggins III8cee65e2020-01-30 14:20:42 -0500546 * @return The {@link ADataSpace} representing the way the colors
547 * are encoded (or {@link ADATASPACE_UNKNOWN} if there is not a
548 * corresponding ADataSpace). This specifies how to interpret the colors
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500549 * in the decoded image, unless {@link AImageDecoder_setDataSpace} is
550 * called to decode to a different ADataSpace.
551 *
552 * Note that ADataSpace only exposes a few values. This may return
Leon Scroggins III8cee65e2020-01-30 14:20:42 -0500553 * {@link ADATASPACE_UNKNOWN}, even for Named ColorSpaces, if they have
554 * no corresponding {@link ADataSpace}.
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500555 */
556int32_t AImageDecoderHeaderInfo_getDataSpace(
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400557 const AImageDecoderHeaderInfo* _Nonnull) __INTRODUCED_IN(30);
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500558
559/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500560 * Return the minimum stride that can be used in
561 * {@link AImageDecoder_decodeImage).
562 *
563 * This stride provides no padding, meaning it will be exactly equal to the
564 * width times the number of bytes per pixel for the {@link AndroidBitmapFormat}
565 * being used.
566 *
567 * If the output is scaled (via {@link AImageDecoder_setTargetSize}) and/or
568 * cropped (via {@link AImageDecoder_setCrop}), this takes those into account.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700569 *
570 * Available since API level 30.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500571 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400572size_t AImageDecoder_getMinimumStride(AImageDecoder* _Nonnull decoder) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500573
574/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500575 * Decode the image into pixels, using the settings of the {@link AImageDecoder}.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500576 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700577 * Available since API level 30.
578 *
Leon Scroggins III1b389712020-10-09 13:12:39 -0400579 * 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 -0500580 * animated image (i.e. GIF, WebP) using new APIs. Internally,
Leon Scroggins III55287002020-10-13 11:30:11 -0400581 * AImageDecoder keeps track of its "current frame" - that is, the frame that
582 * will be decoded by a call to AImageDecoder_decodeImage. At creation time, the
583 * current frame is always the first frame, and multiple calls to this method
584 * will each decode the first frame. {@link AImageDecoder_advanceFrame} advances
585 * the current frame to the following frame, so that future calls to this method
586 * will decode that frame. Some frames may update only part of the image. They
587 * may only update a sub-rectangle (see {@link
588 * AImageDecoderFrameInfo_getFrameRect}), or they may have alpha (see
589 * {@link AImageDecoderFrameInfo_hasAlphaWithinBounds}). In these cases, this
590 * method assumes that the prior frame is still residing in the |pixels| buffer,
591 * decodes only the new portion, and blends it with the buffer. Frames that change
592 * the entire |pixels| buffer are "independent", and do not require the prior
593 * frame to remain in the buffer. The first frame is always independent. A
594 * sophisticated client can use information from the {@link AImageDecoderFrameInfo}
595 * to determine whether other frames are independent, or what frames they rely on.
596 *
597 * If the current frame is marked {@link ANDROID_IMAGE_DECODER_DISPOSE_OP_PREVIOUS},
Leon Scroggins III78859b42021-01-13 11:48:34 -0500598 * AImageDecoder_decodeImage will store the |pixels| buffer prior to decoding
Leon Scroggins III55287002020-10-13 11:30:11 -0400599 * (note: this only happens for the first in a string of consecutive
600 * ANDROID_IMAGE_DECODER_DISPOSE_OP_PREVIOUS frames). After advancing to the
Leon Scroggins III78859b42021-01-13 11:48:34 -0500601 * following frame, AImageDecoder_decodeImage will restore that buffer prior to
602 * decoding that frame. This is the default behavior, but it can be disabled
603 * by passing false to {@link AImageDecoder_setInternallyHandleDisposePrevious}.
Leon Scroggins III55287002020-10-13 11:30:11 -0400604 *
605 * Ignoring timing information, display, etc, a client wishing to decode all
606 * frames of an animated image may conceptually use code like the following:
607 *
608 * while (true) {
609 * int result = AImageDecoder_decodeImage(decoder, pixels, stride, size);
610 * if (result != ANDROID_IMAGE_DECODER_SUCCESS) break;
611 *
612 * // Display or save the image in |pixels|, keeping the buffer intact for
613 * // AImageDecoder to decode the next frame correctly.
614 * Application_viewImage(pixels);
615 *
616 * result = AImageDecoder_advanceFrame(decoder);
617 * if (result != ANDROID_IMAGE_DECODER_SUCCESS) break;
618 * }
Leon Scroggins III1b389712020-10-09 13:12:39 -0400619 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500620 * @param decoder Opaque object representing the decoder.
621 * @param pixels On success, will be filled with the result
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500622 * of the decode. Must be large enough to hold |size| bytes.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500623 * @param stride Width in bytes of a single row. Must be at least
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500624 * {@link AImageDecoder_getMinimumStride} and a multiple of the
625 * bytes per pixel of the {@link AndroidBitmapFormat}.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500626 * @param size Size of the pixel buffer in bytes. Must be at least
627 * stride * (height - 1) +
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500628 * {@link AImageDecoder_getMinimumStride}.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500629 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
630 * indicating the reason for the failure.
631 *
632 * Errors:
633 * - {@link ANDROID_IMAGE_DECODER_INCOMPLETE}: The image was truncated. A
634 * partial image was decoded, and undecoded lines have been initialized to all
635 * zeroes.
636 * - {@link ANDROID_IMAGE_DECODER_ERROR}: The image contained an error. A
637 * partial image was decoded, and undecoded lines have been initialized to all
638 * zeroes.
639 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The {@link AImageDecoder} or
640 * |pixels| is null, the stride is not large enough or not pixel aligned, or
641 * |size| is not large enough.
642 * - {@link ANDROID_IMAGE_DECODER_SEEK_ERROR}: The asset or file descriptor
643 * failed to seek.
644 * - {@link ANDROID_IMAGE_DECODER_INTERNAL_ERROR}: Some other error, like a
645 * failure to allocate memory.
Leon Scroggins III55287002020-10-13 11:30:11 -0400646 * - {@link ANDROID_IMAGE_DECODER_FINISHED}: The input contains no
647 * more frames. No decoding occurred. The client must call
648 * {@link AImageDecoder_rewind} before calling
649 * {@link AImageDecoder_decodeImage} again.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500650 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400651int AImageDecoder_decodeImage(AImageDecoder* _Nonnull decoder,
652 void* _Nonnull pixels, size_t stride,
Leon Scroggins III2f984942019-11-22 17:02:23 -0500653 size_t size) __INTRODUCED_IN(30);
654
Leon Scroggins III1b389712020-10-09 13:12:39 -0400655/**
656 * Return true iff the image is animated - i.e. has multiple frames.
657 *
658 * Introduced in API 31.
659 *
Leon Scroggins III55287002020-10-13 11:30:11 -0400660 * A single frame GIF is considered to *not* be animated. This may require
661 * seeking past the first frame to verify whether there is a following frame.
Leon Scroggins III1b389712020-10-09 13:12:39 -0400662 *
663 * Errors:
664 * - returns false if |decoder| is null.
665 */
666bool AImageDecoder_isAnimated(AImageDecoder* _Nonnull decoder)
667 __INTRODUCED_IN(31);
668
Leon Scroggins III728a9ac2020-10-12 10:34:05 -0400669enum {
670 /*
671 * Reported by {@link AImageDecoder_getRepeatCount} if the
672 * animation should repeat forever.
Leon Scroggins III3b3700e2020-12-29 11:52:12 -0500673 *
674 * Introduced in API 31
Leon Scroggins III728a9ac2020-10-12 10:34:05 -0400675 */
676 ANDROID_IMAGE_DECODER_INFINITE = INT32_MAX,
677};
678
679/**
680 * Report how many times the animation should repeat.
681 *
682 * Introduced in API 31.
683 *
684 * This does not include the first play through. e.g. a repeat
685 * count of 4 means that each frame is played 5 times.
686 *
687 * {@link ANDROID_IMAGE_DECODER_INFINITE} means to repeat forever.
688 *
689 * This may require seeking.
690 *
691 * For non-animated formats, this returns 0. It may return non-zero for
692 * an image with only one frame (i.e. {@link AImageDecoder_isAnimated} returns
693 * false) if the encoded image contains a repeat count.
694 *
695 * @return Number of times to repeat on success or a value
696 * indicating the reason for the failure.
697 *
698 * Errors:
699 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The AImageDecoder
700 * is null.
701 */
Jiyong Parkf1f20902021-01-13 08:28:13 +0900702int32_t AImageDecoder_getRepeatCount(AImageDecoder* _Nonnull decoder)
Leon Scroggins III728a9ac2020-10-12 10:34:05 -0400703 __INTRODUCED_IN(31);
704
Leon Scroggins III55287002020-10-13 11:30:11 -0400705/**
706 * Advance to the next frame in the animation.
707 *
708 * Introduced in API 31.
709 *
710 * The AImageDecoder keeps track internally which frame it is ready to decode
711 * (the "current frame"). Initially it is set to decode the first frame, and
712 * each call to {@link AImageDecoder_decodeImage} will continue to decode
713 * the same frame until this method (or {@link AImageDecoder_rewind})
714 * is called.
715 *
716 * Note that this can be used to skip a frame without decoding it. But
717 * some frames depend on (i.e. blend with) prior frames, and
718 * AImageDecoder_decodeImage assumes that the prior frame is in the
719 * |pixels| buffer. In addition, AImageDecoder_decodeImage handles caching and
720 * restoring frames (see {@link ANDROID_IMAGE_DECODER_DISPOSE_OP_PREVIOUS}), so
721 * skipping frames in an image with such frames may not produce the correct
722 * results.
723 *
724 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
725 * indicating the reason for the failure.
726 *
727 * Errors:
728 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The AImageDecoder
729 * represents an image that is not animated (see
730 * {@link AImageDecoder_isAnimated}) or the AImageDecoder is null.
731 * - {@link ANDROID_IMAGE_DECODER_INCOMPLETE}: The input appears
732 * to be truncated. The client must call {@link AImageDecoder_rewind}
733 * before calling {@link AImageDecoder_decodeImage} again.
734 * - {@link ANDROID_IMAGE_DECODER_ERROR}: The input contains an error.
735 * The client must call {@link AImageDecoder_rewind} before
736 * calling {@link AImageDecoder_decodeImage} again.
737 * - {@link ANDROID_IMAGE_DECODER_FINISHED}: The input contains no
738 * more frames. The client must call {@link AImageDecoder_rewind}
739 * before calling {@link AImageDecoder_decodeImage} again.
740 */
741int AImageDecoder_advanceFrame(AImageDecoder* _Nonnull decoder)
742 __INTRODUCED_IN(31);
743
744/**
745 * Return to the beginning of the animation.
746 *
747 * Introduced in API 31.
748 *
749 * After this call, the AImageDecoder will be ready to decode the
750 * first frame of the animation. This can be called after reaching
751 * the end of the animation or an error or in the middle of the
752 * animation.
753 *
754 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
755 * indicating the reason for the failure.
756 *
757 * Errors:
758 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The AImageDecoder
759 * represents an image that is not animated (see
760 * {@link AImageDecoder_isAnimated}) or the AImageDecoder is
761 * null.
762 * - {@link ANDROID_IMAGE_DECODER_SEEK_ERROR}: The asset or file
763 * descriptor failed to seek.
764 */
765int AImageDecoder_rewind(AImageDecoder* _Nonnull decoder)
766 __INTRODUCED_IN(31);
767
Leon Scroggins IIIac9f7492020-12-28 15:33:13 -0500768struct AImageDecoderFrameInfo;
769
770/**
771 * Opaque handle to animation information about a single frame.
772 *
773 * Introduced in API 31
774 *
775 * The duration (retrieved with {@link AImageDecoderFrameInfo_getDuration}) is
776 * necessary for clients to display the animation at the proper speed. The other
777 * information is helpful for a client that wants to determine what frames are
778 * independent (or what frames they depend on), but is unnecessary for
779 * a simple client that wants to sequentially display all frames.
780 */
781typedef struct AImageDecoderFrameInfo AImageDecoderFrameInfo;
782
Leon Scroggins IIIac9f7492020-12-28 15:33:13 -0500783/**
784 * Create an uninitialized AImageDecoderFrameInfo.
785 *
786 * Introduced in API 31.
787 *
788 * This can be passed to {@link AImageDecoder_getFrameInfo} to fill
789 * in information about the current frame. It may be reused.
790 *
791 * Must be deleted with {@link AImageDecoderFrameInfo_delete}.
792 */
793AImageDecoderFrameInfo* _Nullable AImageDecoderFrameInfo_create()
794 __INTRODUCED_IN(31);
795
796/**
797 * Delete an AImageDecoderFrameInfo.
798 *
799 * Introduced in API 31.
800 */
801void AImageDecoderFrameInfo_delete(
802 AImageDecoderFrameInfo* _Nullable info) __INTRODUCED_IN(31);
803
804/**
805 * Fill |info| with information about the current frame.
806 *
807 * Introduced in API 31.
808 *
809 * Initially, this will return information about the first frame.
810 * {@link AImageDecoder_advanceFrame} and
811 * {@link AImageDecoder_rewind} can be used to change which frame
812 * is the current frame.
813 *
814 * If the image only has one frame, this will fill the {@link
815 * AImageDecoderFrameInfo} with the encoded info, if any, or reasonable
816 * defaults.
817 *
818 * @param decoder Opaque object representing the decoder.
819 * @param info Opaque object to hold frame information. On success, will be
820 * filled with information regarding the current frame.
821 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
822 * indicating the reason for the failure.
823 *
824 * Errors:
825 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: One of the parameters is null.
826 * - {@link ANDROID_IMAGE_DECODER_FINISHED}: The input contains no
827 * more frames. The client must call {@link AImageDecoder_rewind} to reset the
828 * current frame to a valid frame (0).
829 */
830int AImageDecoder_getFrameInfo(AImageDecoder* _Nonnull decoder,
831 AImageDecoderFrameInfo* _Nonnull info) __INTRODUCED_IN(31);
832
833/**
834 * Report the number of nanoseconds to show the current frame.
835 *
836 * Introduced in API 31.
837 *
838 * Errors:
839 * - returns 0 if |info| is null.
840 */
841int64_t AImageDecoderFrameInfo_getDuration(
842 const AImageDecoderFrameInfo* _Nonnull info) __INTRODUCED_IN(31);
843
844/**
845 * The rectangle of the image (within 0, 0,
846 * {@link AImageDecoder_getWidth}, {@link AImageDecoder_getHeight})
847 * updated by this frame.
848 *
849 * Introduced in API 31.
850 *
851 * Note that this is unaffected by calls to
852 * {@link AImageDecoder_setTargetSize} or
853 * {@link AImageDecoder_setCrop}.
854 *
855 * A frame may update only part of the image. This will always be
856 * contained by the image’s dimensions.
857 *
858 * This, along with other information in AImageDecoderFrameInfo,
859 * can be useful for determining whether a frame is independent, but
860 * the decoder handles blending frames, so a simple
861 * sequential client does not need this.
862 *
863 * Errors:
864 * - returns an empty ARect if |info| is null.
865 */
866ARect AImageDecoderFrameInfo_getFrameRect(
867 const AImageDecoderFrameInfo* _Nonnull info) __INTRODUCED_IN(31);
868
869/**
870 * Whether the new portion of this frame may contain alpha.
871 *
872 * Introduced in API 31.
873 *
874 * Note that this may differ from whether the composed frame has
875 * alpha. If this frame does not fill the entire image dimensions
876 * (see {@link AImageDecoderFrameInfo_getFrameRect}) or it blends
877 * with an opaque frame, for example, the composed frame’s alpha
878 * may not match. It is also conservative; for example, if a color
879 * index-based frame has a color with alpha but does not use it,
880 * this will still return true.
881 *
882 * This, along with other information in AImageDecoderFrameInfo,
883 * can be useful for determining whether a frame is independent, but
884 * the decoder handles blending frames, so a simple
885 * sequential client does not need this.
886 *
887 * Errors:
888 * - returns false if |info| is null.
889 */
890bool AImageDecoderFrameInfo_hasAlphaWithinBounds(
891 const AImageDecoderFrameInfo* _Nonnull info) __INTRODUCED_IN(31);
892
Leon Scroggins IIIac9f7492020-12-28 15:33:13 -0500893/**
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,
Leon Scroggins III78859b42021-01-13 11:48:34 -0500907 // The frame’s rectangle is cleared to transparent (by AImageDecoder)
908 // before decoding the next frame.
Leon Scroggins IIIac9f7492020-12-28 15:33:13 -0500909 ANDROID_IMAGE_DECODER_DISPOSE_OP_BACKGROUND = 2,
Leon Scroggins III78859b42021-01-13 11:48:34 -0500910 // The frame’s rectangle is reverted to the prior frame before decoding
911 // the next frame. This is handled by AImageDecoder, unless
912 // {@link AImageDecoder_setInternallyHandleDisposePrevious} is set to false.
Leon Scroggins IIIac9f7492020-12-28 15:33:13 -0500913 ANDROID_IMAGE_DECODER_DISPOSE_OP_PREVIOUS = 3,
914};
915
Leon Scroggins IIIac9f7492020-12-28 15:33:13 -0500916/**
917 * Return how this frame is “disposed” before showing the next one.
918 *
919 * Introduced in API 31.
920 *
921 * This, along with other information in AImageDecoderFrameInfo,
922 * can be useful for determining whether a frame is independent, but
923 * the decoder handles disposing of frames, so a simple
924 * sequential client does not need this.
925 *
926 * @return one of:
927 * - {@link ANDROID_IMAGE_DECODER_DISPOSE_OP_NONE}
928 * - {@link ANDROID_IMAGE_DECODER_DISPOSE_OP_BACKGROUND}
929 * - {@link ANDROID_IMAGE_DECODER_DISPOSE_OP_PREVIOUS}
930 * Errors:
931 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER} if |info| is null.
932 */
933int32_t AImageDecoderFrameInfo_getDisposeOp(
934 const AImageDecoderFrameInfo* _Nonnull info) __INTRODUCED_IN(31);
935
Leon Scroggins IIIac9f7492020-12-28 15:33:13 -0500936/**
937 * How a frame is blended with the previous frame.
938 *
939 * Introduced in API 31.
940 *
941 * This, along with other information in AImageDecoderFrameInfo,
942 * can be useful for determining whether a frame is independent, but
943 * the decoder handles blending frames, so a simple
944 * sequential client does not need this.
945 */
946enum {
947 // This frame replaces existing content. This corresponds
948 // to webp’s “do not blend”.
949 ANDROID_IMAGE_DECODER_BLEND_OP_SRC = 1,
950 // This frame blends with the previous frame.
951 ANDROID_IMAGE_DECODER_BLEND_OP_SRC_OVER = 2,
952};
953
Leon Scroggins IIIac9f7492020-12-28 15:33:13 -0500954/**
955 * Return how this frame is blended with the previous frame.
956 *
957 * Introduced in API 31.
958 *
959 * This, along with other information in AImageDecoderFrameInfo,
960 * can be useful for determining whether a frame is independent, but
961 * the decoder handles blending frames, so a simple
962 * sequential client does not need this.
963 *
964 * @return one of:
965 * - {@link ANDROID_IMAGE_DECODER_BLEND_OP_SRC}
966 * - {@link ANDROID_IMAGE_DECODER_BLEND_OP_SRC_OVER}
967 * Errors:
968 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER} if |info| is null.
969 */
970int32_t AImageDecoderFrameInfo_getBlendOp(
971 const AImageDecoderFrameInfo* _Nonnull info)
972 __INTRODUCED_IN(31);
973
Leon Scroggins III78859b42021-01-13 11:48:34 -0500974/**
975 * Whether to have AImageDecoder store the frame prior to a
976 * frame marked {@link ANDROID_IMAGE_DECODER_DISPOSE_OP_PREVIOUS}.
977 *
978 * Introduced in API 31.
979 *
980 * The default is true. Many images will not have such a frame (it
981 * is not supported by WebP, and only some GIFs use it). But
982 * if frame i is ANDROID_IMAGE_DECODER_DISPOSE_OP_PREVIOUS, then i+1
983 * may depend on i-1. When this setting is true, AImageDecoder will
984 * defensively copy frame i-1 (i.e. the contents of |pixels| in
985 * {@link AImageDecoder_decodeImage}) into an internal buffer so that
986 * it can be used to decode i+1.
987 *
988 * AImageDecoder will only store a single frame, at the size specified
989 * by {@link AImageDecoder_setTargetSize} (or the original dimensions
990 * if that method has not been called), and will discard it when it is
991 * no longer necessary.
992 *
993 * A client that desires to manually store such frames may set this to
994 * false, so that AImageDecoder does not need to store this extra
995 * frame. Instead, when decoding the same
996 * ANDROID_IMAGE_DECODER_DISPOSE_OP_PREVIOUS frame i, AImageDecoder
997 * will decode directly into |pixels|, assuming the client stored i-1.
998 * When asked to decode frame i+1, AImageDecoder will now assume that
999 * the client provided i-1 in |pixels|.
1000 *
1001 * @param handleInternally Whether AImageDecoder will internally
1002 * handle ANDROID_IMAGE_DECODER_DISPOSE_OP_PREVIOUS
1003 * frames.
1004 */
1005void AImageDecoder_setInternallyHandleDisposePrevious(
1006 AImageDecoder* _Nonnull decoder, bool handleInternally)
1007 __INTRODUCED_IN(31);
1008
1009
Leon Scroggins III2f984942019-11-22 17:02:23 -05001010#ifdef __cplusplus
1011}
1012#endif
1013
1014#endif // ANDROID_IMAGE_DECODER_H
1015
1016/** @} */