IMG_pcx.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 04 Feb 2006 23:39:03 +0000
changeset 121 1bf9c0c87374
parent 118 c5e736a47ad2
child 154 201cc5c1b373
permissions -rw-r--r--
Updated copyright information
     1 /*
     2     SDL_image:  An example image loading library for use with SDL
     3     Copyright (C) 1997-2006 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 	start = SDL_RWtell(src);
    73 	is_PCX = 0;
    74 	if ( SDL_RWread(src, &pcxh, sizeof(pcxh), 1) == 1 ) {
    75 		if ( (pcxh.Manufacturer == ZSoft_Manufacturer) &&
    76 		     (pcxh.Version == PC_Paintbrush_Version) &&
    77 		     (pcxh.Encoding == PCX_RunLength_Encoding) ) {
    78 			is_PCX = 1;
    79 		}
    80 	}
    81 	SDL_RWseek(src, start, SEEK_SET);
    82 	return(is_PCX);
    83 }
    84 
    85 /* Load a PCX type image from an SDL datasource */
    86 SDL_Surface *IMG_LoadPCX_RW(SDL_RWops *src)
    87 {
    88 	int start;
    89 	struct PCXheader pcxh;
    90 	Uint32 Rmask;
    91 	Uint32 Gmask;
    92 	Uint32 Bmask;
    93 	Uint32 Amask;
    94 	SDL_Surface *surface = NULL;
    95 	int width, height;
    96 	int y, bpl;
    97 	Uint8 *row, *buf = NULL;
    98 	char *error = NULL;
    99 	int bits, src_bits;
   100 
   101 	if ( !src ) {
   102 		/* The error message has been set in SDL_RWFromFile */
   103 		return NULL;
   104 	}
   105 	start = SDL_RWtell(src);
   106 
   107 	if ( ! SDL_RWread(src, &pcxh, sizeof(pcxh), 1) ) {
   108 		error = "file truncated";
   109 		goto done;
   110 	}
   111 	pcxh.Xmin = SDL_SwapLE16(pcxh.Xmin);
   112 	pcxh.Ymin = SDL_SwapLE16(pcxh.Ymin);
   113 	pcxh.Xmax = SDL_SwapLE16(pcxh.Xmax);
   114 	pcxh.Ymax = SDL_SwapLE16(pcxh.Ymax);
   115 	pcxh.BytesPerLine = SDL_SwapLE16(pcxh.BytesPerLine);
   116 
   117 	/* Create the surface of the appropriate type */
   118 	width = (pcxh.Xmax - pcxh.Xmin) + 1;
   119 	height = (pcxh.Ymax - pcxh.Ymin) + 1;
   120 	Rmask = Gmask = Bmask = Amask = 0;
   121 	src_bits = pcxh.BitsPerPixel * pcxh.NPlanes;
   122 	if((pcxh.BitsPerPixel == 1 && pcxh.NPlanes >= 1 && pcxh.NPlanes <= 4)
   123 	   || (pcxh.BitsPerPixel == 8 && pcxh.NPlanes == 1)) {
   124 		bits = 8;
   125 	} else if(pcxh.BitsPerPixel == 8 && pcxh.NPlanes == 3) {
   126 		bits = 24;
   127 		if ( SDL_BYTEORDER == SDL_LIL_ENDIAN ) {
   128 			Rmask = 0x000000FF;
   129 			Gmask = 0x0000FF00;
   130 			Bmask = 0x00FF0000;
   131 		} else {
   132 			Rmask = 0xFF0000;
   133 			Gmask = 0x00FF00;
   134 			Bmask = 0x0000FF;
   135 		}
   136 	} else {
   137 		error = "unsupported PCX format";
   138 		goto done;
   139 	}
   140 	surface = SDL_AllocSurface(SDL_SWSURFACE, width, height,
   141 				   bits, Rmask, Gmask, Bmask, Amask);
   142 	if ( surface == NULL )
   143 		goto done;
   144 
   145 	bpl = pcxh.NPlanes * pcxh.BytesPerLine;
   146 	buf = malloc(bpl);
   147 	row = surface->pixels;
   148 	for ( y=0; y<surface->h; ++y ) {
   149 		/* decode a scan line to a temporary buffer first */
   150 		int i, count = 0;
   151 		Uint8 ch;
   152 		Uint8 *dst = (src_bits == 8) ? row : buf;
   153 		for(i = 0; i < bpl; i++) {
   154 			if(!count) {
   155 				if(!SDL_RWread(src, &ch, 1, 1)) {
   156 					error = "file truncated";
   157 					goto done;
   158 				}
   159 				if( (ch & 0xc0) == 0xc0) {
   160 					count = ch & 0x3f;
   161 					if(!SDL_RWread(src, &ch, 1, 1)) {
   162 						error = "file truncated";
   163 						goto done;
   164 					}
   165 				} else
   166 					count = 1;
   167 			}
   168 			dst[i] = ch;
   169 			count--;
   170 		}
   171 
   172 		if(src_bits <= 4) {
   173 			/* expand planes to 1 byte/pixel */
   174 			Uint8 *src = buf;
   175 			int plane;
   176 			for(plane = 0; plane < pcxh.NPlanes; plane++) {
   177 				int i, j, x = 0;
   178 				for(i = 0; i < pcxh.BytesPerLine; i++) {
   179 					Uint8 byte = *src++;
   180 					for(j = 7; j >= 0; j--) {
   181 						unsigned bit = (byte >> j) & 1;
   182 						/* skip padding bits */
   183 						if (i * 8 + j >= width)
   184 							continue;
   185 						row[x++] |= bit << plane;
   186 					}
   187 				}
   188 			}
   189  		} else if(src_bits == 24) {
   190 			/* de-interlace planes */
   191 			Uint8 *src = buf;
   192 			int plane;
   193 			for(plane = 0; plane < pcxh.NPlanes; plane++) {
   194 				int x;
   195 				dst = row + plane;
   196 				for(x = 0; x < width; x++) {
   197 					*dst = *src++;
   198 					dst += pcxh.NPlanes;
   199 				}
   200 			}
   201 		}
   202 
   203 		row += surface->pitch;
   204 	}
   205 
   206 	if(bits == 8) {
   207 		SDL_Color *colors = surface->format->palette->colors;
   208 		int nc = 1 << src_bits;
   209 		int i;
   210 
   211 		surface->format->palette->ncolors = nc;
   212 		if(src_bits == 8) {
   213 			Uint8 ch;
   214 			/* look for a 256-colour palette */
   215 			do {
   216 				if ( !SDL_RWread(src, &ch, 1, 1)) {
   217 					error = "file truncated";
   218 					goto done;
   219 				}
   220 			} while ( ch != 12 );
   221 
   222 			for(i = 0; i < 256; i++) {
   223 				SDL_RWread(src, &colors[i].r, 1, 1);
   224 				SDL_RWread(src, &colors[i].g, 1, 1);
   225 				SDL_RWread(src, &colors[i].b, 1, 1);
   226 			}
   227 		} else {
   228 			for(i = 0; i < nc; i++) {
   229 				colors[i].r = pcxh.Colormap[i * 3];
   230 				colors[i].g = pcxh.Colormap[i * 3 + 1];
   231 				colors[i].b = pcxh.Colormap[i * 3 + 2];
   232 			}
   233 		}
   234 	}
   235 
   236 done:
   237 	free(buf);
   238 	if ( error ) {
   239 		SDL_RWseek(src, start, SEEK_SET);
   240 		if ( surface ) {
   241 			SDL_FreeSurface(surface);
   242 			surface = NULL;
   243 		}
   244 		IMG_SetError(error);
   245 	}
   246 	return(surface);
   247 }
   248 
   249 #else
   250 
   251 /* See if an image is contained in a data source */
   252 int IMG_isPCX(SDL_RWops *src)
   253 {
   254 	return(0);
   255 }
   256 
   257 /* Load a PCX type image from an SDL datasource */
   258 SDL_Surface *IMG_LoadPCX_RW(SDL_RWops *src)
   259 {
   260 	return(NULL);
   261 }
   262 
   263 #endif /* LOAD_PCX */