blob: 87a14c021de370118575df7d5b7c38f185c1e45f [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
Elliott Hughes08e6cf52021-02-08 12:37:53 -080034#if !defined(__INTRODUCED_IN)
35#define __INTRODUCED_IN(__api_level) /* nothing */
Leon Scroggins IIIc1f093f2021-01-08 14:25:31 -050036#endif
37
Mathias Agopiane1c61d32012-03-23 14:19:36 -070038#ifdef __cplusplus
39extern "C" {
40#endif
41
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070042/** AndroidBitmap functions result code. */
43enum {
44 /** Operation was successful. */
45 ANDROID_BITMAP_RESULT_SUCCESS = 0,
46 /** Bad parameter. */
47 ANDROID_BITMAP_RESULT_BAD_PARAMETER = -1,
48 /** JNI exception occured. */
49 ANDROID_BITMAP_RESULT_JNI_EXCEPTION = -2,
50 /** Allocation failed. */
51 ANDROID_BITMAP_RESULT_ALLOCATION_FAILED = -3,
52};
Mathias Agopiane1c61d32012-03-23 14:19:36 -070053
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070054/** Backward compatibility: this macro used to be misspelled. */
Andrew Hsieh370980c2012-12-17 08:01:36 +080055#define ANDROID_BITMAP_RESUT_SUCCESS ANDROID_BITMAP_RESULT_SUCCESS
56
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070057/** Bitmap pixel format. */
Mathias Agopiane1c61d32012-03-23 14:19:36 -070058enum AndroidBitmapFormat {
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070059 /** No format. */
Mathias Agopiane1c61d32012-03-23 14:19:36 -070060 ANDROID_BITMAP_FORMAT_NONE = 0,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070061 /** Red: 8 bits, Green: 8 bits, Blue: 8 bits, Alpha: 8 bits. **/
Mathias Agopiane1c61d32012-03-23 14:19:36 -070062 ANDROID_BITMAP_FORMAT_RGBA_8888 = 1,
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070063 /** Red: 5 bits, Green: 6 bits, Blue: 5 bits. **/
Mathias Agopiane1c61d32012-03-23 14:19:36 -070064 ANDROID_BITMAP_FORMAT_RGB_565 = 4,
Quddus Chong4a1a45b2017-02-08 10:38:21 -080065 /** 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 -070066 ANDROID_BITMAP_FORMAT_RGBA_4444 = 7,
Quddus Chong4a1a45b2017-02-08 10:38:21 -080067 /** Alpha: 8 bits. */
Mathias Agopiane1c61d32012-03-23 14:19:36 -070068 ANDROID_BITMAP_FORMAT_A_8 = 8,
Leon Scroggins IIIf73f5f22019-08-12 13:43:55 -040069 /** Each component is stored as a half float. **/
70 ANDROID_BITMAP_FORMAT_RGBA_F16 = 9,
Alec Mourif6ce3982022-01-20 14:31:35 -080071 /** Red: 10 bits, Green: 10 bits, Blue: 10 bits, Alpha: 2 bits. **/
72 ANDROID_BITMAP_FORMAT_RGBA_1010102 = 10,
Mathias Agopiane1c61d32012-03-23 14:19:36 -070073};
74
Leon Scroggins III0dd622e2019-08-23 15:44:21 -040075/** Bitmap alpha format */
76enum {
77 /** Pixel components are premultiplied by alpha. */
78 ANDROID_BITMAP_FLAGS_ALPHA_PREMUL = 0,
79 /** Pixels are opaque. */
80 ANDROID_BITMAP_FLAGS_ALPHA_OPAQUE = 1,
81 /** Pixel components are independent of alpha. */
82 ANDROID_BITMAP_FLAGS_ALPHA_UNPREMUL = 2,
83 /** Bit mask for AndroidBitmapFormat.flags to isolate the alpha. */
84 ANDROID_BITMAP_FLAGS_ALPHA_MASK = 0x3,
85 /** Shift for AndroidBitmapFormat.flags to isolate the alpha. */
86 ANDROID_BITMAP_FLAGS_ALPHA_SHIFT = 0,
87};
88
Leon Scroggins IIId4672a82020-01-19 19:25:41 -050089enum {
90 /** If this bit is set in AndroidBitmapInfo.flags, the Bitmap uses the
Leon Scroggins III4883c522020-01-30 15:10:11 -050091 * HARDWARE Config, and its {@link AHardwareBuffer} can be retrieved via
92 * {@link AndroidBitmap_getHardwareBuffer}.
Leon Scroggins IIId4672a82020-01-19 19:25:41 -050093 */
94 ANDROID_BITMAP_FLAGS_IS_HARDWARE = 1 << 31,
95};
96
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070097/** Bitmap info, see AndroidBitmap_getInfo(). */
Mathias Agopiane1c61d32012-03-23 14:19:36 -070098typedef struct {
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070099 /** The bitmap width in pixels. */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700100 uint32_t width;
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700101 /** The bitmap height in pixels. */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700102 uint32_t height;
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700103 /** The number of byte per row. */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700104 uint32_t stride;
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700105 /** The bitmap pixel format. See {@link AndroidBitmapFormat} */
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700106 int32_t format;
Leon Scroggins III9cb2ffd2020-02-06 11:16:54 -0500107 /** Bitfield containing information about the bitmap.
108 *
109 * <p>Two bits are used to encode alpha. Use {@link ANDROID_BITMAP_FLAGS_ALPHA_MASK}
110 * and {@link ANDROID_BITMAP_FLAGS_ALPHA_SHIFT} to retrieve them.</p>
111 *
112 * <p>One bit is used to encode whether the Bitmap uses the HARDWARE Config. Use
113 * {@link ANDROID_BITMAP_FLAGS_IS_HARDWARE} to know.</p>
114 *
115 * <p>These flags were introduced in API level 30.</p>
116 */
Leon Scroggins III0dd622e2019-08-23 15:44:21 -0400117 uint32_t flags;
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700118} AndroidBitmapInfo;
119
120/**
Leon Scroggins III9cb2ffd2020-02-06 11:16:54 -0500121 * Given a java bitmap object, fill out the {@link AndroidBitmapInfo} struct for it.
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700122 * If the call fails, the info parameter will be ignored.
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700123 */
124int AndroidBitmap_getInfo(JNIEnv* env, jobject jbitmap,
125 AndroidBitmapInfo* info);
126
Leon Scroggins III3dd64552020-01-10 13:41:44 -0500127/**
Leon Scroggins III4883c522020-01-30 15:10:11 -0500128 * Given a java bitmap object, return its {@link ADataSpace}.
Leon Scroggins III3dd64552020-01-10 13:41:44 -0500129 *
Leon Scroggins III4883c522020-01-30 15:10:11 -0500130 * Note that {@link ADataSpace} only exposes a few values. This may return
131 * {@link ADATASPACE_UNKNOWN}, even for Named ColorSpaces, if they have no
Leon Scroggins III3dd64552020-01-10 13:41:44 -0500132 * corresponding ADataSpace.
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700133 *
134 * Available since API level 30.
Leon Scroggins III3dd64552020-01-10 13:41:44 -0500135 */
136int32_t AndroidBitmap_getDataSpace(JNIEnv* env, jobject jbitmap) __INTRODUCED_IN(30);
137
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700138/**
139 * Given a java bitmap object, attempt to lock the pixel address.
140 * Locking will ensure that the memory for the pixels will not move
141 * until the unlockPixels call, and ensure that, if the pixels had been
142 * previously purged, they will have been restored.
143 *
144 * If this call succeeds, it must be balanced by a call to
145 * AndroidBitmap_unlockPixels, after which time the address of the pixels should
146 * no longer be used.
147 *
148 * If this succeeds, *addrPtr will be set to the pixel address. If the call
149 * fails, addrPtr will be ignored.
150 */
151int AndroidBitmap_lockPixels(JNIEnv* env, jobject jbitmap, void** addrPtr);
152
153/**
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700154 * Call this to balance a successful call to AndroidBitmap_lockPixels.
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700155 */
156int AndroidBitmap_unlockPixels(JNIEnv* env, jobject jbitmap);
157
Leon Scroggins III7f2d5b62019-12-13 14:57:07 -0500158// Note: these values match android.graphics.Bitmap#compressFormat.
159
160/**
161 * Specifies the formats that can be compressed to with
162 * {@link AndroidBitmap_compress}.
163 */
164enum AndroidBitmapCompressFormat {
165 /**
166 * Compress to the JPEG format. quality of 0 means
167 * compress for the smallest size. 100 means compress for max
168 * visual quality.
169 */
170 ANDROID_BITMAP_COMPRESS_FORMAT_JPEG = 0,
171 /**
172 * Compress to the PNG format. PNG is lossless, so quality is
173 * ignored.
174 */
175 ANDROID_BITMAP_COMPRESS_FORMAT_PNG = 1,
176 /**
177 * Compress to the WEBP lossy format. quality of 0 means
178 * compress for the smallest size. 100 means compress for max
179 * visual quality.
180 */
181 ANDROID_BITMAP_COMPRESS_FORMAT_WEBP_LOSSY = 3,
182 /**
183 * Compress to the WEBP lossless format. quality refers to how
184 * much effort to put into compression. A value of 0 means to
185 * compress quickly, resulting in a relatively large file size.
186 * 100 means to spend more time compressing, resulting in a
187 * smaller file.
188 */
189 ANDROID_BITMAP_COMPRESS_FORMAT_WEBP_LOSSLESS = 4,
190};
191
192/**
193 * User-defined function for writing the output of compression.
194 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700195 * Available since API level 30.
196 *
Leon Scroggins III7f2d5b62019-12-13 14:57:07 -0500197 * @param userContext Pointer to user-defined data passed to
198 * {@link AndroidBitmap_compress}.
Elliott Hughes84612ed2023-09-15 22:54:30 +0000199 * @param data Compressed data of `size` bytes to write.
Leon Scroggins III7f2d5b62019-12-13 14:57:07 -0500200 * @param size Length in bytes of data to write.
201 * @return Whether the operation succeeded.
202 */
Leon Scroggins IIIc04a63e2020-01-22 11:52:48 -0500203typedef bool (*AndroidBitmap_CompressWriteFunc)(void* userContext,
Leon Scroggins III7f2d5b62019-12-13 14:57:07 -0500204 const void* data,
205 size_t size) __INTRODUCED_IN(30);
206
207/**
Elliott Hughes84612ed2023-09-15 22:54:30 +0000208 * Compress `pixels` as described by `info`.
Leon Scroggins III7f2d5b62019-12-13 14:57:07 -0500209 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700210 * Available since API level 30.
211 *
Leon Scroggins III7f2d5b62019-12-13 14:57:07 -0500212 * @param info Description of the pixels to compress.
213 * @param dataspace {@link ADataSpace} describing the color space of the
214 * pixels.
215 * @param pixels Pointer to pixels to compress.
Leon Scroggins III4883c522020-01-30 15:10:11 -0500216 * @param format {@link AndroidBitmapCompressFormat} to compress to.
Leon Scroggins III7f2d5b62019-12-13 14:57:07 -0500217 * @param quality Hint to the compressor, 0-100. The value is interpreted
218 * differently depending on the
219 * {@link AndroidBitmapCompressFormat}.
220 * @param userContext User-defined data which will be passed to the supplied
Leon Scroggins IIIc04a63e2020-01-22 11:52:48 -0500221 * {@link AndroidBitmap_CompressWriteFunc} each time it is
Leon Scroggins III7f2d5b62019-12-13 14:57:07 -0500222 * called. May be null.
Leon Scroggins III4883c522020-01-30 15:10:11 -0500223 * @param fn Function that writes the compressed data. Will be called each time
224 * the compressor has compressed more data that is ready to be
225 * written. May be called more than once for each call to this method.
226 * May not be null.
Leon Scroggins III7f2d5b62019-12-13 14:57:07 -0500227 * @return AndroidBitmap functions result code.
228 */
229int AndroidBitmap_compress(const AndroidBitmapInfo* info,
230 int32_t dataspace,
231 const void* pixels,
232 int32_t format, int32_t quality,
233 void* userContext,
Leon Scroggins IIIc04a63e2020-01-22 11:52:48 -0500234 AndroidBitmap_CompressWriteFunc fn) __INTRODUCED_IN(30);
Leon Scroggins III7f2d5b62019-12-13 14:57:07 -0500235
Leon Scroggins IIId4672a82020-01-19 19:25:41 -0500236struct AHardwareBuffer;
Colin Crossfbcf9d72020-02-24 15:26:37 -0800237typedef struct AHardwareBuffer AHardwareBuffer;
Leon Scroggins IIId4672a82020-01-19 19:25:41 -0500238
239/**
240 * Retrieve the native object associated with a HARDWARE Bitmap.
241 *
242 * Client must not modify it while a Bitmap is wrapping it.
243 *
Elliott Hughes7be0e2d2020-06-02 13:05:04 -0700244 * Available since API level 30.
245 *
gfan7fed43d2021-04-06 18:50:06 -0700246 * @param env Handle to the JNI environment pointer.
Leon Scroggins IIId4672a82020-01-19 19:25:41 -0500247 * @param bitmap Handle to an android.graphics.Bitmap.
248 * @param outBuffer On success, is set to a pointer to the
Leon Scroggins III4883c522020-01-30 15:10:11 -0500249 * {@link AHardwareBuffer} associated with bitmap. This acquires
Leon Scroggins IIId4672a82020-01-19 19:25:41 -0500250 * a reference on the buffer, and the client must call
Leon Scroggins III4883c522020-01-30 15:10:11 -0500251 * {@link AHardwareBuffer_release} when finished with it.
Leon Scroggins IIId4672a82020-01-19 19:25:41 -0500252 * @return AndroidBitmap functions result code.
Leon Scroggins III4883c522020-01-30 15:10:11 -0500253 * {@link ANDROID_BITMAP_RESULT_BAD_PARAMETER} if bitmap is not a
Leon Scroggins IIId4672a82020-01-19 19:25:41 -0500254 * HARDWARE Bitmap.
255 */
256int AndroidBitmap_getHardwareBuffer(JNIEnv* env, jobject bitmap,
257 AHardwareBuffer** outBuffer) __INTRODUCED_IN(30);
258
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700259#ifdef __cplusplus
260}
261#endif
262
263#endif
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700264
265/** @} */