]> icculus.org git repositories - btb/d2x.git/blob - 2d/pcx.c
added fullscreen toggle while playing movies
[btb/d2x.git] / 2d / pcx.c
1 /* $Id: pcx.c,v 1.6 2002-08-26 06:41:38 btb Exp $ */
2 /*
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.
13 */
14 /*
15  *
16  * Routines to read/write pcx images.
17  *
18  * Old Log:
19  * Revision 1.6  1995/03/01  15:38:12  john
20  * Better ModeX support.
21  *
22  * Revision 1.5  1995/01/21  17:54:17  john
23  * Added pcx reader for modes other than modex.
24  *
25  * Revision 1.4  1994/12/08  19:03:56  john
26  * Made functions use cfile.
27  *
28  * Revision 1.3  1994/11/29  02:53:24  john
29  * Added error messages; made call be more similiar to iff.
30  *
31  * Revision 1.2  1994/11/28  20:03:50  john
32  * Added PCX functions.
33  *
34  * Revision 1.1  1994/11/28  19:57:56  john
35  * Initial revision
36  *
37  */
38
39 #ifdef HAVE_CONFIG_H
40 #include <conf.h>
41 #endif
42
43 #include <stdlib.h>
44 #include <stdio.h>
45 #include <string.h>
46
47 #include "gr.h"
48 #include "grdef.h"
49 #include "u_mem.h"
50 #include "pcx.h"
51 #include "cfile.h"
52
53 #if defined(POLY_ACC)
54 #include "poly_acc.h"
55 #endif
56
57 int pcx_encode_byte(ubyte byt, ubyte cnt, FILE * fid);
58 int pcx_encode_line(ubyte *inBuff, int inLen, FILE * fp);
59
60 /* PCX Header data type */
61 typedef struct {
62         ubyte   Manufacturer;
63         ubyte   Version;
64         ubyte   Encoding;
65         ubyte   BitsPerPixel;
66         short   Xmin;
67         short   Ymin;
68         short   Xmax;
69         short   Ymax;
70         short   Hdpi;
71         short   Vdpi;
72         ubyte   ColorMap[16][3];
73         ubyte   Reserved;
74         ubyte   Nplanes;
75         short   BytesPerLine;
76         ubyte   filler[60];
77 } __pack__ PCXHeader;
78
79 #define PCXHEADER_SIZE 128
80
81 #ifdef FAST_FILE_IO
82 #define PCXHeader_read_n(ph, n, fp) cfread(ph, sizeof(PCXHeader), n, fp)
83 #else
84 /*
85  * reads n PCXHeader structs from a CFILE
86  */
87 int PCXHeader_read_n(PCXHeader *ph, int n, CFILE *fp)
88 {
89         int i;
90
91         for (i = 0; i < n; i++) {
92                 ph->Manufacturer = cfile_read_byte(fp);
93                 ph->Version = cfile_read_byte(fp);
94                 ph->Encoding = cfile_read_byte(fp);
95                 ph->BitsPerPixel = cfile_read_byte(fp);
96                 ph->Xmin = cfile_read_short(fp);
97                 ph->Ymin = cfile_read_short(fp);
98                 ph->Xmax = cfile_read_short(fp);
99                 ph->Ymax = cfile_read_short(fp);
100                 ph->Hdpi = cfile_read_short(fp);
101                 ph->Vdpi = cfile_read_short(fp);
102                 cfread(&ph->ColorMap, 16*3, 1, fp);
103                 ph->Reserved = cfile_read_byte(fp);
104                 ph->Nplanes = cfile_read_byte(fp);
105                 ph->BytesPerLine = cfile_read_short(fp);
106                 cfread(&ph->filler, 60, 1, fp);
107         }
108         return i;
109 }
110 #endif
111
112 int pcx_get_dimensions( char *filename, int *width, int *height)
113 {
114         CFILE *PCXfile;
115         PCXHeader header;
116
117         PCXfile = cfopen(filename, "rb");
118         if (!PCXfile) return PCX_ERROR_OPENING;
119
120         if (PCXHeader_read_n(&header, 1, PCXfile) != 1) {
121                 cfclose(PCXfile);
122                 return PCX_ERROR_NO_HEADER;
123         }
124         cfclose(PCXfile);
125
126         *width = header.Xmax - header.Xmin+1;
127         *height = header.Ymax - header.Ymin+1;
128
129         return PCX_ERROR_NONE;
130 }
131
132 #ifdef MACINTOSH
133 int pcx_read_bitmap_palette( char *filename, ubyte *palette)
134 {
135         PCXHeader header;
136         CFILE * PCXfile;
137         ubyte data;
138         int i;
139
140         PCXfile = cfopen( filename , "rb" );
141         if ( !PCXfile )
142                 return PCX_ERROR_OPENING;
143
144         // read 128 char PCX header
145         if (PCXHeader_read_n( &header, 1, PCXfile )!=1) {
146                 cfclose( PCXfile );
147                 return PCX_ERROR_NO_HEADER;
148         }
149         // Is it a 256 color PCX file?
150         if ((header.Manufacturer != 10)||(header.Encoding != 1)||(header.Nplanes != 1)||(header.BitsPerPixel != 8)||(header.Version != 5))      {
151                 cfclose( PCXfile );
152                 return PCX_ERROR_WRONG_VERSION;
153         }
154
155         // Read the extended palette at the end of PCX file
156         // Read in a character which should be 12 to be extended palette file
157
158         if (palette != NULL) {
159                 cfseek( PCXfile, -768, SEEK_END );
160                 cfread( palette, 3, 256, PCXfile );
161                 cfseek( PCXfile, PCXHEADER_SIZE, SEEK_SET );
162                 for (i=0; i<768; i++ )
163                         palette[i] >>= 2;
164 #ifdef MACINTOSH
165                 for (i = 0; i < 3; i++) {
166                         data = palette[i];
167                         palette[i] = palette[765+i];
168                         palette[765+i] = data;
169                 }
170 #endif
171         }
172 }
173 #endif
174
175 int pcx_read_bitmap( char * filename, grs_bitmap * bmp,int bitmap_type ,ubyte * palette )
176 {
177         PCXHeader header;
178         CFILE * PCXfile;
179         int i, row, col, count, xsize, ysize;
180         ubyte data, *pixdata;
181 #if defined(POLY_ACC)
182     unsigned char local_pal[768];
183
184     pa_flush();
185 #endif
186
187         PCXfile = cfopen( filename , "rb" );
188         if ( !PCXfile )
189                 return PCX_ERROR_OPENING;
190
191         // read 128 char PCX header
192         if (PCXHeader_read_n( &header, 1, PCXfile )!=1) {
193                 cfclose( PCXfile );
194                 return PCX_ERROR_NO_HEADER;
195         }
196
197         // Is it a 256 color PCX file?
198         if ((header.Manufacturer != 10)||(header.Encoding != 1)||(header.Nplanes != 1)||(header.BitsPerPixel != 8)||(header.Version != 5))      {
199                 cfclose( PCXfile );
200                 return PCX_ERROR_WRONG_VERSION;
201         }
202
203         // Find the size of the image
204         xsize = header.Xmax - header.Xmin + 1;
205         ysize = header.Ymax - header.Ymin + 1;
206
207 #if defined(POLY_ACC)
208     // Read the extended palette at the end of PCX file
209     if(bitmap_type == BM_LINEAR15)      // need palette for conversion from 8bit pcx to 15bit.
210     {
211         cfseek( PCXfile, -768, SEEK_END );
212         cfread( local_pal, 3, 256, PCXfile );
213         cfseek( PCXfile, PCXHEADER_SIZE, SEEK_SET );
214         for (i=0; i<768; i++ )
215             local_pal[i] >>= 2;
216         pa_save_clut();
217         pa_update_clut(local_pal, 0, 256, 0);
218     }
219 #endif
220
221         if ( bitmap_type == BM_LINEAR ) {
222                 if ( bmp->bm_data == NULL )     {
223                         gr_init_bitmap_alloc (bmp, bitmap_type, 0, 0, xsize, ysize, xsize);
224                 }
225         }
226
227         if ( bmp->bm_type == BM_LINEAR )        {
228                 for (row=0; row< ysize ; row++)      {
229                         pixdata = &bmp->bm_data[bmp->bm_rowsize*row];
230                         for (col=0; col< xsize ; )      {
231                                 if (cfread( &data, 1, 1, PCXfile )!=1 ) {
232                                         cfclose( PCXfile );
233                                         return PCX_ERROR_READING;
234                                 }
235                                 if ((data & 0xC0) == 0xC0)     {
236                                         count =  data & 0x3F;
237                                         if (cfread( &data, 1, 1, PCXfile )!=1 ) {
238                                                 cfclose( PCXfile );
239                                                 return PCX_ERROR_READING;
240                                         }
241 #ifdef MACINTOSH
242                                         if (data == 0)
243                                                 data = 255;
244                                         else if (data == 255)
245                                                 data = 0;
246 #endif
247                                         memset( pixdata, data, count );
248                                         pixdata += count;
249                                         col += count;
250                                 } else {
251 #ifdef MACINTOSH
252                                         if (data == 0)
253                                                 data = 255;
254                                         else if (data == 255)
255                                                 data = 0;
256 #endif
257                                         *pixdata++ = data;
258                                         col++;
259                                 }
260                         }
261                 }
262 #if defined(POLY_ACC)
263     } else if( bmp->bm_type == BM_LINEAR15 )    {
264         ushort *pixdata2, pix15;
265         PA_DFX (pa_set_backbuffer_current());
266                   PA_DFX (pa_set_write_mode(0));
267                 for (row=0; row< ysize ; row++)      {
268             pixdata2 = (ushort *)&bmp->bm_data[bmp->bm_rowsize*row];
269                         for (col=0; col< xsize ; )      {
270                                 if (cfread( &data, 1, 1, PCXfile )!=1 ) {
271                                         cfclose( PCXfile );
272                                         return PCX_ERROR_READING;
273                                 }
274                                 if ((data & 0xC0) == 0xC0)     {
275                                         count =  data & 0x3F;
276                                         if (cfread( &data, 1, 1, PCXfile )!=1 ) {
277                                                 cfclose( PCXfile );
278                                                 return PCX_ERROR_READING;
279                                         }
280                     pix15 = pa_clut[data];
281                     for(i = 0; i != count; ++i) pixdata2[i] = pix15;
282                     pixdata2 += count;
283                                         col += count;
284                                 } else {
285                     *pixdata2++ = pa_clut[data];
286                                         col++;
287                                 }
288                         }
289         }
290         pa_restore_clut();
291                   PA_DFX (pa_swap_buffer());
292         PA_DFX (pa_set_frontbuffer_current());
293
294 #endif
295         } else {
296                 for (row=0; row< ysize ; row++)      {
297                         for (col=0; col< xsize ; )      {
298                                 if (cfread( &data, 1, 1, PCXfile )!=1 ) {
299                                         cfclose( PCXfile );
300                                         return PCX_ERROR_READING;
301                                 }
302                                 if ((data & 0xC0) == 0xC0)     {
303                                         count =  data & 0x3F;
304                                         if (cfread( &data, 1, 1, PCXfile )!=1 ) {
305                                                 cfclose( PCXfile );
306                                                 return PCX_ERROR_READING;
307                                         }
308                                         for (i=0;i<count;i++)
309                                                 gr_bm_pixel( bmp, col+i, row, data );
310                                         col += count;
311                                 } else {
312                                         gr_bm_pixel( bmp, col, row, data );
313                                         col++;
314                                 }
315                         }
316                 }
317         }
318
319         // Read the extended palette at the end of PCX file
320         if ( palette != NULL )  {
321                 // Read in a character which should be 12 to be extended palette file
322                 if (cfread( &data, 1, 1, PCXfile )==1)  {
323                         if ( data == 12 )       {
324                                 if (cfread(palette,768, 1, PCXfile)!=1) {
325                                         cfclose( PCXfile );
326                                         return PCX_ERROR_READING;
327                                 }
328                                 for (i=0; i<768; i++ )
329                                         palette[i] >>= 2;
330 #ifdef MACINTOSH
331                                 for (i = 0; i < 3; i++) {
332                                         data = palette[i];
333                                         palette[i] = palette[765+i];
334                                         palette[765+i] = data;
335                                 }
336 #endif
337                         }
338                 } else {
339                         cfclose( PCXfile );
340                         return PCX_ERROR_NO_PALETTE;
341                 }
342         }
343         cfclose(PCXfile);
344         return PCX_ERROR_NONE;
345 }
346
347 int pcx_write_bitmap( char * filename, grs_bitmap * bmp, ubyte * palette )
348 {
349         int retval;
350         int i;
351         ubyte data;
352         PCXHeader header;
353         FILE * PCXfile;
354
355         memset( &header, 0, PCXHEADER_SIZE );
356
357         header.Manufacturer = 10;
358         header.Encoding = 1;
359         header.Nplanes = 1;
360         header.BitsPerPixel = 8;
361         header.Version = 5;
362         header.Xmax = bmp->bm_w-1;
363         header.Ymax = bmp->bm_h-1;
364         header.BytesPerLine = bmp->bm_w;
365
366         PCXfile = fopen( filename , "wb" );
367         if ( !PCXfile )
368                 return PCX_ERROR_OPENING;
369
370         if ( fwrite( &header, PCXHEADER_SIZE, 1, PCXfile ) != 1 )       {
371                 fclose( PCXfile );
372                 return PCX_ERROR_WRITING;
373         }
374
375         for (i=0; i<bmp->bm_h; i++ )    {
376                 if (!pcx_encode_line( &bmp->bm_data[bmp->bm_rowsize*i], bmp->bm_w, PCXfile ))   {
377                         fclose( PCXfile );
378                         return PCX_ERROR_WRITING;
379                 }
380         }
381
382         // Mark an extended palette
383         data = 12;
384         if (fwrite( &data, 1, 1, PCXfile )!=1)  {
385                 fclose( PCXfile );
386                 return PCX_ERROR_WRITING;
387         }
388
389         // Write the extended palette
390         for (i=0; i<768; i++ )
391                 palette[i] <<= 2;
392
393         retval = fwrite( palette, 768, 1, PCXfile );
394
395         for (i=0; i<768; i++ )
396                 palette[i] >>= 2;
397
398         if (retval !=1) {
399                 fclose( PCXfile );
400                 return PCX_ERROR_WRITING;
401         }
402
403         fclose( PCXfile );
404         return PCX_ERROR_NONE;
405
406 }
407
408 // returns number of bytes written into outBuff, 0 if failed
409 int pcx_encode_line(ubyte *inBuff, int inLen, FILE * fp)
410 {
411         ubyte this, last;
412         int srcIndex, i;
413         register int total;
414         register ubyte runCount;        // max single runlength is 63
415         total = 0;
416         last = *(inBuff);
417         runCount = 1;
418
419         for (srcIndex = 1; srcIndex < inLen; srcIndex++) {
420                 this = *(++inBuff);
421                 if (this == last)       {
422                         runCount++;                     // it encodes
423                         if (runCount == 63)     {
424                                 if (!(i=pcx_encode_byte(last, runCount, fp)))
425                                         return(0);
426                                 total += i;
427                                 runCount = 0;
428                         }
429                 } else {        // this != last
430                         if (runCount)   {
431                                 if (!(i=pcx_encode_byte(last, runCount, fp)))
432                                         return(0);
433                                 total += i;
434                         }
435                         last = this;
436                         runCount = 1;
437                 }
438         }
439
440         if (runCount)   {               // finish up
441                 if (!(i=pcx_encode_byte(last, runCount, fp)))
442                         return 0;
443                 return total + i;
444         }
445         return total;
446 }
447
448 // subroutine for writing an encoded byte pair
449 // returns count of bytes written, 0 if error
450 int pcx_encode_byte(ubyte byt, ubyte cnt, FILE * fid)
451 {
452         if (cnt) {
453                 if ( (cnt==1) && (0xc0 != (0xc0 & byt)) )       {
454                         if(EOF == putc((int)byt, fid))
455                                 return 0;       // disk write error (probably full)
456                         return 1;
457                 } else {
458                         if(EOF == putc((int)0xC0 | cnt, fid))
459                                 return 0;       // disk write error
460                         if(EOF == putc((int)byt, fid))
461                                 return 0;       // disk write error
462                         return 2;
463                 }
464         }
465         return 0;
466 }
467
468 //text for error messges
469 char pcx_error_messages[] = {
470         "No error.\0"
471         "Error opening file.\0"
472         "Couldn't read PCX header.\0"
473         "Unsupported PCX version.\0"
474         "Error reading data.\0"
475         "Couldn't find palette information.\0"
476         "Error writing data.\0"
477 };
478
479
480 //function to return pointer to error message
481 char *pcx_errormsg(int error_number)
482 {
483         char *p = pcx_error_messages;
484
485         while (error_number--) {
486
487                 if (!p) return NULL;
488
489                 p += strlen(p)+1;
490
491         }
492
493         return p;
494
495 }
496
497 // fullscreen loading, 10/14/99 Jan Bobrowski
498
499 int pcx_read_fullscr(char * filename, ubyte * palette)
500 {
501         int pcx_error;
502         grs_bitmap bm;
503         gr_init_bitmap_data(&bm);
504         pcx_error = pcx_read_bitmap(filename, &bm, BM_LINEAR, palette);
505         if (pcx_error == PCX_ERROR_NONE)
506                 show_fullscr(&bm);
507         gr_free_bitmap_data(&bm);
508         return pcx_error;
509 }