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.
30 int pcx_encode_byte(ubyte byt, ubyte cnt, FILE * fid);
31 int pcx_encode_line(ubyte *inBuff, int inLen, FILE * fp);
33 /* PCX Header data type */
45 ubyte ColorMap[16][3];
53 int pcx_read_bitmap( char * filename, grs_bitmap * bmp,int bitmap_type ,ubyte * palette )
57 int i, row, col, count, xsize, ysize;
60 PCXfile = cfopen( filename , "rb" );
62 return PCX_ERROR_OPENING;
64 // read 128 char PCX header
65 if (cfread( &header, sizeof(PCXHeader), 1, PCXfile )!=1) {
67 return PCX_ERROR_NO_HEADER;
70 // Is it a 256 color PCX file?
71 if ((header.Manufacturer != 10)||(header.Encoding != 1)||(header.Nplanes != 1)||(header.BitsPerPixel != 8)||(header.Version != 5)) {
73 return PCX_ERROR_WRONG_VERSION;
76 // Find the size of the image
77 xsize = header.Xmax - header.Xmin + 1;
78 ysize = header.Ymax - header.Ymin + 1;
80 if ( bitmap_type == BM_LINEAR ) {
81 if ( bmp->bm_data == NULL ) {
82 gr_init_bitmap_alloc (bmp, bitmap_type, 0, 0, xsize, ysize, xsize);
86 if ( bmp->bm_type == BM_LINEAR ) {
87 for (row=0; row< ysize ; row++) {
88 pixdata = &bmp->bm_data[bmp->bm_rowsize*row];
89 for (col=0; col< xsize ; ) {
90 if (cfread( &data, 1, 1, PCXfile )!=1 ) {
92 return PCX_ERROR_READING;
94 if ((data & 0xC0) == 0xC0) {
96 if (cfread( &data, 1, 1, PCXfile )!=1 ) {
98 return PCX_ERROR_READING;
100 memset( pixdata, data, count );
110 for (row=0; row< ysize ; row++) {
111 for (col=0; col< xsize ; ) {
112 if (cfread( &data, 1, 1, PCXfile )!=1 ) {
114 return PCX_ERROR_READING;
116 if ((data & 0xC0) == 0xC0) {
118 if (cfread( &data, 1, 1, PCXfile )!=1 ) {
120 return PCX_ERROR_READING;
122 for (i=0;i<count;i++)
123 gr_bm_pixel( bmp, col+i, row, data );
126 gr_bm_pixel( bmp, col, row, data );
133 // Read the extended palette at the end of PCX file
134 if ( palette != NULL ) {
135 // Read in a character which should be 12 to be extended palette file
136 if (cfread( &data, 1, 1, PCXfile )==1) {
138 if (cfread(palette,768, 1, PCXfile)!=1) {
140 return PCX_ERROR_READING;
142 for (i=0; i<768; i++ )
147 return PCX_ERROR_NO_PALETTE;
151 return PCX_ERROR_NONE;
154 int pcx_write_bitmap( char * filename, grs_bitmap * bmp, ubyte * palette )
162 memset( &header, 0, sizeof( PCXHeader ) );
164 header.Manufacturer = 10;
167 header.BitsPerPixel = 8;
169 header.Xmax = bmp->bm_w-1;
170 header.Ymax = bmp->bm_h-1;
171 header.BytesPerLine = bmp->bm_w;
173 PCXfile = fopen( filename , "wb" );
175 return PCX_ERROR_OPENING;
177 if ( fwrite( &header, sizeof( PCXHeader ), 1, PCXfile ) != 1 ) {
179 return PCX_ERROR_WRITING;
182 for (i=0; i<bmp->bm_h; i++ ) {
183 if (!pcx_encode_line( &bmp->bm_data[bmp->bm_rowsize*i], bmp->bm_w, PCXfile )) {
185 return PCX_ERROR_WRITING;
189 // Mark an extended palette
191 if (fwrite( &data, 1, 1, PCXfile )!=1) {
193 return PCX_ERROR_WRITING;
196 // Write the extended palette
197 for (i=0; i<768; i++ )
200 retval = fwrite( palette, 768, 1, PCXfile );
202 for (i=0; i<768; i++ )
207 return PCX_ERROR_WRITING;
211 return PCX_ERROR_NONE;
215 // returns number of bytes written into outBuff, 0 if failed
216 int pcx_encode_line(ubyte *inBuff, int inLen, FILE * fp)
221 register ubyte runCount; // max single runlength is 63
226 for (srcIndex = 1; srcIndex < inLen; srcIndex++) {
229 runCount++; // it encodes
230 if (runCount == 63) {
231 if (!(i=pcx_encode_byte(last, runCount, fp)))
236 } else { // this != last
238 if (!(i=pcx_encode_byte(last, runCount, fp)))
247 if (runCount) { // finish up
248 if (!(i=pcx_encode_byte(last, runCount, fp)))
255 // subroutine for writing an encoded byte pair
256 // returns count of bytes written, 0 if error
257 int pcx_encode_byte(ubyte byt, ubyte cnt, FILE * fid)
260 if ( (cnt==1) && (0xc0 != (0xc0 & byt)) ) {
261 if(EOF == putc((int)byt, fid))
262 return 0; // disk write error (probably full)
265 if(EOF == putc((int)0xC0 | cnt, fid))
266 return 0; // disk write error
267 if(EOF == putc((int)byt, fid))
268 return 0; // disk write error
275 //text for error messges
276 char pcx_error_messages[] = {
278 "Error opening file.\0"
279 "Couldn't read PCX header.\0"
280 "Unsupported PCX version.\0"
281 "Error reading data.\0"
282 "Couldn't find palette information.\0"
283 "Error writing data.\0"
287 //function to return pointer to error message
288 char *pcx_errormsg(int error_number)
290 char *p = pcx_error_messages;
292 while (error_number--) {
303 // fullscreen loading, 10/14/99 Jan Bobrowski
305 int pcx_read_fullscr(char * filename, ubyte * palette)
309 gr_init_bitmap_data(&bm);
310 pcx_error = pcx_read_bitmap(filename, &bm, BM_LINEAR, palette);
311 if (pcx_error == PCX_ERROR_NONE)
313 gr_free_bitmap_data(&bm);