2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
11 COPYRIGHT 1993-1998 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
15 * Routines to read/write pcx images.
33 int pcx_encode_byte(ubyte byt, ubyte cnt, PHYSFS_file *fid);
34 int pcx_encode_line(ubyte *inBuff, int inLen, PHYSFS_file *fp);
36 /* PCX Header data type */
48 ubyte ColorMap[16][3];
55 #define PCXHEADER_SIZE 128
58 #define PCXHeader_read_n(ph, n, fp) cfread(ph, sizeof(PCXHeader), n, fp)
61 * reads n PCXHeader structs from a CFILE
63 int PCXHeader_read_n(PCXHeader *ph, int n, CFILE *fp)
67 for (i = 0; i < n; i++) {
68 ph->Manufacturer = cfile_read_byte(fp);
69 ph->Version = cfile_read_byte(fp);
70 ph->Encoding = cfile_read_byte(fp);
71 ph->BitsPerPixel = cfile_read_byte(fp);
72 ph->Xmin = cfile_read_short(fp);
73 ph->Ymin = cfile_read_short(fp);
74 ph->Xmax = cfile_read_short(fp);
75 ph->Ymax = cfile_read_short(fp);
76 ph->Hdpi = cfile_read_short(fp);
77 ph->Vdpi = cfile_read_short(fp);
78 cfread(&ph->ColorMap, 16*3, 1, fp);
79 ph->Reserved = cfile_read_byte(fp);
80 ph->Nplanes = cfile_read_byte(fp);
81 ph->BytesPerLine = cfile_read_short(fp);
82 cfread(&ph->filler, 60, 1, fp);
88 int pcx_get_dimensions( char *filename, int *width, int *height)
93 PCXfile = cfopen(filename, "rb");
94 if (!PCXfile) return PCX_ERROR_OPENING;
96 if (PCXHeader_read_n(&header, 1, PCXfile) != 1) {
98 return PCX_ERROR_NO_HEADER;
102 *width = header.Xmax - header.Xmin+1;
103 *height = header.Ymax - header.Ymin+1;
105 return PCX_ERROR_NONE;
109 int pcx_read_bitmap_palette( char *filename, ubyte *palette)
116 PCXfile = cfopen( filename , "rb" );
118 return PCX_ERROR_OPENING;
120 // read 128 char PCX header
121 if (PCXHeader_read_n( &header, 1, PCXfile )!=1) {
123 return PCX_ERROR_NO_HEADER;
125 // Is it a 256 color PCX file?
126 if ((header.Manufacturer != 10)||(header.Encoding != 1)||(header.Nplanes != 1)||(header.BitsPerPixel != 8)||(header.Version != 5)) {
128 return PCX_ERROR_WRONG_VERSION;
131 // Read the extended palette at the end of PCX file
132 // Read in a character which should be 12 to be extended palette file
134 if (palette != NULL) {
135 cfseek( PCXfile, -768, SEEK_END );
136 cfread( palette, 3, 256, PCXfile );
137 cfseek( PCXfile, PCXHEADER_SIZE, SEEK_SET );
138 for (i=0; i<768; i++ )
141 for (i = 0; i < 3; i++) {
143 palette[i] = palette[765+i];
144 palette[765+i] = data;
151 int pcx_read_bitmap( char * filename, grs_bitmap * bmp,int bitmap_type ,ubyte * palette )
155 int i, row, col, count, xsize, ysize;
156 ubyte data, *pixdata;
158 PCXfile = cfopen( filename , "rb" );
160 return PCX_ERROR_OPENING;
162 // read 128 char PCX header
163 if (PCXHeader_read_n( &header, 1, PCXfile )!=1) {
165 return PCX_ERROR_NO_HEADER;
168 // Is it a 256 color PCX file?
169 if ((header.Manufacturer != 10)||(header.Encoding != 1)||(header.Nplanes != 1)||(header.BitsPerPixel != 8)||(header.Version != 5)) {
171 return PCX_ERROR_WRONG_VERSION;
174 // Find the size of the image
175 xsize = header.Xmax - header.Xmin + 1;
176 ysize = header.Ymax - header.Ymin + 1;
178 if ( bitmap_type == BM_LINEAR ) {
179 if ( bmp->bm_data == NULL ) {
180 gr_init_bitmap_alloc (bmp, bitmap_type, 0, 0, xsize, ysize, xsize);
184 if ( bmp->bm_type == BM_LINEAR ) {
185 for (row=0; row< ysize ; row++) {
186 pixdata = &bmp->bm_data[bmp->bm_rowsize*row];
187 for (col=0; col< xsize ; ) {
188 if (cfread( &data, 1, 1, PCXfile )!=1 ) {
190 return PCX_ERROR_READING;
192 if ((data & 0xC0) == 0xC0) {
194 if (cfread( &data, 1, 1, PCXfile )!=1 ) {
196 return PCX_ERROR_READING;
201 else if (data == 255)
204 memset( pixdata, data, count );
211 else if (data == 255)
220 for (row=0; row< ysize ; row++) {
221 for (col=0; col< xsize ; ) {
222 if (cfread( &data, 1, 1, PCXfile )!=1 ) {
224 return PCX_ERROR_READING;
226 if ((data & 0xC0) == 0xC0) {
228 if (cfread( &data, 1, 1, PCXfile )!=1 ) {
230 return PCX_ERROR_READING;
232 for (i=0;i<count;i++)
233 gr_bm_pixel( bmp, col+i, row, data );
236 gr_bm_pixel( bmp, col, row, data );
243 // Read the extended palette at the end of PCX file
244 if ( palette != NULL ) {
245 // Read in a character which should be 12 to be extended palette file
246 if (cfread( &data, 1, 1, PCXfile )==1) {
248 if (cfread(palette,768, 1, PCXfile)!=1) {
250 return PCX_ERROR_READING;
252 for (i=0; i<768; i++ )
255 for (i = 0; i < 3; i++) {
257 palette[i] = palette[765+i];
258 palette[765+i] = data;
264 return PCX_ERROR_NO_PALETTE;
268 return PCX_ERROR_NONE;
271 int pcx_write_bitmap( char * filename, grs_bitmap * bmp, ubyte * palette )
277 PHYSFS_file *PCXfile;
279 memset( &header, 0, PCXHEADER_SIZE );
281 header.Manufacturer = 10;
284 header.BitsPerPixel = 8;
286 header.Xmax = bmp->bm_w-1;
287 header.Ymax = bmp->bm_h-1;
288 header.BytesPerLine = bmp->bm_w;
290 PCXfile = PHYSFSX_openWriteBuffered(filename);
292 return PCX_ERROR_OPENING;
294 if (PHYSFS_write(PCXfile, &header, PCXHEADER_SIZE, 1) != 1)
296 PHYSFS_close(PCXfile);
297 return PCX_ERROR_WRITING;
300 for (i=0; i<bmp->bm_h; i++ ) {
301 if (!pcx_encode_line( &bmp->bm_data[bmp->bm_rowsize*i], bmp->bm_w, PCXfile )) {
302 PHYSFS_close(PCXfile);
303 return PCX_ERROR_WRITING;
307 // Mark an extended palette
309 if (PHYSFS_write(PCXfile, &data, 1, 1) != 1)
311 PHYSFS_close(PCXfile);
312 return PCX_ERROR_WRITING;
315 // Write the extended palette
316 for (i=0; i<768; i++ )
319 retval = (int)PHYSFS_write(PCXfile, palette, 768, 1);
321 for (i=0; i<768; i++ )
325 PHYSFS_close(PCXfile);
326 return PCX_ERROR_WRITING;
329 PHYSFS_close(PCXfile);
330 return PCX_ERROR_NONE;
334 // returns number of bytes written into outBuff, 0 if failed
335 int pcx_encode_line(ubyte *inBuff, int inLen, PHYSFS_file *fp)
340 register ubyte runCount; // max single runlength is 63
345 for (srcIndex = 1; srcIndex < inLen; srcIndex++) {
348 runCount++; // it encodes
349 if (runCount == 63) {
350 if (!(i=pcx_encode_byte(last, runCount, fp)))
355 } else { // this != last
357 if (!(i=pcx_encode_byte(last, runCount, fp)))
366 if (runCount) { // finish up
367 if (!(i=pcx_encode_byte(last, runCount, fp)))
374 // subroutine for writing an encoded byte pair
375 // returns count of bytes written, 0 if error
376 int pcx_encode_byte(ubyte byt, ubyte cnt, PHYSFS_file *fid)
379 if ( (cnt==1) && (0xc0 != (0xc0 & byt)) ) {
380 if(EOF == PHYSFSX_putc(fid, (int)byt))
381 return 0; // disk write error (probably full)
384 if(EOF == PHYSFSX_putc(fid, (int)0xC0 | cnt))
385 return 0; // disk write error
386 if(EOF == PHYSFSX_putc(fid, (int)byt))
387 return 0; // disk write error
394 //text for error messges
395 static const char pcx_error_messages[] = {
397 "Error opening file.\0"
398 "Couldn't read PCX header.\0"
399 "Unsupported PCX version.\0"
400 "Error reading data.\0"
401 "Couldn't find palette information.\0"
402 "Error writing data.\0"
406 //function to return pointer to error message
407 const char *pcx_errormsg(int error_number)
409 const char *p = pcx_error_messages;
411 while (error_number--) {
423 // fullscreen loading, 10/14/99 Jan Bobrowski
425 int pcx_read_fullscr(char * filename, ubyte * palette)
429 gr_init_bitmap_data(&bm);
430 pcx_error = pcx_read_bitmap(filename, &bm, BM_LINEAR, palette);
431 if (pcx_error == PCX_ERROR_NONE) {
433 gr_palette_load(palette);
435 gr_bitmap_fullscr(&bm);
437 gr_free_bitmap_data(&bm);