2
IMGLIB: An example image loading library for use with SDL
3
Copyright (C) 1999 Sam Lantinga
5
This library is free software; you can redistribute it and/or
6
modify it under the terms of the GNU Library General Public
7
License as published by the Free Software Foundation; either
8
version 2 of the License, or (at your option) any later version.
10
This library is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
Library General Public License for more details.
15
You should have received a copy of the GNU Library General Public
16
License along with this library; if not, write to the Free
17
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
5635-34 Springhouse Dr.
21
Pleasanton, CA 94588 (USA)
22
slouken@devolution.com
25
/* A simple library to load images of various formats as SDL surfaces */
33
/* Table of image detection and loading functions */
36
int (*is)(SDL_RWops *src);
37
SDL_Surface *(*load)(SDL_RWops *src);
39
/* keep magicless formats first (denoted by is==NULL) */
40
{ "GIF", IMG_isGIF, IMG_LoadGIF_RW },
41
{ "JPG", IMG_isJPG, IMG_LoadJPG_RW },
42
{ "PNG", IMG_isPNG, IMG_LoadPNG_RW },
45
/* Load an image from a file */
46
SDL_Surface *IMG_Load(const char *file)
48
SDL_RWops *src = SDL_RWFromFile(file, "rb");
49
char *ext = strrchr(file, '.');
52
return IMG_LoadTyped_RW(src, 1, ext);
55
/* Load an image from an SDL datasource (for compatibility) */
56
SDL_Surface *IMG_Load_RW(SDL_RWops *src, int freesrc)
58
return IMG_LoadTyped_RW(src, freesrc, NULL);
61
/* Portable case-insensitive string compare function */
62
static int string_equals(const char *str1, const char *str2)
64
while ( *str1 && *str2 ) {
65
if ( toupper((unsigned char)*str1) !=
66
toupper((unsigned char)*str2) )
71
return (!*str1 && !*str2);
74
/* Load an image from an SDL datasource, optionally specifying the type */
75
SDL_Surface *IMG_LoadTyped_RW(SDL_RWops *src, int freesrc, char *type)
80
/* Make sure there is something to do.. */
85
/* See whether or not this data source can handle seeking */
86
if ( SDL_RWseek(src, 0, SEEK_CUR) < 0 ) {
87
IMG_SetError("Can't seek in this data source");
91
/* Detect the type of image being loaded */
92
start = SDL_RWtell(src);
94
for ( i=0; supported[i].type && !image; ++i ) {
96
&& (SDL_RWseek(src, start, SEEK_SET),
97
supported[i].is(src)))
98
|| (type && string_equals(type, supported[i].type))) {
100
fprintf(stderr, "IMGLIB: Loading image as %s\n",
103
SDL_RWseek(src, start, SEEK_SET);
104
image = supported[i].load(src);
109
/* Clean up, check for errors, and return */
113
if ( image == NULL ) {
114
IMG_SetError("Unsupported image format");
119
/* Invert the alpha of a surface for use with OpenGL
120
This function is a no-op and only kept for backwards compatibility.
122
int IMG_InvertAlpha(int on)