]> 4ch.mooo.com Git - 16.git/blob - src/lib/bitmapl.c
16_ca needs huge amounts of work and I should remember what needs to be done soon...
[16.git] / src / lib / bitmapl.c
1 #include <stdio.h>\r
2 #include <stdlib.h>\r
3 #include "src/lib/bitmapl.h"\r
4 #include <malloc.h>\r
5 //#include "modex16.h"\r
6 \r
7 static struct pcxHeader {\r
8     byte id;\r
9     byte version;\r
10     byte encoding;\r
11     byte bpp;\r
12     word xmin;\r
13     word ymin;\r
14     word xmax;\r
15     word ymax;\r
16     word hres;\r
17     word vres;\r
18     byte pal16[48];\r
19     byte res1;\r
20     word bpplane;\r
21     word palType;\r
22     word hScreenSize;\r
23     word vScreenSize;\r
24     byte padding[54];\r
25 } head;\r
26 \r
27 byte *\r
28 modexNewPal() {\r
29     byte *ptr;\r
30     ptr = _fmalloc(768);\r
31 \r
32     /* handle errors */\r
33     if(!ptr) {\r
34         printf("Could not allocate palette.\n");\r
35         exit(-1);\r
36     }\r
37 \r
38     return ptr;\r
39 }\r
40 \r
41 static void loadPcxStage1(FILE *file, bitmap_t *result) {\r
42 //    long bufSize;\r
43 //    int index;\r
44 //    byte count, val;\r
45 //    long int pos;\r
46 \r
47     /* read the header */\r
48     fread(&head, sizeof(char), sizeof(struct pcxHeader), file);\r
49 \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
53 \r
54     /* make sure this  is 8bpp */\r
55     if(head.bpp != 8) {\r
56         printf("I only know how to handle 8bpp pcx files!\n");\r
57         fclose(file);\r
58         exit(-2);\r
59     }\r
60 }\r
61 \r
62 \r
63 static void loadPcxPalette(FILE *file, bitmap_t *result) {\r
64     byte val;\r
65     int index;\r
66 \r
67     /* handle the palette */\r
68     fseek(file, -769, SEEK_END);\r
69     val = fgetc(file);\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
74             val = fgetc(file);\r
75             result->palette[index] = val >> 2;\r
76         }\r
77     } else {\r
78         /* use the 16 color palette */\r
79         for(index=0; index<48; index++) {\r
80             result->palette[index]  = head.pal16[index];\r
81         }\r
82     }\r
83 }\r
84 \r
85 \r
86 bitmap_t\r
87 bitmapLoadPcx(char *filename) {\r
88     FILE *file;\r
89     bitmap_t result;\r
90     long bufSize;\r
91     int index;\r
92     byte count, val;\r
93 \r
94     /* open the PCX file for reading */\r
95     file = fopen(filename, "rb");\r
96     if(!file) {\r
97         printf("Could not open %s for reading.\n", filename);\r
98         //return result;//exit(-2);\r
99     }\r
100 \r
101     /* load the first part of the pcx file */\r
102     loadPcxStage1(file, &result);\r
103 \r
104     /* allocate the buffer */\r
105     bufSize = result.width * result.height;\r
106     result.data = malloc(bufSize);\r
107     if(!result.data) {\r
108         printf("Could not allocate memory for bitmap data.");\r
109         fclose(file);\r
110         return result;//exit(-1);\r
111     }\r
112 \r
113     /*  read the buffer in */\r
114     index = 0;\r
115     do {\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
119             count &= 0x3f;\r
120             val = fgetc(file);\r
121         } else {\r
122             val = count;\r
123             count = 1;\r
124         }\r
125 \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
129         }\r
130     } while(index < bufSize);\r
131 \r
132     loadPcxPalette(file, &result);\r
133 \r
134     fclose(file);\r
135 \r
136     return result;\r
137 }\r
138 \r
139 \r
140 tileset_t\r
141 bitmapLoadPcxTiles(char *filename, word twidth, word theight) {\r
142     tileset_t ts;\r
143     FILE *file;\r
144     bitmap_t result;\r
145     int i;\r
146 \r
147     /* open the PCX file for reading */\r
148     file = fopen(filename, "rb");\r
149     if(!file) {\r
150         printf("Could not open %s for reading.\n", filename);\r
151         exit(-2);\r
152     }\r
153 \r
154     /* load the first part of the pcx file */\r
155     loadPcxStage1(file, &result);\r
156 \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
162 \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
168     }\r
169 \r
170     /* finish off the file */\r
171     loadPcxPalette(file, &result);\r
172 \r
173     fclose(file);\r
174 \r
175     return ts;\r
176 }\r