1 /* $Id: pcx.c,v 1.8 2003-06-16 06:57:34 btb 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.
19 * Revision 1.6 1995/03/01 15:38:12 john
20 * Better ModeX support.
22 * Revision 1.5 1995/01/21 17:54:17 john
23 * Added pcx reader for modes other than modex.
25 * Revision 1.4 1994/12/08 19:03:56 john
26 * Made functions use cfile.
28 * Revision 1.3 1994/11/29 02:53:24 john
29 * Added error messages; made call be more similiar to iff.
31 * Revision 1.2 1994/11/28 20:03:50 john
32 * Added PCX functions.
34 * Revision 1.1 1994/11/28 19:57:56 john
61 int pcx_encode_byte(ubyte byt, ubyte cnt, CFILE *fid);
62 int pcx_encode_line(ubyte *inBuff, int inLen, CFILE *fp);
64 /* PCX Header data type */
76 ubyte ColorMap[16][3];
83 #define PCXHEADER_SIZE 128
86 #define PCXHeader_read_n(ph, n, fp) cfread(ph, sizeof(PCXHeader), n, fp)
89 * reads n PCXHeader structs from a CFILE
91 int PCXHeader_read_n(PCXHeader *ph, int n, CFILE *fp)
95 for (i = 0; i < n; i++) {
96 ph->Manufacturer = cfile_read_byte(fp);
97 ph->Version = cfile_read_byte(fp);
98 ph->Encoding = cfile_read_byte(fp);
99 ph->BitsPerPixel = cfile_read_byte(fp);
100 ph->Xmin = cfile_read_short(fp);
101 ph->Ymin = cfile_read_short(fp);
102 ph->Xmax = cfile_read_short(fp);
103 ph->Ymax = cfile_read_short(fp);
104 ph->Hdpi = cfile_read_short(fp);
105 ph->Vdpi = cfile_read_short(fp);
106 cfread(&ph->ColorMap, 16*3, 1, fp);
107 ph->Reserved = cfile_read_byte(fp);
108 ph->Nplanes = cfile_read_byte(fp);
109 ph->BytesPerLine = cfile_read_short(fp);
110 cfread(&ph->filler, 60, 1, fp);
116 int pcx_get_dimensions( char *filename, int *width, int *height)
121 PCXfile = cfopen(filename, "rb");
122 if (!PCXfile) return PCX_ERROR_OPENING;
124 if (PCXHeader_read_n(&header, 1, PCXfile) != 1) {
126 return PCX_ERROR_NO_HEADER;
130 *width = header.Xmax - header.Xmin+1;
131 *height = header.Ymax - header.Ymin+1;
133 return PCX_ERROR_NONE;
137 int pcx_read_bitmap_palette( char *filename, ubyte *palette)
144 PCXfile = cfopen( filename , "rb" );
146 return PCX_ERROR_OPENING;
148 // read 128 char PCX header
149 if (PCXHeader_read_n( &header, 1, PCXfile )!=1) {
151 return PCX_ERROR_NO_HEADER;
153 // Is it a 256 color PCX file?
154 if ((header.Manufacturer != 10)||(header.Encoding != 1)||(header.Nplanes != 1)||(header.BitsPerPixel != 8)||(header.Version != 5)) {
156 return PCX_ERROR_WRONG_VERSION;
159 // Read the extended palette at the end of PCX file
160 // Read in a character which should be 12 to be extended palette file
162 if (palette != NULL) {
163 cfseek( PCXfile, -768, SEEK_END );
164 cfread( palette, 3, 256, PCXfile );
165 cfseek( PCXfile, PCXHEADER_SIZE, SEEK_SET );
166 for (i=0; i<768; i++ )
169 for (i = 0; i < 3; i++) {
171 palette[i] = palette[765+i];
172 palette[765+i] = data;
179 int pcx_read_bitmap( char * filename, grs_bitmap * bmp,int bitmap_type ,ubyte * palette )
183 int i, row, col, count, xsize, ysize;
184 ubyte data, *pixdata;
185 #if defined(POLY_ACC)
186 unsigned char local_pal[768];
191 PCXfile = cfopen( filename , "rb" );
193 return PCX_ERROR_OPENING;
195 // read 128 char PCX header
196 if (PCXHeader_read_n( &header, 1, PCXfile )!=1) {
198 return PCX_ERROR_NO_HEADER;
201 // Is it a 256 color PCX file?
202 if ((header.Manufacturer != 10)||(header.Encoding != 1)||(header.Nplanes != 1)||(header.BitsPerPixel != 8)||(header.Version != 5)) {
204 return PCX_ERROR_WRONG_VERSION;
207 // Find the size of the image
208 xsize = header.Xmax - header.Xmin + 1;
209 ysize = header.Ymax - header.Ymin + 1;
211 #if defined(POLY_ACC)
212 // Read the extended palette at the end of PCX file
213 if(bitmap_type == BM_LINEAR15) // need palette for conversion from 8bit pcx to 15bit.
215 cfseek( PCXfile, -768, SEEK_END );
216 cfread( local_pal, 3, 256, PCXfile );
217 cfseek( PCXfile, PCXHEADER_SIZE, SEEK_SET );
218 for (i=0; i<768; i++ )
221 pa_update_clut(local_pal, 0, 256, 0);
225 if ( bitmap_type == BM_LINEAR ) {
226 if ( bmp->bm_data == NULL ) {
227 gr_init_bitmap_alloc (bmp, bitmap_type, 0, 0, xsize, ysize, xsize);
231 if ( bmp->bm_type == BM_LINEAR ) {
232 for (row=0; row< ysize ; row++) {
233 pixdata = &bmp->bm_data[bmp->bm_rowsize*row];
234 for (col=0; col< xsize ; ) {
235 if (cfread( &data, 1, 1, PCXfile )!=1 ) {
237 return PCX_ERROR_READING;
239 if ((data & 0xC0) == 0xC0) {
241 if (cfread( &data, 1, 1, PCXfile )!=1 ) {
243 return PCX_ERROR_READING;
248 else if (data == 255)
251 memset( pixdata, data, count );
258 else if (data == 255)
266 #if defined(POLY_ACC)
267 } else if( bmp->bm_type == BM_LINEAR15 ) {
268 ushort *pixdata2, pix15;
269 PA_DFX (pa_set_backbuffer_current());
270 PA_DFX (pa_set_write_mode(0));
271 for (row=0; row< ysize ; row++) {
272 pixdata2 = (ushort *)&bmp->bm_data[bmp->bm_rowsize*row];
273 for (col=0; col< xsize ; ) {
274 if (cfread( &data, 1, 1, PCXfile )!=1 ) {
276 return PCX_ERROR_READING;
278 if ((data & 0xC0) == 0xC0) {
280 if (cfread( &data, 1, 1, PCXfile )!=1 ) {
282 return PCX_ERROR_READING;
284 pix15 = pa_clut[data];
285 for(i = 0; i != count; ++i) pixdata2[i] = pix15;
289 *pixdata2++ = pa_clut[data];
295 PA_DFX (pa_swap_buffer());
296 PA_DFX (pa_set_frontbuffer_current());
300 for (row=0; row< ysize ; row++) {
301 for (col=0; col< xsize ; ) {
302 if (cfread( &data, 1, 1, PCXfile )!=1 ) {
304 return PCX_ERROR_READING;
306 if ((data & 0xC0) == 0xC0) {
308 if (cfread( &data, 1, 1, PCXfile )!=1 ) {
310 return PCX_ERROR_READING;
312 for (i=0;i<count;i++)
313 gr_bm_pixel( bmp, col+i, row, data );
316 gr_bm_pixel( bmp, col, row, data );
323 // Read the extended palette at the end of PCX file
324 if ( palette != NULL ) {
325 // Read in a character which should be 12 to be extended palette file
326 if (cfread( &data, 1, 1, PCXfile )==1) {
328 if (cfread(palette,768, 1, PCXfile)!=1) {
330 return PCX_ERROR_READING;
332 for (i=0; i<768; i++ )
335 for (i = 0; i < 3; i++) {
337 palette[i] = palette[765+i];
338 palette[765+i] = data;
344 return PCX_ERROR_NO_PALETTE;
348 return PCX_ERROR_NONE;
351 int pcx_write_bitmap( char * filename, grs_bitmap * bmp, ubyte * palette )
359 memset( &header, 0, PCXHEADER_SIZE );
361 header.Manufacturer = 10;
364 header.BitsPerPixel = 8;
366 header.Xmax = bmp->bm_w-1;
367 header.Ymax = bmp->bm_h-1;
368 header.BytesPerLine = bmp->bm_w;
370 PCXfile = cfopen(filename, "wb");
372 return PCX_ERROR_OPENING;
374 if (cfwrite(&header, PCXHEADER_SIZE, 1, PCXfile) != 1)
377 return PCX_ERROR_WRITING;
380 for (i=0; i<bmp->bm_h; i++ ) {
381 if (!pcx_encode_line( &bmp->bm_data[bmp->bm_rowsize*i], bmp->bm_w, PCXfile )) {
383 return PCX_ERROR_WRITING;
387 // Mark an extended palette
389 if (cfwrite(&data, 1, 1, PCXfile) != 1)
392 return PCX_ERROR_WRITING;
395 // Write the extended palette
396 for (i=0; i<768; i++ )
399 retval = cfwrite(palette, 768, 1, PCXfile);
401 for (i=0; i<768; i++ )
406 return PCX_ERROR_WRITING;
410 return PCX_ERROR_NONE;
414 // returns number of bytes written into outBuff, 0 if failed
415 int pcx_encode_line(ubyte *inBuff, int inLen, CFILE *fp)
420 register ubyte runCount; // max single runlength is 63
425 for (srcIndex = 1; srcIndex < inLen; srcIndex++) {
428 runCount++; // it encodes
429 if (runCount == 63) {
430 if (!(i=pcx_encode_byte(last, runCount, fp)))
435 } else { // this != last
437 if (!(i=pcx_encode_byte(last, runCount, fp)))
446 if (runCount) { // finish up
447 if (!(i=pcx_encode_byte(last, runCount, fp)))
454 // subroutine for writing an encoded byte pair
455 // returns count of bytes written, 0 if error
456 int pcx_encode_byte(ubyte byt, ubyte cnt, CFILE *fid)
459 if ( (cnt==1) && (0xc0 != (0xc0 & byt)) ) {
460 if(EOF == cfputc((int)byt, fid))
461 return 0; // disk write error (probably full)
464 if(EOF == cfputc((int)0xC0 | cnt, fid))
465 return 0; // disk write error
466 if(EOF == cfputc((int)byt, fid))
467 return 0; // disk write error
474 //text for error messges
475 char pcx_error_messages[] = {
477 "Error opening file.\0"
478 "Couldn't read PCX header.\0"
479 "Unsupported PCX version.\0"
480 "Error reading data.\0"
481 "Couldn't find palette information.\0"
482 "Error writing data.\0"
486 //function to return pointer to error message
487 char *pcx_errormsg(int error_number)
489 char *p = pcx_error_messages;
491 while (error_number--) {
503 // fullscreen loading, 10/14/99 Jan Bobrowski
505 int pcx_read_fullscr(char * filename, ubyte * palette)
509 gr_init_bitmap_data(&bm);
510 pcx_error = pcx_read_bitmap(filename, &bm, BM_LINEAR, palette);
511 if (pcx_error == PCX_ERROR_NONE) {
513 gr_palette_load(palette);
517 gr_free_bitmap_data(&bm);