IMG.c
author Sam Lantinga <slouken@lokigames.com>
Fri, 01 Sep 2000 00:45:57 +0000
changeset 4 b1bb33e907f8
parent 0 76be7dab668c
child 7 e1b6443ffb6b
permissions -rw-r--r--
Mattias Engdeg�rd - Wed Aug 9 20:32:22 MET DST 2000
* Removed the alpha flipping, made IMG_InvertAlpha() a noop
* Fixed nonexisting PCX alpha support
* Some TIFF bugfixes
* PNG greyscale images are loaded as 8bpp with a greyscale palette
slouken@0
     1
/*
slouken@0
     2
    IMGLIB:  An example image loading library for use with SDL
slouken@0
     3
    Copyright (C) 1999  Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@0
     6
    modify it under the terms of the GNU Library General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@0
     8
    version 2 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@0
    13
    Library General Public License for more details.
slouken@0
    14
slouken@0
    15
    You should have received a copy of the GNU Library General Public
slouken@0
    16
    License along with this library; if not, write to the Free
slouken@0
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@0
    20
    5635-34 Springhouse Dr.
slouken@0
    21
    Pleasanton, CA 94588 (USA)
slouken@0
    22
    slouken@devolution.com
slouken@0
    23
*/
slouken@0
    24
slouken@0
    25
/* A simple library to load images of various formats as SDL surfaces */
slouken@0
    26
slouken@0
    27
#include <stdio.h>
slouken@0
    28
#include <string.h>
slouken@0
    29
#include <ctype.h>
slouken@0
    30
slouken@0
    31
#include "SDL_image.h"
slouken@0
    32
slouken@0
    33
/* Table of image detection and loading functions */
slouken@0
    34
static struct {
slouken@0
    35
	char *type;
slouken@0
    36
	int (*is)(SDL_RWops *src);
slouken@0
    37
	SDL_Surface *(*load)(SDL_RWops *src);
slouken@0
    38
} supported[] = {
slouken@0
    39
        /* keep magicless formats first (denoted by is==NULL) */
slouken@0
    40
	{ "TGA", NULL,      IMG_LoadTGA_RW },
slouken@0
    41
	{ "BMP", IMG_isBMP, IMG_LoadBMP_RW },
slouken@0
    42
	{ "PPM", IMG_isPPM, IMG_LoadPPM_RW },
slouken@0
    43
	{ "PCX", IMG_isPCX, IMG_LoadPCX_RW },
slouken@0
    44
	{ "GIF", IMG_isGIF, IMG_LoadGIF_RW },
slouken@0
    45
	{ "JPG", IMG_isJPG, IMG_LoadJPG_RW },
slouken@0
    46
	{ "TIF", IMG_isTIF, IMG_LoadTIF_RW },
slouken@0
    47
	{ "PNG", IMG_isPNG, IMG_LoadPNG_RW },
slouken@0
    48
};
slouken@0
    49
slouken@0
    50
/* Load an image from a file */
slouken@0
    51
SDL_Surface *IMG_Load(const char *file)
slouken@0
    52
{
slouken@0
    53
    SDL_RWops *src = SDL_RWFromFile(file, "rb");
slouken@0
    54
    char *ext = strrchr(file, '.');
slouken@0
    55
    if(ext)
slouken@0
    56
	ext++;
slouken@0
    57
    return IMG_LoadTyped_RW(src, 1, ext);
slouken@0
    58
}
slouken@0
    59
slouken@0
    60
/* Load an image from an SDL datasource (for compatibility) */
slouken@0
    61
SDL_Surface *IMG_Load_RW(SDL_RWops *src, int freesrc)
slouken@0
    62
{
slouken@0
    63
    return IMG_LoadTyped_RW(src, freesrc, NULL);
slouken@0
    64
}
slouken@0
    65
slouken@0
    66
/* Portable case-insensitive string compare function */
slouken@0
    67
static int string_equals(const char *str1, const char *str2)
slouken@0
    68
{
slouken@0
    69
	while ( *str1 && *str2 ) {
slouken@0
    70
		if ( toupper((unsigned char)*str1) !=
slouken@0
    71
		     toupper((unsigned char)*str2) )
slouken@0
    72
			break;
slouken@0
    73
		++str1;
slouken@0
    74
		++str2;
slouken@0
    75
	}
slouken@0
    76
	return (!*str1 && !*str2);
slouken@0
    77
}
slouken@0
    78
slouken@0
    79
/* Load an image from an SDL datasource, optionally specifying the type */
slouken@0
    80
SDL_Surface *IMG_LoadTyped_RW(SDL_RWops *src, int freesrc, char *type)
slouken@0
    81
{
slouken@0
    82
	int i, start;
slouken@0
    83
	SDL_Surface *image;
slouken@0
    84
slouken@0
    85
	/* Make sure there is something to do.. */
slouken@0
    86
	if ( src == NULL ) {
slouken@0
    87
		return(NULL);
slouken@0
    88
	}
slouken@0
    89
slouken@0
    90
	/* See whether or not this data source can handle seeking */
slouken@0
    91
	if ( SDL_RWseek(src, 0, SEEK_CUR) < 0 ) {
slouken@0
    92
		IMG_SetError("Can't seek in this data source");
slouken@0
    93
		return(NULL);
slouken@0
    94
	}
slouken@0
    95
slouken@0
    96
	/* Detect the type of image being loaded */
slouken@0
    97
	start = SDL_RWtell(src);
slouken@0
    98
	image = NULL;
slouken@0
    99
	for ( i=0; supported[i].type && !image; ++i ) {
slouken@0
   100
	        if( (supported[i].is
slouken@0
   101
		     && (SDL_RWseek(src, start, SEEK_SET),
slouken@0
   102
			 supported[i].is(src)))
slouken@0
   103
		    || (type && string_equals(type, supported[i].type))) {
slouken@0
   104
#ifdef DEBUG_IMGLIB
slouken@0
   105
			fprintf(stderr, "IMGLIB: Loading image as %s\n",
slouken@0
   106
							supported[i].type);
slouken@0
   107
#endif
slouken@0
   108
			SDL_RWseek(src, start, SEEK_SET);
slouken@0
   109
			image = supported[i].load(src);
slouken@0
   110
			break;
slouken@0
   111
		}
slouken@0
   112
	}
slouken@0
   113
slouken@0
   114
	/* Clean up, check for errors, and return */
slouken@0
   115
	if ( freesrc ) {
slouken@0
   116
		SDL_RWclose(src);
slouken@0
   117
	}
slouken@0
   118
	if ( image == NULL ) {
slouken@0
   119
		IMG_SetError("Unsupported image format");
slouken@0
   120
	}
slouken@0
   121
	return(image);
slouken@0
   122
}
slouken@0
   123
slouken@0
   124
/* Invert the alpha of a surface for use with OpenGL
slouken@4
   125
   This function is a no-op and only kept for backwards compatibility.
slouken@0
   126
 */
slouken@0
   127
int IMG_InvertAlpha(int on)
slouken@0
   128
{
slouken@4
   129
    return 1;
slouken@0
   130
}