IMG_pnm.c
changeset 368 8a61842d00ce
parent 367 b2aa197f6774
child 451 48116d511e5d
     1.1 --- a/IMG_pnm.c	Thu Apr 25 00:22:51 2013 -0700
     1.2 +++ b/IMG_pnm.c	Tue May 21 21:24:32 2013 -0700
     1.3 @@ -39,206 +39,206 @@
     1.4  /* See if an image is contained in a data source */
     1.5  int IMG_isPNM(SDL_RWops *src)
     1.6  {
     1.7 -	Sint64 start;
     1.8 -	int is_PNM;
     1.9 -	char magic[2];
    1.10 +    Sint64 start;
    1.11 +    int is_PNM;
    1.12 +    char magic[2];
    1.13  
    1.14 -	if ( !src )
    1.15 -		return 0;
    1.16 -	start = SDL_RWtell(src);
    1.17 -	is_PNM = 0;
    1.18 -	if ( SDL_RWread(src, magic, sizeof(magic), 1) ) {
    1.19 -		/*
    1.20 -		 * PNM magic signatures:
    1.21 -		 * P1	PBM, ascii format
    1.22 -		 * P2	PGM, ascii format
    1.23 -		 * P3	PPM, ascii format
    1.24 -		 * P4	PBM, binary format
    1.25 -		 * P5	PGM, binary format
    1.26 -		 * P6	PPM, binary format
    1.27 -		 * P7	PAM, a general wrapper for PNM data
    1.28 -		 */
    1.29 -		if ( magic[0] == 'P' && magic[1] >= '1' && magic[1] <= '6' ) {
    1.30 -			is_PNM = 1;
    1.31 -		}
    1.32 -	}
    1.33 -	SDL_RWseek(src, start, RW_SEEK_SET);
    1.34 -	return(is_PNM);
    1.35 +    if ( !src )
    1.36 +        return 0;
    1.37 +    start = SDL_RWtell(src);
    1.38 +    is_PNM = 0;
    1.39 +    if ( SDL_RWread(src, magic, sizeof(magic), 1) ) {
    1.40 +        /*
    1.41 +         * PNM magic signatures:
    1.42 +         * P1   PBM, ascii format
    1.43 +         * P2   PGM, ascii format
    1.44 +         * P3   PPM, ascii format
    1.45 +         * P4   PBM, binary format
    1.46 +         * P5   PGM, binary format
    1.47 +         * P6   PPM, binary format
    1.48 +         * P7   PAM, a general wrapper for PNM data
    1.49 +         */
    1.50 +        if ( magic[0] == 'P' && magic[1] >= '1' && magic[1] <= '6' ) {
    1.51 +            is_PNM = 1;
    1.52 +        }
    1.53 +    }
    1.54 +    SDL_RWseek(src, start, RW_SEEK_SET);
    1.55 +    return(is_PNM);
    1.56  }
    1.57  
    1.58  /* read a non-negative integer from the source. return -1 upon error */
    1.59  static int ReadNumber(SDL_RWops *src)
    1.60  {
    1.61 -	int number;
    1.62 -	unsigned char ch;
    1.63 +    int number;
    1.64 +    unsigned char ch;
    1.65  
    1.66 -	/* Initialize return value */
    1.67 -	number = 0;
    1.68 +    /* Initialize return value */
    1.69 +    number = 0;
    1.70  
    1.71 -	/* Skip leading whitespace */
    1.72 -	do {
    1.73 -		if ( ! SDL_RWread(src, &ch, 1, 1) ) {
    1.74 -			return(0);
    1.75 -		}
    1.76 -		/* Eat comments as whitespace */
    1.77 -		if ( ch == '#' ) {  /* Comment is '#' to end of line */
    1.78 -			do {
    1.79 -				if ( ! SDL_RWread(src, &ch, 1, 1) ) {
    1.80 -					return -1;
    1.81 -				}
    1.82 -			} while ( (ch != '\r') && (ch != '\n') );
    1.83 -		}
    1.84 -	} while ( isspace(ch) );
    1.85 +    /* Skip leading whitespace */
    1.86 +    do {
    1.87 +        if ( ! SDL_RWread(src, &ch, 1, 1) ) {
    1.88 +            return(0);
    1.89 +        }
    1.90 +        /* Eat comments as whitespace */
    1.91 +        if ( ch == '#' ) {  /* Comment is '#' to end of line */
    1.92 +            do {
    1.93 +                if ( ! SDL_RWread(src, &ch, 1, 1) ) {
    1.94 +                    return -1;
    1.95 +                }
    1.96 +            } while ( (ch != '\r') && (ch != '\n') );
    1.97 +        }
    1.98 +    } while ( isspace(ch) );
    1.99  
   1.100 -	/* Add up the number */
   1.101 -	do {
   1.102 -		number *= 10;
   1.103 -		number += ch-'0';
   1.104 +    /* Add up the number */
   1.105 +    do {
   1.106 +        number *= 10;
   1.107 +        number += ch-'0';
   1.108  
   1.109 -		if ( !SDL_RWread(src, &ch, 1, 1) ) {
   1.110 -			return -1;
   1.111 -		}
   1.112 -	} while ( isdigit(ch) );
   1.113 +        if ( !SDL_RWread(src, &ch, 1, 1) ) {
   1.114 +            return -1;
   1.115 +        }
   1.116 +    } while ( isdigit(ch) );
   1.117  
   1.118 -	return(number);
   1.119 +    return(number);
   1.120  }
   1.121  
   1.122  SDL_Surface *IMG_LoadPNM_RW(SDL_RWops *src)
   1.123  {
   1.124 -	Sint64 start;
   1.125 -	SDL_Surface *surface = NULL;
   1.126 -	int width, height;
   1.127 -	int maxval, y, bpl;
   1.128 -	Uint8 *row;
   1.129 -	Uint8 *buf = NULL;
   1.130 -	char *error = NULL;
   1.131 -	Uint8 magic[2];
   1.132 -	int ascii;
   1.133 -	enum { PBM, PGM, PPM, PAM } kind;
   1.134 +    Sint64 start;
   1.135 +    SDL_Surface *surface = NULL;
   1.136 +    int width, height;
   1.137 +    int maxval, y, bpl;
   1.138 +    Uint8 *row;
   1.139 +    Uint8 *buf = NULL;
   1.140 +    char *error = NULL;
   1.141 +    Uint8 magic[2];
   1.142 +    int ascii;
   1.143 +    enum { PBM, PGM, PPM, PAM } kind;
   1.144  
   1.145  #define ERROR(s) do { error = (s); goto done; } while(0)
   1.146  
   1.147 -	if ( !src ) {
   1.148 -		/* The error message has been set in SDL_RWFromFile */
   1.149 -		return NULL;
   1.150 -	}
   1.151 -	start = SDL_RWtell(src);
   1.152 +    if ( !src ) {
   1.153 +        /* The error message has been set in SDL_RWFromFile */
   1.154 +        return NULL;
   1.155 +    }
   1.156 +    start = SDL_RWtell(src);
   1.157  
   1.158 -	SDL_RWread(src, magic, 2, 1);
   1.159 -	kind = magic[1] - '1';
   1.160 -	ascii = 1;
   1.161 -	if(kind >= 3) {
   1.162 -		ascii = 0;
   1.163 -		kind -= 3;
   1.164 -	}
   1.165 +    SDL_RWread(src, magic, 2, 1);
   1.166 +    kind = magic[1] - '1';
   1.167 +    ascii = 1;
   1.168 +    if(kind >= 3) {
   1.169 +        ascii = 0;
   1.170 +        kind -= 3;
   1.171 +    }
   1.172  
   1.173 -	width = ReadNumber(src);
   1.174 -	height = ReadNumber(src);
   1.175 -	if(width <= 0 || height <= 0)
   1.176 -		ERROR("Unable to read image width and height");
   1.177 +    width = ReadNumber(src);
   1.178 +    height = ReadNumber(src);
   1.179 +    if(width <= 0 || height <= 0)
   1.180 +        ERROR("Unable to read image width and height");
   1.181  
   1.182 -	if(kind != PBM) {
   1.183 -		maxval = ReadNumber(src);
   1.184 -		if(maxval <= 0 || maxval > 255)
   1.185 -			ERROR("unsupported PNM format");
   1.186 -	} else
   1.187 -		maxval = 255;	/* never scale PBMs */
   1.188 +    if(kind != PBM) {
   1.189 +        maxval = ReadNumber(src);
   1.190 +        if(maxval <= 0 || maxval > 255)
   1.191 +            ERROR("unsupported PNM format");
   1.192 +    } else
   1.193 +        maxval = 255;   /* never scale PBMs */
   1.194  
   1.195 -	/* binary PNM allows just a single character of whitespace after
   1.196 -	   the last parameter, and we've already consumed it */
   1.197 +    /* binary PNM allows just a single character of whitespace after
   1.198 +       the last parameter, and we've already consumed it */
   1.199  
   1.200 -	if(kind == PPM) {
   1.201 -		/* 24-bit surface in R,G,B byte order */
   1.202 -		surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 24,
   1.203 +    if(kind == PPM) {
   1.204 +        /* 24-bit surface in R,G,B byte order */
   1.205 +        surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 24,
   1.206  #if SDL_BYTEORDER == SDL_LIL_ENDIAN
   1.207 -					   0x000000ff, 0x0000ff00, 0x00ff0000,
   1.208 +                       0x000000ff, 0x0000ff00, 0x00ff0000,
   1.209  #else
   1.210 -					   0x00ff0000, 0x0000ff00, 0x000000ff,
   1.211 +                       0x00ff0000, 0x0000ff00, 0x000000ff,
   1.212  #endif
   1.213 -					   0);
   1.214 -	} else {
   1.215 -		/* load PBM/PGM as 8-bit indexed images */
   1.216 -		surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 8,
   1.217 -					   0, 0, 0, 0);
   1.218 -	}
   1.219 -	if ( surface == NULL )
   1.220 -		ERROR("Out of memory");
   1.221 -	bpl = width * surface->format->BytesPerPixel;
   1.222 -	if(kind == PGM) {
   1.223 -		SDL_Color *c = surface->format->palette->colors;
   1.224 -		int i;
   1.225 -		for(i = 0; i < 256; i++)
   1.226 -			c[i].r = c[i].g = c[i].b = i;
   1.227 -		surface->format->palette->ncolors = 256;
   1.228 -	} else if(kind == PBM) {
   1.229 -		/* for some reason PBM has 1=black, 0=white */
   1.230 -		SDL_Color *c = surface->format->palette->colors;
   1.231 -		c[0].r = c[0].g = c[0].b = 255;
   1.232 -		c[1].r = c[1].g = c[1].b = 0;
   1.233 -		surface->format->palette->ncolors = 2;
   1.234 -		bpl = (width + 7) >> 3;
   1.235 -		buf = (Uint8 *)SDL_malloc(bpl);
   1.236 -		if(buf == NULL)
   1.237 -			ERROR("Out of memory");
   1.238 -	}
   1.239 +                       0);
   1.240 +    } else {
   1.241 +        /* load PBM/PGM as 8-bit indexed images */
   1.242 +        surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 8,
   1.243 +                       0, 0, 0, 0);
   1.244 +    }
   1.245 +    if ( surface == NULL )
   1.246 +        ERROR("Out of memory");
   1.247 +    bpl = width * surface->format->BytesPerPixel;
   1.248 +    if(kind == PGM) {
   1.249 +        SDL_Color *c = surface->format->palette->colors;
   1.250 +        int i;
   1.251 +        for(i = 0; i < 256; i++)
   1.252 +            c[i].r = c[i].g = c[i].b = i;
   1.253 +        surface->format->palette->ncolors = 256;
   1.254 +    } else if(kind == PBM) {
   1.255 +        /* for some reason PBM has 1=black, 0=white */
   1.256 +        SDL_Color *c = surface->format->palette->colors;
   1.257 +        c[0].r = c[0].g = c[0].b = 255;
   1.258 +        c[1].r = c[1].g = c[1].b = 0;
   1.259 +        surface->format->palette->ncolors = 2;
   1.260 +        bpl = (width + 7) >> 3;
   1.261 +        buf = (Uint8 *)SDL_malloc(bpl);
   1.262 +        if(buf == NULL)
   1.263 +            ERROR("Out of memory");
   1.264 +    }
   1.265  
   1.266 -	/* Read the image into the surface */
   1.267 -	row = (Uint8 *)surface->pixels;
   1.268 -	for(y = 0; y < height; y++) {
   1.269 -		if(ascii) {
   1.270 -			int i;
   1.271 -			if(kind == PBM) {
   1.272 -				for(i = 0; i < width; i++) {
   1.273 -					Uint8 ch;
   1.274 -					do {
   1.275 -						if(!SDL_RWread(src, &ch,
   1.276 -							       1, 1))
   1.277 -						       ERROR("file truncated");
   1.278 -						ch -= '0';
   1.279 -					} while(ch > 1);
   1.280 -					row[i] = ch;
   1.281 -				}
   1.282 -			} else {
   1.283 -				for(i = 0; i < bpl; i++) {
   1.284 -					int c;
   1.285 -					c = ReadNumber(src);
   1.286 -					if(c < 0)
   1.287 -						ERROR("file truncated");
   1.288 -					row[i] = c;
   1.289 -				}
   1.290 -			}
   1.291 -		} else {
   1.292 -			Uint8 *dst = (kind == PBM) ? buf : row;
   1.293 -			if(!SDL_RWread(src, dst, bpl, 1))
   1.294 -				ERROR("file truncated");
   1.295 -			if(kind == PBM) {
   1.296 -				/* expand bitmap to 8bpp */
   1.297 -				int i;
   1.298 -				for(i = 0; i < width; i++) {
   1.299 -					int bit = 7 - (i & 7);
   1.300 -					row[i] = (buf[i >> 3] >> bit) & 1;
   1.301 -				}
   1.302 -			}
   1.303 -		}
   1.304 -		if(maxval < 255) {
   1.305 -			/* scale up to full dynamic range (slow) */
   1.306 -			int i;
   1.307 -			for(i = 0; i < bpl; i++)
   1.308 -				row[i] = row[i] * 255 / maxval;
   1.309 -		}
   1.310 -		row += surface->pitch;
   1.311 -	}
   1.312 +    /* Read the image into the surface */
   1.313 +    row = (Uint8 *)surface->pixels;
   1.314 +    for(y = 0; y < height; y++) {
   1.315 +        if(ascii) {
   1.316 +            int i;
   1.317 +            if(kind == PBM) {
   1.318 +                for(i = 0; i < width; i++) {
   1.319 +                    Uint8 ch;
   1.320 +                    do {
   1.321 +                        if(!SDL_RWread(src, &ch,
   1.322 +                                   1, 1))
   1.323 +                               ERROR("file truncated");
   1.324 +                        ch -= '0';
   1.325 +                    } while(ch > 1);
   1.326 +                    row[i] = ch;
   1.327 +                }
   1.328 +            } else {
   1.329 +                for(i = 0; i < bpl; i++) {
   1.330 +                    int c;
   1.331 +                    c = ReadNumber(src);
   1.332 +                    if(c < 0)
   1.333 +                        ERROR("file truncated");
   1.334 +                    row[i] = c;
   1.335 +                }
   1.336 +            }
   1.337 +        } else {
   1.338 +            Uint8 *dst = (kind == PBM) ? buf : row;
   1.339 +            if(!SDL_RWread(src, dst, bpl, 1))
   1.340 +                ERROR("file truncated");
   1.341 +            if(kind == PBM) {
   1.342 +                /* expand bitmap to 8bpp */
   1.343 +                int i;
   1.344 +                for(i = 0; i < width; i++) {
   1.345 +                    int bit = 7 - (i & 7);
   1.346 +                    row[i] = (buf[i >> 3] >> bit) & 1;
   1.347 +                }
   1.348 +            }
   1.349 +        }
   1.350 +        if(maxval < 255) {
   1.351 +            /* scale up to full dynamic range (slow) */
   1.352 +            int i;
   1.353 +            for(i = 0; i < bpl; i++)
   1.354 +                row[i] = row[i] * 255 / maxval;
   1.355 +        }
   1.356 +        row += surface->pitch;
   1.357 +    }
   1.358  done:
   1.359 -	SDL_free(buf);
   1.360 -	if(error) {
   1.361 -		SDL_RWseek(src, start, RW_SEEK_SET);
   1.362 -		if ( surface ) {
   1.363 -			SDL_FreeSurface(surface);
   1.364 -			surface = NULL;
   1.365 -		}
   1.366 -		IMG_SetError(error);
   1.367 -	}
   1.368 -	return(surface);
   1.369 +    SDL_free(buf);
   1.370 +    if(error) {
   1.371 +        SDL_RWseek(src, start, RW_SEEK_SET);
   1.372 +        if ( surface ) {
   1.373 +            SDL_FreeSurface(surface);
   1.374 +            surface = NULL;
   1.375 +        }
   1.376 +        IMG_SetError(error);
   1.377 +    }
   1.378 +    return(surface);
   1.379  }
   1.380  
   1.381  #else
   1.382 @@ -246,13 +246,13 @@
   1.383  /* See if an image is contained in a data source */
   1.384  int IMG_isPNM(SDL_RWops *src)
   1.385  {
   1.386 -	return(0);
   1.387 +    return(0);
   1.388  }
   1.389  
   1.390  /* Load a PNM type image from an SDL datasource */
   1.391  SDL_Surface *IMG_LoadPNM_RW(SDL_RWops *src)
   1.392  {
   1.393 -	return(NULL);
   1.394 +    return(NULL);
   1.395  }
   1.396  
   1.397  #endif /* LOAD_PNM */