Skip to content

Latest commit

 

History

History
444 lines (371 loc) · 10.6 KB

IMG_UIImage.m

File metadata and controls

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