1 /* $Id: pcx.c,v 1.12 2005-07-30 01:51:42 chris Exp $ */
3 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
4 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
5 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
6 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
7 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
8 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
9 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
10 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
11 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
12 COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
16 * Routines to read/write pcx images.
40 int pcx_encode_byte(ubyte byt, ubyte cnt, PHYSFS_file *fid);
41 int pcx_encode_line(ubyte *inBuff, int inLen, PHYSFS_file *fp);
43 /* PCX Header data type */
55 ubyte ColorMap[16][3];
62 #define PCXHEADER_SIZE 128
65 #define PCXHeader_read_n(ph, n, fp) cfread(ph, sizeof(PCXHeader), n, fp)
68 * reads n PCXHeader structs from a CFILE
70 int PCXHeader_read_n(PCXHeader *ph, int n, CFILE *fp)
74 for (i = 0; i < n; i++) {
75 ph->Manufacturer = cfile_read_byte(fp);
76 ph->Version = cfile_read_byte(fp);
77 ph->Encoding = cfile_read_byte(fp);
78 ph->BitsPerPixel = cfile_read_byte(fp);
79 ph->Xmin = cfile_read_short(fp);
80 ph->Ymin = cfile_read_short(fp);
81 ph->Xmax = cfile_read_short(fp);
82 ph->Ymax = cfile_read_short(fp);
83 ph->Hdpi = cfile_read_short(fp);
84 ph->Vdpi = cfile_read_short(fp);
85 cfread(&ph->ColorMap, 16*3, 1, fp);
86 ph->Reserved = cfile_read_byte(fp);
87 ph->Nplanes = cfile_read_byte(fp);
88 ph->BytesPerLine = cfile_read_short(fp);
89 cfread(&ph->filler, 60, 1, fp);
95 int pcx_get_dimensions( char *filename, int *width, int *height)
100 PCXfile = cfopen(filename, "rb");
101 if (!PCXfile) return PCX_ERROR_OPENING;
103 if (PCXHeader_read_n(&header, 1, PCXfile) != 1) {
105 return PCX_ERROR_NO_HEADER;
109 *width = header.Xmax - header.Xmin+1;
110 *height = header.Ymax - header.Ymin+1;
112 return PCX_ERROR_NONE;
116 int pcx_read_bitmap_palette( char *filename, ubyte *palette)
123 PCXfile = cfopen( filename , "rb" );
125 return PCX_ERROR_OPENING;
127 // read 128 char PCX header
128 if (PCXHeader_read_n( &header, 1, PCXfile )!=1) {
130 return PCX_ERROR_NO_HEADER;
132 // Is it a 256 color PCX file?
133 if ((header.Manufacturer != 10)||(header.Encoding != 1)||(header.Nplanes != 1)||(header.BitsPerPixel != 8)||(header.Version != 5)) {
135 return PCX_ERROR_WRONG_VERSION;
138 // Read the extended palette at the end of PCX file
139 // Read in a character which should be 12 to be extended palette file
141 if (palette != NULL) {
142 cfseek( PCXfile, -768, SEEK_END );
143 cfread( palette, 3, 256, PCXfile );
144 cfseek( PCXfile, PCXHEADER_SIZE, SEEK_SET );
145 for (i=0; i<768; i++ )
148 for (i = 0; i < 3; i++) {
150 palette[i] = palette[765+i];
151 palette[765+i] = data;
158 int pcx_read_bitmap( char * filename, grs_bitmap * bmp,int bitmap_type ,ubyte * palette )
162 int i, row, col, count, xsize, ysize;
163 ubyte data, *pixdata;
165 PCXfile = cfopen( filename , "rb" );
167 return PCX_ERROR_OPENING;
169 // read 128 char PCX header
170 if (PCXHeader_read_n( &header, 1, PCXfile )!=1) {
172 return PCX_ERROR_NO_HEADER;
175 // Is it a 256 color PCX file?
176 if ((header.Manufacturer != 10)||(header.Encoding != 1)||(header.Nplanes != 1)||(header.BitsPerPixel != 8)||(header.Version != 5)) {
178 return PCX_ERROR_WRONG_VERSION;
181 // Find the size of the image
182 xsize = header.Xmax - header.Xmin + 1;
183 ysize = header.Ymax - header.Ymin + 1;
185 if ( bitmap_type == BM_LINEAR ) {
186 if ( bmp->bm_data == NULL ) {
187 gr_init_bitmap_alloc (bmp, bitmap_type, 0, 0, xsize, ysize, xsize);
191 if ( bmp->bm_type == BM_LINEAR ) {
192 for (row=0; row< ysize ; row++) {
193 pixdata = &bmp->bm_data[bmp->bm_rowsize*row];
194 for (col=0; col< xsize ; ) {
195 if (cfread( &data, 1, 1, PCXfile )!=1 ) {
197 return PCX_ERROR_READING;
199 if ((data & 0xC0) == 0xC0) {
201 if (cfread( &data, 1, 1, PCXfile )!=1 ) {
203 return PCX_ERROR_READING;
208 else if (data == 255)
211 memset( pixdata, data, count );
218 else if (data == 255)
227 for (row=0; row< ysize ; row++) {
228 for (col=0; col< xsize ; ) {
229 if (cfread( &data, 1, 1, PCXfile )!=1 ) {
231 return PCX_ERROR_READING;
233 if ((data & 0xC0) == 0xC0) {
235 if (cfread( &data, 1, 1, PCXfile )!=1 ) {
237 return PCX_ERROR_READING;
239 for (i=0;i<count;i++)
240 gr_bm_pixel( bmp, col+i, row, data );
243 gr_bm_pixel( bmp, col, row, data );
250 // Read the extended palette at the end of PCX file
251 if ( palette != NULL ) {
252 // Read in a character which should be 12 to be extended palette file
253 if (cfread( &data, 1, 1, PCXfile )==1) {
255 if (cfread(palette,768, 1, PCXfile)!=1) {
257 return PCX_ERROR_READING;
259 for (i=0; i<768; i++ )
262 for (i = 0; i < 3; i++) {
264 palette[i] = palette[765+i];
265 palette[765+i] = data;
271 return PCX_ERROR_NO_PALETTE;
275 return PCX_ERROR_NONE;
278 int pcx_write_bitmap( char * filename, grs_bitmap * bmp, ubyte * palette )
284 PHYSFS_file *PCXfile;
286 memset( &header, 0, PCXHEADER_SIZE );
288 header.Manufacturer = 10;
291 header.BitsPerPixel = 8;
293 header.Xmax = bmp->bm_w-1;
294 header.Ymax = bmp->bm_h-1;
295 header.BytesPerLine = bmp->bm_w;
297 PCXfile = PHYSFSX_openWriteBuffered(filename);
299 return PCX_ERROR_OPENING;
301 if (PHYSFS_write(PCXfile, &header, PCXHEADER_SIZE, 1) != 1)
303 PHYSFS_close(PCXfile);
304 return PCX_ERROR_WRITING;
307 for (i=0; i<bmp->bm_h; i++ ) {
308 if (!pcx_encode_line( &bmp->bm_data[bmp->bm_rowsize*i], bmp->bm_w, PCXfile )) {
309 PHYSFS_close(PCXfile);
310 return PCX_ERROR_WRITING;
314 // Mark an extended palette
316 if (PHYSFS_write(PCXfile, &data, 1, 1) != 1)
318 PHYSFS_close(PCXfile);
319 return PCX_ERROR_WRITING;
322 // Write the extended palette
323 for (i=0; i<768; i++ )
326 retval = PHYSFS_write(PCXfile, palette, 768, 1);
328 for (i=0; i<768; i++ )
332 PHYSFS_close(PCXfile);
333 return PCX_ERROR_WRITING;
336 PHYSFS_close(PCXfile);
337 return PCX_ERROR_NONE;
341 // returns number of bytes written into outBuff, 0 if failed
342 int pcx_encode_line(ubyte *inBuff, int inLen, PHYSFS_file *fp)
347 register ubyte runCount; // max single runlength is 63
352 for (srcIndex = 1; srcIndex < inLen; srcIndex++) {
355 runCount++; // it encodes
356 if (runCount == 63) {
357 if (!(i=pcx_encode_byte(last, runCount, fp)))
362 } else { // this != last
364 if (!(i=pcx_encode_byte(last, runCount, fp)))
373 if (runCount) { // finish up
374 if (!(i=pcx_encode_byte(last, runCount, fp)))
381 // subroutine for writing an encoded byte pair
382 // returns count of bytes written, 0 if error
383 int pcx_encode_byte(ubyte byt, ubyte cnt, PHYSFS_file *fid)
386 if ( (cnt==1) && (0xc0 != (0xc0 & byt)) ) {
387 if(EOF == PHYSFSX_putc(fid, (int)byt))
388 return 0; // disk write error (probably full)
391 if(EOF == PHYSFSX_putc(fid, (int)0xC0 | cnt))
392 return 0; // disk write error
393 if(EOF == PHYSFSX_putc(fid, (int)byt))
394 return 0; // disk write error
401 //text for error messges
402 char pcx_error_messages[] = {
404 "Error opening file.\0"
405 "Couldn't read PCX header.\0"
406 "Unsupported PCX version.\0"
407 "Error reading data.\0"
408 "Couldn't find palette information.\0"
409 "Error writing data.\0"
413 //function to return pointer to error message
414 char *pcx_errormsg(int error_number)
416 char *p = pcx_error_messages;
418 while (error_number--) {
430 // fullscreen loading, 10/14/99 Jan Bobrowski
432 int pcx_read_fullscr(char * filename, ubyte * palette)
436 gr_init_bitmap_data(&bm);
437 pcx_error = pcx_read_bitmap(filename, &bm, BM_LINEAR, palette);
438 if (pcx_error == PCX_ERROR_NONE) {
440 gr_palette_load(palette);
444 gr_free_bitmap_data(&bm);