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