IMG_UIImage.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /*
  2. * IMG_ImageIO.c
  3. * SDL_image
  4. *
  5. * Created by Eric Wing on 1/2/09.
  6. * Copyright 2009 __MyCompanyName__. All rights reserved.
  7. *
  8. */
  9. #include "SDL_image.h"
  10. #import <UIKit/UIKit.h>
  11. #import <MobileCoreServices/MobileCoreServices.h> // for UTCoreTypes.h
  12. // Once we have our image, we need to get it into an SDL_Surface
  13. // (Copied straight from the ImageIO backend.)
  14. static SDL_Surface* Create_SDL_Surface_From_CGImage(CGImageRef image_ref)
  15. {
  16. /* This code is adapted from Apple's Documentation found here:
  17. * http://developer.apple.com/documentation/GraphicsImaging/Conceptual/OpenGL-MacProgGuide/index.html
  18. * Listing 9-4††Using a Quartz image as a texture source.
  19. * Unfortunately, this guide doesn't show what to do about
  20. * non-RGBA image formats so I'm making the rest up.
  21. * All this code should be scrutinized.
  22. */
  23. size_t w = CGImageGetWidth(image_ref);
  24. size_t h = CGImageGetHeight(image_ref);
  25. CGRect rect = {{0, 0}, {w, h}};
  26. CGImageAlphaInfo alpha = CGImageGetAlphaInfo(image_ref);
  27. //size_t bits_per_pixel = CGImageGetBitsPerPixel(image_ref);
  28. size_t bits_per_component = 8;
  29. SDL_Surface* surface;
  30. Uint32 Amask;
  31. Uint32 Rmask;
  32. Uint32 Gmask;
  33. Uint32 Bmask;
  34. CGContextRef bitmap_context;
  35. CGBitmapInfo bitmap_info;
  36. CGColorSpaceRef color_space = CGColorSpaceCreateDeviceRGB();
  37. if (alpha == kCGImageAlphaNone ||
  38. alpha == kCGImageAlphaNoneSkipFirst ||
  39. alpha == kCGImageAlphaNoneSkipLast) {
  40. bitmap_info = kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Host; /* XRGB */
  41. Amask = 0x00000000;
  42. } else {
  43. /* kCGImageAlphaFirst isn't supported */
  44. //bitmap_info = kCGImageAlphaFirst | kCGBitmapByteOrder32Host; /* ARGB */
  45. bitmap_info = kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host; /* ARGB */
  46. Amask = 0xFF000000;
  47. }
  48. Rmask = 0x00FF0000;
  49. Gmask = 0x0000FF00;
  50. Bmask = 0x000000FF;
  51. surface = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 32, Rmask, Gmask, Bmask, Amask);
  52. if (surface)
  53. {
  54. // Sets up a context to be drawn to with surface->pixels as the area to be drawn to
  55. bitmap_context = CGBitmapContextCreate(
  56. surface->pixels,
  57. surface->w,
  58. surface->h,
  59. bits_per_component,
  60. surface->pitch,
  61. color_space,
  62. bitmap_info
  63. );
  64. // Draws the image into the context's image_data
  65. CGContextDrawImage(bitmap_context, rect, image_ref);
  66. CGContextRelease(bitmap_context);
  67. // FIXME: Reverse the premultiplied alpha
  68. if ((bitmap_info & kCGBitmapAlphaInfoMask) == kCGImageAlphaPremultipliedFirst) {
  69. int i, j;
  70. Uint8 *p = (Uint8 *)surface->pixels;
  71. for (i = surface->h * surface->pitch/4; i--; ) {
  72. #if __LITTLE_ENDIAN__
  73. Uint8 A = p[3];
  74. if (A) {
  75. for (j = 0; j < 3; ++j) {
  76. p[j] = (p[j] * 255) / A;
  77. }
  78. }
  79. #else
  80. Uint8 A = p[0];
  81. if (A) {
  82. for (j = 1; j < 4; ++j) {
  83. p[j] = (p[j] * 255) / A;
  84. }
  85. }
  86. #endif /* ENDIAN */
  87. p += 4;
  88. }
  89. }
  90. }
  91. if (color_space)
  92. {
  93. CGColorSpaceRelease(color_space);
  94. }
  95. return surface;
  96. }
  97. static SDL_Surface* LoadImageFromRWops(SDL_RWops* rw_ops, CFStringRef uti_string_hint)
  98. {
  99. NSAutoreleasePool* autorelease_pool = [[NSAutoreleasePool alloc] init];
  100. SDL_Surface* sdl_surface;
  101. UIImage* ui_image;
  102. int bytes_read = 0;
  103. // I don't know what a good size is.
  104. // Max recommended texture size is 1024x1024 on iPhone so maybe base it on that?
  105. const int block_size = 1024*4;
  106. char temp_buffer[block_size];
  107. NSMutableData* ns_data = [[NSMutableData alloc] initWithCapacity:1024*1024*4];
  108. do
  109. {
  110. bytes_read = SDL_RWread(rw_ops, temp_buffer, 1, block_size);
  111. [ns_data appendBytes:temp_buffer length:bytes_read];
  112. } while(bytes_read > 0);
  113. ui_image = [[UIImage alloc] initWithData:ns_data];
  114. sdl_surface = Create_SDL_Surface_From_CGImage([ui_image CGImage]);
  115. [ui_image release];
  116. [ns_data release];
  117. [autorelease_pool drain];
  118. return sdl_surface;
  119. }
  120. static SDL_Surface* LoadImageFromFile(const char *file)
  121. {
  122. NSAutoreleasePool* autorelease_pool = [[NSAutoreleasePool alloc] init];
  123. SDL_Surface* sdl_surface = NULL;
  124. UIImage* ui_image;
  125. NSString* ns_string;
  126. ns_string = [[NSString alloc] initWithUTF8String:file];
  127. ui_image = [[UIImage alloc] initWithContentsOfFile:ns_string];
  128. if(ui_image != NULL)
  129. {
  130. sdl_surface = Create_SDL_Surface_From_CGImage([ui_image CGImage]);
  131. }
  132. [ui_image release];
  133. [ns_string release];
  134. [autorelease_pool drain];
  135. return sdl_surface;
  136. }
  137. /* Since UIImage doesn't really support streams well, we should optimize for the file case. */
  138. SDL_Surface *IMG_Load(const char *file)
  139. {
  140. SDL_Surface* sdl_surface = NULL;
  141. sdl_surface = LoadImageFromFile(file);
  142. if(NULL == sdl_surface)
  143. {
  144. // Either the file doesn't exist or ImageIO doesn't understand the format.
  145. // For the latter case, fallback to the native SDL_image handlers.
  146. SDL_RWops *src = SDL_RWFromFile(file, "rb");
  147. char *ext = strrchr(file, '.');
  148. if(ext) {
  149. ext++;
  150. }
  151. if(!src) {
  152. /* The error message has been set in SDL_RWFromFile */
  153. return NULL;
  154. }
  155. sdl_surface = IMG_LoadTyped_RW(src, 1, ext);
  156. }
  157. return sdl_surface;
  158. }
  159. int IMG_InitJPG()
  160. {
  161. return 0;
  162. }
  163. void IMG_QuitJPG()
  164. {
  165. }
  166. int IMG_InitPNG()
  167. {
  168. return 0;
  169. }
  170. void IMG_QuitPNG()
  171. {
  172. }
  173. int IMG_InitTIF()
  174. {
  175. return 0;
  176. }
  177. void IMG_QuitTIF()
  178. {
  179. }
  180. /* Copied straight from other files so I don't have to alter them. */
  181. static int IMG_isICOCUR(SDL_RWops *src, int type)
  182. {
  183. int start;
  184. int is_ICOCUR;
  185. /* The Win32 ICO file header (14 bytes) */
  186. Uint16 bfReserved;
  187. Uint16 bfType;
  188. Uint16 bfCount;
  189. if ( !src )
  190. return 0;
  191. start = SDL_RWtell(src);
  192. is_ICOCUR = 0;
  193. bfReserved = SDL_ReadLE16(src);
  194. bfType = SDL_ReadLE16(src);
  195. bfCount = SDL_ReadLE16(src);
  196. if ((bfReserved == 0) && (bfType == type) && (bfCount != 0))
  197. is_ICOCUR = 1;
  198. SDL_RWseek(src, start, SEEK_SET);
  199. return (is_ICOCUR);
  200. }
  201. int IMG_isICO(SDL_RWops *src)
  202. {
  203. return IMG_isICOCUR(src, 1);
  204. }
  205. int IMG_isCUR(SDL_RWops *src)
  206. {
  207. return IMG_isICOCUR(src, 2);
  208. }
  209. int IMG_isBMP(SDL_RWops *src)
  210. {
  211. int start;
  212. int is_BMP;
  213. char magic[2];
  214. if ( !src )
  215. return 0;
  216. start = SDL_RWtell(src);
  217. is_BMP = 0;
  218. if ( SDL_RWread(src, magic, sizeof(magic), 1) ) {
  219. if ( strncmp(magic, "BM", 2) == 0 ) {
  220. is_BMP = 1;
  221. }
  222. }
  223. SDL_RWseek(src, start, SEEK_SET);
  224. return(is_BMP);
  225. }
  226. int IMG_isGIF(SDL_RWops *src)
  227. {
  228. int start;
  229. int is_GIF;
  230. char magic[6];
  231. if ( !src )
  232. return 0;
  233. start = SDL_RWtell(src);
  234. is_GIF = 0;
  235. if ( SDL_RWread(src, magic, sizeof(magic), 1) ) {
  236. if ( (strncmp(magic, "GIF", 3) == 0) &&
  237. ((memcmp(magic + 3, "87a", 3) == 0) ||
  238. (memcmp(magic + 3, "89a", 3) == 0)) ) {
  239. is_GIF = 1;
  240. }
  241. }
  242. SDL_RWseek(src, start, SEEK_SET);
  243. return(is_GIF);
  244. }
  245. int IMG_isJPG(SDL_RWops *src)
  246. {
  247. int start;
  248. int is_JPG;
  249. int in_scan;
  250. Uint8 magic[4];
  251. /* This detection code is by Steaphan Greene <[email protected]> */
  252. /* Blame me, not Sam, if this doesn't work right. */
  253. /* And don't forget to report the problem to the the sdl list too! */
  254. if ( !src )
  255. return 0;
  256. start = SDL_RWtell(src);
  257. is_JPG = 0;
  258. in_scan = 0;
  259. if ( SDL_RWread(src, magic, 2, 1) ) {
  260. if ( (magic[0] == 0xFF) && (magic[1] == 0xD8) ) {
  261. is_JPG = 1;
  262. while (is_JPG == 1) {
  263. if(SDL_RWread(src, magic, 1, 2) != 2) {
  264. is_JPG = 0;
  265. } else if( (magic[0] != 0xFF) && (in_scan == 0) ) {
  266. is_JPG = 0;
  267. } else if( (magic[0] != 0xFF) || (magic[1] == 0xFF) ) {
  268. /* Extra padding in JPEG (legal) */
  269. /* or this is data and we are scanning */
  270. SDL_RWseek(src, -1, SEEK_CUR);
  271. } else if(magic[1] == 0xD9) {
  272. /* Got to end of good JPEG */
  273. break;
  274. } else if( (in_scan == 1) && (magic[1] == 0x00) ) {
  275. /* This is an encoded 0xFF within the data */
  276. } else if( (magic[1] >= 0xD0) && (magic[1] < 0xD9) ) {
  277. /* These have nothing else */
  278. } else if(SDL_RWread(src, magic+2, 1, 2) != 2) {
  279. is_JPG = 0;
  280. } else {
  281. /* Yes, it's big-endian */
  282. Uint32 start;
  283. Uint32 size;
  284. Uint32 end;
  285. start = SDL_RWtell(src);
  286. size = (magic[2] << 8) + magic[3];
  287. end = SDL_RWseek(src, size-2, SEEK_CUR);
  288. if ( end != start + size - 2 ) is_JPG = 0;
  289. if ( magic[1] == 0xDA ) {
  290. /* Now comes the actual JPEG meat */
  291. #ifdef FAST_IS_JPEG
  292. /* Ok, I'm convinced. It is a JPEG. */
  293. break;
  294. #else
  295. /* I'm not convinced. Prove it! */
  296. in_scan = 1;
  297. #endif
  298. }
  299. }
  300. }
  301. }
  302. }
  303. SDL_RWseek(src, start, SEEK_SET);
  304. return(is_JPG);
  305. }
  306. int IMG_isPNG(SDL_RWops *src)
  307. {
  308. int start;
  309. int is_PNG;
  310. Uint8 magic[4];
  311. if ( !src )
  312. return 0;
  313. start = SDL_RWtell(src);
  314. is_PNG = 0;
  315. if ( SDL_RWread(src, magic, 1, sizeof(magic)) == sizeof(magic) ) {
  316. if ( magic[0] == 0x89 &&
  317. magic[1] == 'P' &&
  318. magic[2] == 'N' &&
  319. magic[3] == 'G' ) {
  320. is_PNG = 1;
  321. }
  322. }
  323. SDL_RWseek(src, start, SEEK_SET);
  324. return(is_PNG);
  325. }
  326. int IMG_isTIF(SDL_RWops* src)
  327. {
  328. int start;
  329. int is_TIF;
  330. Uint8 magic[4];
  331. if ( !src )
  332. return 0;
  333. start = SDL_RWtell(src);
  334. is_TIF = 0;
  335. if ( SDL_RWread(src, magic, 1, sizeof(magic)) == sizeof(magic) ) {
  336. if ( (magic[0] == 'I' &&
  337. magic[1] == 'I' &&
  338. magic[2] == 0x2a &&
  339. magic[3] == 0x00) ||
  340. (magic[0] == 'M' &&
  341. magic[1] == 'M' &&
  342. magic[2] == 0x00 &&
  343. magic[3] == 0x2a) ) {
  344. is_TIF = 1;
  345. }
  346. }
  347. SDL_RWseek(src, start, SEEK_SET);
  348. return(is_TIF);
  349. }
  350. SDL_Surface* IMG_LoadCUR_RW(SDL_RWops *src)
  351. {
  352. /* FIXME: Is this a supported type? */
  353. return LoadImageFromRWops(src, CFSTR("com.microsoft.cur"));
  354. }
  355. SDL_Surface* IMG_LoadICO_RW(SDL_RWops *src)
  356. {
  357. return LoadImageFromRWops(src, kUTTypeICO);
  358. }
  359. SDL_Surface* IMG_LoadBMP_RW(SDL_RWops *src)
  360. {
  361. return LoadImageFromRWops(src, kUTTypeBMP);
  362. }
  363. SDL_Surface* IMG_LoadGIF_RW(SDL_RWops *src)
  364. {
  365. return LoadImageFromRWops(src, kUTTypeGIF);
  366. }
  367. SDL_Surface* IMG_LoadJPG_RW(SDL_RWops *src)
  368. {
  369. return LoadImageFromRWops(src, kUTTypeJPEG);
  370. }
  371. SDL_Surface* IMG_LoadPNG_RW(SDL_RWops *src)
  372. {
  373. return LoadImageFromRWops(src, kUTTypePNG);
  374. }
  375. SDL_Surface* IMG_LoadTGA_RW(SDL_RWops *src)
  376. {
  377. return LoadImageFromRWops(src, CFSTR("com.truevision.tga-image"));
  378. }
  379. SDL_Surface* IMG_LoadTIF_RW(SDL_RWops *src)
  380. {
  381. return LoadImageFromRWops(src, kUTTypeTIFF);
  382. }