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