IMG_pcx.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 08 Dec 2008 00:27:32 +0000
changeset 186 5fbbeabf647f
parent 154 201cc5c1b373
child 216 7714c2bb6302
permissions -rw-r--r--
Updated copyright date
     1 /*
     2     SDL_image:  An example image loading library for use with SDL
     3     Copyright (C) 1997-2009 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 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     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 
    23 /*
    24  * PCX file reader:
    25  * Supports:
    26  *  1..4 bits/pixel in multiplanar format (1 bit/plane/pixel)
    27  *  8 bits/pixel in single-planar format (8 bits/plane/pixel)
    28  *  24 bits/pixel in 3-plane format (8 bits/plane/pixel)
    29  *
    30  * (The <8bpp formats are expanded to 8bpp surfaces)
    31  *
    32  * Doesn't support:
    33  *  single-planar packed-pixel formats other than 8bpp
    34  *  4-plane 32bpp format with a fourth "intensity" plane
    35  */
    36 #include <stdio.h>
    37 #include <stdlib.h>
    38 
    39 #include "SDL_endian.h"
    40 
    41 #include "SDL_image.h"
    42 
    43 #ifdef LOAD_PCX
    44 
    45 struct PCXheader {
    46 	Uint8 Manufacturer;
    47 	Uint8 Version;
    48 	Uint8 Encoding;
    49 	Uint8 BitsPerPixel;
    50 	Sint16 Xmin, Ymin, Xmax, Ymax;
    51 	Sint16 HDpi, VDpi;
    52 	Uint8 Colormap[48];
    53 	Uint8 Reserved;
    54 	Uint8 NPlanes;
    55 	Sint16 BytesPerLine;
    56 	Sint16 PaletteInfo;
    57 	Sint16 HscreenSize;
    58 	Sint16 VscreenSize;
    59 	Uint8 Filler[54];
    60 };
    61 
    62 /* See if an image is contained in a data source */
    63 int IMG_isPCX(SDL_RWops *src)
    64 {
    65 	int start;
    66 	int is_PCX;
    67 	const int ZSoft_Manufacturer = 10;
    68 	const int PC_Paintbrush_Version = 5;
    69 	const int PCX_RunLength_Encoding = 1;
    70 	struct PCXheader pcxh;
    71 
    72 	if ( !src )
    73 		return 0;
    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 	int start;
    91 	struct PCXheader pcxh;
    92 	Uint32 Rmask;
    93 	Uint32 Gmask;
    94 	Uint32 Bmask;
    95 	Uint32 Amask;
    96 	SDL_Surface *surface = NULL;
    97 	int width, height;
    98 	int y, bpl;
    99 	Uint8 *row, *buf = NULL;
   100 	char *error = NULL;
   101 	int bits, src_bits;
   102 
   103 	if ( !src ) {
   104 		/* The error message has been set in SDL_RWFromFile */
   105 		return NULL;
   106 	}
   107 	start = SDL_RWtell(src);
   108 
   109 	if ( ! SDL_RWread(src, &pcxh, sizeof(pcxh), 1) ) {
   110 		error = "file truncated";
   111 		goto done;
   112 	}
   113 	pcxh.Xmin = SDL_SwapLE16(pcxh.Xmin);
   114 	pcxh.Ymin = SDL_SwapLE16(pcxh.Ymin);
   115 	pcxh.Xmax = SDL_SwapLE16(pcxh.Xmax);
   116 	pcxh.Ymax = SDL_SwapLE16(pcxh.Ymax);
   117 	pcxh.BytesPerLine = SDL_SwapLE16(pcxh.BytesPerLine);
   118 
   119 	/* Create the surface of the appropriate type */
   120 	width = (pcxh.Xmax - pcxh.Xmin) + 1;
   121 	height = (pcxh.Ymax - pcxh.Ymin) + 1;
   122 	Rmask = Gmask = Bmask = Amask = 0;
   123 	src_bits = pcxh.BitsPerPixel * pcxh.NPlanes;
   124 	if((pcxh.BitsPerPixel == 1 && pcxh.NPlanes >= 1 && pcxh.NPlanes <= 4)
   125 	   || (pcxh.BitsPerPixel == 8 && pcxh.NPlanes == 1)) {
   126 		bits = 8;
   127 	} else if(pcxh.BitsPerPixel == 8 && pcxh.NPlanes == 3) {
   128 		bits = 24;
   129 		if ( SDL_BYTEORDER == SDL_LIL_ENDIAN ) {
   130 			Rmask = 0x000000FF;
   131 			Gmask = 0x0000FF00;
   132 			Bmask = 0x00FF0000;
   133 		} else {
   134 			Rmask = 0xFF0000;
   135 			Gmask = 0x00FF00;
   136 			Bmask = 0x0000FF;
   137 		}
   138 	} else {
   139 		error = "unsupported PCX format";
   140 		goto done;
   141 	}
   142 	surface = SDL_AllocSurface(SDL_SWSURFACE, width, height,
   143 				   bits, Rmask, Gmask, Bmask, Amask);
   144 	if ( surface == NULL )
   145 		goto done;
   146 
   147 	bpl = pcxh.NPlanes * pcxh.BytesPerLine;
   148 	buf = malloc(bpl);
   149 	row = surface->pixels;
   150 	for ( y=0; y<surface->h; ++y ) {
   151 		/* decode a scan line to a temporary buffer first */
   152 		int i, count = 0;
   153 		Uint8 ch;
   154 		Uint8 *dst = (src_bits == 8) ? row : buf;
   155 		for(i = 0; i < bpl; i++) {
   156 			if(!count) {
   157 				if(!SDL_RWread(src, &ch, 1, 1)) {
   158 					error = "file truncated";
   159 					goto done;
   160 				}
   161 				if( (ch & 0xc0) == 0xc0) {
   162 					count = ch & 0x3f;
   163 					if(!SDL_RWread(src, &ch, 1, 1)) {
   164 						error = "file truncated";
   165 						goto done;
   166 					}
   167 				} else
   168 					count = 1;
   169 			}
   170 			dst[i] = ch;
   171 			count--;
   172 		}
   173 
   174 		if(src_bits <= 4) {
   175 			/* expand planes to 1 byte/pixel */
   176 			Uint8 *src = buf;
   177 			int plane;
   178 			for(plane = 0; plane < pcxh.NPlanes; plane++) {
   179 				int i, j, x = 0;
   180 				for(i = 0; i < pcxh.BytesPerLine; i++) {
   181 					Uint8 byte = *src++;
   182 					for(j = 7; j >= 0; j--) {
   183 						unsigned bit = (byte >> j) & 1;
   184 						/* skip padding bits */
   185 						if (i * 8 + j >= width)
   186 							continue;
   187 						row[x++] |= bit << plane;
   188 					}
   189 				}
   190 			}
   191  		} else if(src_bits == 24) {
   192 			/* de-interlace planes */
   193 			Uint8 *src = buf;
   194 			int plane;
   195 			for(plane = 0; plane < pcxh.NPlanes; plane++) {
   196 				int x;
   197 				dst = row + plane;
   198 				for(x = 0; x < width; x++) {
   199 					*dst = *src++;
   200 					dst += pcxh.NPlanes;
   201 				}
   202 			}
   203 		}
   204 
   205 		row += surface->pitch;
   206 	}
   207 
   208 	if(bits == 8) {
   209 		SDL_Color *colors = surface->format->palette->colors;
   210 		int nc = 1 << src_bits;
   211 		int i;
   212 
   213 		surface->format->palette->ncolors = nc;
   214 		if(src_bits == 8) {
   215 			Uint8 ch;
   216 			/* look for a 256-colour palette */
   217 			do {
   218 				if ( !SDL_RWread(src, &ch, 1, 1)) {
   219 					error = "file truncated";
   220 					goto done;
   221 				}
   222 			} while ( ch != 12 );
   223 
   224 			for(i = 0; i < 256; i++) {
   225 				SDL_RWread(src, &colors[i].r, 1, 1);
   226 				SDL_RWread(src, &colors[i].g, 1, 1);
   227 				SDL_RWread(src, &colors[i].b, 1, 1);
   228 			}
   229 		} else {
   230 			for(i = 0; i < nc; i++) {
   231 				colors[i].r = pcxh.Colormap[i * 3];
   232 				colors[i].g = pcxh.Colormap[i * 3 + 1];
   233 				colors[i].b = pcxh.Colormap[i * 3 + 2];
   234 			}
   235 		}
   236 	}
   237 
   238 done:
   239 	free(buf);
   240 	if ( error ) {
   241 		SDL_RWseek(src, start, SEEK_SET);
   242 		if ( surface ) {
   243 			SDL_FreeSurface(surface);
   244 			surface = NULL;
   245 		}
   246 		IMG_SetError(error);
   247 	}
   248 	return(surface);
   249 }
   250 
   251 #else
   252 
   253 /* See if an image is contained in a data source */
   254 int IMG_isPCX(SDL_RWops *src)
   255 {
   256 	return(0);
   257 }
   258 
   259 /* Load a PCX type image from an SDL datasource */
   260 SDL_Surface *IMG_LoadPCX_RW(SDL_RWops *src)
   261 {
   262 	return(NULL);
   263 }
   264 
   265 #endif /* LOAD_PCX */