/
IMG.c
288 lines (252 loc) · 7.9 KB
1
/*
2
SDL_image: An example image loading library for use with SDL
3
Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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.
20
21
22
23
24
25
*/
/* A simple library to load images of various formats as SDL surfaces */
#include "SDL_image.h"
26
27
28
29
#ifdef __EMSCRIPTEN__
#include <emscripten/emscripten.h>
#endif
30
31
#define ARRAYSIZE(a) (sizeof(a) / sizeof((a)[0]))
32
33
/* Table of image detection and loading functions */
static struct {
34
const char *type;
35
36
int (SDLCALL *is)(SDL_RWops *src);
SDL_Surface *(SDLCALL *load)(SDL_RWops *src);
37
} supported[] = {
38
39
40
41
42
43
44
45
46
47
48
/* keep magicless formats first */
{ "TGA", NULL, IMG_LoadTGA_RW },
{ "CUR", IMG_isCUR, IMG_LoadCUR_RW },
{ "ICO", IMG_isICO, IMG_LoadICO_RW },
{ "BMP", IMG_isBMP, IMG_LoadBMP_RW },
{ "GIF", IMG_isGIF, IMG_LoadGIF_RW },
{ "JPG", IMG_isJPG, IMG_LoadJPG_RW },
{ "LBM", IMG_isLBM, IMG_LoadLBM_RW },
{ "PCX", IMG_isPCX, IMG_LoadPCX_RW },
{ "PNG", IMG_isPNG, IMG_LoadPNG_RW },
{ "PNM", IMG_isPNM, IMG_LoadPNM_RW }, /* P[BGP]M share code */
49
{ "SVG", IMG_isSVG, IMG_LoadSVG_RW },
50
51
52
53
54
{ "TIF", IMG_isTIF, IMG_LoadTIF_RW },
{ "XCF", IMG_isXCF, IMG_LoadXCF_RW },
{ "XPM", IMG_isXPM, IMG_LoadXPM_RW },
{ "XV", IMG_isXV, IMG_LoadXV_RW },
{ "WEBP", IMG_isWEBP, IMG_LoadWEBP_RW },
55
56
};
57
58
const SDL_version *IMG_Linked_Version(void)
{
59
60
61
static SDL_version linked_version;
SDL_IMAGE_VERSION(&linked_version);
return(&linked_version);
62
63
}
64
65
66
67
68
69
extern int IMG_InitJPG(void);
extern void IMG_QuitJPG(void);
extern int IMG_InitPNG(void);
extern void IMG_QuitPNG(void);
extern int IMG_InitTIF(void);
extern void IMG_QuitTIF(void);
70
71
72
extern int IMG_InitWEBP(void);
extern void IMG_QuitWEBP(void);
73
74
75
76
77
static int initialized = 0;
int IMG_Init(int flags)
{
78
79
int result = 0;
80
81
82
83
84
/* Passing 0 returns the currently initialized loaders */
if (!flags) {
return initialized;
}
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
if (flags & IMG_INIT_JPG) {
if ((initialized & IMG_INIT_JPG) || IMG_InitJPG() == 0) {
result |= IMG_INIT_JPG;
}
}
if (flags & IMG_INIT_PNG) {
if ((initialized & IMG_INIT_PNG) || IMG_InitPNG() == 0) {
result |= IMG_INIT_PNG;
}
}
if (flags & IMG_INIT_TIF) {
if ((initialized & IMG_INIT_TIF) || IMG_InitTIF() == 0) {
result |= IMG_INIT_TIF;
}
}
if (flags & IMG_INIT_WEBP) {
if ((initialized & IMG_INIT_WEBP) || IMG_InitWEBP() == 0) {
result |= IMG_INIT_WEBP;
}
}
initialized |= result;
107
return result;
108
109
110
111
}
void IMG_Quit()
{
112
113
114
115
116
117
118
119
120
121
122
123
124
if (initialized & IMG_INIT_JPG) {
IMG_QuitJPG();
}
if (initialized & IMG_INIT_PNG) {
IMG_QuitPNG();
}
if (initialized & IMG_INIT_TIF) {
IMG_QuitTIF();
}
if (initialized & IMG_INIT_WEBP) {
IMG_QuitWEBP();
}
initialized = 0;
125
126
}
127
#if !defined(__APPLE__) || defined(SDL_IMAGE_USE_COMMON_BACKEND)
128
129
130
/* Load an image from a file */
SDL_Surface *IMG_Load(const char *file)
{
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#if __EMSCRIPTEN__
int w, h;
char *data;
SDL_Surface *surf;
data = emscripten_get_preloaded_image_data(file, &w, &h);
if (data != NULL) {
surf = SDL_CreateRGBSurface(0, w, h, 32, 0xFF, 0xFF00, 0xFF0000, 0xFF000000);
if (surf != NULL) {
memcpy(surf->pixels, data, w * h * 4);
}
free(data);
return surf;
}
#endif
147
SDL_RWops *src = SDL_RWFromFile(file, "rb");
148
const char *ext = SDL_strrchr(file, '.');
149
150
151
152
153
154
155
if(ext) {
ext++;
}
if(!src) {
/* The error message has been set in SDL_RWFromFile */
return NULL;
}
156
157
return IMG_LoadTyped_RW(src, 1, ext);
}
158
#endif
159
160
161
162
163
164
165
166
/* 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 */
167
static int IMG_string_equals(const char *str1, const char *str2)
168
{
169
while ( *str1 && *str2 ) {
170
171
if ( SDL_toupper((unsigned char)*str1) !=
SDL_toupper((unsigned char)*str2) )
172
173
174
175
176
break;
++str1;
++str2;
}
return (!*str1 && !*str2);
177
178
179
}
/* Load an image from an SDL datasource, optionally specifying the type */
180
SDL_Surface *IMG_LoadTyped_RW(SDL_RWops *src, int freesrc, const char *type)
181
{
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
int i;
SDL_Surface *image;
/* Make sure there is something to do.. */
if ( src == NULL ) {
IMG_SetError("Passed a NULL data source");
return(NULL);
}
/* See whether or not this data source can handle seeking */
if ( SDL_RWseek(src, 0, RW_SEEK_CUR) < 0 ) {
IMG_SetError("Can't seek in this data source");
if(freesrc)
SDL_RWclose(src);
return(NULL);
}
199
200
201
202
203
204
205
206
207
208
209
210
#ifdef __EMSCRIPTEN__
/*load through preloadedImages*/
if ( src->type == SDL_RWOPS_STDFILE ) {
int w, h, success;
char *data;
SDL_Surface *surf;
data = emscripten_get_preloaded_image_data_from_FILE(src->hidden.stdio.fp, &w, &h);
if(data)
{
211
212
213
214
surf = SDL_CreateRGBSurface(0, w, h, 32, 0xFF, 0xFF00, 0xFF0000, 0xFF000000);
if (surf != NULL) {
memcpy(surf->pixels, data, w * h * 4);
}
215
216
217
218
219
free(data);
if(freesrc)
SDL_RWclose(src);
220
/* If SDL_CreateRGBSurface returns NULL, it has set the error message for us */
221
222
223
224
225
return surf;
}
}
#endif
226
227
228
229
230
231
232
233
234
235
236
237
/* Detect the type of image being loaded */
image = NULL;
for ( i=0; i < ARRAYSIZE(supported); ++i ) {
if(supported[i].is) {
if(!supported[i].is(src))
continue;
} else {
/* magicless format */
if(!type
|| !IMG_string_equals(type, supported[i].type))
continue;
}
238
#ifdef DEBUG_IMGLIB
239
240
fprintf(stderr, "IMGLIB: Loading image as %s\n",
supported[i].type);
241
#endif
242
243
244
245
246
247
248
249
250
251
252
image = supported[i].load(src);
if(freesrc)
SDL_RWclose(src);
return image;
}
if ( freesrc ) {
SDL_RWclose(src);
}
IMG_SetError("Unsupported image format");
return NULL;
253
254
}
255
#if SDL_VERSION_ATLEAST(2,0,0)
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
SDL_Texture *IMG_LoadTexture(SDL_Renderer *renderer, const char *file)
{
SDL_Texture *texture = NULL;
SDL_Surface *surface = IMG_Load(file);
if (surface) {
texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_FreeSurface(surface);
}
return texture;
}
SDL_Texture *IMG_LoadTexture_RW(SDL_Renderer *renderer, SDL_RWops *src, int freesrc)
{
SDL_Texture *texture = NULL;
SDL_Surface *surface = IMG_Load_RW(src, freesrc);
if (surface) {
texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_FreeSurface(surface);
}
return texture;
}
SDL_Texture *IMG_LoadTextureTyped_RW(SDL_Renderer *renderer, SDL_RWops *src, int freesrc, const char *type)
{
SDL_Texture *texture = NULL;
SDL_Surface *surface = IMG_LoadTyped_RW(src, freesrc, type);
if (surface) {
texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_FreeSurface(surface);
}
return texture;
}
288
#endif /* SDL 2.0 */