IMG_pcx.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  * PCX file reader:
    27  * Supports:
    28  *  1..4 bits/pixel in multiplanar format (1 bit/plane/pixel)
    29  *  8 bits/pixel in single-planar format (8 bits/plane/pixel)
    30  *  24 bits/pixel in 3-plane format (8 bits/plane/pixel)
    31  *
    32  * (The <8bpp formats are expanded to 8bpp surfaces)
    33  *
    34  * Doesn't support:
    35  *  single-planar packed-pixel formats other than 8bpp
    36  *  4-plane 32bpp format with a fourth "intensity" plane
    37  */
    38 #include <stdio.h>
    39 #include <stdlib.h>
    40 
    41 #include "SDL_endian.h"
    42 
    43 #include "SDL_image.h"
    44 
    45 #ifdef LOAD_PCX
    46 
    47 struct PCXheader {
    48 	Uint8 Manufacturer;
    49 	Uint8 Version;
    50 	Uint8 Encoding;
    51 	Uint8 BitsPerPixel;
    52 	Sint16 Xmin, Ymin, Xmax, Ymax;
    53 	Sint16 HDpi, VDpi;
    54 	Uint8 Colormap[48];
    55 	Uint8 Reserved;
    56 	Uint8 NPlanes;
    57 	Sint16 BytesPerLine;
    58 	Sint16 PaletteInfo;
    59 	Sint16 HscreenSize;
    60 	Sint16 VscreenSize;
    61 	Uint8 Filler[54];
    62 };
    63 
    64 /* See if an image is contained in a data source */
    65 int IMG_isPCX(SDL_RWops *src)
    66 {
    67 	int start;
    68 	int is_PCX;
    69 	const int ZSoft_Manufacturer = 10;
    70 	const int PC_Paintbrush_Version = 5;
    71 	const int PCX_RunLength_Encoding = 1;
    72 	struct PCXheader pcxh;
    73 
    74 	start = SDL_RWtell(src);
    75 	is_PCX = 0;
    76 	if ( SDL_RWread(src, &pcxh, sizeof(pcxh), 1) == 1 ) {
    77 		if ( (pcxh.Manufacturer == ZSoft_Manufacturer) &&
    78 		     (pcxh.Version == PC_Paintbrush_Version) &&
    79 		     (pcxh.Encoding == PCX_RunLength_Encoding) ) {
    80 			is_PCX = 1;
    81 		}
    82 	}
    83 	SDL_RWseek(src, start, SEEK_SET);
    84 	return(is_PCX);
    85 }
    86 
    87 /* Load a PCX type image from an SDL datasource */
    88 SDL_Surface *IMG_LoadPCX_RW(SDL_RWops *src)
    89 {
    90 	struct PCXheader pcxh;
    91 	Uint32 Rmask;
    92 	Uint32 Gmask;
    93 	Uint32 Bmask;
    94 	Uint32 Amask;
    95 	SDL_Surface *surface = NULL;
    96 	int width, height;
    97 	int y, bpl;
    98 	Uint8 *row, *buf = NULL;
    99 	char *error = NULL;
   100 	int bits, src_bits;
   101 
   102 	if ( !src ) {
   103 		/* The error message has been set in SDL_RWFromFile */
   104 		return NULL;
   105 	}
   106 	if ( ! SDL_RWread(src, &pcxh, sizeof(pcxh), 1) ) {
   107 		error = "file truncated";
   108 		goto done;
   109 	}
   110 	pcxh.Xmin = SDL_SwapLE16(pcxh.Xmin);
   111 	pcxh.Ymin = SDL_SwapLE16(pcxh.Ymin);
   112 	pcxh.Xmax = SDL_SwapLE16(pcxh.Xmax);
   113 	pcxh.Ymax = SDL_SwapLE16(pcxh.Ymax);
   114 	pcxh.BytesPerLine = SDL_SwapLE16(pcxh.BytesPerLine);
   115 
   116 	/* Create the surface of the appropriate type */
   117 	width = (pcxh.Xmax - pcxh.Xmin) + 1;
   118 	height = (pcxh.Ymax - pcxh.Ymin) + 1;
   119 	Rmask = Gmask = Bmask = Amask = 0;
   120 	src_bits = pcxh.BitsPerPixel * pcxh.NPlanes;
   121 	if((pcxh.BitsPerPixel == 1 && pcxh.NPlanes >= 1 && pcxh.NPlanes <= 4)
   122 	   || (pcxh.BitsPerPixel == 8 && pcxh.NPlanes == 1)) {
   123 		bits = 8;
   124 	} else if(pcxh.BitsPerPixel == 8 && pcxh.NPlanes == 3) {
   125 		bits = 24;
   126 		if ( SDL_BYTEORDER == SDL_LIL_ENDIAN ) {
   127 			Rmask = 0x000000FF;
   128 			Gmask = 0x0000FF00;
   129 			Bmask = 0x00FF0000;
   130 		} else {
   131 			Rmask = 0xFF0000;
   132 			Gmask = 0x00FF00;
   133 			Bmask = 0x0000FF;
   134 		}
   135 	} else {
   136 		error = "unsupported PCX format";
   137 		goto done;
   138 	}
   139 	surface = SDL_AllocSurface(SDL_SWSURFACE, width, height,
   140 				   bits, Rmask, Gmask, Bmask, Amask);
   141 	if ( surface == NULL )
   142 		goto done;
   143 
   144 	bpl = pcxh.NPlanes * pcxh.BytesPerLine;
   145 	buf = malloc(bpl);
   146 	row = surface->pixels;
   147 	for ( y=0; y<surface->h; ++y ) {
   148 		/* decode a scan line to a temporary buffer first */
   149 		int i, count = 0;
   150 		Uint8 ch;
   151 		Uint8 *dst = (src_bits == 8) ? row : buf;
   152 		for(i = 0; i < bpl; i++) {
   153 			if(!count) {
   154 				if(!SDL_RWread(src, &ch, 1, 1)) {
   155 					error = "file truncated";
   156 					goto done;
   157 				}
   158 				if( (ch & 0xc0) == 0xc0) {
   159 					count = ch & 0x3f;
   160 					if(!SDL_RWread(src, &ch, 1, 1)) {
   161 						error = "file truncated";
   162 						goto done;
   163 					}
   164 				} else
   165 					count = 1;
   166 			}
   167 			dst[i] = ch;
   168 			count--;
   169 		}
   170 
   171 		if(src_bits <= 4) {
   172 			/* expand planes to 1 byte/pixel */
   173 			Uint8 *src = buf;
   174 			int plane;
   175 			for(plane = 0; plane < pcxh.NPlanes; plane++) {
   176 				int i, j, x = 0;
   177 				for(i = 0; i < pcxh.BytesPerLine; i++) {
   178 					Uint8 byte = *src++;
   179 					for(j = 7; j >= 0; j--) {
   180 						unsigned bit = (byte >> j) & 1;
   181 						/* skip padding bits */
   182 						if (i * 8 + j >= width)
   183 							continue;
   184 						row[x++] |= bit << plane;
   185 					}
   186 				}
   187 			}
   188  		} else if(src_bits == 24) {
   189 			/* de-interlace planes */
   190 			Uint8 *src = buf;
   191 			int plane;
   192 			for(plane = 0; plane < pcxh.NPlanes; plane++) {
   193 				int x;
   194 				dst = row + plane;
   195 				for(x = 0; x < width; x++) {
   196 					*dst = *src++;
   197 					dst += pcxh.NPlanes;
   198 				}
   199 			}
   200 		}
   201 
   202 		row += surface->pitch;
   203 	}
   204 
   205 	if(bits == 8) {
   206 		SDL_Color *colors = surface->format->palette->colors;
   207 		int nc = 1 << src_bits;
   208 		int i;
   209 
   210 		surface->format->palette->ncolors = nc;
   211 		if(src_bits == 8) {
   212 			Uint8 ch;
   213 			/* look for a 256-colour palette */
   214 			do {
   215 				if ( !SDL_RWread(src, &ch, 1, 1)) {
   216 					error = "file truncated";
   217 					goto done;
   218 				}
   219 			} while ( ch != 12 );
   220 
   221 			for(i = 0; i < 256; i++) {
   222 				SDL_RWread(src, &colors[i].r, 1, 1);
   223 				SDL_RWread(src, &colors[i].g, 1, 1);
   224 				SDL_RWread(src, &colors[i].b, 1, 1);
   225 			}
   226 		} else {
   227 			for(i = 0; i < nc; i++) {
   228 				colors[i].r = pcxh.Colormap[i * 3];
   229 				colors[i].g = pcxh.Colormap[i * 3 + 1];
   230 				colors[i].b = pcxh.Colormap[i * 3 + 2];
   231 			}
   232 		}
   233 	}
   234 
   235 done:
   236 	free(buf);
   237 	if ( error ) {
   238 		SDL_FreeSurface(surface);
   239 		IMG_SetError(error);
   240 		surface = NULL;
   241 	}
   242 	return(surface);
   243 }
   244 
   245 #else
   246 
   247 /* See if an image is contained in a data source */
   248 int IMG_isPCX(SDL_RWops *src)
   249 {
   250 	return(0);
   251 }
   252 
   253 /* Load a PCX type image from an SDL datasource */
   254 SDL_Surface *IMG_LoadPCX_RW(SDL_RWops *src)
   255 {
   256 	return(NULL);
   257 }
   258 
   259 #endif /* LOAD_PCX */