Navigation Menu

Skip to content

Commit

Permalink
Date: Sat, 3 Jan 2009 18:11:52 -0800
Browse files Browse the repository at this point in the history
From: "E. Wing"
Subject: Re: [SDL] Submission: ImageIO backend for SDL_Image on Mac OS X

Ooops. I just caught a bug in the decoupled file loaders.

For the load file routines, the code failed to fallback to the
built-in SDL_image loaders if ImageIO failed to handle the image type.
This meant images like PCX would fail to load.
  • Loading branch information
slouken committed Jan 4, 2009
1 parent e86ca7d commit 3f6af4f
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 23 deletions.
63 changes: 44 additions & 19 deletions IMG_ImageIO.c
Expand Up @@ -392,6 +392,34 @@ static SDL_Surface* LoadImageFromRWops(SDL_RWops* rw_ops, CFStringRef uti_string



static SDL_Surface* LoadImageFromFile(const char* file)
{
SDL_Surface* sdl_surface = NULL;
CGImageSourceRef image_source = NULL;
CGImageRef image_ref = NULL;

// First ImageIO
image_source = CreateCGImageSourceFromFile(file);

if(NULL == image_source)
{
return NULL;
}

image_ref = CreateCGImageFromCGImageSource(image_source);
CFRelease(image_source);

if(NULL == image_ref)
{
return NULL;
}

sdl_surface = Create_SDL_Surface_From_CGImage(image_ref);
CFRelease(image_ref);
return sdl_surface;
}


int IMG_isBMP(SDL_RWops *src)
{
return Internal_isType(src, kUTTypeBMP);
Expand Down Expand Up @@ -452,27 +480,24 @@ SDL_Surface* IMG_LoadTIF_RW(SDL_RWops *src)
// Potentially, Apple can optimize for either case.
SDL_Surface* IMG_Load(const char *file)
{
SDL_Surface* sdl_surface;
CGImageSourceRef image_source;
CGImageRef image_ref = NULL;
SDL_Surface* sdl_surface = NULL;

image_source = CreateCGImageSourceFromFile(file);

if(NULL == image_source)
{
return NULL;
}

image_ref = CreateCGImageFromCGImageSource(image_source);
CFRelease(image_source);

if(NULL == image_ref)
sdl_surface = LoadImageFromFile(file);
if(NULL == sdl_surface)
{
return NULL;
// 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);
}

sdl_surface = Create_SDL_Surface_From_CGImage(image_ref);
CFRelease(image_ref);
return sdl_surface;
return sdl_surface;
}

33 changes: 29 additions & 4 deletions IMG_UIImage.m
Expand Up @@ -219,8 +219,7 @@
return sdl_surface;
}

/* Since UIImage doesn't really support streams well, we should optimize for the file case. */
SDL_Surface *IMG_Load(const char *file)
static SDL_Surface* LoadImageFromFile(const char *file)
{
NSAutoreleasePool* autorelease_pool = [[NSAutoreleasePool alloc] init];
SDL_Surface* sdl_surface = NULL;
Expand All @@ -229,14 +228,40 @@

ns_string = [[NSString alloc] initWithUTF8String:file];
ui_image = [[UIImage alloc] initWithContentsOfFile:ns_string];

sdl_surface = Create_SDL_Surface_From_CGImage([ui_image CGImage]);

[ui_image release];
[ns_string release];

[autorelease_pool drain];

return sdl_surface;
}


/* 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);
}
return sdl_surface;
}

Expand Down

0 comments on commit 3f6af4f

Please sign in to comment.