]> 4ch.mooo.com Git - 16.git/blob - src/lib/bitmap.c
fack!! oh well~
[16.git] / src / lib / bitmap.c
1 #include <stdio.h>\r
2 #include <stdlib.h>
3 #include <malloc.h>
4 #include "src/lib/bitmap.h"\r
5 #include "src/lib/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 \r
28 static void loadPcxStage1(FILE *file, bitmap_t *result) {\r
29     long bufSize;\r
30     int index;\r
31     byte count, val;\r
32     long int pos;\r
33 \r
34     /* read the header */\r
35     fread(&head, sizeof(char), sizeof(struct pcxHeader), file);\r
36 \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
40 \r
41     /* make sure this  is 8bpp */\r
42     if(head.bpp != 8) {\r
43                 printf("I only know how to handle 8bpp pcx files!\n");\r
44                 fclose(file);\r
45                 exit(-2);\r
46     }\r
47 }\r
48 \r
49 \r
50 static void loadPcxPalette(FILE *file, bitmap_t *result) {\r
51     byte val;\r
52     int index;\r
53 \r
54     /* handle the palette */\r
55     fseek(file, -769, SEEK_END);\r
56     val = fgetc(file);\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
61             val = fgetc(file);\r
62             result->palette[index] = val >> 2;\r
63         }\r
64     } else {\r
65         /* use the 16 color palette */\r
66         for(index=0; index<48; index++) {\r
67             result->palette[index]  = head.pal16[index];\r
68         }\r
69     }\r
70 }\r
71 \r
72 \r
73 bitmap_t\r
74 bitmapLoadPcx(char *filename) {\r
75     FILE *file;\r
76     bitmap_t result;\r
77     dword bufSize;\r
78     int index;\r
79     byte count, val;
80 \r
81     /* open the PCX file for reading */\r
82     file = fopen(filename, "rb");\r
83     if(!file) {\r
84                 printf("Could not open %s for reading.\n", filename);\r
85                 exit(-2);\r
86     }\r
87 \r
88     /* load the first part of the pcx file */\r
89     loadPcxStage1(file, &result);
90 \r
91     /* allocate the buffer */\r
92         bufSize = (result.width * result.height);
93         result.data = malloc((bufSize));
94         /*0000printf("Size of block is %u bytes\n", _msize(result.data));
95         printf("Size of bufSize is %lu bytes\n", bufSize);
96         printf("Size of result.width is %lu \n", result.width);
97         printf("Size of result.height is %lu \n", result.height);
98         printf("Dimensions of result is %lu\n", result.width*result.height);*/
99         //exit(0);
100         if(!result.data) {
101                 fprintf(stderr, "Could not allocate memory for bitmap data.");\r
102                 fclose(file);\r
103                 exit(-1);\r
104         }\r
105 \r
106     /*  read the buffer in */\r
107     index = 0;\r
108     do {\r
109         /* get the run length and the value */\r
110         count = fgetc(file);\r
111         if(0xC0 ==  (count & 0xC0)) { /* this is the run count */\r
112                 count &= 0x3f;\r
113                 val = fgetc(file);\r
114         } else {\r
115                 val = count;\r
116                 count = 1;\r
117         }\r
118 \r
119         /* write the pixel the specified number of times */\r
120         for(; count && index < bufSize; count--,index++)  {\r
121                 result.data[index] = val;\r
122         }\r
123     } while(index < bufSize);\r
124 \r
125     loadPcxPalette(file, &result);\r
126 \r
127     fclose(file);\r
128 \r
129     return result;\r
130 }\r
131 \r
132 //update!!\r
133 tileset_t\r
134 bitmapLoadPcxTiles(char *filename, word twidth, word theight) {\r
135     tileset_t ts;\r
136     FILE *file;\r
137     bitmap_t result;\r
138     int i;\r
139 \r
140     /* open the PCX file for reading */\r
141     file = fopen(filename, "rb");\r
142         if(!file) {\r
143                 printf("Could not open %s for reading.\n", filename);\r
144                 exit(-2);\r
145         }\r
146 \r
147     /* load the first part of the pcx file */\r
148     loadPcxStage1(file, &result);\r
149 \r
150     /* get the number of tiles and set up the result structure */\r
151     ts.twidth = twidth;\r
152     ts.theight = theight;\r
153     ts.ntiles = (result.width/twidth) * (result.height/theight);\r
154     ts.palette = result.palette;\r
155 \r
156         /* allocate the pixel storage for the tiles */\r
157         ts.data = malloc(sizeof(byte*) * ts.ntiles);\r
158         ts.data[0] = malloc(sizeof(byte) * ts.ntiles * twidth * theight);\r
159         for(i=1; i < ts.ntiles; i++) {\r
160                 ts.data[i] = ts.data[i-1] + twidth * theight;\r
161         }\r
162 \r
163     /* finish off the file */\r
164     loadPcxPalette(file, &result);\r
165 \r
166     fclose(file);\r
167 \r
168     return ts;\r
169 }\r