IMG.c
author Sam Lantinga <slouken@lokigames.com>
Wed, 29 Nov 2000 11:55:32 +0000
changeset 7 e1b6443ffb6b
parent 4 b1bb33e907f8
child 11 b3d1b573a542
permissions -rw-r--r--
1.1.0:
Sam Lantinga - Wed Nov 29 00:46:27 PST 2000
* Added XPM file format support
Supports color, greyscale, and mono XPMs with and without transparency
Mattias Engdeg�rd - Thu, 2 Nov 2000 23:23:17 +0100 (MET)
* Fixed array overrun when loading an unsupported format
* Minor compilation fixes for various platforms
     1 /*
     2     IMGLIB:  An example image loading library for use with SDL
     3     Copyright (C) 1999  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     5635-34 Springhouse Dr.
    21     Pleasanton, CA 94588 (USA)
    22     slouken@devolution.com
    23 */
    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 (denoted by is==NULL) */
    42 	{ "TGA", NULL,      IMG_LoadTGA_RW },
    43 	{ "BMP", IMG_isBMP, IMG_LoadBMP_RW },
    44 	{ "PPM", IMG_isPPM, IMG_LoadPPM_RW },
    45 	{ "XPM", IMG_isXPM, IMG_LoadXPM_RW },
    46 	{ "PCX", IMG_isPCX, IMG_LoadPCX_RW },
    47 	{ "GIF", IMG_isGIF, IMG_LoadGIF_RW },
    48 	{ "JPG", IMG_isJPG, IMG_LoadJPG_RW },
    49 	{ "TIF", IMG_isTIF, IMG_LoadTIF_RW },
    50 	{ "PNG", IMG_isPNG, IMG_LoadPNG_RW }
    51 };
    52 
    53 /* Load an image from a file */
    54 SDL_Surface *IMG_Load(const char *file)
    55 {
    56     SDL_RWops *src = SDL_RWFromFile(file, "rb");
    57     char *ext = strrchr(file, '.');
    58     if(ext)
    59 	ext++;
    60     return IMG_LoadTyped_RW(src, 1, ext);
    61 }
    62 
    63 /* Load an image from an SDL datasource (for compatibility) */
    64 SDL_Surface *IMG_Load_RW(SDL_RWops *src, int freesrc)
    65 {
    66     return IMG_LoadTyped_RW(src, freesrc, NULL);
    67 }
    68 
    69 /* Portable case-insensitive string compare function */
    70 static int string_equals(const char *str1, const char *str2)
    71 {
    72 	while ( *str1 && *str2 ) {
    73 		if ( toupper((unsigned char)*str1) !=
    74 		     toupper((unsigned char)*str2) )
    75 			break;
    76 		++str1;
    77 		++str2;
    78 	}
    79 	return (!*str1 && !*str2);
    80 }
    81 
    82 /* Load an image from an SDL datasource, optionally specifying the type */
    83 SDL_Surface *IMG_LoadTyped_RW(SDL_RWops *src, int freesrc, char *type)
    84 {
    85 	int i, start;
    86 	SDL_Surface *image;
    87 
    88 	/* Make sure there is something to do.. */
    89 	if ( src == NULL ) {
    90 		return(NULL);
    91 	}
    92 
    93 	/* See whether or not this data source can handle seeking */
    94 	if ( SDL_RWseek(src, 0, SEEK_CUR) < 0 ) {
    95 		IMG_SetError("Can't seek in this data source");
    96 		return(NULL);
    97 	}
    98 
    99 	/* Detect the type of image being loaded */
   100 	start = SDL_RWtell(src);
   101 	image = NULL;
   102 	for ( i=0; i < ARRAYSIZE(supported) && !image; ++i ) {
   103 	        if( (supported[i].is
   104 		     && (SDL_RWseek(src, start, SEEK_SET),
   105 			 supported[i].is(src)))
   106 		    || (type && string_equals(type, supported[i].type))) {
   107 #ifdef DEBUG_IMGLIB
   108 			fprintf(stderr, "IMGLIB: Loading image as %s\n",
   109 							supported[i].type);
   110 #endif
   111 			SDL_RWseek(src, start, SEEK_SET);
   112 			image = supported[i].load(src);
   113 			break;
   114 		}
   115 	}
   116 
   117 	/* Clean up, check for errors, and return */
   118 	if ( freesrc ) {
   119 		SDL_RWclose(src);
   120 	}
   121 	if ( image == NULL ) {
   122 		IMG_SetError("Unsupported image format");
   123 	}
   124 	return(image);
   125 }
   126 
   127 /* Invert the alpha of a surface for use with OpenGL
   128    This function is a no-op and only kept for backwards compatibility.
   129  */
   130 int IMG_InvertAlpha(int on)
   131 {
   132     return 1;
   133 }