IMG_ppm.c
changeset 25 c9117619e2af
parent 24 69f3d44d56da
child 26 d51719e1b5d9
     1.1 --- a/IMG_ppm.c	Wed Mar 07 16:59:28 2001 +0000
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,242 +0,0 @@
     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 */