blob: e8a63a7d6b5a1590028dee375cc6ed9bb2da66c2 [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
60#if __ANDROID_API__ >= 30
61
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050062/**
63 * {@link AImageDecoder} functions result code. Many functions will return one of these
64 * to indicate success ({@link ANDROID_IMAGE_DECODER_SUCCESS}) or the reason
65 * for the failure. On failure, any out-parameters should be considered
66 * uninitialized, except where specified.
67 */
Leon Scroggins III2f984942019-11-22 17:02:23 -050068enum {
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050069 /**
70 * Decoding was successful and complete.
71 */
Leon Scroggins III2f984942019-11-22 17:02:23 -050072 ANDROID_IMAGE_DECODER_SUCCESS = 0,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050073 /**
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -050074 * The input is incomplete.
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050075 */
Leon Scroggins III2f984942019-11-22 17:02:23 -050076 ANDROID_IMAGE_DECODER_INCOMPLETE = -1,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050077 /**
78 * The input contained an error after decoding some lines.
79 */
Leon Scroggins III2f984942019-11-22 17:02:23 -050080 ANDROID_IMAGE_DECODER_ERROR = -2,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050081 /**
82 * Could not convert. For example, attempting to decode an image with
83 * alpha to an opaque format.
84 */
Leon Scroggins III2f984942019-11-22 17:02:23 -050085 ANDROID_IMAGE_DECODER_INVALID_CONVERSION = -3,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050086 /**
87 * The scale is invalid. It may have overflowed, or it may be incompatible
88 * with the current alpha setting.
89 */
Leon Scroggins III2f984942019-11-22 17:02:23 -050090 ANDROID_IMAGE_DECODER_INVALID_SCALE = -4,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050091 /**
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -050092 * Some other parameter is invalid.
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050093 */
Leon Scroggins III2f984942019-11-22 17:02:23 -050094 ANDROID_IMAGE_DECODER_BAD_PARAMETER = -5,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050095 /**
96 * Input was invalid before decoding any pixels.
97 */
Leon Scroggins III2f984942019-11-22 17:02:23 -050098 ANDROID_IMAGE_DECODER_INVALID_INPUT = -6,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050099 /**
100 * A seek was required and it failed.
101 */
Leon Scroggins III2f984942019-11-22 17:02:23 -0500102 ANDROID_IMAGE_DECODER_SEEK_ERROR = -7,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500103 /**
104 * Some other error. For example, an internal allocation failed.
105 */
Leon Scroggins III2f984942019-11-22 17:02:23 -0500106 ANDROID_IMAGE_DECODER_INTERNAL_ERROR = -8,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500107 /**
108 * AImageDecoder did not recognize the format.
109 */
Leon Scroggins III2f984942019-11-22 17:02:23 -0500110 ANDROID_IMAGE_DECODER_UNSUPPORTED_FORMAT = -9
111};
112
113struct AImageDecoder;
114
115/**
116 * Opaque handle for decoding images.
117 *
118 * Create using one of the following:
119 * - {@link AImageDecoder_createFromAAsset}
120 * - {@link AImageDecoder_createFromFd}
121 * - {@link AImageDecoder_createFromBuffer}
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500122 *
123 * After creation, {@link AImageDecoder_getHeaderInfo} can be used to retrieve
124 * information about the encoded image. Other functions, like
125 * {@link AImageDecoder_setTargetSize}, can be used to specify how to decode, and
126 * {@link AImageDecoder_decode} will decode into client provided memory.
127 *
128 * {@link AImageDecoder} objects are NOT thread-safe, and should not be shared across
129 * threads.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500130 */
131typedef struct AImageDecoder AImageDecoder;
132
133/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500134 * Create a new {@link AImageDecoder} from an {@link AAsset}.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500135 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700136 * Available since API level 30.
137 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500138 * @param asset {@link AAsset} containing encoded image data. Client is still
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500139 * responsible for calling {@link AAsset_close} on it, which may be
140 * done after deleting the returned {@link AImageDecoder}.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500141 * @param outDecoder On success (i.e. return value is
142 * {@link ANDROID_IMAGE_DECODER_SUCCESS}), this will be set to
143 * a newly created {@link AImageDecoder}. Caller is
144 * responsible for calling {@link AImageDecoder_delete} on it.
145 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500146 * indicating the reason for the failure.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500147 *
148 * Errors:
149 * - {@link ANDROID_IMAGE_DECODER_INCOMPLETE}: The asset was truncated before
150 * reading the image header.
151 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: One of the parameters is
152 * null.
153 * - {@link ANDROID_IMAGE_DECODER_INVALID_INPUT}: There is an error in the
154 * header.
155 * - {@link ANDROID_IMAGE_DECODER_SEEK_ERROR}: The asset failed to seek.
156 * - {@link ANDROID_IMAGE_DECODER_INTERNAL_ERROR}: Some other error, like a
157 * failure to allocate memory.
158 * - {@link ANDROID_IMAGE_DECODER_UNSUPPORTED_FORMAT}: The format is not
159 * supported.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500160 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400161int AImageDecoder_createFromAAsset(struct AAsset* _Nonnull asset,
162 AImageDecoder* _Nonnull * _Nonnull outDecoder)
Leon Scroggins IIIa9f397b2020-01-27 12:42:56 -0500163 __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500164
165/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500166 * Create a new {@link AImageDecoder} from a file descriptor.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500167 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700168 * Available since API level 30.
169 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500170 * @param fd Seekable, readable, open file descriptor for encoded data.
171 * Client is still responsible for closing it, which may be done
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500172 * after deleting the returned {@link AImageDecoder}.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500173 * @param outDecoder On success (i.e. return value is
174 * {@link ANDROID_IMAGE_DECODER_SUCCESS}), this will be set to
175 * a newly created {@link AImageDecoder}. Caller is
176 * responsible for calling {@link AImageDecoder_delete} on it.
177 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500178 * indicating the reason for the failure.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500179 *
180 * Errors:
181 * - {@link ANDROID_IMAGE_DECODER_INCOMPLETE}: The file was truncated before
182 * reading the image header.
183 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The {@link AImageDecoder} is
184 * null, or |fd| does not represent a valid, seekable file descriptor.
185 * - {@link ANDROID_IMAGE_DECODER_INVALID_INPUT}: There is an error in the
186 * header.
187 * - {@link ANDROID_IMAGE_DECODER_SEEK_ERROR}: The descriptor failed to seek.
188 * - {@link ANDROID_IMAGE_DECODER_INTERNAL_ERROR}: Some other error, like a
189 * failure to allocate memory.
190 * - {@link ANDROID_IMAGE_DECODER_UNSUPPORTED_FORMAT}: The format is not
191 * supported.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500192 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400193int AImageDecoder_createFromFd(int fd, AImageDecoder* _Nonnull * _Nonnull outDecoder)
194 __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500195
196/**
197 * Create a new AImageDecoder from a buffer.
198 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700199 * Available since API level 30.
200 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500201 * @param buffer Pointer to encoded data. Must be valid for the entire time
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500202 * the {@link AImageDecoder} is used.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500203 * @param length Byte length of buffer.
204 * @param outDecoder On success (i.e. return value is
205 * {@link ANDROID_IMAGE_DECODER_SUCCESS}), this will be set to
206 * a newly created {@link AImageDecoder}. Caller is
207 * responsible for calling {@link AImageDecoder_delete} on it.
208 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500209 * indicating the reason for the failure.
210 *
211 * Errors:
212 * - {@link ANDROID_IMAGE_DECODER_INCOMPLETE}: The encoded image was truncated before
213 * reading the image header.
214 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: One of the parameters is
215 * invalid.
216 * - {@link ANDROID_IMAGE_DECODER_INVALID_INPUT}: There is an error in the
217 * header.
218 * - {@link ANDROID_IMAGE_DECODER_INTERNAL_ERROR}: Some other error, like a
219 * failure to allocate memory.
220 * - {@link ANDROID_IMAGE_DECODER_UNSUPPORTED_FORMAT}: The format is not
221 * supported.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500222 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400223int AImageDecoder_createFromBuffer(const void* _Nonnull buffer, size_t length,
224 AImageDecoder* _Nonnull * _Nonnull outDecoder)
225 __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500226
227/**
228 * Delete the AImageDecoder.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700229 *
230 * Available since API level 30.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500231 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400232void AImageDecoder_delete(AImageDecoder* _Nonnull decoder) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500233
234/**
235 * Choose the desired output format.
236 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700237 * Available since API level 30.
238 *
Leon Scroggins III5d0445c2020-01-23 09:43:43 -0500239 * @param format {@link AndroidBitmapFormat} to use for the output.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500240 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
241 * indicating the reason for the failure. On failure, the
242 * {@link AImageDecoder} uses the format it was already planning
243 * to use (either its default or a previously successful setting
244 * from this function).
245 *
246 * Errors:
247 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The
248 * {@link AImageDecoder} is null or |format| does not correspond to an
249 * {@link AndroidBitmapFormat}.
250 * - {@link ANDROID_IMAGE_DECODER_INVALID_CONVERSION}: The
251 * {@link AndroidBitmapFormat} is incompatible with the image.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500252 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400253int AImageDecoder_setAndroidBitmapFormat(AImageDecoder* _Nonnull decoder,
Leon Scroggins III2f984942019-11-22 17:02:23 -0500254 int32_t format) __INTRODUCED_IN(30);
255
Leon Scroggins III1be112f2020-01-15 04:26:44 -0500256/**
257 * Specify whether the output's pixels should be unpremultiplied.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500258 *
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500259 * By default, {@link AImageDecoder_decodeImage} will premultiply the pixels, if they have alpha.
260 * Pass true to this method to leave them unpremultiplied. This has no effect on an
Leon Scroggins III1be112f2020-01-15 04:26:44 -0500261 * opaque image.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500262 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700263 * Available since API level 30.
264 *
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500265 * @param unpremultipliedRequired Pass true to leave the pixels unpremultiplied.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500266 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
267 * indicating the reason for the failure.
268 *
269 * Errors:
270 * - {@link ANDROID_IMAGE_DECODER_INVALID_CONVERSION}: Unpremultiplied is not
271 * possible due to an existing scale set by
272 * {@link AImageDecoder_setTargetSize}.
273 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The
274 * {@link AImageDecoder} is null.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500275 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400276int AImageDecoder_setUnpremultipliedRequired(AImageDecoder* _Nonnull decoder,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500277 bool unpremultipliedRequired) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500278
279/**
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500280 * Choose the dataspace for the output.
281 *
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500282 * Ignored by {@link ANDROID_BITMAP_FORMAT_A_8}, which does not support
283 * an {@link ADataSpace}.
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500284 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700285 * Available since API level 30.
286 *
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500287 * @param dataspace The {@link ADataSpace} to decode into. An ADataSpace
288 * specifies how to interpret the colors. By default,
289 * AImageDecoder will decode into the ADataSpace specified by
290 * {@link AImageDecoderHeaderInfo_getDataSpace}. If this
291 * parameter is set to a different ADataSpace, AImageDecoder
292 * will transform the output into the specified ADataSpace.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500293 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
294 * indicating the reason for the failure.
295 *
296 * Errors:
297 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The
298 * {@link AImageDecoder} is null or |dataspace| does not correspond to an
299 * {@link ADataSpace} value.
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500300 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400301int AImageDecoder_setDataSpace(AImageDecoder* _Nonnull decoder, int32_t dataspace)
302 __INTRODUCED_IN(30);
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500303
304/**
Leon Scroggins III2f984942019-11-22 17:02:23 -0500305 * Specify the output size for a decoded image.
306 *
307 * Future calls to {@link AImageDecoder_decodeImage} will sample or scale the
308 * encoded image to reach the desired size. If a crop rect is set (via
309 * {@link AImageDecoder_setCrop}), it must be contained within the dimensions
310 * specified by width and height, and the output image will be the size of the
311 * crop rect.
312 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700313 * Available since API level 30.
314 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500315 * @param width Width of the output (prior to cropping).
316 * This will affect future calls to
317 * {@link AImageDecoder_getMinimumStride}, which will now return
318 * a value based on this width.
319 * @param height Height of the output (prior to cropping).
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500320 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
321 * indicating the reason for the failure.
322 *
323 * Errors:
324 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The
325 * {@link AImageDecoder} is null.
326 * - {@link ANDROID_IMAGE_DECODER_INVALID_SCALE}: |width| or |height| is <= 0,
327 * the size is too big, any existing crop is not contained by the new image dimensions,
328 * or the scale is incompatible with a previous call to
329 * {@link AImageDecoder_setUnpremultipliedRequired}(true).
Leon Scroggins III2f984942019-11-22 17:02:23 -0500330 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400331int AImageDecoder_setTargetSize(AImageDecoder* _Nonnull decoder, int32_t width,
332 int32_t height) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500333
Leon Scroggins IIIf27256b2020-01-19 21:13:04 -0500334
335/**
336 * Compute the dimensions to use for a given sampleSize.
337 *
338 * Although AImageDecoder can scale to an arbitrary target size (see
339 * {@link AImageDecoder_setTargetSize}), some sizes may be more efficient than
340 * others. This computes the most efficient target size to use to reach a
341 * particular sampleSize.
342 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700343 * Available since API level 30.
344 *
Leon Scroggins IIIf27256b2020-01-19 21:13:04 -0500345 * @param sampleSize A subsampling rate of the original image. Must be greater
346 * than or equal to 1. A sampleSize of 2 means to skip every
347 * other pixel/line, resulting in a width and height that are
348 * 1/2 of the original dimensions, with 1/4 the number of
349 * pixels.
350 * @param width Out parameter for the width sampled by sampleSize, and rounded
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500351 * in the direction that the decoder can do most efficiently.
Leon Scroggins IIIf27256b2020-01-19 21:13:04 -0500352 * @param height Out parameter for the height sampled by sampleSize, and rounded
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500353 * in the direction that the decoder can do most efficiently.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500354 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
355 * indicating the reason for the failure.
356 *
357 * Errors:
358 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The
359 * {@link AImageDecoder}, |width| or |height| is null or |sampleSize| is < 1.
Leon Scroggins IIIf27256b2020-01-19 21:13:04 -0500360 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400361int AImageDecoder_computeSampledSize(const AImageDecoder* _Nonnull decoder, int sampleSize,
362 int32_t* _Nonnull width, int32_t* _Nonnull height)
363 __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500364/**
365 * Specify how to crop the output after scaling (if any).
366 *
367 * Future calls to {@link AImageDecoder_decodeImage} will crop their output to
368 * the specified {@link ARect}. Clients will only need to allocate enough memory
369 * for the cropped ARect.
370 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700371 * Available since API level 30.
372 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500373 * @param crop Rectangle describing a crop of the decode. It must be contained inside of
374 * the (possibly scaled, by {@link AImageDecoder_setTargetSize})
375 * image dimensions. This will affect future calls to
376 * {@link AImageDecoder_getMinimumStride}, which will now return a
377 * value based on the width of the crop. An empty ARect -
378 * specifically { 0, 0, 0, 0 } - may be used to remove the cropping
379 * behavior. Any other empty or unsorted ARects will result in
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500380 * returning {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}.
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 or the crop is not contained by the
387 * (possibly scaled) image dimensions.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500388 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400389int AImageDecoder_setCrop(AImageDecoder* _Nonnull decoder, ARect crop) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500390
Leon Scroggins III2f984942019-11-22 17:02:23 -0500391struct AImageDecoderHeaderInfo;
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500392/**
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500393 * Opaque handle for representing information about the encoded image. Retrieved
394 * using {@link AImageDecoder_getHeaderInfo} and passed to methods like
395 * {@link AImageDecoderHeaderInfo_getWidth} and
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500396 * {@link AImageDecoderHeaderInfo_getHeight}.
397 */
Leon Scroggins III2f984942019-11-22 17:02:23 -0500398typedef struct AImageDecoderHeaderInfo AImageDecoderHeaderInfo;
399
400/**
401 * Return an opaque handle for reading header info.
402 *
403 * This is owned by the {@link AImageDecoder} and will be destroyed when the
404 * AImageDecoder is destroyed via {@link AImageDecoder_delete}.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700405 *
406 * Available since API level 30.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500407 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400408const AImageDecoderHeaderInfo* _Nonnull AImageDecoder_getHeaderInfo(
409 const AImageDecoder* _Nonnull decoder) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500410
411/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500412 * Report the native width of the encoded image. This is also the logical
413 * pixel width of the output, unless {@link AImageDecoder_setTargetSize} is
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500414 * used to choose a different size or {@link AImageDecoder_setCrop} is used to
415 * set a crop rect.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700416 *
417 * Available since API level 30.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500418 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400419int32_t AImageDecoderHeaderInfo_getWidth(const AImageDecoderHeaderInfo* _Nonnull)
420 __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500421
422/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500423 * Report the native height of the encoded image. This is also the logical
424 * pixel height of the output, unless {@link AImageDecoder_setTargetSize} is
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500425 * used to choose a different size or {@link AImageDecoder_setCrop} is used to
426 * set a crop rect.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700427 *
428 * Available since API level 30.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500429 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400430int32_t AImageDecoderHeaderInfo_getHeight(const AImageDecoderHeaderInfo* _Nonnull)
431 __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500432
433/**
434 * Report the mimeType of the encoded image.
435 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700436 * Available since API level 30.
437 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500438 * @return a string literal describing the mime type.
439 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400440const char* _Nonnull AImageDecoderHeaderInfo_getMimeType(
441 const AImageDecoderHeaderInfo* _Nonnull) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500442
443/**
Leon Scroggins III5d0445c2020-01-23 09:43:43 -0500444 * Report the {@link AndroidBitmapFormat} the AImageDecoder will decode to
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500445 * by default. {@link AImageDecoder} will try to choose one that is sensible
Leon Scroggins III2f984942019-11-22 17:02:23 -0500446 * for the image and the system. Note that this does not indicate the
447 * encoded format of the image.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700448 *
449 * Available since API level 30.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500450 */
Leon Scroggins III5d0445c2020-01-23 09:43:43 -0500451int32_t AImageDecoderHeaderInfo_getAndroidBitmapFormat(
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400452 const AImageDecoderHeaderInfo* _Nonnull) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500453
454/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500455 * Report how the {@link AImageDecoder} will handle alpha by default. If the image
Leon Scroggins III2f984942019-11-22 17:02:23 -0500456 * contains no alpha (according to its header), this will return
457 * {@link ANDROID_BITMAP_FLAGS_ALPHA_OPAQUE}. If the image may contain alpha,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500458 * this returns {@link ANDROID_BITMAP_FLAGS_ALPHA_PREMUL}, because
459 * {@link AImageDecoder_decodeImage} will premultiply pixels by default.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700460 *
461 * Available since API level 30.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500462 */
463int AImageDecoderHeaderInfo_getAlphaFlags(
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400464 const AImageDecoderHeaderInfo* _Nonnull) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500465
466/**
Leon Scroggins III8cee65e2020-01-30 14:20:42 -0500467 * Report the dataspace the AImageDecoder will decode to by default.
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500468 *
Leon Scroggins III8cee65e2020-01-30 14:20:42 -0500469 * By default, {@link AImageDecoder_decodeImage} will not do any color
470 * conversion.
471 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700472 * Available since API level 30.
473 *
Leon Scroggins III8cee65e2020-01-30 14:20:42 -0500474 * @return The {@link ADataSpace} representing the way the colors
475 * are encoded (or {@link ADATASPACE_UNKNOWN} if there is not a
476 * corresponding ADataSpace). This specifies how to interpret the colors
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500477 * in the decoded image, unless {@link AImageDecoder_setDataSpace} is
478 * called to decode to a different ADataSpace.
479 *
480 * Note that ADataSpace only exposes a few values. This may return
Leon Scroggins III8cee65e2020-01-30 14:20:42 -0500481 * {@link ADATASPACE_UNKNOWN}, even for Named ColorSpaces, if they have
482 * no corresponding {@link ADataSpace}.
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500483 */
484int32_t AImageDecoderHeaderInfo_getDataSpace(
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400485 const AImageDecoderHeaderInfo* _Nonnull) __INTRODUCED_IN(30);
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500486
487/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500488 * Return the minimum stride that can be used in
489 * {@link AImageDecoder_decodeImage).
490 *
491 * This stride provides no padding, meaning it will be exactly equal to the
492 * width times the number of bytes per pixel for the {@link AndroidBitmapFormat}
493 * being used.
494 *
495 * If the output is scaled (via {@link AImageDecoder_setTargetSize}) and/or
496 * cropped (via {@link AImageDecoder_setCrop}), this takes those into account.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700497 *
498 * Available since API level 30.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500499 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400500size_t AImageDecoder_getMinimumStride(AImageDecoder* _Nonnull decoder) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500501
502/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500503 * Decode the image into pixels, using the settings of the {@link AImageDecoder}.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500504 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700505 * Available since API level 30.
506 *
Leon Scroggins III1b389712020-10-09 13:12:39 -0400507 * Starting in API level 31, it can be used to decode all of the frames of an
508 * animated image (i.e. GIF, WebP, HEIF) using new APIs (TODO (scroggo): list
509 * and describe here).
510 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500511 * @param decoder Opaque object representing the decoder.
512 * @param pixels On success, will be filled with the result
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500513 * of the decode. Must be large enough to hold |size| bytes.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500514 * @param stride Width in bytes of a single row. Must be at least
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500515 * {@link AImageDecoder_getMinimumStride} and a multiple of the
516 * bytes per pixel of the {@link AndroidBitmapFormat}.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500517 * @param size Size of the pixel buffer in bytes. Must be at least
518 * stride * (height - 1) +
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500519 * {@link AImageDecoder_getMinimumStride}.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500520 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
521 * indicating the reason for the failure.
522 *
523 * Errors:
524 * - {@link ANDROID_IMAGE_DECODER_INCOMPLETE}: The image was truncated. A
525 * partial image was decoded, and undecoded lines have been initialized to all
526 * zeroes.
527 * - {@link ANDROID_IMAGE_DECODER_ERROR}: The image contained an error. A
528 * partial image was decoded, and undecoded lines have been initialized to all
529 * zeroes.
530 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The {@link AImageDecoder} or
531 * |pixels| is null, the stride is not large enough or not pixel aligned, or
532 * |size| is not large enough.
533 * - {@link ANDROID_IMAGE_DECODER_SEEK_ERROR}: The asset or file descriptor
534 * failed to seek.
535 * - {@link ANDROID_IMAGE_DECODER_INTERNAL_ERROR}: Some other error, like a
536 * failure to allocate memory.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500537 */
Leon Scroggins III4a8ecfe2020-10-07 10:53:24 -0400538int AImageDecoder_decodeImage(AImageDecoder* _Nonnull decoder,
539 void* _Nonnull pixels, size_t stride,
Leon Scroggins III2f984942019-11-22 17:02:23 -0500540 size_t size) __INTRODUCED_IN(30);
541
542#endif // __ANDROID_API__ >= 30
543
Leon Scroggins III1b389712020-10-09 13:12:39 -0400544#if __ANDROID_API__ >= 31
545
546/**
547 * Return true iff the image is animated - i.e. has multiple frames.
548 *
549 * Introduced in API 31.
550 *
551 * This may require seeking past the first frame to verify whether
552 * there is a following frame (e.g. for GIF).
553 *
554 * Errors:
555 * - returns false if |decoder| is null.
556 */
557bool AImageDecoder_isAnimated(AImageDecoder* _Nonnull decoder)
558 __INTRODUCED_IN(31);
559
560#endif // __ANDROID_API__ >= 31
561
Leon Scroggins III2f984942019-11-22 17:02:23 -0500562#ifdef __cplusplus
563}
564#endif
565
566#endif // ANDROID_IMAGE_DECODER_H
567
568/** @} */