2 SDL_image: An example image loading library for use with SDL
3 Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
22 /* A simple library to load images of various formats as SDL surfaces */
28 #include "SDL_image.h"
30 #define ARRAYSIZE(a) (sizeof(a) / sizeof((a)[0]))
32 /* Table of image detection and loading functions */
35 int (SDLCALL *is)(SDL_RWops *src);
36 SDL_Surface *(SDLCALL *load)(SDL_RWops *src);
38 /* keep magicless formats first */
39 { "TGA", NULL, IMG_LoadTGA_RW },
40 { "CUR", IMG_isCUR, IMG_LoadCUR_RW },
41 { "ICO", IMG_isICO, IMG_LoadICO_RW },
42 { "BMP", IMG_isBMP, IMG_LoadBMP_RW },
43 { "GIF", IMG_isGIF, IMG_LoadGIF_RW },
44 { "JPG", IMG_isJPG, IMG_LoadJPG_RW },
45 { "LBM", IMG_isLBM, IMG_LoadLBM_RW },
46 { "PCX", IMG_isPCX, IMG_LoadPCX_RW },
47 { "PNG", IMG_isPNG, IMG_LoadPNG_RW },
48 { "PNM", IMG_isPNM, IMG_LoadPNM_RW }, /* P[BGP]M share code */
49 { "TIF", IMG_isTIF, IMG_LoadTIF_RW },
50 { "XCF", IMG_isXCF, IMG_LoadXCF_RW },
51 { "XPM", IMG_isXPM, IMG_LoadXPM_RW },
52 { "XV", IMG_isXV, IMG_LoadXV_RW },
53 { "WEBP", IMG_isWEBP, IMG_LoadWEBP_RW },
56 const SDL_version *IMG_Linked_Version(void)
58 static SDL_version linked_version;
59 SDL_IMAGE_VERSION(&linked_version);
60 return(&linked_version);
63 extern int IMG_InitJPG();
64 extern void IMG_QuitJPG();
65 extern int IMG_InitPNG();
66 extern void IMG_QuitPNG();
67 extern int IMG_InitTIF();
68 extern void IMG_QuitTIF();
70 extern int IMG_InitWEBP();
71 extern void IMG_QuitWEBP();
73 static int initialized = 0;
75 int IMG_Init(int flags)
79 if (flags & IMG_INIT_JPG) {
80 if ((initialized & IMG_INIT_JPG) || IMG_InitJPG() == 0) {
81 result |= IMG_INIT_JPG;
84 if (flags & IMG_INIT_PNG) {
85 if ((initialized & IMG_INIT_PNG) || IMG_InitPNG() == 0) {
86 result |= IMG_INIT_PNG;
89 if (flags & IMG_INIT_TIF) {
90 if ((initialized & IMG_INIT_TIF) || IMG_InitTIF() == 0) {
91 result |= IMG_INIT_TIF;
94 if (flags & IMG_INIT_WEBP) {
95 if ((initialized & IMG_INIT_WEBP) || IMG_InitWEBP() == 0) {
96 result |= IMG_INIT_WEBP;
99 initialized |= result;
101 return (initialized);
106 if (initialized & IMG_INIT_JPG) {
109 if (initialized & IMG_INIT_PNG) {
112 if (initialized & IMG_INIT_TIF) {
115 if (initialized & IMG_INIT_WEBP) {
121 #if !defined(__APPLE__) || defined(SDL_IMAGE_USE_COMMON_BACKEND)
122 /* Load an image from a file */
123 SDL_Surface *IMG_Load(const char *file)
125 SDL_RWops *src = SDL_RWFromFile(file, "rb");
126 const char *ext = strrchr(file, '.');
131 /* The error message has been set in SDL_RWFromFile */
134 return IMG_LoadTyped_RW(src, 1, ext);
138 /* Load an image from an SDL datasource (for compatibility) */
139 SDL_Surface *IMG_Load_RW(SDL_RWops *src, int freesrc)
141 return IMG_LoadTyped_RW(src, freesrc, NULL);
144 /* Portable case-insensitive string compare function */
145 static int IMG_string_equals(const char *str1, const char *str2)
147 while ( *str1 && *str2 ) {
148 if ( toupper((unsigned char)*str1) !=
149 toupper((unsigned char)*str2) )
154 return (!*str1 && !*str2);
157 /* Load an image from an SDL datasource, optionally specifying the type */
158 SDL_Surface *IMG_LoadTyped_RW(SDL_RWops *src, int freesrc, const char *type)
163 /* Make sure there is something to do.. */
165 IMG_SetError("Passed a NULL data source");
169 /* See whether or not this data source can handle seeking */
170 if ( SDL_RWseek(src, 0, RW_SEEK_CUR) < 0 ) {
171 IMG_SetError("Can't seek in this data source");
177 /* Detect the type of image being loaded */
179 for ( i=0; i < ARRAYSIZE(supported); ++i ) {
180 if(supported[i].is) {
181 if(!supported[i].is(src))
184 /* magicless format */
186 || !IMG_string_equals(type, supported[i].type))
190 fprintf(stderr, "IMGLIB: Loading image as %s\n",
193 image = supported[i].load(src);
202 IMG_SetError("Unsupported image format");
206 #if SDL_VERSION_ATLEAST(2,0,0)
207 SDL_Texture *IMG_LoadTexture(SDL_Renderer *renderer, const char *file)
209 SDL_Texture *texture = NULL;
210 SDL_Surface *surface = IMG_Load(file);
212 texture = SDL_CreateTextureFromSurface(renderer, surface);
213 SDL_FreeSurface(surface);
218 SDL_Texture *IMG_LoadTexture_RW(SDL_Renderer *renderer, SDL_RWops *src, int freesrc)
220 SDL_Texture *texture = NULL;
221 SDL_Surface *surface = IMG_Load_RW(src, freesrc);
223 texture = SDL_CreateTextureFromSurface(renderer, surface);
224 SDL_FreeSurface(surface);
229 SDL_Texture *IMG_LoadTextureTyped_RW(SDL_Renderer *renderer, SDL_RWops *src, int freesrc, const char *type)
231 SDL_Texture *texture = NULL;
232 SDL_Surface *surface = IMG_LoadTyped_RW(src, freesrc, type);
234 texture = SDL_CreateTextureFromSurface(renderer, surface);
235 SDL_FreeSurface(surface);