Added support for XV thumbnail images
authorSam Lantinga <slouken@libsdl.org>
Sat, 04 Feb 2006 23:26:45 +0000
changeset 119336c49447e0d
parent 118 c5e736a47ad2
child 120 d31c1397b87c
Added support for XV thumbnail images
CHANGES
IMG.c
IMG_pnm.c
IMG_xv.c
Makefile.am
SDL_image.h
configure.in
     1.1 --- a/CHANGES	Sat Feb 04 22:26:04 2006 +0000
     1.2 +++ b/CHANGES	Sat Feb 04 23:26:45 2006 +0000
     1.3 @@ -1,4 +1,6 @@
     1.4  1.2.5:
     1.5 +Sam Lantinga - Sat Feb  4 15:17:44 PST 2006
     1.6 + * Added support for XV thumbnail images
     1.7  Gautier Portet - Fri, 19 Mar 2004 17:35:12 +0100
     1.8   * Added support for 32-bit BMP files with alpha
     1.9  
     2.1 --- a/IMG.c	Sat Feb 04 22:26:04 2006 +0000
     2.2 +++ b/IMG.c	Sat Feb 04 23:26:45 2006 +0000
     2.3 @@ -41,15 +41,16 @@
     2.4  	/* keep magicless formats first */
     2.5  	{ "TGA", NULL,      IMG_LoadTGA_RW },
     2.6  	{ "BMP", IMG_isBMP, IMG_LoadBMP_RW },
     2.7 -	{ "PNM", IMG_isPNM, IMG_LoadPNM_RW }, /* P[BGP]M share code */
     2.8 -	{ "XPM", IMG_isXPM, IMG_LoadXPM_RW },
     2.9 -	{ "XCF", IMG_isXCF, IMG_LoadXCF_RW },
    2.10 -	{ "PCX", IMG_isPCX, IMG_LoadPCX_RW },
    2.11  	{ "GIF", IMG_isGIF, IMG_LoadGIF_RW },
    2.12  	{ "JPG", IMG_isJPG, IMG_LoadJPG_RW },
    2.13 +	{ "LBM", IMG_isLBM, IMG_LoadLBM_RW },
    2.14 +	{ "PCX", IMG_isPCX, IMG_LoadPCX_RW },
    2.15 +	{ "PNG", IMG_isPNG, IMG_LoadPNG_RW },
    2.16 +	{ "PNM", IMG_isPNM, IMG_LoadPNM_RW }, /* P[BGP]M share code */
    2.17  	{ "TIF", IMG_isTIF, IMG_LoadTIF_RW },
    2.18 -	{ "LBM", IMG_isLBM, IMG_LoadLBM_RW },
    2.19 -	{ "PNG", IMG_isPNG, IMG_LoadPNG_RW }
    2.20 +	{ "XCF", IMG_isXCF, IMG_LoadXCF_RW },
    2.21 +	{ "XPM", IMG_isXPM, IMG_LoadXPM_RW },
    2.22 +	{ "XV",  IMG_isXV,  IMG_LoadXV_RW  }
    2.23  };
    2.24  
    2.25  const SDL_version *IMG_Linked_Version(void)
     3.1 --- a/IMG_pnm.c	Sat Feb 04 22:26:04 2006 +0000
     3.2 +++ b/IMG_pnm.c	Sat Feb 04 23:26:45 2006 +0000
     3.3 @@ -57,6 +57,7 @@
     3.4  		 * P4	PBM, binary format
     3.5  		 * P5	PGM, binary format
     3.6  		 * P6	PPM, binary format
     3.7 +		 * P7	PAM, a general wrapper for PNM data
     3.8  		 */
     3.9  		if ( magic[0] == 'P' && magic[1] >= '1' && magic[1] <= '6' ) {
    3.10  			is_PNM = 1;
    3.11 @@ -114,7 +115,7 @@
    3.12  	char *error = NULL;
    3.13  	Uint8 magic[2];
    3.14  	int ascii;
    3.15 -	enum { PBM, PGM, PPM } kind;
    3.16 +	enum { PBM, PGM, PPM, PAM } kind;
    3.17  
    3.18  #define ERROR(s) do { error = (s); goto done; } while(0)
    3.19  
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/IMG_xv.c	Sat Feb 04 23:26:45 2006 +0000
     4.3 @@ -0,0 +1,166 @@
     4.4 +/*
     4.5 +    SDL_image:  An example image loading library for use with SDL
     4.6 +    Copyright (C) 1999-2004 Sam Lantinga
     4.7 +
     4.8 +    This library is free software; you can redistribute it and/or
     4.9 +    modify it under the terms of the GNU Library General Public
    4.10 +    License as published by the Free Software Foundation; either
    4.11 +    version 2 of the License, or (at your option) any later version.
    4.12 +
    4.13 +    This library is distributed in the hope that it will be useful,
    4.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    4.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    4.16 +    Library General Public License for more details.
    4.17 +
    4.18 +    You should have received a copy of the GNU Library General Public
    4.19 +    License along with this library; if not, write to the Free
    4.20 +    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    4.21 +
    4.22 +    Sam Lantinga
    4.23 +    slouken@libsdl.org
    4.24 +*/
    4.25 +
    4.26 +/* $Id$ */
    4.27 +
    4.28 +/* This is a generic "format not supported" image framework */
    4.29 +
    4.30 +#include <stdio.h>
    4.31 +#include <string.h>
    4.32 +
    4.33 +#include "SDL_image.h"
    4.34 +
    4.35 +#ifdef LOAD_XV
    4.36 +
    4.37 +static int get_line(SDL_RWops *src, char *line, int size)
    4.38 +{
    4.39 +	while ( size > 0 ) {
    4.40 +		if ( SDL_RWread(src, line, 1, 1) <= 0 ) {
    4.41 +			return -1;
    4.42 +		}
    4.43 +		if ( *line == '\r' ) {
    4.44 +			continue;
    4.45 +		}
    4.46 +		if ( *line == '\n' ) {
    4.47 +			*line = '\0';
    4.48 +			return 0;
    4.49 +		}
    4.50 +		++line;
    4.51 +		--size;
    4.52 +	}
    4.53 +	/* Out of space for the line */
    4.54 +	return -1;
    4.55 +}
    4.56 +
    4.57 +static int get_header(SDL_RWops *src, int *w, int *h)
    4.58 +{
    4.59 +	char line[1024];
    4.60 +
    4.61 +	*w = 0;
    4.62 +	*h = 0;
    4.63 +
    4.64 +	/* Check the header magic */
    4.65 +	if ( (get_line(src, line, sizeof(line)) < 0) ||
    4.66 +	     (memcmp(line, "P7 332", 6) != 0) ) {
    4.67 +		return -1;
    4.68 +	}
    4.69 +
    4.70 +	/* Read the header */
    4.71 +	while ( get_line(src, line, sizeof(line)) == 0 ) {
    4.72 +		if ( memcmp(line, "#BUILTIN:", 9) == 0 ) {
    4.73 +			/* Builtin image, no data */
    4.74 +			break;
    4.75 +		}
    4.76 +		if ( memcmp(line, "#END_OF_COMMENTS", 16) == 0 ) {
    4.77 +			if ( get_line(src, line, sizeof(line)) == 0 ) {
    4.78 +				sscanf(line, "%d %d", w, h);
    4.79 +				if ( *w >= 0 && *h >= 0 ) {
    4.80 +					return 0;
    4.81 +				}
    4.82 +			}
    4.83 +			break;
    4.84 +		}
    4.85 +	}
    4.86 +	/* No image data */
    4.87 +	return -1;
    4.88 +}
    4.89 +
    4.90 +/* See if an image is contained in a data source */
    4.91 +int IMG_isXV(SDL_RWops *src)
    4.92 +{
    4.93 +	int start;
    4.94 +	int is_XV;
    4.95 +	int w, h;
    4.96 +
    4.97 +	start = SDL_RWtell(src);
    4.98 +	is_XV = 0;
    4.99 +	if ( get_header(src, &w, &h) == 0 ) {
   4.100 +		is_XV = 1;
   4.101 +	}
   4.102 +	SDL_RWseek(src, start, SEEK_SET);
   4.103 +	return(is_XV);
   4.104 +}
   4.105 +
   4.106 +/* Load a XV thumbnail image from an SDL datasource */
   4.107 +SDL_Surface *IMG_LoadXV_RW(SDL_RWops *src)
   4.108 +{
   4.109 +	int start;
   4.110 +	const char *error = NULL;
   4.111 +	SDL_Surface *surface = NULL;
   4.112 +	int w, h;
   4.113 +	Uint8 *pixels;
   4.114 +
   4.115 +	if ( !src ) {
   4.116 +		/* The error message has been set in SDL_RWFromFile */
   4.117 +		return NULL;
   4.118 +	}
   4.119 +	start = SDL_RWtell(src);
   4.120 +
   4.121 +	/* Read the header */
   4.122 +	if ( get_header(src, &w, &h) < 0 ) {
   4.123 +		error = "Unsupported image format";
   4.124 +		goto done;
   4.125 +	}
   4.126 +
   4.127 +	/* Create the 3-3-2 indexed palette surface */
   4.128 +	surface = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 8, 0xe0, 0x1c, 0x03, 0);
   4.129 +	if ( surface == NULL ) {
   4.130 +		error = "Out of memory";
   4.131 +		goto done;
   4.132 +	}
   4.133 +
   4.134 +	/* Load the image data */
   4.135 +	for ( pixels = (Uint8 *)surface->pixels; h > 0; --h ) {
   4.136 +		if ( SDL_RWread(src, pixels, w, 1) <= 0 ) {
   4.137 +			error = "Couldn't read image data";
   4.138 +			goto done;
   4.139 +		}
   4.140 +		pixels += surface->pitch;
   4.141 +	}
   4.142 +
   4.143 +done:
   4.144 +	if ( error ) {
   4.145 +		SDL_RWseek(src, start, SEEK_SET);
   4.146 +		if ( surface ) {
   4.147 +			SDL_FreeSurface(surface);
   4.148 +			surface = NULL;
   4.149 +		}
   4.150 +		IMG_SetError(error);
   4.151 +	}
   4.152 +	return surface;
   4.153 +}
   4.154 +
   4.155 +#else
   4.156 +
   4.157 +/* See if an image is contained in a data source */
   4.158 +int IMG_isXV(SDL_RWops *src)
   4.159 +{
   4.160 +	return(0);
   4.161 +}
   4.162 +
   4.163 +/* Load a XXX type image from an SDL datasource */
   4.164 +SDL_Surface *IMG_LoadXV_RW(SDL_RWops *src)
   4.165 +{
   4.166 +	return(NULL);
   4.167 +}
   4.168 +
   4.169 +#endif /* LOAD_XV */
     5.1 --- a/Makefile.am	Sat Feb 04 22:26:04 2006 +0000
     5.2 +++ b/Makefile.am	Sat Feb 04 23:26:45 2006 +0000
     5.3 @@ -18,7 +18,8 @@
     5.4  	IMG_tga.c		\
     5.5  	IMG_tif.c		\
     5.6  	IMG_xcf.c		\
     5.7 -	IMG_xpm.c
     5.8 +	IMG_xpm.c		\
     5.9 +	IMG_xv.c
    5.10  
    5.11  EXTRA_DIST =			\
    5.12  	CHANGES			\
     6.1 --- a/SDL_image.h	Sat Feb 04 22:26:04 2006 +0000
     6.2 +++ b/SDL_image.h	Sat Feb 04 23:26:45 2006 +0000
     6.3 @@ -78,28 +78,30 @@
     6.4  
     6.5  /* Functions to detect a file type, given a seekable source */
     6.6  extern DECLSPEC int SDLCALL IMG_isBMP(SDL_RWops *src);
     6.7 -extern DECLSPEC int SDLCALL IMG_isPNM(SDL_RWops *src);
     6.8 -extern DECLSPEC int SDLCALL IMG_isXPM(SDL_RWops *src);
     6.9 -extern DECLSPEC int SDLCALL IMG_isXCF(SDL_RWops *src);
    6.10 -extern DECLSPEC int SDLCALL IMG_isPCX(SDL_RWops *src);
    6.11  extern DECLSPEC int SDLCALL IMG_isGIF(SDL_RWops *src);
    6.12  extern DECLSPEC int SDLCALL IMG_isJPG(SDL_RWops *src);
    6.13 +extern DECLSPEC int SDLCALL IMG_isLBM(SDL_RWops *src);
    6.14 +extern DECLSPEC int SDLCALL IMG_isPCX(SDL_RWops *src);
    6.15 +extern DECLSPEC int SDLCALL IMG_isPNG(SDL_RWops *src);
    6.16 +extern DECLSPEC int SDLCALL IMG_isPNM(SDL_RWops *src);
    6.17  extern DECLSPEC int SDLCALL IMG_isTIF(SDL_RWops *src);
    6.18 -extern DECLSPEC int SDLCALL IMG_isPNG(SDL_RWops *src);
    6.19 -extern DECLSPEC int SDLCALL IMG_isLBM(SDL_RWops *src);
    6.20 +extern DECLSPEC int SDLCALL IMG_isXCF(SDL_RWops *src);
    6.21 +extern DECLSPEC int SDLCALL IMG_isXPM(SDL_RWops *src);
    6.22 +extern DECLSPEC int SDLCALL IMG_isXV(SDL_RWops *src);
    6.23  
    6.24  /* Individual loading functions */
    6.25  extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadBMP_RW(SDL_RWops *src);
    6.26 -extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadPNM_RW(SDL_RWops *src);
    6.27 -extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadXPM_RW(SDL_RWops *src);
    6.28 -extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadXCF_RW(SDL_RWops *src);
    6.29 -extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadPCX_RW(SDL_RWops *src);
    6.30  extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadGIF_RW(SDL_RWops *src);
    6.31  extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadJPG_RW(SDL_RWops *src);
    6.32 +extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadLBM_RW(SDL_RWops *src);
    6.33 +extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadPCX_RW(SDL_RWops *src);
    6.34 +extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadPNG_RW(SDL_RWops *src);
    6.35 +extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadPNM_RW(SDL_RWops *src);
    6.36 +extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadTGA_RW(SDL_RWops *src);
    6.37  extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadTIF_RW(SDL_RWops *src);
    6.38 -extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadPNG_RW(SDL_RWops *src);
    6.39 -extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadTGA_RW(SDL_RWops *src);
    6.40 -extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadLBM_RW(SDL_RWops *src);
    6.41 +extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadXCF_RW(SDL_RWops *src);
    6.42 +extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadXPM_RW(SDL_RWops *src);
    6.43 +extern DECLSPEC SDL_Surface * SDLCALL IMG_LoadXV_RW(SDL_RWops *src);
    6.44  
    6.45  extern DECLSPEC SDL_Surface * SDLCALL IMG_ReadXPMFromArray(char **xpm);
    6.46  
     7.1 --- a/configure.in	Sat Feb 04 22:26:04 2006 +0000
     7.2 +++ b/configure.in	Sat Feb 04 23:26:45 2006 +0000
     7.3 @@ -184,6 +184,12 @@
     7.4  if test x$enable_xpm = xyes; then
     7.5      CFLAGS="$CFLAGS -DLOAD_XPM"
     7.6  fi
     7.7 +AC_ARG_ENABLE(xv,
     7.8 +[  --enable-xv             support loading XV thumbnail images [default=yes]],
     7.9 +              , enable_xv=yes)
    7.10 +if test x$enable_xv = xyes; then
    7.11 +    CFLAGS="$CFLAGS -DLOAD_XV"
    7.12 +fi
    7.13  AC_SUBST(IMG_LIBS)
    7.14  
    7.15  # Finally create all the generated files