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 /* global PCX error value */
49 int errno_pcx = PCX_Success;
51 static byte *PCX_ReadBitmap(Image *image, byte *buffer_ptr, byte *buffer_last)
53 /* Run Length Encoding: If the two high bits are set,
54 * then the low 6 bits contain a repeat count, and the byte to
55 * repeat is the next byte in the file. If the two high bits are
56 * not set, then this is the byte to write.
59 unsigned int bytes_per_pixel = (image->depth + 7) / 8;
60 register byte *bitmap_ptr, *bitmap_last;
61 register byte value, count;
63 bitmap_ptr = image->data;
64 bitmap_last = bitmap_ptr + (image->width * image->height * bytes_per_pixel);
66 while (bitmap_ptr < bitmap_last && buffer_ptr < buffer_last)
68 value = *buffer_ptr++;
70 if ((value & 0xc0) == 0xc0) /* this is a repeat count byte */
72 count = value & 0x3f; /* extract repeat count from byte */
73 value = *buffer_ptr++; /* next byte is value to repeat */
75 for (; count && bitmap_ptr < bitmap_last; count--)
76 *bitmap_ptr++ = value;
78 if (count) /* repeat count spans end of bitmap */
82 *bitmap_ptr++ = value;
84 image->rgb.color_used[value] = TRUE;
87 /* check if end of buffer was reached before end of bitmap */
88 if (bitmap_ptr < bitmap_last)
91 /* return current buffer position for next decoding function */
95 static byte *PCX_ReadColormap(Image *image,byte *buffer_ptr, byte *buffer_last)
99 /* read colormap magic byte */
100 magic = *buffer_ptr++;
102 /* check magic colormap header byte */
103 if (magic != PCX_256COLORS_MAGIC)
106 /* check if enough bytes left for a complete colormap */
107 if (buffer_ptr + PCX_COLORMAP_SIZE > buffer_last)
110 /* read 256 colors from PCX colormap */
111 for (i=0; i<PCX_MAXCOLORS; i++)
113 image->rgb.red[i] = *buffer_ptr++ << 8;
114 image->rgb.green[i] = *buffer_ptr++ << 8;
115 image->rgb.blue[i] = *buffer_ptr++ << 8;
118 /* return current buffer position for next decoding function */
122 Image *Read_PCX_to_Image(char *filename)
126 byte *buffer_ptr, *buffer_last;
127 unsigned int file_length;
128 struct PCX_Header pcx;
130 int width, height, depth;
133 errno_pcx = PCX_Success;
135 if (!(file = fopen(filename, "r")))
137 errno_pcx = PCX_OpenFailed;
141 if (fseek(file, 0, SEEK_END) == -1)
144 errno_pcx = PCX_ReadFailed;
148 file_length = ftell(file);
151 if (file_length < PCX_HEADER_SIZE)
153 /* PCX file is too short to contain a valid PCX header */
155 errno_pcx = PCX_FileInvalid;
159 file_buffer = checked_malloc(file_length);
161 if (fread(file_buffer, 1, file_length, file) != file_length)
164 errno_pcx = PCX_ReadFailed;
170 pcx.signature = file_buffer[0];
171 pcx.version = file_buffer[1];
172 pcx.encoding = file_buffer[2];
173 pcx.bits_per_pixel = file_buffer[3];
174 pcx.xmin = file_buffer[4] + 256 * file_buffer[5];
175 pcx.ymin = file_buffer[6] + 256 * file_buffer[7];
176 pcx.xmax = file_buffer[8] + 256 * file_buffer[9];
177 pcx.ymax = file_buffer[10] + 256 * file_buffer[11];
178 pcx.color_planes = file_buffer[65];
179 pcx.bytes_per_line = file_buffer[66] + 256 * file_buffer[67];
180 pcx.palette_type = file_buffer[68] + 256 * file_buffer[69];
182 width = pcx.xmax - pcx.xmin + 1;
183 height = pcx.ymax - pcx.ymin + 1;
184 depth = pcx.bits_per_pixel;
186 if (pcx.signature != PCX_MAGIC || pcx.version > PCX_LAST_VERSION ||
187 pcx.encoding != PCX_ENCODING || pcx.color_planes > PCX_MAXDEPTH ||
188 width < 0 || height < 0)
191 errno_pcx = PCX_FileInvalid;
197 printf("%s is a %dx%d PC Paintbrush image with %d bitplanes\n",
198 filename, pcx.xmax, pcx.ymax,
200 printf("depth: %d\n", pcx.bits_per_pixel);
201 printf("color_planes: %d\n", pcx.color_planes);
202 printf("bytes_per_line: %d\n", pcx.bytes_per_line);
203 printf("palette type: %s\n",
204 (pcx.palette_type == 1 ? "color" :
205 pcx.palette_type == 2 ? "grayscale" : "undefined"));
208 /* allocate new image structure */
209 image = newImage(width, height, depth);
211 buffer_ptr = file_buffer + PCX_HEADER_SIZE;
212 buffer_last = file_buffer + file_length;
214 /* read compressed bitmap data */
215 if ((buffer_ptr = PCX_ReadBitmap(image, buffer_ptr, buffer_last)) == NULL)
219 errno_pcx = PCX_FileInvalid;
223 if (file_length < PCX_HEADER_SIZE + PCX_COLORMAP_SIZE)
225 /* PCX file is too short to contain a valid 256 colors colormap */
227 errno_pcx = PCX_ColorFailed;
231 /* read colormap data */
232 if (!PCX_ReadColormap(image, buffer_ptr, buffer_last))
236 errno_pcx = PCX_ColorFailed;
242 /* determine number of used colormap entries */
244 for (i=0; i<PCX_MAXCOLORS; i++)
245 if (image->rgb.color_used[i])
249 printf("Read_PCX_to_Image: %d colors found\n", image->rgb.used);