IMG_pcx.c
author Sam Lantinga <slouken@lokigames.com>
Wed, 29 Nov 2000 11:55:32 +0000
changeset 7 e1b6443ffb6b
parent 4 b1bb33e907f8
child 20 463951886af1
permissions -rw-r--r--
1.1.0:
Sam Lantinga - Wed Nov 29 00:46:27 PST 2000
* Added XPM file format support
Supports color, greyscale, and mono XPMs with and without transparency
Mattias Engdeg�rd - Thu, 2 Nov 2000 23:23:17 +0100 (MET)
* Fixed array overrun when loading an unsupported format
* Minor compilation fixes for various platforms
     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 		} else {
   116 			Rmask = 0xFF0000;
   117 			Gmask = 0x00FF00;
   118 			Bmask = 0x0000FF;
   119 		}
   120 	}
   121 	surface = SDL_AllocSurface(SDL_SWSURFACE, width, height,
   122 			pcxh.BitsPerPixel*pcxh.NPlanes,Rmask,Gmask,Bmask,Amask);
   123 	if ( surface == NULL ) {
   124 		IMG_SetError("Out of memory");
   125 		goto done;
   126 	}
   127 
   128 	/* Decode the image to the surface */
   129 	for ( y=0; y<surface->h; ++y ) {
   130 		for ( i=0; i<pcxh.NPlanes; ++i ) {
   131 			row = (Uint8 *)surface->pixels + y*surface->pitch;
   132 			index = i;
   133 			for ( x=0; x<pcxh.BytesPerLine; ) {
   134 				if ( ! SDL_RWread(src, &ch, 1, 1) ) {
   135 					read_error = 1;
   136 					goto done;
   137 				}
   138 				if ( (ch & 0xC0) == 0xC0 ) {
   139 					count = ch & 0x3F;
   140 					SDL_RWread(src, &ch, 1, 1);
   141 				} else {
   142 					count = 1;
   143 				}
   144 				while ( count-- ) {
   145 					row[index] = ch;
   146 					++x;
   147 					index += pcxh.NPlanes;
   148 				}
   149 			}
   150 		}
   151 	}
   152 
   153 	/* Look for the palette, if necessary */
   154 	switch (surface->format->BitsPerPixel) {
   155 	    case 1: {
   156 		SDL_Color *colors = surface->format->palette->colors;
   157 
   158 		colors[0].r = 0x00;
   159 		colors[0].g = 0x00;
   160 		colors[0].b = 0x00;
   161 		colors[1].r = 0xFF;
   162 		colors[1].g = 0xFF;
   163 		colors[1].b = 0xFF;
   164 	    }
   165 	    break;
   166 
   167 	    case 8: {
   168 		SDL_Color *colors = surface->format->palette->colors;
   169 
   170 		/* Look for the palette */
   171 		do {
   172 			if ( ! SDL_RWread(src, &ch, 1, 1) ) {
   173 				read_error = 1;
   174 				goto done;
   175 			}
   176 		} while ( ch != 12 );
   177 
   178 		/* Set the image surface palette */
   179 		for ( i=0; i<256; ++i ) {
   180 			SDL_RWread(src, &colors[i].r, 1, 1);
   181 			SDL_RWread(src, &colors[i].g, 1, 1);
   182 			SDL_RWread(src, &colors[i].b, 1, 1);
   183 		}
   184 	    }
   185 	    break;
   186 
   187 	    default: {
   188 		/* Don't do anything... */;
   189 	    }
   190 	    break;
   191 	}
   192 
   193 done:
   194 	if ( read_error ) {
   195 		SDL_FreeSurface(surface);
   196 		IMG_SetError("Error reading PCX data");
   197 		surface = NULL;
   198 	}
   199 	return(surface);
   200 }
   201 
   202 #else
   203 
   204 /* See if an image is contained in a data source */
   205 int IMG_isPCX(SDL_RWops *src)
   206 {
   207 	return(0);
   208 }
   209 
   210 /* Load a PCX type image from an SDL datasource */
   211 SDL_Surface *IMG_LoadPCX_RW(SDL_RWops *src)
   212 {
   213 	return(NULL);
   214 }
   215 
   216 #endif /* LOAD_PCX */