Skip to content

Latest commit

 

History

History
212 lines (187 loc) · 5.31 KB

IMG.c

File metadata and controls

212 lines (187 loc) · 5.31 KB
 
Aug 10, 2000
Aug 10, 2000
1
/*
Dec 31, 2011
Dec 31, 2011
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
SDL_image: An example image loading library for use with SDL
Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
Aug 10, 2000
Aug 10, 2000
20
21
22
23
24
25
26
27
28
29
*/
/* 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
30
31
#define ARRAYSIZE(a) (sizeof(a) / sizeof((a)[0]))
Aug 10, 2000
Aug 10, 2000
32
33
/* Table of image detection and loading functions */
static struct {
Oct 16, 2018
Oct 16, 2018
34
const char *type;
Nov 23, 2005
Nov 23, 2005
35
36
int (SDLCALL *is)(SDL_RWops *src);
SDL_Surface *(SDLCALL *load)(SDL_RWops *src);
Aug 10, 2000
Aug 10, 2000
37
} supported[] = {
Mar 7, 2001
Mar 7, 2001
38
/* keep magicless formats first */
Dec 19, 2003
Dec 19, 2003
39
{ "TGA", NULL, IMG_LoadTGA_RW },
Jan 13, 2009
Jan 13, 2009
40
41
{ "CUR", IMG_isCUR, IMG_LoadCUR_RW },
{ "ICO", IMG_isICO, IMG_LoadICO_RW },
Aug 10, 2000
Aug 10, 2000
42
43
44
{ "BMP", IMG_isBMP, IMG_LoadBMP_RW },
{ "GIF", IMG_isGIF, IMG_LoadGIF_RW },
{ "JPG", IMG_isJPG, IMG_LoadJPG_RW },
Sep 23, 2001
Sep 23, 2001
45
{ "LBM", IMG_isLBM, IMG_LoadLBM_RW },
Feb 4, 2006
Feb 4, 2006
46
47
48
49
50
51
{ "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 },
Dec 30, 2011
Dec 30, 2011
52
53
{ "XV", IMG_isXV, IMG_LoadXV_RW },
{ "WEBP", IMG_isWEBP, IMG_LoadWEBP_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
Dec 30, 2011
Dec 30, 2011
70
71
72
extern int IMG_InitWEBP();
extern void IMG_QuitWEBP();
Sep 26, 2009
Sep 26, 2009
73
74
75
76
77
78
static int initialized = 0;
int IMG_Init(int flags)
{
int result = 0;
Nov 14, 2009
Nov 14, 2009
79
80
if (flags & IMG_INIT_JPG) {
if ((initialized & IMG_INIT_JPG) || IMG_InitJPG() == 0) {
Sep 26, 2009
Sep 26, 2009
81
82
83
result |= IMG_INIT_JPG;
}
}
Nov 14, 2009
Nov 14, 2009
84
85
if (flags & IMG_INIT_PNG) {
if ((initialized & IMG_INIT_PNG) || IMG_InitPNG() == 0) {
Sep 26, 2009
Sep 26, 2009
86
87
88
result |= IMG_INIT_PNG;
}
}
Nov 14, 2009
Nov 14, 2009
89
90
if (flags & IMG_INIT_TIF) {
if ((initialized & IMG_INIT_TIF) || IMG_InitTIF() == 0) {
Sep 26, 2009
Sep 26, 2009
91
92
93
result |= IMG_INIT_TIF;
}
}
Dec 30, 2011
Dec 30, 2011
94
95
96
97
98
if (flags & IMG_INIT_WEBP) {
if ((initialized & IMG_INIT_WEBP) || IMG_InitWEBP() == 0) {
result |= IMG_INIT_WEBP;
}
}
Sep 26, 2009
Sep 26, 2009
99
100
initialized |= result;
Feb 13, 2011
Feb 13, 2011
101
return (initialized);
Sep 26, 2009
Sep 26, 2009
102
103
104
105
106
107
108
109
110
111
112
113
114
}
void IMG_Quit()
{
if (initialized & IMG_INIT_JPG) {
IMG_QuitJPG();
}
if (initialized & IMG_INIT_PNG) {
IMG_QuitPNG();
}
if (initialized & IMG_INIT_TIF) {
IMG_QuitTIF();
}
Dec 30, 2011
Dec 30, 2011
115
116
117
if (initialized & IMG_INIT_WEBP) {
IMG_QuitWEBP();
}
Sep 26, 2009
Sep 26, 2009
118
119
120
initialized = 0;
}
Jan 4, 2009
Jan 4, 2009
121
#if !defined(__APPLE__) || defined(SDL_IMAGE_USE_COMMON_BACKEND)
Aug 10, 2000
Aug 10, 2000
122
123
124
125
126
/* 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
127
128
129
130
131
132
133
if(ext) {
ext++;
}
if(!src) {
/* The error message has been set in SDL_RWFromFile */
return NULL;
}
Aug 10, 2000
Aug 10, 2000
134
135
return IMG_LoadTyped_RW(src, 1, ext);
}
Jan 4, 2009
Jan 4, 2009
136
#endif
Aug 10, 2000
Aug 10, 2000
137
138
139
140
141
142
143
144
/* 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
145
static int IMG_string_equals(const char *str1, const char *str2)
Aug 10, 2000
Aug 10, 2000
146
147
148
149
150
151
152
153
154
155
156
157
158
159
{
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
160
int i;
Aug 10, 2000
Aug 10, 2000
161
162
163
164
SDL_Surface *image;
/* Make sure there is something to do.. */
if ( src == NULL ) {
Dec 19, 2003
Dec 19, 2003
165
IMG_SetError("Passed a NULL data source");
Aug 10, 2000
Aug 10, 2000
166
167
168
169
return(NULL);
}
/* See whether or not this data source can handle seeking */
Nov 8, 2009
Nov 8, 2009
170
if ( SDL_RWseek(src, 0, RW_SEEK_CUR) < 0 ) {
Aug 10, 2000
Aug 10, 2000
171
IMG_SetError("Can't seek in this data source");
Feb 9, 2003
Feb 9, 2003
172
173
if(freesrc)
SDL_RWclose(src);
Aug 10, 2000
Aug 10, 2000
174
175
176
177
178
return(NULL);
}
/* Detect the type of image being loaded */
image = NULL;
Dec 6, 2000
Dec 6, 2000
179
for ( i=0; i < ARRAYSIZE(supported); ++i ) {
Jan 4, 2002
Jan 4, 2002
180
181
182
183
184
185
186
187
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
188
}
Jan 4, 2002
Jan 4, 2002
189
190
191
192
193
194
195
196
#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
197
198
199
200
201
}
if ( freesrc ) {
SDL_RWclose(src);
}
Jan 4, 2002
Jan 4, 2002
202
203
IMG_SetError("Unsupported image format");
return NULL;
Aug 10, 2000
Aug 10, 2000
204
205
206
}
/* Invert the alpha of a surface for use with OpenGL
Sep 1, 2000
Sep 1, 2000
207
This function is a no-op and only kept for backwards compatibility.
Aug 10, 2000
Aug 10, 2000
208
209
210
*/
int IMG_InvertAlpha(int on)
{
Sep 1, 2000
Sep 1, 2000
211
return 1;
Aug 10, 2000
Aug 10, 2000
212
}