*** empty log message ***
authorSam Lantinga <slouken@lokigames.com>
Wed, 07 Mar 2001 17:00:59 +0000
changeset 25c9117619e2af
parent 24 69f3d44d56da
child 26 d51719e1b5d9
*** empty log message ***
IMG_pnm.c
IMG_ppm.c
Makefile.am
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/IMG_pnm.c	Wed Mar 07 17:00:59 2001 +0000
     1.3 @@ -0,0 +1,242 @@
     1.4 +/*
     1.5 +    IMGLIB:  An example image loading library for use with SDL
     1.6 +    Copyright (C) 1999  Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Library General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Library General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Library General Public
    1.19 +    License along with this library; if not, write to the Free
    1.20 +    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    5635-34 Springhouse Dr.
    1.24 +    Pleasanton, CA 94588 (USA)
    1.25 +    slouken@devolution.com
    1.26 +*/
    1.27 +
    1.28 +/*
    1.29 + * PNM (portable anymap) image loader:
    1.30 + *
    1.31 + * Supports: PBM, PGM and PPM, ASCII and binary formats
    1.32 + * (PBM and PGM are loaded as 8bpp surfaces)
    1.33 + * Does not support: maximum component value > 255
    1.34 + */
    1.35 +
    1.36 +#include <stdio.h>
    1.37 +#include <stdlib.h>
    1.38 +#include <ctype.h>
    1.39 +#include <string.h>
    1.40 +
    1.41 +#include "SDL_image.h"
    1.42 +
    1.43 +#ifdef LOAD_PNM
    1.44 +
    1.45 +/* See if an image is contained in a data source */
    1.46 +int IMG_isPNM(SDL_RWops *src)
    1.47 +{
    1.48 +	char magic[2];
    1.49 +
    1.50 +	/*
    1.51 +	 * PNM magic signatures:
    1.52 +	 * P1	PBM, ascii format
    1.53 +	 * P2	PGM, ascii format
    1.54 +	 * P3	PPM, ascii format
    1.55 +	 * P4	PBM, binary format
    1.56 +	 * P5	PGM, binary format
    1.57 +	 * P6	PPM, binary format
    1.58 +	 */
    1.59 +	return (SDL_RWread(src, magic, 2, 1)
    1.60 +		&& magic[0] == 'P' && magic[1] >= '1' && magic[1] <= '6');
    1.61 +}
    1.62 +
    1.63 +/* read a non-negative integer from the source. return -1 upon error */
    1.64 +static int ReadNumber(SDL_RWops *src)
    1.65 +{
    1.66 +	int number;
    1.67 +	unsigned char ch;
    1.68 +
    1.69 +	/* Initialize return value */
    1.70 +	number = 0;
    1.71 +
    1.72 +	/* Skip leading whitespace */
    1.73 +	do {
    1.74 +		if ( ! SDL_RWread(src, &ch, 1, 1) ) {
    1.75 +			return(0);
    1.76 +		}
    1.77 +		/* Eat comments as whitespace */
    1.78 +		if ( ch == '#' ) {  /* Comment is '#' to end of line */
    1.79 +			do {
    1.80 +				if ( ! SDL_RWread(src, &ch, 1, 1) ) {
    1.81 +					return -1;
    1.82 +				}
    1.83 +			} while ( (ch != '\r') && (ch != '\n') );
    1.84 +		}
    1.85 +	} while ( isspace(ch) );
    1.86 +
    1.87 +	/* Add up the number */
    1.88 +	do {
    1.89 +		number *= 10;
    1.90 +		number += ch-'0';
    1.91 +
    1.92 +		if ( !SDL_RWread(src, &ch, 1, 1) ) {
    1.93 +			return -1;
    1.94 +		}
    1.95 +	} while ( isdigit(ch) );
    1.96 +
    1.97 +	return(number);
    1.98 +}
    1.99 +
   1.100 +SDL_Surface *IMG_LoadPNM_RW(SDL_RWops *src)
   1.101 +{
   1.102 +	SDL_Surface *surface = NULL;
   1.103 +	int width, height;
   1.104 +	int maxval, y, bpl;
   1.105 +	Uint8 *row;
   1.106 +	Uint8 *buf = NULL;
   1.107 +	char *error = NULL;
   1.108 +	Uint8 magic[2];
   1.109 +	int ascii;
   1.110 +	enum { PBM, PGM, PPM } kind;
   1.111 +
   1.112 +#define ERROR(s) do { error = (s); goto done; } while(0)
   1.113 +
   1.114 +	if(!src)
   1.115 +		return NULL;
   1.116 +
   1.117 +	SDL_RWread(src, magic, 2, 1);
   1.118 +	kind = magic[1] - '1';
   1.119 +	ascii = 1;
   1.120 +	if(kind >= 3) {
   1.121 +		ascii = 0;
   1.122 +		kind -= 3;
   1.123 +	}
   1.124 +
   1.125 +	width = ReadNumber(src);
   1.126 +	height = ReadNumber(src);
   1.127 +	if(width <= 0 || height <= 0)
   1.128 +		ERROR("Unable to read image width and height");
   1.129 +
   1.130 +	if(kind != PBM) {
   1.131 +		maxval = ReadNumber(src);
   1.132 +		if(maxval <= 0 || maxval > 255)
   1.133 +			ERROR("unsupported PNM format");
   1.134 +	} else
   1.135 +		maxval = 255;	/* never scale PBMs */
   1.136 +
   1.137 +	/* binary PNM allows just a single character of whitespace after
   1.138 +	   the last parameter, and we've already consumed it */
   1.139 +
   1.140 +	if(kind == PPM) {
   1.141 +		/* 24-bit surface in R,G,B byte order */
   1.142 +		surface = SDL_AllocSurface(SDL_SWSURFACE, width, height, 24,
   1.143 +#if SDL_BYTEORDER == SDL_LIL_ENDIAN
   1.144 +					   0x000000ff, 0x0000ff00, 0x00ff0000,
   1.145 +#else
   1.146 +					   0x00ff0000, 0x0000ff00, 0x000000ff,
   1.147 +#endif
   1.148 +					   0);
   1.149 +	} else {
   1.150 +		/* load PBM/PGM as 8-bit indexed images */
   1.151 +		surface = SDL_AllocSurface(SDL_SWSURFACE, width, height, 8,
   1.152 +					   0, 0, 0, 0);
   1.153 +	}
   1.154 +	if ( surface == NULL )
   1.155 +		ERROR("Out of memory");
   1.156 +	bpl = width * surface->format->BytesPerPixel;
   1.157 +	if(kind == PGM) {
   1.158 +		SDL_Color *c = surface->format->palette->colors;
   1.159 +		int i;
   1.160 +		for(i = 0; i < 256; i++)
   1.161 +			c[i].r = c[i].g = c[i].b = i;
   1.162 +		surface->format->palette->ncolors = 256;
   1.163 +	} else if(kind == PBM) {
   1.164 +		/* for some reason PBM has 1=black, 0=white */
   1.165 +		SDL_Color *c = surface->format->palette->colors;
   1.166 +		c[0].r = c[0].g = c[0].b = 255;
   1.167 +		c[1].r = c[1].g = c[1].b = 0;
   1.168 +		surface->format->palette->ncolors = 2;
   1.169 +		bpl = (width + 7) >> 3;
   1.170 +		buf = malloc(bpl);
   1.171 +		if(buf == NULL)
   1.172 +			ERROR("Out of memory");
   1.173 +	}
   1.174 +
   1.175 +	/* Read the image into the surface */
   1.176 +	row = surface->pixels;
   1.177 +	for(y = 0; y < height; y++) {
   1.178 +		if(ascii) {
   1.179 +			int i;
   1.180 +			if(kind == PBM) {
   1.181 +				for(i = 0; i < width; i++) {
   1.182 +					Uint8 ch;
   1.183 +					do {
   1.184 +						if(!SDL_RWread(src, &ch,
   1.185 +							       1, 1))
   1.186 +						       ERROR("file truncated");
   1.187 +						ch -= '0';
   1.188 +					} while(ch > 1);
   1.189 +					row[i] = ch;
   1.190 +				}
   1.191 +			} else {
   1.192 +				for(i = 0; i < bpl; i++) {
   1.193 +					int c;
   1.194 +					c = ReadNumber(src);
   1.195 +					if(c < 0)
   1.196 +						ERROR("file truncated");
   1.197 +					row[i] = c;
   1.198 +				}
   1.199 +			}
   1.200 +		} else {
   1.201 +			Uint8 *dst = (kind == PBM) ? buf : row;
   1.202 +			if(!SDL_RWread(src, dst, bpl, 1))
   1.203 +				ERROR("file truncated");
   1.204 +			if(kind == PBM) {
   1.205 +				/* expand bitmap to 8bpp */
   1.206 +				int i;
   1.207 +				for(i = 0; i < width; i++) {
   1.208 +					int bit = 7 - (i & 7);
   1.209 +					row[i] = (buf[i >> 3] >> bit) & 1;
   1.210 +				}
   1.211 +			}
   1.212 +		}
   1.213 +		if(maxval < 255) {
   1.214 +			/* scale up to full dynamic range (slow) */
   1.215 +			int i;
   1.216 +			for(i = 0; i < bpl; i++)
   1.217 +				row[i] = row[i] * 255 / maxval;
   1.218 +		}
   1.219 +		row += surface->pitch;
   1.220 +	}
   1.221 +done:
   1.222 +	free(buf);
   1.223 +	if(error) {
   1.224 +		SDL_FreeSurface(surface);
   1.225 +		IMG_SetError(error);
   1.226 +		surface = NULL;
   1.227 +	}
   1.228 +	return(surface);
   1.229 +}
   1.230 +
   1.231 +#else
   1.232 +
   1.233 +/* See if an image is contained in a data source */
   1.234 +int IMG_isPNM(SDL_RWops *src)
   1.235 +{
   1.236 +	return(0);
   1.237 +}
   1.238 +
   1.239 +/* Load a PNM type image from an SDL datasource */
   1.240 +SDL_Surface *IMG_LoadPNM_RW(SDL_RWops *src)
   1.241 +{
   1.242 +	return(NULL);
   1.243 +}
   1.244 +
   1.245 +#endif /* LOAD_PNM */
     2.1 --- a/IMG_ppm.c	Wed Mar 07 16:59:28 2001 +0000
     2.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.3 @@ -1,242 +0,0 @@
     2.4 -/*
     2.5 -    IMGLIB:  An example image loading library for use with SDL
     2.6 -    Copyright (C) 1999  Sam Lantinga
     2.7 -
     2.8 -    This library is free software; you can redistribute it and/or
     2.9 -    modify it under the terms of the GNU Library General Public
    2.10 -    License as published by the Free Software Foundation; either
    2.11 -    version 2 of the License, or (at your option) any later version.
    2.12 -
    2.13 -    This library is distributed in the hope that it will be useful,
    2.14 -    but WITHOUT ANY WARRANTY; without even the implied warranty of
    2.15 -    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    2.16 -    Library General Public License for more details.
    2.17 -
    2.18 -    You should have received a copy of the GNU Library General Public
    2.19 -    License along with this library; if not, write to the Free
    2.20 -    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    2.21 -
    2.22 -    Sam Lantinga
    2.23 -    5635-34 Springhouse Dr.
    2.24 -    Pleasanton, CA 94588 (USA)
    2.25 -    slouken@devolution.com
    2.26 -*/
    2.27 -
    2.28 -/*
    2.29 - * PNM (portable anymap) image loader:
    2.30 - *
    2.31 - * Supports: PBM, PGM and PPM, ASCII and binary formats
    2.32 - * (PBM and PGM are loaded as 8bpp surfaces)
    2.33 - * Does not support: maximum component value > 255
    2.34 - */
    2.35 -
    2.36 -#include <stdio.h>
    2.37 -#include <stdlib.h>
    2.38 -#include <ctype.h>
    2.39 -#include <string.h>
    2.40 -
    2.41 -#include "SDL_image.h"
    2.42 -
    2.43 -#ifdef LOAD_PNM
    2.44 -
    2.45 -/* See if an image is contained in a data source */
    2.46 -int IMG_isPNM(SDL_RWops *src)
    2.47 -{
    2.48 -	char magic[2];
    2.49 -
    2.50 -	/*
    2.51 -	 * PNM magic signatures:
    2.52 -	 * P1	PBM, ascii format
    2.53 -	 * P2	PGM, ascii format
    2.54 -	 * P3	PPM, ascii format
    2.55 -	 * P4	PBM, binary format
    2.56 -	 * P5	PGM, binary format
    2.57 -	 * P6	PPM, binary format
    2.58 -	 */
    2.59 -	return (SDL_RWread(src, magic, 2, 1)
    2.60 -		&& magic[0] == 'P' && magic[1] >= '1' && magic[1] <= '6');
    2.61 -}
    2.62 -
    2.63 -/* read a non-negative integer from the source. return -1 upon error */
    2.64 -static int ReadNumber(SDL_RWops *src)
    2.65 -{
    2.66 -	int number;
    2.67 -	unsigned char ch;
    2.68 -
    2.69 -	/* Initialize return value */
    2.70 -	number = 0;
    2.71 -
    2.72 -	/* Skip leading whitespace */
    2.73 -	do {
    2.74 -		if ( ! SDL_RWread(src, &ch, 1, 1) ) {
    2.75 -			return(0);
    2.76 -		}
    2.77 -		/* Eat comments as whitespace */
    2.78 -		if ( ch == '#' ) {  /* Comment is '#' to end of line */
    2.79 -			do {
    2.80 -				if ( ! SDL_RWread(src, &ch, 1, 1) ) {
    2.81 -					return -1;
    2.82 -				}
    2.83 -			} while ( (ch != '\r') && (ch != '\n') );
    2.84 -		}
    2.85 -	} while ( isspace(ch) );
    2.86 -
    2.87 -	/* Add up the number */
    2.88 -	do {
    2.89 -		number *= 10;
    2.90 -		number += ch-'0';
    2.91 -
    2.92 -		if ( !SDL_RWread(src, &ch, 1, 1) ) {
    2.93 -			return -1;
    2.94 -		}
    2.95 -	} while ( isdigit(ch) );
    2.96 -
    2.97 -	return(number);
    2.98 -}
    2.99 -
   2.100 -SDL_Surface *IMG_LoadPNM_RW(SDL_RWops *src)
   2.101 -{
   2.102 -	SDL_Surface *surface = NULL;
   2.103 -	int width, height;
   2.104 -	int maxval, y, bpl;
   2.105 -	Uint8 *row;
   2.106 -	Uint8 *buf = NULL;
   2.107 -	char *error = NULL;
   2.108 -	Uint8 magic[2];
   2.109 -	int ascii;
   2.110 -	enum { PBM, PGM, PPM } kind;
   2.111 -
   2.112 -#define ERROR(s) do { error = (s); goto done; } while(0)
   2.113 -
   2.114 -	if(!src)
   2.115 -		return NULL;
   2.116 -
   2.117 -	SDL_RWread(src, magic, 2, 1);
   2.118 -	kind = magic[1] - '1';
   2.119 -	ascii = 1;
   2.120 -	if(kind >= 3) {
   2.121 -		ascii = 0;
   2.122 -		kind -= 3;
   2.123 -	}
   2.124 -
   2.125 -	width = ReadNumber(src);
   2.126 -	height = ReadNumber(src);
   2.127 -	if(width <= 0 || height <= 0)
   2.128 -		ERROR("Unable to read image width and height");
   2.129 -
   2.130 -	if(kind != PBM) {
   2.131 -		maxval = ReadNumber(src);
   2.132 -		if(maxval <= 0 || maxval > 255)
   2.133 -			ERROR("unsupported PNM format");
   2.134 -	} else
   2.135 -		maxval = 255;	/* never scale PBMs */
   2.136 -
   2.137 -	/* binary PNM allows just a single character of whitespace after
   2.138 -	   the last parameter, and we've already consumed it */
   2.139 -
   2.140 -	if(kind == PPM) {
   2.141 -		/* 24-bit surface in R,G,B byte order */
   2.142 -		surface = SDL_AllocSurface(SDL_SWSURFACE, width, height, 24,
   2.143 -#if SDL_BYTEORDER == SDL_LIL_ENDIAN
   2.144 -					   0x000000ff, 0x0000ff00, 0x00ff0000,
   2.145 -#else
   2.146 -					   0x00ff0000, 0x0000ff00, 0x000000ff,
   2.147 -#endif
   2.148 -					   0);
   2.149 -	} else {
   2.150 -		/* load PBM/PGM as 8-bit indexed images */
   2.151 -		surface = SDL_AllocSurface(SDL_SWSURFACE, width, height, 8,
   2.152 -					   0, 0, 0, 0);
   2.153 -	}
   2.154 -	if ( surface == NULL )
   2.155 -		ERROR("Out of memory");
   2.156 -	bpl = width * surface->format->BytesPerPixel;
   2.157 -	if(kind == PGM) {
   2.158 -		SDL_Color *c = surface->format->palette->colors;
   2.159 -		int i;
   2.160 -		for(i = 0; i < 256; i++)
   2.161 -			c[i].r = c[i].g = c[i].b = i;
   2.162 -		surface->format->palette->ncolors = 256;
   2.163 -	} else if(kind == PBM) {
   2.164 -		/* for some reason PBM has 1=black, 0=white */
   2.165 -		SDL_Color *c = surface->format->palette->colors;
   2.166 -		c[0].r = c[0].g = c[0].b = 255;
   2.167 -		c[1].r = c[1].g = c[1].b = 0;
   2.168 -		surface->format->palette->ncolors = 2;
   2.169 -		bpl = (width + 7) >> 3;
   2.170 -		buf = malloc(bpl);
   2.171 -		if(buf == NULL)
   2.172 -			ERROR("Out of memory");
   2.173 -	}
   2.174 -
   2.175 -	/* Read the image into the surface */
   2.176 -	row = surface->pixels;
   2.177 -	for(y = 0; y < height; y++) {
   2.178 -		if(ascii) {
   2.179 -			int i;
   2.180 -			if(kind == PBM) {
   2.181 -				for(i = 0; i < width; i++) {
   2.182 -					Uint8 ch;
   2.183 -					do {
   2.184 -						if(!SDL_RWread(src, &ch,
   2.185 -							       1, 1))
   2.186 -						       ERROR("file truncated");
   2.187 -						ch -= '0';
   2.188 -					} while(ch > 1);
   2.189 -					row[i] = ch;
   2.190 -				}
   2.191 -			} else {
   2.192 -				for(i = 0; i < bpl; i++) {
   2.193 -					int c;
   2.194 -					c = ReadNumber(src);
   2.195 -					if(c < 0)
   2.196 -						ERROR("file truncated");
   2.197 -					row[i] = c;
   2.198 -				}
   2.199 -			}
   2.200 -		} else {
   2.201 -			Uint8 *dst = (kind == PBM) ? buf : row;
   2.202 -			if(!SDL_RWread(src, dst, bpl, 1))
   2.203 -				ERROR("file truncated");
   2.204 -			if(kind == PBM) {
   2.205 -				/* expand bitmap to 8bpp */
   2.206 -				int i;
   2.207 -				for(i = 0; i < width; i++) {
   2.208 -					int bit = 7 - (i & 7);
   2.209 -					row[i] = (buf[i >> 3] >> bit) & 1;
   2.210 -				}
   2.211 -			}
   2.212 -		}
   2.213 -		if(maxval < 255) {
   2.214 -			/* scale up to full dynamic range (slow) */
   2.215 -			int i;
   2.216 -			for(i = 0; i < bpl; i++)
   2.217 -				row[i] = row[i] * 255 / maxval;
   2.218 -		}
   2.219 -		row += surface->pitch;
   2.220 -	}
   2.221 -done:
   2.222 -	free(buf);
   2.223 -	if(error) {
   2.224 -		SDL_FreeSurface(surface);
   2.225 -		IMG_SetError(error);
   2.226 -		surface = NULL;
   2.227 -	}
   2.228 -	return(surface);
   2.229 -}
   2.230 -
   2.231 -#else
   2.232 -
   2.233 -/* See if an image is contained in a data source */
   2.234 -int IMG_isPNM(SDL_RWops *src)
   2.235 -{
   2.236 -	return(0);
   2.237 -}
   2.238 -
   2.239 -/* Load a PNM type image from an SDL datasource */
   2.240 -SDL_Surface *IMG_LoadPNM_RW(SDL_RWops *src)
   2.241 -{
   2.242 -	return(NULL);
   2.243 -}
   2.244 -
   2.245 -#endif /* LOAD_PNM */
     3.1 --- a/Makefile.am	Wed Mar 07 16:59:28 2001 +0000
     3.2 +++ b/Makefile.am	Wed Mar 07 17:00:59 2001 +0000
     3.3 @@ -13,7 +13,7 @@
     3.4  	IMG_jpg.c		\
     3.5  	IMG_pcx.c		\
     3.6  	IMG_png.c		\
     3.7 -	IMG_ppm.c		\
     3.8 +	IMG_pnm.c		\
     3.9  	IMG_tga.c		\
    3.10  	IMG_tif.c		\
    3.11  	IMG_xpm.c