1 /* $Id: pcx.c,v 1.9 2004-08-28 23:17:45 schaffner 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.
42 int pcx_encode_byte(ubyte byt, ubyte cnt, CFILE *fid);
43 int pcx_encode_line(ubyte *inBuff, int inLen, CFILE *fp);
45 /* PCX Header data type */
57 ubyte ColorMap[16][3];
64 #define PCXHEADER_SIZE 128
67 #define PCXHeader_read_n(ph, n, fp) cfread(ph, sizeof(PCXHeader), n, fp)
70 * reads n PCXHeader structs from a CFILE
72 int PCXHeader_read_n(PCXHeader *ph, int n, CFILE *fp)
76 for (i = 0; i < n; i++) {
77 ph->Manufacturer = cfile_read_byte(fp);
78 ph->Version = cfile_read_byte(fp);
79 ph->Encoding = cfile_read_byte(fp);
80 ph->BitsPerPixel = cfile_read_byte(fp);
81 ph->Xmin = cfile_read_short(fp);
82 ph->Ymin = cfile_read_short(fp);
83 ph->Xmax = cfile_read_short(fp);
84 ph->Ymax = cfile_read_short(fp);
85 ph->Hdpi = cfile_read_short(fp);
86 ph->Vdpi = cfile_read_short(fp);
87 cfread(&ph->ColorMap, 16*3, 1, fp);
88 ph->Reserved = cfile_read_byte(fp);
89 ph->Nplanes = cfile_read_byte(fp);
90 ph->BytesPerLine = cfile_read_short(fp);
91 cfread(&ph->filler, 60, 1, fp);
97 int pcx_get_dimensions( char *filename, int *width, int *height)
102 PCXfile = cfopen(filename, "rb");
103 if (!PCXfile) return PCX_ERROR_OPENING;
105 if (PCXHeader_read_n(&header, 1, PCXfile) != 1) {
107 return PCX_ERROR_NO_HEADER;
111 *width = header.Xmax - header.Xmin+1;
112 *height = header.Ymax - header.Ymin+1;
114 return PCX_ERROR_NONE;
118 int pcx_read_bitmap_palette( char *filename, ubyte *palette)
125 PCXfile = cfopen( filename , "rb" );
127 return PCX_ERROR_OPENING;
129 // read 128 char PCX header
130 if (PCXHeader_read_n( &header, 1, PCXfile )!=1) {
132 return PCX_ERROR_NO_HEADER;
134 // Is it a 256 color PCX file?
135 if ((header.Manufacturer != 10)||(header.Encoding != 1)||(header.Nplanes != 1)||(header.BitsPerPixel != 8)||(header.Version != 5)) {
137 return PCX_ERROR_WRONG_VERSION;
140 // Read the extended palette at the end of PCX file
141 // Read in a character which should be 12 to be extended palette file
143 if (palette != NULL) {
144 cfseek( PCXfile, -768, SEEK_END );
145 cfread( palette, 3, 256, PCXfile );
146 cfseek( PCXfile, PCXHEADER_SIZE, SEEK_SET );
147 for (i=0; i<768; i++ )
150 for (i = 0; i < 3; i++) {
152 palette[i] = palette[765+i];
153 palette[765+i] = data;
160 int pcx_read_bitmap( char * filename, grs_bitmap * bmp,int bitmap_type ,ubyte * palette )
164 int i, row, col, count, xsize, ysize;
165 ubyte data, *pixdata;
166 #if defined(POLY_ACC)
167 unsigned char local_pal[768];
172 PCXfile = cfopen( filename , "rb" );
174 return PCX_ERROR_OPENING;
176 // read 128 char PCX header
177 if (PCXHeader_read_n( &header, 1, PCXfile )!=1) {
179 return PCX_ERROR_NO_HEADER;
182 // Is it a 256 color PCX file?
183 if ((header.Manufacturer != 10)||(header.Encoding != 1)||(header.Nplanes != 1)||(header.BitsPerPixel != 8)||(header.Version != 5)) {
185 return PCX_ERROR_WRONG_VERSION;
188 // Find the size of the image
189 xsize = header.Xmax - header.Xmin + 1;
190 ysize = header.Ymax - header.Ymin + 1;
192 #if defined(POLY_ACC)
193 // Read the extended palette at the end of PCX file
194 if(bitmap_type == BM_LINEAR15) // need palette for conversion from 8bit pcx to 15bit.
196 cfseek( PCXfile, -768, SEEK_END );
197 cfread( local_pal, 3, 256, PCXfile );
198 cfseek( PCXfile, PCXHEADER_SIZE, SEEK_SET );
199 for (i=0; i<768; i++ )
202 pa_update_clut(local_pal, 0, 256, 0);
206 if ( bitmap_type == BM_LINEAR ) {
207 if ( bmp->bm_data == NULL ) {
208 gr_init_bitmap_alloc (bmp, bitmap_type, 0, 0, xsize, ysize, xsize);
212 if ( bmp->bm_type == BM_LINEAR ) {
213 for (row=0; row< ysize ; row++) {
214 pixdata = &bmp->bm_data[bmp->bm_rowsize*row];
215 for (col=0; col< xsize ; ) {
216 if (cfread( &data, 1, 1, PCXfile )!=1 ) {
218 return PCX_ERROR_READING;
220 if ((data & 0xC0) == 0xC0) {
222 if (cfread( &data, 1, 1, PCXfile )!=1 ) {
224 return PCX_ERROR_READING;
229 else if (data == 255)
232 memset( pixdata, data, count );
239 else if (data == 255)
247 #if defined(POLY_ACC)
248 } else if( bmp->bm_type == BM_LINEAR15 ) {
249 ushort *pixdata2, pix15;
250 PA_DFX (pa_set_backbuffer_current());
251 PA_DFX (pa_set_write_mode(0));
252 for (row=0; row< ysize ; row++) {
253 pixdata2 = (ushort *)&bmp->bm_data[bmp->bm_rowsize*row];
254 for (col=0; col< xsize ; ) {
255 if (cfread( &data, 1, 1, PCXfile )!=1 ) {
257 return PCX_ERROR_READING;
259 if ((data & 0xC0) == 0xC0) {
261 if (cfread( &data, 1, 1, PCXfile )!=1 ) {
263 return PCX_ERROR_READING;
265 pix15 = pa_clut[data];
266 for(i = 0; i != count; ++i) pixdata2[i] = pix15;
270 *pixdata2++ = pa_clut[data];
276 PA_DFX (pa_swap_buffer());
277 PA_DFX (pa_set_frontbuffer_current());
281 for (row=0; row< ysize ; row++) {
282 for (col=0; col< xsize ; ) {
283 if (cfread( &data, 1, 1, PCXfile )!=1 ) {
285 return PCX_ERROR_READING;
287 if ((data & 0xC0) == 0xC0) {
289 if (cfread( &data, 1, 1, PCXfile )!=1 ) {
291 return PCX_ERROR_READING;
293 for (i=0;i<count;i++)
294 gr_bm_pixel( bmp, col+i, row, data );
297 gr_bm_pixel( bmp, col, row, data );
304 // Read the extended palette at the end of PCX file
305 if ( palette != NULL ) {
306 // Read in a character which should be 12 to be extended palette file
307 if (cfread( &data, 1, 1, PCXfile )==1) {
309 if (cfread(palette,768, 1, PCXfile)!=1) {
311 return PCX_ERROR_READING;
313 for (i=0; i<768; i++ )
316 for (i = 0; i < 3; i++) {
318 palette[i] = palette[765+i];
319 palette[765+i] = data;
325 return PCX_ERROR_NO_PALETTE;
329 return PCX_ERROR_NONE;
332 int pcx_write_bitmap( char * filename, grs_bitmap * bmp, ubyte * palette )
340 memset( &header, 0, PCXHEADER_SIZE );
342 header.Manufacturer = 10;
345 header.BitsPerPixel = 8;
347 header.Xmax = bmp->bm_w-1;
348 header.Ymax = bmp->bm_h-1;
349 header.BytesPerLine = bmp->bm_w;
351 PCXfile = cfopen(filename, "wb");
353 return PCX_ERROR_OPENING;
355 if (cfwrite(&header, PCXHEADER_SIZE, 1, PCXfile) != 1)
358 return PCX_ERROR_WRITING;
361 for (i=0; i<bmp->bm_h; i++ ) {
362 if (!pcx_encode_line( &bmp->bm_data[bmp->bm_rowsize*i], bmp->bm_w, PCXfile )) {
364 return PCX_ERROR_WRITING;
368 // Mark an extended palette
370 if (cfwrite(&data, 1, 1, PCXfile) != 1)
373 return PCX_ERROR_WRITING;
376 // Write the extended palette
377 for (i=0; i<768; i++ )
380 retval = cfwrite(palette, 768, 1, PCXfile);
382 for (i=0; i<768; i++ )
387 return PCX_ERROR_WRITING;
391 return PCX_ERROR_NONE;
395 // returns number of bytes written into outBuff, 0 if failed
396 int pcx_encode_line(ubyte *inBuff, int inLen, CFILE *fp)
401 register ubyte runCount; // max single runlength is 63
406 for (srcIndex = 1; srcIndex < inLen; srcIndex++) {
409 runCount++; // it encodes
410 if (runCount == 63) {
411 if (!(i=pcx_encode_byte(last, runCount, fp)))
416 } else { // this != last
418 if (!(i=pcx_encode_byte(last, runCount, fp)))
427 if (runCount) { // finish up
428 if (!(i=pcx_encode_byte(last, runCount, fp)))
435 // subroutine for writing an encoded byte pair
436 // returns count of bytes written, 0 if error
437 int pcx_encode_byte(ubyte byt, ubyte cnt, CFILE *fid)
440 if ( (cnt==1) && (0xc0 != (0xc0 & byt)) ) {
441 if(EOF == cfputc((int)byt, fid))
442 return 0; // disk write error (probably full)
445 if(EOF == cfputc((int)0xC0 | cnt, fid))
446 return 0; // disk write error
447 if(EOF == cfputc((int)byt, fid))
448 return 0; // disk write error
455 //text for error messges
456 char pcx_error_messages[] = {
458 "Error opening file.\0"
459 "Couldn't read PCX header.\0"
460 "Unsupported PCX version.\0"
461 "Error reading data.\0"
462 "Couldn't find palette information.\0"
463 "Error writing data.\0"
467 //function to return pointer to error message
468 char *pcx_errormsg(int error_number)
470 char *p = pcx_error_messages;
472 while (error_number--) {
484 // fullscreen loading, 10/14/99 Jan Bobrowski
486 int pcx_read_fullscr(char * filename, ubyte * palette)
490 gr_init_bitmap_data(&bm);
491 pcx_error = pcx_read_bitmap(filename, &bm, BM_LINEAR, palette);
492 if (pcx_error == PCX_ERROR_NONE) {
494 gr_palette_load(palette);
498 gr_free_bitmap_data(&bm);