3 #include "src/lib/bitmapl.h"
\r
5 //#include "modex16.h"
\r
7 static struct pcxHeader {
\r
30 ptr = _fmalloc(768);
\r
34 printf("Could not allocate palette.\n");
\r
41 static void loadPcxStage1(FILE *file, bitmap_t *result) {
\r
47 /* read the header */
\r
48 fread(&head, sizeof(char), sizeof(struct pcxHeader), file);
\r
50 /* get the width and height */
\r
51 result->width = head.xmax - head.xmin + 1;
\r
52 result->height = head.ymax - head.ymin + 1;
\r
54 /* make sure this is 8bpp */
\r
56 printf("I only know how to handle 8bpp pcx files!\n");
\r
63 static void loadPcxPalette(FILE *file, bitmap_t *result) {
\r
67 /* handle the palette */
\r
68 fseek(file, -769, SEEK_END);
\r
70 result->palette = modexNewPal();
\r
71 if(head.version == 5 && val == 12) {
\r
72 /* use the vga palette */
\r
73 for(index=0; !feof(file) && index < 768; index++) {
\r
75 result->palette[index] = val >> 2;
\r
78 /* use the 16 color palette */
\r
79 for(index=0; index<48; index++) {
\r
80 result->palette[index] = head.pal16[index];
\r
87 bitmapLoadPcx(char *filename) {
\r
94 /* open the PCX file for reading */
\r
95 file = fopen(filename, "rb");
\r
97 printf("Could not open %s for reading.\n", filename);
\r
98 //return result;//exit(-2);
\r
101 /* load the first part of the pcx file */
\r
102 loadPcxStage1(file, &result);
\r
104 /* allocate the buffer */
\r
105 bufSize = result.width * result.height;
\r
106 result.data = malloc(bufSize);
\r
108 printf("Could not allocate memory for bitmap data.");
\r
110 return result;//exit(-1);
\r
113 /* read the buffer in */
\r
116 /* get the run length and the value */
\r
117 count = fgetc(file);
\r
118 if(0xC0 == (count & 0xC0)) { /* this is the run count */
\r
126 /* write the pixel the specified number of times */
\r
127 for(; count && index < bufSize; count--,index++) {
\r
128 result.data[index] = val;
\r
130 } while(index < bufSize);
\r
132 loadPcxPalette(file, &result);
\r
141 bitmapLoadPcxTiles(char *filename, word twidth, word theight) {
\r
147 /* open the PCX file for reading */
\r
148 file = fopen(filename, "rb");
\r
150 printf("Could not open %s for reading.\n", filename);
\r
154 /* load the first part of the pcx file */
\r
155 loadPcxStage1(file, &result);
\r
157 /* get the number of tiles and set up the result structure */
\r
158 ts.twidth = twidth;
\r
159 ts.theight = theight;
\r
160 ts.ntiles = (result.width/twidth) * (result.height/theight);
\r
161 ts.palette = result.palette;
\r
163 /* allocate the pixel storage for the tiles */
\r
164 ts.data = _fmalloc(sizeof(byte*) * ts.ntiles);
\r
165 ts.data[0] = _fmalloc(sizeof(byte) * ts.ntiles * twidth * theight);
\r
166 for(i=1; i < ts.ntiles; i++) {
\r
167 ts.data[i] = ts.data[i-1] + twidth * theight;
\r
170 /* finish off the file */
\r
171 loadPcxPalette(file, &result);
\r