Skip to content

Latest commit

 

History

History
139 lines (120 loc) · 3.68 KB

IMG.c

File metadata and controls

139 lines (120 loc) · 3.68 KB
 
Aug 10, 2000
Aug 10, 2000
1
/*
Dec 14, 2001
Dec 14, 2001
2
3
SDL_image: An example image loading library for use with SDL
Copyright (C) 1999, 2000, 2001 Sam Lantinga
Aug 10, 2000
Aug 10, 2000
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Sam Lantinga
Dec 14, 2001
Dec 14, 2001
20
slouken@libsdl.org
Aug 10, 2000
Aug 10, 2000
21
22
*/
Dec 14, 2001
Dec 14, 2001
23
24
/* $Id$ */
Aug 10, 2000
Aug 10, 2000
25
26
27
28
29
30
31
32
/* 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
33
34
#define ARRAYSIZE(a) (sizeof(a) / sizeof((a)[0]))
Aug 10, 2000
Aug 10, 2000
35
36
37
38
39
40
/* Table of image detection and loading functions */
static struct {
char *type;
int (*is)(SDL_RWops *src);
SDL_Surface *(*load)(SDL_RWops *src);
} supported[] = {
Mar 7, 2001
Mar 7, 2001
41
42
/* keep magicless formats first */
{ "TGA", 0, IMG_LoadTGA_RW },
Aug 10, 2000
Aug 10, 2000
43
{ "BMP", IMG_isBMP, IMG_LoadBMP_RW },
Mar 7, 2001
Mar 7, 2001
44
{ "PNM", IMG_isPNM, IMG_LoadPNM_RW }, /* P[BGP]M share code */
Nov 29, 2000
Nov 29, 2000
45
{ "XPM", IMG_isXPM, IMG_LoadXPM_RW },
Mar 7, 2001
Mar 7, 2001
46
{ "XCF", IMG_isXCF, IMG_LoadXCF_RW },
Aug 10, 2000
Aug 10, 2000
47
48
49
50
{ "PCX", IMG_isPCX, IMG_LoadPCX_RW },
{ "GIF", IMG_isGIF, IMG_LoadGIF_RW },
{ "JPG", IMG_isJPG, IMG_LoadJPG_RW },
{ "TIF", IMG_isTIF, IMG_LoadTIF_RW },
Sep 23, 2001
Sep 23, 2001
51
{ "LBM", IMG_isLBM, IMG_LoadLBM_RW },
Nov 29, 2000
Nov 29, 2000
52
{ "PNG", IMG_isPNG, IMG_LoadPNG_RW }
Aug 10, 2000
Aug 10, 2000
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
};
/* Load an image from a file */
SDL_Surface *IMG_Load(const char *file)
{
SDL_RWops *src = SDL_RWFromFile(file, "rb");
char *ext = strrchr(file, '.');
if(ext)
ext++;
return IMG_LoadTyped_RW(src, 1, ext);
}
/* 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
72
static int IMG_string_equals(const char *str1, const char *str2)
Aug 10, 2000
Aug 10, 2000
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
{
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)
{
int i, start;
SDL_Surface *image;
/* Make sure there is something to do.. */
if ( src == NULL ) {
return(NULL);
}
/* See whether or not this data source can handle seeking */
if ( SDL_RWseek(src, 0, SEEK_CUR) < 0 ) {
IMG_SetError("Can't seek in this data source");
return(NULL);
}
/* Detect the type of image being loaded */
start = SDL_RWtell(src);
image = NULL;
Dec 6, 2000
Dec 6, 2000
104
for ( i=0; i < ARRAYSIZE(supported); ++i ) {
Jan 4, 2002
Jan 4, 2002
105
if(supported[i].is) {
Aug 10, 2000
Aug 10, 2000
106
SDL_RWseek(src, start, SEEK_SET);
Jan 4, 2002
Jan 4, 2002
107
108
109
110
111
112
113
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
114
}
Jan 4, 2002
Jan 4, 2002
115
116
117
118
119
120
121
122
123
#ifdef DEBUG_IMGLIB
fprintf(stderr, "IMGLIB: Loading image as %s\n",
supported[i].type);
#endif
SDL_RWseek(src, start, SEEK_SET);
image = supported[i].load(src);
if(freesrc)
SDL_RWclose(src);
return image;
Aug 10, 2000
Aug 10, 2000
124
125
126
127
128
}
if ( freesrc ) {
SDL_RWclose(src);
}
Jan 4, 2002
Jan 4, 2002
129
130
IMG_SetError("Unsupported image format");
return NULL;
Aug 10, 2000
Aug 10, 2000
131
132
133
}
/* Invert the alpha of a surface for use with OpenGL
Sep 1, 2000
Sep 1, 2000
134
This function is a no-op and only kept for backwards compatibility.
Aug 10, 2000
Aug 10, 2000
135
136
137
*/
int IMG_InvertAlpha(int on)
{
Sep 1, 2000
Sep 1, 2000
138
return 1;
Aug 10, 2000
Aug 10, 2000
139
}