1 /***********************************************************
2 * Rocks'n'Diamonds -- McDuffin Strikes Back! *
3 *----------------------------------------------------------*
4 * (c) 1995-98 Artsoft Entertainment *
8 * phone: ++49 +521 290471 *
9 * email: aeglos@valinor.owl.de *
10 *----------------------------------------------------------*
12 ***********************************************************/
17 #define PCX_MAGIC 0x0a /* first byte in a PCX image file */
18 #define PCX_LAST_VERSION 5 /* last acceptable version number */
19 #define PCX_ENCODING 1 /* PCX encoding method */
20 #define PCX_256COLORS_MAGIC 0x0c /* first byte of a PCX 256 color map */
21 #define PCX_MAXDEPTH 8 /* supports up to 8 bits per pixel */
22 #define PCX_MAXCOLORS 256 /* maximum number of colors */
24 #define PCX_HEADER_SIZE 128
25 #define PCX_COLORMAP_SIZE (3 * PCX_MAXCOLORS)
29 unsigned char signature; /* PCX file identifier */
30 unsigned char version; /* version compatibility level */
31 unsigned char encoding; /* encoding method */
32 unsigned char bits_per_pixel; /* bits per pixel, or depth */
33 unsigned short xmin; /* X position of left edge */
34 unsigned short ymin; /* Y position of top edge */
35 unsigned short xmax; /* X position of right edge */
36 unsigned short ymax; /* Y position of bottom edge */
37 unsigned short hres; /* X screen resolution of source image */
38 unsigned short vres; /* Y screen resolution of source image */
39 unsigned char palette[16][3]; /* PCX color map */
40 unsigned char reserved; /* should be 0, 1 if std res fax */
41 unsigned char color_planes; /* bit planes in image */
42 unsigned short bytes_per_line;/* byte delta between scanlines */
43 unsigned short palette_type; /* 0 = undef, 1 = color, 2 = grayscale */
44 unsigned char filler[58]; /* fill to struct size of 128 */
47 static byte *PCX_ReadBitmap(Image *, byte *, byte *);
48 static byte *PCX_ReadColormap(Image *, byte *, byte *);
50 Image *Read_PCX_to_Image(char *filename)
54 byte *buffer_ptr, *buffer_last;
55 unsigned int file_length;
56 struct PCX_Header pcx;
58 int width, height, depth;
61 if (!(file = fopen(filename, "r")))
64 if (fseek(file, 0, SEEK_END) == -1)
70 file_length = ftell(file);
73 if (file_length < PCX_HEADER_SIZE + PCX_COLORMAP_SIZE)
79 file_buffer = checked_malloc(file_length);
81 if (fread(file_buffer, 1, file_length, file) != file_length)
89 pcx.signature = file_buffer[0];
90 pcx.version = file_buffer[1];
91 pcx.encoding = file_buffer[2];
92 pcx.bits_per_pixel = file_buffer[3];
93 pcx.xmin = file_buffer[4] + 256 * file_buffer[5];
94 pcx.ymin = file_buffer[6] + 256 * file_buffer[7];
95 pcx.xmax = file_buffer[8] + 256 * file_buffer[9];
96 pcx.ymax = file_buffer[10] + 256 * file_buffer[11];
97 pcx.color_planes = file_buffer[65];
98 pcx.bytes_per_line = file_buffer[66] + 256 * file_buffer[67];
99 pcx.palette_type = file_buffer[68] + 256 * file_buffer[69];
101 width = pcx.xmax - pcx.xmin + 1;
102 height = pcx.ymax - pcx.ymin + 1;
103 depth = pcx.bits_per_pixel;
105 if (pcx.signature != PCX_MAGIC || pcx.version > PCX_LAST_VERSION ||
106 pcx.encoding != PCX_ENCODING || pcx.color_planes > PCX_MAXDEPTH ||
107 width < 0 || height < 0)
115 printf("%s is a %dx%d PC Paintbrush image with %d bitplanes\n",
116 filename, pcx.xmax, pcx.ymax,
118 printf("depth: %d\n", pcx.bits_per_pixel);
119 printf("bytes_per_line: %d\n", pcx.bytes_per_line);
120 printf("palette type: %s\n",
121 (pcx.palette_type == 1 ? "color" :
122 pcx.palette_type == 2 ? "grayscale" : "undefined"));
125 /* allocate new image structure */
126 image = newImage(width, height, depth);
128 buffer_ptr = file_buffer + PCX_HEADER_SIZE;
129 buffer_last = file_buffer + file_length;
131 /* read compressed bitmap data */
132 if ((buffer_ptr = PCX_ReadBitmap(image, buffer_ptr, buffer_last)) == NULL)
139 /* read colormap data */
140 if (!PCX_ReadColormap(image, buffer_ptr, buffer_last))
149 /* determine number of used colormap entries */
151 for (i=0; i<PCX_MAXCOLORS; i++)
152 if (image->rgb.color_used[i])
156 printf("Read_PCX_to_Image: %d colors found\n", image->rgb.used);
161 static byte *PCX_ReadBitmap(Image *image, byte *buffer_ptr, byte *buffer_last)
163 /* Run Length Encoding: If the two high bits are set,
164 * then the low 6 bits contain a repeat count, and the byte to
165 * repeat is the next byte in the file. If the two high bits are
166 * not set, then this is the byte to write.
169 unsigned int bytes_per_pixel = (image->depth + 7) / 8;
170 register byte *bitmap_ptr, *bitmap_last;
171 register byte value, count;
173 bitmap_ptr = image->data;
174 bitmap_last = bitmap_ptr + (image->width * image->height * bytes_per_pixel);
176 while (bitmap_ptr < bitmap_last && buffer_ptr < buffer_last)
178 value = *buffer_ptr++;
180 if ((value & 0xc0) == 0xc0) /* this is a repeat count byte */
182 count = value & 0x3f; /* extract repeat count from byte */
183 value = *buffer_ptr++; /* next byte is value to repeat */
185 for (; count && bitmap_ptr < bitmap_last; count--)
186 *bitmap_ptr++ = value;
188 if (count) /* repeat count spans end of bitmap */
192 *bitmap_ptr++ = value;
194 image->rgb.color_used[value] = TRUE;
197 /* check if end of buffer was reached before end of bitmap */
198 if (bitmap_ptr < bitmap_last)
201 /* return current buffer position for next decoding function */
205 static byte *PCX_ReadColormap(Image *image,byte *buffer_ptr, byte *buffer_last)
209 /* read colormap magic byte */
210 magic = *buffer_ptr++;
212 /* check magic colormap header byte */
213 if (magic != PCX_256COLORS_MAGIC)
216 /* check if enough bytes left for a complete colormap */
217 if (buffer_ptr + PCX_COLORMAP_SIZE > buffer_last)
220 /* read 256 colors from PCX colormap */
221 for (i=0; i<PCX_MAXCOLORS; i++)
223 image->rgb.red[i] = *buffer_ptr++ << 8;
224 image->rgb.green[i] = *buffer_ptr++ << 8;
225 image->rgb.blue[i] = *buffer_ptr++ << 8;
228 /* return current buffer position for next decoding function */