2 SDL_image: An example image loading library for use with SDL
3 Copyright (C) 1997-2009 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 /* A simple library to load images of various formats as SDL surfaces */
29 #include "SDL_image.h"
31 #define ARRAYSIZE(a) (sizeof(a) / sizeof((a)[0]))
33 /* Table of image detection and loading functions */
36 int (SDLCALL *is)(SDL_RWops *src);
37 SDL_Surface *(SDLCALL *load)(SDL_RWops *src);
39 /* keep magicless formats first */
40 { "TGA", NULL, IMG_LoadTGA_RW },
41 { "CUR", IMG_isCUR, IMG_LoadCUR_RW },
42 { "ICO", IMG_isICO, IMG_LoadICO_RW },
43 { "BMP", IMG_isBMP, IMG_LoadBMP_RW },
44 { "GIF", IMG_isGIF, IMG_LoadGIF_RW },
45 { "JPG", IMG_isJPG, IMG_LoadJPG_RW },
46 { "LBM", IMG_isLBM, IMG_LoadLBM_RW },
47 { "PCX", IMG_isPCX, IMG_LoadPCX_RW },
48 { "PNG", IMG_isPNG, IMG_LoadPNG_RW },
49 { "PNM", IMG_isPNM, IMG_LoadPNM_RW }, /* P[BGP]M share code */
50 { "TIF", IMG_isTIF, IMG_LoadTIF_RW },
51 { "XCF", IMG_isXCF, IMG_LoadXCF_RW },
52 { "XPM", IMG_isXPM, IMG_LoadXPM_RW },
53 { "XV", IMG_isXV, IMG_LoadXV_RW }
56 const SDL_version *IMG_Linked_Version(void)
58 static SDL_version linked_version;
59 SDL_IMAGE_VERSION(&linked_version);
60 return(&linked_version);
63 extern int IMG_InitJPG();
64 extern int IMG_QuitJPG();
65 extern int IMG_InitPNG();
66 extern int IMG_QuitPNG();
67 extern int IMG_InitTIF();
68 extern int IMG_QuitTIF();
70 static int initialized = 0;
72 int IMG_Init(int flags)
74 #if defined(__APPLE__) && !defined(SDL_IMAGE_USE_COMMON_BACKEND)
75 initialized = IMG_INIT_JPG | IMG_INIT_PNG | IMG_INIT_TIF;
80 if ((flags & IMG_INIT_JPG) && !(initialized & IMG_INIT_JPG)) {
81 if (IMG_InitJPG() == 0) {
82 result |= IMG_INIT_JPG;
85 if ((flags & IMG_INIT_PNG) && !(initialized & IMG_INIT_PNG)) {
86 if (IMG_InitPNG() == 0) {
87 result |= IMG_INIT_PNG;
90 if ((flags & IMG_INIT_TIF) && !(initialized & IMG_INIT_TIF)) {
91 if (IMG_InitTIF() == 0) {
92 result |= IMG_INIT_TIF;
95 initialized |= result;
103 #if !defined(__APPLE__) || defined(SDL_IMAGE_USE_COMMON_BACKEND)
104 if (initialized & IMG_INIT_JPG) {
107 if (initialized & IMG_INIT_PNG) {
110 if (initialized & IMG_INIT_TIF) {
117 #if !defined(__APPLE__) || defined(SDL_IMAGE_USE_COMMON_BACKEND)
118 /* Load an image from a file */
119 SDL_Surface *IMG_Load(const char *file)
121 SDL_RWops *src = SDL_RWFromFile(file, "rb");
122 char *ext = strrchr(file, '.');
127 /* The error message has been set in SDL_RWFromFile */
130 return IMG_LoadTyped_RW(src, 1, ext);
134 /* Load an image from an SDL datasource (for compatibility) */
135 SDL_Surface *IMG_Load_RW(SDL_RWops *src, int freesrc)
137 return IMG_LoadTyped_RW(src, freesrc, NULL);
140 /* Portable case-insensitive string compare function */
141 static int IMG_string_equals(const char *str1, const char *str2)
143 while ( *str1 && *str2 ) {
144 if ( toupper((unsigned char)*str1) !=
145 toupper((unsigned char)*str2) )
150 return (!*str1 && !*str2);
153 /* Load an image from an SDL datasource, optionally specifying the type */
154 SDL_Surface *IMG_LoadTyped_RW(SDL_RWops *src, int freesrc, char *type)
159 /* Make sure there is something to do.. */
161 IMG_SetError("Passed a NULL data source");
165 /* See whether or not this data source can handle seeking */
166 if ( SDL_RWseek(src, 0, SEEK_CUR) < 0 ) {
167 IMG_SetError("Can't seek in this data source");
173 /* Detect the type of image being loaded */
175 for ( i=0; i < ARRAYSIZE(supported); ++i ) {
176 if(supported[i].is) {
177 if(!supported[i].is(src))
180 /* magicless format */
182 || !IMG_string_equals(type, supported[i].type))
186 fprintf(stderr, "IMGLIB: Loading image as %s\n",
189 image = supported[i].load(src);
198 IMG_SetError("Unsupported image format");
202 /* Invert the alpha of a surface for use with OpenGL
203 This function is a no-op and only kept for backwards compatibility.
205 int IMG_InvertAlpha(int on)