]> icculus.org git repositories - btb/d2x.git/blob - 2d/pcx.c
stuff
[btb/d2x.git] / 2d / pcx.c
1 /* $Id: pcx.c,v 1.5 2002-08-06 04:49:43 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  */
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 int pcx_encode_byte(ubyte byt, ubyte cnt, FILE * fid);
35 int pcx_encode_line(ubyte *inBuff, int inLen, FILE * fp);
36
37 int pcx_read_bitmap( char * filename, grs_bitmap * bmp,int bitmap_type ,ubyte * palette )
38 {
39         PCXHeader header;
40         CFILE * PCXfile;
41         int i, row, col, count, xsize, ysize;
42         ubyte data, *pixdata;
43
44         PCXfile = cfopen( filename , "rb" );
45         if ( !PCXfile )
46                 return PCX_ERROR_OPENING;
47
48         // read 128 char PCX header
49         PCXHeader_read(&header, PCXfile);
50 #if 0
51         if (cfread( &header, sizeof(PCXHeader), 1, PCXfile )!=1)        {
52                 cfclose( PCXfile );
53                 return PCX_ERROR_NO_HEADER;
54         }
55 #endif
56
57         // Is it a 256 color PCX file?
58         if ((header.Manufacturer != 10)||(header.Encoding != 1)||(header.Nplanes != 1)||(header.BitsPerPixel != 8)||(header.Version != 5))      {
59                 cfclose( PCXfile );
60                 return PCX_ERROR_WRONG_VERSION;
61         }
62
63         // Find the size of the image
64         xsize = header.Xmax - header.Xmin + 1;
65         ysize = header.Ymax - header.Ymin + 1;
66
67         if ( bitmap_type == BM_LINEAR ) {
68                 if ( bmp->bm_data == NULL )     {
69                         gr_init_bitmap_alloc (bmp, bitmap_type, 0, 0, xsize, ysize, xsize);
70                 }
71         }
72
73         if ( bmp->bm_type == BM_LINEAR )        {
74                 for (row=0; row< ysize ; row++)      {
75                         pixdata = &bmp->bm_data[bmp->bm_rowsize*row];
76                         for (col=0; col< xsize ; )      {
77                                 if (cfread( &data, 1, 1, PCXfile )!=1 ) {
78                                         cfclose( PCXfile );
79                                         return PCX_ERROR_READING;
80                                 }
81                                 if ((data & 0xC0) == 0xC0)     {
82                                         count =  data & 0x3F;
83                                         if (cfread( &data, 1, 1, PCXfile )!=1 ) {
84                                                 cfclose( PCXfile );
85                                                 return PCX_ERROR_READING;
86                                         }
87                                         memset( pixdata, data, count );
88                                         pixdata += count;
89                                         col += count;
90                                 } else {
91                                         *pixdata++ = data;
92                                         col++;
93                                 }
94                         }
95                 }
96         } else {
97                 for (row=0; row< ysize ; row++)      {
98                         for (col=0; col< xsize ; )      {
99                                 if (cfread( &data, 1, 1, PCXfile )!=1 ) {
100                                         cfclose( PCXfile );
101                                         return PCX_ERROR_READING;
102                                 }
103                                 if ((data & 0xC0) == 0xC0)     {
104                                         count =  data & 0x3F;
105                                         if (cfread( &data, 1, 1, PCXfile )!=1 ) {
106                                                 cfclose( PCXfile );
107                                                 return PCX_ERROR_READING;
108                                         }
109                                         for (i=0;i<count;i++)
110                                                 gr_bm_pixel( bmp, col+i, row, data );
111                                         col += count;
112                                 } else {
113                                         gr_bm_pixel( bmp, col, row, data );
114                                         col++;
115                                 }
116                         }
117                 }
118         }
119
120         // Read the extended palette at the end of PCX file
121         if ( palette != NULL )  {
122                 // Read in a character which should be 12 to be extended palette file
123                 if (cfread( &data, 1, 1, PCXfile )==1)  {
124                         if ( data == 12 )       {
125                                 if (cfread(palette,768, 1, PCXfile)!=1) {
126                                         cfclose( PCXfile );
127                                         return PCX_ERROR_READING;
128                                 }
129                                 for (i=0; i<768; i++ )
130                                         palette[i] >>= 2;
131                         }
132                 } else {
133                         cfclose( PCXfile );
134                         return PCX_ERROR_NO_PALETTE;
135                 }
136         }
137         cfclose(PCXfile);
138         return PCX_ERROR_NONE;
139 }
140
141 int pcx_write_bitmap( char * filename, grs_bitmap * bmp, ubyte * palette )
142 {
143         int retval;
144         int i;
145         ubyte data;
146         PCXHeader header;
147         FILE * PCXfile;
148
149         memset( &header, 0, sizeof( PCXHeader ) );
150
151         header.Manufacturer = 10;
152         header.Encoding = 1;
153         header.Nplanes = 1;
154         header.BitsPerPixel = 8;
155         header.Version = 5;
156         header.Xmax = bmp->bm_w-1;
157         header.Ymax = bmp->bm_h-1;
158         header.BytesPerLine = bmp->bm_w;
159
160         PCXfile = fopen( filename , "wb" );
161         if ( !PCXfile )
162                 return PCX_ERROR_OPENING;
163
164         if ( fwrite( &header, sizeof( PCXHeader ), 1, PCXfile ) != 1 )  {
165                 fclose( PCXfile );
166                 return PCX_ERROR_WRITING;
167         }
168
169         for (i=0; i<bmp->bm_h; i++ )    {
170                 if (!pcx_encode_line( &bmp->bm_data[bmp->bm_rowsize*i], bmp->bm_w, PCXfile ))   {
171                         fclose( PCXfile );
172                         return PCX_ERROR_WRITING;
173                 }
174         }
175
176         // Mark an extended palette
177         data = 12;
178         if (fwrite( &data, 1, 1, PCXfile )!=1)  {
179                 fclose( PCXfile );
180                 return PCX_ERROR_WRITING;
181         }
182
183         // Write the extended palette
184         for (i=0; i<768; i++ )
185                 palette[i] <<= 2;
186
187         retval = fwrite( palette, 768, 1, PCXfile );
188
189         for (i=0; i<768; i++ )
190                 palette[i] >>= 2;
191
192         if (retval !=1) {
193                 fclose( PCXfile );
194                 return PCX_ERROR_WRITING;
195         }
196
197         fclose( PCXfile );
198         return PCX_ERROR_NONE;
199
200 }
201
202 // returns number of bytes written into outBuff, 0 if failed
203 int pcx_encode_line(ubyte *inBuff, int inLen, FILE * fp)
204 {
205         ubyte this, last;
206         int srcIndex, i;
207         register int total;
208         register ubyte runCount;        // max single runlength is 63
209         total = 0;
210         last = *(inBuff);
211         runCount = 1;
212
213         for (srcIndex = 1; srcIndex < inLen; srcIndex++) {
214                 this = *(++inBuff);
215                 if (this == last)       {
216                         runCount++;                     // it encodes
217                         if (runCount == 63)     {
218                                 if (!(i=pcx_encode_byte(last, runCount, fp)))
219                                         return(0);
220                                 total += i;
221                                 runCount = 0;
222                         }
223                 } else {        // this != last
224                         if (runCount)   {
225                                 if (!(i=pcx_encode_byte(last, runCount, fp)))
226                                         return(0);
227                                 total += i;
228                         }
229                         last = this;
230                         runCount = 1;
231                 }
232         }
233
234         if (runCount)   {               // finish up
235                 if (!(i=pcx_encode_byte(last, runCount, fp)))
236                         return 0;
237                 return total + i;
238         }
239         return total;
240 }
241
242 // subroutine for writing an encoded byte pair
243 // returns count of bytes written, 0 if error
244 int pcx_encode_byte(ubyte byt, ubyte cnt, FILE * fid)
245 {
246         if (cnt) {
247                 if ( (cnt==1) && (0xc0 != (0xc0 & byt)) )       {
248                         if(EOF == putc((int)byt, fid))
249                                 return 0;       // disk write error (probably full)
250                         return 1;
251                 } else {
252                         if(EOF == putc((int)0xC0 | cnt, fid))
253                                 return 0;       // disk write error
254                         if(EOF == putc((int)byt, fid))
255                                 return 0;       // disk write error
256                         return 2;
257                 }
258         }
259         return 0;
260 }
261
262 //text for error messges
263 char pcx_error_messages[] = {
264         "No error.\0"
265         "Error opening file.\0"
266         "Couldn't read PCX header.\0"
267         "Unsupported PCX version.\0"
268         "Error reading data.\0"
269         "Couldn't find palette information.\0"
270         "Error writing data.\0"
271 };
272
273
274 //function to return pointer to error message
275 char *pcx_errormsg(int error_number)
276 {
277         char *p = pcx_error_messages;
278
279         while (error_number--) {
280
281                 if (!p) return NULL;
282
283                 p += strlen(p)+1;
284
285         }
286
287         return p;
288 }
289
290 // fullscreen loading, 10/14/99 Jan Bobrowski
291
292 int pcx_read_fullscr(char * filename, ubyte * palette)
293 {
294         int pcx_error;
295         grs_bitmap bm;
296         gr_init_bitmap_data(&bm);
297         pcx_error = pcx_read_bitmap(filename, &bm, BM_LINEAR, palette);
298         if (pcx_error == PCX_ERROR_NONE)
299                 show_fullscr(&bm);
300         gr_free_bitmap_data(&bm);
301         return pcx_error;
302 }
303
304 /*
305  * reads a PCXHeader structure from a CFILE
306  */
307 void PCXHeader_read(PCXHeader *ph, CFILE *fp)
308 {
309         ph->Manufacturer = cfile_read_byte(fp);
310         ph->Version = cfile_read_byte(fp);
311         ph->Encoding = cfile_read_byte(fp);
312         ph->BitsPerPixel = cfile_read_byte(fp);
313         ph->Xmin = cfile_read_short(fp);
314         ph->Ymin = cfile_read_short(fp);
315         ph->Xmax = cfile_read_short(fp);
316         ph->Ymax = cfile_read_short(fp);
317         ph->Hdpi = cfile_read_short(fp);
318         ph->Vdpi = cfile_read_short(fp);
319         cfread(&ph->ColorMap, 16*3, 1, fp);
320         ph->Reserved = cfile_read_byte(fp);
321         ph->Nplanes = cfile_read_byte(fp);
322         ph->BytesPerLine = cfile_read_short(fp);
323         cfread(&ph->filler, 60, 1, fp);
324 }