Debian Codegraph @

src/gfx/bmp/bmp2pth.c

raw symbols SCIP
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <stdint.h>
5
6 #define OUTPUT_PAD_SIZE 20
7 #define RLE_ID 0xCC // DO NOT change this!
8
9 static char input[4096 + 1];
10 static uint8_t *ToPtr, *DataPtr, *a0, *a1;
11 static int32_t DataLen, CompLen, d0, d1, d4, d7;
12
13 int main(int argc, char *argv[])
14 {
15 char bitmap_magic[2], *out_filename;
16 uint8_t p8_1, p8_2, p8_3, p8_4;
17 int32_t bitmap_width, bitmap_height, bitmap_depth, bitmap_offset;
18 int32_t x, y, i, oy, t;
19 uint32_t *pixel_data, p32_1, p32_2, p32_3, p32_4;
20 FILE *in, *out;
21
22 #ifndef _DEBUG
23 if (argc != 2)
24 {
25 printf("Usage: bmp2pth <bitmap.bmp>\n");
26 return -1;
27 }
28
29 strcpy(input, argv[1]);
30 #else
31 strcpy(input, "debug.bmp");
32 #endif
33
34 in = fopen(input, "rb");
35 if (in == NULL)
36 {
37 printf("ERROR: Could not open input bitmap!\n");
38 system("PAUSE");
39 return 1;
40 }
41
42 out_filename = (char *)malloc(strlen(input) + 3);
43 if (out_filename == NULL)
44 {
45 printf("ERROR: Out of memory!\n");
46 fclose(in);
47 system("PAUSE");
48 return 1;
49 }
50
51 sprintf(out_filename, "%s.c", input);
52
53 out = fopen(out_filename, "w");
54 if (out == NULL)
55 {
56 printf("ERROR: Could not open output bitmap!\n");
57 free(out_filename);
58 fclose(in);
59 system("PAUSE");
60 return 1;
61 }
62
63 fread(bitmap_magic, 1, 2, in);
64 if (strncmp(bitmap_magic, "BM", 2) != 0)
65 {
66 printf("ERROR: Input is not a valid Windows bitmap!\n");
67 fclose(in);
68 fclose(out);
69 system("PAUSE");
70 return 1;
71 }
72
73 fseek(in, 0x12, SEEK_SET);
74 fread(&bitmap_width, 4, 1, in);
75 fread(&bitmap_height, 4, 1, in);
76 fseek(in, 0x1C, SEEK_SET);
77 fread(&bitmap_depth, 4, 1, in);
78 bitmap_depth &= 0xFF;
79 fseek(in, 0x0A, SEEK_SET);
80 fread(&bitmap_offset, 4, 1, in);
81 fseek(in, bitmap_offset, SEEK_SET);
82
83 if (bitmap_depth != 16 && bitmap_depth != 24 && bitmap_depth != 32)
84 {
85 printf("ERROR: Bitmap is not truecolor bitmap (16, 24, 32)\n");
86
87 fclose(in);
88 fclose(out);
89 system("PAUSE");
90
91 return 1;
92 }
93
94 pixel_data = (uint32_t *)malloc(bitmap_width * bitmap_height * (bitmap_depth / 8));
95 if (pixel_data == NULL)
96 {
97 printf("ERROR: Out of memory!\n");
98
99 fclose(in);
100 fclose(out);
101 system("PAUSE");
102
103 return 1;
104 }
105
106 if (fread(pixel_data, bitmap_depth / 8, bitmap_width * bitmap_height, in) != (size_t)(bitmap_width * bitmap_height))
107 {
108 printf("ERROR: Could not copy bitmap data into memory!\n");
109
110 fclose(in);
111 fclose(out);
112 free(pixel_data);
113
114 system("PAUSE");
115
116 return 1;
117 }
118
119 // mirror pixel data so we get the real deal
120 for (y = 0; y < bitmap_height/2; y++)
121 {
122 for (x = 0; x < bitmap_width; x++)
123 {
124 oy = bitmap_height - 1 - y;
125 t = pixel_data[y * bitmap_width + x];
126
127 pixel_data[y * bitmap_width + x] = pixel_data[oy * bitmap_width + x];
128 pixel_data[oy * bitmap_width + x] = t;
129 }
130 }
131
132 DataLen = (bitmap_width * bitmap_height) / 4;
133 DataPtr = (uint8_t *)malloc(DataLen);
134
135 for (i = 0; i < DataLen; i++)
136 {
137 p32_1 = pixel_data[(i * 4) + 0];
138 p32_2 = pixel_data[(i * 4) + 1];
139 p32_3 = pixel_data[(i * 4) + 2];
140 p32_4 = pixel_data[(i * 4) + 3];
141
142 if (p32_1 == 0x000000) p8_1 = 0;
143 else if (p32_1 == 0xBBBBBB) p8_1 = 1;
144 else if (p32_1 == 0x888888) p8_1 = 2;
145 else if (p32_1 == 0x555555) p8_1 = 3;
146 else
147 {
148 printf("ERROR: Illegal pixel 0x%08X found. Stopping!\n", p32_1);
149 free(pixel_data);
150 free(DataPtr);
151 system("PAUSE");
152 return 1;
153 }
154
155 if (p32_2 == 0x000000) p8_2 = 0;
156 else if (p32_2 == 0xBBBBBB) p8_2 = 1;
157 else if (p32_2 == 0x888888) p8_2 = 2;
158 else if (p32_2 == 0x555555) p8_2 = 3;
159 else
160 {
161 printf("ERROR: Illegal pixel 0x%08X found. Stopping!\n", p32_2);
162 free(pixel_data);
163 free(DataPtr);
164 fclose(out);
165 system("PAUSE");
166 return (1);
167 }
168
169 if (p32_3 == 0x000000) p8_3 = 0;
170 else if (p32_3 == 0xBBBBBB) p8_3 = 1;
171 else if (p32_3 == 0x888888) p8_3 = 2;
172 else if (p32_3 == 0x555555) p8_3 = 3;
173 else
174 {
175 printf("ERROR: Illegal pixel 0x%08X found. Stopping!\n", p32_3);
176 free(pixel_data);
177 free(DataPtr);
178 fclose(out);
179 system("PAUSE");
180 return 1;
181 }
182
183 if (p32_4 == 0x000000) p8_4 = 0;
184 else if (p32_4 == 0xBBBBBB) p8_4 = 1;
185 else if (p32_4 == 0x888888) p8_4 = 2;
186 else if (p32_4 == 0x555555) p8_4 = 3;
187 else
188 {
189 printf("ERROR: Illegal pixel 0x%08X found. Stopping!\n", p32_4);
190 free(pixel_data);
191 free(DataPtr);
192 fclose(out);
193 system("PAUSE");
194 return 1;
195 }
196
197 DataPtr[i] = (p8_1 << 6) | (p8_2 << 4) | (p8_3 << 2) | p8_4;
198 }
199
200 free(pixel_data);
201
202 // The following mess is a direct 68k asm of ptcompactor.s found in
203 // the ProTracker 1.3 source code archive.
204 goto Main;
205
206 JustCode:
207 *a1++ = RLE_ID; // Output compacter code
208 *a1++ = 0; // Output zero
209 *a1++ = RLE_ID; // Output compacter code
210 goto NextByte; // Do next byte
211
212 Equal:
213 d1 = d0;
214 d4++; // Add one to equal-count
215 if (d4 >= 255) // 255 or more?
216 goto FlushBytes; // Yes, flush buffer
217 goto NextByte; // Do next byte
218
219 FlushBytes:
220 if (d4 >= 3) // 4 or more
221 goto FourOrMore; // Yes, output codes
222 NotFour:
223 *a1++ = (uint8_t)d1; // Output byte
224 if (--d4 != -1) goto NotFour; // Loop...
225 d4 = 0; // Zero count
226 goto NextByte; // Another byte
227 FourOrMore:
228 *a1++ = RLE_ID; // Output compacter code
229 *a1++ = (uint8_t)d4; // Output count
230 *a1++ = (uint8_t)d1; // Output byte
231 d4 = 0; // Zero count
232 d0++;
233 goto NextByte; // Do next byte
234
235 Main:
236 ToPtr = (uint8_t *)malloc(DataLen);
237 a0 = DataPtr; // From ptr.
238 a1 = ToPtr; // To ptr.
239 d7 = DataLen; // Length
240 d4 = 0; // Clear count
241
242 EqLoop:
243 d0 = *a0++; // Get a byte
244 if (d0 == RLE_ID) // Same as compacter code?
245 goto JustCode; // Output JustCode
246
247 if (d7 == 1)
248 goto endskip;
249
250 if (d0 == *a0) // Same as previous byte?
251 goto Equal; // Yes, it was equal
252 endskip:
253 if (d4 > 0) // Not equal, any equal buffered?
254 goto FlushBytes; // Yes, output them
255 *a1++ = (uint8_t)d0; // Output byte
256 d4 = 0;
257 NextByte:
258 d7--; // Subtract 1 from length
259 if (d7 > 0) goto EqLoop; // Loop until length = 0
260
261 if (d4 == 0) // Any buffered bytes?
262 goto endok; // No, goto end
263
264 if (d4 >= 3) // More than 4?
265 goto FourOrMore2; // Yes, skip
266 NotFour2:
267 *a1++ = (uint8_t)d0; // Output byte
268 if (--d4 != -1) goto NotFour2; // Loop...
269 goto endok; // Goto end;
270 FourOrMore2:
271 *a1++ = RLE_ID; // Output compacter code
272 *a1++ = (uint8_t)d4; // Output count
273 *a1++ = (uint8_t)d0; // Output byte
274 endok:
275 free(DataPtr);
276
277 CompLen = (uint32_t)a1 - (uint32_t)ToPtr;
278
279 fprintf(out, "#include <stdint.h>\n\n");
280
281 fprintf(out, "// Final unpack length: %d\n", DataLen * 4);
282 fprintf(out, "// Decoded length: %d (first four bytes of buffer)\n", DataLen);
283 fprintf(out, "const uint8_t renameMe[%d] =\n{\n", CompLen + 4);
284 fprintf(out, "\t0x%02X,0x%02X,0x%02X,0x%02X,",
285 (DataLen & 0xFF000000) >> 24, (DataLen & 0x00FF0000) >> 16,
286 (DataLen & 0x0000FF00) >> 8, DataLen & 0x000000FF);
287
288 for (i = 4; i < CompLen+4; i++)
289 {
290 if (i == CompLen+3) fprintf(out, "0x%02X\n};\n", ToPtr[i-4]);
291 else if ((i % OUTPUT_PAD_SIZE) == 0) fprintf(out, "\t0x%02X,", ToPtr[i-4]);
292 else if ((i % OUTPUT_PAD_SIZE) == OUTPUT_PAD_SIZE-1) fprintf(out, "0x%02X,\n", ToPtr[i-4]);
293 else fprintf(out, "0x%02X,", ToPtr[i-4]);
294 }
295
296 free(ToPtr);
297 fclose(out);
298
299 printf("Done successfully.\n");
300 return 0;
301 }