blob: 48d217f7653ce9fd84e39524af98a2ab34d5c17d [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
Leon Scroggins IIIc1f093f2021-01-08 14:25:31 -050054#ifndef __ANDROID__
55 // Value copied from 'bionic/libc/include/android/api-level.h' which is not available on
56 // non Android systems. It is set to 10000 which is same as __ANDROID_API_FUTURE__ value.
57 #ifndef __ANDROID_API__
58 #define __ANDROID_API__ 10000
59 #endif
60
61 // Value copied from 'bionic/libc/include/android/versioning.h' which is not available on
62 // non Android systems
63 #ifndef __INTRODUCED_IN
64 #define __INTRODUCED_IN(api_level)
65 #endif
66#endif
67
Leon Scroggins III2f984942019-11-22 17:02:23 -050068#ifdef __cplusplus
69extern "C" {
70#endif
71
72struct AAsset;
Leon Scroggins III2f984942019-11-22 17:02:23 -050073
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050074/**
75 * {@link AImageDecoder} functions result code. Many functions will return one of these
76 * to indicate success ({@link ANDROID_IMAGE_DECODER_SUCCESS}) or the reason
77 * for the failure. On failure, any out-parameters should be considered
78 * uninitialized, except where specified.
79 */
Leon Scroggins III2f984942019-11-22 17:02:23 -050080enum {
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050081 /**
82 * Decoding was successful and complete.
83 */
Leon Scroggins III2f984942019-11-22 17:02:23 -050084 ANDROID_IMAGE_DECODER_SUCCESS = 0,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050085 /**
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -050086 * The input is incomplete.
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050087 */
Leon Scroggins III2f984942019-11-22 17:02:23 -050088 ANDROID_IMAGE_DECODER_INCOMPLETE = -1,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050089 /**
90 * The input contained an error after decoding some lines.
91 */
Leon Scroggins III2f984942019-11-22 17:02:23 -050092 ANDROID_IMAGE_DECODER_ERROR = -2,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050093 /**
94 * Could not convert. For example, attempting to decode an image with
95 * alpha to an opaque format.
96 */
Leon Scroggins III2f984942019-11-22 17:02:23 -050097 ANDROID_IMAGE_DECODER_INVALID_CONVERSION = -3,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -050098 /**
99 * The scale is invalid. It may have overflowed, or it may be incompatible
100 * with the current alpha setting.
101 */
Leon Scroggins III2f984942019-11-22 17:02:23 -0500102 ANDROID_IMAGE_DECODER_INVALID_SCALE = -4,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500103 /**
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500104 * Some other parameter is invalid.
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500105 */
Leon Scroggins III2f984942019-11-22 17:02:23 -0500106 ANDROID_IMAGE_DECODER_BAD_PARAMETER = -5,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500107 /**
108 * Input was invalid before decoding any pixels.
109 */
Leon Scroggins III2f984942019-11-22 17:02:23 -0500110 ANDROID_IMAGE_DECODER_INVALID_INPUT = -6,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500111 /**
112 * A seek was required and it failed.
113 */
Leon Scroggins III2f984942019-11-22 17:02:23 -0500114 ANDROID_IMAGE_DECODER_SEEK_ERROR = -7,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500115 /**
116 * Some other error. For example, an internal allocation failed.
117 */
Leon Scroggins III2f984942019-11-22 17:02:23 -0500118 ANDROID_IMAGE_DECODER_INTERNAL_ERROR = -8,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500119 /**
120 * AImageDecoder did not recognize the format.
121 */
Leon Scroggins III2f984942019-11-22 17:02:23 -0500122 ANDROID_IMAGE_DECODER_UNSUPPORTED_FORMAT = -9
123};
124
125struct AImageDecoder;
126
127/**
128 * Opaque handle for decoding images.
129 *
130 * Create using one of the following:
131 * - {@link AImageDecoder_createFromAAsset}
132 * - {@link AImageDecoder_createFromFd}
133 * - {@link AImageDecoder_createFromBuffer}
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500134 *
135 * After creation, {@link AImageDecoder_getHeaderInfo} can be used to retrieve
136 * information about the encoded image. Other functions, like
137 * {@link AImageDecoder_setTargetSize}, can be used to specify how to decode, and
138 * {@link AImageDecoder_decode} will decode into client provided memory.
139 *
140 * {@link AImageDecoder} objects are NOT thread-safe, and should not be shared across
141 * threads.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500142 */
143typedef struct AImageDecoder AImageDecoder;
144
145/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500146 * Create a new {@link AImageDecoder} from an {@link AAsset}.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500147 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700148 * Available since API level 30.
149 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500150 * @param asset {@link AAsset} containing encoded image data. Client is still
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500151 * responsible for calling {@link AAsset_close} on it, which may be
152 * done after deleting the returned {@link AImageDecoder}.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500153 * @param outDecoder On success (i.e. return value is
154 * {@link ANDROID_IMAGE_DECODER_SUCCESS}), this will be set to
155 * a newly created {@link AImageDecoder}. Caller is
156 * responsible for calling {@link AImageDecoder_delete} on it.
157 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500158 * indicating the reason for the failure.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500159 *
160 * Errors:
161 * - {@link ANDROID_IMAGE_DECODER_INCOMPLETE}: The asset was truncated before
162 * reading the image header.
163 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: One of the parameters is
164 * null.
165 * - {@link ANDROID_IMAGE_DECODER_INVALID_INPUT}: There is an error in the
166 * header.
167 * - {@link ANDROID_IMAGE_DECODER_SEEK_ERROR}: The asset failed to seek.
168 * - {@link ANDROID_IMAGE_DECODER_INTERNAL_ERROR}: Some other error, like a
169 * failure to allocate memory.
170 * - {@link ANDROID_IMAGE_DECODER_UNSUPPORTED_FORMAT}: The format is not
171 * supported.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500172 */
Leon Scroggins IIIa9f397b2020-01-27 12:42:56 -0500173int AImageDecoder_createFromAAsset(struct AAsset* asset, AImageDecoder** outDecoder)
174 __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500175
176/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500177 * Create a new {@link AImageDecoder} from a file descriptor.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500178 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700179 * Available since API level 30.
180 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500181 * @param fd Seekable, readable, open file descriptor for encoded data.
182 * Client is still responsible for closing it, which may be done
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500183 * after deleting the returned {@link AImageDecoder}.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500184 * @param outDecoder On success (i.e. return value is
185 * {@link ANDROID_IMAGE_DECODER_SUCCESS}), this will be set to
186 * a newly created {@link AImageDecoder}. Caller is
187 * responsible for calling {@link AImageDecoder_delete} on it.
188 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500189 * indicating the reason for the failure.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500190 *
191 * Errors:
192 * - {@link ANDROID_IMAGE_DECODER_INCOMPLETE}: The file was truncated before
193 * reading the image header.
194 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The {@link AImageDecoder} is
195 * null, or |fd| does not represent a valid, seekable file descriptor.
196 * - {@link ANDROID_IMAGE_DECODER_INVALID_INPUT}: There is an error in the
197 * header.
198 * - {@link ANDROID_IMAGE_DECODER_SEEK_ERROR}: The descriptor failed to seek.
199 * - {@link ANDROID_IMAGE_DECODER_INTERNAL_ERROR}: Some other error, like a
200 * failure to allocate memory.
201 * - {@link ANDROID_IMAGE_DECODER_UNSUPPORTED_FORMAT}: The format is not
202 * supported.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500203 */
204int AImageDecoder_createFromFd(int fd, AImageDecoder** outDecoder) __INTRODUCED_IN(30);
205
206/**
207 * Create a new AImageDecoder from a buffer.
208 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700209 * Available since API level 30.
210 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500211 * @param buffer Pointer to encoded data. Must be valid for the entire time
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500212 * the {@link AImageDecoder} is used.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500213 * @param length Byte length of buffer.
214 * @param outDecoder On success (i.e. return value is
215 * {@link ANDROID_IMAGE_DECODER_SUCCESS}), this will be set to
216 * a newly created {@link AImageDecoder}. Caller is
217 * responsible for calling {@link AImageDecoder_delete} on it.
218 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500219 * indicating the reason for the failure.
220 *
221 * Errors:
222 * - {@link ANDROID_IMAGE_DECODER_INCOMPLETE}: The encoded image was truncated before
223 * reading the image header.
224 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: One of the parameters is
225 * invalid.
226 * - {@link ANDROID_IMAGE_DECODER_INVALID_INPUT}: There is an error in the
227 * header.
228 * - {@link ANDROID_IMAGE_DECODER_INTERNAL_ERROR}: Some other error, like a
229 * failure to allocate memory.
230 * - {@link ANDROID_IMAGE_DECODER_UNSUPPORTED_FORMAT}: The format is not
231 * supported.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500232 */
233int AImageDecoder_createFromBuffer(const void* buffer, size_t length,
234 AImageDecoder** outDecoder) __INTRODUCED_IN(30);
235
236/**
237 * Delete the AImageDecoder.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700238 *
239 * Available since API level 30.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500240 */
241void AImageDecoder_delete(AImageDecoder* decoder) __INTRODUCED_IN(30);
242
243/**
244 * Choose the desired output format.
245 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700246 * Available since API level 30.
247 *
Leon Scroggins III5d0445c2020-01-23 09:43:43 -0500248 * @param format {@link AndroidBitmapFormat} to use for the output.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500249 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
250 * indicating the reason for the failure. On failure, the
251 * {@link AImageDecoder} uses the format it was already planning
252 * to use (either its default or a previously successful setting
253 * from this function).
254 *
255 * Errors:
256 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The
257 * {@link AImageDecoder} is null or |format| does not correspond to an
258 * {@link AndroidBitmapFormat}.
259 * - {@link ANDROID_IMAGE_DECODER_INVALID_CONVERSION}: The
260 * {@link AndroidBitmapFormat} is incompatible with the image.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500261 */
262int AImageDecoder_setAndroidBitmapFormat(AImageDecoder*,
263 int32_t format) __INTRODUCED_IN(30);
264
Leon Scroggins III1be112f2020-01-15 04:26:44 -0500265/**
266 * Specify whether the output's pixels should be unpremultiplied.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500267 *
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500268 * By default, {@link AImageDecoder_decodeImage} will premultiply the pixels, if they have alpha.
269 * Pass true to this method to leave them unpremultiplied. This has no effect on an
Leon Scroggins III1be112f2020-01-15 04:26:44 -0500270 * opaque image.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500271 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700272 * Available since API level 30.
273 *
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500274 * @param unpremultipliedRequired Pass true to leave the pixels unpremultiplied.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500275 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
276 * indicating the reason for the failure.
277 *
278 * Errors:
279 * - {@link ANDROID_IMAGE_DECODER_INVALID_CONVERSION}: Unpremultiplied is not
280 * possible due to an existing scale set by
281 * {@link AImageDecoder_setTargetSize}.
282 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The
283 * {@link AImageDecoder} is null.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500284 */
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500285int AImageDecoder_setUnpremultipliedRequired(AImageDecoder*,
286 bool unpremultipliedRequired) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500287
288/**
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500289 * Choose the dataspace for the output.
290 *
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500291 * Ignored by {@link ANDROID_BITMAP_FORMAT_A_8}, which does not support
292 * an {@link ADataSpace}.
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500293 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700294 * Available since API level 30.
295 *
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500296 * @param dataspace The {@link ADataSpace} to decode into. An ADataSpace
297 * specifies how to interpret the colors. By default,
298 * AImageDecoder will decode into the ADataSpace specified by
299 * {@link AImageDecoderHeaderInfo_getDataSpace}. If this
300 * parameter is set to a different ADataSpace, AImageDecoder
301 * will transform the output into the specified ADataSpace.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500302 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
303 * indicating the reason for the failure.
304 *
305 * Errors:
306 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The
307 * {@link AImageDecoder} is null or |dataspace| does not correspond to an
308 * {@link ADataSpace} value.
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500309 */
310int AImageDecoder_setDataSpace(AImageDecoder*, int32_t dataspace) __INTRODUCED_IN(30);
311
312/**
Leon Scroggins III2f984942019-11-22 17:02:23 -0500313 * Specify the output size for a decoded image.
314 *
315 * Future calls to {@link AImageDecoder_decodeImage} will sample or scale the
316 * encoded image to reach the desired size. If a crop rect is set (via
317 * {@link AImageDecoder_setCrop}), it must be contained within the dimensions
318 * specified by width and height, and the output image will be the size of the
319 * crop rect.
320 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700321 * Available since API level 30.
322 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500323 * @param width Width of the output (prior to cropping).
324 * This will affect future calls to
325 * {@link AImageDecoder_getMinimumStride}, which will now return
326 * a value based on this width.
327 * @param height Height of the output (prior to cropping).
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500328 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
329 * indicating the reason for the failure.
330 *
331 * Errors:
332 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The
333 * {@link AImageDecoder} is null.
334 * - {@link ANDROID_IMAGE_DECODER_INVALID_SCALE}: |width| or |height| is <= 0,
335 * the size is too big, any existing crop is not contained by the new image dimensions,
336 * or the scale is incompatible with a previous call to
337 * {@link AImageDecoder_setUnpremultipliedRequired}(true).
Leon Scroggins III2f984942019-11-22 17:02:23 -0500338 */
Leon Scroggins III5d0445c2020-01-23 09:43:43 -0500339int AImageDecoder_setTargetSize(AImageDecoder*, int32_t width, int32_t height) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500340
Leon Scroggins IIIf27256b2020-01-19 21:13:04 -0500341
342/**
343 * Compute the dimensions to use for a given sampleSize.
344 *
345 * Although AImageDecoder can scale to an arbitrary target size (see
346 * {@link AImageDecoder_setTargetSize}), some sizes may be more efficient than
347 * others. This computes the most efficient target size to use to reach a
348 * particular sampleSize.
349 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700350 * Available since API level 30.
351 *
Leon Scroggins IIIf27256b2020-01-19 21:13:04 -0500352 * @param sampleSize A subsampling rate of the original image. Must be greater
353 * than or equal to 1. A sampleSize of 2 means to skip every
354 * other pixel/line, resulting in a width and height that are
355 * 1/2 of the original dimensions, with 1/4 the number of
356 * pixels.
357 * @param width Out parameter for the width 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 IIIf27256b2020-01-19 21:13:04 -0500359 * @param height Out parameter for the height sampled by sampleSize, and rounded
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500360 * in the direction that the decoder can do most efficiently.
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500361 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
362 * indicating the reason for the failure.
363 *
364 * Errors:
365 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER}: The
366 * {@link AImageDecoder}, |width| or |height| is null or |sampleSize| is < 1.
Leon Scroggins IIIf27256b2020-01-19 21:13:04 -0500367 */
368int AImageDecoder_computeSampledSize(const AImageDecoder*, int sampleSize,
Leon Scroggins III5d0445c2020-01-23 09:43:43 -0500369 int32_t* width, int32_t* height) __INTRODUCED_IN(30);
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 */
395int AImageDecoder_setCrop(AImageDecoder*, ARect crop) __INTRODUCED_IN(30);
396
Leon Scroggins III2f984942019-11-22 17:02:23 -0500397struct AImageDecoderHeaderInfo;
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500398/**
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500399 * Opaque handle for representing information about the encoded image. Retrieved
400 * using {@link AImageDecoder_getHeaderInfo} and passed to methods like
401 * {@link AImageDecoderHeaderInfo_getWidth} and
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500402 * {@link AImageDecoderHeaderInfo_getHeight}.
403 */
Leon Scroggins III2f984942019-11-22 17:02:23 -0500404typedef struct AImageDecoderHeaderInfo AImageDecoderHeaderInfo;
405
406/**
407 * Return an opaque handle for reading header info.
408 *
409 * This is owned by the {@link AImageDecoder} and will be destroyed when the
410 * AImageDecoder is destroyed via {@link AImageDecoder_delete}.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700411 *
412 * Available since API level 30.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500413 */
414const AImageDecoderHeaderInfo* AImageDecoder_getHeaderInfo(
415 const AImageDecoder*) __INTRODUCED_IN(30);
416
417/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500418 * Report the native width of the encoded image. This is also the logical
419 * pixel width of the output, unless {@link AImageDecoder_setTargetSize} is
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500420 * used to choose a different size or {@link AImageDecoder_setCrop} is used to
421 * set a crop rect.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700422 *
423 * Available since API level 30.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500424 */
425int32_t AImageDecoderHeaderInfo_getWidth(const AImageDecoderHeaderInfo*) __INTRODUCED_IN(30);
426
427/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500428 * Report the native height of the encoded image. This is also the logical
429 * pixel height of the output, unless {@link AImageDecoder_setTargetSize} is
Leon Scroggins IIIbb5ffd22020-02-06 11:45:16 -0500430 * used to choose a different size or {@link AImageDecoder_setCrop} is used to
431 * set a crop rect.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700432 *
433 * Available since API level 30.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500434 */
435int32_t AImageDecoderHeaderInfo_getHeight(const AImageDecoderHeaderInfo*) __INTRODUCED_IN(30);
436
437/**
438 * Report the mimeType of the encoded image.
439 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700440 * Available since API level 30.
441 *
Leon Scroggins III2f984942019-11-22 17:02:23 -0500442 * @return a string literal describing the mime type.
443 */
444const char* AImageDecoderHeaderInfo_getMimeType(
445 const AImageDecoderHeaderInfo*) __INTRODUCED_IN(30);
446
447/**
Leon Scroggins III5d0445c2020-01-23 09:43:43 -0500448 * Report the {@link AndroidBitmapFormat} the AImageDecoder will decode to
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500449 * by default. {@link AImageDecoder} will try to choose one that is sensible
Leon Scroggins III2f984942019-11-22 17:02:23 -0500450 * for the image and the system. Note that this does not indicate the
451 * encoded format of the image.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700452 *
453 * Available since API level 30.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500454 */
Leon Scroggins III5d0445c2020-01-23 09:43:43 -0500455int32_t AImageDecoderHeaderInfo_getAndroidBitmapFormat(
Leon Scroggins III2f984942019-11-22 17:02:23 -0500456 const AImageDecoderHeaderInfo*) __INTRODUCED_IN(30);
457
458/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500459 * Report how the {@link AImageDecoder} will handle alpha by default. If the image
Leon Scroggins III2f984942019-11-22 17:02:23 -0500460 * contains no alpha (according to its header), this will return
461 * {@link ANDROID_BITMAP_FLAGS_ALPHA_OPAQUE}. If the image may contain alpha,
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500462 * this returns {@link ANDROID_BITMAP_FLAGS_ALPHA_PREMUL}, because
463 * {@link AImageDecoder_decodeImage} will premultiply pixels by default.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700464 *
465 * Available since API level 30.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500466 */
467int AImageDecoderHeaderInfo_getAlphaFlags(
468 const AImageDecoderHeaderInfo*) __INTRODUCED_IN(30);
469
470/**
Leon Scroggins III8cee65e2020-01-30 14:20:42 -0500471 * Report the dataspace the AImageDecoder will decode to by default.
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500472 *
Leon Scroggins III8cee65e2020-01-30 14:20:42 -0500473 * By default, {@link AImageDecoder_decodeImage} will not do any color
474 * conversion.
475 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700476 * Available since API level 30.
477 *
Leon Scroggins III8cee65e2020-01-30 14:20:42 -0500478 * @return The {@link ADataSpace} representing the way the colors
479 * are encoded (or {@link ADATASPACE_UNKNOWN} if there is not a
480 * corresponding ADataSpace). This specifies how to interpret the colors
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500481 * in the decoded image, unless {@link AImageDecoder_setDataSpace} is
482 * called to decode to a different ADataSpace.
483 *
484 * Note that ADataSpace only exposes a few values. This may return
Leon Scroggins III8cee65e2020-01-30 14:20:42 -0500485 * {@link ADATASPACE_UNKNOWN}, even for Named ColorSpaces, if they have
486 * no corresponding {@link ADataSpace}.
Leon Scroggins III20d480c2020-01-15 15:32:59 -0500487 */
488int32_t AImageDecoderHeaderInfo_getDataSpace(
489 const AImageDecoderHeaderInfo*) __INTRODUCED_IN(30);
490
491/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500492 * Return the minimum stride that can be used in
493 * {@link AImageDecoder_decodeImage).
494 *
495 * This stride provides no padding, meaning it will be exactly equal to the
496 * width times the number of bytes per pixel for the {@link AndroidBitmapFormat}
497 * being used.
498 *
499 * If the output is scaled (via {@link AImageDecoder_setTargetSize}) and/or
500 * cropped (via {@link AImageDecoder_setCrop}), this takes those into account.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700501 *
502 * Available since API level 30.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500503 */
504size_t AImageDecoder_getMinimumStride(AImageDecoder*) __INTRODUCED_IN(30);
505
506/**
Leon Scroggins III97fea5f2020-01-30 12:18:20 -0500507 * Decode the image into pixels, using the settings of the {@link AImageDecoder}.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500508 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700509 * Available since API level 30.
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 */
538int AImageDecoder_decodeImage(AImageDecoder* decoder,
539 void* pixels, size_t stride,
540 size_t size) __INTRODUCED_IN(30);
541
Leon Scroggins III2f984942019-11-22 17:02:23 -0500542#ifdef __cplusplus
543}
544#endif
545
546#endif // ANDROID_IMAGE_DECODER_H
547
548/** @} */