IMG_pnm.c
author Thomas Bernard <miniupnp@free.fr>
Fri, 30 Nov 2018 11:04:15 +0100
branchSDL-1.2
changeset 634 68f958f43339
parent 607 1a1189c2978f
permissions -rw-r--r--
IMG_xcf.c: Avoid infinite loop in read_xcf_header()
slouken@25
     1
/*
slouken@280
     2
  SDL_image:  An example image loading library for use with SDL
slouken@280
     3
  Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
slouken@25
     4
slouken@280
     5
  This software is provided 'as-is', without any express or implied
slouken@280
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@280
     7
  arising from the use of this software.
slouken@25
     8
slouken@280
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@280
    10
  including commercial applications, and to alter it and redistribute it
slouken@280
    11
  freely, subject to the following restrictions:
slouken@25
    12
slouken@280
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@280
    14
     claim that you wrote the original software. If you use this software
slouken@280
    15
     in a product, an acknowledgment in the product documentation would be
slouken@280
    16
     appreciated but is not required.
slouken@280
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@280
    18
     misrepresented as being the original software.
slouken@280
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@25
    20
*/
slouken@25
    21
slouken@25
    22
/*
slouken@25
    23
 * PNM (portable anymap) image loader:
slouken@25
    24
 *
slouken@25
    25
 * Supports: PBM, PGM and PPM, ASCII and binary formats
slouken@25
    26
 * (PBM and PGM are loaded as 8bpp surfaces)
slouken@25
    27
 * Does not support: maximum component value > 255
slouken@25
    28
 */
slouken@25
    29
sezeroz@607
    30
#include <limits.h>
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
icculus@154
    47
	if ( !src )
icculus@154
    48
		return 0;
slouken@117
    49
	start = SDL_RWtell(src);
slouken@117
    50
	is_PNM = 0;
slouken@117
    51
	if ( SDL_RWread(src, magic, sizeof(magic), 1) ) {
slouken@117
    52
		/*
slouken@117
    53
		 * PNM magic signatures:
slouken@117
    54
		 * P1	PBM, ascii format
slouken@117
    55
		 * P2	PGM, ascii format
slouken@117
    56
		 * P3	PPM, ascii format
slouken@117
    57
		 * P4	PBM, binary format
slouken@117
    58
		 * P5	PGM, binary format
slouken@117
    59
		 * P6	PPM, binary format
slouken@119
    60
		 * P7	PAM, a general wrapper for PNM data
slouken@117
    61
		 */
slouken@117
    62
		if ( magic[0] == 'P' && magic[1] >= '1' && magic[1] <= '6' ) {
slouken@117
    63
			is_PNM = 1;
slouken@117
    64
		}
slouken@117
    65
	}
slouken@236
    66
	SDL_RWseek(src, start, RW_SEEK_SET);
slouken@117
    67
	return(is_PNM);
slouken@25
    68
}
slouken@25
    69
slouken@25
    70
/* read a non-negative integer from the source. return -1 upon error */
slouken@25
    71
static int ReadNumber(SDL_RWops *src)
slouken@25
    72
{
slouken@25
    73
	int number;
slouken@25
    74
	unsigned char ch;
slouken@25
    75
slouken@25
    76
	/* Initialize return value */
slouken@25
    77
	number = 0;
slouken@25
    78
slouken@25
    79
	/* Skip leading whitespace */
slouken@25
    80
	do {
slouken@25
    81
		if ( ! SDL_RWread(src, &ch, 1, 1) ) {
sezeroz@607
    82
			return(-1);
slouken@25
    83
		}
slouken@25
    84
		/* Eat comments as whitespace */
slouken@25
    85
		if ( ch == '#' ) {  /* Comment is '#' to end of line */
slouken@25
    86
			do {
slouken@25
    87
				if ( ! SDL_RWread(src, &ch, 1, 1) ) {
slouken@25
    88
					return -1;
slouken@25
    89
				}
slouken@25
    90
			} while ( (ch != '\r') && (ch != '\n') );
slouken@25
    91
		}
slouken@25
    92
	} while ( isspace(ch) );
slouken@25
    93
slouken@25
    94
	/* Add up the number */
sezeroz@607
    95
	if (!isdigit(ch)) {
sezeroz@607
    96
		return -1;
sezeroz@607
    97
	}
slouken@25
    98
	do {
sezeroz@607
    99
		/* Protect from possible overflow */
sezeroz@607
   100
		if (number >= INT_MAX / 10) {
sezeroz@607
   101
			return -1;
sezeroz@607
   102
		}
slouken@25
   103
		number *= 10;
slouken@25
   104
		number += ch-'0';
slouken@25
   105
slouken@25
   106
		if ( !SDL_RWread(src, &ch, 1, 1) ) {
slouken@25
   107
			return -1;
slouken@25
   108
		}
slouken@25
   109
	} while ( isdigit(ch) );
slouken@25
   110
slouken@25
   111
	return(number);
slouken@25
   112
}
slouken@25
   113
