Debian Codegraph @

support/splicebmp/splicebmp.cpp

raw symbols SCIP
1 // splicebmp.cpp : Defines the entry point for the console application.
2 //
3
4 #include "stdafx.h"
5
6 #include <iostream>
7 #include <fstream>
8
9 using namespace std;
10
11 //
12 // This key is from line 384 to line 384+15 of gfx/classic/dungeon16_16.c
13 // It is the picture of the well. It occasionally changes with palette
14 // shifts.
15 //
16 const int image_keylen = 8*8*2*2;
17 const unsigned char image_key[image_keylen] =
18 {
19 0x02, 0x02, 0x02, 0x03, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x02, 0x02, 0x02, 0x02,
20 0x03, 0x03, 0x03, 0x03, 0x0a, 0x0b, 0x0a, 0x0a, 0x02, 0x02, 0x02, 0x0a, 0x12, 0x0a, 0x12, 0x12,
21 0x02, 0x02, 0x0a, 0x12, 0x07, 0x07, 0x07, 0x07, 0x02, 0x02, 0x0a, 0x0b, 0x07, 0x07, 0x07, 0x07,
22 0x02, 0x02, 0x0b, 0x0a, 0x0b, 0x0a, 0x0b, 0x0b, 0x03, 0x03, 0x0b, 0x0a, 0x0a, 0x0b, 0x0a, 0x0a,
23 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x02, 0x02,
24 0x01, 0x0b, 0x0a, 0x03, 0x03, 0x03, 0x03, 0x03, 0x12, 0x0a, 0x12, 0x0a, 0x02, 0x02, 0x02, 0x02,
25 0x07, 0x07, 0x07, 0x12, 0x0b, 0x02, 0x02, 0x02, 0x07, 0x07, 0x07, 0x0a, 0x0b, 0x02, 0x02, 0x02,
26 0x0a, 0x0b, 0x0a, 0x0a, 0x0b, 0x02, 0x02, 0x02, 0x0b, 0x0a, 0x0b, 0x0b, 0x0a, 0x03, 0x03, 0x03,
27 0x02, 0x02, 0x0b, 0x0a, 0x0b, 0x0a, 0x0a, 0x0b, 0x02, 0x02, 0x0a, 0x0b, 0x0a, 0x0b, 0x0a, 0x0b,
28 0x02, 0x02, 0x0a, 0x0b, 0x0a, 0x0a, 0x0b, 0x0a, 0x02, 0x02, 0x0b, 0x0a, 0x0b, 0x0a, 0x0b, 0x0a,
29 0x03, 0x03, 0x03, 0x0a, 0x0a, 0x0b, 0x0a, 0x0b, 0x02, 0x02, 0x02, 0x03, 0x0a, 0x0b, 0x0a, 0x0a,
30 0x02, 0x02, 0x02, 0x03, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x02, 0x02, 0x02, 0x02,
31 0x0a, 0x0a, 0x0b, 0x0a, 0x0b, 0x02, 0x03, 0x02, 0x0b, 0x0a, 0x0b, 0x0a, 0x0a, 0x02, 0x03, 0x02,
32 0x0a, 0x0b, 0x0a, 0x0b, 0x0a, 0x02, 0x03, 0x02, 0x0a, 0x0b, 0x0a, 0x0b, 0x0a, 0x02, 0x03, 0x02,
33 0x0b, 0x0a, 0x0a, 0x0b, 0x03, 0x03, 0x03, 0x03, 0x0a, 0x0b, 0x0a, 0x02, 0x02, 0x03, 0x02, 0x02,
34 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x03, 0x02, 0x02,
35 };
36
37 const int palette_keylen = 16;
38 const unsigned char palette_key[palette_keylen] =
39 {
40 0x00, 0x00,
41 0xef, 0x3d,
42 0x10, 0x00,
43 0x1f, 0x00,
44 0x10, 0x02,
45 0xff, 0x03,
46 0x00, 0x40,
47 0x00, 0x7c
48 };
49
50 int
51 findOffset(const unsigned char *data, int datalen, const unsigned char *key, int keylen)
52 {
53 int off, i;
54
55 for (off = 0; off < datalen - keylen; off++)
56 {
57 for (i = 0; i < keylen; i++)
58 {
59 if (data[i + off] != key[i])
60 {
61 // Mis match!
62 break;
63 }
64 }
65 if (i == keylen)
66 {
67 // Full match!
68 return off;
69 }
70 }
71
72 // No match
73 return -1;
74 }
75
76 #pragma pack(push, 1)
77
78 struct BMPHEAD
79 {
80 char id[2];
81 int size;
82 int reserved;
83 int headersize;
84 int infosize;
85 int width;
86 int depth;
87 short biplane;
88 short bits;
89 int comp;
90 int imagesize;
91 int xpelperm;
92 int ypelpm;
93 int clrused;
94 int clrimport;
95 };
96
97 #pragma pack(pop)
98
99 struct RGBQUAD
100 {
101 unsigned char blue;
102 unsigned char green;
103 unsigned char red;
104 unsigned char filler;
105 };
106
107 unsigned char *
108 loadbitmap(const char *name, unsigned char *palette, int &size)
109 {
110 ifstream is;
111 BMPHEAD head;
112
113 is.open(name, ios_base::in | ios_base::binary);
114
115 if (!is)
116 {
117 cerr << "Failure to open: " << name << endl;
118 return 0;
119 }
120
121 is.read((char *) &head, sizeof(BMPHEAD));
122
123 if (head.bits != 8)
124 {
125 cerr << "Head.bits is " << head.bits << endl;
126 cerr << "Cannot support non-256 colour bitmaps." << endl;
127 return 0;
128 }
129
130 unsigned char *result;
131
132 size = head.imagesize;
133 result = new unsigned char [size];
134
135 // Read the palette. Stored 4 bytes per entry
136 // so needs some processing
137 RGBQUAD filepalette[256];
138 int i;
139
140 is.read((char *) &filepalette, sizeof(RGBQUAD) * 256);
141 for (i = 0; i < 256; i++)
142 {
143 // Convert to 15 bit BGR.
144 // Bright green is made black.
145 if (filepalette[i].green == 0xff && !filepalette[i].red && !filepalette[i].blue)
146 filepalette[i].green = 0;
147
148 // Convert all to 5 bits from 8 bits.
149 filepalette[i].red >>= 3;
150 filepalette[i].green >>= 3;
151 filepalette[i].blue >>= 3;
152
153 // Account for endian issues & repack
154 palette[i*2+1] = (filepalette[i].blue << 2) | (filepalette[i].green >> 3);
155 palette[i*2] = (filepalette[i].green << 5) | filepalette[i].red;
156 }
157
158 // Read data. Assume width is 4 byte aligned.
159 is.read((char *)result, size);
160
161 return result;
162 }
163
164 void
165 buildpalettelut(unsigned char *palette_lut, unsigned char *newpal, unsigned char *oldpal)
166 {
167 // We wish to alter the new palette so it matches the old palette.
168 // To do this:
169 // 1) Find valid colours in old palette. (Count 0s from end)
170 // 2) Find LUT of each of new palette into old palette.
171 // 3) Write out unused colours.
172
173 int i, j, oldsize;
174
175 for (i = 255; i >= 0; i--)
176 {
177 if (oldpal[i*2] || oldpal[i*2-1])
178 {
179 // Valid palette,
180 break;
181 }
182 }
183
184 oldsize = i;
185
186 // palette_lut is all 0, for no mapping. Only valid 0 mapping
187 // is 0->0.
188 memset(palette_lut, 0, 256);
189 for (i = 1; i < 256; i++)
190 {
191 // Find match.
192 for (j = 1; j < oldsize; j++)
193 {
194 if (newpal[i*2] == oldpal[j*2] &&
195 newpal[i*2+1] == oldpal[j*2+1])
196 {
197 // Valid mapping
198 palette_lut[i] = j;
199 break;
200 }
201 }
202 }
203
204 // Now, create the new palette.
205 unsigned char tmppal[512];
206 // Clear out all of it.
207 memset(tmppal, 0, 512);
208
209 memcpy(tmppal, oldpal, oldsize * 2);
210
211 // Assign all unused ones from the new list.
212 j = oldsize;
213 for (i = 1; i < 256; i++)
214 {
215 if (j > 255)
216 break;
217
218 if (!palette_lut[i])
219 {
220 palette_lut[i] = j;
221 tmppal[j*2] = newpal[i*2];
222 tmppal[j*2+1] = newpal[i*2+1];
223 j++;
224 }
225 }
226
227 memcpy(newpal, tmppal, 512);
228 }
229
230 unsigned char *
231 tilify(const unsigned char *flat, int len, const unsigned char *palette_lut)
232 {
233 // This tilifies the flat bitmap into 2x2 clusters of 8x8 tiles.
234 int width = 384, bytesperrow, tileperrow = width / 16;
235 int tx, ty, sx, sy, x, y;
236 unsigned char *result;
237 int off = 0;
238 int th;
239
240 result = new unsigned char[len];
241
242 bytesperrow = 8 * 8 * 2 * 2 * tileperrow;
243
244 th = len / bytesperrow;
245 for (ty = 0; ty < th; ty++)
246 {
247 for (tx = 0; tx < 384 / 16; tx++)
248 {
249 for (sy = 0; sy < 2; sy++)
250 {
251 for (sx = 0; sx < 2; sx++)
252 {
253 for (y = 0; y < 8; y++)
254 {
255 for (x = 0; x < 8; x++)
256 {
257 result[off++] =
258 palette_lut[flat[ ((th - ty - 1) * 16 + (1-sy) * 8 + (7-y)) * width + (tx * 16 + sx * 8 + x) ]];
259 }
260 }
261 }
262 }
263 }
264 }
265
266 //memcpy(result, flat, off);
267 //memset(result, 0, 8*8*2*2);
268
269 return result;
270 }
271
272 void splicedata(unsigned char *dst, int offset, unsigned char *src, int len)
273 {
274 int i;
275
276 for (i = 0; i < len; i++)
277 {
278 dst[offset+i] = src[i];
279 }
280 }
281
282
283 int main(int argc, char* argv[])
284 {
285 ifstream is;
286
287 cout << "splicebmp: Version 002" << endl;
288
289 is.open("powder.gba", ios_base::in | ios_base::binary);
290
291 if (!is)
292 {
293 cerr << "Unable to open \"powder.gba\", aborting." << endl;
294 return -1;
295 }
296
297 // Read in the original file.
298 unsigned char *powdergba;
299 int powderlen;
300
301 is.seekg(0, ios_base::end);
302 powderlen = is.tellg();
303
304 powdergba = new unsigned char [powderlen];
305
306 is.seekg(0, ios_base::beg);
307 is.read((char *)powdergba, powderlen);
308
309 is.close();
310
311 int powderoff;
312
313 powderoff = findOffset(powdergba, powderlen, image_key, image_keylen);
314
315 if (powderoff < 0)
316 {
317 cerr << "Unable to find old image in \"powder.gba\", aborting." << endl;
318 return -2;
319 }
320
321 // Adjust the powder offset as it refers to the well icon
322 powderoff -= 384 * 16;
323
324 int paletteoff;
325 paletteoff = findOffset(powdergba, powderlen, palette_key, palette_keylen);
326 if (paletteoff < 0)
327 {
328 cerr << "Unable to find old palette in \"powder.gba\", aborting." << endl;
329 return -2;
330 }
331
332 unsigned char *bitmap, palette[512], *tiled;
333 int bitmaplen;
334
335 bitmap = loadbitmap("dungeon16.bmp", palette, bitmaplen);
336 if (!bitmap)
337 {
338 cerr << "Unable to load \"dungeon16.bmp\", aborting." << endl;
339 return -3;
340 }
341
342 unsigned char palette_lut[256];
343 buildpalettelut(palette_lut, palette, &powdergba[paletteoff]);
344
345 tiled = tilify(bitmap, bitmaplen, palette_lut);
346
347 splicedata(powdergba, powderoff, tiled, bitmaplen);
348 splicedata(powdergba, paletteoff, palette, 512);
349
350 // Save out result.
351 ofstream os;
352
353 os.open("powder_c.gba", ios_base::out | ios_base::trunc | ios_base::binary);
354
355 if (!os)
356 {
357 cerr << "Cannot write to \"powder_c.gba\", aborting." << endl;
358 return -4;
359 }
360
361 os.write((char *)powdergba, powderlen);
362
363 os.close();
364
365 cout << "Successfully spliced data!" << endl;
366
367 return 0;
368 }
369