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