IMG.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 08 Dec 2008 00:27:32 +0000
changeset 186 5fbbeabf647f
parent 121 1bf9c0c87374
child 187 3405ab551300
permissions -rw-r--r--
Updated copyright date
     1 /*
     2     SDL_image:  An example image loading library for use with SDL
     3     Copyright (C) 1997-2009 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 
    23 /* A simple library to load images of various formats as SDL surfaces */
    24 
    25 #include <stdio.h>
    26 #include <string.h>
    27 #include <ctype.h>
    28 
    29 #include "SDL_image.h"
    30 
    31 #define ARRAYSIZE(a) (sizeof(a) / sizeof((a)[0]))
    32 
    33 /* Table of image detection and loading functions */
    34 static struct {
    35 	char *type;
    36 	int (SDLCALL *is)(SDL_RWops *src);
    37 	SDL_Surface *(SDLCALL *load)(SDL_RWops *src);
    38 } supported[] = {
    39 	/* keep magicless formats first */
    40 	{ "TGA", NULL,      IMG_LoadTGA_RW },
    41 	{ "BMP", IMG_isBMP, IMG_LoadBMP_RW },
    42 	{ "GIF", IMG_isGIF, IMG_LoadGIF_RW },
    43 	{ "JPG", IMG_isJPG, IMG_LoadJPG_RW },
    44 	{ "LBM", IMG_isLBM, IMG_LoadLBM_RW },
    45 	{ "PCX", IMG_isPCX, IMG_LoadPCX_RW },
    46 	{ "PNG", IMG_isPNG, IMG_LoadPNG_RW },
    47 	{ "PNM", IMG_isPNM, IMG_LoadPNM_RW }, /* P[BGP]M share code */
    48 	{ "TIF", IMG_isTIF, IMG_LoadTIF_RW },
    49 	{ "XCF", IMG_isXCF, IMG_LoadXCF_RW },
    50 	{ "XPM", IMG_isXPM, IMG_LoadXPM_RW },
    51 	{ "XV",  IMG_isXV,  IMG_LoadXV_RW  }
    52 };
    53 
    54 const SDL_version *IMG_Linked_Version(void)
    55 {
    56 	static SDL_version linked_version;
    57 	SDL_IMAGE_VERSION(&linked_version);
    58 	return(&linked_version);
    59 }
    60 
    61 /* Load an image from a file */
    62 SDL_Surface *IMG_Load(const char *file)
    63 {
    64     SDL_RWops *src = SDL_RWFromFile(file, "rb");
    65     char *ext = strrchr(file, '.');
    66     if(ext) {
    67         ext++;
    68     }
    69     if(!src) {
    70         /* The error message has been set in SDL_RWFromFile */
    71         return NULL;
    72     }
    73     return IMG_LoadTyped_RW(src, 1, ext);
    74 }
    75 
    76 /* Load an image from an SDL datasource (for compatibility) */
    77 SDL_Surface *IMG_Load_RW(SDL_RWops *src, int freesrc)
    78 {
    79     return IMG_LoadTyped_RW(src, freesrc, NULL);
    80 }
    81 
    82 /* Portable case-insensitive string compare function */
    83 static int IMG_string_equals(const char *str1, const char *str2)
    84 {
    85 	while ( *str1 && *str2 ) {
    86 		if ( toupper((unsigned char)*str1) !=
    87 		     toupper((unsigned char)*str2) )
    88 			break;
    89 		++str1;
    90 		++str2;
    91 	}
    92 	return (!*str1 && !*str2);
    93 }
    94 
    95 /* Load an image from an SDL datasource, optionally specifying the type */
    96 SDL_Surface *IMG_LoadTyped_RW(SDL_RWops *src, int freesrc, char *type)
    97 {
    98 	int i;
    99 	SDL_Surface *image;
   100 
   101 	/* Make sure there is something to do.. */
   102 	if ( src == NULL ) {
   103 		IMG_SetError("Passed a NULL data source");
   104 		return(NULL);
   105 	}
   106 
   107 	/* See whether or not this data source can handle seeking */
   108 	if ( SDL_RWseek(src, 0, SEEK_CUR) < 0 ) {
   109 		IMG_SetError("Can't seek in this data source");
   110 		if(freesrc)
   111 			SDL_RWclose(src);
   112 		return(NULL);
   113 	}
   114 
   115 	/* Detect the type of image being loaded */
   116 	image = NULL;
   117 	for ( i=0; i < ARRAYSIZE(supported); ++i ) {
   118 		if(supported[i].is) {
   119 			if(!supported[i].is(src))
   120 				continue;
   121 		} else {
   122 			/* magicless format */
   123 			if(!type
   124 			   || !IMG_string_equals(type, supported[i].type))
   125 				continue;
   126 		}
   127 #ifdef DEBUG_IMGLIB
   128 		fprintf(stderr, "IMGLIB: Loading image as %s\n",
   129 			supported[i].type);
   130 #endif
   131 		image = supported[i].load(src);
   132 		if(freesrc)
   133 			SDL_RWclose(src);
   134 		return image;
   135 	}
   136 
   137 	if ( freesrc ) {
   138 		SDL_RWclose(src);
   139 	}
   140 	IMG_SetError("Unsupported image format");
   141 	return NULL;
   142 }
   143 
   144 /* Invert the alpha of a surface for use with OpenGL
   145    This function is a no-op and only kept for backwards compatibility.
   146  */
   147 int IMG_InvertAlpha(int on)
   148 {
   149     return 1;
   150 }