blob: 2362c9e491a1bd90b9677392f2fa30d96dbb3405 [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>
Leon Scroggins IIIc1f093f2021-01-08 14:25:31 -050031#include <stddef.h>
Mathias Agopiane1c61d32012-03-23 14:19:36 -070032#include <jni.h>
33
Leon Scroggins IIIc1f093f2021-01-08 14:25:31 -050034#ifndef __ANDROID__
35 // Value copied from 'bionic/libc/include/android/api-level.h' which is not available on
36 // non Android systems. It is set to 10000 which is same as __ANDROID_API_FUTURE__ value.
37 #ifndef __ANDROID_API__
38 #define __ANDROID_API__ 10000
39 #endif
40
41 // Value copied from 'bionic/libc/include/android/versioning.h' which is not available on
42 // non Android systems
43 #ifndef __INTRODUCED_IN
44 #define __INTRODUCED_IN(api_level)
45 #endif
46#endif
47
Mathias Agopiane1c61d32012-03-23 14:19:36 -070048#ifdef __cplusplus
49extern "C" {
50#endif
51
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070052/** AndroidBitmap functions result code. */
53enum {
54 /** Operation was successful. */
55 ANDROID_BITMAP_RESULT_SUCCESS = 0,
56 /** Bad parameter. */
57 ANDROID_BITMAP_RESULT_BAD_PARAMETER = -1,
58 /** JNI exception occured. */
59 ANDROID_BITMAP_RESULT_JNI_EXCEPTION = -2,
60 /** Allocation failed. */
61 ANDROID_BITMAP_RESULT_ALLOCATION_FAILED = -3,
62};
Mathias Agopiane1c61d32012-03-23 14:19:36 -070063
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070064/** Backward compatibility: this macro used to be misspelled. */
Andrew Hsieh370980c2012-12-17 08:01:36 +080065#define ANDROID_BITMAP_RESUT_SUCCESS ANDROID_BITMAP_RESULT_SUCCESS
66
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070067/** Bitmap pixel format. */
Mathias Agopiane1c61d32012-03-23 14:19:36 -070068enum AndroidBitmapFormat {
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070069 /** No format. */
Mathias Agopiane1c61d32012-03-23 14:19:36 -070070 ANDROID_BITMAP_FORMAT_NONE = 0,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070071 /** Red: 8 bits, Green: 8 bits, Blue: 8 bits, Alpha: 8 bits. **/
Mathias Agopiane1c61d32012-03-23 14:19:36 -070072 ANDROID_BITMAP_FORMAT_RGBA_8888 = 1,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070073 /** Red: 5 bits, Green: 6 bits, Blue: 5 bits. **/
Mathias Agopiane1c61d32012-03-23 14:19:36 -070074 ANDROID_BITMAP_FORMAT_RGB_565 = 4,
Quddus Chong4a1a45b2017-02-08 10:38:21 -080075 /** 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 -070076 ANDROID_BITMAP_FORMAT_RGBA_4444 = 7,
Quddus Chong4a1a45b2017-02-08 10:38:21 -080077 /** Alpha: 8 bits. */
Mathias Agopiane1c61d32012-03-23 14:19:36 -070078 ANDROID_BITMAP_FORMAT_A_8 = 8,
Leon Scroggins IIIf73f5f22019-08-12 13:43:55 -040079 /** Each component is stored as a half float. **/
80 ANDROID_BITMAP_FORMAT_RGBA_F16 = 9,
Mathias Agopiane1c61d32012-03-23 14:19:36 -070081};
82
Leon Scroggins III0dd622e2019-08-23 15:44:21 -040083/** Bitmap alpha format */
84enum {
85 /** Pixel components are premultiplied by alpha. */
86 ANDROID_BITMAP_FLAGS_ALPHA_PREMUL = 0,
87 /** Pixels are opaque. */
88 ANDROID_BITMAP_FLAGS_ALPHA_OPAQUE = 1,
89 /** Pixel components are independent of alpha. */
90 ANDROID_BITMAP_FLAGS_ALPHA_UNPREMUL = 2,
91 /** Bit mask for AndroidBitmapFormat.flags to isolate the alpha. */
92 ANDROID_BITMAP_FLAGS_ALPHA_MASK = 0x3,
93 /** Shift for AndroidBitmapFormat.flags to isolate the alpha. */
94 ANDROID_BITMAP_FLAGS_ALPHA_SHIFT = 0,
95};
96
Leon Scroggins IIId4672a82020-01-19 19:25:41 -050097enum {
98 /** If this bit is set in AndroidBitmapInfo.flags, the Bitmap uses the
Leon Scroggins III4883c522020-01-30 15:10:11 -050099 * HARDWARE Config, and its {@link AHardwareBuffer} can be retrieved via
100 * {@link AndroidBitmap_getHardwareBuffer}.
Leon Scroggins IIId4672a82020-01-19 19:25:41 -0500101 */
102 ANDROID_BITMAP_FLAGS_IS_HARDWARE = 1 << 31,
103};
104
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700105/** Bitmap info, see AndroidBitmap_getInfo(). */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700106typedef struct {
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700107 /** The bitmap width in pixels. */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700108 uint32_t width;
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700109 /** The bitmap height in pixels. */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700110 uint32_t height;
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700111 /** The number of byte per row. */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700112 uint32_t stride;
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700113 /** The bitmap pixel format. See {@link AndroidBitmapFormat} */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700114 int32_t format;
Leon Scroggins III9cb2ffd2020-02-06 11:16:54 -0500115 /** Bitfield containing information about the bitmap.
116 *
117 * <p>Two bits are used to encode alpha. Use {@link ANDROID_BITMAP_FLAGS_ALPHA_MASK}
118 * and {@link ANDROID_BITMAP_FLAGS_ALPHA_SHIFT} to retrieve them.</p>
119 *
120 * <p>One bit is used to encode whether the Bitmap uses the HARDWARE Config. Use
121 * {@link ANDROID_BITMAP_FLAGS_IS_HARDWARE} to know.</p>
122 *
123 * <p>These flags were introduced in API level 30.</p>
124 */
Leon Scroggins III0dd622e2019-08-23 15:44:21 -0400125 uint32_t flags;
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700126} AndroidBitmapInfo;
127
128/**
Leon Scroggins III9cb2ffd2020-02-06 11:16:54 -0500129 * Given a java bitmap object, fill out the {@link AndroidBitmapInfo} struct for it.
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700130 * If the call fails, the info parameter will be ignored.
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700131 */
132int AndroidBitmap_getInfo(JNIEnv* env, jobject jbitmap,
133 AndroidBitmapInfo* info);
134
Leon Scroggins III3dd64552020-01-10 13:41:44 -0500135/**
Leon Scroggins III4883c522020-01-30 15:10:11 -0500136 * Given a java bitmap object, return its {@link ADataSpace}.
Leon Scroggins III3dd64552020-01-10 13:41:44 -0500137 *
Leon Scroggins III4883c522020-01-30 15:10:11 -0500138 * Note that {@link ADataSpace} only exposes a few values. This may return
139 * {@link ADATASPACE_UNKNOWN}, even for Named ColorSpaces, if they have no
Leon Scroggins III3dd64552020-01-10 13:41:44 -0500140 * corresponding ADataSpace.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700141 *
142 * Available since API level 30.
Leon Scroggins III3dd64552020-01-10 13:41:44 -0500143 */
144int32_t AndroidBitmap_getDataSpace(JNIEnv* env, jobject jbitmap) __INTRODUCED_IN(30);
145
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700146/**
147 * Given a java bitmap object, attempt to lock the pixel address.
148 * Locking will ensure that the memory for the pixels will not move
149 * until the unlockPixels call, and ensure that, if the pixels had been
150 * previously purged, they will have been restored.
151 *
152 * If this call succeeds, it must be balanced by a call to
153 * AndroidBitmap_unlockPixels, after which time the address of the pixels should
154 * no longer be used.
155 *
156 * If this succeeds, *addrPtr will be set to the pixel address. If the call
157 * fails, addrPtr will be ignored.
158 */
159int AndroidBitmap_lockPixels(JNIEnv* env, jobject jbitmap, void** addrPtr);
160
161/**
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700162 * Call this to balance a successful call to AndroidBitmap_lockPixels.
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700163 */
164int AndroidBitmap_unlockPixels(JNIEnv* env, jobject jbitmap);
165
Leon Scroggins III7f2d5b62019-12-13 14:57:07 -0500166// Note: these values match android.graphics.Bitmap#compressFormat.
167
168/**
169 * Specifies the formats that can be compressed to with
170 * {@link AndroidBitmap_compress}.
171 */
172enum AndroidBitmapCompressFormat {
173 /**
174 * Compress to the JPEG format. quality of 0 means
175 * compress for the smallest size. 100 means compress for max
176 * visual quality.
177 */
178 ANDROID_BITMAP_COMPRESS_FORMAT_JPEG = 0,
179 /**
180 * Compress to the PNG format. PNG is lossless, so quality is
181 * ignored.
182 */
183 ANDROID_BITMAP_COMPRESS_FORMAT_PNG = 1,
184 /**
185 * Compress to the WEBP lossy format. quality of 0 means
186 * compress for the smallest size. 100 means compress for max
187 * visual quality.
188 */
189 ANDROID_BITMAP_COMPRESS_FORMAT_WEBP_LOSSY = 3,
190 /**
191 * Compress to the WEBP lossless format. quality refers to how
192 * much effort to put into compression. A value of 0 means to
193 * compress quickly, resulting in a relatively large file size.
194 * 100 means to spend more time compressing, resulting in a
195 * smaller file.
196 */
197 ANDROID_BITMAP_COMPRESS_FORMAT_WEBP_LOSSLESS = 4,
198};
199
200/**
201 * User-defined function for writing the output of compression.
202 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700203 * Available since API level 30.
204 *
Leon Scroggins III7f2d5b62019-12-13 14:57:07 -0500205 * @param userContext Pointer to user-defined data passed to
206 * {@link AndroidBitmap_compress}.
207 * @param data Compressed data of |size| bytes to write.
208 * @param size Length in bytes of data to write.
209 * @return Whether the operation succeeded.
210 */
Leon Scroggins IIIc04a63e2020-01-22 11:52:48 -0500211typedef bool (*AndroidBitmap_CompressWriteFunc)(void* userContext,
Leon Scroggins III7f2d5b62019-12-13 14:57:07 -0500212 const void* data,
213 size_t size) __INTRODUCED_IN(30);
214
215/**
216 * Compress |pixels| as described by |info|.
217 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700218 * Available since API level 30.
219 *
Leon Scroggins III7f2d5b62019-12-13 14:57:07 -0500220 * @param info Description of the pixels to compress.
221 * @param dataspace {@link ADataSpace} describing the color space of the
222 * pixels.
223 * @param pixels Pointer to pixels to compress.
Leon Scroggins III4883c522020-01-30 15:10:11 -0500224 * @param format {@link AndroidBitmapCompressFormat} to compress to.
Leon Scroggins III7f2d5b62019-12-13 14:57:07 -0500225 * @param quality Hint to the compressor, 0-100. The value is interpreted
226 * differently depending on the
227 * {@link AndroidBitmapCompressFormat}.
228 * @param userContext User-defined data which will be passed to the supplied
Leon Scroggins IIIc04a63e2020-01-22 11:52:48 -0500229 * {@link AndroidBitmap_CompressWriteFunc} each time it is
Leon Scroggins III7f2d5b62019-12-13 14:57:07 -0500230 * called. May be null.
Leon Scroggins III4883c522020-01-30 15:10:11 -0500231 * @param fn Function that writes the compressed data. Will be called each time
232 * the compressor has compressed more data that is ready to be
233 * written. May be called more than once for each call to this method.
234 * May not be null.
Leon Scroggins III7f2d5b62019-12-13 14:57:07 -0500235 * @return AndroidBitmap functions result code.
236 */
237int AndroidBitmap_compress(const AndroidBitmapInfo* info,
238 int32_t dataspace,
239 const void* pixels,
240 int32_t format, int32_t quality,
241 void* userContext,
Leon Scroggins IIIc04a63e2020-01-22 11:52:48 -0500242 AndroidBitmap_CompressWriteFunc fn) __INTRODUCED_IN(30);
Leon Scroggins III7f2d5b62019-12-13 14:57:07 -0500243
Leon Scroggins IIId4672a82020-01-19 19:25:41 -0500244struct AHardwareBuffer;
Colin Crossfbcf9d72020-02-24 15:26:37 -0800245typedef struct AHardwareBuffer AHardwareBuffer;
Leon Scroggins IIId4672a82020-01-19 19:25:41 -0500246
247/**
248 * Retrieve the native object associated with a HARDWARE Bitmap.
249 *
250 * Client must not modify it while a Bitmap is wrapping it.
251 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700252 * Available since API level 30.
253 *
Leon Scroggins IIId4672a82020-01-19 19:25:41 -0500254 * @param bitmap Handle to an android.graphics.Bitmap.
255 * @param outBuffer On success, is set to a pointer to the
Leon Scroggins III4883c522020-01-30 15:10:11 -0500256 * {@link AHardwareBuffer} associated with bitmap. This acquires
Leon Scroggins IIId4672a82020-01-19 19:25:41 -0500257 * a reference on the buffer, and the client must call
Leon Scroggins III4883c522020-01-30 15:10:11 -0500258 * {@link AHardwareBuffer_release} when finished with it.
Leon Scroggins IIId4672a82020-01-19 19:25:41 -0500259 * @return AndroidBitmap functions result code.
Leon Scroggins III4883c522020-01-30 15:10:11 -0500260 * {@link ANDROID_BITMAP_RESULT_BAD_PARAMETER} if bitmap is not a
Leon Scroggins IIId4672a82020-01-19 19:25:41 -0500261 * HARDWARE Bitmap.
262 */
263int AndroidBitmap_getHardwareBuffer(JNIEnv* env, jobject bitmap,
264 AHardwareBuffer** outBuffer) __INTRODUCED_IN(30);
265
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700266#ifdef __cplusplus
267}
268#endif
269
270#endif
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700271
272/** @} */