1 /* $Id: pcx.c,v 1.11 2005-02-25 05:20:36 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.
43 int pcx_encode_byte(ubyte byt, ubyte cnt, PHYSFS_file *fid);
44 int pcx_encode_line(ubyte *inBuff, int inLen, PHYSFS_file *fp);
46 /* PCX Header data type */
58 ubyte ColorMap[16][3];
65 #define PCXHEADER_SIZE 128
68 #define PCXHeader_read_n(ph, n, fp) cfread(ph, sizeof(PCXHeader), n, fp)
71 * reads n PCXHeader structs from a CFILE
73 int PCXHeader_read_n(PCXHeader *ph, int n, CFILE *fp)
77 for (i = 0; i < n; i++) {
78 ph->Manufacturer = cfile_read_byte(fp);
79 ph->Version = cfile_read_byte(fp);
80 ph->Encoding = cfile_read_byte(fp);
81 ph->BitsPerPixel = cfile_read_byte(fp);
82 ph->Xmin = cfile_read_short(fp);
83 ph->Ymin = cfile_read_short(fp);
84 ph->Xmax = cfile_read_short(fp);
85 ph->Ymax = cfile_read_short(fp);
86 ph->Hdpi = cfile_read_short(fp);
87 ph->Vdpi = cfile_read_short(fp);
88 cfread(&ph->ColorMap, 16*3, 1, fp);
89 ph->Reserved = cfile_read_byte(fp);
90 ph->Nplanes = cfile_read_byte(fp);
91 ph->BytesPerLine = cfile_read_short(fp);
92 cfread(&ph->filler, 60, 1, fp);
98 int pcx_get_dimensions( char *filename, int *width, int *height)
103 PCXfile = cfopen(filename, "rb");
104 if (!PCXfile) return PCX_ERROR_OPENING;
106 if (PCXHeader_read_n(&header, 1, PCXfile) != 1) {
108 return PCX_ERROR_NO_HEADER;
112 *width = header.Xmax - header.Xmin+1;
113 *height = header.Ymax - header.Ymin+1;
115 return PCX_ERROR_NONE;
119 int pcx_read_bitmap_palette( char *filename, ubyte *palette)
126 PCXfile = cfopen( filename , "rb" );
128 return PCX_ERROR_OPENING;
130 // read 128 char PCX header
131 if (PCXHeader_read_n( &header, 1, PCXfile )!=1) {
133 return PCX_ERROR_NO_HEADER;
135 // Is it a 256 color PCX file?
136 if ((header.Manufacturer != 10)||(header.Encoding != 1)||(header.Nplanes != 1)||(header.BitsPerPixel != 8)||(header.Version != 5)) {
138 return PCX_ERROR_WRONG_VERSION;
141 // Read the extended palette at the end of PCX file
142 // Read in a character which should be 12 to be extended palette file
144 if (palette != NULL) {
145 cfseek( PCXfile, -768, SEEK_END );
146 cfread( palette, 3, 256, PCXfile );
147 cfseek( PCXfile, PCXHEADER_SIZE, SEEK_SET );
148 for (i=0; i<768; i++ )
151 for (i = 0; i < 3; i++) {
153 palette[i] = palette[765+i];
154 palette[765+i] = data;
161 int pcx_read_bitmap( char * filename, grs_bitmap * bmp,int bitmap_type ,ubyte * palette )
165 int i, row, col, count, xsize, ysize;
166 ubyte data, *pixdata;
167 #if defined(POLY_ACC)
168 unsigned char local_pal[768];
173 PCXfile = cfopen( filename , "rb" );
175 return PCX_ERROR_OPENING;
177 // read 128 char PCX header
178 if (PCXHeader_read_n( &header, 1, PCXfile )!=1) {
180 return PCX_ERROR_NO_HEADER;
183 // Is it a 256 color PCX file?
184 if ((header.Manufacturer != 10)||(header.Encoding != 1)||(header.Nplanes != 1)||(header.BitsPerPixel != 8)||(header.Version != 5)) {
186 return PCX_ERROR_WRONG_VERSION;
189 // Find the size of the image
190 xsize = header.Xmax - header.Xmin + 1;
191 ysize = header.Ymax - header.Ymin + 1;
193 #if defined(POLY_ACC)
194 // Read the extended palette at the end of PCX file
195 if(bitmap_type == BM_LINEAR15) // need palette for conversion from 8bit pcx to 15bit.
197 cfseek( PCXfile, -768, SEEK_END );
198 cfread( local_pal, 3, 256, PCXfile );
199 cfseek( PCXfile, PCXHEADER_SIZE, SEEK_SET );
200 for (i=0; i<768; i++ )
203 pa_update_clut(local_pal, 0, 256, 0);
207 if ( bitmap_type == BM_LINEAR ) {
208 if ( bmp->bm_data == NULL ) {
209 gr_init_bitmap_alloc (bmp, bitmap_type, 0, 0, xsize, ysize, xsize);
213 if ( bmp->bm_type == BM_LINEAR ) {
214 for (row=0; row< ysize ; row++) {
215 pixdata = &bmp->bm_data[bmp->bm_rowsize*row];
216 for (col=0; col< xsize ; ) {
217 if (cfread( &data, 1, 1, PCXfile )!=1 ) {
219 return PCX_ERROR_READING;
221 if ((data & 0xC0) == 0xC0) {
223 if (cfread( &data, 1, 1, PCXfile )!=1 ) {
225 return PCX_ERROR_READING;
230 else if (data == 255)
233 memset( pixdata, data, count );
240 else if (data == 255)
248 #if defined(POLY_ACC)
249 } else if( bmp->bm_type == BM_LINEAR15 ) {
250 ushort *pixdata2, pix15;
251 PA_DFX (pa_set_backbuffer_current());
252 PA_DFX (pa_set_write_mode(0));
253 for (row=0; row< ysize ; row++) {
254 pixdata2 = (ushort *)&bmp->bm_data[bmp->bm_rowsize*row];
255 for (col=0; col< xsize ; ) {
256 if (cfread( &data, 1, 1, PCXfile )!=1 ) {
258 return PCX_ERROR_READING;
260 if ((data & 0xC0) == 0xC0) {
262 if (cfread( &data, 1, 1, PCXfile )!=1 ) {
264 return PCX_ERROR_READING;
266 pix15 = pa_clut[data];
267 for(i = 0; i != count; ++i) pixdata2[i] = pix15;
271 *pixdata2++ = pa_clut[data];
277 PA_DFX (pa_swap_buffer());
278 PA_DFX (pa_set_frontbuffer_current());
282 for (row=0; row< ysize ; row++) {
283 for (col=0; col< xsize ; ) {
284 if (cfread( &data, 1, 1, PCXfile )!=1 ) {
286 return PCX_ERROR_READING;
288 if ((data & 0xC0) == 0xC0) {
290 if (cfread( &data, 1, 1, PCXfile )!=1 ) {
292 return PCX_ERROR_READING;
294 for (i=0;i<count;i++)
295 gr_bm_pixel( bmp, col+i, row, data );
298 gr_bm_pixel( bmp, col, row, data );
305 // Read the extended palette at the end of PCX file
306 if ( palette != NULL ) {
307 // Read in a character which should be 12 to be extended palette file
308 if (cfread( &data, 1, 1, PCXfile )==1) {
310 if (cfread(palette,768, 1, PCXfile)!=1) {
312 return PCX_ERROR_READING;
314 for (i=0; i<768; i++ )
317 for (i = 0; i < 3; i++) {
319 palette[i] = palette[765+i];
320 palette[765+i] = data;
326 return PCX_ERROR_NO_PALETTE;
330 return PCX_ERROR_NONE;
333 int pcx_write_bitmap( char * filename, grs_bitmap * bmp, ubyte * palette )
339 PHYSFS_file *PCXfile;
341 memset( &header, 0, PCXHEADER_SIZE );
343 header.Manufacturer = 10;
346 header.BitsPerPixel = 8;
348 header.Xmax = bmp->bm_w-1;
349 header.Ymax = bmp->bm_h-1;
350 header.BytesPerLine = bmp->bm_w;
352 PCXfile = PHYSFSX_openWriteBuffered(filename);
354 return PCX_ERROR_OPENING;
356 if (PHYSFS_write(PCXfile, &header, PCXHEADER_SIZE, 1) != 1)
358 PHYSFS_close(PCXfile);
359 return PCX_ERROR_WRITING;
362 for (i=0; i<bmp->bm_h; i++ ) {
363 if (!pcx_encode_line( &bmp->bm_data[bmp->bm_rowsize*i], bmp->bm_w, PCXfile )) {
364 PHYSFS_close(PCXfile);
365 return PCX_ERROR_WRITING;
369 // Mark an extended palette
371 if (PHYSFS_write(PCXfile, &data, 1, 1) != 1)
373 PHYSFS_close(PCXfile);
374 return PCX_ERROR_WRITING;
377 // Write the extended palette
378 for (i=0; i<768; i++ )
381 retval = PHYSFS_write(PCXfile, palette, 768, 1);
383 for (i=0; i<768; i++ )
387 PHYSFS_close(PCXfile);
388 return PCX_ERROR_WRITING;
391 PHYSFS_close(PCXfile);
392 return PCX_ERROR_NONE;
396 // returns number of bytes written into outBuff, 0 if failed
397 int pcx_encode_line(ubyte *inBuff, int inLen, PHYSFS_file *fp)
402 register ubyte runCount; // max single runlength is 63
407 for (srcIndex = 1; srcIndex < inLen; srcIndex++) {
410 runCount++; // it encodes
411 if (runCount == 63) {
412 if (!(i=pcx_encode_byte(last, runCount, fp)))
417 } else { // this != last
419 if (!(i=pcx_encode_byte(last, runCount, fp)))
428 if (runCount) { // finish up
429 if (!(i=pcx_encode_byte(last, runCount, fp)))
436 // subroutine for writing an encoded byte pair
437 // returns count of bytes written, 0 if error
438 int pcx_encode_byte(ubyte byt, ubyte cnt, PHYSFS_file *fid)
441 if ( (cnt==1) && (0xc0 != (0xc0 & byt)) ) {
442 if(EOF == PHYSFSX_putc(fid, (int)byt))
443 return 0; // disk write error (probably full)
446 if(EOF == PHYSFSX_putc(fid, (int)0xC0 | cnt))
447 return 0; // disk write error
448 if(EOF == PHYSFSX_putc(fid, (int)byt))
449 return 0; // disk write error
456 //text for error messges
457 char pcx_error_messages[] = {
459 "Error opening file.\0"
460 "Couldn't read PCX header.\0"
461 "Unsupported PCX version.\0"
462 "Error reading data.\0"
463 "Couldn't find palette information.\0"
464 "Error writing data.\0"
468 //function to return pointer to error message
469 char *pcx_errormsg(int error_number)
471 char *p = pcx_error_messages;
473 while (error_number--) {
485 // fullscreen loading, 10/14/99 Jan Bobrowski
487 int pcx_read_fullscr(char * filename, ubyte * palette)
491 gr_init_bitmap_data(&bm);
492 pcx_error = pcx_read_bitmap(filename, &bm, BM_LINEAR, palette);
493 if (pcx_error == PCX_ERROR_NONE) {
495 gr_palette_load(palette);
499 gr_free_bitmap_data(&bm);