-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathbmp2bin.cpp
557 lines (485 loc) · 20.6 KB
/
bmp2bin.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
//////////////////////////////////////////////////////////////////////////////
// bmp2bin.cpp //
//////////////////////////////////////////////////////////////////////////////
/*
Bitmap to binary converter
by Rafael Vuijk (aka Dark Fader)
History:
V1.08 - added RGB565 support
V1.07 - fixed for proper endian detection
V1.06 - added support for MirkoSDK sprite format
v1.05 - palette output code for gp32 by mr.spiv
v1.04+ - bigendian support by mr.spiv
v1.04 - .act & MS palette loading support, palette quantization method 1
v1.03 - added 8 bit bmp support
v1.02 - fixed rotation bug, copied structures from WinGDI.h
v1.01 - combined
v1.00 - seperate versions for each platform
*/
//////////////////////////////////////////////////////////////////////////////
// Includes //
//////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <sys/param.h>
//////////////////////////////////////////////////////////////////////////////
// Defines //
//////////////////////////////////////////////////////////////////////////////
#define VER "1.08"
#define VERF "1.05"
#define ALIGN4(n) (((n)+3) &~ 3)
//////////////////////////////////////////////////////////////////////////////
// Typedefs //
//////////////////////////////////////////////////////////////////////////////
typedef unsigned char BYTE;
typedef unsigned short WORD;
typedef unsigned int DWORD;
typedef int LONG;
//
// Convert Little Endian to Big Endian..
//
/*static BYTE endiaB( BYTE b ) {
return b;
}
*/
static WORD endiaW( WORD w ) {
#if BYTE_ORDER == BIG_ENDIAN
WORD rw;
rw = ((w >> 8) | (w << 8));
//printf("%04x -> %04x\n",w,rw);
return rw;
#elif BYTE_ORDER == LITTLE_ENDIAN
return w;
#else
#error unknown endianess..
#endif
}
static DWORD endiaDW( DWORD w ) {
#if BYTE_ORDER == BIG_ENDIAN
DWORD rw;
WORD wu = w >> 16;
WORD wl = w & 0xffff;
wu = (wu >> 8) | (wu << 8);
wl = (wl >> 8) | (wl << 8);
rw = ((wl << 16) | (wu));
//printf("%08x -> %08x\n",w,rw);
return rw;
#elif BYTE_ORDER == LITTLE_ENDIAN
return w;
#else
#error unknown endianess..
#endif
}
static LONG endiaL( LONG w ) {
#if BYTE_ORDER == BIG_ENDIAN
LONG rw;
WORD wu = (WORD)(w >> 16);
WORD wl = (WORD)(w & 0xffff);
wu = (wu >> 8) | (wu << 8);
wl = (wl >> 8) | (wl << 8);
rw = ((wl << 16) | (wu));
//printf("%08x -> %08x\n",w,rw);
return rw;
#elif BYTE_ORDER == LITTLE_ENDIAN
return w;
#else
#error unknown endianess..
#endif
}
//
//
//
#pragma pack(1)
typedef struct tagRGBTRIPLE {
BYTE rgbtBlue;
BYTE rgbtGreen;
BYTE rgbtRed;
} RGBTRIPLE;
typedef struct tagRGBQUAD {
BYTE rgbBlue;
BYTE rgbGreen;
BYTE rgbRed;
BYTE rgbReserved;
} RGBQUAD;
typedef struct tagBITMAPFILEHEADER { // size 14 bytes
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
} BITMAPFILEHEADER;
typedef struct tagBITMAPINFOHEADER{ // size 40 bytes
DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
} BITMAPINFOHEADER;
#pragma pack()
typedef void WritePixel(const RGBTRIPLE *p);
//////////////////////////////////////////////////////////////////////////////
// Variables //
//////////////////////////////////////////////////////////////////////////////
static char *inputFile;
static char *outputFile;
static char *paletteFile;
static char *outPaletteFile ;
static RGBTRIPLE palette[256];
static RGBTRIPLE *imageData;
static BITMAPFILEHEADER bfh;
static BITMAPINFOHEADER bih;
static char flags[256];
static WritePixel *writePixel; // pixel write function
static FILE *fi;
static FILE *fo;
static FILE *fp = NULL;
//
//
//
static int writePaletteGP( RGBQUAD* p, int fmt, FILE *fp ) {
int m,n;
RGBQUAD q;
for (n = 0; n < 256; n += 16 ) {
for (m = 0; m < 16; m++) {
q = p[n+m];
// printf("%02x %02x %02x - %04x %04x %04x\n",
// q.rgbRed,q.rgbGreen,q.rgbBlue,
// q.rgbRed>>3<<11,q.rgbGreen>>2<<5,q.rgbBlue>>3);
fprintf(fp,"0x%04x",(q.rgbBlue>>3<<1) | (q.rgbGreen>>3<<6) | (q.rgbRed>>3<<11));
if (m < 15) {
fprintf(fp,",");
} else {
fprintf(fp,"\n");
}
}
}
return 0;
}
//////////////////////////////////////////////////////////////////////////////
// Sqr //
//////////////////////////////////////////////////////////////////////////////
inline unsigned long Sqr(unsigned int n)
{
return n*n;
}
//////////////////////////////////////////////////////////////////////////////
// Dist1 //
//////////////////////////////////////////////////////////////////////////////
unsigned long Dist1(const RGBTRIPLE *a, const RGBTRIPLE *b)
{
return
(
Sqr((int)a->rgbtRed - (int)b->rgbtRed)*28 +
Sqr((int)a->rgbtGreen - (int)b->rgbtGreen)*91 +
Sqr((int)a->rgbtBlue - (int)b->rgbtBlue)*9
);
}
//////////////////////////////////////////////////////////////////////////////
// WritePixelP1 //
//////////////////////////////////////////////////////////////////////////////
void WritePixelP1(const RGBTRIPLE *p) // '1': 8 bits palette (method 1)
{
unsigned char out;
unsigned long bestDist = (unsigned long)-1;
for (int i=0; i<256; i++)
{
unsigned long dist = Dist1(p, &palette[i]);
if (dist < bestDist) { bestDist = dist; out = i; }
}
fwrite(&out, 1, 1, fo);
}
//////////////////////////////////////////////////////////////////////////////
// WritePixel24 //
//////////////////////////////////////////////////////////////////////////////
void WritePixel24(const RGBTRIPLE *p) // 't': 24 bits
{
fwrite(&p->rgbtRed, 1, 1, fo);
fwrite(&p->rgbtGreen, 1, 1, fo);
fwrite(&p->rgbtBlue, 1, 1, fo);
}
//////////////////////////////////////////////////////////////////////////////
// WritePixel8 //
//////////////////////////////////////////////////////////////////////////////
void WritePixel8(const RGBTRIPLE *p) // 'e': 8 bits (b2g3r3)
{
unsigned char out = (p->rgbtBlue>>6<<6) | (p->rgbtGreen>>5<<3) | (p->rgbtRed>>5<<0);
fwrite(&out, 1, 1, fo);
}
//////////////////////////////////////////////////////////////////////////////
// WritePixelGP8 //
//////////////////////////////////////////////////////////////////////////////
void WritePixelGP8(const RGBTRIPLE *p) // 'i': 8 bits (LUT, GamePark)
{
unsigned char out = p->rgbtRed;
// hack by Mr.Spiv
fwrite(&out, 1, 1, fo);
}
//////////////////////////////////////////////////////////////////////////////
// WritePixelGP32 //
//////////////////////////////////////////////////////////////////////////////
void WritePixelGP32(const RGBTRIPLE *p) // 'p': 16 bits (r5g5b5x1, GamePark)
{
unsigned short out = endiaW((p->rgbtBlue>>3<<1) | (p->rgbtGreen>>3<<6) | (p->rgbtRed>>3<<11));
fwrite(&out, 2, 1, fo);
}
//////////////////////////////////////////////////////////////////////////////
// WritePixelGP2X //
//////////////////////////////////////////////////////////////////////////////
void WritePixelGP2X(const RGBTRIPLE *p) // '2': 16 bits (r5g6b5, GP2X)
{
unsigned short out = endiaW((p->rgbtBlue>>3) | ((p->rgbtGreen&0xFC) << 3) | ((p->rgbtRed&0xF8)<<8));
fwrite(&out, 2, 1, fo);
}
//////////////////////////////////////////////////////////////////////////////
// WritePixelGB //
//////////////////////////////////////////////////////////////////////////////
void WritePixelGB(const RGBTRIPLE *p) // 'g': 16 bits (x1b5g5r5, GameBoy)
{
unsigned short out = (p->rgbtBlue>>3<<10) | (p->rgbtGreen>>3<<5) | (p->rgbtRed>>3<<0);
if ( flags['d']) out |= 0x8000;
fwrite(&out, 2, 1, fo);
}
//////////////////////////////////////////////////////////////////////////////
// main //
//////////////////////////////////////////////////////////////////////////////
int main(int argc, char *argv[])
{
// parse parameters
for (int a=1; a<argc; a++)
{
if (argv[a][0] == '-')
{
for (int i=1; argv[a][i]; i++)
{
flags[argv[a][i]]++;
if ((argv[a][i] >= '0') && (argv[a][i] <= '9')) paletteFile = argv[++a];
}
}
else
{
if (!inputFile) inputFile = argv[a];
else if (!outputFile) outputFile = argv[a];
else if (!outPaletteFile) outPaletteFile = argv[a];
else
{
fprintf(stderr, "Error: Too many filenames given!\n");
}
}
}
// show help
if (flags['?'] || flags['h'] || !inputFile || !outputFile)
{
fprintf(stderr, "bmp2bin " VER "\n");
fprintf(stderr, "\n");
fprintf(stderr, "Syntax: bmp2bin [-flags] <input.bmp> <output.raw> [<palette.txt>]\n");
fprintf(stderr, "\n");
fprintf(stderr, "Flags/parameters:\n");
fprintf(stderr, " -i 8 bits output, LUT, GP32 256 colors palette\n");
fprintf(stderr, " -e 8 bits output, b2g3r3)\n");
fprintf(stderr, " -1 palette.act 8 bits output, palette quantization method 1\n");
fprintf(stderr, " -g 16 bits output, x1b5g5r5, GameBoy\n");
fprintf(stderr, " -d 16 bits output, x1b5g5r5, DS, x bit set\n");
fprintf(stderr, " -p 16 bits output, r5g5b5x1, GP32 (default)\n");
fprintf(stderr, " -q 16 bits output, r5g6b5, GP2X\n");
fprintf(stderr, " -t 24 bits output, b8g8r8\n");
fprintf(stderr, " -r rotate 90 degrees clockwise\n");
fprintf(stderr, " -x write sprite header, Mr.Mirko SDK\n");
return -1;
}
// read palette
if (paletteFile)
{
// open bitmap file
FILE *f = fopen(paletteFile, "rb");
if (!f) { fprintf(stderr, "Error opening palette file!\n"); return -1; }
fseek(f, 0, SEEK_END);
int size = ftell(f);
fseek(f, 0, SEEK_SET);
// read data
//if (strstr(paletteFile, ".act"))
if (size == 3*256)
{
// assume r,g,b...
unsigned char paletteData[256][3];
fread(paletteData, sizeof(paletteData), 1, f);
for (int i=0; i<256; i++)
{
palette[i].rgbtRed = paletteData[i][0];
palette[i].rgbtGreen = paletteData[i][1];
palette[i].rgbtBlue = paletteData[i][2];
}
}
else if (size == 4*256)
{
// assume r,g,b,x...
unsigned char paletteData[256][4];
fread(paletteData, sizeof(paletteData), 1, f);
for (int i=0; i<256; i++)
{
palette[i].rgbtRed = paletteData[i][0];
palette[i].rgbtGreen = paletteData[i][1];
palette[i].rgbtBlue = paletteData[i][2];
}
}
else
{
fprintf(stderr, "Unknown palette format!\n");
return -1;
}
// close file
fclose(f);
}
// outpalette
if (outPaletteFile && (flags['i'])) {
if ((fp = fopen(outPaletteFile,"wb")) == NULL) {
fprintf(stderr,"Error opening output palette file!\n");
return -1; // let the compiler do the cleanup :/
}
}
// read
{
// open bitmap file
fi = fopen(inputFile, "rb");
if (!fi) { fprintf(stderr, "Error opening bitmap file!\n"); return -1; }
// read headers
fread(&bfh, sizeof(bfh), 1, fi);
fread(&bih, sizeof(bih), 1, fi);
// checks
if (endiaW(bih.biPlanes) != 1) { fprintf(stderr, "Unsupported number of planes!\n"); return -1; }
if (endiaDW(bih.biCompression) != 0) { fprintf(stderr, "Unsupported compression type!\n"); return -1; }
// load to RGB format
imageData = new RGBTRIPLE[endiaL(bih.biWidth) * endiaL(bih.biHeight) + 1/*dummy*/];
// load data with correct bit depth
if (endiaW(bih.biBitCount) == 8)
{
fprintf(stderr," The BMP is a 8bits image..\n");
// read palette (quads -> triples)
RGBQUAD paletteQ[256];
RGBTRIPLE palette[256];
fread(paletteQ, sizeof(paletteQ), 1, fi);
for (int i=0; i<256; i++)
{
palette[i].rgbtRed = paletteQ[i].rgbRed;
palette[i].rgbtGreen = paletteQ[i].rgbGreen;
palette[i].rgbtBlue = paletteQ[i].rgbBlue;
}
if (fp && flags['i']) {
writePaletteGP(paletteQ,0,fp);
}
// go to pixel data
fseek(fi, endiaDW(bfh.bfOffBits), SEEK_SET);
int lineSize = endiaL(bih.biWidth) * 1;
int dummysize = ALIGN4(lineSize) - lineSize;
int dummy;
unsigned char *lineData = new unsigned char [lineSize];
// read & convert pixel data
for (int y=endiaL(bih.biHeight)-1; y>=0; y--)
{
RGBTRIPLE *pImageData = imageData + (endiaL(bih.biWidth)*y); // imageData is up-side down!
fread(lineData, lineSize, 1, fi);
fread(&dummy, dummysize, 1, fi);
if (flags['i']) {
for (int x=0; x<endiaL(bih.biWidth); x++) pImageData[x].rgbtRed = lineData[x];
} else {
for (int x=0; x<endiaL(bih.biWidth); x++) pImageData[x] = palette[lineData[x]];
}
}
}
else if (endiaW(bih.biBitCount) == 24)
{
fprintf(stderr," The BMP is a 24bits image..\n");
// go to pixel data
fseek(fi, endiaDW(bfh.bfOffBits), SEEK_SET);
int lineSize = endiaL(bih.biWidth) * 3;
int dummysize = ALIGN4(lineSize) - lineSize;
int dummy;
// read pixel data
for (int y=endiaL(bih.biHeight)-1; y>=0; y--)
{
RGBTRIPLE *pImageData = imageData + endiaL(bih.biWidth)*y; // imageData is up-side down!
fread(pImageData, lineSize, 1, fi);
fread(&dummy, dummysize, 1, fi);
}
}
else
{
fprintf(stderr, "Unsupported bit depth!\n");
return -1;
}
// close file
fclose(fi);
}
// Close output palette file..
if (fp) fclose(fp);
// select pixel writer
writePixel = WritePixelGP32;
if (flags['p']) writePixel = WritePixelGP32;
if (flags['q']) writePixel = WritePixelGP2X;
if (flags['g'] || flags['d'] ) writePixel = WritePixelGB;
if (flags['i']) writePixel = WritePixelGP8;
if (flags['e']) writePixel = WritePixel8;
if (flags['t']) writePixel = WritePixel24;
if (flags['1']) writePixel = WritePixelP1;
// write
{
fo = fopen(outputFile, "wb");
if (!fo) { fprintf(stderr, "Error opening output file!\n"); return -1; }
// Mr.Mirko 2004
// write Header
/*
typedef struct {
char magic[4];
u16 size_x;
u16 size_y;
u16 reserved1;
u16 reserved2;
} SHEADER; */
if (flags['x'])
{
char sdk[]="Mr.M";
short reserved=0;
short x = endiaL(bih.biWidth);
short y = endiaL(bih.biHeight);
if (flags['r']) { printf ("Please dont rotate Sprite...\n"); }
fprintf(stderr, "X: %d\n",x);
fprintf(stderr, "Y: %d\n",y);
fwrite(&sdk, 1, 4, fo); // 4 bytes
fwrite(&x, 2, 1, fo); // 1 short
fwrite(&y, 2, 1, fo); // 1 short
fwrite(&reserved, 2, 1, fo);// 1 short
fwrite(&reserved, 2, 1, fo);// 1 short
}
// rotated?
if (flags['r'])
{
for (int x=0; x<endiaL(bih.biWidth); x++)
{
for (int y=endiaL(bih.biHeight)-1; y>=0; y--)
{
RGBTRIPLE *p = imageData + (y*endiaL(bih.biWidth)) + x;
writePixel(p);
}
}
}
else
{
// RGBTRIPLE *p = imageData;
for (int y=0; y<endiaL(bih.biHeight); y++)
{
for (int x=0; x<endiaL(bih.biWidth); x++)
{
RGBTRIPLE *p = imageData + (y*endiaL(bih.biWidth)) + x;
writePixel(p);
}
}
}
// close files
fclose(fo);
}
return 0;
}