2 *************************************************************************
\r
4 * PCX_EXAM.C - PCX_LIB File Header Examination Utility
\r
8 * History: 91/02/14 - Created
\r
9 * 91/04/01 - Release 1.00A
\r
10 * 91/04/06 - Release 1.00B
\r
12 * Compiler: Microsoft C V6.0
\r
14 * Author: Ian Ashdown, P.Eng.
\r
16 * 620 Ballantree Road
\r
17 * West Vancouver, B.C.
\r
19 * Tel. (604) 922-6148
\r
20 * Fax. (604) 987-7621
\r
22 * Copyright: Public Domain
\r
24 *************************************************************************
\r
28 *************************************************************************
\r
32 * 1. When porting this program to other processors, remember that words
\r
33 * are stored by 80x86-based machines in the big-endian format. That
\r
34 * is, the eight least significant bits (lower byte) are stored
\r
35 * first, followed by the eight most significant bits (upper byte).
\r
36 * If PCX-format files are transferred to little-endian machines
\r
37 * (such as those based on 680x0 and Z8000 processors), the order of
\r
38 * bytes within each word will have to be reversed before they can
\r
39 * be interpreted. (This applies to the file header only, since the
\r
40 * encoded image data and optional 256-color palette are stored as
\r
43 *************************************************************************
\r
50 #include "pcx_int.h"
\r
52 /* FORWARD REFERENCES */
\r
56 /* PUBLIC FUNCTIONS */
\r
59 *************************************************************************
\r
61 * MAIN - Executive Function
\r
63 * Purpose: To read and display a PCX-format image file header.
\r
71 * Where: argc is the number of command-line arguments.
\r
72 * argv is a pointer to an array of command-line argument
\r
75 * Return: 0 if successful; otherwise 2.
\r
77 * Result: The file header information is written to "stdout".
\r
83 * where "filename" is the name of a PCX-format image file.
\r
85 *************************************************************************
\r
94 char *vers; /* Version number string pointer */
\r
95 char *pal_type; /* Palette type string pointer */
\r
96 int range; /* Palette range */
\r
97 int indicator; /* Extended palette indicator byte */
\r
98 BOOL status = TRUE; /* Return status */
\r
99 BOOL ep_flag = FALSE; /* Extended palette flag */
\r
100 FILE *fp; /* File pointer */
\r
101 PCX_HDR *hdrp; /* File header structure pointer */
\r
102 PCX_PAL *ext_palettep; /* Extended palette pointer */
\r
104 /* Display program title */
\r
106 puts("\nPCX_EXAM - PCX Image File Examination Utility\n");
\r
108 /* Check for filename */
\r
112 /* Display usage information */
\r
114 fputs(" Synopsis: This public domain utility displays inf", stderr);
\r
115 fputs("ormation contained in the\n header of a ", stderr);
\r
116 fputs("Paintbrush (R) PCX-format image file.\n\n Usage: ", stderr);
\r
117 fputs(" PCX_EXAM filename\n\n Example: PCX_EXAM picture", stderr);
\r
118 fputs(".pcx\n", stderr);
\r
123 /* Allocate a PCX file header buffer */
\r
125 if ((hdrp = (PCX_HDR *) malloc(sizeof(PCX_HDR))) == (PCX_HDR *) NULL)
\r
127 fputs("ERROR: out of memory\n", stderr);
\r
131 /* Open the PCX image file in binary mode */
\r
133 if ((fp = fopen(argv[1], "rb")) == (FILE *) NULL)
\r
135 fprintf(stderr, "ERROR: cannot open file %s\n", argv[1]);
\r
137 free(hdrp); /* Free the file header memory */
\r
142 /* Read the file header */
\r
144 if (status == TRUE)
\r
145 if (fseek(fp, 0L, SEEK_SET) != 0)
\r
147 fprintf(stderr, "ERROR: cannot read file %s\n", argv[1]);
\r
151 if (status == TRUE)
\r
152 if (fread(hdrp, sizeof(PCX_HDR), 1, fp) != 1)
\r
154 fprintf(stderr, "ERROR: cannot read file %s\n", argv[1]);
\r
158 /* Validate the PCX file format */
\r
160 if (status == TRUE)
\r
161 if ((hdrp->pcx_id != 0x0a) || (hdrp->encoding != 1))
\r
163 fprintf(stderr, "ERROR: file %s not valid PCX format\n", argv[1]);
\r
167 /* Determine the version number */
\r
169 switch (hdrp->version)
\r
173 vers = "PC Paintbrush 2.5";
\r
179 vers = "PC Paintbrush 2.8 (with palette information)";
\r
185 vers = "PC Paintbrush 2.8 (without palette information)";
\r
191 vers = "PC Paintbrush for Windows (not 3.0)";
\r
197 vers = "PC Paintbrush 3.0 and greater";
\r
203 vers = "Unknown version";
\r
208 /* Display the PCX file header information */
\r
210 printf("PCX filename: %s\n", argv[1]);
\r
211 printf("Version: %s\n", vers);
\r
212 printf("Encoding: %s\n", hdrp->encoding == 1 ? "Run length" :
\r
214 printf("%d bits per pixel\n", hdrp->bppixel);
\r
215 printf("Image from (%d, %d) to (%d, %d) pixels.\n", hdrp->xul,
\r
216 hdrp->yul, hdrp->xlr, hdrp->ylr);
\r
217 printf("Created on a device with %d x %d dpi resolution\n",
\r
218 hdrp->horz_res, hdrp->vert_res);
\r
219 printf("Number of color planes: %d\n", hdrp->nplanes);
\r
220 printf("Bytes per color plane scan line: %d\n", hdrp->bppscan);
\r
222 switch (hdrp->palette_type & PCX_PAL_MASK)
\r
226 pal_type = "color or B&W";
\r
232 pal_type = "grayscale";
\r
238 pal_type = "unknown";
\r
243 printf("Palette type: %s\n", pal_type);
\r
245 /* Check for extended (256-color) palette */
\r
247 if (hdrp->version == 5)
\r
249 /* Check for valid palette indicator byte */
\r
251 if (fseek(fp, -769L, SEEK_END) != 0)
\r
253 fprintf(stderr, "ERROR: cannot read file %s\n", argv[1]);
\r
257 if (status == TRUE)
\r
259 if ((indicator = getc(fp)) == PCX_EPAL_FLAG)
\r
261 /* Allocate an extended palette buffer */
\r
263 if ((ext_palettep = (PCX_PAL *) calloc(sizeof(PCX_PAL),
\r
264 PCX_EPAL_SIZE)) == (PCX_PAL *) NULL)
\r
266 fputs("ERROR: out of memory\n", stderr);
\r
270 /* Read the extended palette */
\r
272 if (status == TRUE)
\r
274 if (fread(ext_palettep, sizeof(PCX_PAL), PCX_EPAL_SIZE, fp) !=
\r
277 free(ext_palettep); /* Free extended palette buffer */
\r
282 ep_flag = TRUE; /* Indicate extended palette exists */
\r
287 if (status == TRUE)
\r
288 if (ep_flag == TRUE)
\r
290 /* Display extended (256-color) palette */
\r
292 puts("Extended 256-color palette:\n");
\r
294 puts("COLOR RED GREEN BLUE\n");
\r
296 for (range = 0; range < PCX_EPAL_SIZE; range++)
\r
297 printf(" %03d %2x %2x %2x\n", range,
\r
298 ext_palettep[range].red, ext_palettep[range].green,
\r
299 ext_palettep[range].blue);
\r
303 free(ext_palettep); /* Free the extended palette */
\r
307 /* Display file header palette */
\r
309 puts("File header color palette:\n");
\r
311 printf("RED ... ");
\r
313 for (range = 0; range < PCX_PAL_SIZE; range++)
\r
314 printf("%2x ", hdrp->palette[range].red);
\r
316 printf("\nGREEN ... ");
\r
318 for (range = 0; range < PCX_PAL_SIZE; range++)
\r
319 printf("%2x ", hdrp->palette[range].green);
\r
321 printf("\nBLUE ... ");
\r
323 for (range = 0; range < PCX_PAL_SIZE; range++)
\r
324 printf("%2x ", hdrp->palette[range].blue);
\r
329 if (fclose(fp) == EOF) /* Close the file */
\r
331 fprintf(stderr, "Error: cannot close file %s\n", argv[1]);
\r
335 free (hdrp); /* Free the file header buffer */
\r