Skip to content

Latest commit

 

History

History
440 lines (382 loc) · 11.8 KB

IMG_UIImage.m

File metadata and controls

440 lines (382 loc) · 11.8 KB
 
1
2
3
4
5
6
7
8
9
10
/*
* 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>
Oct 8, 2009
Oct 8, 2009
11
#import <MobileCoreServices/MobileCoreServices.h> // for UTCoreTypes.h
12
13
14
15
16
// 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)
{
May 22, 2013
May 22, 2013
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
/* 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 w = CGImageGetWidth(image_ref);
size_t h = CGImageGetHeight(image_ref);
CGRect rect = {{0, 0}, {w, h}};
CGImageAlphaInfo alpha = CGImageGetAlphaInfo(image_ref);
//size_t bits_per_pixel = CGImageGetBitsPerPixel(image_ref);
size_t bits_per_component = 8;
SDL_Surface* surface;
Uint32 Amask;
Uint32 Rmask;
Uint32 Gmask;
Uint32 Bmask;
CGContextRef bitmap_context;
CGBitmapInfo bitmap_info;
CGColorSpaceRef color_space = CGColorSpaceCreateDeviceRGB();
if (alpha == kCGImageAlphaNone ||
alpha == kCGImageAlphaNoneSkipFirst ||
alpha == kCGImageAlphaNoneSkipLast) {
bitmap_info = kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Host; /* XRGB */
Amask = 0x00000000;
} else {
/* kCGImageAlphaFirst isn't supported */
//bitmap_info = kCGImageAlphaFirst | kCGBitmapByteOrder32Host; /* ARGB */
bitmap_info = kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host; /* ARGB */
Amask = 0xFF000000;
}
Rmask = 0x00FF0000;
Gmask = 0x0000FF00;
Bmask = 0x000000FF;
surface = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 32, Rmask, Gmask, Bmask, Amask);
if (surface)
{
// Sets up a context to be drawn to with surface->pixels as the area to be drawn to
bitmap_context = CGBitmapContextCreate(
surface->pixels,
surface->w,
surface->h,
bits_per_component,
surface->pitch,
color_space,
bitmap_info
);
// Draws the image into the context's image_data
CGContextDrawImage(bitmap_context, rect, image_ref);
CGContextRelease(bitmap_context);
// FIXME: Reverse the premultiplied alpha
if ((bitmap_info & kCGBitmapAlphaInfoMask) == kCGImageAlphaPremultipliedFirst) {
int i, j;
Uint8 *p = (Uint8 *)surface->pixels;
for (i = surface->h * surface->pitch/4; i--; ) {
Nov 10, 2009
Nov 10, 2009
83
#if __LITTLE_ENDIAN__
May 22, 2013
May 22, 2013
84
85
86
87
88
89
Uint8 A = p[3];
if (A) {
for (j = 0; j < 3; ++j) {
p[j] = (p[j] * 255) / A;
}
}
Nov 10, 2009
Nov 10, 2009
90
#else
May 22, 2013
May 22, 2013
91
92
93
94
95
96
Uint8 A = p[0];
if (A) {
for (j = 1; j < 4; ++j) {
p[j] = (p[j] * 255) / A;
}
}
Nov 10, 2009
Nov 10, 2009
97
#endif /* ENDIAN */
May 22, 2013
May 22, 2013
98
99
100
101
p += 4;
}
}
}
102
May 22, 2013
May 22, 2013
103
104
105
106
if (color_space)
{
CGColorSpaceRelease(color_space);
}
107
May 22, 2013
May 22, 2013
108
return surface;
109
110
111
112
}
static SDL_Surface* LoadImageFromRWops(SDL_RWops* rw_ops, CFStringRef uti_string_hint)
{
May 22, 2013
May 22, 2013
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
NSAutoreleasePool* autorelease_pool = [[NSAutoreleasePool alloc] init];
SDL_Surface* sdl_surface;
UIImage* ui_image;
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);
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;
142
143
}
Jan 4, 2009
Jan 4, 2009
144
static SDL_Surface* LoadImageFromFile(const char *file)
145
{
May 22, 2013
May 22, 2013
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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];
if(ui_image != NULL)
{
sdl_surface = Create_SDL_Surface_From_CGImage([ui_image CGImage]);
}
[ui_image release];
[ns_string release];
[autorelease_pool drain];
return sdl_surface;
Jan 4, 2009
Jan 4, 2009
164
}
165
Jan 4, 2009
Jan 4, 2009
166
167
168
169
/* Since UIImage doesn't really support streams well, we should optimize for the file case. */
SDL_Surface *IMG_Load(const char *file)
{
May 22, 2013
May 22, 2013
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
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);
}
return sdl_surface;
190
191
192
}
Nov 8, 2009
Nov 8, 2009
193
194
int IMG_InitJPG()
{
May 22, 2013
May 22, 2013
195
return 0;
Nov 8, 2009
Nov 8, 2009
196
197
198
199
200
201
202
203
}
void IMG_QuitJPG()
{
}
int IMG_InitPNG()
{
May 22, 2013
May 22, 2013
204
return 0;
Nov 8, 2009
Nov 8, 2009
205
206
207
208
209
210
211
212
}
void IMG_QuitPNG()
{
}
int IMG_InitTIF()
{
May 22, 2013
May 22, 2013
213
return 0;
Nov 8, 2009
Nov 8, 2009
214
215
216
217
218
}
void IMG_QuitTIF()
{
}
219
220
/* Copied straight from other files so I don't have to alter them. */
Aug 2, 2009
Aug 2, 2009
221
222
static int IMG_isICOCUR(SDL_RWops *src, int type)
{
May 22, 2013
May 22, 2013
223
224
int start;
int is_ICOCUR;
Aug 2, 2009
Aug 2, 2009
225
May 22, 2013
May 22, 2013
226
/* The Win32 ICO file header (14 bytes) */
Aug 2, 2009
Aug 2, 2009
227
228
229
230
Uint16 bfReserved;
Uint16 bfType;
Uint16 bfCount;
May 22, 2013
May 22, 2013
231
232
233
234
if ( !src )
return 0;
start = SDL_RWtell(src);
is_ICOCUR = 0;
Aug 2, 2009
Aug 2, 2009
235
236
237
bfReserved = SDL_ReadLE16(src);
bfType = SDL_ReadLE16(src);
bfCount = SDL_ReadLE16(src);
May 22, 2013
May 22, 2013
238
239
240
if ((bfReserved == 0) && (bfType == type) && (bfCount != 0))
is_ICOCUR = 1;
SDL_RWseek(src, start, SEEK_SET);
Aug 2, 2009
Aug 2, 2009
241
May 22, 2013
May 22, 2013
242
return (is_ICOCUR);
Aug 2, 2009
Aug 2, 2009
243
244
245
246
}
int IMG_isICO(SDL_RWops *src)
{
May 22, 2013
May 22, 2013
247
return IMG_isICOCUR(src, 1);
Aug 2, 2009
Aug 2, 2009
248
249
250
251
}
int IMG_isCUR(SDL_RWops *src)
{
May 22, 2013
May 22, 2013
252
return IMG_isICOCUR(src, 2);
Aug 2, 2009
Aug 2, 2009
253
254
}
255
256
int IMG_isBMP(SDL_RWops *src)
{
May 22, 2013
May 22, 2013
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
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);
272
273
274
275
}
int IMG_isGIF(SDL_RWops *src)
{
May 22, 2013
May 22, 2013
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
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);
293
294
295
296
}
int IMG_isJPG(SDL_RWops *src)
{
May 22, 2013
May 22, 2013
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
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;
346
#else
May 22, 2013
May 22, 2013
347
348
/* I'm not convinced. Prove it! */
in_scan = 1;
349
#endif
May 22, 2013
May 22, 2013
350
351
352
353
354
355
356
}
}
}
}
}
SDL_RWseek(src, start, SEEK_SET);
return(is_JPG);
357
358
359
360
}
int IMG_isPNG(SDL_RWops *src)
{
May 22, 2013
May 22, 2013
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
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);
379
380
381
382
}
int IMG_isTIF(SDL_RWops* src)
{
May 22, 2013
May 22, 2013
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
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);
405
406
}
Aug 2, 2009
Aug 2, 2009
407
408
SDL_Surface* IMG_LoadCUR_RW(SDL_RWops *src)
{
May 22, 2013
May 22, 2013
409
410
/* FIXME: Is this a supported type? */
return LoadImageFromRWops(src, CFSTR("com.microsoft.cur"));
Aug 2, 2009
Aug 2, 2009
411
412
413
}
SDL_Surface* IMG_LoadICO_RW(SDL_RWops *src)
{
May 22, 2013
May 22, 2013
414
return LoadImageFromRWops(src, kUTTypeICO);
Aug 2, 2009
Aug 2, 2009
415
}
416
417
SDL_Surface* IMG_LoadBMP_RW(SDL_RWops *src)
{
May 22, 2013
May 22, 2013
418
return LoadImageFromRWops(src, kUTTypeBMP);
419
420
421
}
SDL_Surface* IMG_LoadGIF_RW(SDL_RWops *src)
{
May 22, 2013
May 22, 2013
422
return LoadImageFromRWops(src, kUTTypeGIF);
423
424
425
}
SDL_Surface* IMG_LoadJPG_RW(SDL_RWops *src)
{
May 22, 2013
May 22, 2013
426
return LoadImageFromRWops(src, kUTTypeJPEG);
427
428
429
}
SDL_Surface* IMG_LoadPNG_RW(SDL_RWops *src)
{
May 22, 2013
May 22, 2013
430
return LoadImageFromRWops(src, kUTTypePNG);
431
}
Jan 10, 2011
Jan 10, 2011
432
433
SDL_Surface* IMG_LoadTGA_RW(SDL_RWops *src)
{
May 22, 2013
May 22, 2013
434
return LoadImageFromRWops(src, CFSTR("com.truevision.tga-image"));
Jan 10, 2011
Jan 10, 2011
435
}
436
437
SDL_Surface* IMG_LoadTIF_RW(SDL_RWops *src)
{
May 22, 2013
May 22, 2013
438
return LoadImageFromRWops(src, kUTTypeTIFF);
439
}