]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/libsdl-image/contrib/IMG.c
e7a96cfd4e7404a485e4fa121d14247a18be4b5b
[l4.git] / l4 / pkg / libsdl-image / contrib / IMG.c
1 /*
2     SDL_image:  An example image loading library for use with SDL
3     Copyright (C) 1997-2009 Sam Lantinga
4
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.
9
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.
14
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
18
19     Sam Lantinga
20     slouken@libsdl.org
21 */
22
23 /* A simple library to load images of various formats as SDL surfaces */
24
25 #include <stdio.h>
26 #include <string.h>
27 #include <ctype.h>
28
29 #include "SDL_image.h"
30
31 #define ARRAYSIZE(a) (sizeof(a) / sizeof((a)[0]))
32
33 /* Table of image detection and loading functions */
34 static struct {
35         char *type;
36         int (SDLCALL *is)(SDL_RWops *src);
37         SDL_Surface *(SDLCALL *load)(SDL_RWops *src);
38 } supported[] = {
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  }
54 };
55
56 const SDL_version *IMG_Linked_Version(void)
57 {
58         static SDL_version linked_version;
59         SDL_IMAGE_VERSION(&linked_version);
60         return(&linked_version);
61 }
62
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();
69
70 static int initialized = 0;
71
72 int IMG_Init(int flags)
73 {
74 #if defined(__APPLE__) && !defined(SDL_IMAGE_USE_COMMON_BACKEND)
75         initialized = IMG_INIT_JPG | IMG_INIT_PNG | IMG_INIT_TIF;
76         return initialized;
77 #else
78         int result = 0;
79
80         if ((flags & IMG_INIT_JPG) && !(initialized & IMG_INIT_JPG)) {
81                 if (IMG_InitJPG() == 0) {
82                         result |= IMG_INIT_JPG;
83                 }
84         }
85         if ((flags & IMG_INIT_PNG) && !(initialized & IMG_INIT_PNG)) {
86                 if (IMG_InitPNG() == 0) {
87                         result |= IMG_INIT_PNG;
88                 }
89         }
90         if ((flags & IMG_INIT_TIF) && !(initialized & IMG_INIT_TIF)) {
91                 if (IMG_InitTIF() == 0) {
92                         result |= IMG_INIT_TIF;
93                 }
94         }
95         initialized |= result;
96
97         return (result);
98 #endif
99 }
100
101 void IMG_Quit()
102 {
103 #if !defined(__APPLE__) || defined(SDL_IMAGE_USE_COMMON_BACKEND)
104         if (initialized & IMG_INIT_JPG) {
105                 IMG_QuitJPG();
106         }
107         if (initialized & IMG_INIT_PNG) {
108                 IMG_QuitPNG();
109         }
110         if (initialized & IMG_INIT_TIF) {
111                 IMG_QuitTIF();
112         }
113 #endif
114         initialized = 0;
115 }
116
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)
120 {
121     SDL_RWops *src = SDL_RWFromFile(file, "rb");
122     char *ext = strrchr(file, '.');
123     if(ext) {
124         ext++;
125     }
126     if(!src) {
127         /* The error message has been set in SDL_RWFromFile */
128         return NULL;
129     }
130     return IMG_LoadTyped_RW(src, 1, ext);
131 }
132 #endif
133
134 /* Load an image from an SDL datasource (for compatibility) */
135 SDL_Surface *IMG_Load_RW(SDL_RWops *src, int freesrc)
136 {
137     return IMG_LoadTyped_RW(src, freesrc, NULL);
138 }
139
140 /* Portable case-insensitive string compare function */
141 static int IMG_string_equals(const char *str1, const char *str2)
142 {
143         while ( *str1 && *str2 ) {
144                 if ( toupper((unsigned char)*str1) !=
145                      toupper((unsigned char)*str2) )
146                         break;
147                 ++str1;
148                 ++str2;
149         }
150         return (!*str1 && !*str2);
151 }
152
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)
155 {
156         int i;
157         SDL_Surface *image;
158
159         /* Make sure there is something to do.. */
160         if ( src == NULL ) {
161                 IMG_SetError("Passed a NULL data source");
162                 return(NULL);
163         }
164
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");
168                 if(freesrc)
169                         SDL_RWclose(src);
170                 return(NULL);
171         }
172
173         /* Detect the type of image being loaded */
174         image = NULL;
175         for ( i=0; i < ARRAYSIZE(supported); ++i ) {
176                 if(supported[i].is) {
177                         if(!supported[i].is(src))
178                                 continue;
179                 } else {
180                         /* magicless format */
181                         if(!type
182                            || !IMG_string_equals(type, supported[i].type))
183                                 continue;
184                 }
185 #ifdef DEBUG_IMGLIB
186                 fprintf(stderr, "IMGLIB: Loading image as %s\n",
187                         supported[i].type);
188 #endif
189                 image = supported[i].load(src);
190                 if(freesrc)
191                         SDL_RWclose(src);
192                 return image;
193         }
194
195         if ( freesrc ) {
196                 SDL_RWclose(src);
197         }
198         IMG_SetError("Unsupported image format");
199         return NULL;
200 }
201
202 /* Invert the alpha of a surface for use with OpenGL
203    This function is a no-op and only kept for backwards compatibility.
204  */
205 int IMG_InvertAlpha(int on)
206 {
207     return 1;
208 }