Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 

214 lignes
5.6 KiB

  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. #if defined HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  13. #include <cmath>
  14. #if defined __APPLE__ && defined __MACH__
  15. # import <UIKit/UIKit.h>
  16. #elif defined USE_SDL
  17. # include <SDL.h>
  18. # include <SDL_image.h>
  19. #elif defined ANDROID_NDK
  20. # include <jni.h>
  21. # include <android/log.h>
  22. #endif
  23. #include "core.h"
  24. namespace lol
  25. {
  26. #if defined ANDROID_NDK
  27. extern JNIEnv *g_env;
  28. extern jobject g_ctx;
  29. #endif
  30. /*
  31. * Image implementation class
  32. */
  33. class ImageData
  34. {
  35. friend class Image;
  36. private:
  37. vec2i size;
  38. Image::format_t format;
  39. #if defined __APPLE__ && defined __MACH__
  40. uint8_t *pixels;
  41. #elif defined USE_SDL
  42. SDL_Surface *img;
  43. #elif defined ANDROID_NDK
  44. jobject bmp;
  45. jintArray array;
  46. jint *pixels;
  47. #else
  48. uint8_t *pixels;
  49. #endif
  50. };
  51. /*
  52. * Public Image class
  53. */
  54. Image::Image(char const *path)
  55. : data(new ImageData())
  56. {
  57. #if defined __APPLE__ && defined __MACH__
  58. NSString *fullpath = [NSString stringWithUTF8String:path];
  59. NSArray *chunks = [fullpath componentsSeparatedByString: @"/"];
  60. NSString *filename = [chunks objectAtIndex: [chunks count] - 1];
  61. chunks = [filename componentsSeparatedByString: @"."];
  62. NSString *prefix = [chunks objectAtIndex: 0];
  63. NSString *mypath = [[NSBundle mainBundle] pathForResource:prefix ofType:@"png"];
  64. NSData *pngdata = [[NSData alloc] initWithContentsOfFile:mypath];
  65. UIImage *image = [[UIImage alloc] initWithData:pngdata];
  66. if (!image)
  67. {
  68. #if !LOL_RELEASE
  69. Log::Error("could not load %s\n", path);
  70. #endif
  71. exit(1);
  72. }
  73. int w = CGImageGetWidth(image.CGImage);
  74. int h = CGImageGetHeight(image.CGImage);
  75. data->size = vec2i(w, h);
  76. data->format = FORMAT_RGBA;
  77. CGColorSpaceRef cspace = CGColorSpaceCreateDeviceRGB();
  78. data->pixels = (uint8_t *)malloc(w * h * 4);
  79. CGContextRef ctx =
  80. CGBitmapContextCreate(data->pixels, w, h, 8, 4 * w, cspace,
  81. kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
  82. CGColorSpaceRelease(cspace);
  83. CGContextClearRect(ctx, CGRectMake(0, 0, w, h));
  84. CGContextTranslateCTM(ctx, 0, h - h);
  85. CGContextDrawImage(ctx, CGRectMake(0, 0, w, h), image.CGImage);
  86. CGContextRelease(ctx);
  87. [image release];
  88. [pngdata release];
  89. #elif defined USE_SDL
  90. for (char const *name = path; *name; name++)
  91. if ((data->img = IMG_Load(name)))
  92. break;
  93. if (!data->img)
  94. {
  95. #if !LOL_RELEASE
  96. Log::Error("could not load %s\n", path);
  97. #endif
  98. SDL_Quit();
  99. exit(1);
  100. }
  101. data->size = vec2i(data->img->w, data->img->h);
  102. data->format = data->img->format->Amask ? FORMAT_RGBA : FORMAT_RGB;
  103. #elif defined ANDROID_NDK
  104. jclass cls = g_env->GetObjectClass(g_ctx);
  105. jmethodID mid;
  106. mid = g_env->GetMethodID(cls, "openImage",
  107. "(Ljava/lang/String;)Landroid/graphics/Bitmap;");
  108. jstring name = g_env->NewStringUTF(path);
  109. data->bmp = g_env->CallObjectMethod(g_ctx, mid, name);
  110. g_env->DeleteLocalRef(name);
  111. g_env->NewGlobalRef(data->bmp);
  112. /* Get image dimensions */
  113. mid = g_env->GetMethodID(cls, "getWidth", "(Landroid/graphics/Bitmap;)I");
  114. data->size.x = g_env->CallIntMethod(g_ctx, mid, data->bmp);
  115. mid = g_env->GetMethodID(cls, "getHeight", "(Landroid/graphics/Bitmap;)I");
  116. data->size.y = g_env->CallIntMethod(g_ctx, mid, data->bmp);
  117. /* Get pixels */
  118. data->array = g_env->NewIntArray(data->size.x * data->size.y);
  119. g_env->NewGlobalRef(data->array);
  120. mid = g_env->GetMethodID(cls, "getPixels", "(Landroid/graphics/Bitmap;[I)V");
  121. g_env->CallVoidMethod(g_ctx, mid, data->bmp, data->array);
  122. data->pixels = g_env->GetIntArrayElements(data->array, 0);
  123. for (int n = 0; n < data->size.x * data->size.y; n++)
  124. {
  125. uint32_t u = data->pixels[n];
  126. u = (u & 0xff00ff00) | ((u & 0xff0000) >> 16) | ((u & 0xff) << 16);
  127. data->pixels[n] = u;
  128. }
  129. data->format = FORMAT_RGBA;
  130. #else
  131. data->size = 256;
  132. data->format = FORMAT_RGBA;
  133. data->pixels = (uint8_t *)malloc(256 * 256 * 4 * sizeof(*data->pixels));
  134. uint8_t *parser = data->pixels;
  135. for (int j = 0; j < 256; j++)
  136. for (int i = 0; i < 256; i++)
  137. {
  138. *parser++ = ((i ^ j) & 1) * 0xff;
  139. *parser++ = (uint8_t)i;
  140. *parser++ = (uint8_t)j;
  141. *parser++ = (((i >> 4) ^ (j >> 4)) & 1) * 0xff;
  142. }
  143. #endif
  144. }
  145. vec2i Image::GetSize() const
  146. {
  147. return data->size;
  148. }
  149. Image::format_t Image::GetFormat() const
  150. {
  151. return data->format;
  152. }
  153. void * Image::GetData() const
  154. {
  155. #if defined __APPLE__ && defined __MACH__
  156. return data->pixels;
  157. #elif defined USE_SDL
  158. return data->img->pixels;
  159. #elif defined ANDROID_NDK
  160. return data->pixels;
  161. #else
  162. return data->pixels;
  163. #endif
  164. }
  165. Image::~Image()
  166. {
  167. #if defined __APPLE__ && defined __MACH__
  168. free(data->pixels);
  169. #elif defined USE_SDL
  170. SDL_FreeSurface(data->img);
  171. #elif defined ANDROID_NDK
  172. jclass cls = g_env->GetObjectClass(g_ctx);
  173. jmethodID mid;
  174. g_env->ReleaseIntArrayElements(data->array, data->pixels, 0);
  175. g_env->DeleteGlobalRef(data->array);
  176. /* Free image */
  177. mid = g_env->GetMethodID(cls, "closeImage", "(Landroid/graphics/Bitmap;)V");
  178. g_env->CallVoidMethod(g_ctx, mid, data->bmp);
  179. g_env->DeleteGlobalRef(data->bmp);
  180. #else
  181. free(data->pixels);
  182. #endif
  183. delete data;
  184. }
  185. } /* namespace lol */