1 /* $Id: pcx.c,v 1.5 2002-08-06 04:49:43 btb 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 int pcx_read_bitmap( char * filename, grs_bitmap * bmp,int bitmap_type ,ubyte * palette )
41 int i, row, col, count, xsize, ysize;
44 PCXfile = cfopen( filename , "rb" );
46 return PCX_ERROR_OPENING;
48 // read 128 char PCX header
49 PCXHeader_read(&header, PCXfile);
51 if (cfread( &header, sizeof(PCXHeader), 1, PCXfile )!=1) {
53 return PCX_ERROR_NO_HEADER;
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)) {
60 return PCX_ERROR_WRONG_VERSION;
63 // Find the size of the image
64 xsize = header.Xmax - header.Xmin + 1;
65 ysize = header.Ymax - header.Ymin + 1;
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);
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 ) {
79 return PCX_ERROR_READING;
81 if ((data & 0xC0) == 0xC0) {
83 if (cfread( &data, 1, 1, PCXfile )!=1 ) {
85 return PCX_ERROR_READING;
87 memset( pixdata, data, count );
97 for (row=0; row< ysize ; row++) {
98 for (col=0; col< xsize ; ) {
99 if (cfread( &data, 1, 1, PCXfile )!=1 ) {
101 return PCX_ERROR_READING;
103 if ((data & 0xC0) == 0xC0) {
105 if (cfread( &data, 1, 1, PCXfile )!=1 ) {
107 return PCX_ERROR_READING;
109 for (i=0;i<count;i++)
110 gr_bm_pixel( bmp, col+i, row, data );
113 gr_bm_pixel( bmp, col, row, data );
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) {
125 if (cfread(palette,768, 1, PCXfile)!=1) {
127 return PCX_ERROR_READING;
129 for (i=0; i<768; i++ )
134 return PCX_ERROR_NO_PALETTE;
138 return PCX_ERROR_NONE;
141 int pcx_write_bitmap( char * filename, grs_bitmap * bmp, ubyte * palette )
149 memset( &header, 0, sizeof( PCXHeader ) );
151 header.Manufacturer = 10;
154 header.BitsPerPixel = 8;
156 header.Xmax = bmp->bm_w-1;
157 header.Ymax = bmp->bm_h-1;
158 header.BytesPerLine = bmp->bm_w;
160 PCXfile = fopen( filename , "wb" );
162 return PCX_ERROR_OPENING;
164 if ( fwrite( &header, sizeof( PCXHeader ), 1, PCXfile ) != 1 ) {
166 return PCX_ERROR_WRITING;
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 )) {
172 return PCX_ERROR_WRITING;
176 // Mark an extended palette
178 if (fwrite( &data, 1, 1, PCXfile )!=1) {
180 return PCX_ERROR_WRITING;
183 // Write the extended palette
184 for (i=0; i<768; i++ )
187 retval = fwrite( palette, 768, 1, PCXfile );
189 for (i=0; i<768; i++ )
194 return PCX_ERROR_WRITING;
198 return PCX_ERROR_NONE;
202 // returns number of bytes written into outBuff, 0 if failed
203 int pcx_encode_line(ubyte *inBuff, int inLen, FILE * fp)
208 register ubyte runCount; // max single runlength is 63
213 for (srcIndex = 1; srcIndex < inLen; srcIndex++) {
216 runCount++; // it encodes
217 if (runCount == 63) {
218 if (!(i=pcx_encode_byte(last, runCount, fp)))
223 } else { // this != last
225 if (!(i=pcx_encode_byte(last, runCount, fp)))
234 if (runCount) { // finish up
235 if (!(i=pcx_encode_byte(last, runCount, fp)))
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)
247 if ( (cnt==1) && (0xc0 != (0xc0 & byt)) ) {
248 if(EOF == putc((int)byt, fid))
249 return 0; // disk write error (probably full)
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
262 //text for error messges
263 char pcx_error_messages[] = {
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"
274 //function to return pointer to error message
275 char *pcx_errormsg(int error_number)
277 char *p = pcx_error_messages;
279 while (error_number--) {
290 // fullscreen loading, 10/14/99 Jan Bobrowski
292 int pcx_read_fullscr(char * filename, ubyte * palette)
296 gr_init_bitmap_data(&bm);
297 pcx_error = pcx_read_bitmap(filename, &bm, BM_LINEAR, palette);
298 if (pcx_error == PCX_ERROR_NONE)
300 gr_free_bitmap_data(&bm);
305 * reads a PCXHeader structure from a CFILE
307 void PCXHeader_read(PCXHeader *ph, CFILE *fp)
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);