blob: c9fde0d3b7077c68dbf05a66ec1fda87bb3c36e8 [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
Leon Scroggins III1be112f2020-01-15 04:26:44 -0500146/**
147 * Specify whether the output's pixels should be unpremultiplied.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500148 *
Leon Scroggins III1be112f2020-01-15 04:26:44 -0500149 * By default, the decoder will premultiply the pixels, if they have alpha. Pass
150 * false to this method to leave them unpremultiplied. This has no effect on an
151 * opaque image.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500152 *
Leon Scroggins III1be112f2020-01-15 04:26:44 -0500153 * @param required Pass true to leave the pixels unpremultiplied.
Leon Scroggins III2f984942019-11-22 17:02:23 -0500154 * @return - {@link ANDROID_IMAGE_DECODER_SUCCESS} on success
155 * - {@link ANDROID_IMAGE_DECODER_INVALID_CONVERSION} if the conversion
156 * is not possible
157 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER} for bad parameters
158 */
Leon Scroggins III1be112f2020-01-15 04:26:44 -0500159int AImageDecoder_setUnpremultipliedRequired(AImageDecoder*, bool required) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500160
161/**
162 * Specify the output size for a decoded image.
163 *
164 * Future calls to {@link AImageDecoder_decodeImage} will sample or scale the
165 * encoded image to reach the desired size. If a crop rect is set (via
166 * {@link AImageDecoder_setCrop}), it must be contained within the dimensions
167 * specified by width and height, and the output image will be the size of the
168 * crop rect.
169 *
170 * @param width Width of the output (prior to cropping).
171 * This will affect future calls to
172 * {@link AImageDecoder_getMinimumStride}, which will now return
173 * a value based on this width.
174 * @param height Height of the output (prior to cropping).
175 * @return - {@link ANDROID_IMAGE_DECODER_SUCCESS} on success
176 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER} if the AImageDecoder
177 * pointer is null, width or height is <= 0, or any existing crop is
178 * not contained by the image dimensions.
179 */
180int AImageDecoder_setTargetSize(AImageDecoder*, int width, int height) __INTRODUCED_IN(30);
181
Leon Scroggins IIIf27256b2020-01-19 21:13:04 -0500182
183/**
184 * Compute the dimensions to use for a given sampleSize.
185 *
186 * Although AImageDecoder can scale to an arbitrary target size (see
187 * {@link AImageDecoder_setTargetSize}), some sizes may be more efficient than
188 * others. This computes the most efficient target size to use to reach a
189 * particular sampleSize.
190 *
191 * @param sampleSize A subsampling rate of the original image. Must be greater
192 * than or equal to 1. A sampleSize of 2 means to skip every
193 * other pixel/line, resulting in a width and height that are
194 * 1/2 of the original dimensions, with 1/4 the number of
195 * pixels.
196 * @param width Out parameter for the width sampled by sampleSize, and rounded
197 * direction that the decoder can do most efficiently.
198 * @param height Out parameter for the height sampled by sampleSize, and rounded
199 * direction that the decoder can do most efficiently.
200 * @return ANDROID_IMAGE_DECODER result code.
201 */
202int AImageDecoder_computeSampledSize(const AImageDecoder*, int sampleSize,
203 int* width, int* height) __INTRODUCED_IN(30);
Leon Scroggins III2f984942019-11-22 17:02:23 -0500204/**
205 * Specify how to crop the output after scaling (if any).
206 *
207 * Future calls to {@link AImageDecoder_decodeImage} will crop their output to
208 * the specified {@link ARect}. Clients will only need to allocate enough memory
209 * for the cropped ARect.
210 *
211 * @param crop Rectangle describing a crop of the decode. It must be contained inside of
212 * the (possibly scaled, by {@link AImageDecoder_setTargetSize})
213 * image dimensions. This will affect future calls to
214 * {@link AImageDecoder_getMinimumStride}, which will now return a
215 * value based on the width of the crop. An empty ARect -
216 * specifically { 0, 0, 0, 0 } - may be used to remove the cropping
217 * behavior. Any other empty or unsorted ARects will result in
218 * returning ANDROID_IMAGE_DECODER_BAD_PARAMETER.
219 * @return - {@link ANDROID_IMAGE_DECODER_SUCCESS} on success
220 * - {@link ANDROID_IMAGE_DECODER_BAD_PARAMETER} if the AImageDecoder
221 * pointer is null or the crop is not contained by the image
222 * dimensions.
223 */
224int AImageDecoder_setCrop(AImageDecoder*, ARect crop) __INTRODUCED_IN(30);
225
226/**
227 * Opaque handle for reading header info.
228 */
229struct AImageDecoderHeaderInfo;
230typedef struct AImageDecoderHeaderInfo AImageDecoderHeaderInfo;
231
232/**
233 * Return an opaque handle for reading header info.
234 *
235 * This is owned by the {@link AImageDecoder} and will be destroyed when the
236 * AImageDecoder is destroyed via {@link AImageDecoder_delete}.
237 */
238const AImageDecoderHeaderInfo* AImageDecoder_getHeaderInfo(
239 const AImageDecoder*) __INTRODUCED_IN(30);
240
241/**
242 * Report the native width of the encoded image.
243 */
244int32_t AImageDecoderHeaderInfo_getWidth(const AImageDecoderHeaderInfo*) __INTRODUCED_IN(30);
245
246/**
247 * Report the native height of the encoded image.
248 */
249int32_t AImageDecoderHeaderInfo_getHeight(const AImageDecoderHeaderInfo*) __INTRODUCED_IN(30);
250
251/**
252 * Report the mimeType of the encoded image.
253 *
254 * @return a string literal describing the mime type.
255 */
256const char* AImageDecoderHeaderInfo_getMimeType(
257 const AImageDecoderHeaderInfo*) __INTRODUCED_IN(30);
258
259/**
260 * Report whether the encoded image represents an animation.
261 */
262bool AImageDecoderHeaderInfo_isAnimated(
263 const AImageDecoderHeaderInfo*) __INTRODUCED_IN(30);
264
265/**
266 * Report the AndroidBitmapFormat the AImageDecoder will decode to
267 * by default. AImageDecoder will try to choose one that is sensible
268 * for the image and the system. Note that this does not indicate the
269 * encoded format of the image.
270 */
271AndroidBitmapFormat AImageDecoderHeaderInfo_getAndroidBitmapFormat(
272 const AImageDecoderHeaderInfo*) __INTRODUCED_IN(30);
273
274/**
275 * Report how the AImageDecoder will handle alpha by default. If the image
276 * contains no alpha (according to its header), this will return
277 * {@link ANDROID_BITMAP_FLAGS_ALPHA_OPAQUE}. If the image may contain alpha,
278 * this returns {@link ANDROID_BITMAP_FLAGS_ALPHA_PREMUL}.
279 *
280 * For animated images only the opacity of the first frame is reported.
281 */
282int AImageDecoderHeaderInfo_getAlphaFlags(
283 const AImageDecoderHeaderInfo*) __INTRODUCED_IN(30);
284
285/**
286 * Return the minimum stride that can be used, taking the specified
287 * (or default) (possibly scaled) width, crop rect and
288 * {@link AndroidBitmapFormat} into account.
289 */
290size_t AImageDecoder_getMinimumStride(AImageDecoder*) __INTRODUCED_IN(30);
291
292/**
293 * Decode the image into pixels, using the settings of the AImageDecoder.
294 *
295 * @param decoder Opaque object representing the decoder.
296 * @param pixels On success, will be filled with the result
297 * of the decode. Must be large enough to fit |size| bytes.
298 * @param stride Width in bytes of a single row. Must be at least
299 * {@link AImageDecoder_getMinimumStride}.
300 * @param size Size of the pixel buffer in bytes. Must be at least
301 * stride * (height - 1) +
302 * {@link AImageDecoder_getMinimumStride}.
303 * @return {@link ANDROID_IMAGE_DECODER_SUCCESS} on success, or an error code
304 * from the same enum describing the failure.
305 */
306int AImageDecoder_decodeImage(AImageDecoder* decoder,
307 void* pixels, size_t stride,
308 size_t size) __INTRODUCED_IN(30);
309
310#endif // __ANDROID_API__ >= 30
311
312#ifdef __cplusplus
313}
314#endif
315
316#endif // ANDROID_IMAGE_DECODER_H
317
318/** @} */