Skip to content

Latest commit

 

History

History
201 lines (176 loc) · 4.94 KB

IMG.c

File metadata and controls

201 lines (176 loc) · 4.94 KB
 
Aug 10, 2000
Aug 10, 2000
1
/*
Dec 14, 2001
Dec 14, 2001
2
SDL_image: An example image loading library for use with SDL
Dec 8, 2008
Dec 8, 2008
3
Copyright (C) 1997-2009 Sam Lantinga
Aug 10, 2000
Aug 10, 2000
4
5
This library is free software; you can redistribute it and/or
Feb 4, 2006
Feb 4, 2006
6
modify it under the terms of the GNU Lesser General Public
Aug 10, 2000
Aug 10, 2000
7
License as published by the Free Software Foundation; either
Feb 4, 2006
Feb 4, 2006
8
version 2.1 of the License, or (at your option) any later version.
Aug 10, 2000
Aug 10, 2000
9
10
11
12
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Feb 4, 2006
Feb 4, 2006
13
Lesser General Public License for more details.
Aug 10, 2000
Aug 10, 2000
14
Feb 4, 2006
Feb 4, 2006
15
16
17
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Aug 10, 2000
Aug 10, 2000
18
19
Sam Lantinga
Dec 14, 2001
Dec 14, 2001
20
slouken@libsdl.org
Aug 10, 2000
Aug 10, 2000
21
22
23
24
25
26
27
28
29
30
*/
/* A simple library to load images of various formats as SDL surfaces */
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "SDL_image.h"
Nov 29, 2000
Nov 29, 2000
31
32
#define ARRAYSIZE(a) (sizeof(a) / sizeof((a)[0]))
Aug 10, 2000
Aug 10, 2000
33
34
35
/* Table of image detection and loading functions */
static struct {
char *type;
Nov 23, 2005
Nov 23, 2005
36
37
int (SDLCALL *is)(SDL_RWops *src);
SDL_Surface *(SDLCALL *load)(SDL_RWops *src);
Aug 10, 2000
Aug 10, 2000
38
} supported[] = {
Mar 7, 2001
Mar 7, 2001
39
/* keep magicless formats first */
Dec 19, 2003
Dec 19, 2003
40
{ "TGA", NULL, IMG_LoadTGA_RW },
Jan 13, 2009
Jan 13, 2009
41
42
{ "CUR", IMG_isCUR, IMG_LoadCUR_RW },
{ "ICO", IMG_isICO, IMG_LoadICO_RW },
Aug 10, 2000
Aug 10, 2000
43
44
45
{ "BMP", IMG_isBMP, IMG_LoadBMP_RW },
{ "GIF", IMG_isGIF, IMG_LoadGIF_RW },
{ "JPG", IMG_isJPG, IMG_LoadJPG_RW },
Sep 23, 2001
Sep 23, 2001
46
{ "LBM", IMG_isLBM, IMG_LoadLBM_RW },
Feb 4, 2006
Feb 4, 2006
47
48
49
50
51
52
53
{ "PCX", IMG_isPCX, IMG_LoadPCX_RW },
{ "PNG", IMG_isPNG, IMG_LoadPNG_RW },
{ "PNM", IMG_isPNM, IMG_LoadPNM_RW }, /* P[BGP]M share code */
{ "TIF", IMG_isTIF, IMG_LoadTIF_RW },
{ "XCF", IMG_isXCF, IMG_LoadXCF_RW },
{ "XPM", IMG_isXPM, IMG_LoadXPM_RW },
{ "XV", IMG_isXV, IMG_LoadXV_RW }
Aug 10, 2000
Aug 10, 2000
54
55
};
Jul 23, 2003
Jul 23, 2003
56
57
58
59
60
61
62
const SDL_version *IMG_Linked_Version(void)
{
static SDL_version linked_version;
SDL_IMAGE_VERSION(&linked_version);
return(&linked_version);
}
Sep 26, 2009
Sep 26, 2009
63
extern int IMG_InitJPG();
Nov 8, 2009
Nov 8, 2009
64
extern void IMG_QuitJPG();
Sep 26, 2009
Sep 26, 2009
65
extern int IMG_InitPNG();
Nov 8, 2009
Nov 8, 2009
66
extern void IMG_QuitPNG();
Sep 26, 2009
Sep 26, 2009
67
extern int IMG_InitTIF();
Nov 8, 2009
Nov 8, 2009
68
extern void IMG_QuitTIF();
Sep 26, 2009
Sep 26, 2009
69
70
71
72
73
74
75
static int initialized = 0;
int IMG_Init(int flags)
{
int result = 0;
Nov 14, 2009
Nov 14, 2009
76
77
if (flags & IMG_INIT_JPG) {
if ((initialized & IMG_INIT_JPG) || IMG_InitJPG() == 0) {
Sep 26, 2009
Sep 26, 2009
78
79
80
result |= IMG_INIT_JPG;
}
}
Nov 14, 2009
Nov 14, 2009
81
82
if (flags & IMG_INIT_PNG) {
if ((initialized & IMG_INIT_PNG) || IMG_InitPNG() == 0) {
Sep 26, 2009
Sep 26, 2009
83
84
85
result |= IMG_INIT_PNG;
}
}
Nov 14, 2009
Nov 14, 2009
86
87
if (flags & IMG_INIT_TIF) {
if ((initialized & IMG_INIT_TIF) || IMG_InitTIF() == 0) {
Sep 26, 2009
Sep 26, 2009
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
result |= IMG_INIT_TIF;
}
}
initialized |= result;
return (result);
}
void IMG_Quit()
{
if (initialized & IMG_INIT_JPG) {
IMG_QuitJPG();
}
if (initialized & IMG_INIT_PNG) {
IMG_QuitPNG();
}
if (initialized & IMG_INIT_TIF) {
IMG_QuitTIF();
}
initialized = 0;
}
Jan 4, 2009
Jan 4, 2009
110
#if !defined(__APPLE__) || defined(SDL_IMAGE_USE_COMMON_BACKEND)
Aug 10, 2000
Aug 10, 2000
111
112
113
114
115
/* Load an image from a file */
SDL_Surface *IMG_Load(const char *file)
{
SDL_RWops *src = SDL_RWFromFile(file, "rb");
char *ext = strrchr(file, '.');
Dec 19, 2003
Dec 19, 2003
116
117
118
119
120
121
122
if(ext) {
ext++;
}
if(!src) {
/* The error message has been set in SDL_RWFromFile */
return NULL;
}
Aug 10, 2000
Aug 10, 2000
123
124
return IMG_LoadTyped_RW(src, 1, ext);
}
Jan 4, 2009
Jan 4, 2009
125
#endif
Aug 10, 2000
Aug 10, 2000
126
127
128
129
130
131
132
133
/* Load an image from an SDL datasource (for compatibility) */
SDL_Surface *IMG_Load_RW(SDL_RWops *src, int freesrc)
{
return IMG_LoadTyped_RW(src, freesrc, NULL);
}
/* Portable case-insensitive string compare function */
Nov 20, 2001
Nov 20, 2001
134
static int IMG_string_equals(const char *str1, const char *str2)
Aug 10, 2000
Aug 10, 2000
135
136
137
138
139
140
141
142
143
144
145
146
147
148
{
while ( *str1 && *str2 ) {
if ( toupper((unsigned char)*str1) !=
toupper((unsigned char)*str2) )
break;
++str1;
++str2;
}
return (!*str1 && !*str2);
}
/* Load an image from an SDL datasource, optionally specifying the type */
SDL_Surface *IMG_LoadTyped_RW(SDL_RWops *src, int freesrc, char *type)
{
Feb 4, 2006
Feb 4, 2006
149
int i;
Aug 10, 2000
Aug 10, 2000
150
151
152
153
SDL_Surface *image;
/* Make sure there is something to do.. */
if ( src == NULL ) {
Dec 19, 2003
Dec 19, 2003
154
IMG_SetError("Passed a NULL data source");
Aug 10, 2000
Aug 10, 2000
155
156
157
158
return(NULL);
}
/* See whether or not this data source can handle seeking */
Nov 8, 2009
Nov 8, 2009
159
if ( SDL_RWseek(src, 0, RW_SEEK_CUR) < 0 ) {
Aug 10, 2000
Aug 10, 2000
160
IMG_SetError("Can't seek in this data source");
Feb 9, 2003
Feb 9, 2003
161
162
if(freesrc)
SDL_RWclose(src);
Aug 10, 2000
Aug 10, 2000
163
164
165
166
167
return(NULL);
}
/* Detect the type of image being loaded */
image = NULL;
Dec 6, 2000
Dec 6, 2000
168
for ( i=0; i < ARRAYSIZE(supported); ++i ) {
Jan 4, 2002
Jan 4, 2002
169
170
171
172
173
174
175
176
if(supported[i].is) {
if(!supported[i].is(src))
continue;
} else {
/* magicless format */
if(!type
|| !IMG_string_equals(type, supported[i].type))
continue;
Aug 10, 2000
Aug 10, 2000
177
}
Jan 4, 2002
Jan 4, 2002
178
179
180
181
182
183
184
185
#ifdef DEBUG_IMGLIB
fprintf(stderr, "IMGLIB: Loading image as %s\n",
supported[i].type);
#endif
image = supported[i].load(src);
if(freesrc)
SDL_RWclose(src);
return image;
Aug 10, 2000
Aug 10, 2000
186
187
188
189
190
}
if ( freesrc ) {
SDL_RWclose(src);
}
Jan 4, 2002
Jan 4, 2002
191
192
IMG_SetError("Unsupported image format");
return NULL;
Aug 10, 2000
Aug 10, 2000
193
194
195
}
/* Invert the alpha of a surface for use with OpenGL
Sep 1, 2000
Sep 1, 2000
196
This function is a no-op and only kept for backwards compatibility.
Aug 10, 2000
Aug 10, 2000
197
198
199
*/
int IMG_InvertAlpha(int on)
{
Sep 1, 2000
Sep 1, 2000
200
return 1;
Aug 10, 2000
Aug 10, 2000
201
}