Skip to content

Latest commit

 

History

History
401 lines (353 loc) · 11.1 KB

IMG.c

File metadata and controls

401 lines (353 loc) · 11.1 KB
 
Aug 10, 2000
Aug 10, 2000
1
/*
Dec 31, 2011
Dec 31, 2011
2
SDL_image: An example image loading library for use with SDL
Jan 17, 2020
Jan 17, 2020
3
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
Dec 31, 2011
Dec 31, 2011
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.
Aug 10, 2000
Aug 10, 2000
20
21
22
23
24
25
*/
/* A simple library to load images of various formats as SDL surfaces */
#include "SDL_image.h"
Jun 10, 2019
Jun 10, 2019
26
27
28
29
#ifdef __EMSCRIPTEN__
#include <emscripten/emscripten.h>
#endif
Nov 29, 2000
Nov 29, 2000
30
Aug 10, 2000
Aug 10, 2000
31
32
/* Table of image detection and loading functions */
static struct {
Sep 12, 2017
Sep 12, 2017
33
const char *type;
May 22, 2013
May 22, 2013
34
35
int (SDLCALL *is)(SDL_RWops *src);
SDL_Surface *(SDLCALL *load)(SDL_RWops *src);
Aug 10, 2000
Aug 10, 2000
36
} supported[] = {
May 22, 2013
May 22, 2013
37
38
39
40
41
42
43
44
45
46
47
/* 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 */
Oct 22, 2017
Oct 22, 2017
48
{ "SVG", IMG_isSVG, IMG_LoadSVG_RW },
May 22, 2013
May 22, 2013
49
50
51
52
53
{ "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 },
Aug 10, 2000
Aug 10, 2000
54
55
};
Oct 4, 2019
Oct 4, 2019
56
57
58
59
60
61
62
63
64
65
/* Table of animation detection and loading functions */
static struct {
const char *type;
int (SDLCALL *is)(SDL_RWops *src);
IMG_Animation *(SDLCALL *load)(SDL_RWops *src);
} supported_anims[] = {
/* keep magicless formats first */
{ "GIF", IMG_isGIF, IMG_LoadGIFAnimation_RW },
};
Jul 23, 2003
Jul 23, 2003
66
67
const SDL_version *IMG_Linked_Version(void)
{
May 22, 2013
May 22, 2013
68
69
70
static SDL_version linked_version;
SDL_IMAGE_VERSION(&linked_version);
return(&linked_version);
Jul 23, 2003
Jul 23, 2003
71
72
}
Dec 13, 2017
Dec 13, 2017
73
74
75
76
77
78
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);
Sep 26, 2009
Sep 26, 2009
79
Dec 13, 2017
Dec 13, 2017
80
81
extern int IMG_InitWEBP(void);
extern void IMG_QuitWEBP(void);
Dec 30, 2011
Dec 30, 2011
82
Sep 26, 2009
Sep 26, 2009
83
84
85
86
static int initialized = 0;
int IMG_Init(int flags)
{
May 22, 2013
May 22, 2013
87
88
int result = 0;
Jul 26, 2014
Jul 26, 2014
89
90
91
92
93
/* Passing 0 returns the currently initialized loaders */
if (!flags) {
return initialized;
}
May 22, 2013
May 22, 2013
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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;
Jul 26, 2014
Jul 26, 2014
116
return result;
Sep 26, 2009
Sep 26, 2009
117
118
119
120
}
void IMG_Quit()
{
May 22, 2013
May 22, 2013
121
122
123
124
125
126
127
128
129
130
131
132
133
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;
Sep 26, 2009
Sep 26, 2009
134
135
}
Jan 4, 2009
Jan 4, 2009
136
#if !defined(__APPLE__) || defined(SDL_IMAGE_USE_COMMON_BACKEND)
Aug 10, 2000
Aug 10, 2000
137
138
139
/* Load an image from a file */
SDL_Surface *IMG_Load(const char *file)
{
Jun 10, 2019
Jun 10, 2019
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#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
Aug 10, 2000
Aug 10, 2000
156
SDL_RWops *src = SDL_RWFromFile(file, "rb");
Jun 15, 2015
Jun 15, 2015
157
const char *ext = SDL_strrchr(file, '.');
Oct 4, 2019
Oct 4, 2019
158
if (ext) {
Dec 19, 2003
Dec 19, 2003
159
160
ext++;
}
Oct 4, 2019
Oct 4, 2019
161
if (!src) {
Dec 19, 2003
Dec 19, 2003
162
163
164
/* The error message has been set in SDL_RWFromFile */
return NULL;
}
Aug 10, 2000
Aug 10, 2000
165
166
return IMG_LoadTyped_RW(src, 1, ext);
}
Jan 4, 2009
Jan 4, 2009
167
#endif
Aug 10, 2000
Aug 10, 2000
168
169
170
171
172
173
174
175
/* 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
176
static int IMG_string_equals(const char *str1, const char *str2)
Aug 10, 2000
Aug 10, 2000
177
{
May 22, 2013
May 22, 2013
178
while ( *str1 && *str2 ) {
Jun 15, 2015
Jun 15, 2015
179
180
if ( SDL_toupper((unsigned char)*str1) !=
SDL_toupper((unsigned char)*str2) )
May 22, 2013
May 22, 2013
181
182
183
184
185
break;
++str1;
++str2;
}
return (!*str1 && !*str2);
Aug 10, 2000
Aug 10, 2000
186
187
188
}
/* Load an image from an SDL datasource, optionally specifying the type */
Jan 23, 2012
Jan 23, 2012
189
SDL_Surface *IMG_LoadTyped_RW(SDL_RWops *src, int freesrc, const char *type)
Aug 10, 2000
Aug 10, 2000
190
{
May 22, 2013
May 22, 2013
191
192
193
194
195
196
197
198
199
200
201
202
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");
Oct 4, 2019
Oct 4, 2019
203
if (freesrc)
May 22, 2013
May 22, 2013
204
205
206
207
SDL_RWclose(src);
return(NULL);
}
Jun 10, 2019
Jun 10, 2019
208
209
210
211
212
213
214
215
216
217
#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);
Oct 4, 2019
Oct 4, 2019
218
if (data)
Jun 10, 2019
Jun 10, 2019
219
{
Jun 10, 2019
Jun 10, 2019
220
221
222
223
surf = SDL_CreateRGBSurface(0, w, h, 32, 0xFF, 0xFF00, 0xFF0000, 0xFF000000);
if (surf != NULL) {
memcpy(surf->pixels, data, w * h * 4);
}
Jun 10, 2019
Jun 10, 2019
224
225
free(data);
Oct 4, 2019
Oct 4, 2019
226
if (freesrc)
Jun 10, 2019
Jun 10, 2019
227
228
SDL_RWclose(src);
Jun 10, 2019
Jun 10, 2019
229
/* If SDL_CreateRGBSurface returns NULL, it has set the error message for us */
Jun 10, 2019
Jun 10, 2019
230
231
232
233
234
return surf;
}
}
#endif
May 22, 2013
May 22, 2013
235
/* Detect the type of image being loaded */
Oct 4, 2019
Oct 4, 2019
236
237
238
for ( i=0; i < SDL_arraysize(supported); ++i ) {
if (supported[i].is) {
if (!supported[i].is(src))
May 22, 2013
May 22, 2013
239
240
241
continue;
} else {
/* magicless format */
Oct 4, 2019
Oct 4, 2019
242
if (!type || !IMG_string_equals(type, supported[i].type))
May 22, 2013
May 22, 2013
243
244
continue;
}
Jan 4, 2002
Jan 4, 2002
245
#ifdef DEBUG_IMGLIB
May 22, 2013
May 22, 2013
246
247
fprintf(stderr, "IMGLIB: Loading image as %s\n",
supported[i].type);
Jan 4, 2002
Jan 4, 2002
248
#endif
May 22, 2013
May 22, 2013
249
image = supported[i].load(src);
Oct 4, 2019
Oct 4, 2019
250
if (freesrc)
May 22, 2013
May 22, 2013
251
252
253
254
255
256
257
258
259
SDL_RWclose(src);
return image;
}
if ( freesrc ) {
SDL_RWclose(src);
}
IMG_SetError("Unsupported image format");
return NULL;
Aug 10, 2000
Aug 10, 2000
260
261
}
Jun 3, 2013
Jun 3, 2013
262
#if SDL_VERSION_ATLEAST(2,0,0)
Jan 23, 2012
Jan 23, 2012
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
288
289
290
291
292
293
294
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;
}
Jun 3, 2013
Jun 3, 2013
295
#endif /* SDL 2.0 */
Oct 4, 2019
Oct 4, 2019
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/* Load an animation from a file */
IMG_Animation *IMG_LoadAnimation(const char *file)
{
SDL_RWops *src = SDL_RWFromFile(file, "rb");
const char *ext = SDL_strrchr(file, '.');
if (ext) {
ext++;
}
if (!src) {
/* The error message has been set in SDL_RWFromFile */
return NULL;
}
return IMG_LoadAnimationTyped_RW(src, 1, ext);
}
/* Load an animation from an SDL datasource (for compatibility) */
IMG_Animation *IMG_LoadAnimation_RW(SDL_RWops *src, int freesrc)
{
return IMG_LoadAnimationTyped_RW(src, freesrc, NULL);
}
/* Load an animation from an SDL datasource, optionally specifying the type */
IMG_Animation *IMG_LoadAnimationTyped_RW(SDL_RWops *src, int freesrc, const char *type)
{
int i;
IMG_Animation *anim;
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);
}
/* Detect the type of image being loaded */
for ( i=0; i < SDL_arraysize(supported_anims); ++i ) {
if (supported_anims[i].is) {
if (!supported_anims[i].is(src))
continue;
} else {
/* magicless format */
if (!type || !IMG_string_equals(type, supported_anims[i].type))
continue;
}
#ifdef DEBUG_IMGLIB
fprintf(stderr, "IMGLIB: Loading image as %s\n",
supported_anims[i].type);
#endif
anim = supported_anims[i].load(src);
if (freesrc)
SDL_RWclose(src);
return anim;
}
/* Create a single frame animation from an image */
image = IMG_LoadTyped_RW(src, freesrc, type);
if (image) {
anim = (IMG_Animation *)SDL_malloc(sizeof(*anim));
if (anim) {
anim->w = image->w;
anim->h = image->h;
anim->count = 1;
anim->frames = (SDL_Surface **)SDL_calloc(anim->count, sizeof(*anim->frames));
anim->delays = (int *)SDL_calloc(anim->count, sizeof(*anim->delays));
if (anim->frames && anim->delays) {
anim->frames[0] = image;
return anim;
}
IMG_FreeAnimation(anim);
}
SDL_FreeSurface(image);
SDL_OutOfMemory();
}
return NULL;
}
void IMG_FreeAnimation(IMG_Animation *anim)
{
if (anim) {
if (anim->frames) {
int i;
for (i = 0; i < anim->count; ++i) {
if (anim->frames[i]) {
SDL_FreeSurface(anim->frames[i]);
}
}
SDL_free(anim->frames);
}
if (anim->delays) {
SDL_free(anim->delays);
}
SDL_free(anim);
}
}