IMG.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 26 Sep 2009 09:09:53 +0000
changeset 205 0aa18e250940
parent 189 39d5ac4ec0f8
child 221 86e0ba2d47cf
permissions -rw-r--r--
Mason Wheeler - 2009-06-10 06:29:45 PDT
* Added IMG_Init()/IMG_Quit() to prevent constantly loading and unloading DLLs
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 },
couriersud@189
    41
	{ "CUR", IMG_isCUR, IMG_LoadCUR_RW },
couriersud@189
    42
	{ "ICO", IMG_isICO, IMG_LoadICO_RW },
slouken@0
    43
	{ "BMP", IMG_isBMP, IMG_LoadBMP_RW },
slouken@0
    44
	{ "GIF", IMG_isGIF, IMG_LoadGIF_RW },
slouken@0
    45
	{ "JPG", IMG_isJPG, IMG_LoadJPG_RW },
slouken@119
    46
	{ "LBM", IMG_isLBM, IMG_LoadLBM_RW },
slouken@119
    47
	{ "PCX", IMG_isPCX, IMG_LoadPCX_RW },
slouken@119
    48
	{ "PNG", IMG_isPNG, IMG_LoadPNG_RW },
slouken@119
    49
	{ "PNM", IMG_isPNM, IMG_LoadPNM_RW }, /* P[BGP]M share code */
slouken@0
    50
	{ "TIF", IMG_isTIF, IMG_LoadTIF_RW },
slouken@119
    51
	{ "XCF", IMG_isXCF, IMG_LoadXCF_RW },
slouken@119
    52
	{ "XPM", IMG_isXPM, IMG_LoadXPM_RW },
slouken@119
    53
	{ "XV",  IMG_isXV,  IMG_LoadXV_RW  }
slouken@0
    54
};
slouken@0
    55
slouken@89
    56
const SDL_version *IMG_Linked_Version(void)
slouken@89
    57
{
slouken@89
    58
	static SDL_version linked_version;
slouken@89
    59
	SDL_IMAGE_VERSION(&linked_version);
slouken@89
    60
	return(&linked_version);
slouken@89
    61
}
slouken@89
    62
slouken@205
    63
extern int IMG_InitJPG();
slouken@205
    64
extern int IMG_QuitJPG();
slouken@205
    65
extern int IMG_InitPNG();
slouken@205
    66
extern int IMG_QuitPNG();
slouken@205
    67
extern int IMG_InitTIF();
slouken@205
    68
extern int IMG_QuitTIF();
slouken@205
    69
slouken@205
    70
static int initialized = 0;
slouken@205
    71
slouken@205
    72
int IMG_Init(int flags)
slouken@205
    73
{
slouken@205
    74
	int result = 0;
slouken@205
    75
slouken@205
    76
	if ((flags & IMG_INIT_JPG) && !(initialized & IMG_INIT_JPG)) {
slouken@205
    77
		if (IMG_InitJPG() == 0) {
slouken@205
    78
			result |= IMG_INIT_JPG;
slouken@205
    79
		}
slouken@205
    80
	}
slouken@205
    81
	if ((flags & IMG_INIT_PNG) && !(initialized & IMG_INIT_PNG)) {
slouken@205
    82
		if (IMG_InitPNG() == 0) {
slouken@205
    83
			result |= IMG_INIT_PNG;
slouken@205
    84
		}
slouken@205
    85
	}
slouken@205
    86
	if ((flags & IMG_INIT_TIF) && !(initialized & IMG_INIT_TIF)) {
slouken@205
    87
		if (IMG_InitTIF() == 0) {
slouken@205
    88
			result |= IMG_INIT_TIF;
slouken@205
    89
		}
slouken@205
    90
	}
slouken@205
    91
	initialized |= result;
slouken@205
    92
slouken@205
    93
	return (result);
slouken@205
    94
}
slouken@205
    95
slouken@205
    96
void IMG_Quit()
slouken@205
    97
{
slouken@205
    98
	if (initialized & IMG_INIT_JPG) {
slouken@205
    99
		IMG_QuitJPG();
slouken@205
   100
	}
slouken@205
   101
	if (initialized & IMG_INIT_PNG) {
slouken@205
   102
		IMG_QuitPNG();
slouken@205
   103
	}
slouken@205
   104
	if (initialized & IMG_INIT_TIF) {
slouken@205
   105
		IMG_QuitTIF();
slouken@205
   106
	}
slouken@205
   107
	initialized = 0;
slouken@205
   108
}
slouken@205
   109
slouken@187
   110
#if !defined(__APPLE__) || defined(SDL_IMAGE_USE_COMMON_BACKEND)
slouken@0
   111
