blob: d920a9084681cf0a14b59905a6bf781b02a90c9c [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
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070082/** Bitmap info, see AndroidBitmap_getInfo(). */
Mathias Agopiane1c61d32012-03-23 14:19:36 -070083typedef struct {
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070084 /** The bitmap width in pixels. */
Mathias Agopiane1c61d32012-03-23 14:19:36 -070085 uint32_t width;
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070086 /** The bitmap height in pixels. */
Mathias Agopiane1c61d32012-03-23 14:19:36 -070087 uint32_t height;
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070088 /** The number of byte per row. */
Mathias Agopiane1c61d32012-03-23 14:19:36 -070089 uint32_t stride;
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070090 /** The bitmap pixel format. See {@link AndroidBitmapFormat} */
Mathias Agopiane1c61d32012-03-23 14:19:36 -070091 int32_t format;
Leon Scroggins III0dd622e2019-08-23 15:44:21 -040092 /** Two bits are used to encode alpha. Use ANDROID_BITMAP_FLAGS_ALPHA_MASK
93 * and ANDROID_BITMAP_FLAGS_ALPHA_SHIFT to retrieve them. */
94 uint32_t flags;
Mathias Agopiane1c61d32012-03-23 14:19:36 -070095} AndroidBitmapInfo;
96
97/**
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -070098 * Given a java bitmap object, fill out the AndroidBitmapInfo struct for it.
99 * If the call fails, the info parameter will be ignored.
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700100 */
101int AndroidBitmap_getInfo(JNIEnv* env, jobject jbitmap,
102 AndroidBitmapInfo* info);
103
Leon Scroggins III3dd64552020-01-10 13:41:44 -0500104#if __ANDROID_API__ >= 30
105
106/**
107 * Given a java bitmap object, return its ADataSpace.
108 *
109 * Note that ADataSpace only exposes a few values. This may return
110 * ADATASPACE_UNKNOWN, even for Named ColorSpaces, if they have no
111 * corresponding ADataSpace.
112 */
113int32_t AndroidBitmap_getDataSpace(JNIEnv* env, jobject jbitmap) __INTRODUCED_IN(30);
114
115#endif // __ANDROID_API__ >= 30
116
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700117/**
118 * Given a java bitmap object, attempt to lock the pixel address.
119 * Locking will ensure that the memory for the pixels will not move
120 * until the unlockPixels call, and ensure that, if the pixels had been
121 * previously purged, they will have been restored.
122 *
123 * If this call succeeds, it must be balanced by a call to
124 * AndroidBitmap_unlockPixels, after which time the address of the pixels should
125 * no longer be used.
126 *
127 * If this succeeds, *addrPtr will be set to the pixel address. If the call
128 * fails, addrPtr will be ignored.
129 */
130int AndroidBitmap_lockPixels(JNIEnv* env, jobject jbitmap, void** addrPtr);
131
132/**
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700133 * Call this to balance a successful call to AndroidBitmap_lockPixels.
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700134 */
135int AndroidBitmap_unlockPixels(JNIEnv* env, jobject jbitmap);
136
Leon Scroggins III7f2d5b62019-12-13 14:57:07 -0500137#if __ANDROID_API__ >= 30
138
139// Note: these values match android.graphics.Bitmap#compressFormat.
140
141/**
142 * Specifies the formats that can be compressed to with
143 * {@link AndroidBitmap_compress}.
144 */
145enum AndroidBitmapCompressFormat {
146 /**
147 * Compress to the JPEG format. quality of 0 means
148 * compress for the smallest size. 100 means compress for max
149 * visual quality.
150 */
151 ANDROID_BITMAP_COMPRESS_FORMAT_JPEG = 0,
152 /**
153 * Compress to the PNG format. PNG is lossless, so quality is
154 * ignored.
155 */
156 ANDROID_BITMAP_COMPRESS_FORMAT_PNG = 1,
157 /**
158 * Compress to the WEBP lossy format. quality of 0 means
159 * compress for the smallest size. 100 means compress for max
160 * visual quality.
161 */
162 ANDROID_BITMAP_COMPRESS_FORMAT_WEBP_LOSSY = 3,
163 /**
164 * Compress to the WEBP lossless format. quality refers to how
165 * much effort to put into compression. A value of 0 means to
166 * compress quickly, resulting in a relatively large file size.
167 * 100 means to spend more time compressing, resulting in a
168 * smaller file.
169 */
170 ANDROID_BITMAP_COMPRESS_FORMAT_WEBP_LOSSLESS = 4,
171};
172
173/**
174 * User-defined function for writing the output of compression.
175 *
176 * @param userContext Pointer to user-defined data passed to
177 * {@link AndroidBitmap_compress}.
178 * @param data Compressed data of |size| bytes to write.
179 * @param size Length in bytes of data to write.
180 * @return Whether the operation succeeded.
181 */
182typedef bool (*AndroidBitmap_compress_write_fn)(void* userContext,
183 const void* data,
184 size_t size) __INTRODUCED_IN(30);
185
186/**
187 * Compress |pixels| as described by |info|.
188 *
189 * @param info Description of the pixels to compress.
190 * @param dataspace {@link ADataSpace} describing the color space of the
191 * pixels.
192 * @param pixels Pointer to pixels to compress.
193 * @param format (@link AndroidBitmapCompressFormat} to compress to.
194 * @param quality Hint to the compressor, 0-100. The value is interpreted
195 * differently depending on the
196 * {@link AndroidBitmapCompressFormat}.
197 * @param userContext User-defined data which will be passed to the supplied
198 * {@link AndroidBitmap_compress_write_fn} each time it is
199 * called. May be null.
200 * @parm fn Function that writes the compressed data. Will be called each time
201 * the compressor has compressed more data that is ready to be
202 * written. May be called more than once for each call to this method.
203 * May not be null.
204 * @return AndroidBitmap functions result code.
205 */
206int AndroidBitmap_compress(const AndroidBitmapInfo* info,
207 int32_t dataspace,
208 const void* pixels,
209 int32_t format, int32_t quality,
210 void* userContext,
211 AndroidBitmap_compress_write_fn fn) __INTRODUCED_IN(30);
212
213#endif // __ANDROID_API__ >= 30
214
Mathias Agopiane1c61d32012-03-23 14:19:36 -0700215#ifdef __cplusplus
216}
217#endif
218
219#endif
Johan Euphrosinebf6d5e02015-03-27 17:15:43 -0700220
221/** @} */