6 static struct pcxHeader {
\r
27 static void loadPcxStage1(FILE *file, bitmap_t *result) {
\r
33 /* read the header */
\r
34 fread(&head, sizeof(char), sizeof(struct pcxHeader), file);
\r
36 /* get the width and height */
\r
37 result->width = head.xmax - head.xmin + 1;
\r
38 result->height = head.ymax - head.ymin + 1;
\r
40 /* make sure this is 8bpp */
\r
42 printf("I only know how to handle 8bpp pcx files!\n");
\r
49 static void loadPcxPalette(FILE *file, bitmap_t *result) {
\r
53 /* handle the palette */
\r
54 fseek(file, -769, SEEK_END);
\r
56 result->palette = modexNewPal();
\r
57 if(head.version == 5 && val == 12) {
\r
58 /* use the vga palette */
\r
59 for(index=0; !feof(file) && index < PAL_SIZE; index++) {
\r
61 result->palette[index] = val >> 2;
\r
64 /* use the 16 color palette */
\r
65 for(index=0; index<48; index++) {
\r
66 result->palette[index] = head.pal16[index];
\r
73 bitmapLoadPcx(char *filename) {
\r
80 /* open the PCX file for reading */
\r
81 file = fopen(filename, "rb");
\r
83 printf("Could not open %s for reading.\n", filename);
\r
87 /* load the first part of the pcx file */
\r
88 loadPcxStage1(file, &result);
\r
90 /* allocate the buffer */
\r
91 bufSize = result.width * result.height;
\r
92 result.data = malloc(bufSize);
\r
94 printf("Could not allocate memory for bitmap data.");
\r
99 /* read the buffer in */
\r
102 /* get the run length and the value */
\r
103 count = fgetc(file);
\r
104 if(0xC0 == (count & 0xC0)) { /* this is the run count */
\r
112 /* write the pixel the specified number of times */
\r
113 for(; count && index < bufSize; count--,index++) {
\r
114 result.data[index] = val;
\r
116 } while(index < bufSize);
\r
118 loadPcxPalette(file, &result);
\r
127 bitmapLoadPcxTiles(char *filename, word twidth, word theight) {
\r
133 /* open the PCX file for reading */
\r
134 file = fopen(filename, "rb");
\r
136 printf("Could not open %s for reading.\n", filename);
\r
140 /* load the first part of the pcx file */
\r
141 loadPcxStage1(file, &result);
\r
143 /* get the number of tiles and set up the result structure */
\r
144 ts.twidth = twidth;
\r
145 ts.theight = theight;
\r
146 ts.ntiles = (result.width/twidth) * (result.height/theight);
\r
147 ts.palette = result.palette;
\r
149 /* allocate the pixel storage for the tiles */
\r
150 ts.data = malloc(sizeof(byte*) * ts.ntiles);
\r
151 ts.data[0] = malloc(sizeof(byte) * ts.ntiles * twidth * theight);
\r
152 for(i=1; i < ts.ntiles; i++) {
\r
153 ts.data[i] = ts.data[i-1] + twidth * theight;
\r
156 /* finish off the file */
\r
157 loadPcxPalette(file, &result);
\r