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