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