Debian Codegraph @

src/android/android_image.c

raw
1 /* ______ ___ ___
2 * /\ _ \ /\_ \ /\_ \
3 * \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___
4 * \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\
5 * \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \
6 * \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
7 * \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
8 * /\____/
9 * \_/__/
10 *
11 * Android image loading.
12 *
13 * By Thomas Fjellstrom.
14 */
15
16 #include "allegro5/allegro.h"
17 #include "allegro5/allegro_android.h"
18 #include "allegro5/internal/aintern_android.h"
19 #include "allegro5/internal/aintern_bitmap.h"
20
21 #include <jni.h>
22
23 ALLEGRO_DEBUG_CHANNEL("android")
24
25 static void
26 copy_bitmap_data(ALLEGRO_BITMAP *bitmap,
27 const uint32_t *src, ALLEGRO_PIXEL_FORMAT src_format, int src_pitch,
28 int bitmap_w, int bitmap_h)
29 {
30 ALLEGRO_LOCKED_REGION *lr;
31
32 lr = al_lock_bitmap(bitmap, ALLEGRO_PIXEL_FORMAT_ANY,
33 ALLEGRO_LOCK_WRITEONLY);
34 ASSERT(lr);
35 if (!lr)
36 return;
37
38 _al_convert_bitmap_data(src, src_format, src_pitch,
39 lr->data, lr->format, lr->pitch,
40 0, 0, 0, 0, bitmap_w, bitmap_h);
41
42 al_unlock_bitmap(bitmap);
43 }
44
45 static void
46 copy_bitmap_data_multiply_alpha(ALLEGRO_BITMAP *bitmap, const uint32_t *src,
47 int bitmap_w, int bitmap_h)
48 {
49 ALLEGRO_LOCKED_REGION *lr;
50 int x, y;
51
52 lr = al_lock_bitmap(bitmap, ALLEGRO_PIXEL_FORMAT_ABGR_8888,
53 ALLEGRO_LOCK_WRITEONLY);
54 ASSERT(lr);
55 if (!lr)
56 return;
57
58 for (y = 0; y < bitmap_h; y++) {
59 uint32_t *dst = (uint32_t *)(((uint8_t *)lr->data) + y * lr->pitch);
60 for (x = 0; x < bitmap_w; x++) {
61 uint32_t c = *src++;
62 uint32_t a = (c >> 24) & 0xff;
63 c = (a << 24)
64 | (((c >> 16) & 0xff) * a / 255)
65 | ((((c >> 8) & 0xff) * a / 255) << 8)
66 | (((c & 0xff) * a / 255) << 16);
67 *dst++ = c;
68 }
69 }
70
71 al_unlock_bitmap(bitmap);
72 }
73
74 static void
75 copy_bitmap_data_demultiply_alpha(ALLEGRO_BITMAP *bitmap, const uint32_t *src,
76 int src_format, int src_pitch, int bitmap_w, int bitmap_h)
77 {
78 ALLEGRO_LOCKED_REGION *lr;
79 int x, y;
80
81 lr = al_lock_bitmap(bitmap, ALLEGRO_PIXEL_FORMAT_ABGR_8888,
82 ALLEGRO_LOCK_WRITEONLY);
83 ASSERT(lr);
84 if (!lr)
85 return;
86
87 _al_convert_bitmap_data(src, src_format, src_pitch,
88 lr->data, lr->format, lr->pitch,
89 0, 0, 0, 0, bitmap_w, bitmap_h);
90
91 for (y = 0; y < bitmap_h; y++) {
92 uint32_t *dst = (uint32_t *)(((uint8_t *)lr->data) + y * lr->pitch);
93 for (x = 0; x < bitmap_w; x++) {
94 uint32_t c = *dst;
95 uint8_t a = (c >> 24);
96 uint8_t b = (c >> 16);
97 uint8_t g = (c >> 8);
98 uint8_t r = (c >> 0);
99 // NOTE: avoid divide by zero by adding a fraction.
100 float alpha_mul = 255.0f / (a+0.001f);
101 r *= alpha_mul;
102 g *= alpha_mul;
103 b *= alpha_mul;
104 *dst = ((uint32_t)a << 24)
105 | ((uint32_t)b << 16)
106 | ((uint32_t)g << 8)
107 | ((uint32_t)r);
108 dst++;
109 }
110 }
111
112 al_unlock_bitmap(bitmap);
113 }
114
115 /* Note: This is not used when loading an image from the .apk.
116 *
117 * The ImageLoader class uses Java to load a bitmap. To support
118 * Allegro's filesystem functions, the bitmap is read from an
119 * AllegroInputStream which in turn calls back into C to use Allegro's
120 * file functions.
121 */
122 ALLEGRO_BITMAP *_al_android_load_image_f(ALLEGRO_FILE *fh, int flags)
123 {
124 JNIEnv *jnienv;
125 jclass image_loader_class;
126 jobject input_stream_class;
127 jmethodID input_stream_ctor;
128 jobject input_stream;
129 int buffer_len;
130 uint8_t *buffer;
131 jobject byte_buffer;
132 jobject jbitmap;
133 ALLEGRO_BITMAP *bitmap;
134 int bitmap_w;
135 int bitmap_h;
136 int pitch;
137
138 if (flags & ALLEGRO_KEEP_INDEX) {
139 ALLEGRO_ERROR("ALLEGRO_KEEP_INDEX not yet supported\n");
140 return NULL;
141 }
142
143 jnienv = (JNIEnv *)_al_android_get_jnienv();
144 // Note: This is always ImageLoader
145 image_loader_class = _al_android_image_loader_class();
146 // Note: This is always AllegroInputStream
147 input_stream_class = _al_android_input_stream_class();
148 input_stream_ctor = _jni_call(jnienv, jclass, GetMethodID,
149 input_stream_class, "<init>", "(J)V");
150 input_stream = _jni_call(jnienv, jobject, NewObject, input_stream_class,
151 input_stream_ctor, (jlong)(intptr_t)fh);
152 if (!input_stream) {
153 ALLEGRO_ERROR("failed to create new AllegroInputStream object");
154 return NULL;
155 }
156
157 jbitmap = _jni_callStaticObjectMethodV(jnienv, image_loader_class,
158 "decodeBitmapStream",
159 "(Ljava/io/InputStream;)Landroid/graphics/Bitmap;",
160 input_stream);
161
162 _jni_callv(jnienv, DeleteLocalRef, input_stream);
163
164 if (!jbitmap)
165 return NULL;
166
167 bitmap_w = _jni_callIntMethod(jnienv, jbitmap, "getWidth");
168 bitmap_h = _jni_callIntMethod(jnienv, jbitmap, "getHeight");
169 pitch = _jni_callIntMethod(jnienv, jbitmap, "getRowBytes");
170
171 buffer_len = pitch * bitmap_h;
172 buffer = al_malloc(buffer_len);
173 if (!buffer) {
174 _jni_callv(jnienv, DeleteLocalRef, jbitmap);
175 return NULL;
176 }
177
178 int src_format = _jni_callStaticIntMethodV(jnienv, image_loader_class,
179 "getBitmapFormat", "(Landroid/graphics/Bitmap;)I", jbitmap);
180
181 // FIXME: at some point add support for the ndk AndroidBitmap api need to
182 // check for header at build time, and android version at runtime if thats
183 // even possible, might need to try and dynamically load the lib? I dunno.
184 // That would get rid of this buffer allocation and copy.
185 byte_buffer = _jni_call(jnienv, jobject, NewDirectByteBuffer, buffer,
186 buffer_len);
187
188 _jni_callVoidMethodV(jnienv, jbitmap,
189 "copyPixelsToBuffer", "(Ljava/nio/Buffer;)V", byte_buffer);
190
191 // Tell java we don't need the byte_buffer object.
192 _jni_callv(jnienv, DeleteLocalRef, byte_buffer);
193
194 // Tell java we're done with the bitmap as well.
195 _jni_callv(jnienv, DeleteLocalRef, jbitmap);
196
197 bitmap = al_create_bitmap(bitmap_w, bitmap_h);
198 if (!bitmap) {
199 al_free(buffer);
200 return NULL;
201 }
202
203 /* buffer already has alpha multiplied in. */
204 if (flags & ALLEGRO_NO_PREMULTIPLIED_ALPHA) {
205 copy_bitmap_data_demultiply_alpha(bitmap, (const uint32_t *)buffer,
206 src_format, pitch, bitmap_w, bitmap_h);
207 }
208 else {
209 copy_bitmap_data(bitmap, (const uint32_t *)buffer, src_format, pitch,
210 bitmap_w, bitmap_h);
211 }
212
213 al_free(buffer);
214
215 return bitmap;
216 }
217
218 static ALLEGRO_BITMAP *android_load_image_asset(const char *filename, int flags)
219 {
220 JNIEnv *jnienv;
221 jclass image_loader_class;
222 jobject activity;
223 jobject str;
224 jobject jbitmap;
225 int bitmap_w;
226 int bitmap_h;
227 ALLEGRO_BITMAP *bitmap;
228 jintArray ia;
229 jint *arr;
230
231 if (flags & ALLEGRO_KEEP_INDEX) {
232 ALLEGRO_ERROR("ALLEGRO_KEEP_INDEX not yet supported\n");
233 return NULL;
234 }
235
236 jnienv = _al_android_get_jnienv();
237 image_loader_class = _al_android_image_loader_class();
238 activity = _al_android_activity_object();
239 str = (*jnienv)->NewStringUTF(jnienv, filename);
240 jbitmap = _jni_callStaticObjectMethodV(jnienv, image_loader_class,
241 "decodeBitmapAsset",
242 "(Landroid/app/Activity;Ljava/lang/String;)Landroid/graphics/Bitmap;",
243 activity, str);
244
245 /* For future Java noobs like me: If the calling thread is a Java
246 * thread, it will clean up these references when the native method
247 * returns. But here that's not the case (though technically we were
248 * spawned ultimately by a Java thread, we never return.) In any case,
249 * it never does any harm to release the reference anyway.
250 */
251 (*jnienv)->DeleteLocalRef(jnienv, str);
252
253 if (!jbitmap)
254 return NULL;
255
256 bitmap_w = _jni_callIntMethod(jnienv, jbitmap, "getWidth");
257 bitmap_h = _jni_callIntMethod(jnienv, jbitmap, "getHeight");
258 ALLEGRO_DEBUG("bitmap dimensions: %d, %d", bitmap_w, bitmap_h);
259
260 bitmap = al_create_bitmap(bitmap_w, bitmap_h);
261 if (!bitmap) {
262 _jni_callv(jnienv, DeleteLocalRef, jbitmap);
263 return NULL;
264 }
265
266 ia = _jni_callStaticObjectMethodV(jnienv, image_loader_class,
267 "getPixels", "(Landroid/graphics/Bitmap;)[I", jbitmap);
268 arr = (*jnienv)->GetIntArrayElements(jnienv, ia, 0);
269
270 /* arr is an array of packed colours, NON-premultiplied alpha. */
271 if (flags & ALLEGRO_NO_PREMULTIPLIED_ALPHA) {
272 int src_format = ALLEGRO_PIXEL_FORMAT_ARGB_8888;
273 int src_pitch = bitmap_w * sizeof(uint32_t);
274 copy_bitmap_data(bitmap, (const uint32_t *)arr,
275 src_format, src_pitch, bitmap_w, bitmap_h);
276 }
277 else {
278 copy_bitmap_data_multiply_alpha(bitmap, (const uint32_t *)arr,
279 bitmap_w, bitmap_h);
280 }
281
282 (*jnienv)->ReleaseIntArrayElements(jnienv, ia, arr, JNI_ABORT);
283 _jni_callv(jnienv, DeleteLocalRef, ia);
284 _jni_callv(jnienv, DeleteLocalRef, jbitmap);
285
286 return bitmap;
287 }
288
289 ALLEGRO_BITMAP *_al_android_load_image(const char *filename, int flags)
290 {
291 ALLEGRO_FILE *fp;
292 ALLEGRO_BITMAP *bmp;
293
294 /* Bypass the ALLEGRO_FILE interface when we know the underlying stream
295 * implementation, to avoid a lot of shunting between C and Java.
296 * We could probably do this for normal filesystem as well.
297 */
298 if (al_get_new_file_interface() == _al_get_apk_file_vtable()) {
299 return android_load_image_asset(filename, flags);
300 }
301
302 fp = al_fopen(filename, "rb");
303 if (fp) {
304 bmp = _al_android_load_image_f(fp, flags);
305 al_fclose(fp);
306 return bmp;
307 }
308
309 return NULL;
310 }
311
312 /* vim: set sts=3 sw=3 et: */