IMG_pcx.c
author Sam Lantinga <slouken@lokigames.com>
Thu, 10 Aug 2000 06:05:39 +0000
changeset 0 76be7dab668c
child 4 b1bb33e907f8
permissions -rw-r--r--
Initial revision
     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 /* This is a PCX image file loading framework */
    26 
    27 #include <stdio.h>
    28 
    29 #include "SDL_endian.h"
    30 
    31 #include "SDL_image.h"
    32 
    33 #ifdef LOAD_PCX
    34 
    35 struct PCXheader {
    36 	Uint8 Manufacturer;
    37 	Uint8 Version;
    38 	Uint8 Encoding;
    39 	Uint8 BitsPerPixel;
    40 	Sint16 Xmin, Ymin, Xmax, Ymax;
    41 	Sint16 HDpi, VDpi;
    42 	Uint8 Colormap[48];
    43 	Uint8 Reserved;
    44 	Uint8 NPlanes;
    45 	Sint16 BytesPerLine;
    46 	Sint16 PaletteInfo;
    47 	Sint16 HscreenSize;
    48 	Sint16 VscreenSize;
    49 	Uint8 Filler[54];
    50 };
    51 
    52 /* See if an image is contained in a data source */
    53 int IMG_isPCX(SDL_RWops *src)
    54 {
    55 	int is_PCX;
    56 	const int ZSoft_Manufacturer = 10;
    57 	const int PC_Paintbrush_Version = 5;
    58 	const int PCX_RunLength_Encoding = 1;
    59 	struct PCXheader pcxh;
    60 
    61 	is_PCX = 0;
    62 	if ( SDL_RWread(src, &pcxh, sizeof(pcxh), 1) == 1 ) {
    63 		if ( (pcxh.Manufacturer == ZSoft_Manufacturer) &&
    64 		     (pcxh.Version == PC_Paintbrush_Version) &&
    65 		     (pcxh.Encoding == PCX_RunLength_Encoding) ) {
    66 			is_PCX = 1;
    67 		}
    68 	}
    69 	return(is_PCX);
    70 }
    71 
    72 /* Load a PCX type image from an SDL datasource */
    73 SDL_Surface *IMG_LoadPCX_RW(SDL_RWops *src)
    74 {
    75 	struct PCXheader pcxh;
    76 	Uint32 Rmask;
    77 	Uint32 Gmask;
    78 	Uint32 Bmask;
    79 	Uint32 Amask;
    80 	SDL_Surface *surface;
    81 	int width, height;
    82 	int i, index, x, y;
    83 	int count;
    84 	Uint8 *row, ch;
    85 	int read_error;
    86 
    87 	/* Initialize the data we will clean up when we're done */
    88 	surface = NULL;
    89 	read_error = 0;
    90 
    91 	/* Check to make sure we have something to do */
    92 	if ( ! src ) {
    93 		goto done;
    94 	}
    95 
    96 	/* Read and convert the header */
    97 	if ( ! SDL_RWread(src, &pcxh, sizeof(pcxh), 1) ) {
    98 		goto done;
    99 	}
   100 	pcxh.Xmin = SDL_SwapLE16(pcxh.Xmin);
   101 	pcxh.Ymin = SDL_SwapLE16(pcxh.Ymin);
   102 	pcxh.Xmax = SDL_SwapLE16(pcxh.Xmax);
   103 	pcxh.Ymax = SDL_SwapLE16(pcxh.Ymax);
   104 	pcxh.BytesPerLine = SDL_SwapLE16(pcxh.BytesPerLine);
   105 
   106 	/* Create the surface of the appropriate type */
   107 	width = (pcxh.Xmax - pcxh.Xmin) + 1;
   108 	height = (pcxh.Ymax - pcxh.Ymin) + 1;
   109 	Rmask = Gmask = Bmask = Amask = 0 ; 
   110 	if ( pcxh.BitsPerPixel * pcxh.NPlanes > 16 ) {
   111 		if ( SDL_BYTEORDER == SDL_LIL_ENDIAN ) {
   112 			Rmask = 0x000000FF;
   113 			Gmask = 0x0000FF00;
   114 			Bmask = 0x00FF0000;
   115 			Amask = 0xFF000000;
   116 		} else {
   117 		        int s = (pcxh.NPlanes == 4) ? 0 : 8;
   118 			Rmask = 0xFF000000 >> s;
   119 			Gmask = 0x00FF0000 >> s;
   120 			Bmask = 0x0000FF00 >> s;
   121 			Amask = 0x000000FF >> s;
   122 		}
   123 	}
   124 	surface = SDL_AllocSurface(SDL_SWSURFACE, width, height,
   125 			pcxh.BitsPerPixel*pcxh.NPlanes,Rmask,Gmask,Bmask,Amask);
   126 	if ( surface == NULL ) {
   127 		IMG_SetError("Out of memory");
   128 		goto done;
   129 	}
   130 
   131 	/* Decode the image to the surface */
   132 	for ( y=0; y<surface->h; ++y ) {
   133 		for ( i=0; i<pcxh.NPlanes; ++i ) {
   134 			row = (Uint8 *)surface->pixels + y*surface->pitch;
   135 			index = i;
   136 			for ( x=0; x<pcxh.BytesPerLine; ) {
   137 				if ( ! SDL_RWread(src, &ch, 1, 1) ) {
   138 					read_error = 1;
   139 					goto done;
   140 				}
   141 				if ( (ch & 0xC0) == 0xC0 ) {
   142 					count = ch & 0x3F;
   143 					SDL_RWread(src, &ch, 1, 1);
   144 				} else {
   145 					count = 1;
   146 				}
   147 				while ( count-- ) {
   148 					row[index] = ch;
   149 					++x;
   150 					index += pcxh.NPlanes;
   151 				}
   152 			}
   153 		}
   154 	}
   155 
   156 	/* Look for the palette, if necessary */
   157 	switch (surface->format->BitsPerPixel) {
   158 	    case 1: {
   159 		SDL_Color *colors = surface->format->palette->colors;
   160 
   161 		colors[0].r = 0x00;
   162 		colors[0].g = 0x00;
   163 		colors[0].b = 0x00;
   164 		colors[1].r = 0xFF;
   165 		colors[1].g = 0xFF;
   166 		colors[1].b = 0xFF;
   167 	    }
   168 	    break;
   169 
   170 	    case 8: {
   171 		SDL_Color *colors = surface->format->palette->colors;
   172 
   173 		/* Look for the palette */
   174 		do {
   175 			if ( ! SDL_RWread(src, &ch, 1, 1) ) {
   176 				read_error = 1;
   177 				goto done;
   178 			}
   179 		} while ( ch != 12 );
   180 
   181 		/* Set the image surface palette */
   182 		for ( i=0; i<256; ++i ) {
   183 			SDL_RWread(src, &colors[i].r, 1, 1);
   184 			SDL_RWread(src, &colors[i].g, 1, 1);
   185 			SDL_RWread(src, &colors[i].b, 1, 1);
   186 		}
   187 	    }
   188 	    break;
   189 
   190 	    default: {
   191 		/* Don't do anything... */;
   192 	    }
   193 	    break;
   194 	}
   195 
   196 done:
   197 	if ( read_error ) {
   198 		SDL_FreeSurface(surface);
   199 		IMG_SetError("Error reading PCX data");
   200 		surface = NULL;
   201 	}
   202 	return(surface);
   203 }
   204 
   205 #else
   206 
   207 /* See if an image is contained in a data source */
   208 int IMG_isPCX(SDL_RWops *src)
   209 {
   210 	return(0);
   211 }
   212 
   213 /* Load a PCX type image from an SDL datasource */
   214 SDL_Surface *IMG_LoadPCX_RW(SDL_RWops *src)
   215 {
   216 	return(NULL);
   217 }
   218 
   219 #endif /* LOAD_PCX */