source: trunk/src/image.cpp @ 714

Last change on this file since 714 was 714, checked in by sam, 12 years ago

android: move the PNG loading test into the Image class. It now works.

File size: 4.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#include <cmath>
16#include <cstdio>
17
18#if defined USE_SDL
19#   include <SDL.h>
20#   include <SDL_image.h>
21#elif defined ANDROID_NDK
22#   include <jni.h>
23#   include <android/log.h>
24#endif
25
26#include "core.h"
27#include "lolgl.h"
28
29namespace lol
30{
31
32#if defined ANDROID_NDK
33extern JNIEnv *g_env;
34extern jobject g_ctx;
35#endif
36
37/*
38 * Image implementation class
39 */
40
41class ImageData
42{
43    friend class Image;
44
45private:
46    vec2i size;
47    Image::format_t format;
48
49#if defined USE_SDL
50    SDL_Surface *img;
51#elif defined ANDROID_NDK
52    jobject bmp;
53    jintArray array;
54    jint *pixels;
55#else
56    uint8_t *dummy;
57#endif
58};
59
60/*
61 * Public Image class
62 */
63
64Image::Image(char const *path)
65  : data(new ImageData())
66{
67#if defined USE_SDL
68    for (char const *name = path; *name; name++)
69        if ((data->img = IMG_Load(name)))
70            break;
71
72    if (!data->img)
73    {
74#if !LOL_RELEASE
75        fprintf(stderr, "ERROR: could not load %s\n", path);
76#endif
77        SDL_Quit();
78        exit(1);
79    }
80
81    data->size = vec2i(data->img->w, data->img->h);
82    data->format = data->img->format->Amask ? FORMAT_RGBA : FORMAT_RGB;
83#elif defined ANDROID_NDK
84    jclass cls = g_env->GetObjectClass(g_ctx);
85    jmethodID mid;
86
87    mid = g_env->GetMethodID(cls, "openImage",
88                             "(Ljava/lang/String;)Landroid/graphics/Bitmap;");
89    jstring name = g_env->NewStringUTF(path);
90    data->bmp = g_env->CallObjectMethod(g_ctx, mid, name);
91    g_env->DeleteLocalRef(name);
92    g_env->NewGlobalRef(data->bmp);
93
94    /* Get image dimensions */
95    mid = g_env->GetMethodID(cls, "getWidth", "(Landroid/graphics/Bitmap;)I");
96    data->size.x = g_env->CallIntMethod(g_ctx, mid, data->bmp);
97    mid = g_env->GetMethodID(cls, "getHeight", "(Landroid/graphics/Bitmap;)I");
98    data->size.y = g_env->CallIntMethod(g_ctx, mid, data->bmp);
99
100    /* Get pixels */
101    data->array = g_env->NewIntArray(data->size.x * data->size.y);
102    g_env->NewGlobalRef(data->array);
103    mid = g_env->GetMethodID(cls, "getPixels", "(Landroid/graphics/Bitmap;[I)V");
104    g_env->CallVoidMethod(g_ctx, mid, data->bmp, data->array);
105
106    data->pixels = g_env->GetIntArrayElements(data->array, 0);
107    for (int n = 0; n < data->size.x * data->size.y; n++)
108    {
109        uint32_t u = data->pixels[n];
110        u = (u & 0xff00ff00) | ((u & 0xff0000) >> 16) | ((u & 0xff) << 16);
111        data->pixels[n] = u;
112    }
113    data->format = FORMAT_RGBA;
114#else
115    data->size = 256;
116    data->format = FORMAT_RGBA;
117    data->dummy = (uint8_t *)malloc(256 * 256 * 4 * sizeof(*data->dummy));
118    uint8_t *parser = data->dummy;
119    for (int j = 0; j < 256; j++)
120        for (int i = 0; i < 256; i++)
121        {
122            *parser++ = ((i ^ j) & 1) * 0xff;
123            *parser++ = (uint8_t)i;
124            *parser++ = (uint8_t)j;
125            *parser++ = (((i >> 4) ^ (j >> 4)) & 1) * 0xff;
126        }
127#endif
128}
129
130vec2i Image::GetSize() const
131{
132    return data->size;
133}
134
135Image::format_t Image::GetFormat() const
136{
137    return data->format;
138}
139
140void * Image::GetData() const
141{
142#if defined USE_SDL
143    return data->img->pixels;
144#elif defined ANDROID_NDK
145    return data->pixels;
146#else
147    return data->dummy;
148#endif
149}
150
151Image::~Image()
152{
153#if defined USE_SDL
154    SDL_FreeSurface(data->img);
155#elif defined ANDROID_NDK
156    jclass cls = g_env->GetObjectClass(g_ctx);
157    jmethodID mid;
158
159    g_env->ReleaseIntArrayElements(data->array, data->pixels, 0);
160    g_env->DeleteGlobalRef(data->array);
161
162    /* Free image */
163    mid = g_env->GetMethodID(cls, "closeImage", "(Landroid/graphics/Bitmap;)V");
164    g_env->CallVoidMethod(g_ctx, mid, data->bmp);
165    g_env->DeleteGlobalRef(data->bmp);
166#else
167    free(data->dummy);
168#endif
169    delete data;
170}
171
172} /* namespace lol */
173
Note: See TracBrowser for help on using the repository browser.