word hScreenSize;\r
word vScreenSize;\r
byte padding[54];\r
-};\r
+} head;\r
\r
\r
static void loadPcxStage1(FILE *file, bitmap_t *result) {\r
- struct pcxHeader head;\r
long bufSize;\r
int index;\r
byte count, val;\r
/* read the header */\r
fread(&head, sizeof(char), sizeof(struct pcxHeader), file);\r
\r
+ /* get the width and height */\r
+ result->width = head.xmax - head.xmin + 1;\r
+ result->height = head.ymax - head.ymin + 1;\r
+\r
/* make sure this is 8bpp */\r
if(head.bpp != 8) {\r
printf("I only know how to handle 8bpp pcx files!\n");\r
fclose(file);\r
exit(-2);\r
}\r
+}\r
\r
- /* allocate the buffer */\r
- result->width = head.xmax - head.xmin + 1;\r
- result->height = head.ymax - head.ymin + 1;\r
- bufSize = result->width * result->height;\r
- result->data = malloc(bufSize);\r
- if(!result->data) {\r
- printf("Could not allocate memory for bitmap data.");\r
- fclose(file);\r
- exit(-1);\r
- }\r
\r
- /* save the position of the pixel data */\r
- pos = ftell(file);\r
+static void loadPcxPalette(FILE *file, bitmap_t *result) {\r
+ byte val;\r
+ int index;\r
\r
/* handle the palette */\r
fseek(file, -769, SEEK_END);\r
result->palette[index] = head.pal16[index];\r
}\r
}\r
-\r
- /* go back to the pixel data */\r
- fseek(file, -pos, SEEK_END);\r
}\r
\r
\r
bitmapLoadPcx(char *filename) {\r
FILE *file;\r
bitmap_t result;\r
- struct pcxHeader head;\r
long bufSize;\r
int index;\r
byte count, val;\r
/* load the first part of the pcx file */\r
loadPcxStage1(file, &result);\r
\r
+ /* allocate the buffer */\r
+ bufSize = result.width * result.height;\r
+ result.data = malloc(bufSize);\r
+ if(!result.data) {\r
+ printf("Could not allocate memory for bitmap data.");\r
+ fclose(file);\r
+ exit(-1);\r
+ }\r
+\r
/* read the buffer in */\r
index = 0;\r
do {\r
}\r
} while(index < bufSize);\r
\r
+ loadPcxPalette(file, &result);\r
\r
fclose(file);\r
\r