IMG_pnm.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 04 Feb 2006 20:37:17 +0000
changeset 117 e613cf987897
parent 98 9f94c4674cc9
child 118 c5e736a47ad2
permissions -rw-r--r--
Fixed image type functions so they seek back to where they started
     1 /*
     2     SDL_image:  An example image loading library for use with SDL
     3     Copyright (C) 1999-2004 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Library General Public
     7     License as published by the Free Software Foundation; either
     8     version 2 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Library General Public License for more details.
    14 
    15     You should have received a copy of the GNU Library General Public
    16     License along with this library; if not, write to the Free
    17     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 
    23 /* $Id$ */
    24 
    25 /*
    26  * PNM (portable anymap) image loader:
    27  *
    28  * Supports: PBM, PGM and PPM, ASCII and binary formats
    29  * (PBM and PGM are loaded as 8bpp surfaces)
    30  * Does not support: maximum component value > 255
    31  */
    32 
    33 #include <stdio.h>
    34 #include <stdlib.h>
    35 #include <ctype.h>
    36 #include <string.h>
    37 
    38 #include "SDL_image.h"
    39 
    40 #ifdef LOAD_PNM
    41 
    42 /* See if an image is contained in a data source */
    43 int IMG_isPNM(SDL_RWops *src)
    44 {
    45 	int start;
    46 	int is_PNM;
    47 	char magic[2];
    48 
    49 	start = SDL_RWtell(src);
    50 	is_PNM = 0;
    51 	if ( SDL_RWread(src, magic, sizeof(magic), 1) ) {
    52 		/*
    53 		 * PNM magic signatures:
    54 		 * P1	PBM, ascii format
    55 		 * P2	PGM, ascii format
    56 		 * P3	PPM, ascii format
    57 		 * P4	PBM, binary format
    58 		 * P5	PGM, binary format
    59 		 * P6	PPM, binary format
    60 		 */
    61 		if ( magic[0] == 'P' && magic[1] >= '1' && magic[1] <= '6' ) {
    62 			is_PNM = 1;
    63 		}
    64 	}
    65 	SDL_RWseek(src, start, SEEK_SET);
    66 	return(is_PNM);
    67 }
    68 
    69 /* read a non-negative integer from the source. return -1 upon error */
    70 static int ReadNumber(SDL_RWops *src)
    71 {
    72 	int number;
    73 	unsigned char ch;
    74 
    75 	/* Initialize return value */
    76 	number = 0;
    77 
    78 	/* Skip leading whitespace */
    79 	do {
    80 		if ( ! SDL_RWread(src, &ch, 1, 1) ) {
    81 			return(0);
    82 		}
    83 		/* Eat comments as whitespace */
    84 		if ( ch == '#' ) {  /* Comment is '#' to end of line */
    85 			do {
    86 				if ( ! SDL_RWread(src, &ch, 1, 1) ) {
    87 					return -1;
    88 				}
    89 			} while ( (ch != '\r') && (ch != '\n') );
    90 		}
    91 	} while ( isspace(ch) );
    92 
    93 	/* Add up the number */
    94 	do {
    95 		number *= 10;
    96 		number += ch-'0';
    97 
    98 		if ( !SDL_RWread(src, &ch, 1, 1) ) {
    99 			return -1;
   100 		}
   101 	} while ( isdigit(ch) );
   102 
   103 	return(number);
   104 }
   105 
   106 SDL_Surface *IMG_LoadPNM_RW(SDL_RWops *src)
   107 {
   108 	SDL_Surface *surface = NULL;
   109 	int width, height;
   110 	int maxval, y, bpl;
   111 	Uint8 *row;
   112 	Uint8 *buf = NULL;
   113 	char *error = NULL;
   114 	Uint8 magic[2];
   115 	int ascii;
   116 	enum { PBM, PGM, PPM } kind;
   117 
   118 #define ERROR(s) do { error = (s); goto done; } while(0)
   119 
   120 	if ( !src ) {
   121 		/* The error message has been set in SDL_RWFromFile */
   122 		return NULL;
   123 	}
   124 
   125 	SDL_RWread(src, magic, 2, 1);
   126 	kind = magic[1] - '1';
   127 	ascii = 1;
   128 	if(kind >= 3) {
   129 		ascii = 0;
   130 		kind -= 3;
   131 	}
   132 
   133 	width = ReadNumber(src);
   134 	height = ReadNumber(src);
   135 	if(width <= 0 || height <= 0)
   136 		ERROR("Unable to read image width and height");
   137 
   138 	if(kind != PBM) {
   139 		maxval = ReadNumber(src);
   140 		if(maxval <= 0 || maxval > 255)
   141 			ERROR("unsupported PNM format");
   142 	} else
   143 		maxval = 255;	/* never scale PBMs */
   144 
   145 	/* binary PNM allows just a single character of whitespace after
   146 	   the last parameter, and we've already consumed it */
   147 
   148 	if(kind == PPM) {
   149 		/* 24-bit surface in R,G,B byte order */
   150 		surface = SDL_AllocSurface(SDL_SWSURFACE, width, height, 24,
   151 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
   152 					   0x000000ff, 0x0000ff00, 0x00ff0000,
   153 #else
   154 					   0x00ff0000, 0x0000ff00, 0x000000ff,
   155 #endif
   156 					   0);
   157 	} else {
   158 		/* load PBM/PGM as 8-bit indexed images */
   159 		surface = SDL_AllocSurface(SDL_SWSURFACE, width, height, 8,
   160 					   0, 0, 0, 0);
   161 	}
   162 	if ( surface == NULL )
   163 		ERROR("Out of memory");
   164 	bpl = width * surface->format->BytesPerPixel;
   165 	if(kind == PGM) {
   166 		SDL_Color *c = surface->format->palette->colors;
   167 		int i;
   168 		for(i = 0; i < 256; i++)
   169 			c[i].r = c[i].g = c[i].b = i;
   170 		surface->format->palette->ncolors = 256;
   171 	} else if(kind == PBM) {
   172 		/* for some reason PBM has 1=black, 0=white */
   173 		SDL_Color *c = surface->format->palette->colors;
   174 		c[0].r = c[0].g = c[0].b = 255;
   175 		c[1].r = c[1].g = c[1].b = 0;
   176 		surface->format->palette->ncolors = 2;
   177 		bpl = (width + 7) >> 3;
   178 		buf = malloc(bpl);
   179 		if(buf == NULL)
   180 			ERROR("Out of memory");
   181 	}
   182 
   183 	/* Read the image into the surface */
   184 	row = surface->pixels;
   185 	for(y = 0; y < height; y++) {
   186 		if(ascii) {
   187 			int i;
   188 			if(kind == PBM) {
   189 				for(i = 0; i < width; i++) {
   190 					Uint8 ch;
   191 					do {
   192 						if(!SDL_RWread(src, &ch,
   193 							       1, 1))
   194 						       ERROR("file truncated");
   195 						ch -= '0';
   196 					} while(ch > 1);
   197 					row[i] = ch;
   198 				}
   199 			} else {
   200 				for(i = 0; i < bpl; i++) {
   201 					int c;
   202 					c = ReadNumber(src);
   203 					if(c < 0)
   204 						ERROR("file truncated");
   205 					row[i] = c;
   206 				}
   207 			}
   208 		} else {
   209 			Uint8 *dst = (kind == PBM) ? buf : row;
   210 			if(!SDL_RWread(src, dst, bpl, 1))
   211 				ERROR("file truncated");
   212 			if(kind == PBM) {
   213 				/* expand bitmap to 8bpp */
   214 				int i;
   215 				for(i = 0; i < width; i++) {
   216 					int bit = 7 - (i & 7);
   217 					row[i] = (buf[i >> 3] >> bit) & 1;
   218 				}
   219 			}
   220 		}
   221 		if(maxval < 255) {
   222 			/* scale up to full dynamic range (slow) */
   223 			int i;
   224 			for(i = 0; i < bpl; i++)
   225 				row[i] = row[i] * 255 / maxval;
   226 		}
   227 		row += surface->pitch;
   228 	}
   229 done:
   230 	free(buf);
   231 	if(error) {
   232 		SDL_FreeSurface(surface);
   233 		IMG_SetError(error);
   234 		surface = NULL;
   235 	}
   236 	return(surface);
   237 }
   238 
   239 #else
   240 
   241 /* See if an image is contained in a data source */
   242 int IMG_isPNM(SDL_RWops *src)
   243 {
   244 	return(0);
   245 }
   246 
   247 /* Load a PNM type image from an SDL datasource */
   248 SDL_Surface *IMG_LoadPNM_RW(SDL_RWops *src)
   249 {
   250 	return(NULL);
   251 }
   252 
   253 #endif /* LOAD_PNM */