slouken@25
   114
SDL_Surface *IMG_LoadPNM_RW(SDL_RWops *src)
slouken@25
   115
{
slouken@118
   116
	int start;
slouken@25
   117
	SDL_Surface *surface = NULL;
slouken@25
   118
	int width, height;
slouken@25
   119
	int maxval, y, bpl;
slouken@25
   120
	Uint8 *row;
slouken@25
   121
	Uint8 *buf = NULL;
slouken@25
   122
	char *error = NULL;
slouken@25
   123
	Uint8 magic[2];
slouken@25
   124
	int ascii;
slouken@119
   125
	enum { PBM, PGM, PPM, PAM } kind;
slouken@25
   126
slouken@25
   127
#define ERROR(s) do { error = (s); goto done; } while(0)
slouken@25
   128
slouken@98
   129
	if ( !src ) {
slouken@98
   130
		/* The error message has been set in SDL_RWFromFile */
slouken@98
   131
		return NULL;
slouken@98
   132
	}
slouken@118
   133
	start = SDL_RWtell(src);
slouken@98
   134
slouken@25
   135
	SDL_RWread(src, magic, 2, 1);
slouken@25
   136
	kind = magic[1] - '1';
slouken@25
   137
	ascii = 1;
slouken@25
   138
	if(kind >= 3) {
slouken@25
   139
		ascii = 0;
slouken@25
   140
		kind -= 3;
slouken@25
   141
	}
slouken@25
   142
slouken@25
   143
	width = ReadNumber(src);
slouken@25
   144
	height = ReadNumber(src);
slouken@25
   145
	if(width <= 0 || height <= 0)
slouken@25
   146
		ERROR("Unable to read image width and height");
slouken@25
   147
slouken@25
   148
	if(kind != PBM) {
slouken@25
   149
		maxval = ReadNumber(src);
slouken@25
   150
		if(maxval <= 0 || maxval > 255)
slouken@25
   151
			ERROR("unsupported PNM format");
slouken@25
   152
	} else
slouken@25
   153
		maxval = 255;	/* never scale PBMs */
slouken@25
   154
slouken@25
   155
	/* binary PNM allows just a single character of whitespace after
slouken@25
   156
	   the last parameter, and we've already consumed it */
slouken@25
   157
slouken@25
   158
	if(kind == PPM) {
slouken@25
   159
		/* 24-bit surface in R,G,B byte order */
slouken@25
   160
		surface = SDL_AllocSurface(SDL_SWSURFACE, width, height, 24,
slouken@25
   161
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@25
   162
					   0x000000ff, 0x0000ff00, 0x00ff0000,
slouken@25
   163
#else
slouken@25
   164
					   0x00ff0000, 0x0000ff00, 0x000000ff,
slouken@25
   165
#endif
slouken@25
   166
					   0);
slouken@25
   167
	} else {
slouken@25
   168
		/* load PBM/PGM as 8-bit indexed images */
slouken@25
   169
		surface = SDL_AllocSurface(SDL_SWSURFACE, width, height, 8,
slouken@25
   170
					   0, 0, 0, 0);
slouken@25
   171
	}
slouken@25
   172
	if ( surface == NULL )
slouken@25
   173
		ERROR("Out of memory");
slouken@25
   174
	bpl = width * surface->format->BytesPerPixel;
slouken@25
   175
	if(kind == PGM) {
slouken@25
   176
		SDL_Color *c = surface->format->palette->colors;
slouken@25
   177
		int i;
slouken@25
   178
		for(i = 0; i < 256; i++)
slouken@25
   179
			c[i].r = c[i].g = c[i].b = i;
slouken@25
   180
		surface->format->palette->ncolors = 256;
slouken@25
   181
	} else if(kind == PBM) {
slouken@25
   182
		/* for some reason PBM has 1=black, 0=white */
slouken@25
   183
		SDL_Color *c = surface->format->palette->colors;
slouken@25
   184
		c[0].r = c[0].g = c[0].b = 255;
slouken@25
   185
		c[1].r = c[1].g = c[1].b = 0;
slouken@25
   186
		surface->format->palette->ncolors = 2;
slouken@25
   187
		bpl = (width + 7) >> 3;
sezeroz@607
   188
		buf = (Uint8 *)malloc(bpl);
slouken@25
   189
		if(buf == NULL)
slouken@25
   190
			ERROR("Out of memory");
slouken@25
   191
	}
slouken@25
   192
slouken@25
   193
	/* Read the image into the surface */
sezeroz@607
   194
	row = (Uint8 *)surface->pixels;
slouken@25
   195
	for(y = 0; y < height; y++) {
slouken@25
   196
		if(ascii) {
slouken@25
   197
			int i;
slouken@25
   198
			if(kind == PBM) {
slouken@25
   199
				for(i = 0; i < width; i++) {
slouken@25
   200
					Uint8 ch;
slouken@25
   201
					do {
slouken@25
   202
						if(!SDL_RWread(src, &ch,
slouken@25
   203
							       1, 1))
slouken@25
   204
						       ERROR("file truncated");
slouken@25
   205
						ch -= '0';
slouken@25
   206
					} while(ch > 1);
slouken@25
   207
					row[i] = ch;
slouken@25
   208
				}
slouken@25
   209
			} else {
slouken@25
   210
				for(i = 0; i < bpl; i++) {
slouken@25
   211
					int c;
slouken@25
   212
					c = ReadNumber(src);
slouken@25
   213
					if(c < 0)
slouken@25
   214
						ERROR("file truncated");
slouken@25
   215
					row[i] = c;
slouken@25
   216
				}
slouken@25
   217
			}
slouken@25
   218
		} else {
slouken@25
   219
			Uint8 *dst = (kind == PBM) ? buf : row;
slouken@25
   220
			if(!SDL_RWread(src, dst, bpl, 1))
slouken@25
   221
				ERROR("file truncated");
slouken@25
   222
			if(kind == PBM) {
slouken@25
   223
				/* expand bitmap to 8bpp */
slouken@25
   224
				int i;
slouken@25
   225
				for(i = 0; i < width; i++) {
slouken@25
   226
					int bit = 7 - (i & 7);
slouken@25
   227
					row[i] = (buf[i >> 3] >> bit) & 1;
slouken@25
   228
				}
slouken@25
   229
			}
slouken@25
   230
		}
slouken@25
   231
		if(maxval < 255) {
slouken@25
   232
			/* scale up to full dynamic range (slow) */
slouken@25
   233
			int i;
slouken@25
   234
			for(i = 0; i < bpl; i++)
slouken@25
   235
				row[i] = row[i] * 255 / maxval;
slouken@25
   236
		}
slouken@25
   237
		row += surface->pitch;
slouken@25
   238
	}
slouken@25
   239
done:
slouken@25
   240
	free(buf);
slouken@25
   241
	if(error) {
slouken@236
   242
		SDL_RWseek(src, start, RW_SEEK_SET);
slouken@118
   243
		if ( surface ) {
slouken@118
   244
			SDL_FreeSurface(surface);
slouken@118
   245
			surface = NULL;
slouken@118
   246
		}
slouken@25
   247
		IMG_SetError(error);
slouken@25
   248
	}
slouken@25
   249
	return(surface);
slouken@25
   250
}
slouken@25
   251
slouken@25
   252
#else
slouken@25
   253
slouken@25
   254
/* See if an image is contained in a data source */
slouken@25
   255
int IMG_isPNM(SDL_RWops *src)
slouken@25
   256
{
slouken@25
   257
	return(0);
slouken@25
   258
}
slouken@25
   259
slouken@25
   260
/* Load a PNM type image from an SDL datasource */
slouken@25
   261
SDL_Surface *IMG_LoadPNM_RW(SDL_RWops *src)
slouken@25
   262
{
slouken@25
   263
	return(NULL);
slouken@25
   264
}
slouken@25
   265
slouken@25
   266
#endif /* LOAD_PNM */