cocoaTouch/otherSrc/IMG.c
author nemo
Wed, 31 Mar 2010 22:02:12 +0000
changeset 3220 109f11de64b6
parent 2698 90585aba87ad
permissions -rw-r--r--
Partial update by jose1711
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2698
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
     1
/*
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
     2
    SDL_image:  An example image loading library for use with SDL
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
     3
    Copyright (C) 1997-2009 Sam Lantinga
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
     4
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
     5
    This library is free software; you can redistribute it and/or
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
     6
    modify it under the terms of the GNU Lesser General Public
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
     7
    License as published by the Free Software Foundation; either
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
     8
    version 2.1 of the License, or (at your option) any later version.
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
     9
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    10
    This library is distributed in the hope that it will be useful,
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    13
    Lesser General Public License for more details.
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    14
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    15
    You should have received a copy of the GNU Lesser General Public
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    16
    License along with this library; if not, write to the Free Software
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    18
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    19
    Sam Lantinga
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    20
    slouken@libsdl.org
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    21
*/
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    22
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    23
/* A simple library to load images of various formats as SDL surfaces */
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    24
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    25
#include <stdio.h>
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    26
#include <string.h>
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    27
#include <ctype.h>
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    28
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    29
#include "SDL_image.h"
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    30
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    31
#define ARRAYSIZE(a) (sizeof(a) / sizeof((a)[0]))
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    32
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    33
/* Table of image detection and loading functions */
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    34
static struct {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    35
	char *type;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    36
	int (SDLCALL *is)(SDL_RWops *src);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    37
	SDL_Surface *(SDLCALL *load)(SDL_RWops *src);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    38
} supported[] = {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    39
	/* keep magicless formats first */
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    40
	{ "PNG", IMG_isPNG, IMG_LoadPNG_RW }
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    41
};
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    42
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    43
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    44
extern int IMG_InitPNG();
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    45
extern void IMG_QuitPNG();
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    46
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    47
static int initialized = 0;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    48
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    49
int IMG_Init(int flags)
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    50
{
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    51
	int result = 0;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    52
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    53
	if (flags & IMG_INIT_PNG) {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    54
		if ((initialized & IMG_INIT_PNG) || IMG_InitPNG() == 0) {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    55
			result |= IMG_INIT_PNG;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    56
		}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    57
	}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    58
	initialized |= result;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    59
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    60
	return (result);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    61
}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    62
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    63
void IMG_Quit()
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    64
{
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    65
	if (initialized & IMG_INIT_PNG) {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    66
		IMG_QuitPNG();
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    67
	}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    68
	initialized = 0;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    69
}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    70
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    71
/* Load an image from a file */
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    72
SDL_Surface *IMG_Load(const char *file)
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    73
{
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    74
    SDL_RWops *src = SDL_RWFromFile(file, "rb");
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    75
    char *ext = strrchr(file, '.');
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    76
    if(ext) {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    77
        ext++;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    78
    }
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    79
    if(!src) {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    80
        /* The error message has been set in SDL_RWFromFile */
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    81
        return NULL;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    82
    }
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    83
    return IMG_LoadTyped_RW(src, 1, ext);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    84
}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    85
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    86
/* Load an image from an SDL datasource (for compatibility) */
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    87
SDL_Surface *IMG_Load_RW(SDL_RWops *src, int freesrc)
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    88
{
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    89
    return IMG_LoadTyped_RW(src, freesrc, NULL);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    90
}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    91
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    92
/* Portable case-insensitive string compare function */
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    93
static int IMG_string_equals(const char *str1, const char *str2)
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    94
{
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    95
	while ( *str1 && *str2 ) {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    96
		if ( toupper((unsigned char)*str1) !=
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    97
		     toupper((unsigned char)*str2) )
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    98
			break;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
    99
		++str1;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   100
		++str2;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   101
	}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   102
	return (!*str1 && !*str2);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   103
}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   104
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   105
/* Load an image from an SDL datasource, optionally specifying the type */
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   106
SDL_Surface *IMG_LoadTyped_RW(SDL_RWops *src, int freesrc, char *type)
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   107
{
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   108
	int i;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   109
	SDL_Surface *image;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   110
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   111
	/* Make sure there is something to do.. */
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   112
	if ( src == NULL ) {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   113
		IMG_SetError("Passed a NULL data source");
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   114
		return(NULL);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   115
	}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   116
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   117
	/* See whether or not this data source can handle seeking */
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   118
	if ( SDL_RWseek(src, 0, RW_SEEK_CUR) < 0 ) {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   119
		IMG_SetError("Can't seek in this data source");
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   120
		if(freesrc)
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   121
			SDL_RWclose(src);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   122
		return(NULL);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   123
	}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   124
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   125
	/* Detect the type of image being loaded */
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   126
	image = NULL;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   127
	for ( i=0; i < ARRAYSIZE(supported); ++i ) {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   128
		if(supported[i].is) {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   129
			if(!supported[i].is(src))
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   130
				continue;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   131
		} else {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   132
			/* magicless format */
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   133
			if(!type
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   134
			   || !IMG_string_equals(type, supported[i].type))
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   135
				continue;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   136
		}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   137
#ifdef DEBUG_IMGLIB
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   138
		fprintf(stderr, "IMGLIB: Loading image as %s\n", supported[i].type);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   139
#endif
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   140
		image = supported[i].load(src);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   141
		if(freesrc)
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   142
			SDL_RWclose(src);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   143
		return image;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   144
	}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   145
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   146
	if ( freesrc ) {
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   147
		SDL_RWclose(src);
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   148
	}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   149
	IMG_SetError("Unsupported image format");
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   150
	return NULL;
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   151
}
90585aba87ad objc/pascal finally working
koda
parents:
diff changeset
   152