IMG.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 08 Dec 2008 00:27:32 +0000
changeset 186 5fbbeabf647f
parent 121 1bf9c0c87374
child 187 3405ab551300
permissions -rw-r--r--
Updated copyright date
slouken@0
     1
/*
slouken@53
     2
    SDL_image:  An example image loading library for use with SDL
slouken@186
     3
    Copyright (C) 1997-2009 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@121
     6
    modify it under the terms of the GNU Lesser General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@121
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@121
    13
    Lesser General Public License for more details.
slouken@0
    14
slouken@121
    15
    You should have received a copy of the GNU Lesser General Public
slouken@121
    16
    License along with this library; if not, write to the Free Software
slouken@121
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@53
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@0
    22
slouken@0
    23
/* A simple library to load images of various formats as SDL surfaces */
slouken@0
    24
slouken@0
    25
#include <stdio.h>
slouken@0
    26
#include <string.h>
slouken@0
    27
#include <ctype.h>
slouken@0
    28
slouken@0
    29
#include "SDL_image.h"
slouken@0
    30
slouken@7
    31
#define ARRAYSIZE(a) (sizeof(a) / sizeof((a)[0]))
slouken@7
    32
slouken@0
    33
/* Table of image detection and loading functions */
slouken@0
    34
static struct {
slouken@0
    35
	char *type;
icculus@110
    36
	int (SDLCALL *is)(SDL_RWops *src);
icculus@110
    37
	SDL_Surface *(SDLCALL *load)(SDL_RWops *src);
slouken@0
    38
} supported[] = {
slouken@24
    39
	/* keep magicless formats first */
slouken@95
    40
	{ "TGA", NULL,      IMG_LoadTGA_RW },
slouken@0
    41
	{ "BMP", IMG_isBMP, IMG_LoadBMP_RW },
slouken@0
    42
	{ "GIF", IMG_isGIF, IMG_LoadGIF_RW },
slouken@0
    43
	{ "JPG", IMG_isJPG, IMG_LoadJPG_RW },
slouken@119
    44
	{ "LBM", IMG_isLBM, IMG_LoadLBM_RW },
slouken@119
    45
	{ "PCX", IMG_isPCX, IMG_LoadPCX_RW },
slouken@119
    46
	{ "PNG", IMG_isPNG, IMG_LoadPNG_RW },
slouken@119
    47
	{ "PNM", IMG_isPNM, IMG_LoadPNM_RW }, /* P[BGP]M share code */
slouken@0
    48
	{ "TIF", IMG_isTIF, IMG_LoadTIF_RW },
slouken@119
    49
	{ "XCF", IMG_isXCF, IMG_LoadXCF_RW },
slouken@119
    50
	{ "XPM", IMG_isXPM, IMG_LoadXPM_RW },
slouken@119
    51
	{ "XV",  IMG_isXV,  IMG_LoadXV_RW  }
slouken@0
    52
};
slouken@0
    53
slouken@89
    54
const SDL_version *IMG_Linked_Version(void)
slouken@89
    55
{
slouken@89
    56
	static SDL_version linked_version;
slouken@89
    57
	SDL_IMAGE_VERSION(&linked_version);
slouken@89
    58
	return(&linked_version);
slouken@89
    59
}
slouken@89
    60
slouken@0
    61
/* Load an image from a file */
slouken@0
    62
SDL_Surface *IMG_Load(const char *file)
slouken@0
    63
{
slouken@0
    64
    SDL_RWops *src = SDL_RWFromFile(file, "rb");
slouken@0
    65
    char *ext = strrchr(file, '.');
slouken@95
    66
    if(ext) {
slouken@95
    67
        ext++;
slouken@95
    68
    }
slouken@95
    69
    if(!src) {
slouken@95
    70
        /* The error message has been set in SDL_RWFromFile */
slouken@95
    71
        return NULL;
slouken@95
    72
    }
slouken@0
    73
    return IMG_LoadTyped_RW(src, 1, ext);
slouken@0
    74
}
slouken@0
    75