/* Load an image from a file */
slouken@0
   112
SDL_Surface *IMG_Load(const char *file)
slouken@0
   113
{
slouken@0
   114
    SDL_RWops *src = SDL_RWFromFile(file, "rb");
slouken@0
   115
    char *ext = strrchr(file, '.');
slouken@95
   116
    if(ext) {
slouken@95
   117
        ext++;
slouken@95
   118
    }
slouken@95
   119
    if(!src) {
slouken@95
   120
        /* The error message has been set in SDL_RWFromFile */
slouken@95
   121
        return NULL;
slouken@95
   122
    }
slouken@0
   123
    return IMG_LoadTyped_RW(src, 1, ext);
slouken@0
   124
}
slouken@187
   125
#endif
slouken@0
   126
slouken@0
   127
/* Load an image from an SDL datasource (for compatibility) */
slouken@0
   128
SDL_Surface *IMG_Load_RW(SDL_RWops *src, int freesrc)
slouken@0
   129
{
slouken@0
   130
    return IMG_LoadTyped_RW(src, freesrc, NULL);
slouken@0
   131
}
slouken@0
   132
slouken@0
   133
/* Portable case-insensitive string compare function */
slouken@50
   134
static int IMG_string_equals(const char *str1, const char *str2)
slouken@0
   135
{
slouken@0
   136
	while ( *str1 && *str2 ) {
slouken@0
   137
		if ( toupper((unsigned char)*str1) !=
slouken@0
   138
		     toupper((unsigned char)*str2) )
slouken@0
   139
			break;
slouken@0
   140
		++str1;
slouken@0
   141
		++str2;
slouken@0
   142
	}
slouken@0
   143
	return (!*str1 && !*str2);
slouken@0
   144
}
slouken@0
   145
slouken@0
   146
/* Load an image from an SDL datasource, optionally specifying the type */
slouken@0
   147
SDL_Surface *IMG_LoadTyped_RW(SDL_RWops *src, int freesrc, char *type)
slouken@0
   148
{
slouken@117
   149
	int i;
slouken@0
   150
	SDL_Surface *image;
slouken@0
   151
slouken@0
   152
	/* Make sure there is something to do.. */
slouken@0
   153
	if ( src == NULL ) {
slouken@95
   154
		IMG_SetError("Passed a NULL data source");
slouken@0
   155
		return(NULL);
slouken@0
   156
	}
slouken@0
   157
slouken@0
   158
	/* See whether or not this data source can handle seeking */
slouken@0
   159
	if ( SDL_RWseek(src, 0, SEEK_CUR) < 0 ) {
slouken@0
   160
		IMG_SetError("Can't seek in this data source");
slouken@78
   161
		if(freesrc)
slouken@78
   162
			SDL_RWclose(src);
slouken@0
   163
		return(NULL);
slouken@0
   164
	}
slouken@0
   165
slouken@0
   166
	/* Detect the type of image being loaded */
slouken@0
   167
	image = NULL;
slouken@11
   168
	for ( i=0; i < ARRAYSIZE(supported); ++i ) {
slouken@62
   169
		if(supported[i].is) {
slouken@62
   170
			if(!supported[i].is(src))
slouken@62
   171
				continue;
slouken@62
   172
		} else {
slouken@62
   173
			/* magicless format */
slouken@62
   174
			if(!type
slouken@62
   175
			   || !IMG_string_equals(type, supported[i].type))
slouken@62
   176
				continue;
slouken@62
   177
		}
slouken@0
   178
#ifdef DEBUG_IMGLIB
slouken@62
   179
		fprintf(stderr, "IMGLIB: Loading image as %s\n",
slouken@62
   180
			supported[i].type);
slouken@0
   181
#endif
slouken@62
   182
		image = supported[i].load(src);
slouken@62
   183
		if(freesrc)
slouken@62
   184
			SDL_RWclose(src);
slouken@62
   185
		return image;
slouken@0
   186
	}
slouken@0
   187
slouken@0
   188
	if ( freesrc ) {
slouken@0
   189
		SDL_RWclose(src);
slouken@0
   190
	}
slouken@62
   191
	IMG_SetError("Unsupported image format");
slouken@62
   192
	return NULL;
slouken@0
   193
}
slouken@0
   194
slouken@0
   195
/* Invert the alpha of a surface for use with OpenGL
slouken@4
   196
   This function is a no-op and only kept for backwards compatibility.
slouken@0
   197
 */
slouken@0
   198
int IMG_InvertAlpha(int on)
slouken@0
   199
{
slouken@4
   200
    return 1;
slouken@0
   201
}