IMG_UIImage.m
author Sam Lantinga <slouken@libsdl.org>
Sun, 04 Jan 2009 02:09:27 +0000
changeset 187 3405ab551300
child 188 1318044935ad
permissions -rw-r--r--
Date: Fri, 2 Jan 2009 02:01:16 -0800
From: "E. Wing"
Subject: [SDL] Submission: ImageIO backend for SDL_Image on Mac OS X

I have a submission for SDL_image. I have implemented an ImageIO
backend for Mac OS X.

I've uploaded the code here:
http://www.assembla.com/spaces/SDL_Image_ImageIO

A Mercurial repository and a raw tar-ball can be found there.


The existing SDL_image implementation relies on 3rd-party libjpeg,
libpng, libgif, and libtiff which is a pain to build and maintain. In
the Mac release, we currently statically link all these libraries into
the framework. But Mac OS X already comes with these libraries, but
they are hidden away underneath their ImageIO framework. This
submission uses ImageIO to load these image formats.

To further minimize duplicated code, I also moved the bmp and tga
loader to ImageIO. Incidentally, Apple seems to have an implementation
that can detect if a file is a TGA.


ImageIO was introduced in Tiger 10.4. So this submission is intended
to go with the SDL_image that will accompany the SDL 1.3 release.

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