1 /***********************************************************
2 * Artsoft Retro-Game Library *
3 *----------------------------------------------------------*
4 * (c) 1994-2006 Artsoft Entertainment *
6 * Detmolder Strasse 189 *
9 * e-mail: info@artsoft.org *
10 *----------------------------------------------------------*
12 ***********************************************************/
18 #if !defined(TARGET_SDL)
22 #define PCX_MAGIC 0x0a /* first byte in a PCX image file */
23 #define PCX_SUPPORTED_VERSION 5 /* last acceptable version number */
24 #define PCX_ENCODING 1 /* PCX encoding method */
25 #define PCX_256COLORS_MAGIC 0x0c /* first byte of a PCX 256 color map */
26 #define PCX_MAXCOLORS 256 /* maximum number of colors */
28 #define PCX_HEADER_SIZE 128
29 #define PCX_COLORMAP_SIZE (3 * PCX_MAXCOLORS)
33 unsigned char signature; /* PCX file identifier */
34 unsigned char version; /* version compatibility level */
35 unsigned char encoding; /* encoding method */
36 unsigned char bits_per_pixel; /* bits per pixel (not depth!) */
37 unsigned short xmin; /* X position of left edge */
38 unsigned short ymin; /* Y position of top edge */
39 unsigned short xmax; /* X position of right edge */
40 unsigned short ymax; /* Y position of bottom edge */
41 unsigned short hres; /* X screen resolution of source image */
42 unsigned short vres; /* Y screen resolution of source image */
43 unsigned char palette[16][3]; /* PCX color map */
44 unsigned char reserved; /* should be 0, 1 if std res fax */
45 unsigned char color_planes; /* "color planes" in image */
46 unsigned short bytes_per_line;/* byte delta between scanlines */
47 unsigned short palette_type; /* 0 = undef, 1 = color, 2 = grayscale */
48 unsigned char filler[58]; /* fill to struct size of 128 */
51 /* global PCX error value */
52 int errno_pcx = PCX_Success;
54 static boolean PCX_ReadBitmap(FILE *file, struct PCX_Header *pcx, Image *image)
56 int width = image->width;
57 int height = image->height;
58 int pcx_depth = pcx->bits_per_pixel * pcx->color_planes;
59 int bytes_per_row = pcx->color_planes * pcx->bytes_per_line;
60 byte *row_buffer = checked_malloc(bytes_per_row);
61 byte *bitmap_ptr = image->data;
64 for (y = 0; y < height; y++)
66 /* decode a scan line into a temporary buffer first */
67 byte *dst_ptr = (pcx_depth == 8 ? bitmap_ptr : row_buffer);
68 byte value = 0, count = 0;
72 for (i = 0; i < bytes_per_row; i++)
76 if ((value_int = fgetc(file)) == EOF)
82 value = (byte)value_int;
84 if ((value & 0xc0) == 0xc0) /* this is a repeat count byte */
86 count = value & 0x3f; /* extract repeat count from byte */
88 if ((value_int = fgetc(file)) == EOF)
94 value = (byte)value_int;
104 image->rgb.color_used[value] = TRUE;
107 if (pcx_depth <= 4) /* expand planes to 1 byte/pixel */
109 byte *src_ptr = row_buffer;
112 for (plane = 0; plane < pcx->color_planes; plane++)
116 for (i = 0; i < pcx->bytes_per_line; i++)
118 byte value = *src_ptr++;
120 for (j = 7; j >= 0; j--)
122 byte bit = (value >> j) & 1;
124 if (i * 8 + j >= width) /* skip padding bits */
127 bitmap_ptr[x++] |= bit << plane;
132 else if (pcx_depth == 24) /* de-interlace planes */
134 byte *src_ptr = row_buffer;
137 for (plane = 0; plane < pcx->color_planes; plane++)
141 dst_ptr = bitmap_ptr + plane;
142 for (x = 0; x < width; x++)
144 *dst_ptr = *src_ptr++;
145 dst_ptr += pcx->color_planes;
150 bitmap_ptr += image->bytes_per_row;
158 static boolean PCX_ReadColormap(FILE *file,struct PCX_Header *pcx,Image *image)
160 int pcx_depth = pcx->bits_per_pixel * pcx->color_planes;
161 int num_colors = (1 << pcx_depth);
164 if (image->depth != 8)
172 /* look for a 256-colour palette */
175 if ((value_int = fgetc(file)) == EOF)
177 value = (byte)value_int;
179 while (value != PCX_256COLORS_MAGIC);
181 /* read 256 colors from PCX colormap */
182 for (i = 0; i < PCX_MAXCOLORS; i++)
184 image->rgb.red[i] = (byte)fgetc(file) << 8;
185 image->rgb.green[i] = (byte)fgetc(file) << 8;
186 image->rgb.blue[i] = (byte)fgetc(file) << 8;
191 for (i = 0; i < num_colors; i++)
193 image->rgb.red[i] = pcx->palette[i][0] << 8;
194 image->rgb.green[i] = pcx->palette[i][1] << 8;
195 image->rgb.blue[i] = pcx->palette[i][2] << 8;
202 Image *Read_PCX_to_Image(char *filename)
205 byte header_buffer[PCX_HEADER_SIZE];
206 struct PCX_Header pcx;
208 int width, height, depth, pcx_depth;
211 errno_pcx = PCX_Success;
213 if (!(file = fopen(filename, MODE_READ)))
215 errno_pcx = PCX_OpenFailed;
219 if (fread(header_buffer, 1, PCX_HEADER_SIZE, file) != PCX_HEADER_SIZE)
223 errno_pcx = PCX_ReadFailed;
227 pcx.signature = header_buffer[0];
228 pcx.version = header_buffer[1];
229 pcx.encoding = header_buffer[2];
230 pcx.bits_per_pixel = header_buffer[3];
231 pcx.xmin = (header_buffer[5] << 8) | header_buffer[4];
232 pcx.ymin = (header_buffer[7] << 8) | header_buffer[6];
233 pcx.xmax = (header_buffer[9] << 8) | header_buffer[8];
234 pcx.ymax = (header_buffer[11] << 8) | header_buffer[10];
235 pcx.color_planes = header_buffer[65];
236 pcx.bytes_per_line = (header_buffer[67] << 8) | header_buffer[66];
237 pcx.palette_type = (header_buffer[69] << 8) | header_buffer[68];
239 for (i = 0; i < 48; i++)
240 pcx.palette[i / 3][i % 3] = header_buffer[16 + i];
242 width = pcx.xmax - pcx.xmin + 1;
243 height = pcx.ymax - pcx.ymin + 1;
244 pcx_depth = pcx.bits_per_pixel * pcx.color_planes;
245 depth = ((pcx_depth + 7) / 8) * 8;
247 if (pcx.signature != PCX_MAGIC ||
248 pcx.version != PCX_SUPPORTED_VERSION ||
249 pcx.encoding != PCX_ENCODING ||
250 width < 0 || height < 0)
254 errno_pcx = PCX_FileInvalid;
262 printf("%s is a %dx%d PC Paintbrush image\n", filename, width, height);
263 printf("depth: %d\n", depth);
264 printf("bits_per_pixel: %d\n", pcx.bits_per_pixel);
265 printf("color_planes: %d\n", pcx.color_planes);
266 printf("bytes_per_line: %d\n", pcx.bytes_per_line);
267 printf("palette type: %s\n",
268 (pcx.palette_type == 1 ? "color" :
269 pcx.palette_type == 2 ? "grayscale" : "undefined"));
273 /* allocate new image structure */
274 image = newImage(width, height, depth);
276 /* read compressed bitmap data */
277 if (!PCX_ReadBitmap(file, &pcx, image))
282 errno_pcx = PCX_FileInvalid;
286 /* read colormap data */
287 if (!PCX_ReadColormap(file, &pcx, image))
292 errno_pcx = PCX_ColorFailed;
300 /* determine number of used colormap entries for 8-bit PCX images */
301 for (i = 0; i < PCX_MAXCOLORS; i++)
302 if (image->rgb.color_used[i])
308 printf("Read_PCX_to_Image: %d colors in colormap\n", image->rgb.used);
314 #endif /* !TARGET_SDL */