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_MAGIC 0x0a /* first byte in a PCX image file */
19 #define PCX_LAST_VERSION 5 /* last acceptable version number */
20 #define PCX_ENCODING 1 /* PCX encoding method */
21 #define PCX_256COLORS_MAGIC 0x0c /* first byte of a PCX 256 color map */
22 #define PCX_MAXDEPTH 8 /* supports up to 8 bits per pixel */
23 #define PCX_MAXCOLORS 256 /* maximum number of colors */
25 #define PCX_HEADER_SIZE 128
26 #define PCX_COLORMAP_SIZE (3 * PCX_MAXCOLORS)
30 unsigned char signature; /* PCX file identifier */
31 unsigned char version; /* version compatibility level */
32 unsigned char encoding; /* encoding method */
33 unsigned char bits_per_pixel; /* bits per pixel, or depth */
34 unsigned short xmin; /* X position of left edge */
35 unsigned short ymin; /* Y position of top edge */
36 unsigned short xmax; /* X position of right edge */
37 unsigned short ymax; /* Y position of bottom edge */
38 unsigned short hres; /* X screen resolution of source image */
39 unsigned short vres; /* Y screen resolution of source image */
40 unsigned char palette[16][3]; /* PCX color map */
41 unsigned char reserved; /* should be 0, 1 if std res fax */
42 unsigned char color_planes; /* bit planes in image */
43 unsigned short bytes_per_line;/* byte delta between scanlines */
44 unsigned short palette_type; /* 0 = undef, 1 = color, 2 = grayscale */
45 unsigned char filler[58]; /* fill to struct size of 128 */
48 static byte *PCX_ReadBitmap(Image *image, byte *buffer_ptr, byte *buffer_last)
50 /* Run Length Encoding: If the two high bits are set,
51 * then the low 6 bits contain a repeat count, and the byte to
52 * repeat is the next byte in the file. If the two high bits are
53 * not set, then this is the byte to write.
56 unsigned int bytes_per_pixel = (image->depth + 7) / 8;
57 register byte *bitmap_ptr, *bitmap_last;
58 register byte value, count;
60 bitmap_ptr = image->data;
61 bitmap_last = bitmap_ptr + (image->width * image->height * bytes_per_pixel);
63 while (bitmap_ptr < bitmap_last && buffer_ptr < buffer_last)
65 value = *buffer_ptr++;
67 if ((value & 0xc0) == 0xc0) /* this is a repeat count byte */
69 count = value & 0x3f; /* extract repeat count from byte */
70 value = *buffer_ptr++; /* next byte is value to repeat */
72 for (; count && bitmap_ptr < bitmap_last; count--)
73 *bitmap_ptr++ = value;
75 if (count) /* repeat count spans end of bitmap */
79 *bitmap_ptr++ = value;
81 image->rgb.color_used[value] = TRUE;
84 /* check if end of buffer was reached before end of bitmap */
85 if (bitmap_ptr < bitmap_last)
88 /* return current buffer position for next decoding function */
92 static byte *PCX_ReadColormap(Image *image,byte *buffer_ptr, byte *buffer_last)
96 /* read colormap magic byte */
97 magic = *buffer_ptr++;
99 /* check magic colormap header byte */
100 if (magic != PCX_256COLORS_MAGIC)
103 /* check if enough bytes left for a complete colormap */
104 if (buffer_ptr + PCX_COLORMAP_SIZE > buffer_last)
107 /* read 256 colors from PCX colormap */
108 for (i=0; i<PCX_MAXCOLORS; i++)
110 image->rgb.red[i] = *buffer_ptr++ << 8;
111 image->rgb.green[i] = *buffer_ptr++ << 8;
112 image->rgb.blue[i] = *buffer_ptr++ << 8;
115 /* return current buffer position for next decoding function */
119 Image *Read_PCX_to_Image(char *filename)
123 byte *buffer_ptr, *buffer_last;
124 unsigned int file_length;
125 struct PCX_Header pcx;
127 int width, height, depth;
130 if (!(file = fopen(filename, "r")))
133 if (fseek(file, 0, SEEK_END) == -1)
139 file_length = ftell(file);
142 if (file_length < PCX_HEADER_SIZE)
144 /* PCX file is too short to contain a valid PCX header */
149 file_buffer = checked_malloc(file_length);
151 if (fread(file_buffer, 1, file_length, file) != file_length)
159 pcx.signature = file_buffer[0];
160 pcx.version = file_buffer[1];
161 pcx.encoding = file_buffer[2];
162 pcx.bits_per_pixel = file_buffer[3];
163 pcx.xmin = file_buffer[4] + 256 * file_buffer[5];
164 pcx.ymin = file_buffer[6] + 256 * file_buffer[7];
165 pcx.xmax = file_buffer[8] + 256 * file_buffer[9];
166 pcx.ymax = file_buffer[10] + 256 * file_buffer[11];
167 pcx.color_planes = file_buffer[65];
168 pcx.bytes_per_line = file_buffer[66] + 256 * file_buffer[67];
169 pcx.palette_type = file_buffer[68] + 256 * file_buffer[69];
171 width = pcx.xmax - pcx.xmin + 1;
172 height = pcx.ymax - pcx.ymin + 1;
173 depth = pcx.bits_per_pixel;
175 if (pcx.signature != PCX_MAGIC || pcx.version > PCX_LAST_VERSION ||
176 pcx.encoding != PCX_ENCODING || pcx.color_planes > PCX_MAXDEPTH ||
177 width < 0 || height < 0)
185 printf("%s is a %dx%d PC Paintbrush image with %d bitplanes\n",
186 filename, pcx.xmax, pcx.ymax,
188 printf("depth: %d\n", pcx.bits_per_pixel);
189 printf("color_planes: %d\n", pcx.color_planes);
190 printf("bytes_per_line: %d\n", pcx.bytes_per_line);
191 printf("palette type: %s\n",
192 (pcx.palette_type == 1 ? "color" :
193 pcx.palette_type == 2 ? "grayscale" : "undefined"));
196 /* allocate new image structure */
197 image = newImage(width, height, depth);
199 buffer_ptr = file_buffer + PCX_HEADER_SIZE;
200 buffer_last = file_buffer + file_length;
202 /* read compressed bitmap data */
203 if ((buffer_ptr = PCX_ReadBitmap(image, buffer_ptr, buffer_last)) == NULL)
210 if (file_length < PCX_HEADER_SIZE + PCX_COLORMAP_SIZE)
212 /* PCX file is too short to contain a valid 256 colors colormap */
217 /* read colormap data */
218 if (!PCX_ReadColormap(image, buffer_ptr, buffer_last))
227 /* determine number of used colormap entries */
229 for (i=0; i<PCX_MAXCOLORS; i++)
230 if (image->rgb.color_used[i])
234 printf("Read_PCX_to_Image: %d colors found\n", image->rgb.used);