1 /* $Id: pcx.c,v 1.3 2002-07-17 21:55:19 bradleyb Exp $ */
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.
16 * Routines to read/write pcx images.
34 int pcx_encode_byte(ubyte byt, ubyte cnt, FILE * fid);
35 int pcx_encode_line(ubyte *inBuff, int inLen, FILE * fp);
37 /* PCX Header data type */
49 ubyte ColorMap[16][3];
57 int pcx_read_bitmap( char * filename, grs_bitmap * bmp,int bitmap_type ,ubyte * palette )
61 int i, row, col, count, xsize, ysize;
64 PCXfile = cfopen( filename , "rb" );
66 return PCX_ERROR_OPENING;
68 // read 128 char PCX header
69 if (cfread( &header, sizeof(PCXHeader), 1, PCXfile )!=1) {
71 return PCX_ERROR_NO_HEADER;
74 // Is it a 256 color PCX file?
75 if ((header.Manufacturer != 10)||(header.Encoding != 1)||(header.Nplanes != 1)||(header.BitsPerPixel != 8)||(header.Version != 5)) {
77 return PCX_ERROR_WRONG_VERSION;
80 // Find the size of the image
81 xsize = header.Xmax - header.Xmin + 1;
82 ysize = header.Ymax - header.Ymin + 1;
84 if ( bitmap_type == BM_LINEAR ) {
85 if ( bmp->bm_data == NULL ) {
86 gr_init_bitmap_alloc (bmp, bitmap_type, 0, 0, xsize, ysize, xsize);
90 if ( bmp->bm_type == BM_LINEAR ) {
91 for (row=0; row< ysize ; row++) {
92 pixdata = &bmp->bm_data[bmp->bm_rowsize*row];
93 for (col=0; col< xsize ; ) {
94 if (cfread( &data, 1, 1, PCXfile )!=1 ) {
96 return PCX_ERROR_READING;
98 if ((data & 0xC0) == 0xC0) {
100 if (cfread( &data, 1, 1, PCXfile )!=1 ) {
102 return PCX_ERROR_READING;
104 memset( pixdata, data, count );
114 for (row=0; row< ysize ; row++) {
115 for (col=0; col< xsize ; ) {
116 if (cfread( &data, 1, 1, PCXfile )!=1 ) {
118 return PCX_ERROR_READING;
120 if ((data & 0xC0) == 0xC0) {
122 if (cfread( &data, 1, 1, PCXfile )!=1 ) {
124 return PCX_ERROR_READING;
126 for (i=0;i<count;i++)
127 gr_bm_pixel( bmp, col+i, row, data );
130 gr_bm_pixel( bmp, col, row, data );
137 // Read the extended palette at the end of PCX file
138 if ( palette != NULL ) {
139 // Read in a character which should be 12 to be extended palette file
140 if (cfread( &data, 1, 1, PCXfile )==1) {
142 if (cfread(palette,768, 1, PCXfile)!=1) {
144 return PCX_ERROR_READING;
146 for (i=0; i<768; i++ )
151 return PCX_ERROR_NO_PALETTE;
155 return PCX_ERROR_NONE;
158 int pcx_write_bitmap( char * filename, grs_bitmap * bmp, ubyte * palette )
166 memset( &header, 0, sizeof( PCXHeader ) );
168 header.Manufacturer = 10;
171 header.BitsPerPixel = 8;
173 header.Xmax = bmp->bm_w-1;
174 header.Ymax = bmp->bm_h-1;
175 header.BytesPerLine = bmp->bm_w;
177 PCXfile = fopen( filename , "wb" );
179 return PCX_ERROR_OPENING;
181 if ( fwrite( &header, sizeof( PCXHeader ), 1, PCXfile ) != 1 ) {
183 return PCX_ERROR_WRITING;
186 for (i=0; i<bmp->bm_h; i++ ) {
187 if (!pcx_encode_line( &bmp->bm_data[bmp->bm_rowsize*i], bmp->bm_w, PCXfile )) {
189 return PCX_ERROR_WRITING;
193 // Mark an extended palette
195 if (fwrite( &data, 1, 1, PCXfile )!=1) {
197 return PCX_ERROR_WRITING;
200 // Write the extended palette
201 for (i=0; i<768; i++ )
204 retval = fwrite( palette, 768, 1, PCXfile );
206 for (i=0; i<768; i++ )
211 return PCX_ERROR_WRITING;
215 return PCX_ERROR_NONE;
219 // returns number of bytes written into outBuff, 0 if failed
220 int pcx_encode_line(ubyte *inBuff, int inLen, FILE * fp)
225 register ubyte runCount; // max single runlength is 63
230 for (srcIndex = 1; srcIndex < inLen; srcIndex++) {
233 runCount++; // it encodes
234 if (runCount == 63) {
235 if (!(i=pcx_encode_byte(last, runCount, fp)))
240 } else { // this != last
242 if (!(i=pcx_encode_byte(last, runCount, fp)))
251 if (runCount) { // finish up
252 if (!(i=pcx_encode_byte(last, runCount, fp)))
259 // subroutine for writing an encoded byte pair
260 // returns count of bytes written, 0 if error
261 int pcx_encode_byte(ubyte byt, ubyte cnt, FILE * fid)
264 if ( (cnt==1) && (0xc0 != (0xc0 & byt)) ) {
265 if(EOF == putc((int)byt, fid))
266 return 0; // disk write error (probably full)
269 if(EOF == putc((int)0xC0 | cnt, fid))
270 return 0; // disk write error
271 if(EOF == putc((int)byt, fid))
272 return 0; // disk write error
279 //text for error messges
280 char pcx_error_messages[] = {
282 "Error opening file.\0"
283 "Couldn't read PCX header.\0"
284 "Unsupported PCX version.\0"
285 "Error reading data.\0"
286 "Couldn't find palette information.\0"
287 "Error writing data.\0"
291 //function to return pointer to error message
292 char *pcx_errormsg(int error_number)
294 char *p = pcx_error_messages;
296 while (error_number--) {
307 // fullscreen loading, 10/14/99 Jan Bobrowski
309 int pcx_read_fullscr(char * filename, ubyte * palette)
313 gr_init_bitmap_data(&bm);
314 pcx_error = pcx_read_bitmap(filename, &bm, BM_LINEAR, palette);
315 if (pcx_error == PCX_ERROR_NONE)
317 gr_free_bitmap_data(&bm);