4 #include "src/lib/bitmap.h"
\r
5 #include "src/lib/modex16.h"
\r
7 static struct pcxHeader {
\r
28 static void loadPcxStage1(FILE *file, bitmap_t *result) {
\r
34 /* read the header */
\r
35 fread(&head, sizeof(char), sizeof(struct pcxHeader), file);
\r
37 /* get the width and height */
\r
38 result->width = head.xmax - head.xmin + 1;
\r
39 result->height = head.ymax - head.ymin + 1;
\r
41 /* make sure this is 8bpp */
\r
43 printf("I only know how to handle 8bpp pcx files!\n");
\r
50 static void loadPcxPalette(FILE *file, bitmap_t *result) {
\r
54 /* handle the palette */
\r
55 fseek(file, -769, SEEK_END);
\r
57 result->palette = modexNewPal();
\r
58 if(head.version == 5 && val == 12) {
\r
59 /* use the vga palette */
\r
60 for(index=0; !feof(file) && index < PAL_SIZE; index++) {
\r
62 result->palette[index] = val >> 2;
\r
65 /* use the 16 color palette */
\r
66 for(index=0; index<48; index++) {
\r
67 result->palette[index] = head.pal16[index];
\r
74 bitmapLoadPcx(char *filename) {
\r
81 /* open the PCX file for reading */
\r
82 file = fopen(filename, "rb");
\r
84 printf("Could not open %s for reading.\n", filename);
\r
88 /* load the first part of the pcx file */
\r
89 loadPcxStage1(file, &result);
91 /* allocate the buffer */
92 //printf("%zu\n", _memmax());
\r
93 bufSize = (/*(dword)*/result.width * result.height);
94 result.data = malloc(bufSize);
95 // result.data = (byte far *)_fmalloc(bufSize);
96 // result.data = (byte __huge *)halloc(bufSize, sizeof(byte));
97 /*printf("&bufSize=%p\n", &bufSize);
98 printf("&result.data=%p\n", result.data);
99 printf("Size of block is %zu bytes\n", _msize(result.data));
100 printf("Size of bufSize is %zu bytes\n", bufSize);
101 printf("Size of result.width is %zu \n", result.width);
102 printf("Size of result.height is %zu \n", result.height);
103 printf("Dimensions of result is %lu\n", (dword)result.width*result.height);*/
106 fprintf(stderr, "Could not allocate memory for bitmap data.");
\r
111 /* read the buffer in */
\r
114 /* get the run length and the value */
\r
115 count = fgetc(file);
\r
116 if(0xC0 == (count & 0xC0)) { /* this is the run count */
\r
124 /* write the pixel the specified number of times */
\r
125 for(; count && index < bufSize; count--,index++) {
\r
126 result.data[index] = val;
\r
128 } while(index < bufSize);
\r
130 loadPcxPalette(file, &result);
\r
139 bitmapLoadPcxTiles(char *filename, word twidth, word theight) {
\r
145 /* open the PCX file for reading */
\r
146 file = fopen(filename, "rb");
\r
148 printf("Could not open %s for reading.\n", filename);
\r
152 /* load the first part of the pcx file */
\r
153 loadPcxStage1(file, &result);
\r
155 /* get the number of tiles and set up the result structure */
\r
156 ts.twidth = twidth;
\r
157 ts.theight = theight;
\r
158 ts.ntiles = (result.width/twidth) * (result.height/theight);
\r
159 ts.palette = result.palette;
\r
161 /* allocate the pixel storage for the tiles */
\r
162 ts.data = malloc(sizeof(byte*) * ts.ntiles);
\r
163 ts.data[0] = malloc(sizeof(byte) * ts.ntiles * twidth * theight);
\r
164 for(i=1; i < ts.ntiles; i++) {
\r
165 ts.data[i] = ts.data[i-1] + twidth * theight;
\r
168 /* finish off the file */
\r
169 loadPcxPalette(file, &result);
\r