IMG_pnm.c
branchSDL-1.2
changeset 607 1a1189c2978f
parent 280 ec4ae96c100c
equal deleted inserted replaced
606:4872fdc71670 607:1a1189c2978f
    25  * Supports: PBM, PGM and PPM, ASCII and binary formats
    25  * Supports: PBM, PGM and PPM, ASCII and binary formats
    26  * (PBM and PGM are loaded as 8bpp surfaces)
    26  * (PBM and PGM are loaded as 8bpp surfaces)
    27  * Does not support: maximum component value > 255
    27  * Does not support: maximum component value > 255
    28  */
    28  */
    29 
    29 
       
    30 #include <limits.h>
    30 #include <stdio.h>
    31 #include <stdio.h>
    31 #include <stdlib.h>
    32 #include <stdlib.h>
    32 #include <ctype.h>
    33 #include <ctype.h>
    33 #include <string.h>
    34 #include <string.h>
    34 
    35 
    76 	number = 0;
    77 	number = 0;
    77 
    78 
    78 	/* Skip leading whitespace */
    79 	/* Skip leading whitespace */
    79 	do {
    80 	do {
    80 		if ( ! SDL_RWread(src, &ch, 1, 1) ) {
    81 		if ( ! SDL_RWread(src, &ch, 1, 1) ) {
    81 			return(0);
    82 			return(-1);
    82 		}
    83 		}
    83 		/* Eat comments as whitespace */
    84 		/* Eat comments as whitespace */
    84 		if ( ch == '#' ) {  /* Comment is '#' to end of line */
    85 		if ( ch == '#' ) {  /* Comment is '#' to end of line */
    85 			do {
    86 			do {
    86 				if ( ! SDL_RWread(src, &ch, 1, 1) ) {
    87 				if ( ! SDL_RWread(src, &ch, 1, 1) ) {
    89 			} while ( (ch != '\r') && (ch != '\n') );
    90 			} while ( (ch != '\r') && (ch != '\n') );
    90 		}
    91 		}
    91 	} while ( isspace(ch) );
    92 	} while ( isspace(ch) );
    92 
    93 
    93 	/* Add up the number */
    94 	/* Add up the number */
       
    95 	if (!isdigit(ch)) {
       
    96 		return -1;
       
    97 	}
    94 	do {
    98 	do {
       
    99 		/* Protect from possible overflow */
       
   100 		if (number >= INT_MAX / 10) {
       
   101 			return -1;
       
   102 		}
    95 		number *= 10;
   103 		number *= 10;
    96 		number += ch-'0';
   104 		number += ch-'0';
    97 
   105 
    98 		if ( !SDL_RWread(src, &ch, 1, 1) ) {
   106 		if ( !SDL_RWread(src, &ch, 1, 1) ) {
    99 			return -1;
   107 			return -1;
   175 		SDL_Color *c = surface->format->palette->colors;
   183 		SDL_Color *c = surface->format->palette->colors;
   176 		c[0].r = c[0].g = c[0].b = 255;
   184 		c[0].r = c[0].g = c[0].b = 255;
   177 		c[1].r = c[1].g = c[1].b = 0;
   185 		c[1].r = c[1].g = c[1].b = 0;
   178 		surface->format->palette->ncolors = 2;
   186 		surface->format->palette->ncolors = 2;
   179 		bpl = (width + 7) >> 3;
   187 		bpl = (width + 7) >> 3;
   180 		buf = malloc(bpl);
   188 		buf = (Uint8 *)malloc(bpl);
   181 		if(buf == NULL)
   189 		if(buf == NULL)
   182 			ERROR("Out of memory");
   190 			ERROR("Out of memory");
   183 	}
   191 	}
   184 
   192 
   185 	/* Read the image into the surface */
   193 	/* Read the image into the surface */
   186 	row = surface->pixels;
   194 	row = (Uint8 *)surface->pixels;
   187 	for(y = 0; y < height; y++) {
   195 	for(y = 0; y < height; y++) {
   188 		if(ascii) {
   196 		if(ascii) {
   189 			int i;
   197 			int i;
   190 			if(kind == PBM) {
   198 			if(kind == PBM) {
   191 				for(i = 0; i < width; i++) {
   199 				for(i = 0; i < width; i++) {