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