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, FILE * fid);
34 int pcx_encode_line(ubyte *inBuff, int inLen, FILE * fp);
36 /* PCX Header data type */
48 ubyte ColorMap[16][3];
56 int pcx_read_bitmap( char * filename, grs_bitmap * bmp,int bitmap_type ,ubyte * palette )
60 int i, row, col, count, xsize, ysize;
63 PCXfile = cfopen( filename , "rb" );
65 return PCX_ERROR_OPENING;
67 // read 128 char PCX header
68 if (cfread( &header, sizeof(PCXHeader), 1, PCXfile )!=1) {
70 return PCX_ERROR_NO_HEADER;
73 // Is it a 256 color PCX file?
74 if ((header.Manufacturer != 10)||(header.Encoding != 1)||(header.Nplanes != 1)||(header.BitsPerPixel != 8)||(header.Version != 5)) {
76 return PCX_ERROR_WRONG_VERSION;
79 // Find the size of the image
80 xsize = header.Xmax - header.Xmin + 1;
81 ysize = header.Ymax - header.Ymin + 1;
83 if ( bitmap_type == BM_LINEAR ) {
84 if ( bmp->bm_data == NULL ) {
85 gr_init_bitmap_alloc (bmp, bitmap_type, 0, 0, xsize, ysize, xsize);
89 if ( bmp->bm_type == BM_LINEAR ) {
90 for (row=0; row< ysize ; row++) {
91 pixdata = &bmp->bm_data[bmp->bm_rowsize*row];
92 for (col=0; col< xsize ; ) {
93 if (cfread( &data, 1, 1, PCXfile )!=1 ) {
95 return PCX_ERROR_READING;
97 if ((data & 0xC0) == 0xC0) {
99 if (cfread( &data, 1, 1, PCXfile )!=1 ) {
101 return PCX_ERROR_READING;
103 memset( pixdata, data, count );
113 for (row=0; row< ysize ; row++) {
114 for (col=0; col< xsize ; ) {
115 if (cfread( &data, 1, 1, PCXfile )!=1 ) {
117 return PCX_ERROR_READING;
119 if ((data & 0xC0) == 0xC0) {
121 if (cfread( &data, 1, 1, PCXfile )!=1 ) {
123 return PCX_ERROR_READING;
125 for (i=0;i<count;i++)
126 gr_bm_pixel( bmp, col+i, row, data );
129 gr_bm_pixel( bmp, col, row, data );
136 // Read the extended palette at the end of PCX file
137 if ( palette != NULL ) {
138 // Read in a character which should be 12 to be extended palette file
139 if (cfread( &data, 1, 1, PCXfile )==1) {
141 if (cfread(palette,768, 1, PCXfile)!=1) {
143 return PCX_ERROR_READING;
145 for (i=0; i<768; i++ )
150 return PCX_ERROR_NO_PALETTE;
154 return PCX_ERROR_NONE;
157 int pcx_write_bitmap( char * filename, grs_bitmap * bmp, ubyte * palette )
165 memset( &header, 0, sizeof( PCXHeader ) );
167 header.Manufacturer = 10;
170 header.BitsPerPixel = 8;
172 header.Xmax = bmp->bm_w-1;
173 header.Ymax = bmp->bm_h-1;
174 header.BytesPerLine = bmp->bm_w;
176 PCXfile = fopen( filename , "wb" );
178 return PCX_ERROR_OPENING;
180 if ( fwrite( &header, sizeof( PCXHeader ), 1, PCXfile ) != 1 ) {
182 return PCX_ERROR_WRITING;
185 for (i=0; i<bmp->bm_h; i++ ) {
186 if (!pcx_encode_line( &bmp->bm_data[bmp->bm_rowsize*i], bmp->bm_w, PCXfile )) {
188 return PCX_ERROR_WRITING;
192 // Mark an extended palette
194 if (fwrite( &data, 1, 1, PCXfile )!=1) {
196 return PCX_ERROR_WRITING;
199 // Write the extended palette
200 for (i=0; i<768; i++ )
203 retval = fwrite( palette, 768, 1, PCXfile );
205 for (i=0; i<768; i++ )
210 return PCX_ERROR_WRITING;
214 return PCX_ERROR_NONE;
218 // returns number of bytes written into outBuff, 0 if failed
219 int pcx_encode_line(ubyte *inBuff, int inLen, FILE * fp)
224 register ubyte runCount; // max single runlength is 63
229 for (srcIndex = 1; srcIndex < inLen; srcIndex++) {
232 runCount++; // it encodes
233 if (runCount == 63) {
234 if (!(i=pcx_encode_byte(last, runCount, fp)))
239 } else { // this != last
241 if (!(i=pcx_encode_byte(last, runCount, fp)))
250 if (runCount) { // finish up
251 if (!(i=pcx_encode_byte(last, runCount, fp)))
258 // subroutine for writing an encoded byte pair
259 // returns count of bytes written, 0 if error
260 int pcx_encode_byte(ubyte byt, ubyte cnt, FILE * fid)
263 if ( (cnt==1) && (0xc0 != (0xc0 & byt)) ) {
264 if(EOF == putc((int)byt, fid))
265 return 0; // disk write error (probably full)
268 if(EOF == putc((int)0xC0 | cnt, fid))
269 return 0; // disk write error
270 if(EOF == putc((int)byt, fid))
271 return 0; // disk write error
278 //text for error messges
279 char pcx_error_messages[] = {
281 "Error opening file.\0"
282 "Couldn't read PCX header.\0"
283 "Unsupported PCX version.\0"
284 "Error reading data.\0"
285 "Couldn't find palette information.\0"
286 "Error writing data.\0"
290 //function to return pointer to error message
291 char *pcx_errormsg(int error_number)
293 char *p = pcx_error_messages;
295 while (error_number--) {
306 // fullscreen loading, 10/14/99 Jan Bobrowski
308 int pcx_read_fullscr(char * filename, ubyte * palette)
312 gr_init_bitmap_data(&bm);
313 pcx_error = pcx_read_bitmap(filename, &bm, BM_LINEAR, palette);
314 if (pcx_error == PCX_ERROR_NONE)
316 gr_free_bitmap_data(&bm);