source: trunk/src/image/codec/android-image.cpp @ 2183

Last change on this file since 2183 was 2183, checked in by sam, 7 years ago

build: fix the WTFPL site URL in all code comments.

  • Property svn:keywords set to Id
File size: 3.3 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://www.wtfpl.net/ for more details.
9//
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#if defined __ANDROID__
16
17#include <jni.h>
18#include <android/log.h>
19
20#include "core.h"
21#include "../../image/image-private.h"
22
23using namespace std;
24
25namespace lol
26{
27
28extern JavaVM *g_vm;
29extern jobject g_activity;
30
31/*
32 * Image implementation class
33 */
34
35DECLARE_IMAGE_LOADER(AndroidImageData, 100)
36{
37public:
38    virtual bool Open(char const *);
39    virtual bool Close();
40
41    virtual void *GetData() const;
42
43private:
44    jobject bmp;
45    jintArray array;
46    jint *pixels;
47};
48
49bool AndroidImageData::Open(char const *path)
50{
51    JNIEnv *env;
52    jint res = g_vm->GetEnv((void **)&env, JNI_VERSION_1_2);
53    if (res < 0)
54    {
55#if !LOL_RELEASE
56        Log::Error("JVM environment not found, trying to attach thread\n");
57#endif
58        res = g_vm->AttachCurrentThread(&env, NULL);
59    }
60    if (res < 0)
61    {
62#if !LOL_RELEASE
63        Log::Error("JVM environment not found, cannot open image %s\n", path);
64#endif
65        return false;
66    }
67    jclass cls = env->GetObjectClass(g_activity);
68    jmethodID mid;
69
70    mid = env->GetMethodID(cls, "openImage",
71                           "(Ljava/lang/String;)Landroid/graphics/Bitmap;");
72    jstring name = env->NewStringUTF(path);
73    bmp = env->CallObjectMethod(g_activity, mid, name);
74    env->DeleteLocalRef(name);
75    if (!bmp)
76    {
77#if !LOL_RELEASE
78        Log::Error("could not load %s\n", path);
79#endif
80        return false;
81    }
82    env->NewGlobalRef(bmp);
83
84    /* Get image dimensions */
85    mid = env->GetMethodID(cls, "getWidth", "(Landroid/graphics/Bitmap;)I");
86    size.x = env->CallIntMethod(g_activity, mid, bmp);
87    mid = env->GetMethodID(cls, "getHeight", "(Landroid/graphics/Bitmap;)I");
88    size.y = env->CallIntMethod(g_activity, mid, bmp);
89
90    /* Get pixels */
91    array = env->NewIntArray(size.x * size.y);
92    env->NewGlobalRef(array);
93    mid = env->GetMethodID(cls, "getPixels", "(Landroid/graphics/Bitmap;[I)V");
94    env->CallVoidMethod(g_activity, mid, bmp, array);
95
96    pixels = env->GetIntArrayElements(array, 0);
97    for (int n = 0; n < size.x * size.y; n++)
98    {
99        uint32_t u = pixels[n];
100        u = (u & 0xff00ff00) | ((u & 0xff0000) >> 16) | ((u & 0xff) << 16);
101        pixels[n] = u;
102    }
103    format = Image::FORMAT_RGBA;
104
105    return true;
106}
107
108bool AndroidImageData::Close()
109{
110    JNIEnv *env;
111    jint res = g_vm->GetEnv((void **)&env, JNI_VERSION_1_2);
112    if (res < 0)
113    {
114#if !LOL_RELEASE
115        Log::Error("JVM environment not found, cannot close image\n");
116#endif
117        return false;
118    }
119    jclass cls = env->GetObjectClass(g_activity);
120    jmethodID mid;
121
122    env->ReleaseIntArrayElements(array, pixels, 0);
123    env->DeleteGlobalRef(array);
124
125    /* Free image */
126    mid = env->GetMethodID(cls, "closeImage", "(Landroid/graphics/Bitmap;)V");
127    env->CallVoidMethod(g_activity, mid, bmp);
128    env->DeleteGlobalRef(bmp);
129
130    return true;
131}
132
133void * AndroidImageData::GetData() const
134{
135    return pixels;
136}
137
138} /* namespace lol */
139
140#endif /* __ANDROID__ */
141
Note: See TracBrowser for help on using the repository browser.