blob: 49a616fce1a4f8327563cfcb1d91fc76904d2cd9 [file] [log] [blame]
Leon Scroggins III2f984942019-11-22 17:02:23 -05001/*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050018 * @defgroup ImageDecoder
19 *
20 * Functions for converting encoded images into RGBA pixels.
21 *
22 * Similar to the Java counterpart android.graphics.ImageDecoder, it can be used
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -050023 * to decode images in the following formats:
24 * - JPEG
25 * - PNG
26 * - GIF
27 * - WebP
28 * - BMP
29 * - ICO
30 * - WBMP
31 * - HEIF
32 * - Digital negatives (via the DNG SDK)
33 * <p>It has similar options for scaling, cropping, and choosing the output format.
34 * Unlike the Java API, which can create an android.graphics.Bitmap or
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050035 * android.graphics.drawable.Drawable object, AImageDecoder decodes directly
36 * into memory provided by the client. For more information, see the
37 * <a href="https://developer.android.com/ndk/guides/image-decoder">Image decoder</a>
38 * developer guide.
Leon Scroggins III2f984942019-11-22 17:02:23 -050039 * @{
40 */
41
42/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050043 * @file imagedecoder.h
44 * @brief API for decoding images.
Leon Scroggins III2f984942019-11-22 17:02:23 -050045 */
46
47#ifndef ANDROID_IMAGE_DECODER_H
48#define ANDROID_IMAGE_DECODER_H
49
50#include "bitmap.h"
Leon Scroggins IIIa9f397b2020-01-27 12:42:56 -050051#include <android/rect.h>
Leon Scroggins III2f984942019-11-22 17:02:23 -050052#include <stdint.h>
53
54#ifdef __cplusplus
55extern "C" {
56#endif
57
58struct AAsset;
Leon Scroggins III2f984942019-11-22 17:02:23 -050059
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050060/**
Leon Scroggins III3b3700e2020-12-29 11:52:12 -050061 * {@link AImageDecoder} functions result code.
62 *
63 * Introduced in API 30.
64 *
65 * Many functions will return this to indicate success
66 * ({@link ANDROID_IMAGE_DECODER_SUCCESS}) or the reason for the failure. On
67 * failure, any out-parameters should be considered uninitialized, except where
68 * specified.
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050069 */
Leon Scroggins III2f984942019-11-22 17:02:23 -050070enum {
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050071 /**
72 * Decoding was successful and complete.
73 */
Leon Scroggins III2f984942019-11-22 17:02:23 -050074 ANDROID_IMAGE_DECODER_SUCCESS = 0,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050075 /**
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -050076 * The input is incomplete.
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050077 */
Leon Scroggins III2f984942019-11-22 17:02:23 -050078 ANDROID_IMAGE_DECODER_INCOMPLETE = -1,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050079 /**
80 * The input contained an error after decoding some lines.
81 */
Leon Scroggins III2f984942019-11-22 17:02:23 -050082 ANDROID_IMAGE_DECODER_ERROR = -2,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050083 /**
84 * Could not convert. For example, attempting to decode an image with
85 * alpha to an opaque format.
86 */
Leon Scroggins III2f984942019-11-22 17:02:23 -050087 ANDROID_IMAGE_DECODER_INVALID_CONVERSION = -3,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050088 /**
89 * The scale is invalid. It may have overflowed, or it may be incompatible
90 * with the current alpha setting.
91 */
Leon Scroggins III2f984942019-11-22 17:02:23 -050092 ANDROID_IMAGE_DECODER_INVALID_SCALE = -4,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050093 /**
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -050094 * Some other parameter is invalid.
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050095 */
Leon Scroggins III2f984942019-11-22 17:02:23 -050096 ANDROID_IMAGE_DECODER_BAD_PARAMETER = -5,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050097 /**
98 * Input was invalid before decoding any pixels.
99 */
Leon Scroggins III2f984942019-11-22 17:02:23 -0500100 ANDROID_IMAGE_DECODER_INVALID_INPUT = -6,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500101 /**
102 * A seek was required and it failed.
103 */
Leon Scroggins III2f984942019-11-22 17:02:23 -0500104 ANDROID_IMAGE_DECODER_SEEK_ERROR = -7,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500105 /**
106 * Some other error. For example, an internal allocation failed.
107 */
Leon Scroggins III2f984942019-11-22 17:02:23 -0500108 ANDROID_IMAGE_DECODER_INTERNAL_ERROR = -8,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500109 /**
110 * AImageDecoder did not recognize the format.
111 */
Leon Scroggins III2f984942019-11-22 17:02:23 -0500112 ANDROID_IMAGE_DECODER_UNSUPPORTED_FORMAT = -9
113};
114
115struct AImageDecoder;
116
117/**
118 * Opaque handle for decoding images.
119 *
Leon Scroggins III3b3700e2020-12-29 11:52:12 -0500120 * Introduced in API 30
121 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500122 * Create using one of the following:
123 * - {@link AImageDecoder_createFromAAsset}
124 * - {@link AImageDecoder_createFromFd}
125 * - {@link AImageDecoder_createFromBuffer}
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500126 *
127 * After creation, {@link AImageDecoder_getHeaderInfo} can be used to retrieve
128 * information about the encoded image. Other functions, like
129 * {@link AImageDecoder_setTargetSize}, can be used to specify how to decode, and
130 * {@link AImageDecoder_decode} will decode into client provided memory.
131 *
132 * {@link AImageDecoder} objects are NOT thread-safe, and should not be shared across
133 * threads.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500134 */
135typedef struct AImageDecoder AImageDecoder;
136
Leon Scroggins III3b3700e2020-12-29 11:52:12 -0500137#if __ANDROID_API__ >= 30
138
Leon Scroggins III2f984942019-11-22 17:02:23 -0500139/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500140 * Create a new {@link AImageDecoder} from an {@link AAsset}.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500141 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700142 * Available since API level 30.
143 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500144 * @param asset {@link AAsset} containing encoded image data. Client is still
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500145 * responsible for calling {@link AAsset_close} on it, which may be
146 * done after deleting the returned {@link AImageDecoder}.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500147 * @param outDecoder On success (i.e. return value is
148 * {@link ANDROID_IMAGE_DECODER_SUCCESS}), this will be set to
149 * a newly created {@link AImageDecoder}. Caller is
150 * responsible for calling {@link AImageDecoder_delete} on it.
151 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500152 * indicating the reason for the failure.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500153 *
154 * Errors:
155 * - {@link ANDROID_IMAGE_DECODER_INCOMPLETE}: The asset was truncated before
156 * reading the image header.
157 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: One of the parameters is
158 * null.
159 * - {@link ANDROID_IMAGE_DECODER_INVALID_INPUT}: There is an error in the
160 * header.
161 * - {@link ANDROID_IMAGE_DECODER_SEEK_ERROR}: The asset failed to seek.
162 * - {@link ANDROID_IMAGE_DECODER_INTERNAL_ERROR}: Some other error, like a
163 * failure to allocate memory.
164 * - {@link ANDROID_IMAGE_DECODER_UNSUPPORTED_FORMAT}: The format is not
165 * supported.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500166 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400167int AImageDecoder_createFromAAsset(struct AAsset* _Nonnull asset,
168 AImageDecoder* _Nonnull * _Nonnull outDecoder)
Leon Scroggins IIIa9f397b2020-01-27 12:42:56 -0500169 __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500170
171/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500172 * Create a new {@link AImageDecoder} from a file descriptor.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500173 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700174 * Available since API level 30.
175 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500176 * @param fd Seekable, readable, open file descriptor for encoded data.
177 * Client is still responsible for closing it, which may be done
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500178 * after deleting the returned {@link AImageDecoder}.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500179 * @param outDecoder On success (i.e. return value is
180 * {@link ANDROID_IMAGE_DECODER_SUCCESS}), this will be set to
181 * a newly created {@link AImageDecoder}. Caller is
182 * responsible for calling {@link AImageDecoder_delete} on it.
183 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500184 * indicating the reason for the failure.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500185 *
186 * Errors:
187 * - {@link ANDROID_IMAGE_DECODER_INCOMPLETE}: The file was truncated before
188 * reading the image header.
189 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The {@link AImageDecoder} is
190 * null, or |fd| does not represent a valid, seekable file descriptor.
191 * - {@link ANDROID_IMAGE_DECODER_INVALID_INPUT}: There is an error in the
192 * header.
193 * - {@link ANDROID_IMAGE_DECODER_SEEK_ERROR}: The descriptor failed to seek.
194 * - {@link ANDROID_IMAGE_DECODER_INTERNAL_ERROR}: Some other error, like a
195 * failure to allocate memory.
196 * - {@link ANDROID_IMAGE_DECODER_UNSUPPORTED_FORMAT}: The format is not
197 * supported.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500198 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400199int AImageDecoder_createFromFd(int fd, AImageDecoder* _Nonnull * _Nonnull outDecoder)
200 __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500201
202/**
203 * Create a new AImageDecoder from a buffer.
204 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700205 * Available since API level 30.
206 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500207 * @param buffer Pointer to encoded data. Must be valid for the entire time
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500208 * the {@link AImageDecoder} is used.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500209 * @param length Byte length of buffer.
210 * @param outDecoder On success (i.e. return value is
211 * {@link ANDROID_IMAGE_DECODER_SUCCESS}), this will be set to
212 * a newly created {@link AImageDecoder}. Caller is
213 * responsible for calling {@link AImageDecoder_delete} on it.
214 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500215 * indicating the reason for the failure.
216 *
217 * Errors:
218 * - {@link ANDROID_IMAGE_DECODER_INCOMPLETE}: The encoded image was truncated before
219 * reading the image header.
220 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: One of the parameters is
221 * invalid.
222 * - {@link ANDROID_IMAGE_DECODER_INVALID_INPUT}: There is an error in the
223 * header.
224 * - {@link ANDROID_IMAGE_DECODER_INTERNAL_ERROR}: Some other error, like a
225 * failure to allocate memory.
226 * - {@link ANDROID_IMAGE_DECODER_UNSUPPORTED_FORMAT}: The format is not
227 * supported.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500228 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400229int AImageDecoder_createFromBuffer(const void* _Nonnull buffer, size_t length,
230 AImageDecoder* _Nonnull * _Nonnull outDecoder)
231 __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500232
233/**
234 * Delete the AImageDecoder.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700235 *
236 * Available since API level 30.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500237 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400238void AImageDecoder_delete(AImageDecoder* _Nonnull decoder) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500239
240/**
241 * Choose the desired output format.
242 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700243 * Available since API level 30.
244 *
Leon Scroggins III5d0445c2020-01-23 09:43:43 -0500245 * @param format {@link AndroidBitmapFormat} to use for the output.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500246 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
247 * indicating the reason for the failure. On failure, the
248 * {@link AImageDecoder} uses the format it was already planning
249 * to use (either its default or a previously successful setting
250 * from this function).
251 *
252 * Errors:
253 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The
254 * {@link AImageDecoder} is null or |format| does not correspond to an
255 * {@link AndroidBitmapFormat}.
256 * - {@link ANDROID_IMAGE_DECODER_INVALID_CONVERSION}: The
257 * {@link AndroidBitmapFormat} is incompatible with the image.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500258 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400259int AImageDecoder_setAndroidBitmapFormat(AImageDecoder* _Nonnull decoder,
Leon Scroggins III2f984942019-11-22 17:02:23 -0500260 int32_t format) __INTRODUCED_IN(30);
261
Leon Scroggins III1be112f2020-01-15 04:26:44 -0500262/**
263 * Specify whether the output's pixels should be unpremultiplied.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500264 *
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500265 * By default, {@link AImageDecoder_decodeImage} will premultiply the pixels, if they have alpha.
266 * Pass true to this method to leave them unpremultiplied. This has no effect on an
Leon Scroggins III1be112f2020-01-15 04:26:44 -0500267 * opaque image.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500268 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700269 * Available since API level 30.
270 *
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500271 * @param unpremultipliedRequired Pass true to leave the pixels unpremultiplied.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500272 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
273 * indicating the reason for the failure.
274 *
275 * Errors:
276 * - {@link ANDROID_IMAGE_DECODER_INVALID_CONVERSION}: Unpremultiplied is not
277 * possible due to an existing scale set by
278 * {@link AImageDecoder_setTargetSize}.
279 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The
280 * {@link AImageDecoder} is null.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500281 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400282int AImageDecoder_setUnpremultipliedRequired(AImageDecoder* _Nonnull decoder,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500283 bool unpremultipliedRequired) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500284
285/**
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500286 * Choose the dataspace for the output.
287 *
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500288 * Ignored by {@link ANDROID_BITMAP_FORMAT_A_8}, which does not support
289 * an {@link ADataSpace}.
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500290 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700291 * Available since API level 30.
292 *
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500293 * @param dataspace The {@link ADataSpace} to decode into. An ADataSpace
294 * specifies how to interpret the colors. By default,
295 * AImageDecoder will decode into the ADataSpace specified by
296 * {@link AImageDecoderHeaderInfo_getDataSpace}. If this
297 * parameter is set to a different ADataSpace, AImageDecoder
298 * will transform the output into the specified ADataSpace.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500299 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
300 * indicating the reason for the failure.
301 *
302 * Errors:
303 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The
304 * {@link AImageDecoder} is null or |dataspace| does not correspond to an
305 * {@link ADataSpace} value.
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500306 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400307int AImageDecoder_setDataSpace(AImageDecoder* _Nonnull decoder, int32_t dataspace)
308 __INTRODUCED_IN(30);
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500309
310/**
Leon Scroggins III2f984942019-11-22 17:02:23 -0500311 * Specify the output size for a decoded image.
312 *
313 * Future calls to {@link AImageDecoder_decodeImage} will sample or scale the
314 * encoded image to reach the desired size. If a crop rect is set (via
315 * {@link AImageDecoder_setCrop}), it must be contained within the dimensions
316 * specified by width and height, and the output image will be the size of the
317 * crop rect.
318 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700319 * Available since API level 30.
320 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500321 * @param width Width of the output (prior to cropping).
322 * This will affect future calls to
323 * {@link AImageDecoder_getMinimumStride}, which will now return
324 * a value based on this width.
325 * @param height Height of the output (prior to cropping).
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500326 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
327 * indicating the reason for the failure.
328 *
329 * Errors:
330 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The
331 * {@link AImageDecoder} is null.
332 * - {@link ANDROID_IMAGE_DECODER_INVALID_SCALE}: |width| or |height| is <= 0,
333 * the size is too big, any existing crop is not contained by the new image dimensions,
334 * or the scale is incompatible with a previous call to
335 * {@link AImageDecoder_setUnpremultipliedRequired}(true).
Leon Scroggins III2f984942019-11-22 17:02:23 -0500336 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400337int AImageDecoder_setTargetSize(AImageDecoder* _Nonnull decoder, int32_t width,
338 int32_t height) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500339
Leon Scroggins IIIf27256b2020-01-19 21:13:04 -0500340/**
341 * Compute the dimensions to use for a given sampleSize.
342 *
343 * Although AImageDecoder can scale to an arbitrary target size (see
344 * {@link AImageDecoder_setTargetSize}), some sizes may be more efficient than
345 * others. This computes the most efficient target size to use to reach a
346 * particular sampleSize.
347 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700348 * Available since API level 30.
349 *
Leon Scroggins IIIf27256b2020-01-19 21:13:04 -0500350 * @param sampleSize A subsampling rate of the original image. Must be greater
351 * than or equal to 1. A sampleSize of 2 means to skip every
352 * other pixel/line, resulting in a width and height that are
353 * 1/2 of the original dimensions, with 1/4 the number of
354 * pixels.
355 * @param width Out parameter for the width sampled by sampleSize, and rounded
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500356 * in the direction that the decoder can do most efficiently.
Leon Scroggins IIIf27256b2020-01-19 21:13:04 -0500357 * @param height Out parameter for the height sampled by sampleSize, and rounded
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500358 * in the direction that the decoder can do most efficiently.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500359 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
360 * indicating the reason for the failure.
361 *
362 * Errors:
363 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The
364 * {@link AImageDecoder}, |width| or |height| is null or |sampleSize| is < 1.
Leon Scroggins IIIf27256b2020-01-19 21:13:04 -0500365 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400366int AImageDecoder_computeSampledSize(const AImageDecoder* _Nonnull decoder, int sampleSize,
367 int32_t* _Nonnull width, int32_t* _Nonnull height)
368 __INTRODUCED_IN(30);
Leon Scroggins III3b3700e2020-12-29 11:52:12 -0500369
Leon Scroggins III2f984942019-11-22 17:02:23 -0500370/**
371 * Specify how to crop the output after scaling (if any).
372 *
373 * Future calls to {@link AImageDecoder_decodeImage} will crop their output to
374 * the specified {@link ARect}. Clients will only need to allocate enough memory
375 * for the cropped ARect.
376 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700377 * Available since API level 30.
378 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500379 * @param crop Rectangle describing a crop of the decode. It must be contained inside of
380 * the (possibly scaled, by {@link AImageDecoder_setTargetSize})
381 * image dimensions. This will affect future calls to
382 * {@link AImageDecoder_getMinimumStride}, which will now return a
383 * value based on the width of the crop. An empty ARect -
384 * specifically { 0, 0, 0, 0 } - may be used to remove the cropping
385 * behavior. Any other empty or unsorted ARects will result in
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500386 * returning {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500387 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
388 * indicating the reason for the failure.
389 *
390 * Errors:
391 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The
392 * {@link AImageDecoder} is null or the crop is not contained by the
393 * (possibly scaled) image dimensions.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500394 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400395int AImageDecoder_setCrop(AImageDecoder* _Nonnull decoder, ARect crop) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500396
Leon Scroggins III3b3700e2020-12-29 11:52:12 -0500397#endif // __ANDROID_API__ >= 30
398
Leon Scroggins III2f984942019-11-22 17:02:23 -0500399struct AImageDecoderHeaderInfo;
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500400/**
Leon Scroggins III3b3700e2020-12-29 11:52:12 -0500401 * Opaque handle for representing information about the encoded image.
402 *
403 * Introduced in API 30
404 *
405 * Retrieved using {@link AImageDecoder_getHeaderInfo} and passed to methods
406 * like {@link AImageDecoderHeaderInfo_getWidth} and
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500407 * {@link AImageDecoderHeaderInfo_getHeight}.
408 */
Leon Scroggins III2f984942019-11-22 17:02:23 -0500409typedef struct AImageDecoderHeaderInfo AImageDecoderHeaderInfo;
410
Leon Scroggins III3b3700e2020-12-29 11:52:12 -0500411#if __ANDROID_API__ >= 30
412
Leon Scroggins III2f984942019-11-22 17:02:23 -0500413/**
414 * Return an opaque handle for reading header info.
415 *
416 * This is owned by the {@link AImageDecoder} and will be destroyed when the
417 * AImageDecoder is destroyed via {@link AImageDecoder_delete}.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700418 *
419 * Available since API level 30.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500420 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400421const AImageDecoderHeaderInfo* _Nonnull AImageDecoder_getHeaderInfo(
422 const AImageDecoder* _Nonnull decoder) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500423
424/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500425 * Report the native width of the encoded image. This is also the logical
426 * pixel width of the output, unless {@link AImageDecoder_setTargetSize} is
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500427 * used to choose a different size or {@link AImageDecoder_setCrop} is used to
428 * set a crop rect.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700429 *
430 * Available since API level 30.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500431 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400432int32_t AImageDecoderHeaderInfo_getWidth(const AImageDecoderHeaderInfo* _Nonnull)
433 __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500434
435/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500436 * Report the native height of the encoded image. This is also the logical
437 * pixel height of the output, unless {@link AImageDecoder_setTargetSize} is
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500438 * used to choose a different size or {@link AImageDecoder_setCrop} is used to
439 * set a crop rect.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700440 *
441 * Available since API level 30.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500442 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400443int32_t AImageDecoderHeaderInfo_getHeight(const AImageDecoderHeaderInfo* _Nonnull)
444 __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500445
446/**
447 * Report the mimeType of the encoded image.
448 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700449 * Available since API level 30.
450 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500451 * @return a string literal describing the mime type.
452 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400453const char* _Nonnull AImageDecoderHeaderInfo_getMimeType(
454 const AImageDecoderHeaderInfo* _Nonnull) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500455
456/**
Leon Scroggins III5d0445c2020-01-23 09:43:43 -0500457 * Report the {@link AndroidBitmapFormat} the AImageDecoder will decode to
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500458 * by default. {@link AImageDecoder} will try to choose one that is sensible
Leon Scroggins III2f984942019-11-22 17:02:23 -0500459 * for the image and the system. Note that this does not indicate the
460 * encoded format of the image.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700461 *
462 * Available since API level 30.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500463 */
Leon Scroggins III5d0445c2020-01-23 09:43:43 -0500464int32_t AImageDecoderHeaderInfo_getAndroidBitmapFormat(
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400465 const AImageDecoderHeaderInfo* _Nonnull) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500466
467/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500468 * Report how the {@link AImageDecoder} will handle alpha by default. If the image
Leon Scroggins III2f984942019-11-22 17:02:23 -0500469 * contains no alpha (according to its header), this will return
470 * {@link ANDROID_BITMAP_FLAGS_ALPHA_OPAQUE}. If the image may contain alpha,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500471 * this returns {@link ANDROID_BITMAP_FLAGS_ALPHA_PREMUL}, because
472 * {@link AImageDecoder_decodeImage} will premultiply pixels by default.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700473 *
474 * Available since API level 30.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500475 */
476int AImageDecoderHeaderInfo_getAlphaFlags(
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400477 const AImageDecoderHeaderInfo* _Nonnull) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500478
479/**
Leon Scroggins III8cee65e2020-01-30 14:20:42 -0500480 * Report the dataspace the AImageDecoder will decode to by default.
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500481 *
Leon Scroggins III8cee65e2020-01-30 14:20:42 -0500482 * By default, {@link AImageDecoder_decodeImage} will not do any color
483 * conversion.
484 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700485 * Available since API level 30.
486 *
Leon Scroggins III8cee65e2020-01-30 14:20:42 -0500487 * @return The {@link ADataSpace} representing the way the colors
488 * are encoded (or {@link ADATASPACE_UNKNOWN} if there is not a
489 * corresponding ADataSpace). This specifies how to interpret the colors
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500490 * in the decoded image, unless {@link AImageDecoder_setDataSpace} is
491 * called to decode to a different ADataSpace.
492 *
493 * Note that ADataSpace only exposes a few values. This may return
Leon Scroggins III8cee65e2020-01-30 14:20:42 -0500494 * {@link ADATASPACE_UNKNOWN}, even for Named ColorSpaces, if they have
495 * no corresponding {@link ADataSpace}.
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500496 */
497int32_t AImageDecoderHeaderInfo_getDataSpace(
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400498 const AImageDecoderHeaderInfo* _Nonnull) __INTRODUCED_IN(30);
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500499
500/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500501 * Return the minimum stride that can be used in
502 * {@link AImageDecoder_decodeImage).
503 *
504 * This stride provides no padding, meaning it will be exactly equal to the
505 * width times the number of bytes per pixel for the {@link AndroidBitmapFormat}
506 * being used.
507 *
508 * If the output is scaled (via {@link AImageDecoder_setTargetSize}) and/or
509 * cropped (via {@link AImageDecoder_setCrop}), this takes those into account.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700510 *
511 * Available since API level 30.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500512 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400513size_t AImageDecoder_getMinimumStride(AImageDecoder* _Nonnull decoder) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500514
515/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500516 * Decode the image into pixels, using the settings of the {@link AImageDecoder}.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500517 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700518 * Available since API level 30.
519 *
Leon Scroggins III1b389712020-10-09 13:12:39 -0400520 * Starting in API level 31, it can be used to decode all of the frames of an
521 * animated image (i.e. GIF, WebP, HEIF) using new APIs (TODO (scroggo): list
522 * and describe here).
523 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500524 * @param decoder Opaque object representing the decoder.
525 * @param pixels On success, will be filled with the result
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500526 * of the decode. Must be large enough to hold |size| bytes.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500527 * @param stride Width in bytes of a single row. Must be at least
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500528 * {@link AImageDecoder_getMinimumStride} and a multiple of the
529 * bytes per pixel of the {@link AndroidBitmapFormat}.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500530 * @param size Size of the pixel buffer in bytes. Must be at least
531 * stride * (height - 1) +
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500532 * {@link AImageDecoder_getMinimumStride}.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500533 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
534 * indicating the reason for the failure.
535 *
536 * Errors:
537 * - {@link ANDROID_IMAGE_DECODER_INCOMPLETE}: The image was truncated. A
538 * partial image was decoded, and undecoded lines have been initialized to all
539 * zeroes.
540 * - {@link ANDROID_IMAGE_DECODER_ERROR}: The image contained an error. A
541 * partial image was decoded, and undecoded lines have been initialized to all
542 * zeroes.
543 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The {@link AImageDecoder} or
544 * |pixels| is null, the stride is not large enough or not pixel aligned, or
545 * |size| is not large enough.
546 * - {@link ANDROID_IMAGE_DECODER_SEEK_ERROR}: The asset or file descriptor
547 * failed to seek.
548 * - {@link ANDROID_IMAGE_DECODER_INTERNAL_ERROR}: Some other error, like a
549 * failure to allocate memory.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500550 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400551int AImageDecoder_decodeImage(AImageDecoder* _Nonnull decoder,
552 void* _Nonnull pixels, size_t stride,
Leon Scroggins III2f984942019-11-22 17:02:23 -0500553 size_t size) __INTRODUCED_IN(30);
554
555#endif // __ANDROID_API__ >= 30
556
Leon Scroggins III1b389712020-10-09 13:12:39 -0400557#if __ANDROID_API__ >= 31
558
559/**
560 * Return true iff the image is animated - i.e. has multiple frames.
561 *
562 * Introduced in API 31.
563 *
564 * This may require seeking past the first frame to verify whether
565 * there is a following frame (e.g. for GIF).
566 *
567 * Errors:
568 * - returns false if |decoder| is null.
569 */
570bool AImageDecoder_isAnimated(AImageDecoder* _Nonnull decoder)
571 __INTRODUCED_IN(31);
572
Leon Scroggins III3b3700e2020-12-29 11:52:12 -0500573#endif // __ANDROID_API__ >= 31
574
Leon Scroggins III728a9ac2020-10-12 10:34:05 -0400575enum {
576 /*
577 * Reported by {@link AImageDecoder_getRepeatCount} if the
578 * animation should repeat forever.
Leon Scroggins III3b3700e2020-12-29 11:52:12 -0500579 *
580 * Introduced in API 31
Leon Scroggins III728a9ac2020-10-12 10:34:05 -0400581 */
582 ANDROID_IMAGE_DECODER_INFINITE = INT32_MAX,
583};
584
Leon Scroggins III3b3700e2020-12-29 11:52:12 -0500585#if __ANDROID_API__ >= 31
586
Leon Scroggins III728a9ac2020-10-12 10:34:05 -0400587/**
588 * Report how many times the animation should repeat.
589 *
590 * Introduced in API 31.
591 *
592 * This does not include the first play through. e.g. a repeat
593 * count of 4 means that each frame is played 5 times.
594 *
595 * {@link ANDROID_IMAGE_DECODER_INFINITE} means to repeat forever.
596 *
597 * This may require seeking.
598 *
599 * For non-animated formats, this returns 0. It may return non-zero for
600 * an image with only one frame (i.e. {@link AImageDecoder_isAnimated} returns
601 * false) if the encoded image contains a repeat count.
602 *
603 * @return Number of times to repeat on success or a value
604 * indicating the reason for the failure.
605 *
606 * Errors:
607 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The AImageDecoder
608 * is null.
609 */
610int32_t AImageDecoder_getRepeatCount(AImageDecoder* _Nonnull decoder);
611 __INTRODUCED_IN(31);
612
Leon Scroggins III1b389712020-10-09 13:12:39 -0400613#endif // __ANDROID_API__ >= 31
614
Leon Scroggins III2f984942019-11-22 17:02:23 -0500615#ifdef __cplusplus
616}
617#endif
618
619#endif // ANDROID_IMAGE_DECODER_H
620
621/** @} */