5 * Created by Eric Wing on 1/2/09.
6 * Copyright 2009 __MyCompanyName__. All rights reserved.
10 #import <UIKit/UIKit.h>
11 #import <MobileCoreServices/MobileCoreServices.h> // for UTCoreTypes.h
13 // Once we have our image, we need to get it into an SDL_Surface
14 // (Copied straight from the ImageIO backend.)
15 static SDL_Surface* Create_SDL_Surface_From_CGImage(CGImageRef image_ref)
17 /* This code is adapted from Apple's Documentation found here:
18 * http://developer.apple.com/documentation/GraphicsImaging/Conceptual/OpenGL-MacProgGuide/index.html
19 * Listing 9-4††Using a Quartz image as a texture source.
20 * Unfortunately, this guide doesn't show what to do about
21 * non-RGBA image formats so I'm making the rest up.
22 * All this code should be scrutinized.
25 size_t w = CGImageGetWidth(image_ref);
26 size_t h = CGImageGetHeight(image_ref);
27 CGRect rect = {{0, 0}, {w, h}};
29 CGImageAlphaInfo alpha = CGImageGetAlphaInfo(image_ref);
30 //size_t bits_per_pixel = CGImageGetBitsPerPixel(image_ref);
31 size_t bits_per_component = 8;
39 CGContextRef bitmap_context;
40 CGBitmapInfo bitmap_info;
41 CGColorSpaceRef color_space = CGColorSpaceCreateDeviceRGB();
43 if (alpha == kCGImageAlphaNone ||
44 alpha == kCGImageAlphaNoneSkipFirst ||
45 alpha == kCGImageAlphaNoneSkipLast) {
46 bitmap_info = kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Host; /* XRGB */
49 /* kCGImageAlphaFirst isn't supported */
50 //bitmap_info = kCGImageAlphaFirst | kCGBitmapByteOrder32Host; /* ARGB */
51 bitmap_info = kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host; /* ARGB */
59 surface = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 32, Rmask, Gmask, Bmask, Amask);
62 // Sets up a context to be drawn to with surface->pixels as the area to be drawn to
63 bitmap_context = CGBitmapContextCreate(
73 // Draws the image into the context's image_data
74 CGContextDrawImage(bitmap_context, rect, image_ref);
76 CGContextRelease(bitmap_context);
78 // FIXME: Reverse the premultiplied alpha
79 if ((bitmap_info & kCGBitmapAlphaInfoMask) == kCGImageAlphaPremultipliedFirst) {
81 Uint8 *p = (Uint8 *)surface->pixels;
82 for (i = surface->h * surface->pitch/4; i--; ) {
86 for (j = 0; j < 3; ++j) {
87 p[j] = (p[j] * 255) / A;
93 for (j = 1; j < 4; ++j) {
94 p[j] = (p[j] * 255) / A;
105 CGColorSpaceRelease(color_space);
111 static SDL_Surface* LoadImageFromRWops(SDL_RWops* rw_ops, CFStringRef uti_string_hint)
113 NSAutoreleasePool* autorelease_pool = [[NSAutoreleasePool alloc] init];
114 SDL_Surface* sdl_surface;
118 // I don't know what a good size is.
119 // Max recommended texture size is 1024x1024 on iPhone so maybe base it on that?
120 const int block_size = 1024*4;
121 char temp_buffer[block_size];
123 NSMutableData* ns_data = [[NSMutableData alloc] initWithCapacity:1024*1024*4];
128 bytes_read = SDL_RWread(rw_ops, temp_buffer, 1, block_size);
129 [ns_data appendBytes:temp_buffer length:bytes_read];
130 } while(bytes_read > 0);
132 ui_image = [[UIImage alloc] initWithData:ns_data];
134 sdl_surface = Create_SDL_Surface_From_CGImage([ui_image CGImage]);
139 [autorelease_pool drain];
144 static SDL_Surface* LoadImageFromFile(const char *file)
146 NSAutoreleasePool* autorelease_pool = [[NSAutoreleasePool alloc] init];
147 SDL_Surface* sdl_surface = NULL;
151 ns_string = [[NSString alloc] initWithUTF8String:file];
152 ui_image = [[UIImage alloc] initWithContentsOfFile:ns_string];
155 sdl_surface = Create_SDL_Surface_From_CGImage([ui_image CGImage]);
161 [autorelease_pool drain];
167 /* Since UIImage doesn't really support streams well, we should optimize for the file case. */
168 SDL_Surface *IMG_Load(const char *file)
170 SDL_Surface* sdl_surface = NULL;
172 sdl_surface = LoadImageFromFile(file);
173 if(NULL == sdl_surface)
175 // Either the file doesn't exist or ImageIO doesn't understand the format.
176 // For the latter case, fallback to the native SDL_image handlers.
178 SDL_RWops *src = SDL_RWFromFile(file, "rb");
179 char *ext = strrchr(file, '.');
184 /* The error message has been set in SDL_RWFromFile */
187 sdl_surface = IMG_LoadTyped_RW(src, 1, ext);
220 /* Copied straight from other files so I don't have to alter them. */
221 static int IMG_isICOCUR(SDL_RWops *src, int type)
226 /* The Win32 ICO file header (14 bytes) */
233 start = SDL_RWtell(src);
235 bfReserved = SDL_ReadLE16(src);
236 bfType = SDL_ReadLE16(src);
237 bfCount = SDL_ReadLE16(src);
238 if ((bfReserved == 0) && (bfType == type) && (bfCount != 0))
240 SDL_RWseek(src, start, SEEK_SET);
245 int IMG_isICO(SDL_RWops *src)
247 return IMG_isICOCUR(src, 1);
250 int IMG_isCUR(SDL_RWops *src)
252 return IMG_isICOCUR(src, 2);
255 int IMG_isBMP(SDL_RWops *src)
263 start = SDL_RWtell(src);
265 if ( SDL_RWread(src, magic, sizeof(magic), 1) ) {
266 if ( strncmp(magic, "BM", 2) == 0 ) {
270 SDL_RWseek(src, start, SEEK_SET);
274 int IMG_isGIF(SDL_RWops *src)
282 start = SDL_RWtell(src);
284 if ( SDL_RWread(src, magic, sizeof(magic), 1) ) {
285 if ( (strncmp(magic, "GIF", 3) == 0) &&
286 ((memcmp(magic + 3, "87a", 3) == 0) ||
287 (memcmp(magic + 3, "89a", 3) == 0)) ) {
291 SDL_RWseek(src, start, SEEK_SET);
295 int IMG_isJPG(SDL_RWops *src)
302 /* This detection code is by Steaphan Greene <stea@cs.binghamton.edu> */
303 /* Blame me, not Sam, if this doesn't work right. */
304 /* And don't forget to report the problem to the the sdl list too! */
308 start = SDL_RWtell(src);
311 if ( SDL_RWread(src, magic, 2, 1) ) {
312 if ( (magic[0] == 0xFF) && (magic[1] == 0xD8) ) {
314 while (is_JPG == 1) {
315 if(SDL_RWread(src, magic, 1, 2) != 2) {
317 } else if( (magic[0] != 0xFF) && (in_scan == 0) ) {
319 } else if( (magic[0] != 0xFF) || (magic[1] == 0xFF) ) {
320 /* Extra padding in JPEG (legal) */
321 /* or this is data and we are scanning */
322 SDL_RWseek(src, -1, SEEK_CUR);
323 } else if(magic[1] == 0xD9) {
324 /* Got to end of good JPEG */
326 } else if( (in_scan == 1) && (magic[1] == 0x00) ) {
327 /* This is an encoded 0xFF within the data */
328 } else if( (magic[1] >= 0xD0) && (magic[1] < 0xD9) ) {
329 /* These have nothing else */
330 } else if(SDL_RWread(src, magic+2, 1, 2) != 2) {
333 /* Yes, it's big-endian */
337 start = SDL_RWtell(src);
338 size = (magic[2] << 8) + magic[3];
339 end = SDL_RWseek(src, size-2, SEEK_CUR);
340 if ( end != start + size - 2 ) is_JPG = 0;
341 if ( magic[1] == 0xDA ) {
342 /* Now comes the actual JPEG meat */
344 /* Ok, I'm convinced. It is a JPEG. */
347 /* I'm not convinced. Prove it! */
355 SDL_RWseek(src, start, SEEK_SET);
359 int IMG_isPNG(SDL_RWops *src)
367 start = SDL_RWtell(src);
369 if ( SDL_RWread(src, magic, 1, sizeof(magic)) == sizeof(magic) ) {
370 if ( magic[0] == 0x89 &&
377 SDL_RWseek(src, start, SEEK_SET);
381 int IMG_isTIF(SDL_RWops* src)
389 start = SDL_RWtell(src);
391 if ( SDL_RWread(src, magic, 1, sizeof(magic)) == sizeof(magic) ) {
392 if ( (magic[0] == 'I' &&
399 magic[3] == 0x2a) ) {
403 SDL_RWseek(src, start, SEEK_SET);
407 SDL_Surface* IMG_LoadCUR_RW(SDL_RWops *src)
409 /* FIXME: Is this a supported type? */
410 return LoadImageFromRWops(src, CFSTR("com.microsoft.cur"));
412 SDL_Surface* IMG_LoadICO_RW(SDL_RWops *src)
414 return LoadImageFromRWops(src, kUTTypeICO);
416 SDL_Surface* IMG_LoadBMP_RW(SDL_RWops *src)
418 return LoadImageFromRWops(src, kUTTypeBMP);
420 SDL_Surface* IMG_LoadGIF_RW(SDL_RWops *src)
422 return LoadImageFromRWops(src, kUTTypeGIF);
424 SDL_Surface* IMG_LoadJPG_RW(SDL_RWops *src)
426 return LoadImageFromRWops(src, kUTTypeJPEG);
428 SDL_Surface* IMG_LoadPNG_RW(SDL_RWops *src)
430 return LoadImageFromRWops(src, kUTTypePNG);
432 SDL_Surface* IMG_LoadTGA_RW(SDL_RWops *src)
434 return LoadImageFromRWops(src, CFSTR("com.truevision.tga-image"));
436 SDL_Surface* IMG_LoadTIF_RW(SDL_RWops *src)
438 return LoadImageFromRWops(src, kUTTypeTIFF);