slouken@0
    76
/* Load an image from an SDL datasource (for compatibility) */
slouken@0
    77
SDL_Surface *IMG_Load_RW(SDL_RWops *src, int freesrc)
slouken@0
    78
{
slouken@0
    79
    return IMG_LoadTyped_RW(src, freesrc, NULL);
slouken@0
    80
}
slouken@0
    81
slouken@0
    82
/* Portable case-insensitive string compare function */
slouken@50
    83
static int IMG_string_equals(const char *str1, const char *str2)
slouken@0
    84
{
slouken@0
    85
	while ( *str1 && *str2 ) {
slouken@0
    86
		if ( toupper((unsigned char)*str1) !=
slouken@0
    87
		     toupper((unsigned char)*str2) )
slouken@0
    88
			break;
slouken@0
    89
		++str1;
slouken@0
    90
		++str2;
slouken@0
    91
	}
slouken@0
    92
	return (!*str1 && !*str2);
slouken@0
    93
}
slouken@0
    94
slouken@0
    95
/* Load an image from an SDL datasource, optionally specifying the type */
slouken@0
    96
SDL_Surface *IMG_LoadTyped_RW(SDL_RWops *src, int freesrc, char *type)
slouken@0
    97
{
slouken@117
    98
	int i;
slouken@0
    99
	SDL_Surface *image;
slouken@0
   100
slouken@0
   101
	/* Make sure there is something to do.. */
slouken@0
   102
	if ( src == NULL ) {
slouken@95
   103
		IMG_SetError("Passed a NULL data source");
slouken@0
   104
		return(NULL);
slouken@0
   105
	}
slouken@0
   106
slouken@0
   107
	/* See whether or not this data source can handle seeking */
slouken@0
   108
	if ( SDL_RWseek(src, 0, SEEK_CUR) < 0 ) {
slouken@0
   109
		IMG_SetError("Can't seek in this data source");
slouken@78
   110
		if(freesrc)
slouken@78
   111
			SDL_RWclose(src);
slouken@0
   112
		return(NULL);
slouken@0
   113
	}
slouken@0
   114
slouken@0
   115
	/* Detect the type of image being loaded */
slouken@0
   116
	image = NULL;
slouken@11
   117
	for ( i=0; i < ARRAYSIZE(supported); ++i ) {
slouken@62
   118
		if(supported[i].is) {
slouken@62
   119
			if(!supported[i].is(src))
slouken@62
   120
				continue;
slouken@62
   121
		} else {
slouken@62
   122
			/* magicless format */
slouken@62
   123
			if(!type
slouken@62
   124
			   || !IMG_string_equals(type, supported[i].type))
slouken@62
   125
				continue;
slouken@62
   126
		}
slouken@0
   127
#ifdef DEBUG_IMGLIB
slouken@62
   128
		fprintf(stderr, "IMGLIB: Loading image as %s\n",
slouken@62
   129
			supported[i].type);
slouken@0
   130
#endif
slouken@62
   131
		image = supported[i].load(src);
slouken@62
   132
		if(freesrc)
slouken@62
   133
			SDL_RWclose(src);
slouken@62
   134
		return image;
slouken@0
   135
	}
slouken@0
   136
slouken@0
   137
	if ( freesrc ) {
slouken@0
   138
		SDL_RWclose(src);
slouken@0
   139
	}
slouken@62
   140
	IMG_SetError("Unsupported image format");
slouken@62
   141
	return NULL;
slouken@0
   142
}
slouken@0
   143
slouken@0
   144
/* Invert the alpha of a surface for use with OpenGL
slouken@4
   145
   This function is a no-op and only kept for backwards compatibility.
slouken@0
   146
 */
slouken@0
   147
int IMG_InvertAlpha(int on)
slouken@0
   148
{
slouken@4
   149
    return 1;
slouken@0
   150
}