1 /* $Id: pcx.c,v 1.6 2002-08-26 06:41:38 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
57 int pcx_encode_byte(ubyte byt, ubyte cnt, FILE * fid);
58 int pcx_encode_line(ubyte *inBuff, int inLen, FILE * fp);
60 /* PCX Header data type */
72 ubyte ColorMap[16][3];
79 #define PCXHEADER_SIZE 128
82 #define PCXHeader_read_n(ph, n, fp) cfread(ph, sizeof(PCXHeader), n, fp)
85 * reads n PCXHeader structs from a CFILE
87 int PCXHeader_read_n(PCXHeader *ph, int n, CFILE *fp)
91 for (i = 0; i < n; i++) {
92 ph->Manufacturer = cfile_read_byte(fp);
93 ph->Version = cfile_read_byte(fp);
94 ph->Encoding = cfile_read_byte(fp);
95 ph->BitsPerPixel = cfile_read_byte(fp);
96 ph->Xmin = cfile_read_short(fp);
97 ph->Ymin = cfile_read_short(fp);
98 ph->Xmax = cfile_read_short(fp);
99 ph->Ymax = cfile_read_short(fp);
100 ph->Hdpi = cfile_read_short(fp);
101 ph->Vdpi = cfile_read_short(fp);
102 cfread(&ph->ColorMap, 16*3, 1, fp);
103 ph->Reserved = cfile_read_byte(fp);
104 ph->Nplanes = cfile_read_byte(fp);
105 ph->BytesPerLine = cfile_read_short(fp);
106 cfread(&ph->filler, 60, 1, fp);
112 int pcx_get_dimensions( char *filename, int *width, int *height)
117 PCXfile = cfopen(filename, "rb");
118 if (!PCXfile) return PCX_ERROR_OPENING;
120 if (PCXHeader_read_n(&header, 1, PCXfile) != 1) {
122 return PCX_ERROR_NO_HEADER;
126 *width = header.Xmax - header.Xmin+1;
127 *height = header.Ymax - header.Ymin+1;
129 return PCX_ERROR_NONE;
133 int pcx_read_bitmap_palette( char *filename, ubyte *palette)
140 PCXfile = cfopen( filename , "rb" );
142 return PCX_ERROR_OPENING;
144 // read 128 char PCX header
145 if (PCXHeader_read_n( &header, 1, PCXfile )!=1) {
147 return PCX_ERROR_NO_HEADER;
149 // Is it a 256 color PCX file?
150 if ((header.Manufacturer != 10)||(header.Encoding != 1)||(header.Nplanes != 1)||(header.BitsPerPixel != 8)||(header.Version != 5)) {
152 return PCX_ERROR_WRONG_VERSION;
155 // Read the extended palette at the end of PCX file
156 // Read in a character which should be 12 to be extended palette file
158 if (palette != NULL) {
159 cfseek( PCXfile, -768, SEEK_END );
160 cfread( palette, 3, 256, PCXfile );
161 cfseek( PCXfile, PCXHEADER_SIZE, SEEK_SET );
162 for (i=0; i<768; i++ )
165 for (i = 0; i < 3; i++) {
167 palette[i] = palette[765+i];
168 palette[765+i] = data;
175 int pcx_read_bitmap( char * filename, grs_bitmap * bmp,int bitmap_type ,ubyte * palette )
179 int i, row, col, count, xsize, ysize;
180 ubyte data, *pixdata;
181 #if defined(POLY_ACC)
182 unsigned char local_pal[768];
187 PCXfile = cfopen( filename , "rb" );
189 return PCX_ERROR_OPENING;
191 // read 128 char PCX header
192 if (PCXHeader_read_n( &header, 1, PCXfile )!=1) {
194 return PCX_ERROR_NO_HEADER;
197 // Is it a 256 color PCX file?
198 if ((header.Manufacturer != 10)||(header.Encoding != 1)||(header.Nplanes != 1)||(header.BitsPerPixel != 8)||(header.Version != 5)) {
200 return PCX_ERROR_WRONG_VERSION;
203 // Find the size of the image
204 xsize = header.Xmax - header.Xmin + 1;
205 ysize = header.Ymax - header.Ymin + 1;
207 #if defined(POLY_ACC)
208 // Read the extended palette at the end of PCX file
209 if(bitmap_type == BM_LINEAR15) // need palette for conversion from 8bit pcx to 15bit.
211 cfseek( PCXfile, -768, SEEK_END );
212 cfread( local_pal, 3, 256, PCXfile );
213 cfseek( PCXfile, PCXHEADER_SIZE, SEEK_SET );
214 for (i=0; i<768; i++ )
217 pa_update_clut(local_pal, 0, 256, 0);
221 if ( bitmap_type == BM_LINEAR ) {
222 if ( bmp->bm_data == NULL ) {
223 gr_init_bitmap_alloc (bmp, bitmap_type, 0, 0, xsize, ysize, xsize);
227 if ( bmp->bm_type == BM_LINEAR ) {
228 for (row=0; row< ysize ; row++) {
229 pixdata = &bmp->bm_data[bmp->bm_rowsize*row];
230 for (col=0; col< xsize ; ) {
231 if (cfread( &data, 1, 1, PCXfile )!=1 ) {
233 return PCX_ERROR_READING;
235 if ((data & 0xC0) == 0xC0) {
237 if (cfread( &data, 1, 1, PCXfile )!=1 ) {
239 return PCX_ERROR_READING;
244 else if (data == 255)
247 memset( pixdata, data, count );
254 else if (data == 255)
262 #if defined(POLY_ACC)
263 } else if( bmp->bm_type == BM_LINEAR15 ) {
264 ushort *pixdata2, pix15;
265 PA_DFX (pa_set_backbuffer_current());
266 PA_DFX (pa_set_write_mode(0));
267 for (row=0; row< ysize ; row++) {
268 pixdata2 = (ushort *)&bmp->bm_data[bmp->bm_rowsize*row];
269 for (col=0; col< xsize ; ) {
270 if (cfread( &data, 1, 1, PCXfile )!=1 ) {
272 return PCX_ERROR_READING;
274 if ((data & 0xC0) == 0xC0) {
276 if (cfread( &data, 1, 1, PCXfile )!=1 ) {
278 return PCX_ERROR_READING;
280 pix15 = pa_clut[data];
281 for(i = 0; i != count; ++i) pixdata2[i] = pix15;
285 *pixdata2++ = pa_clut[data];
291 PA_DFX (pa_swap_buffer());
292 PA_DFX (pa_set_frontbuffer_current());
296 for (row=0; row< ysize ; row++) {
297 for (col=0; col< xsize ; ) {
298 if (cfread( &data, 1, 1, PCXfile )!=1 ) {
300 return PCX_ERROR_READING;
302 if ((data & 0xC0) == 0xC0) {
304 if (cfread( &data, 1, 1, PCXfile )!=1 ) {
306 return PCX_ERROR_READING;
308 for (i=0;i<count;i++)
309 gr_bm_pixel( bmp, col+i, row, data );
312 gr_bm_pixel( bmp, col, row, data );
319 // Read the extended palette at the end of PCX file
320 if ( palette != NULL ) {
321 // Read in a character which should be 12 to be extended palette file
322 if (cfread( &data, 1, 1, PCXfile )==1) {
324 if (cfread(palette,768, 1, PCXfile)!=1) {
326 return PCX_ERROR_READING;
328 for (i=0; i<768; i++ )
331 for (i = 0; i < 3; i++) {
333 palette[i] = palette[765+i];
334 palette[765+i] = data;
340 return PCX_ERROR_NO_PALETTE;
344 return PCX_ERROR_NONE;
347 int pcx_write_bitmap( char * filename, grs_bitmap * bmp, ubyte * palette )
355 memset( &header, 0, PCXHEADER_SIZE );
357 header.Manufacturer = 10;
360 header.BitsPerPixel = 8;
362 header.Xmax = bmp->bm_w-1;
363 header.Ymax = bmp->bm_h-1;
364 header.BytesPerLine = bmp->bm_w;
366 PCXfile = fopen( filename , "wb" );
368 return PCX_ERROR_OPENING;
370 if ( fwrite( &header, PCXHEADER_SIZE, 1, PCXfile ) != 1 ) {
372 return PCX_ERROR_WRITING;
375 for (i=0; i<bmp->bm_h; i++ ) {
376 if (!pcx_encode_line( &bmp->bm_data[bmp->bm_rowsize*i], bmp->bm_w, PCXfile )) {
378 return PCX_ERROR_WRITING;
382 // Mark an extended palette
384 if (fwrite( &data, 1, 1, PCXfile )!=1) {
386 return PCX_ERROR_WRITING;
389 // Write the extended palette
390 for (i=0; i<768; i++ )
393 retval = fwrite( palette, 768, 1, PCXfile );
395 for (i=0; i<768; i++ )
400 return PCX_ERROR_WRITING;
404 return PCX_ERROR_NONE;
408 // returns number of bytes written into outBuff, 0 if failed
409 int pcx_encode_line(ubyte *inBuff, int inLen, FILE * fp)
414 register ubyte runCount; // max single runlength is 63
419 for (srcIndex = 1; srcIndex < inLen; srcIndex++) {
422 runCount++; // it encodes
423 if (runCount == 63) {
424 if (!(i=pcx_encode_byte(last, runCount, fp)))
429 } else { // this != last
431 if (!(i=pcx_encode_byte(last, runCount, fp)))
440 if (runCount) { // finish up
441 if (!(i=pcx_encode_byte(last, runCount, fp)))
448 // subroutine for writing an encoded byte pair
449 // returns count of bytes written, 0 if error
450 int pcx_encode_byte(ubyte byt, ubyte cnt, FILE * fid)
453 if ( (cnt==1) && (0xc0 != (0xc0 & byt)) ) {
454 if(EOF == putc((int)byt, fid))
455 return 0; // disk write error (probably full)
458 if(EOF == putc((int)0xC0 | cnt, fid))
459 return 0; // disk write error
460 if(EOF == putc((int)byt, fid))
461 return 0; // disk write error
468 //text for error messges
469 char pcx_error_messages[] = {
471 "Error opening file.\0"
472 "Couldn't read PCX header.\0"
473 "Unsupported PCX version.\0"
474 "Error reading data.\0"
475 "Couldn't find palette information.\0"
476 "Error writing data.\0"
480 //function to return pointer to error message
481 char *pcx_errormsg(int error_number)
483 char *p = pcx_error_messages;
485 while (error_number--) {
497 // fullscreen loading, 10/14/99 Jan Bobrowski
499 int pcx_read_fullscr(char * filename, ubyte * palette)
503 gr_init_bitmap_data(&bm);
504 pcx_error = pcx_read_bitmap(filename, &bm, BM_LINEAR, palette);
505 if (pcx_error == PCX_ERROR_NONE)
507 gr_free_bitmap_data(&bm);