blob: 571a5ca1ef524a9a5ea66d69732bedb823761e1c [file] [log] [blame]
Mathias Agopiane1c61d32012-03-23 14:19:36 -07001/*
2 * Copyright (C) 2009 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
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070017/**
18 * @addtogroup Bitmap
19 * @{
20 */
21
22/**
23 * @file bitmap.h
24 */
25
Mathias Agopiane1c61d32012-03-23 14:19:36 -070026#ifndef ANDROID_BITMAP_H
27#define ANDROID_BITMAP_H
28
Leon Scroggins III7f2d5b62019-12-13 14:57:07 -050029#include <stdbool.h>
Mathias Agopiane1c61d32012-03-23 14:19:36 -070030#include <stdint.h>
31#include <jni.h>
32
33#ifdef __cplusplus
34extern "C" {
35#endif
36
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070037/** AndroidBitmap functions result code. */
38enum {
39 /** Operation was successful. */
40 ANDROID_BITMAP_RESULT_SUCCESS = 0,
41 /** Bad parameter. */
42 ANDROID_BITMAP_RESULT_BAD_PARAMETER = -1,
43 /** JNI exception occured. */
44 ANDROID_BITMAP_RESULT_JNI_EXCEPTION = -2,
45 /** Allocation failed. */
46 ANDROID_BITMAP_RESULT_ALLOCATION_FAILED = -3,
47};
Mathias Agopiane1c61d32012-03-23 14:19:36 -070048
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070049/** Backward compatibility: this macro used to be misspelled. */
Andrew Hsieh370980c2012-12-17 08:01:36 +080050#define ANDROID_BITMAP_RESUT_SUCCESS ANDROID_BITMAP_RESULT_SUCCESS
51
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070052/** Bitmap pixel format. */
Mathias Agopiane1c61d32012-03-23 14:19:36 -070053enum AndroidBitmapFormat {
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070054 /** No format. */
Mathias Agopiane1c61d32012-03-23 14:19:36 -070055 ANDROID_BITMAP_FORMAT_NONE = 0,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070056 /** Red: 8 bits, Green: 8 bits, Blue: 8 bits, Alpha: 8 bits. **/
Mathias Agopiane1c61d32012-03-23 14:19:36 -070057 ANDROID_BITMAP_FORMAT_RGBA_8888 = 1,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070058 /** Red: 5 bits, Green: 6 bits, Blue: 5 bits. **/
Mathias Agopiane1c61d32012-03-23 14:19:36 -070059 ANDROID_BITMAP_FORMAT_RGB_565 = 4,
Quddus Chong4a1a45b2017-02-08 10:38:21 -080060 /** Deprecated in API level 13. Because of the poor quality of this configuration, it is advised to use ARGB_8888 instead. **/
Mathias Agopiane1c61d32012-03-23 14:19:36 -070061 ANDROID_BITMAP_FORMAT_RGBA_4444 = 7,
Quddus Chong4a1a45b2017-02-08 10:38:21 -080062 /** Alpha: 8 bits. */
Mathias Agopiane1c61d32012-03-23 14:19:36 -070063 ANDROID_BITMAP_FORMAT_A_8 = 8,
Leon Scroggins IIIf73f5f22019-08-12 13:43:55 -040064 /** Each component is stored as a half float. **/
65 ANDROID_BITMAP_FORMAT_RGBA_F16 = 9,
Mathias Agopiane1c61d32012-03-23 14:19:36 -070066};
67
Leon Scroggins III0dd622e2019-08-23 15:44:21 -040068/** Bitmap alpha format */
69enum {
70 /** Pixel components are premultiplied by alpha. */
71 ANDROID_BITMAP_FLAGS_ALPHA_PREMUL = 0,
72 /** Pixels are opaque. */
73 ANDROID_BITMAP_FLAGS_ALPHA_OPAQUE = 1,
74 /** Pixel components are independent of alpha. */
75 ANDROID_BITMAP_FLAGS_ALPHA_UNPREMUL = 2,
76 /** Bit mask for AndroidBitmapFormat.flags to isolate the alpha. */
77 ANDROID_BITMAP_FLAGS_ALPHA_MASK = 0x3,
78 /** Shift for AndroidBitmapFormat.flags to isolate the alpha. */
79 ANDROID_BITMAP_FLAGS_ALPHA_SHIFT = 0,
80};
81
Leon Scroggins IIId4672a82020-01-19 19:25:41 -050082enum {
83 /** If this bit is set in AndroidBitmapInfo.flags, the Bitmap uses the
84 * HARDWARE Config, and its AHardwareBuffer can be retrieved via
85 * AndroidBitmap_getHardwareBuffer.
86 */
87 ANDROID_BITMAP_FLAGS_IS_HARDWARE = 1 << 31,
88};
89
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070090/** Bitmap info, see AndroidBitmap_getInfo(). */
Mathias Agopiane1c61d32012-03-23 14:19:36 -070091typedef struct {
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070092 /** The bitmap width in pixels. */
Mathias Agopiane1c61d32012-03-23 14:19:36 -070093 uint32_t width;
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070094 /** The bitmap height in pixels. */
Mathias Agopiane1c61d32012-03-23 14:19:36 -070095 uint32_t height;
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070096 /** The number of byte per row. */
Mathias Agopiane1c61d32012-03-23 14:19:36 -070097 uint32_t stride;
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070098 /** The bitmap pixel format. See {@link AndroidBitmapFormat} */
Mathias Agopiane1c61d32012-03-23 14:19:36 -070099 int32_t format;
Leon Scroggins III0dd622e2019-08-23 15:44:21 -0400100 /** Two bits are used to encode alpha. Use ANDROID_BITMAP_FLAGS_ALPHA_MASK
Leon Scroggins IIId4672a82020-01-19 19:25:41 -0500101 * and ANDROID_BITMAP_FLAGS_ALPHA_SHIFT to retrieve them. One bit is used
102 * to encode whether the Bitmap uses the HARDWARE Config. Use
103 * ANDROID_BITMAP_FLAGS_IS_HARDWARE to know.*/
Leon Scroggins III0dd622e2019-08-23 15:44:21 -0400104 uint32_t flags;
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700105} AndroidBitmapInfo;
106
107/**
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700108 * Given a java bitmap object, fill out the AndroidBitmapInfo struct for it.
109 * If the call fails, the info parameter will be ignored.
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700110 */
111int AndroidBitmap_getInfo(JNIEnv* env, jobject jbitmap,
112 AndroidBitmapInfo* info);
113
Leon Scroggins III3dd64552020-01-10 13:41:44 -0500114#if __ANDROID_API__ >= 30
115
116/**
117 * Given a java bitmap object, return its ADataSpace.
118 *
119 * Note that ADataSpace only exposes a few values. This may return
120 * ADATASPACE_UNKNOWN, even for Named ColorSpaces, if they have no
121 * corresponding ADataSpace.
122 */
123int32_t AndroidBitmap_getDataSpace(JNIEnv* env, jobject jbitmap) __INTRODUCED_IN(30);
124
125#endif // __ANDROID_API__ >= 30
126
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700127/**
128 * Given a java bitmap object, attempt to lock the pixel address.
129 * Locking will ensure that the memory for the pixels will not move
130 * until the unlockPixels call, and ensure that, if the pixels had been
131 * previously purged, they will have been restored.
132 *
133 * If this call succeeds, it must be balanced by a call to
134 * AndroidBitmap_unlockPixels, after which time the address of the pixels should
135 * no longer be used.
136 *
137 * If this succeeds, *addrPtr will be set to the pixel address. If the call
138 * fails, addrPtr will be ignored.
139 */
140int AndroidBitmap_lockPixels(JNIEnv* env, jobject jbitmap, void** addrPtr);
141
142/**
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700143 * Call this to balance a successful call to AndroidBitmap_lockPixels.
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700144 */
145int AndroidBitmap_unlockPixels(JNIEnv* env, jobject jbitmap);
146
Leon Scroggins III7f2d5b62019-12-13 14:57:07 -0500147#if __ANDROID_API__ >= 30
148
149// Note: these values match android.graphics.Bitmap#compressFormat.
150
151/**
152 * Specifies the formats that can be compressed to with
153 * {@link AndroidBitmap_compress}.
154 */
155enum AndroidBitmapCompressFormat {
156 /**
157 * Compress to the JPEG format. quality of 0 means
158 * compress for the smallest size. 100 means compress for max
159 * visual quality.
160 */
161 ANDROID_BITMAP_COMPRESS_FORMAT_JPEG = 0,
162 /**
163 * Compress to the PNG format. PNG is lossless, so quality is
164 * ignored.
165 */
166 ANDROID_BITMAP_COMPRESS_FORMAT_PNG = 1,
167 /**
168 * Compress to the WEBP lossy format. quality of 0 means
169 * compress for the smallest size. 100 means compress for max
170 * visual quality.
171 */
172 ANDROID_BITMAP_COMPRESS_FORMAT_WEBP_LOSSY = 3,
173 /**
174 * Compress to the WEBP lossless format. quality refers to how
175 * much effort to put into compression. A value of 0 means to
176 * compress quickly, resulting in a relatively large file size.
177 * 100 means to spend more time compressing, resulting in a
178 * smaller file.
179 */
180 ANDROID_BITMAP_COMPRESS_FORMAT_WEBP_LOSSLESS = 4,
181};
182
183/**
184 * User-defined function for writing the output of compression.
185 *
186 * @param userContext Pointer to user-defined data passed to
187 * {@link AndroidBitmap_compress}.
188 * @param data Compressed data of |size| bytes to write.
189 * @param size Length in bytes of data to write.
190 * @return Whether the operation succeeded.
191 */
Leon Scroggins IIIc04a63e2020-01-22 11:52:48 -0500192typedef bool (*AndroidBitmap_CompressWriteFunc)(void* userContext,
Leon Scroggins III7f2d5b62019-12-13 14:57:07 -0500193 const void* data,
194 size_t size) __INTRODUCED_IN(30);
195
196/**
197 * Compress |pixels| as described by |info|.
198 *
199 * @param info Description of the pixels to compress.
200 * @param dataspace {@link ADataSpace} describing the color space of the
201 * pixels.
202 * @param pixels Pointer to pixels to compress.
203 * @param format (@link AndroidBitmapCompressFormat} to compress to.
204 * @param quality Hint to the compressor, 0-100. The value is interpreted
205 * differently depending on the
206 * {@link AndroidBitmapCompressFormat}.
207 * @param userContext User-defined data which will be passed to the supplied
Leon Scroggins IIIc04a63e2020-01-22 11:52:48 -0500208 * {@link AndroidBitmap_CompressWriteFunc} each time it is
Leon Scroggins III7f2d5b62019-12-13 14:57:07 -0500209 * called. May be null.
210 * @parm fn Function that writes the compressed data. Will be called each time
211 * the compressor has compressed more data that is ready to be
212 * written. May be called more than once for each call to this method.
213 * May not be null.
214 * @return AndroidBitmap functions result code.
215 */
216int AndroidBitmap_compress(const AndroidBitmapInfo* info,
217 int32_t dataspace,
218 const void* pixels,
219 int32_t format, int32_t quality,
220 void* userContext,
Leon Scroggins IIIc04a63e2020-01-22 11:52:48 -0500221 AndroidBitmap_CompressWriteFunc fn) __INTRODUCED_IN(30);
Leon Scroggins III7f2d5b62019-12-13 14:57:07 -0500222
Leon Scroggins IIId4672a82020-01-19 19:25:41 -0500223struct AHardwareBuffer;
224
225/**
226 * Retrieve the native object associated with a HARDWARE Bitmap.
227 *
228 * Client must not modify it while a Bitmap is wrapping it.
229 *
230 * @param bitmap Handle to an android.graphics.Bitmap.
231 * @param outBuffer On success, is set to a pointer to the
232 * AHardwareBuffer associated with bitmap. This acquires
233 * a reference on the buffer, and the client must call
234 * AHardwareBuffer_release when finished with it.
235 * @return AndroidBitmap functions result code.
236 * ANDROID_BITMAP_RESULT_BAD_PARAMETER if bitmap is not a
237 * HARDWARE Bitmap.
238 */
239int AndroidBitmap_getHardwareBuffer(JNIEnv* env, jobject bitmap,
240 AHardwareBuffer** outBuffer) __INTRODUCED_IN(30);
241
Leon Scroggins III7f2d5b62019-12-13 14:57:07 -0500242#endif // __ANDROID_API__ >= 30
243
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700244#ifdef __cplusplus
245}
246#endif
247
248#endif
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700249
250/** @} */