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.
39 int pcx_encode_byte(ubyte byt, ubyte cnt, PHYSFS_file *fid);
40 int pcx_encode_line(ubyte *inBuff, int inLen, PHYSFS_file *fp);
42 /* PCX Header data type */
54 ubyte ColorMap[16][3];
61 #define PCXHEADER_SIZE 128
64 #define PCXHeader_read_n(ph, n, fp) cfread(ph, sizeof(PCXHeader), n, fp)
67 * reads n PCXHeader structs from a CFILE
69 int PCXHeader_read_n(PCXHeader *ph, int n, CFILE *fp)
73 for (i = 0; i < n; i++) {
74 ph->Manufacturer = cfile_read_byte(fp);
75 ph->Version = cfile_read_byte(fp);
76 ph->Encoding = cfile_read_byte(fp);
77 ph->BitsPerPixel = cfile_read_byte(fp);
78 ph->Xmin = cfile_read_short(fp);
79 ph->Ymin = cfile_read_short(fp);
80 ph->Xmax = cfile_read_short(fp);
81 ph->Ymax = cfile_read_short(fp);
82 ph->Hdpi = cfile_read_short(fp);
83 ph->Vdpi = cfile_read_short(fp);
84 cfread(&ph->ColorMap, 16*3, 1, fp);
85 ph->Reserved = cfile_read_byte(fp);
86 ph->Nplanes = cfile_read_byte(fp);
87 ph->BytesPerLine = cfile_read_short(fp);
88 cfread(&ph->filler, 60, 1, fp);
94 int pcx_get_dimensions( char *filename, int *width, int *height)
99 PCXfile = cfopen(filename, "rb");
100 if (!PCXfile) return PCX_ERROR_OPENING;
102 if (PCXHeader_read_n(&header, 1, PCXfile) != 1) {
104 return PCX_ERROR_NO_HEADER;
108 *width = header.Xmax - header.Xmin+1;
109 *height = header.Ymax - header.Ymin+1;
111 return PCX_ERROR_NONE;
115 int pcx_read_bitmap_palette( char *filename, ubyte *palette)
122 PCXfile = cfopen( filename , "rb" );
124 return PCX_ERROR_OPENING;
126 // read 128 char PCX header
127 if (PCXHeader_read_n( &header, 1, PCXfile )!=1) {
129 return PCX_ERROR_NO_HEADER;
131 // Is it a 256 color PCX file?
132 if ((header.Manufacturer != 10)||(header.Encoding != 1)||(header.Nplanes != 1)||(header.BitsPerPixel != 8)||(header.Version != 5)) {
134 return PCX_ERROR_WRONG_VERSION;
137 // Read the extended palette at the end of PCX file
138 // Read in a character which should be 12 to be extended palette file
140 if (palette != NULL) {
141 cfseek( PCXfile, -768, SEEK_END );
142 cfread( palette, 3, 256, PCXfile );
143 cfseek( PCXfile, PCXHEADER_SIZE, SEEK_SET );
144 for (i=0; i<768; i++ )
147 for (i = 0; i < 3; i++) {
149 palette[i] = palette[765+i];
150 palette[765+i] = data;
157 int pcx_read_bitmap( char * filename, grs_bitmap * bmp,int bitmap_type ,ubyte * palette )
161 int i, row, col, count, xsize, ysize;
162 ubyte data, *pixdata;
164 PCXfile = cfopen( filename , "rb" );
166 return PCX_ERROR_OPENING;
168 // read 128 char PCX header
169 if (PCXHeader_read_n( &header, 1, PCXfile )!=1) {
171 return PCX_ERROR_NO_HEADER;
174 // Is it a 256 color PCX file?
175 if ((header.Manufacturer != 10)||(header.Encoding != 1)||(header.Nplanes != 1)||(header.BitsPerPixel != 8)||(header.Version != 5)) {
177 return PCX_ERROR_WRONG_VERSION;
180 // Find the size of the image
181 xsize = header.Xmax - header.Xmin + 1;
182 ysize = header.Ymax - header.Ymin + 1;
184 if ( bitmap_type == BM_LINEAR ) {
185 if ( bmp->bm_data == NULL ) {
186 gr_init_bitmap_alloc (bmp, bitmap_type, 0, 0, xsize, ysize, xsize);
190 if ( bmp->bm_type == BM_LINEAR ) {
191 for (row=0; row< ysize ; row++) {
192 pixdata = &bmp->bm_data[bmp->bm_rowsize*row];
193 for (col=0; col< xsize ; ) {
194 if (cfread( &data, 1, 1, PCXfile )!=1 ) {
196 return PCX_ERROR_READING;
198 if ((data & 0xC0) == 0xC0) {
200 if (cfread( &data, 1, 1, PCXfile )!=1 ) {
202 return PCX_ERROR_READING;
207 else if (data == 255)
210 memset( pixdata, data, count );
217 else if (data == 255)
226 for (row=0; row< ysize ; row++) {
227 for (col=0; col< xsize ; ) {
228 if (cfread( &data, 1, 1, PCXfile )!=1 ) {
230 return PCX_ERROR_READING;
232 if ((data & 0xC0) == 0xC0) {
234 if (cfread( &data, 1, 1, PCXfile )!=1 ) {
236 return PCX_ERROR_READING;
238 for (i=0;i<count;i++)
239 gr_bm_pixel( bmp, col+i, row, data );
242 gr_bm_pixel( bmp, col, row, data );
249 // Read the extended palette at the end of PCX file
250 if ( palette != NULL ) {
251 // Read in a character which should be 12 to be extended palette file
252 if (cfread( &data, 1, 1, PCXfile )==1) {
254 if (cfread(palette,768, 1, PCXfile)!=1) {
256 return PCX_ERROR_READING;
258 for (i=0; i<768; i++ )
261 for (i = 0; i < 3; i++) {
263 palette[i] = palette[765+i];
264 palette[765+i] = data;
270 return PCX_ERROR_NO_PALETTE;
274 return PCX_ERROR_NONE;
277 int pcx_write_bitmap( char * filename, grs_bitmap * bmp, ubyte * palette )
283 PHYSFS_file *PCXfile;
285 memset( &header, 0, PCXHEADER_SIZE );
287 header.Manufacturer = 10;
290 header.BitsPerPixel = 8;
292 header.Xmax = bmp->bm_w-1;
293 header.Ymax = bmp->bm_h-1;
294 header.BytesPerLine = bmp->bm_w;
296 PCXfile = PHYSFSX_openWriteBuffered(filename);
298 return PCX_ERROR_OPENING;
300 if (PHYSFS_write(PCXfile, &header, PCXHEADER_SIZE, 1) != 1)
302 PHYSFS_close(PCXfile);
303 return PCX_ERROR_WRITING;
306 for (i=0; i<bmp->bm_h; i++ ) {
307 if (!pcx_encode_line( &bmp->bm_data[bmp->bm_rowsize*i], bmp->bm_w, PCXfile )) {
308 PHYSFS_close(PCXfile);
309 return PCX_ERROR_WRITING;
313 // Mark an extended palette
315 if (PHYSFS_write(PCXfile, &data, 1, 1) != 1)
317 PHYSFS_close(PCXfile);
318 return PCX_ERROR_WRITING;
321 // Write the extended palette
322 for (i=0; i<768; i++ )
325 retval = PHYSFS_write(PCXfile, palette, 768, 1);
327 for (i=0; i<768; i++ )
331 PHYSFS_close(PCXfile);
332 return PCX_ERROR_WRITING;
335 PHYSFS_close(PCXfile);
336 return PCX_ERROR_NONE;
340 // returns number of bytes written into outBuff, 0 if failed
341 int pcx_encode_line(ubyte *inBuff, int inLen, PHYSFS_file *fp)
346 register ubyte runCount; // max single runlength is 63
351 for (srcIndex = 1; srcIndex < inLen; srcIndex++) {
354 runCount++; // it encodes
355 if (runCount == 63) {
356 if (!(i=pcx_encode_byte(last, runCount, fp)))
361 } else { // this != last
363 if (!(i=pcx_encode_byte(last, runCount, fp)))
372 if (runCount) { // finish up
373 if (!(i=pcx_encode_byte(last, runCount, fp)))
380 // subroutine for writing an encoded byte pair
381 // returns count of bytes written, 0 if error
382 int pcx_encode_byte(ubyte byt, ubyte cnt, PHYSFS_file *fid)
385 if ( (cnt==1) && (0xc0 != (0xc0 & byt)) ) {
386 if(EOF == PHYSFSX_putc(fid, (int)byt))
387 return 0; // disk write error (probably full)
390 if(EOF == PHYSFSX_putc(fid, (int)0xC0 | cnt))
391 return 0; // disk write error
392 if(EOF == PHYSFSX_putc(fid, (int)byt))
393 return 0; // disk write error
400 //text for error messges
401 char pcx_error_messages[] = {
403 "Error opening file.\0"
404 "Couldn't read PCX header.\0"
405 "Unsupported PCX version.\0"
406 "Error reading data.\0"
407 "Couldn't find palette information.\0"
408 "Error writing data.\0"
412 //function to return pointer to error message
413 char *pcx_errormsg(int error_number)
415 char *p = pcx_error_messages;
417 while (error_number--) {
429 // fullscreen loading, 10/14/99 Jan Bobrowski
431 int pcx_read_fullscr(char * filename, ubyte * palette)
435 gr_init_bitmap_data(&bm);
436 pcx_error = pcx_read_bitmap(filename, &bm, BM_LINEAR, palette);
437 if (pcx_error == PCX_ERROR_NONE) {
439 gr_palette_load(palette);
443 gr_free_bitmap_data(&bm);