Skip to content

Latest commit

 

History

History
792 lines (667 loc) · 25.5 KB

IMG_ImageIO.m

File metadata and controls

792 lines (667 loc) · 25.5 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
/*
* IMG_ImageIO.c
* SDL_image
*
* Created by Eric Wing on 1/1/09.
* Copyright 2009 __MyCompanyName__. All rights reserved.
*
*/
#if defined(__APPLE__) && !defined(SDL_IMAGE_USE_COMMON_BACKEND)
#include "SDL_image.h"
// Used because CGDataProviderCreate became deprecated in 10.5
#include <AvailabilityMacros.h>
#include <TargetConditionals.h>
#include <Foundation/Foundation.h>
#if (TARGET_OS_IPHONE == 1) || (TARGET_IPHONE_SIMULATOR == 1)
#ifdef ALLOW_UIIMAGE_FALLBACK
#define USE_UIIMAGE_BACKEND() ([UIImage instancesRespondToSelector:@selector(initWithCGImage:scale:orientation:)] == NO)
#else
#define USE_UIIMAGE_BACKEND() (Internal_checkImageIOisAvailable())
#endif
#import <MobileCoreServices/MobileCoreServices.h> // for UTCoreTypes.h
#import <ImageIO/ImageIO.h>
#import <UIKit/UIImage.h>
#else
// For ImageIO framework and also LaunchServices framework (for UTIs)
#include <ApplicationServices/ApplicationServices.h>
#endif
/**************************************************************
***** Begin Callback functions for block reading *************
**************************************************************/
// This callback reads some bytes from an SDL_rwops and copies it
// to a Quartz buffer (supplied by Apple framework).
static size_t MyProviderGetBytesCallback(void* rwops_userdata, void* quartz_buffer, size_t the_count)
{
return (size_t)SDL_RWread((struct SDL_RWops *)rwops_userdata, quartz_buffer, 1, the_count);
}
// This callback is triggered when the data provider is released
// so you can clean up any resources.
static void MyProviderReleaseInfoCallback(void* rwops_userdata)
{
May 22, 2013
May 22, 2013
48
// What should I put here?
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// I think the user and SDL_RWops controls closing, so I don't do anything.
}
static void MyProviderRewindCallback(void* rwops_userdata)
{
SDL_RWseek((struct SDL_RWops *)rwops_userdata, 0, RW_SEEK_SET);
}
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1050 // CGDataProviderCreateSequential was introduced in 10.5; CGDataProviderCreate is deprecated
off_t MyProviderSkipForwardBytesCallback(void* rwops_userdata, off_t the_count)
{
off_t start_position = SDL_RWtell((struct SDL_RWops *)rwops_userdata);
SDL_RWseek((struct SDL_RWops *)rwops_userdata, the_count, RW_SEEK_CUR);
off_t end_position = SDL_RWtell((struct SDL_RWops *)rwops_userdata);
May 22, 2013
May 22, 2013
63
return (end_position - start_position);
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
}
#else // CGDataProviderCreate was deprecated in 10.5
static void MyProviderSkipBytesCallback(void* rwops_userdata, size_t the_count)
{
SDL_RWseek((struct SDL_RWops *)rwops_userdata, the_count, RW_SEEK_CUR);
}
#endif
/**************************************************************
***** End Callback functions for block reading ***************
**************************************************************/
// This creates a CGImageSourceRef which is a handle to an image that can be used to examine information
// about the image or load the actual image data.
static CGImageSourceRef CreateCGImageSourceFromRWops(SDL_RWops* rw_ops, CFDictionaryRef hints_and_options)
{
CGImageSourceRef source_ref;
May 22, 2013
May 22, 2013
81
82
// Similar to SDL_RWops, Apple has their own callbacks for dealing with data streams.
May 22, 2013
May 22, 2013
83
84
85
86
87
88
89
90
91
92
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 1050 // CGDataProviderCreateSequential was introduced in 10.5; CGDataProviderCreate is deprecated
CGDataProviderSequentialCallbacks provider_callbacks =
{
0,
MyProviderGetBytesCallback,
MyProviderSkipForwardBytesCallback,
MyProviderRewindCallback,
MyProviderReleaseInfoCallback
};
May 22, 2013
May 22, 2013
93
94
CGDataProviderRef data_provider = CGDataProviderCreateSequential(rw_ops, &provider_callbacks);
May 22, 2013
May 22, 2013
95
96
97
#else // CGDataProviderCreate was deprecated in 10.5
May 22, 2013
May 22, 2013
98
99
100
101
102
103
104
105
CGDataProviderCallbacks provider_callbacks =
{
MyProviderGetBytesCallback,
MyProviderSkipBytesCallback,
MyProviderRewindCallback,
MyProviderReleaseInfoCallback
};
May 22, 2013
May 22, 2013
106
107
108
109
110
111
CGDataProviderRef data_provider = CGDataProviderCreate(rw_ops, &provider_callbacks);
#endif
// Get the CGImageSourceRef.
// The dictionary can be NULL or contain hints to help ImageIO figure out the image type.
source_ref = CGImageSourceCreateWithDataProvider(data_provider, hints_and_options);
Dec 31, 2011
Dec 31, 2011
112
CGDataProviderRelease(data_provider);
113
114
115
116
117
118
119
120
121
122
return source_ref;
}
/* Create a CGImageSourceRef from a file. */
/* Remember to CFRelease the created source when done. */
static CGImageSourceRef CreateCGImageSourceFromFile(const char* the_path)
{
CFURLRef the_url = NULL;
CGImageSourceRef source_ref = NULL;
CFStringRef cf_string = NULL;
May 22, 2013
May 22, 2013
123
124
125
126
127
128
/* Create a CFString from a C string */
cf_string = CFStringCreateWithCString(NULL, the_path, kCFStringEncodingUTF8);
if (!cf_string) {
return NULL;
}
May 22, 2013
May 22, 2013
129
130
131
/* Create a CFURL from a CFString */
the_url = CFURLCreateWithFileSystemPath(NULL, cf_string, kCFURLPOSIXPathStyle, false);
May 22, 2013
May 22, 2013
132
133
134
/* Don't need the CFString any more (error or not) */
CFRelease(cf_string);
May 22, 2013
May 22, 2013
135
136
137
138
139
if(!the_url)
{
return NULL;
}
May 22, 2013
May 22, 2013
140
141
142
143
144
source_ref = CGImageSourceCreateWithURL(the_url, NULL);
/* Don't need the URL any more (error or not) */
CFRelease(the_url);
May 22, 2013
May 22, 2013
145
146
147
148
149
150
151
return source_ref;
}
static CGImageRef CreateCGImageFromCGImageSource(CGImageSourceRef image_source)
{
CGImageRef image_ref = NULL;
May 22, 2013
May 22, 2013
152
153
154
155
156
if(NULL == image_source)
{
return NULL;
}
May 22, 2013
May 22, 2013
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Get the first item in the image source (some image formats may
// contain multiple items).
image_ref = CGImageSourceCreateImageAtIndex(image_source, 0, NULL);
if(NULL == image_ref)
{
IMG_SetError("CGImageSourceCreateImageAtIndex() failed");
}
return image_ref;
}
static CFDictionaryRef CreateHintDictionary(CFStringRef uti_string_hint)
{
CFDictionaryRef hint_dictionary = NULL;
May 22, 2013
May 22, 2013
171
172
173
174
175
176
if(uti_string_hint != NULL)
{
// Do a bunch of work to setup a CFDictionary containing the jpeg compression properties.
CFStringRef the_keys[1];
CFStringRef the_values[1];
May 22, 2013
May 22, 2013
177
178
179
the_keys[0] = kCGImageSourceTypeIdentifierHint;
the_values[0] = uti_string_hint;
May 22, 2013
May 22, 2013
180
181
182
183
184
185
186
187
// kCFTypeDictionaryKeyCallBacks or kCFCopyStringDictionaryKeyCallBacks?
hint_dictionary = CFDictionaryCreate(NULL, (const void**)&the_keys, (const void**)&the_values, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
}
return hint_dictionary;
}
// Once we have our image, we need to get it into an SDL_Surface
Jan 14, 2012
Jan 14, 2012
188
static SDL_Surface* Create_SDL_Surface_From_CGImage_RGB(CGImageRef image_ref)
189
190
191
192
193
194
195
196
{
/* 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.
*/
May 22, 2013
May 22, 2013
197
198
199
200
size_t w = CGImageGetWidth(image_ref);
size_t h = CGImageGetHeight(image_ref);
CGRect rect = {{0, 0}, {w, h}};
May 22, 2013
May 22, 2013
201
202
203
204
CGImageAlphaInfo alpha = CGImageGetAlphaInfo(image_ref);
//size_t bits_per_pixel = CGImageGetBitsPerPixel(image_ref);
size_t bits_per_component = 8;
May 22, 2013
May 22, 2013
205
206
207
208
209
210
SDL_Surface* surface;
Uint32 Amask;
Uint32 Rmask;
Uint32 Gmask;
Uint32 Bmask;
May 22, 2013
May 22, 2013
211
212
213
CGContextRef bitmap_context;
CGBitmapInfo bitmap_info;
Nov 29, 2014
Nov 29, 2014
214
CGColorSpaceRef color_space = CGColorSpaceCreateDeviceRGB();
May 22, 2013
May 22, 2013
215
216
217
218
219
220
221
222
223
224
225
226
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;
}
May 22, 2013
May 22, 2013
227
228
229
230
Rmask = 0x00FF0000;
Gmask = 0x0000FF00;
Bmask = 0x000000FF;
May 22, 2013
May 22, 2013
231
Dec 13, 2017
Dec 13, 2017
232
surface = SDL_CreateRGBSurface(SDL_SWSURFACE, (int)w, (int)h, 32, Rmask, Gmask, Bmask, Amask);
233
234
235
236
237
238
239
240
241
242
243
244
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
);
May 22, 2013
May 22, 2013
245
246
247
// Draws the image into the context's image_data
CGContextDrawImage(bitmap_context, rect, image_ref);
May 22, 2013
May 22, 2013
248
249
CGContextRelease(bitmap_context);
May 22, 2013
May 22, 2013
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// 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--; ) {
#if __LITTLE_ENDIAN__
Uint8 A = p[3];
if (A) {
for (j = 0; j < 3; ++j) {
p[j] = (p[j] * 255) / A;
}
}
#else
Uint8 A = p[0];
if (A) {
for (j = 1; j < 4; ++j) {
p[j] = (p[j] * 255) / A;
}
}
#endif /* ENDIAN */
p += 4;
}
}
}
May 22, 2013
May 22, 2013
275
276
277
if (color_space)
{
May 22, 2013
May 22, 2013
278
CGColorSpaceRelease(color_space);
May 22, 2013
May 22, 2013
280
281
282
return surface;
}
Jan 14, 2012
Jan 14, 2012
283
284
285
286
287
288
289
290
291
static SDL_Surface* Create_SDL_Surface_From_CGImage_Index(CGImageRef image_ref)
{
size_t w = CGImageGetWidth(image_ref);
size_t h = CGImageGetHeight(image_ref);
size_t bits_per_pixel = CGImageGetBitsPerPixel(image_ref);
size_t bytes_per_row = CGImageGetBytesPerRow(image_ref);
SDL_Surface* surface;
SDL_Palette* palette;
May 22, 2013
May 22, 2013
292
CGColorSpaceRef color_space = CGImageGetColorSpace(image_ref);
Jan 14, 2012
Jan 14, 2012
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
CGColorSpaceRef base_color_space = CGColorSpaceGetBaseColorSpace(color_space);
size_t num_components = CGColorSpaceGetNumberOfComponents(base_color_space);
size_t num_entries = CGColorSpaceGetColorTableCount(color_space);
uint8_t *entry, entries[num_components * num_entries];
/* What do we do if it's not RGB? */
if (num_components != 3) {
SDL_SetError("Unknown colorspace components %lu", num_components);
return NULL;
}
if (bits_per_pixel != 8) {
SDL_SetError("Unknown bits_per_pixel %lu", bits_per_pixel);
return NULL;
}
CGColorSpaceGetColorTable(color_space, entries);
Dec 13, 2017
Dec 13, 2017
309
surface = SDL_CreateRGBSurface(SDL_SWSURFACE, (int)w, (int)h, bits_per_pixel, 0, 0, 0, 0);
Jan 14, 2012
Jan 14, 2012
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
if (surface) {
uint8_t* pixels = (uint8_t*)surface->pixels;
CGDataProviderRef provider = CGImageGetDataProvider(image_ref);
NSData* data = (id)CGDataProviderCopyData(provider);
[data autorelease];
const uint8_t* bytes = [data bytes];
size_t i;
palette = surface->format->palette;
for (i = 0, entry = entries; i < num_entries; ++i) {
palette->colors[i].r = entry[0];
palette->colors[i].g = entry[1];
palette->colors[i].b = entry[2];
entry += num_components;
}
for (i = 0; i < h; ++i) {
SDL_memcpy(pixels, bytes, w);
pixels += surface->pitch;
bytes += bytes_per_row;
}
}
return surface;
}
static SDL_Surface* Create_SDL_Surface_From_CGImage(CGImageRef image_ref)
{
May 22, 2013
May 22, 2013
336
CGColorSpaceRef color_space = CGImageGetColorSpace(image_ref);
Jan 14, 2012
Jan 14, 2012
337
338
339
340
341
342
if (CGColorSpaceGetModel(color_space) == kCGColorSpaceModelIndexed) {
return Create_SDL_Surface_From_CGImage_Index(image_ref);
} else {
return Create_SDL_Surface_From_CGImage_RGB(image_ref);
}
}
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
#pragma mark -
#pragma mark IMG_Init stubs
#if !defined(ALLOW_UIIMAGE_FALLBACK) && ((TARGET_OS_IPHONE == 1) || (TARGET_IPHONE_SIMULATOR == 1))
static int Internal_checkImageIOisAvailable() {
// just check if we are running on ios 4 or more, else throw exception
if ([UIImage instancesRespondToSelector:@selector(initWithCGImage:scale:orientation:)])
return 0;
[NSException raise:@"UIImage fallback not enabled at compile time"
format:@"ImageIO is not available on your platform, please recompile SDL_Image with ALLOW_UIIMAGE_FALLBACK."];
return -1;
}
#endif
int IMG_InitJPG()
{
return 0;
}
void IMG_QuitJPG()
{
}
int IMG_InitPNG()
{
return 0;
}
void IMG_QuitPNG()
{
}
int IMG_InitTIF()
{
return 0;
}
void IMG_QuitTIF()
{
}
#pragma mark -
#pragma mark Get type of image
static int Internal_isType_UIImage (SDL_RWops *rw_ops, CFStringRef uti_string_to_test)
{
int is_type = 0;
May 22, 2013
May 22, 2013
390
391
#if defined(ALLOW_UIIMAGE_FALLBACK) && ((TARGET_OS_IPHONE == 1) || (TARGET_IPHONE_SIMULATOR == 1))
Oct 22, 2017
Oct 22, 2017
392
Sint64 start = SDL_RWtell(rw_ops);
393
394
if ((0 == CFStringCompare(uti_string_to_test, kUTTypeICO, 0)) ||
(0 == CFStringCompare(uti_string_to_test, CFSTR("com.microsoft.cur"), 0))) {
May 22, 2013
May 22, 2013
395
396
397
398
399
400
// The Win32 ICO file header (14 bytes)
Uint16 bfReserved;
Uint16 bfType;
Uint16 bfCount;
int type = (0 == CFStringCompare(uti_string_to_test, kUTTypeICO, 0)) ? 1 : 2;
May 22, 2013
May 22, 2013
401
402
403
404
bfReserved = SDL_ReadLE16(rw_ops);
bfType = SDL_ReadLE16(rw_ops);
bfCount = SDL_ReadLE16(rw_ops);
May 22, 2013
May 22, 2013
405
if ((bfReserved == 0) && (bfType == type) && (bfCount != 0))
406
407
408
is_type = 1;
} else if (0 == CFStringCompare(uti_string_to_test, kUTTypeBMP, 0)) {
char magic[2];
May 22, 2013
May 22, 2013
409
410
411
412
413
414
415
416
if ( SDL_RWread(rw_ops, magic, sizeof(magic), 1) ) {
if ( strncmp(magic, "BM", 2) == 0 ) {
is_type = 1;
}
}
} else if (0 == CFStringCompare(uti_string_to_test, kUTTypeGIF, 0)) {
char magic[6];
May 22, 2013
May 22, 2013
417
418
419
420
421
422
423
424
425
426
427
if ( SDL_RWread(rw_ops, magic, sizeof(magic), 1) ) {
if ( (strncmp(magic, "GIF", 3) == 0) &&
((memcmp(magic + 3, "87a", 3) == 0) ||
(memcmp(magic + 3, "89a", 3) == 0)) ) {
is_type = 1;
}
}
} else if (0 == CFStringCompare(uti_string_to_test, kUTTypeJPEG, 0)) {
int in_scan = 0;
Uint8 magic[4];
May 22, 2013
May 22, 2013
428
429
430
431
// 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! */
May 22, 2013
May 22, 2013
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
if ( SDL_RWread(rw_ops, magic, 2, 1) ) {
if ( (magic[0] == 0xFF) && (magic[1] == 0xD8) ) {
is_type = 1;
while (is_type == 1) {
if(SDL_RWread(rw_ops, magic, 1, 2) != 2) {
is_type = 0;
} else if( (magic[0] != 0xFF) && (in_scan == 0) ) {
is_type = 0;
} else if( (magic[0] != 0xFF) || (magic[1] == 0xFF) ) {
/* Extra padding in JPEG (legal) */
/* or this is data and we are scanning */
SDL_RWseek(rw_ops, -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(rw_ops, magic+2, 1, 2) != 2) {
is_type = 0;
} else {
/* Yes, it's big-endian */
Uint32 start;
Uint32 size;
Uint32 end;
start = SDL_RWtell(rw_ops);
size = (magic[2] << 8) + magic[3];
end = SDL_RWseek(rw_ops, size-2, SEEK_CUR);
if ( end != start + size - 2 ) is_type = 0;
if ( magic[1] == 0xDA ) {
/* Now comes the actual JPEG meat */
May 22, 2013
May 22, 2013
465
#ifdef FAST_IS_JPEG
466
467
468
469
470
471
472
473
474
475
476
477
478
/* Ok, I'm convinced. It is a JPEG. */
break;
#else
/* I'm not convinced. Prove it! */
in_scan = 1;
#endif
}
}
}
}
}
} else if (0 == CFStringCompare(uti_string_to_test, kUTTypePNG, 0)) {
Uint8 magic[4];
May 22, 2013
May 22, 2013
479
480
481
482
483
484
485
486
487
488
489
490
491
if ( SDL_RWread(rw_ops, magic, 1, sizeof(magic)) == sizeof(magic) ) {
if ( magic[0] == 0x89 &&
magic[1] == 'P' &&
magic[2] == 'N' &&
magic[3] == 'G' ) {
is_type = 1;
}
}
} else if (0 == CFStringCompare(uti_string_to_test, CFSTR("com.truevision.tga-image"), 0)) {
//TODO: fill me!
} else if (0 == CFStringCompare(uti_string_to_test, kUTTypeTIFF, 0)) {
Uint8 magic[4];
May 22, 2013
May 22, 2013
492
493
494
495
496
497
498
499
500
501
502
503
504
505
if ( SDL_RWread(rw_ops, 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_type = 1;
}
}
}
May 22, 2013
May 22, 2013
506
Oct 22, 2017
Oct 22, 2017
507
// reset the file pointer
508
509
510
511
512
513
514
515
516
SDL_RWseek(rw_ops, start, SEEK_SET);
#endif /* #if defined(ALLOW_UIIMAGE_FALLBACK) && ((TARGET_OS_IPHONE == 1) || (TARGET_IPHONE_SIMULATOR == 1)) */
return is_type;
}
static int Internal_isType_ImageIO (SDL_RWops *rw_ops, CFStringRef uti_string_to_test)
{
int is_type = 0;
May 22, 2013
May 22, 2013
517
Oct 22, 2017
Oct 22, 2017
518
Sint64 start = SDL_RWtell(rw_ops);
May 22, 2013
May 22, 2013
519
CFDictionaryRef hint_dictionary = CreateHintDictionary(uti_string_to_test);
520
CGImageSourceRef image_source = CreateCGImageSourceFromRWops(rw_ops, hint_dictionary);
May 22, 2013
May 22, 2013
521
522
if (hint_dictionary != NULL) {
May 22, 2013
May 22, 2013
523
CFRelease(hint_dictionary);
May 22, 2013
May 22, 2013
525
526
if (NULL == image_source) {
Oct 22, 2017
Oct 22, 2017
527
528
// reset the file pointer
SDL_RWseek(rw_ops, start, SEEK_SET);
529
530
return 0;
}
May 22, 2013
May 22, 2013
531
532
533
534
535
// This will get the UTI of the container, not the image itself.
// Under most cases, this won't be a problem.
// But if a person passes an icon file which contains a bmp,
// the format will be of the icon file.
May 22, 2013
May 22, 2013
536
// But I think the main SDL_image codebase has this same problem so I'm not going to worry about it.
537
CFStringRef uti_type = CGImageSourceGetType(image_source);
May 22, 2013
May 22, 2013
538
539
// CFShow(uti_type);
540
541
// Unsure if we really want conformance or equality
is_type = (int)UTTypeConformsTo(uti_string_to_test, uti_type);
May 22, 2013
May 22, 2013
542
543
CFRelease(image_source);
Oct 22, 2017
Oct 22, 2017
544
545
546
// reset the file pointer
SDL_RWseek(rw_ops, start, SEEK_SET);
547
548
549
550
551
552
553
return is_type;
}
static int Internal_isType (SDL_RWops *rw_ops, CFStringRef uti_string_to_test)
{
if (rw_ops == NULL)
return 0;
May 22, 2013
May 22, 2013
554
555
556
557
558
559
560
561
562
#if (TARGET_OS_IPHONE == 1) || (TARGET_IPHONE_SIMULATOR == 1)
if (USE_UIIMAGE_BACKEND())
return Internal_isType_UIImage(rw_ops, uti_string_to_test);
else
#endif
return Internal_isType_ImageIO(rw_ops, uti_string_to_test);
}
Sep 26, 2012
Sep 26, 2012
563
564
#ifdef BMP_USES_IMAGEIO
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
int IMG_isCUR(SDL_RWops *src)
{
/* FIXME: Is this a supported type? */
return Internal_isType(src, CFSTR("com.microsoft.cur"));
}
int IMG_isICO(SDL_RWops *src)
{
return Internal_isType(src, kUTTypeICO);
}
int IMG_isBMP(SDL_RWops *src)
{
return Internal_isType(src, kUTTypeBMP);
}
Sep 26, 2012
Sep 26, 2012
581
582
#endif /* BMP_USES_IMAGEIO */
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
int IMG_isGIF(SDL_RWops *src)
{
return Internal_isType(src, kUTTypeGIF);
}
// Note: JPEG 2000 is kUTTypeJPEG2000
int IMG_isJPG(SDL_RWops *src)
{
return Internal_isType(src, kUTTypeJPEG);
}
int IMG_isPNG(SDL_RWops *src)
{
return Internal_isType(src, kUTTypePNG);
}
// This isn't a public API function. Apple seems to be able to identify tga's.
int IMG_isTGA(SDL_RWops *src)
{
return Internal_isType(src, CFSTR("com.truevision.tga-image"));
}
int IMG_isTIF(SDL_RWops *src)
{
return Internal_isType(src, kUTTypeTIFF);
}
#pragma mark -
#pragma mark Load image engine
static SDL_Surface *LoadImageFromRWops_UIImage (SDL_RWops* rw_ops, CFStringRef uti_string_hint)
{
SDL_Surface *sdl_surface = NULL;
#if defined(ALLOW_UIIMAGE_FALLBACK) && ((TARGET_OS_IPHONE == 1) || (TARGET_IPHONE_SIMULATOR == 1))
NSAutoreleasePool* autorelease_pool = [[NSAutoreleasePool alloc] init];
UIImage *ui_image;
int bytes_read = 0;
May 22, 2013
May 22, 2013
620
// I don't know what a good size is.
621
622
623
// 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];
May 22, 2013
May 22, 2013
624
625
626
627
628
629
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);
May 22, 2013
May 22, 2013
630
631
632
633
634
ui_image = [[UIImage alloc] initWithData:ns_data];
if (ui_image != nil)
sdl_surface = Create_SDL_Surface_From_CGImage([ui_image CGImage]);
[ui_image release];
May 22, 2013
May 22, 2013
635
[ns_data release];
636
637
638
639
640
641
642
643
644
645
646
647
[autorelease_pool drain];
#endif /* #if defined(ALLOW_UIIMAGE_FALLBACK) && ((TARGET_OS_IPHONE == 1) || (TARGET_IPHONE_SIMULATOR == 1)) */
return sdl_surface;
}
static SDL_Surface *LoadImageFromRWops_ImageIO (SDL_RWops *rw_ops, CFStringRef uti_string_hint)
{
CFDictionaryRef hint_dictionary = CreateHintDictionary(uti_string_hint);
CGImageSourceRef image_source = CreateCGImageSourceFromRWops(rw_ops, hint_dictionary);
if (hint_dictionary != NULL)
May 22, 2013
May 22, 2013
648
CFRelease(hint_dictionary);
649
650
651
if (NULL == image_source)
return NULL;
May 22, 2013
May 22, 2013
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
CGImageRef image_ref = CreateCGImageFromCGImageSource(image_source);
CFRelease(image_source);
if (NULL == image_ref)
return NULL;
SDL_Surface *sdl_surface = Create_SDL_Surface_From_CGImage(image_ref);
CFRelease(image_ref);
return sdl_surface;
}
static SDL_Surface *LoadImageFromRWops (SDL_RWops *rw_ops, CFStringRef uti_string_hint)
{
#if (TARGET_OS_IPHONE == 1) || (TARGET_IPHONE_SIMULATOR == 1)
May 22, 2013
May 22, 2013
667
if (USE_UIIMAGE_BACKEND())
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
return LoadImageFromRWops_UIImage(rw_ops, uti_string_hint);
else
#endif
return LoadImageFromRWops_ImageIO(rw_ops, uti_string_hint);
}
static SDL_Surface* LoadImageFromFile_UIImage (const char *file)
{
SDL_Surface *sdl_surface = NULL;
#if defined(ALLOW_UIIMAGE_FALLBACK) && ((TARGET_OS_IPHONE == 1) || (TARGET_IPHONE_SIMULATOR == 1))
NSAutoreleasePool* autorelease_pool = [[NSAutoreleasePool alloc] init];
NSString *ns_string = [[NSString alloc] initWithUTF8String:file];
UIImage *ui_image = [[UIImage alloc] initWithContentsOfFile:ns_string];
if (ui_image != nil)
sdl_surface = Create_SDL_Surface_From_CGImage([ui_image CGImage]);
[ui_image release];
[ns_string release];
[autorelease_pool drain];
#endif /* #if defined(ALLOW_UIIMAGE_FALLBACK) && ((TARGET_OS_IPHONE == 1) || (TARGET_IPHONE_SIMULATOR == 1)) */
May 22, 2013
May 22, 2013
689
return sdl_surface;
690
691
692
693
694
695
696
697
698
699
}
static SDL_Surface* LoadImageFromFile_ImageIO (const char *file)
{
CGImageSourceRef image_source = NULL;
image_source = CreateCGImageSourceFromFile(file);
if(NULL == image_source)
return NULL;
May 22, 2013
May 22, 2013
700
701
702
703
704
705
706
707
CGImageRef image_ref = CreateCGImageFromCGImageSource(image_source);
CFRelease(image_source);
if (NULL == image_ref)
return NULL;
SDL_Surface *sdl_surface = Create_SDL_Surface_From_CGImage(image_ref);
CFRelease(image_ref);
May 22, 2013
May 22, 2013
708
return sdl_surface;
709
710
711
712
713
714
715
716
717
718
719
720
}
static SDL_Surface* LoadImageFromFile (const char *file)
{
#if (TARGET_OS_IPHONE == 1) || (TARGET_IPHONE_SIMULATOR == 1)
if (USE_UIIMAGE_BACKEND())
return LoadImageFromFile_UIImage(file);
else
#endif
return LoadImageFromFile_ImageIO(file);
}
Sep 26, 2012
Sep 26, 2012
721
722
#ifdef BMP_USES_IMAGEIO
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
SDL_Surface* IMG_LoadCUR_RW (SDL_RWops *src)
{
/* FIXME: Is this a supported type? */
return LoadImageFromRWops(src, CFSTR("com.microsoft.cur"));
}
SDL_Surface* IMG_LoadICO_RW (SDL_RWops *src)
{
return LoadImageFromRWops(src, kUTTypeICO);
}
SDL_Surface* IMG_LoadBMP_RW (SDL_RWops *src)
{
return LoadImageFromRWops(src, kUTTypeBMP);
}
Sep 26, 2012
Sep 26, 2012
739
740
#endif /* BMP_USES_IMAGEIO */
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
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_LoadTGA_RW (SDL_RWops *src)
{
return LoadImageFromRWops(src, CFSTR("com.truevision.tga-image"));
}
SDL_Surface* IMG_LoadTIF_RW (SDL_RWops *src)
{
return LoadImageFromRWops(src, kUTTypeTIFF);
}
// Since UIImage doesn't really support streams well, we should optimize for the file case.
// Apple provides both stream and file loading functions in ImageIO.
// Potentially, Apple can optimize for either case.
SDL_Surface* IMG_Load (const char *file)
{
SDL_Surface* sdl_surface = NULL;
May 22, 2013
May 22, 2013
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
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;
}
#endif /* defined(__APPLE__) && !defined(SDL_IMAGE_USE_COMMON_BACKEND) */