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