slouken@libsdl.org
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "SDL_image.h"
#define ARRAYSIZE(a) (sizeof(a) / sizeof((a)[0]))
static struct {
char *type;
int (*is)(SDL_RWops *src);
SDL_Surface *(*load)(SDL_RWops *src);
} supported[] = {
{ "TGA", NULL, IMG_LoadTGA_RW },
{ "BMP", IMG_isBMP, IMG_LoadBMP_RW },
{ "PNM", IMG_isPNM, IMG_LoadPNM_RW },
{ "XPM", IMG_isXPM, IMG_LoadXPM_RW },
{ "XCF", IMG_isXCF, IMG_LoadXCF_RW },
{ "PCX", IMG_isPCX, IMG_LoadPCX_RW },
{ "GIF", IMG_isGIF, IMG_LoadGIF_RW },
{ "JPG", IMG_isJPG, IMG_LoadJPG_RW },
{ "TIF", IMG_isTIF, IMG_LoadTIF_RW },
{ "LBM", IMG_isLBM, IMG_LoadLBM_RW },
{ "PNG", IMG_isPNG, IMG_LoadPNG_RW }
};
const SDL_version *IMG_Linked_Version(void)
{
static SDL_version linked_version;
SDL_IMAGE_VERSION(&linked_version);
return(&linked_version);
}
SDL_Surface *IMG_Load(const char *file)
{
SDL_RWops *src = SDL_RWFromFile(file, "rb");
char *ext = strrchr(file, '.');
if(ext) {
ext++;
}
if(!src) {
return NULL;
}
return IMG_LoadTyped_RW(src, 1, ext);
}
SDL_Surface *IMG_Load_RW(SDL_RWops *src, int freesrc)
{
return IMG_LoadTyped_RW(src, freesrc, NULL);
}
static int IMG_string_equals(const char *str1, const char *str2)
{
while ( *str1 && *str2 ) {
if ( toupper((unsigned char)*str1) !=
toupper((unsigned char)*str2) )
break;
++str1;
++str2;
}
return (!*str1 && !*str2);
}
SDL_Surface *IMG_LoadTyped_RW(SDL_RWops *src, int freesrc, char *type)
{
int i, start;
SDL_Surface *image;
if ( src == NULL ) {
IMG_SetError("Passed a NULL data source");
return(NULL);
}
if ( SDL_RWseek(src, 0, SEEK_CUR) < 0 ) {
IMG_SetError("Can't seek in this data source");
if(freesrc)
SDL_RWclose(src);
return(NULL);
}
start = SDL_RWtell(src);
image = NULL;
for ( i=0; i < ARRAYSIZE(supported); ++i ) {
if(supported[i].is) {
SDL_RWseek(src, start, SEEK_SET);
if(!supported[i].is(src))
continue;
} else {
if(!type
|| !IMG_string_equals(type, supported[i].type))
continue;
}
#ifdef DEBUG_IMGLIB
fprintf(stderr, "IMGLIB: Loading image as %s\n",
supported[i].type);
#endif
SDL_RWseek(src, start, SEEK_SET);
image = supported[i].load(src);
if(freesrc)
SDL_RWclose(src);
return image;
}
if ( freesrc ) {
SDL_RWclose(src);
}
IMG_SetError("Unsupported image format");
return NULL;
}
int IMG_InvertAlpha(int on)
{
return 1;
}