]> icculus.org git repositories - btb/d2x.git/blob - 2d/pcx.c
remove rcs tags
[btb/d2x.git] / 2d / pcx.c
1 /*
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.
12 */
13 /*
14  *
15  * Routines to read/write pcx images.
16  *
17  */
18
19 #ifdef HAVE_CONFIG_H
20 #include <conf.h>
21 #endif
22
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26
27 #include "gr.h"
28 #include "grdef.h"
29 #include "u_mem.h"
30 #include "pcx.h"
31 #include "cfile.h"
32
33 #ifdef OGL
34 #include "palette.h"
35 #endif
36
37 #include "physfsx.h"
38
39 int pcx_encode_byte(ubyte byt, ubyte cnt, PHYSFS_file *fid);
40 int pcx_encode_line(ubyte *inBuff, int inLen, PHYSFS_file *fp);
41
42 /* PCX Header data type */
43 typedef struct {
44         ubyte   Manufacturer;
45         ubyte   Version;
46         ubyte   Encoding;
47         ubyte   BitsPerPixel;
48         short   Xmin;
49         short   Ymin;
50         short   Xmax;
51         short   Ymax;
52         short   Hdpi;
53         short   Vdpi;
54         ubyte   ColorMap[16][3];
55         ubyte   Reserved;
56         ubyte   Nplanes;
57         short   BytesPerLine;
58         ubyte   filler[60];
59 } __pack__ PCXHeader;
60
61 #define PCXHEADER_SIZE 128
62
63 #ifdef FAST_FILE_IO
64 #define PCXHeader_read_n(ph, n, fp) cfread(ph, sizeof(PCXHeader), n, fp)
65 #else
66 /*
67  * reads n PCXHeader structs from a CFILE
68  */
69 int PCXHeader_read_n(PCXHeader *ph, int n, CFILE *fp)
70 {
71         int i;
72
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);
89         }
90         return i;
91 }
92 #endif
93
94 int pcx_get_dimensions( char *filename, int *width, int *height)
95 {
96         CFILE *PCXfile;
97         PCXHeader header;
98
99         PCXfile = cfopen(filename, "rb");
100         if (!PCXfile) return PCX_ERROR_OPENING;
101
102         if (PCXHeader_read_n(&header, 1, PCXfile) != 1) {
103                 cfclose(PCXfile);
104                 return PCX_ERROR_NO_HEADER;
105         }
106         cfclose(PCXfile);
107
108         *width = header.Xmax - header.Xmin+1;
109         *height = header.Ymax - header.Ymin+1;
110
111         return PCX_ERROR_NONE;
112 }
113
114 #ifdef MACINTOSH
115 int pcx_read_bitmap_palette( char *filename, ubyte *palette)
116 {
117         PCXHeader header;
118         CFILE * PCXfile;
119         ubyte data;
120         int i;
121
122         PCXfile = cfopen( filename , "rb" );
123         if ( !PCXfile )
124                 return PCX_ERROR_OPENING;
125
126         // read 128 char PCX header
127         if (PCXHeader_read_n( &header, 1, PCXfile )!=1) {
128                 cfclose( PCXfile );
129                 return PCX_ERROR_NO_HEADER;
130         }
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))      {
133                 cfclose( PCXfile );
134                 return PCX_ERROR_WRONG_VERSION;
135         }
136
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
139
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++ )
145                         palette[i] >>= 2;
146 #ifdef MACINTOSH
147                 for (i = 0; i < 3; i++) {
148                         data = palette[i];
149                         palette[i] = palette[765+i];
150                         palette[765+i] = data;
151                 }
152 #endif
153         }
154 }
155 #endif
156
157 int pcx_read_bitmap( char * filename, grs_bitmap * bmp,int bitmap_type ,ubyte * palette )
158 {
159         PCXHeader header;
160         CFILE * PCXfile;
161         int i, row, col, count, xsize, ysize;
162         ubyte data, *pixdata;
163
164         PCXfile = cfopen( filename , "rb" );
165         if ( !PCXfile )
166                 return PCX_ERROR_OPENING;
167
168         // read 128 char PCX header
169         if (PCXHeader_read_n( &header, 1, PCXfile )!=1) {
170                 cfclose( PCXfile );
171                 return PCX_ERROR_NO_HEADER;
172         }
173
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))      {
176                 cfclose( PCXfile );
177                 return PCX_ERROR_WRONG_VERSION;
178         }
179
180         // Find the size of the image
181         xsize = header.Xmax - header.Xmin + 1;
182         ysize = header.Ymax - header.Ymin + 1;
183
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);
187                 }
188         }
189
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 ) {
195                                         cfclose( PCXfile );
196                                         return PCX_ERROR_READING;
197                                 }
198                                 if ((data & 0xC0) == 0xC0)     {
199                                         count =  data & 0x3F;
200                                         if (cfread( &data, 1, 1, PCXfile )!=1 ) {
201                                                 cfclose( PCXfile );
202                                                 return PCX_ERROR_READING;
203                                         }
204 #ifdef MACINTOSH
205                                         if (data == 0)
206                                                 data = 255;
207                                         else if (data == 255)
208                                                 data = 0;
209 #endif
210                                         memset( pixdata, data, count );
211                                         pixdata += count;
212                                         col += count;
213                                 } else {
214 #ifdef MACINTOSH
215                                         if (data == 0)
216                                                 data = 255;
217                                         else if (data == 255)
218                                                 data = 0;
219 #endif
220                                         *pixdata++ = data;
221                                         col++;
222                                 }
223                         }
224                 }
225         } else {
226                 for (row=0; row< ysize ; row++)      {
227                         for (col=0; col< xsize ; )      {
228                                 if (cfread( &data, 1, 1, PCXfile )!=1 ) {
229                                         cfclose( PCXfile );
230                                         return PCX_ERROR_READING;
231                                 }
232                                 if ((data & 0xC0) == 0xC0)     {
233                                         count =  data & 0x3F;
234                                         if (cfread( &data, 1, 1, PCXfile )!=1 ) {
235                                                 cfclose( PCXfile );
236                                                 return PCX_ERROR_READING;
237                                         }
238                                         for (i=0;i<count;i++)
239                                                 gr_bm_pixel( bmp, col+i, row, data );
240                                         col += count;
241                                 } else {
242                                         gr_bm_pixel( bmp, col, row, data );
243                                         col++;
244                                 }
245                         }
246                 }
247         }
248
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)  {
253                         if ( data == 12 )       {
254                                 if (cfread(palette,768, 1, PCXfile)!=1) {
255                                         cfclose( PCXfile );
256                                         return PCX_ERROR_READING;
257                                 }
258                                 for (i=0; i<768; i++ )
259                                         palette[i] >>= 2;
260 #ifdef MACINTOSH
261                                 for (i = 0; i < 3; i++) {
262                                         data = palette[i];
263                                         palette[i] = palette[765+i];
264                                         palette[765+i] = data;
265                                 }
266 #endif
267                         }
268                 } else {
269                         cfclose( PCXfile );
270                         return PCX_ERROR_NO_PALETTE;
271                 }
272         }
273         cfclose(PCXfile);
274         return PCX_ERROR_NONE;
275 }
276
277 int pcx_write_bitmap( char * filename, grs_bitmap * bmp, ubyte * palette )
278 {
279         int retval;
280         int i;
281         ubyte data;
282         PCXHeader header;
283         PHYSFS_file *PCXfile;
284
285         memset( &header, 0, PCXHEADER_SIZE );
286
287         header.Manufacturer = 10;
288         header.Encoding = 1;
289         header.Nplanes = 1;
290         header.BitsPerPixel = 8;
291         header.Version = 5;
292         header.Xmax = bmp->bm_w-1;
293         header.Ymax = bmp->bm_h-1;
294         header.BytesPerLine = bmp->bm_w;
295
296         PCXfile = PHYSFSX_openWriteBuffered(filename);
297         if ( !PCXfile )
298                 return PCX_ERROR_OPENING;
299
300         if (PHYSFS_write(PCXfile, &header, PCXHEADER_SIZE, 1) != 1)
301         {
302                 PHYSFS_close(PCXfile);
303                 return PCX_ERROR_WRITING;
304         }
305
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;
310                 }
311         }
312
313         // Mark an extended palette
314         data = 12;
315         if (PHYSFS_write(PCXfile, &data, 1, 1) != 1)
316         {
317                 PHYSFS_close(PCXfile);
318                 return PCX_ERROR_WRITING;
319         }
320
321         // Write the extended palette
322         for (i=0; i<768; i++ )
323                 palette[i] <<= 2;
324
325         retval = PHYSFS_write(PCXfile, palette, 768, 1);
326
327         for (i=0; i<768; i++ )
328                 palette[i] >>= 2;
329
330         if (retval !=1) {
331                 PHYSFS_close(PCXfile);
332                 return PCX_ERROR_WRITING;
333         }
334
335         PHYSFS_close(PCXfile);
336         return PCX_ERROR_NONE;
337
338 }
339
340 // returns number of bytes written into outBuff, 0 if failed
341 int pcx_encode_line(ubyte *inBuff, int inLen, PHYSFS_file *fp)
342 {
343         ubyte this, last;
344         int srcIndex, i;
345         register int total;
346         register ubyte runCount;        // max single runlength is 63
347         total = 0;
348         last = *(inBuff);
349         runCount = 1;
350
351         for (srcIndex = 1; srcIndex < inLen; srcIndex++) {
352                 this = *(++inBuff);
353                 if (this == last)       {
354                         runCount++;                     // it encodes
355                         if (runCount == 63)     {
356                                 if (!(i=pcx_encode_byte(last, runCount, fp)))
357                                         return(0);
358                                 total += i;
359                                 runCount = 0;
360                         }
361                 } else {        // this != last
362                         if (runCount)   {
363                                 if (!(i=pcx_encode_byte(last, runCount, fp)))
364                                         return(0);
365                                 total += i;
366                         }
367                         last = this;
368                         runCount = 1;
369                 }
370         }
371
372         if (runCount)   {               // finish up
373                 if (!(i=pcx_encode_byte(last, runCount, fp)))
374                         return 0;
375                 return total + i;
376         }
377         return total;
378 }
379
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)
383 {
384         if (cnt) {
385                 if ( (cnt==1) && (0xc0 != (0xc0 & byt)) )       {
386                         if(EOF == PHYSFSX_putc(fid, (int)byt))
387                                 return 0;       // disk write error (probably full)
388                         return 1;
389                 } else {
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
394                         return 2;
395                 }
396         }
397         return 0;
398 }
399
400 //text for error messges
401 char pcx_error_messages[] = {
402         "No error.\0"
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"
409 };
410
411
412 //function to return pointer to error message
413 char *pcx_errormsg(int error_number)
414 {
415         char *p = pcx_error_messages;
416
417         while (error_number--) {
418
419                 if (!p) return NULL;
420
421                 p += strlen(p)+1;
422
423         }
424
425         return p;
426
427 }
428
429 // fullscreen loading, 10/14/99 Jan Bobrowski
430
431 int pcx_read_fullscr(char * filename, ubyte * palette)
432 {
433         int pcx_error;
434         grs_bitmap bm;
435         gr_init_bitmap_data(&bm);
436         pcx_error = pcx_read_bitmap(filename, &bm, BM_LINEAR, palette);
437         if (pcx_error == PCX_ERROR_NONE) {
438 #ifdef OGL
439                 gr_palette_load(palette);
440 #endif
441                 show_fullscr(&bm);
442         }
443         gr_free_bitmap_data(&bm);
444         return pcx_error;
445 }