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

Last change on this file since 955 was 955, checked in by sam, 11 years ago

core: implement dynamic registration of image loaders.

  • Property svn:keywords set to Id
File size: 3.0 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://sam.zoy.org/projects/COPYING.WTFPL 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 <cmath>
18
19#include <jni.h>
20#include <android/log.h>
21
22#include "core.h"
23#include "image-private.h"
24
25using namespace std;
26
27namespace lol
28{
29
30extern JavaVM *g_vm;
31extern jobject g_activity;
32
33/*
34 * Image implementation class
35 */
36
37DECLARE_IMAGE_LOADER(AndroidImageData, 100)
38{
39public:
40    virtual bool Open(char const *);
41    virtual bool Close();
42
43    virtual void *GetData() const;
44
45private:
46    jobject bmp;
47    jintArray array;
48    jint *pixels;
49};
50
51bool AndroidImageData::Open(char const *path)
52{
53    JNIEnv *env;
54    jint res = g_vm->GetEnv((void **)&env, JNI_VERSION_1_2);
55    if (res < 0)
56    {
57#if !LOL_RELEASE
58        Log::Error("could not get JVM environment\n");
59#endif
60        exit(1);
61    }
62    jclass cls = env->GetObjectClass(g_activity);
63    jmethodID mid;
64
65    mid = env->GetMethodID(cls, "openImage",
66                           "(Ljava/lang/String;)Landroid/graphics/Bitmap;");
67    jstring name = env->NewStringUTF(path);
68    bmp = env->CallObjectMethod(g_activity, mid, name);
69    env->DeleteLocalRef(name);
70    if (!bmp)
71    {
72#if !LOL_RELEASE
73        Log::Error("could not load %s\n", path);
74#endif
75        exit(1);
76    }
77    env->NewGlobalRef(bmp);
78
79    /* Get image dimensions */
80    mid = env->GetMethodID(cls, "getWidth", "(Landroid/graphics/Bitmap;)I");
81    size.x = env->CallIntMethod(g_activity, mid, bmp);
82    mid = env->GetMethodID(cls, "getHeight", "(Landroid/graphics/Bitmap;)I");
83    size.y = env->CallIntMethod(g_activity, mid, bmp);
84
85    /* Get pixels */
86    array = env->NewIntArray(size.x * size.y);
87    env->NewGlobalRef(array);
88    mid = env->GetMethodID(cls, "getPixels", "(Landroid/graphics/Bitmap;[I)V");
89    env->CallVoidMethod(g_activity, mid, bmp, array);
90
91    pixels = env->GetIntArrayElements(array, 0);
92    for (int n = 0; n < size.x * size.y; n++)
93    {
94        uint32_t u = pixels[n];
95        u = (u & 0xff00ff00) | ((u & 0xff0000) >> 16) | ((u & 0xff) << 16);
96        pixels[n] = u;
97    }
98    format = FORMAT_RGBA;
99
100    return true;
101}
102
103bool AndroidImageData::Close()
104{
105    JNIEnv *env;
106    jint res = g_vm->GetEnv((void **)&env, JNI_VERSION_1_2);
107    if (res < 0)
108    {
109#if !LOL_RELEASE
110        Log::Error("could not get JVM environment\n");
111#endif
112        exit(1);
113    }
114    jclass cls = env->GetObjectClass(g_activity);
115    jmethodID mid;
116
117    env->ReleaseIntArrayElements(array, pixels, 0);
118    env->DeleteGlobalRef(array);
119
120    /* Free image */
121    mid = env->GetMethodID(cls, "closeImage", "(Landroid/graphics/Bitmap;)V");
122    env->CallVoidMethod(g_activity, mid, bmp);
123    env->DeleteGlobalRef(bmp);
124
125    return true;
126}
127
128void * AndroidImageData::GetData() const
129{
130    return pixels;
131}
132
133} /* namespace lol */
134
135#endif /* __ANDROID__ */
136
Note: See TracBrowser for help on using the repository browser.