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 ***********************************************************/
18 #define PCX_DEBUG FALSE
20 #define PCX_MAGIC 0x0a /* first byte in a PCX image file */
21 #define PCX_LAST_VERSION 5 /* last acceptable version number */
22 #define PCX_ENCODING 1 /* PCX encoding method */
23 #define PCX_256COLORS_MAGIC 0x0c /* first byte of a PCX 256 color map */
24 #define PCX_MAXDEPTH 8 /* supports up to 8 bits per pixel */
25 #define PCX_MAXCOLORS 256 /* maximum number of colors */
27 #define PCX_HEADER_SIZE 128
28 #define PCX_COLORMAP_SIZE (3 * PCX_MAXCOLORS)
32 unsigned char signature; /* PCX file identifier */
33 unsigned char version; /* version compatibility level */
34 unsigned char encoding; /* encoding method */
35 unsigned char bits_per_pixel; /* bits per pixel, or depth */
36 unsigned short xmin; /* X position of left edge */
37 unsigned short ymin; /* Y position of top edge */
38 unsigned short xmax; /* X position of right edge */
39 unsigned short ymax; /* Y position of bottom edge */
40 unsigned short hres; /* X screen resolution of source image */
41 unsigned short vres; /* Y screen resolution of source image */
42 unsigned char palette[16][3]; /* PCX color map */
43 unsigned char reserved; /* should be 0, 1 if std res fax */
44 unsigned char color_planes; /* bit planes in image */
45 unsigned short bytes_per_line;/* byte delta between scanlines */
46 unsigned short palette_type; /* 0 = undef, 1 = color, 2 = grayscale */
47 unsigned char filler[58]; /* fill to struct size of 128 */
50 /* global PCX error value */
51 int errno_pcx = PCX_Success;
53 static byte *PCX_ReadBitmap(Image *image, byte *buffer_ptr, byte *buffer_last)
55 /* Run Length Encoding: If the two high bits are set,
56 * then the low 6 bits contain a repeat count, and the byte to
57 * repeat is the next byte in the file. If the two high bits are
58 * not set, then this is the byte to write.
61 unsigned int bytes_per_pixel = (image->depth + 7) / 8;
62 register byte *bitmap_ptr, *bitmap_last;
63 register byte value, count;
65 bitmap_ptr = image->data;
66 bitmap_last = bitmap_ptr + (image->width * image->height * bytes_per_pixel);
68 while (bitmap_ptr < bitmap_last && buffer_ptr < buffer_last)
70 value = *buffer_ptr++;
72 if ((value & 0xc0) == 0xc0) /* this is a repeat count byte */
74 count = value & 0x3f; /* extract repeat count from byte */
75 value = *buffer_ptr++; /* next byte is value to repeat */
77 for (; count && bitmap_ptr < bitmap_last; count--)
78 *bitmap_ptr++ = value;
80 if (count) /* repeat count spans end of bitmap */
84 *bitmap_ptr++ = value;
86 image->rgb.color_used[value] = TRUE;
89 /* check if end of buffer was reached before end of bitmap */
90 if (bitmap_ptr < bitmap_last)
93 /* return current buffer position for next decoding function */
97 static byte *PCX_ReadColormap(Image *image,byte *buffer_ptr, byte *buffer_last)
101 /* read colormap magic byte */
102 magic = *buffer_ptr++;
104 /* check magic colormap header byte */
105 if (magic != PCX_256COLORS_MAGIC)
108 /* check if enough bytes left for a complete colormap */
109 if (buffer_ptr + PCX_COLORMAP_SIZE > buffer_last)
112 /* read 256 colors from PCX colormap */
113 for (i=0; i<PCX_MAXCOLORS; i++)
115 image->rgb.red[i] = *buffer_ptr++ << 8;
116 image->rgb.green[i] = *buffer_ptr++ << 8;
117 image->rgb.blue[i] = *buffer_ptr++ << 8;
120 /* return current buffer position for next decoding function */
124 Image *Read_PCX_to_Image(char *filename)
128 byte *buffer_ptr, *buffer_last;
129 unsigned int file_length;
130 struct PCX_Header pcx;
132 int width, height, depth;
135 errno_pcx = PCX_Success;
137 if (!(file = fopen(filename, "r")))
139 errno_pcx = PCX_OpenFailed;
143 if (fseek(file, 0, SEEK_END) == -1)
146 errno_pcx = PCX_ReadFailed;
150 file_length = ftell(file);
153 if (file_length < PCX_HEADER_SIZE)
155 /* PCX file is too short to contain a valid PCX header */
157 errno_pcx = PCX_FileInvalid;
161 file_buffer = checked_malloc(file_length);
163 if (fread(file_buffer, 1, file_length, file) != file_length)
166 errno_pcx = PCX_ReadFailed;
172 pcx.signature = file_buffer[0];
173 pcx.version = file_buffer[1];
174 pcx.encoding = file_buffer[2];
175 pcx.bits_per_pixel = file_buffer[3];
176 pcx.xmin = file_buffer[4] + 256 * file_buffer[5];
177 pcx.ymin = file_buffer[6] + 256 * file_buffer[7];
178 pcx.xmax = file_buffer[8] + 256 * file_buffer[9];
179 pcx.ymax = file_buffer[10] + 256 * file_buffer[11];
180 pcx.color_planes = file_buffer[65];
181 pcx.bytes_per_line = file_buffer[66] + 256 * file_buffer[67];
182 pcx.palette_type = file_buffer[68] + 256 * file_buffer[69];
184 width = pcx.xmax - pcx.xmin + 1;
185 height = pcx.ymax - pcx.ymin + 1;
186 depth = pcx.bits_per_pixel;
188 if (pcx.signature != PCX_MAGIC || pcx.version > PCX_LAST_VERSION ||
189 pcx.encoding != PCX_ENCODING || pcx.color_planes > PCX_MAXDEPTH ||
190 width < 0 || height < 0)
193 errno_pcx = PCX_FileInvalid;
200 printf("%s is a %dx%d PC Paintbrush image with %d bitplanes\n",
201 filename, pcx.xmax, pcx.ymax,
203 printf("depth: %d\n", pcx.bits_per_pixel);
204 printf("color_planes: %d\n", pcx.color_planes);
205 printf("bytes_per_line: %d\n", pcx.bytes_per_line);
206 printf("palette type: %s\n",
207 (pcx.palette_type == 1 ? "color" :
208 pcx.palette_type == 2 ? "grayscale" : "undefined"));
212 /* allocate new image structure */
213 image = newImage(width, height, depth);
215 buffer_ptr = file_buffer + PCX_HEADER_SIZE;
216 buffer_last = file_buffer + file_length;
218 /* read compressed bitmap data */
219 if ((buffer_ptr = PCX_ReadBitmap(image, buffer_ptr, buffer_last)) == NULL)
223 errno_pcx = PCX_FileInvalid;
227 if (file_length < PCX_HEADER_SIZE + PCX_COLORMAP_SIZE)
229 /* PCX file is too short to contain a valid 256 colors colormap */
231 errno_pcx = PCX_ColorFailed;
235 /* read colormap data */
236 if (!PCX_ReadColormap(image, buffer_ptr, buffer_last))
240 errno_pcx = PCX_ColorFailed;
246 /* determine number of used colormap entries */
248 for (i=0; i<PCX_MAXCOLORS; i++)
249 if (image->rgb.color_used[i])
254 printf("Read_PCX_to_Image: %d colors found\n", image->rgb.used);