1 /* $Id: pcx.c,v 1.7 2003/02/28 09:56:10 btb Exp $ */
2 /*
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.
13 */
14 /*
15  *
16  * Routines to read/write pcx images.
17  *
18  * Old Log:
19  * Revision 1.6  1995/03/01  15:38:12  john
20  * Better ModeX support.
21  *
22  * Revision 1.5  1995/01/21  17:54:17  john
23  * Added pcx reader for modes other than modex.
24  *
25  * Revision 1.4  1994/12/08  19:03:56  john
26  * Made functions use cfile.
27  *
28  * Revision 1.3  1994/11/29  02:53:24  john
29  * Added error messages; made call be more similiar to iff.
30  *
31  * Revision 1.2  1994/11/28  20:03:50  john
32  * Added PCX functions.
33  *
34  * Revision 1.1  1994/11/28  19:57:56  john
35  * Initial revision
36  *
37  */
38 
39 #ifdef HAVE_CONFIG_H
40 #include <conf.h>
41 #endif
42 
43 #include <stdlib.h>
44 #include <stdio.h>
45 #include <string.h>
46 
47 #include "gr.h"
48 #include "grdef.h"
49 #include "u_mem.h"
50 #include "pcx.h"
51 #include "cfile.h"
52 
53 #ifdef OGL
54 #include "palette.h"
55 #endif
56 
57 #if defined(POLY_ACC)
58 #include "poly_acc.h"
59 #endif
60 
61 int pcx_encode_byte(ubyte byt, ubyte cnt, FILE * fid);
62 int pcx_encode_line(ubyte *inBuff, int inLen, FILE * fp);
63 
64 /* PCX Header data type */
65 typedef struct {
66 	ubyte   Manufacturer;
67 	ubyte   Version;
68 	ubyte   Encoding;
69 	ubyte   BitsPerPixel;
70 	short   Xmin;
71 	short   Ymin;
72 	short   Xmax;
73 	short   Ymax;
74 	short   Hdpi;
75 	short   Vdpi;
76 	ubyte   ColorMap[16][3];
77 	ubyte   Reserved;
78 	ubyte   Nplanes;
79 	short   BytesPerLine;
80 	ubyte   filler[60];
81 } __pack__ PCXHeader;
82 
83 #define PCXHEADER_SIZE 128
84 
85 #ifdef FAST_FILE_IO
86 #define PCXHeader_read_n(ph, n, fp) cfread(ph, sizeof(PCXHeader), n, fp)
87 #else
88 /*
89  * reads n PCXHeader structs from a CFILE
90  */
PCXHeader_read_n(PCXHeader * ph,int n,CFILE * fp)91 int PCXHeader_read_n(PCXHeader *ph, int n, CFILE *fp)
92 {
93 	int i;
94 
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);
111 	}
112 	return i;
113 }
114 #endif
115 
pcx_get_dimensions(char * filename,int * width,int * height)116 int pcx_get_dimensions( char *filename, int *width, int *height)
117 {
118 	CFILE *PCXfile;
119 	PCXHeader header;
120 
121 	PCXfile = cfopen(filename, "rb");
122 	if (!PCXfile) return PCX_ERROR_OPENING;
123 
124 	if (PCXHeader_read_n(&header, 1, PCXfile) != 1) {
125 		cfclose(PCXfile);
126 		return PCX_ERROR_NO_HEADER;
127 	}
128 	cfclose(PCXfile);
129 
130 	*width = header.Xmax - header.Xmin+1;
131 	*height = header.Ymax - header.Ymin+1;
132 
133 	return PCX_ERROR_NONE;
134 }
135 
136 #ifdef MACINTOSH
pcx_read_bitmap_palette(char * filename,ubyte * palette)137 int pcx_read_bitmap_palette( char *filename, ubyte *palette)
138 {
139 	PCXHeader header;
140 	CFILE * PCXfile;
141 	ubyte data;
142 	int i;
143 
144 	PCXfile = cfopen( filename , "rb" );
145 	if ( !PCXfile )
146 		return PCX_ERROR_OPENING;
147 
148 	// read 128 char PCX header
149 	if (PCXHeader_read_n( &header, 1, PCXfile )!=1)	{
150 		cfclose( PCXfile );
151 		return PCX_ERROR_NO_HEADER;
152 	}
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))	{
155 		cfclose( PCXfile );
156 		return PCX_ERROR_WRONG_VERSION;
157 	}
158 
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
161 
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++ )
167 			palette[i] >>= 2;
168 #ifdef MACINTOSH
169 		for (i = 0; i < 3; i++) {
170 			data = palette[i];
171 			palette[i] = palette[765+i];
172 			palette[765+i] = data;
173 		}
174 #endif
175 	}
176 }
177 #endif
178 
pcx_read_bitmap(char * filename,grs_bitmap * bmp,int bitmap_type,ubyte * palette)179 int pcx_read_bitmap( char * filename, grs_bitmap * bmp,int bitmap_type ,ubyte * palette )
180 {
181 	PCXHeader header;
182 	CFILE * PCXfile;
183 	int i, row, col, count, xsize, ysize;
184 	ubyte data, *pixdata;
185 #if defined(POLY_ACC)
186     unsigned char local_pal[768];
187 
188     pa_flush();
189 #endif
190 
191 	PCXfile = cfopen( filename , "rb" );
192 	if ( !PCXfile )
193 		return PCX_ERROR_OPENING;
194 
195 	// read 128 char PCX header
196 	if (PCXHeader_read_n( &header, 1, PCXfile )!=1) {
197 		cfclose( PCXfile );
198 		return PCX_ERROR_NO_HEADER;
199 	}
200 
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))	{
203 		cfclose( PCXfile );
204 		return PCX_ERROR_WRONG_VERSION;
205 	}
206 
207 	// Find the size of the image
208 	xsize = header.Xmax - header.Xmin + 1;
209 	ysize = header.Ymax - header.Ymin + 1;
210 
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.
214     {
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++ )
219             local_pal[i] >>= 2;
220         pa_save_clut();
221         pa_update_clut(local_pal, 0, 256, 0);
222     }
223 #endif
224 
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);
228 		}
229 	}
230 
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 )	{
236 					cfclose( PCXfile );
237 					return PCX_ERROR_READING;
238 				}
239 				if ((data & 0xC0) == 0xC0)     {
240 					count =  data & 0x3F;
241 					if (cfread( &data, 1, 1, PCXfile )!=1 )	{
242 						cfclose( PCXfile );
243 						return PCX_ERROR_READING;
244 					}
245 #ifdef MACINTOSH
246 					if (data == 0)
247 						data = 255;
248 					else if (data == 255)
249 						data = 0;
250 #endif
251 					memset( pixdata, data, count );
252 					pixdata += count;
253 					col += count;
254 				} else {
255 #ifdef MACINTOSH
256 					if (data == 0)
257 						data = 255;
258 					else if (data == 255)
259 						data = 0;
260 #endif
261 					*pixdata++ = data;
262 					col++;
263 				}
264 			}
265 		}
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 )	{
275 					cfclose( PCXfile );
276 					return PCX_ERROR_READING;
277 				}
278 				if ((data & 0xC0) == 0xC0)     {
279 					count =  data & 0x3F;
280 					if (cfread( &data, 1, 1, PCXfile )!=1 )	{
281 						cfclose( PCXfile );
282 						return PCX_ERROR_READING;
283 					}
284                     pix15 = pa_clut[data];
285                     for(i = 0; i != count; ++i) pixdata2[i] = pix15;
286                     pixdata2 += count;
287 					col += count;
288 				} else {
289                     *pixdata2++ = pa_clut[data];
290 					col++;
291 				}
292 			}
293         }
294         pa_restore_clut();
295 		  PA_DFX (pa_swap_buffer());
296         PA_DFX (pa_set_frontbuffer_current());
297 
298 #endif
299 	} else {
300 		for (row=0; row< ysize ; row++)      {
301 			for (col=0; col< xsize ; )      {
302 				if (cfread( &data, 1, 1, PCXfile )!=1 )	{
303 					cfclose( PCXfile );
304 					return PCX_ERROR_READING;
305 				}
306 				if ((data & 0xC0) == 0xC0)     {
307 					count =  data & 0x3F;
308 					if (cfread( &data, 1, 1, PCXfile )!=1 )	{
309 						cfclose( PCXfile );
310 						return PCX_ERROR_READING;
311 					}
312 					for (i=0;i<count;i++)
313 						gr_bm_pixel( bmp, col+i, row, data );
314 					col += count;
315 				} else {
316 					gr_bm_pixel( bmp, col, row, data );
317 					col++;
318 				}
319 			}
320 		}
321 	}
322 
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)	{
327 			if ( data == 12 )	{
328 				if (cfread(palette,768, 1, PCXfile)!=1)	{
329 					cfclose( PCXfile );
330 					return PCX_ERROR_READING;
331 				}
332 				for (i=0; i<768; i++ )
333 					palette[i] >>= 2;
334 #ifdef MACINTOSH
335 				for (i = 0; i < 3; i++) {
336 					data = palette[i];
337 					palette[i] = palette[765+i];
338 					palette[765+i] = data;
339 				}
340 #endif
341 			}
342 		} else {
343 			cfclose( PCXfile );
344 			return PCX_ERROR_NO_PALETTE;
345 		}
346 	}
347 	cfclose(PCXfile);
348 	return PCX_ERROR_NONE;
349 }
350 
pcx_write_bitmap(char * filename,grs_bitmap * bmp,ubyte * palette)351 int pcx_write_bitmap( char * filename, grs_bitmap * bmp, ubyte * palette )
352 {
353 	int retval;
354 	int i;
355 	ubyte data;
356 	PCXHeader header;
357 	FILE * PCXfile;
358 
359 	memset( &header, 0, PCXHEADER_SIZE );
360 
361 	header.Manufacturer = 10;
362 	header.Encoding = 1;
363 	header.Nplanes = 1;
364 	header.BitsPerPixel = 8;
365 	header.Version = 5;
366 	header.Xmax = bmp->bm_w-1;
367 	header.Ymax = bmp->bm_h-1;
368 	header.BytesPerLine = bmp->bm_w;
369 
370 	PCXfile = fopen( filename , "wb" );
371 	if ( !PCXfile )
372 		return PCX_ERROR_OPENING;
373 
374 	if ( fwrite( &header, PCXHEADER_SIZE, 1, PCXfile ) != 1 )	{
375 		fclose( PCXfile );
376 		return PCX_ERROR_WRITING;
377 	}
378 
379 	for (i=0; i<bmp->bm_h; i++ )	{
380 		if (!pcx_encode_line( &bmp->bm_data[bmp->bm_rowsize*i], bmp->bm_w, PCXfile ))	{
381 			fclose( PCXfile );
382 			return PCX_ERROR_WRITING;
383 		}
384 	}
385 
386 	// Mark an extended palette
387 	data = 12;
388 	if (fwrite( &data, 1, 1, PCXfile )!=1)	{
389 		fclose( PCXfile );
390 		return PCX_ERROR_WRITING;
391 	}
392 
393 	// Write the extended palette
394 	for (i=0; i<768; i++ )
395 		palette[i] <<= 2;
396 
397 	retval = fwrite( palette, 768, 1, PCXfile );
398 
399 	for (i=0; i<768; i++ )
400 		palette[i] >>= 2;
401 
402 	if (retval !=1)	{
403 		fclose( PCXfile );
404 		return PCX_ERROR_WRITING;
405 	}
406 
407 	fclose( PCXfile );
408 	return PCX_ERROR_NONE;
409 
410 }
411 
412 // returns number of bytes written into outBuff, 0 if failed
pcx_encode_line(ubyte * inBuff,int inLen,FILE * fp)413 int pcx_encode_line(ubyte *inBuff, int inLen, FILE * fp)
414 {
415 	ubyte this, last;
416 	int srcIndex, i;
417 	register int total;
418 	register ubyte runCount; 	// max single runlength is 63
419 	total = 0;
420 	last = *(inBuff);
421 	runCount = 1;
422 
423 	for (srcIndex = 1; srcIndex < inLen; srcIndex++) {
424 		this = *(++inBuff);
425 		if (this == last)	{
426 			runCount++;			// it encodes
427 			if (runCount == 63)	{
428 				if (!(i=pcx_encode_byte(last, runCount, fp)))
429 					return(0);
430 				total += i;
431 				runCount = 0;
432 			}
433 		} else {   	// this != last
434 			if (runCount)	{
435 				if (!(i=pcx_encode_byte(last, runCount, fp)))
436 					return(0);
437 				total += i;
438 			}
439 			last = this;
440 			runCount = 1;
441 		}
442 	}
443 
444 	if (runCount)	{		// finish up
445 		if (!(i=pcx_encode_byte(last, runCount, fp)))
446 			return 0;
447 		return total + i;
448 	}
449 	return total;
450 }
451 
452 // subroutine for writing an encoded byte pair
453 // returns count of bytes written, 0 if error
pcx_encode_byte(ubyte byt,ubyte cnt,FILE * fid)454 int pcx_encode_byte(ubyte byt, ubyte cnt, FILE * fid)
455 {
456 	if (cnt) {
457 		if ( (cnt==1) && (0xc0 != (0xc0 & byt)) )	{
458 			if(EOF == putc((int)byt, fid))
459 				return 0; 	// disk write error (probably full)
460 			return 1;
461 		} else {
462 			if(EOF == putc((int)0xC0 | cnt, fid))
463 				return 0; 	// disk write error
464 			if(EOF == putc((int)byt, fid))
465 				return 0; 	// disk write error
466 			return 2;
467 		}
468 	}
469 	return 0;
470 }
471 
472 //text for error messges
473 char pcx_error_messages[] = {
474 	"No error.\0"
475 	"Error opening file.\0"
476 	"Couldn't read PCX header.\0"
477 	"Unsupported PCX version.\0"
478 	"Error reading data.\0"
479 	"Couldn't find palette information.\0"
480 	"Error writing data.\0"
481 };
482 
483 
484 //function to return pointer to error message
pcx_errormsg(int error_number)485 char *pcx_errormsg(int error_number)
486 {
487 	char *p = pcx_error_messages;
488 
489 	while (error_number--) {
490 
491 		if (!p) return NULL;
492 
493 		p += strlen(p)+1;
494 
495 	}
496 
497 	return p;
498 
499 }
500 
501 // fullscreen loading, 10/14/99 Jan Bobrowski
502 
pcx_read_fullscr(char * filename,ubyte * palette)503 int pcx_read_fullscr(char * filename, ubyte * palette)
504 {
505 	int pcx_error;
506 	grs_bitmap bm;
507 	gr_init_bitmap_data(&bm);
508 	pcx_error = pcx_read_bitmap(filename, &bm, BM_LINEAR, palette);
509 	if (pcx_error == PCX_ERROR_NONE) {
510 #ifdef OGL
511 		gr_palette_load(palette);
512 #endif
513 		show_fullscr(&bm);
514 	}
515 	gr_free_bitmap_data(&bm);
516 	return pcx_error;
517 }
518