IMG_ImageIO.m
changeset 279 e2fa9ff13b5d
parent 270 98f97f83e203
child 283 ec183abd5996
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/IMG_ImageIO.m	Fri Dec 30 16:14:00 2011 -0500
     1.3 @@ -0,0 +1,718 @@
     1.4 +/*
     1.5 + *  IMG_ImageIO.c
     1.6 + *  SDL_image
     1.7 + *
     1.8 + *  Created by Eric Wing on 1/1/09.
     1.9 + *  Copyright 2009 __MyCompanyName__. All rights reserved.
    1.10 + *
    1.11 + */
    1.12 +
    1.13 +#if defined(__APPLE__) && !defined(SDL_IMAGE_USE_COMMON_BACKEND)
    1.14 +
    1.15 +#include "SDL_image.h"
    1.16 +
    1.17 +// Used because CGDataProviderCreate became deprecated in 10.5
    1.18 +#include <AvailabilityMacros.h>
    1.19 +#include <TargetConditionals.h>
    1.20 +#include <Foundation/Foundation.h>
    1.21 +
    1.22 +#if (TARGET_OS_IPHONE == 1) || (TARGET_IPHONE_SIMULATOR == 1)
    1.23 +#ifdef ALLOW_UIIMAGE_FALLBACK
    1.24 +#define USE_UIIMAGE_BACKEND() ([UIImage instancesRespondToSelector:@selector(initWithCGImage:scale:orientation:)] == NO)
    1.25 +#else
    1.26 +#define USE_UIIMAGE_BACKEND() (Internal_checkImageIOisAvailable())
    1.27 +#endif
    1.28 +#import <MobileCoreServices/MobileCoreServices.h> // for UTCoreTypes.h
    1.29 +#import <ImageIO/ImageIO.h>
    1.30 +#import <UIKit/UIImage.h>
    1.31 +#else
    1.32 +// For ImageIO framework and also LaunchServices framework (for UTIs)
    1.33 +#include <ApplicationServices/ApplicationServices.h>
    1.34 +#endif
    1.35 +
    1.36 +/**************************************************************
    1.37 + ***** Begin Callback functions for block reading *************
    1.38 + **************************************************************/
    1.39 +
    1.40 +// This callback reads some bytes from an SDL_rwops and copies it
    1.41 +// to a Quartz buffer (supplied by Apple framework).
    1.42 +static size_t MyProviderGetBytesCallback(void* rwops_userdata, void* quartz_buffer, size_t the_count)
    1.43 +{
    1.44 +    return (size_t)SDL_RWread((struct SDL_RWops *)rwops_userdata, quartz_buffer, 1, the_count);
    1.45 +}
    1.46 +
    1.47 +// This callback is triggered when the data provider is released
    1.48 +// so you can clean up any resources.
    1.49 +static void MyProviderReleaseInfoCallback(void* rwops_userdata)
    1.50 +{
    1.51 +    // What should I put here? 
    1.52 +    // I think the user and SDL_RWops controls closing, so I don't do anything.
    1.53 +}
    1.54 +
    1.55 +static void MyProviderRewindCallback(void* rwops_userdata)
    1.56 +{
    1.57 +    SDL_RWseek((struct SDL_RWops *)rwops_userdata, 0, RW_SEEK_SET);
    1.58 +}
    1.59 +
    1.60 +#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1050 // CGDataProviderCreateSequential was introduced in 10.5; CGDataProviderCreate is deprecated
    1.61 +off_t MyProviderSkipForwardBytesCallback(void* rwops_userdata, off_t the_count)
    1.62 +{
    1.63 +    off_t start_position = SDL_RWtell((struct SDL_RWops *)rwops_userdata);
    1.64 +    SDL_RWseek((struct SDL_RWops *)rwops_userdata, the_count, RW_SEEK_CUR);
    1.65 +    off_t end_position = SDL_RWtell((struct SDL_RWops *)rwops_userdata);
    1.66 +    return (end_position - start_position);	
    1.67 +}
    1.68 +#else // CGDataProviderCreate was deprecated in 10.5
    1.69 +static void MyProviderSkipBytesCallback(void* rwops_userdata, size_t the_count)
    1.70 +{
    1.71 +    SDL_RWseek((struct SDL_RWops *)rwops_userdata, the_count, RW_SEEK_CUR);
    1.72 +}
    1.73 +#endif
    1.74 +
    1.75 +/**************************************************************
    1.76 + ***** End Callback functions for block reading ***************
    1.77 + **************************************************************/
    1.78 +
    1.79 +// This creates a CGImageSourceRef which is a handle to an image that can be used to examine information
    1.80 +// about the image or load the actual image data.
    1.81 +static CGImageSourceRef CreateCGImageSourceFromRWops(SDL_RWops* rw_ops, CFDictionaryRef hints_and_options)
    1.82 +{
    1.83 +    CGImageSourceRef source_ref;
    1.84 +    
    1.85 +    // Similar to SDL_RWops, Apple has their own callbacks for dealing with data streams.
    1.86 +    
    1.87 +#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1050 // CGDataProviderCreateSequential was introduced in 10.5; CGDataProviderCreate is deprecated
    1.88 +    CGDataProviderSequentialCallbacks provider_callbacks =
    1.89 +    {
    1.90 +        0,
    1.91 +        MyProviderGetBytesCallback,
    1.92 +        MyProviderSkipForwardBytesCallback,
    1.93 +        MyProviderRewindCallback,
    1.94 +        MyProviderReleaseInfoCallback
    1.95 +    };
    1.96 +    
    1.97 +    CGDataProviderRef data_provider = CGDataProviderCreateSequential(rw_ops, &provider_callbacks);
    1.98 +    
    1.99 +    
   1.100 +#else // CGDataProviderCreate was deprecated in 10.5
   1.101 +    
   1.102 +    CGDataProviderCallbacks provider_callbacks =
   1.103 +    {
   1.104 +        MyProviderGetBytesCallback,
   1.105 +        MyProviderSkipBytesCallback,
   1.106 +        MyProviderRewindCallback,
   1.107 +        MyProviderReleaseInfoCallback
   1.108 +    };
   1.109 +    
   1.110 +    CGDataProviderRef data_provider = CGDataProviderCreate(rw_ops, &provider_callbacks);
   1.111 +#endif
   1.112 +    // Get the CGImageSourceRef.
   1.113 +    // The dictionary can be NULL or contain hints to help ImageIO figure out the image type.
   1.114 +    source_ref = CGImageSourceCreateWithDataProvider(data_provider, hints_and_options);
   1.115 +    return source_ref;
   1.116 +}
   1.117 +
   1.118 +/* Create a CGImageSourceRef from a file. */
   1.119 +/* Remember to CFRelease the created source when done. */
   1.120 +static CGImageSourceRef CreateCGImageSourceFromFile(const char* the_path)
   1.121 +{
   1.122 +    CFURLRef the_url = NULL;
   1.123 +    CGImageSourceRef source_ref = NULL;
   1.124 +    CFStringRef cf_string = NULL;
   1.125 +    
   1.126 +    /* Create a CFString from a C string */
   1.127 +    cf_string = CFStringCreateWithCString(NULL, the_path, kCFStringEncodingUTF8);
   1.128 +    if (!cf_string) {
   1.129 +        return NULL;
   1.130 +    }
   1.131 +    
   1.132 +    /* Create a CFURL from a CFString */
   1.133 +    the_url = CFURLCreateWithFileSystemPath(NULL, cf_string, kCFURLPOSIXPathStyle, false);
   1.134 +    
   1.135 +    /* Don't need the CFString any more (error or not) */
   1.136 +    CFRelease(cf_string);
   1.137 +    
   1.138 +    if(!the_url)
   1.139 +    {
   1.140 +        return NULL;
   1.141 +    }
   1.142 +    
   1.143 +    
   1.144 +    source_ref = CGImageSourceCreateWithURL(the_url, NULL);
   1.145 +    /* Don't need the URL any more (error or not) */
   1.146 +    CFRelease(the_url);
   1.147 +    
   1.148 +    return source_ref;
   1.149 +}
   1.150 +
   1.151 +static CGImageRef CreateCGImageFromCGImageSource(CGImageSourceRef image_source)
   1.152 +{
   1.153 +    CGImageRef image_ref = NULL;
   1.154 +    
   1.155 +    if(NULL == image_source)
   1.156 +    {
   1.157 +        return NULL;
   1.158 +    }
   1.159 +    
   1.160 +    // Get the first item in the image source (some image formats may
   1.161 +    // contain multiple items).
   1.162 +    image_ref = CGImageSourceCreateImageAtIndex(image_source, 0, NULL);
   1.163 +    if(NULL == image_ref)
   1.164 +    {
   1.165 +        IMG_SetError("CGImageSourceCreateImageAtIndex() failed");
   1.166 +    }
   1.167 +    return image_ref;
   1.168 +}
   1.169 +
   1.170 +static CFDictionaryRef CreateHintDictionary(CFStringRef uti_string_hint)
   1.171 +{
   1.172 +    CFDictionaryRef hint_dictionary = NULL;
   1.173 +    
   1.174 +    if(uti_string_hint != NULL)
   1.175 +    {
   1.176 +        // Do a bunch of work to setup a CFDictionary containing the jpeg compression properties.
   1.177 +        CFStringRef the_keys[1];
   1.178 +        CFStringRef the_values[1];
   1.179 +        
   1.180 +        the_keys[0] = kCGImageSourceTypeIdentifierHint;
   1.181 +        the_values[0] = uti_string_hint;
   1.182 +        
   1.183 +        // kCFTypeDictionaryKeyCallBacks or kCFCopyStringDictionaryKeyCallBacks?
   1.184 +        hint_dictionary = CFDictionaryCreate(NULL, (const void**)&the_keys, (const void**)&the_values, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
   1.185 +    }
   1.186 +    return hint_dictionary;
   1.187 +}
   1.188 +
   1.189 +
   1.190 +// Once we have our image, we need to get it into an SDL_Surface
   1.191 +static SDL_Surface* Create_SDL_Surface_From_CGImage(CGImageRef image_ref)
   1.192 +{
   1.193 +    /* This code is adapted from Apple's Documentation found here:
   1.194 +     * http://developer.apple.com/documentation/GraphicsImaging/Conceptual/OpenGL-MacProgGuide/index.html
   1.195 +     * Listing 9-4††Using a Quartz image as a texture source.
   1.196 +     * Unfortunately, this guide doesn't show what to do about
   1.197 +     * non-RGBA image formats so I'm making the rest up.
   1.198 +     * All this code should be scrutinized.
   1.199 +     */
   1.200 +    
   1.201 +    size_t w = CGImageGetWidth(image_ref);
   1.202 +    size_t h = CGImageGetHeight(image_ref);
   1.203 +    CGRect rect = {{0, 0}, {w, h}};
   1.204 +    
   1.205 +    CGImageAlphaInfo alpha = CGImageGetAlphaInfo(image_ref);
   1.206 +    //size_t bits_per_pixel = CGImageGetBitsPerPixel(image_ref);
   1.207 +    size_t bits_per_component = 8;
   1.208 +    
   1.209 +    SDL_Surface* surface;
   1.210 +    Uint32 Amask;
   1.211 +    Uint32 Rmask;
   1.212 +    Uint32 Gmask;
   1.213 +    Uint32 Bmask;
   1.214 +    
   1.215 +    CGContextRef bitmap_context;
   1.216 +    CGBitmapInfo bitmap_info;
   1.217 +    CGColorSpaceRef color_space = CGColorSpaceCreateDeviceRGB();
   1.218 +    
   1.219 +    if (alpha == kCGImageAlphaNone ||
   1.220 +        alpha == kCGImageAlphaNoneSkipFirst ||
   1.221 +        alpha == kCGImageAlphaNoneSkipLast) {
   1.222 +        bitmap_info = kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Host; /* XRGB */
   1.223 +        Amask = 0x00000000;
   1.224 +    } else {
   1.225 +        /* kCGImageAlphaFirst isn't supported */
   1.226 +        //bitmap_info = kCGImageAlphaFirst | kCGBitmapByteOrder32Host; /* ARGB */
   1.227 +        bitmap_info = kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host; /* ARGB */
   1.228 +        Amask = 0xFF000000;
   1.229 +    }
   1.230 +    
   1.231 +    Rmask = 0x00FF0000;
   1.232 +    Gmask = 0x0000FF00;
   1.233 +    Bmask = 0x000000FF;
   1.234 +    
   1.235 +    surface = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 32, Rmask, Gmask, Bmask, Amask);
   1.236 +    if (surface)
   1.237 +    {
   1.238 +        // Sets up a context to be drawn to with surface->pixels as the area to be drawn to
   1.239 +        bitmap_context = CGBitmapContextCreate(
   1.240 +                                               surface->pixels,
   1.241 +                                               surface->w,
   1.242 +                                               surface->h,
   1.243 +                                               bits_per_component,
   1.244 +                                               surface->pitch,
   1.245 +                                               color_space,
   1.246 +                                               bitmap_info
   1.247 +                                               );
   1.248 +        
   1.249 +        // Draws the image into the context's image_data
   1.250 +        CGContextDrawImage(bitmap_context, rect, image_ref);
   1.251 +        
   1.252 +        CGContextRelease(bitmap_context);
   1.253 +        
   1.254 +        // FIXME: Reverse the premultiplied alpha
   1.255 +        if ((bitmap_info & kCGBitmapAlphaInfoMask) == kCGImageAlphaPremultipliedFirst) {
   1.256 +            int i, j;
   1.257 +            Uint8 *p = (Uint8 *)surface->pixels;
   1.258 +            for (i = surface->h * surface->pitch/4; i--; ) {
   1.259 +#if __LITTLE_ENDIAN__
   1.260 +                Uint8 A = p[3];
   1.261 +                if (A) {
   1.262 +                    for (j = 0; j < 3; ++j) {
   1.263 +                        p[j] = (p[j] * 255) / A;
   1.264 +                    }
   1.265 +                }
   1.266 +#else
   1.267 +                Uint8 A = p[0];
   1.268 +                if (A) {
   1.269 +                    for (j = 1; j < 4; ++j) {
   1.270 +                        p[j] = (p[j] * 255) / A;
   1.271 +                    }
   1.272 +                }
   1.273 +#endif /* ENDIAN */
   1.274 +                p += 4;
   1.275 +            }
   1.276 +        }
   1.277 +    }
   1.278 +    
   1.279 +    if (color_space)
   1.280 +    {
   1.281 +        CGColorSpaceRelease(color_space);			
   1.282 +    }
   1.283 +    
   1.284 +    return surface;
   1.285 +}
   1.286 +
   1.287 +
   1.288 +#pragma mark -
   1.289 +#pragma mark IMG_Init stubs
   1.290 +#if !defined(ALLOW_UIIMAGE_FALLBACK) && ((TARGET_OS_IPHONE == 1) || (TARGET_IPHONE_SIMULATOR == 1))
   1.291 +static int Internal_checkImageIOisAvailable() {
   1.292 +    // just check if we are running on ios 4 or more, else throw exception
   1.293 +    if ([UIImage instancesRespondToSelector:@selector(initWithCGImage:scale:orientation:)])
   1.294 +        return 0;
   1.295 +    [NSException raise:@"UIImage fallback not enabled at compile time"
   1.296 +                format:@"ImageIO is not available on your platform, please recompile SDL_Image with ALLOW_UIIMAGE_FALLBACK."];
   1.297 +    return -1;
   1.298 +}
   1.299 +#endif
   1.300 +
   1.301 +int IMG_InitJPG()
   1.302 +{
   1.303 +    return 0;
   1.304 +}
   1.305 +
   1.306 +void IMG_QuitJPG()
   1.307 +{
   1.308 +}
   1.309 +
   1.310 +int IMG_InitPNG()
   1.311 +{
   1.312 +    return 0;
   1.313 +}
   1.314 +
   1.315 +void IMG_QuitPNG()
   1.316 +{
   1.317 +}
   1.318 +
   1.319 +int IMG_InitTIF()
   1.320 +{
   1.321 +    return 0;
   1.322 +}
   1.323 +
   1.324 +void IMG_QuitTIF()
   1.325 +{
   1.326 +}
   1.327 +
   1.328 +#pragma mark -
   1.329 +#pragma mark Get type of image
   1.330 +static int Internal_isType_UIImage (SDL_RWops *rw_ops, CFStringRef uti_string_to_test)
   1.331 +{
   1.332 +    int is_type = 0;
   1.333 +    
   1.334 +#if defined(ALLOW_UIIMAGE_FALLBACK) && ((TARGET_OS_IPHONE == 1) || (TARGET_IPHONE_SIMULATOR == 1))
   1.335 +    int start = SDL_RWtell(rw_ops);
   1.336 +    if ((0 == CFStringCompare(uti_string_to_test, kUTTypeICO, 0)) ||
   1.337 +        (0 == CFStringCompare(uti_string_to_test, CFSTR("com.microsoft.cur"), 0))) {
   1.338 +        
   1.339 +        // The Win32 ICO file header (14 bytes)
   1.340 +        Uint16 bfReserved;
   1.341 +        Uint16 bfType;
   1.342 +        Uint16 bfCount;
   1.343 +        int type = (0 == CFStringCompare(uti_string_to_test, kUTTypeICO, 0)) ? 1 : 2;
   1.344 +        
   1.345 +        bfReserved = SDL_ReadLE16(rw_ops);
   1.346 +        bfType = SDL_ReadLE16(rw_ops);
   1.347 +        bfCount = SDL_ReadLE16(rw_ops);
   1.348 +        if ((bfReserved == 0) && (bfType == type) && (bfCount != 0)) 
   1.349 +            is_type = 1;
   1.350 +    } else if (0 == CFStringCompare(uti_string_to_test, kUTTypeBMP, 0)) {
   1.351 +        char magic[2];
   1.352 +        
   1.353 +        if ( SDL_RWread(rw_ops, magic, sizeof(magic), 1) ) {
   1.354 +            if ( strncmp(magic, "BM", 2) == 0 ) {
   1.355 +                is_type = 1;
   1.356 +            }
   1.357 +        }
   1.358 +    } else if (0 == CFStringCompare(uti_string_to_test, kUTTypeGIF, 0)) {
   1.359 +        char magic[6];
   1.360 +        
   1.361 +        if ( SDL_RWread(rw_ops, magic, sizeof(magic), 1) ) {
   1.362 +            if ( (strncmp(magic, "GIF", 3) == 0) &&
   1.363 +                ((memcmp(magic + 3, "87a", 3) == 0) ||
   1.364 +                 (memcmp(magic + 3, "89a", 3) == 0)) ) {
   1.365 +                    is_type = 1;
   1.366 +                }
   1.367 +        }
   1.368 +    } else if (0 == CFStringCompare(uti_string_to_test, kUTTypeJPEG, 0)) {
   1.369 +        int in_scan = 0;
   1.370 +        Uint8 magic[4];
   1.371 +        
   1.372 +        // This detection code is by Steaphan Greene <stea@cs.binghamton.edu>
   1.373 +        // Blame me, not Sam, if this doesn't work right. */
   1.374 +        // And don't forget to report the problem to the the sdl list too! */
   1.375 +        
   1.376 +        if ( SDL_RWread(rw_ops, magic, 2, 1) ) {
   1.377 +            if ( (magic[0] == 0xFF) && (magic[1] == 0xD8) ) {
   1.378 +                is_type = 1;
   1.379 +                while (is_type == 1) {
   1.380 +                    if(SDL_RWread(rw_ops, magic, 1, 2) != 2) {
   1.381 +                        is_type = 0;
   1.382 +                    } else if( (magic[0] != 0xFF) && (in_scan == 0) ) {
   1.383 +                        is_type = 0;
   1.384 +                    } else if( (magic[0] != 0xFF) || (magic[1] == 0xFF) ) {
   1.385 +                        /* Extra padding in JPEG (legal) */
   1.386 +                        /* or this is data and we are scanning */
   1.387 +                        SDL_RWseek(rw_ops, -1, SEEK_CUR);
   1.388 +                    } else if(magic[1] == 0xD9) {
   1.389 +                        /* Got to end of good JPEG */
   1.390 +                        break;
   1.391 +                    } else if( (in_scan == 1) && (magic[1] == 0x00) ) {
   1.392 +                        /* This is an encoded 0xFF within the data */
   1.393 +                    } else if( (magic[1] >= 0xD0) && (magic[1] < 0xD9) ) {
   1.394 +                        /* These have nothing else */
   1.395 +                    } else if(SDL_RWread(rw_ops, magic+2, 1, 2) != 2) {
   1.396 +                        is_type = 0;
   1.397 +                    } else {
   1.398 +                        /* Yes, it's big-endian */
   1.399 +                        Uint32 start;
   1.400 +                        Uint32 size;
   1.401 +                        Uint32 end;
   1.402 +                        start = SDL_RWtell(rw_ops);
   1.403 +                        size = (magic[2] << 8) + magic[3];
   1.404 +                        end = SDL_RWseek(rw_ops, size-2, SEEK_CUR);
   1.405 +                        if ( end != start + size - 2 ) is_type = 0;
   1.406 +                        if ( magic[1] == 0xDA ) {
   1.407 +                            /* Now comes the actual JPEG meat */
   1.408 +#ifdef	FAST_IS_JPEG
   1.409 +                            /* Ok, I'm convinced.  It is a JPEG. */
   1.410 +                            break;
   1.411 +#else
   1.412 +                            /* I'm not convinced.  Prove it! */
   1.413 +                            in_scan = 1;
   1.414 +#endif
   1.415 +                        }
   1.416 +                    }
   1.417 +                }
   1.418 +            }
   1.419 +        }
   1.420 +    } else if (0 == CFStringCompare(uti_string_to_test, kUTTypePNG, 0)) {
   1.421 +        Uint8 magic[4];
   1.422 +        
   1.423 +        if ( SDL_RWread(rw_ops, magic, 1, sizeof(magic)) == sizeof(magic) ) {
   1.424 +            if ( magic[0] == 0x89 &&
   1.425 +                magic[1] == 'P' &&
   1.426 +                magic[2] == 'N' &&
   1.427 +                magic[3] == 'G' ) {
   1.428 +                is_type = 1;
   1.429 +            }
   1.430 +        }
   1.431 +    } else if (0 == CFStringCompare(uti_string_to_test, CFSTR("com.truevision.tga-image"), 0)) {
   1.432 +        //TODO: fill me!
   1.433 +    } else if (0 == CFStringCompare(uti_string_to_test, kUTTypeTIFF, 0)) {
   1.434 +        Uint8 magic[4];
   1.435 +        
   1.436 +        if ( SDL_RWread(rw_ops, magic, 1, sizeof(magic)) == sizeof(magic) ) {
   1.437 +            if ( (magic[0] == 'I' &&
   1.438 +                  magic[1] == 'I' &&
   1.439 +                  magic[2] == 0x2a &&
   1.440 +                  magic[3] == 0x00) ||
   1.441 +                (magic[0] == 'M' &&
   1.442 +                 magic[1] == 'M' &&
   1.443 +                 magic[2] == 0x00 &&
   1.444 +                 magic[3] == 0x2a) ) {
   1.445 +                    is_type = 1;
   1.446 +                }
   1.447 +        }
   1.448 +    }
   1.449 +    
   1.450 +    // reset the file descption pointer
   1.451 +    SDL_RWseek(rw_ops, start, SEEK_SET);
   1.452 +
   1.453 +#endif  /* #if defined(ALLOW_UIIMAGE_FALLBACK) && ((TARGET_OS_IPHONE == 1) || (TARGET_IPHONE_SIMULATOR == 1)) */
   1.454 +    return is_type;
   1.455 +}
   1.456 +
   1.457 +static int Internal_isType_ImageIO (SDL_RWops *rw_ops, CFStringRef uti_string_to_test)
   1.458 +{
   1.459 +    int is_type = 0;
   1.460 +    
   1.461 +    CFDictionaryRef hint_dictionary = CreateHintDictionary(uti_string_to_test);	
   1.462 +    CGImageSourceRef image_source = CreateCGImageSourceFromRWops(rw_ops, hint_dictionary);
   1.463 +    
   1.464 +    if (hint_dictionary != NULL) {
   1.465 +        CFRelease(hint_dictionary);		
   1.466 +    }
   1.467 +    
   1.468 +    if (NULL == image_source) {
   1.469 +        return 0;
   1.470 +    }
   1.471 +    
   1.472 +    // This will get the UTI of the container, not the image itself.
   1.473 +    // Under most cases, this won't be a problem.
   1.474 +    // But if a person passes an icon file which contains a bmp,
   1.475 +    // the format will be of the icon file.
   1.476 +    // But I think the main SDL_image codebase has this same problem so I'm not going to worry about it.	
   1.477 +    CFStringRef uti_type = CGImageSourceGetType(image_source);
   1.478 +    //	CFShow(uti_type);
   1.479 +    
   1.480 +    // Unsure if we really want conformance or equality
   1.481 +    is_type = (int)UTTypeConformsTo(uti_string_to_test, uti_type);
   1.482 +    
   1.483 +    CFRelease(image_source);
   1.484 +    return is_type;
   1.485 +}
   1.486 +
   1.487 +static int Internal_isType (SDL_RWops *rw_ops, CFStringRef uti_string_to_test)
   1.488 +{
   1.489 +    if (rw_ops == NULL)
   1.490 +        return 0;
   1.491 +    
   1.492 +#if (TARGET_OS_IPHONE == 1) || (TARGET_IPHONE_SIMULATOR == 1)
   1.493 +    if (USE_UIIMAGE_BACKEND())
   1.494 +        return Internal_isType_UIImage(rw_ops, uti_string_to_test);
   1.495 +    else
   1.496 +#endif
   1.497 +        return Internal_isType_ImageIO(rw_ops, uti_string_to_test);
   1.498 +}
   1.499 +
   1.500 +int IMG_isCUR(SDL_RWops *src)
   1.501 +{
   1.502 +    /* FIXME: Is this a supported type? */
   1.503 +    return Internal_isType(src, CFSTR("com.microsoft.cur"));
   1.504 +}
   1.505 +
   1.506 +int IMG_isICO(SDL_RWops *src)
   1.507 +{
   1.508 +    return Internal_isType(src, kUTTypeICO);
   1.509 +}
   1.510 +
   1.511 +int IMG_isBMP(SDL_RWops *src)
   1.512 +{
   1.513 +    return Internal_isType(src, kUTTypeBMP);
   1.514 +}
   1.515 +
   1.516 +int IMG_isGIF(SDL_RWops *src)
   1.517 +{
   1.518 +    return Internal_isType(src, kUTTypeGIF);
   1.519 +}
   1.520 +
   1.521 +// Note: JPEG 2000 is kUTTypeJPEG2000
   1.522 +int IMG_isJPG(SDL_RWops *src)
   1.523 +{
   1.524 +    return Internal_isType(src, kUTTypeJPEG);
   1.525 +}
   1.526 +
   1.527 +int IMG_isPNG(SDL_RWops *src)
   1.528 +{
   1.529 +    return Internal_isType(src, kUTTypePNG);
   1.530 +}
   1.531 +
   1.532 +// This isn't a public API function. Apple seems to be able to identify tga's.
   1.533 +int IMG_isTGA(SDL_RWops *src)
   1.534 +{
   1.535 +    return Internal_isType(src, CFSTR("com.truevision.tga-image"));
   1.536 +}
   1.537 +
   1.538 +int IMG_isTIF(SDL_RWops *src)
   1.539 +{
   1.540 +    return Internal_isType(src, kUTTypeTIFF);
   1.541 +}
   1.542 +
   1.543 +#pragma mark -
   1.544 +#pragma mark Load image engine
   1.545 +static SDL_Surface *LoadImageFromRWops_UIImage (SDL_RWops* rw_ops, CFStringRef uti_string_hint)
   1.546 +{
   1.547 +    SDL_Surface *sdl_surface = NULL;
   1.548 +
   1.549 +#if defined(ALLOW_UIIMAGE_FALLBACK) && ((TARGET_OS_IPHONE == 1) || (TARGET_IPHONE_SIMULATOR == 1))
   1.550 +    NSAutoreleasePool* autorelease_pool = [[NSAutoreleasePool alloc] init];
   1.551 +    UIImage *ui_image;
   1.552 +    int bytes_read = 0;
   1.553 +    // I don't know what a good size is. 
   1.554 +    // Max recommended texture size is 1024x1024 on iPhone so maybe base it on that?
   1.555 +    const int block_size = 1024*4;
   1.556 +    char temp_buffer[block_size];
   1.557 +    
   1.558 +    NSMutableData* ns_data = [[NSMutableData alloc] initWithCapacity:1024*1024*4];
   1.559 +    do {
   1.560 +        bytes_read = SDL_RWread(rw_ops, temp_buffer, 1, block_size);
   1.561 +        [ns_data appendBytes:temp_buffer length:bytes_read];
   1.562 +    } while (bytes_read > 0);
   1.563 +    
   1.564 +    ui_image = [[UIImage alloc] initWithData:ns_data];
   1.565 +    if (ui_image != nil)
   1.566 +        sdl_surface = Create_SDL_Surface_From_CGImage([ui_image CGImage]);
   1.567 +    [ui_image release];
   1.568 +    [ns_data release];          
   1.569 +    [autorelease_pool drain];
   1.570 +
   1.571 +#endif  /* #if defined(ALLOW_UIIMAGE_FALLBACK) && ((TARGET_OS_IPHONE == 1) || (TARGET_IPHONE_SIMULATOR == 1)) */
   1.572 +    return sdl_surface;
   1.573 +}
   1.574 +
   1.575 +static SDL_Surface *LoadImageFromRWops_ImageIO (SDL_RWops *rw_ops, CFStringRef uti_string_hint)
   1.576 +{
   1.577 +    CFDictionaryRef hint_dictionary = CreateHintDictionary(uti_string_hint);
   1.578 +    CGImageSourceRef image_source = CreateCGImageSourceFromRWops(rw_ops, hint_dictionary);
   1.579 +
   1.580 +    if (hint_dictionary != NULL)
   1.581 +        CFRelease(hint_dictionary);		
   1.582 +
   1.583 +    if (NULL == image_source)
   1.584 +        return NULL;
   1.585 +    
   1.586 +    CGImageRef image_ref = CreateCGImageFromCGImageSource(image_source);
   1.587 +    CFRelease(image_source);
   1.588 +
   1.589 +    if (NULL == image_ref)
   1.590 +        return NULL;
   1.591 +    SDL_Surface *sdl_surface = Create_SDL_Surface_From_CGImage(image_ref);
   1.592 +    CFRelease(image_ref);
   1.593 +
   1.594 +    return sdl_surface;
   1.595 +}
   1.596 +
   1.597 +static SDL_Surface *LoadImageFromRWops (SDL_RWops *rw_ops, CFStringRef uti_string_hint)
   1.598 +{
   1.599 +#if (TARGET_OS_IPHONE == 1) || (TARGET_IPHONE_SIMULATOR == 1)
   1.600 +    if (USE_UIIMAGE_BACKEND()) 
   1.601 +        return LoadImageFromRWops_UIImage(rw_ops, uti_string_hint);
   1.602 +    else
   1.603 +#endif
   1.604 +        return LoadImageFromRWops_ImageIO(rw_ops, uti_string_hint);
   1.605 +}
   1.606 +
   1.607 +static SDL_Surface* LoadImageFromFile_UIImage (const char *file)
   1.608 +{
   1.609 +    SDL_Surface *sdl_surface = NULL;
   1.610 +
   1.611 +#if defined(ALLOW_UIIMAGE_FALLBACK) && ((TARGET_OS_IPHONE == 1) || (TARGET_IPHONE_SIMULATOR == 1))
   1.612 +    NSAutoreleasePool* autorelease_pool = [[NSAutoreleasePool alloc] init];
   1.613 +    NSString *ns_string = [[NSString alloc] initWithUTF8String:file];
   1.614 +    UIImage *ui_image = [[UIImage alloc] initWithContentsOfFile:ns_string];
   1.615 +    if (ui_image != nil)
   1.616 +        sdl_surface = Create_SDL_Surface_From_CGImage([ui_image CGImage]);
   1.617 +    [ui_image release];
   1.618 +    [ns_string release];
   1.619 +    [autorelease_pool drain];
   1.620 +
   1.621 +#endif  /* #if defined(ALLOW_UIIMAGE_FALLBACK) && ((TARGET_OS_IPHONE == 1) || (TARGET_IPHONE_SIMULATOR == 1)) */
   1.622 +    return sdl_surface;	
   1.623 +}
   1.624 +
   1.625 +static SDL_Surface* LoadImageFromFile_ImageIO (const char *file)
   1.626 +{
   1.627 +    CGImageSourceRef image_source = NULL;
   1.628 +
   1.629 +    image_source = CreateCGImageSourceFromFile(file);
   1.630 +
   1.631 +    if(NULL == image_source)
   1.632 +        return NULL;
   1.633 +    
   1.634 +    CGImageRef image_ref = CreateCGImageFromCGImageSource(image_source);
   1.635 +    CFRelease(image_source);
   1.636 +
   1.637 +    if (NULL == image_ref)
   1.638 +        return NULL;
   1.639 +    SDL_Surface *sdl_surface = Create_SDL_Surface_From_CGImage(image_ref);
   1.640 +    CFRelease(image_ref);
   1.641 +    return sdl_surface;	
   1.642 +}
   1.643 +
   1.644 +static SDL_Surface* LoadImageFromFile (const char *file)
   1.645 +{
   1.646 +#if (TARGET_OS_IPHONE == 1) || (TARGET_IPHONE_SIMULATOR == 1)
   1.647 +    if (USE_UIIMAGE_BACKEND())
   1.648 +        return LoadImageFromFile_UIImage(file);
   1.649 +    else
   1.650 +#endif
   1.651 +        return LoadImageFromFile_ImageIO(file);
   1.652 +}
   1.653 +
   1.654 +SDL_Surface* IMG_LoadCUR_RW (SDL_RWops *src)
   1.655 +{
   1.656 +    /* FIXME: Is this a supported type? */
   1.657 +    return LoadImageFromRWops(src, CFSTR("com.microsoft.cur"));
   1.658 +}
   1.659 +
   1.660 +SDL_Surface* IMG_LoadICO_RW (SDL_RWops *src)
   1.661 +{
   1.662 +    return LoadImageFromRWops(src, kUTTypeICO);
   1.663 +}
   1.664 +
   1.665 +SDL_Surface* IMG_LoadBMP_RW (SDL_RWops *src)
   1.666 +{
   1.667 +    return LoadImageFromRWops(src, kUTTypeBMP);
   1.668 +}
   1.669 +
   1.670 +SDL_Surface* IMG_LoadGIF_RW (SDL_RWops *src)
   1.671 +{
   1.672 +    return LoadImageFromRWops (src, kUTTypeGIF);
   1.673 +}
   1.674 +
   1.675 +SDL_Surface* IMG_LoadJPG_RW (SDL_RWops *src)
   1.676 +{
   1.677 +    return LoadImageFromRWops (src, kUTTypeJPEG);
   1.678 +}
   1.679 +
   1.680 +SDL_Surface* IMG_LoadPNG_RW (SDL_RWops *src)
   1.681 +{
   1.682 +    return LoadImageFromRWops (src, kUTTypePNG);
   1.683 +}
   1.684 +
   1.685 +SDL_Surface* IMG_LoadTGA_RW (SDL_RWops *src)
   1.686 +{
   1.687 +    return LoadImageFromRWops(src, CFSTR("com.truevision.tga-image"));
   1.688 +}
   1.689 +
   1.690 +SDL_Surface* IMG_LoadTIF_RW (SDL_RWops *src)
   1.691 +{
   1.692 +    return LoadImageFromRWops(src, kUTTypeTIFF);
   1.693 +}
   1.694 +
   1.695 +// Since UIImage doesn't really support streams well, we should optimize for the file case.
   1.696 +// Apple provides both stream and file loading functions in ImageIO.
   1.697 +// Potentially, Apple can optimize for either case.
   1.698 +SDL_Surface* IMG_Load (const char *file)
   1.699 +{
   1.700 +    SDL_Surface* sdl_surface = NULL;
   1.701 +    
   1.702 +    sdl_surface = LoadImageFromFile(file);
   1.703 +    if(NULL == sdl_surface)
   1.704 +    {
   1.705 +        // Either the file doesn't exist or ImageIO doesn't understand the format.
   1.706 +        // For the latter case, fallback to the native SDL_image handlers.
   1.707 +        SDL_RWops *src = SDL_RWFromFile(file, "rb");
   1.708 +        char *ext = strrchr(file, '.');
   1.709 +        if (ext) {
   1.710 +            ext++;
   1.711 +        }
   1.712 +        if (!src) {
   1.713 +            /* The error message has been set in SDL_RWFromFile */
   1.714 +            return NULL;
   1.715 +        }
   1.716 +        sdl_surface = IMG_LoadTyped_RW(src, 1, ext);
   1.717 +    }
   1.718 +    return sdl_surface;
   1.719 +}
   1.720 +
   1.721 +#endif /* defined(__APPLE__) && !defined(SDL_IMAGE_USE_COMMON_BACKEND) */