blob: 50daabaaff8146c66b05eea0e3d7a1dcc63cc3ff [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/**
18 * @addtogroup ImageDecoder
19 * @{
20 */
21
22/**
23 * @file imageDecoder.h
24 */
25
26#ifndef ANDROID_IMAGE_DECODER_H
27#define ANDROID_IMAGE_DECODER_H
28
29#include "bitmap.h"
30
31#include <stdint.h>
32
33#ifdef __cplusplus
34extern "C" {
35#endif
36
37struct AAsset;
38struct ARect;
39
40#if __ANDROID_API__ >= 30
41
42/** AImageDecoder functions result code. */
43enum {
44 // Decoding was successful and complete.
45 ANDROID_IMAGE_DECODER_SUCCESS = 0,
46 // The input was incomplete. In decodeImage, this means a partial
47 // image was decoded. Undecoded lines are all zeroes.
48 // In AImageDecoder_create*, no AImageDecoder was created.
49 ANDROID_IMAGE_DECODER_INCOMPLETE = -1,
50 // The input contained an error after decoding some lines. Similar to
51 // INCOMPLETE, above.
52 ANDROID_IMAGE_DECODER_ERROR = -2,
53 // Could not convert, e.g. attempting to decode an image with
54 // alpha to an opaque format.
55 ANDROID_IMAGE_DECODER_INVALID_CONVERSION = -3,
56 // The scale is invalid. It may have overflowed, or it may be incompatible
57 // with the current alpha setting.
58 ANDROID_IMAGE_DECODER_INVALID_SCALE = -4,
59 // Some other parameter was bad (e.g. pixels)
60 ANDROID_IMAGE_DECODER_BAD_PARAMETER = -5,
61 // Input was invalid i.e. broken before decoding any pixels.
62 ANDROID_IMAGE_DECODER_INVALID_INPUT = -6,
63 // A seek was required, and failed.
64 ANDROID_IMAGE_DECODER_SEEK_ERROR = -7,
65 // Some other error (e.g. OOM)
66 ANDROID_IMAGE_DECODER_INTERNAL_ERROR = -8,
67 // We did not recognize the format
68 ANDROID_IMAGE_DECODER_UNSUPPORTED_FORMAT = -9
69};
70
71struct AImageDecoder;
72
73/**
74 * Opaque handle for decoding images.
75 *
76 * Create using one of the following:
77 * - {@link AImageDecoder_createFromAAsset}
78 * - {@link AImageDecoder_createFromFd}
79 * - {@link AImageDecoder_createFromBuffer}
80 */
81typedef struct AImageDecoder AImageDecoder;
82
83/**
84 * Create a new AImageDecoder from an AAsset.
85 *
86 * @param asset {@link AAsset} containing encoded image data. Client is still
87 * responsible for calling {@link AAsset_close} on it.
88 * @param outDecoder On success (i.e. return value is
89 * {@link ANDROID_IMAGE_DECODER_SUCCESS}), this will be set to
90 * a newly created {@link AImageDecoder}. Caller is
91 * responsible for calling {@link AImageDecoder_delete} on it.
92 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
93 * indicating reason for the failure.
94 */
95int AImageDecoder_createFromAAsset(AAsset* asset, AImageDecoder** outDecoder) __INTRODUCED_IN(30);
96
97/**
98 * Create a new AImageDecoder from a file descriptor.
99 *
100 * @param fd Seekable, readable, open file descriptor for encoded data.
101 * Client is still responsible for closing it, which may be done
102 * *after* deleting the returned AImageDecoder.
103 * @param outDecoder On success (i.e. return value is
104 * {@link ANDROID_IMAGE_DECODER_SUCCESS}), this will be set to
105 * a newly created {@link AImageDecoder}. Caller is
106 * responsible for calling {@link AImageDecoder_delete} on it.
107 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
108 * indicating reason for the failure.
109 */
110int AImageDecoder_createFromFd(int fd, AImageDecoder** outDecoder) __INTRODUCED_IN(30);
111
112/**
113 * Create a new AImageDecoder from a buffer.
114 *
115 * @param buffer Pointer to encoded data. Must be valid for the entire time
116 * the AImageDecoder is used.
117 * @param length Byte length of buffer.
118 * @param outDecoder On success (i.e. return value is
119 * {@link ANDROID_IMAGE_DECODER_SUCCESS}), this will be set to
120 * a newly created {@link AImageDecoder}. Caller is
121 * responsible for calling {@link AImageDecoder_delete} on it.
122 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success or a value
123 * indicating reason for the failure.
124 */
125int AImageDecoder_createFromBuffer(const void* buffer, size_t length,
126 AImageDecoder** outDecoder) __INTRODUCED_IN(30);
127
128/**
129 * Delete the AImageDecoder.
130 */
131void AImageDecoder_delete(AImageDecoder* decoder) __INTRODUCED_IN(30);
132
133/**
134 * Choose the desired output format.
135 *
136 * @param format AndroidBitmapFormat to use
137 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} if the format is compatible
138 * with the image and {@link ANDROID_IMAGE_DECODER_INVALID_CONVERSION}
139 * otherwise. In the latter case, the AImageDecoder uses the
140 * format it was already planning to use (either its default
141 * or a previously successful setting from this function).
142 */
143int AImageDecoder_setAndroidBitmapFormat(AImageDecoder*,
144 int32_t format) __INTRODUCED_IN(30);
145
146/*
147 * Choose the desired output format.
148 *
149 * Must be one of:
150 * {@link ANDROID_BITMAP_FLAGS_ALPHA_PREMUL}
151 * {@link ANDROID_BITMAP_FLAGS_ALPHA_OPAQUE}
152 * {@link ANDROID_BITMAP_FLAGS_ALPHA_UNPREMUL}
153 *
154 * Note: An OPAQUE image may be set to any of them.
155 * A non-OPAQUE image may not be set to OPAQUE
156 *
157 * @return - {@link ANDROID_IMAGE_DECODER_SUCCESS} on success
158 * - {@link ANDROID_IMAGE_DECODER_INVALID_CONVERSION} if the conversion
159 * is not possible
160 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER} for bad parameters
161 */
162int AImageDecoder_setAlphaFlags(AImageDecoder*, int alphaFlags) __INTRODUCED_IN(30);
163
164/**
165 * Specify the output size for a decoded image.
166 *
167 * Future calls to {@link AImageDecoder_decodeImage} will sample or scale the
168 * encoded image to reach the desired size. If a crop rect is set (via
169 * {@link AImageDecoder_setCrop}), it must be contained within the dimensions
170 * specified by width and height, and the output image will be the size of the
171 * crop rect.
172 *
173 * @param width Width of the output (prior to cropping).
174 * This will affect future calls to
175 * {@link AImageDecoder_getMinimumStride}, which will now return
176 * a value based on this width.
177 * @param height Height of the output (prior to cropping).
178 * @return - {@link ANDROID_IMAGE_DECODER_SUCCESS} on success
179 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER} if the AImageDecoder
180 * pointer is null, width or height is <= 0, or any existing crop is
181 * not contained by the image dimensions.
182 */
183int AImageDecoder_setTargetSize(AImageDecoder*, int width, int height) __INTRODUCED_IN(30);
184
185/**
186 * Specify how to crop the output after scaling (if any).
187 *
188 * Future calls to {@link AImageDecoder_decodeImage} will crop their output to
189 * the specified {@link ARect}. Clients will only need to allocate enough memory
190 * for the cropped ARect.
191 *
192 * @param crop Rectangle describing a crop of the decode. It must be contained inside of
193 * the (possibly scaled, by {@link AImageDecoder_setTargetSize})
194 * image dimensions. This will affect future calls to
195 * {@link AImageDecoder_getMinimumStride}, which will now return a
196 * value based on the width of the crop. An empty ARect -
197 * specifically { 0, 0, 0, 0 } - may be used to remove the cropping
198 * behavior. Any other empty or unsorted ARects will result in
199 * returning ANDROID_IMAGE_DECODER_BAD_PARAMETER.
200 * @return - {@link ANDROID_IMAGE_DECODER_SUCCESS} on success
201 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER} if the AImageDecoder
202 * pointer is null or the crop is not contained by the image
203 * dimensions.
204 */
205int AImageDecoder_setCrop(AImageDecoder*, ARect crop) __INTRODUCED_IN(30);
206
207/**
208 * Opaque handle for reading header info.
209 */
210struct AImageDecoderHeaderInfo;
211typedef struct AImageDecoderHeaderInfo AImageDecoderHeaderInfo;
212
213/**
214 * Return an opaque handle for reading header info.
215 *
216 * This is owned by the {@link AImageDecoder} and will be destroyed when the
217 * AImageDecoder is destroyed via {@link AImageDecoder_delete}.
218 */
219const AImageDecoderHeaderInfo* AImageDecoder_getHeaderInfo(
220 const AImageDecoder*) __INTRODUCED_IN(30);
221
222/**
223 * Report the native width of the encoded image.
224 */
225int32_t AImageDecoderHeaderInfo_getWidth(const AImageDecoderHeaderInfo*) __INTRODUCED_IN(30);
226
227/**
228 * Report the native height of the encoded image.
229 */
230int32_t AImageDecoderHeaderInfo_getHeight(const AImageDecoderHeaderInfo*) __INTRODUCED_IN(30);
231
232/**
233 * Report the mimeType of the encoded image.
234 *
235 * @return a string literal describing the mime type.
236 */
237const char* AImageDecoderHeaderInfo_getMimeType(
238 const AImageDecoderHeaderInfo*) __INTRODUCED_IN(30);
239
240/**
241 * Report whether the encoded image represents an animation.
242 */
243bool AImageDecoderHeaderInfo_isAnimated(
244 const AImageDecoderHeaderInfo*) __INTRODUCED_IN(30);
245
246/**
247 * Report the AndroidBitmapFormat the AImageDecoder will decode to
248 * by default. AImageDecoder will try to choose one that is sensible
249 * for the image and the system. Note that this does not indicate the
250 * encoded format of the image.
251 */
252AndroidBitmapFormat AImageDecoderHeaderInfo_getAndroidBitmapFormat(
253 const AImageDecoderHeaderInfo*) __INTRODUCED_IN(30);
254
255/**
256 * Report how the AImageDecoder will handle alpha by default. If the image
257 * contains no alpha (according to its header), this will return
258 * {@link ANDROID_BITMAP_FLAGS_ALPHA_OPAQUE}. If the image may contain alpha,
259 * this returns {@link ANDROID_BITMAP_FLAGS_ALPHA_PREMUL}.
260 *
261 * For animated images only the opacity of the first frame is reported.
262 */
263int AImageDecoderHeaderInfo_getAlphaFlags(
264 const AImageDecoderHeaderInfo*) __INTRODUCED_IN(30);
265
266/**
267 * Return the minimum stride that can be used, taking the specified
268 * (or default) (possibly scaled) width, crop rect and
269 * {@link AndroidBitmapFormat} into account.
270 */
271size_t AImageDecoder_getMinimumStride(AImageDecoder*) __INTRODUCED_IN(30);
272
273/**
274 * Decode the image into pixels, using the settings of the AImageDecoder.
275 *
276 * @param decoder Opaque object representing the decoder.
277 * @param pixels On success, will be filled with the result
278 * of the decode. Must be large enough to fit |size| bytes.
279 * @param stride Width in bytes of a single row. Must be at least
280 * {@link AImageDecoder_getMinimumStride}.
281 * @param size Size of the pixel buffer in bytes. Must be at least
282 * stride * (height - 1) +
283 * {@link AImageDecoder_getMinimumStride}.
284 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success, or an error code
285 * from the same enum describing the failure.
286 */
287int AImageDecoder_decodeImage(AImageDecoder* decoder,
288 void* pixels, size_t stride,
289 size_t size) __INTRODUCED_IN(30);
290
291#endif // __ANDROID_API__ >= 30
292
293#ifdef __cplusplus
294}
295#endif
296
297#endif // ANDROID_IMAGE_DECODER_H
298
299/** @} */