-
Notifications
You must be signed in to change notification settings - Fork 0
/
common_orig.cpp
527 lines (474 loc) · 18.3 KB
/
common_orig.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
#define NOT_DEBUG 1 //true = command line arguments are active
int main(int argc, char * argv[])
{
BITMAPFILEHEADER bf;
BITMAPINFO bmi;
RGBQUAD myColors[256]; // needed because size of bmi.bmiColors is defined as 1 in wingdi.h !!!
FILE *infile, *outfile;
uint32_t bytes_read = 0, bmpsize, fillindex, nextlinei, linei, i, offset;
int use1=0, use2=0, use4=0, use8=0, palout=0, padbytes=0;
char drive[_MAX_DRIVE];
char dir[_MAX_DIR];
char file[_MAX_FNAME];
char justfile[_MAX_FNAME];
char ufile[_MAX_FNAME];
char ext[_MAX_EXT];
char of_name[_MAX_FNAME+_MAX_EXT];
unsigned char *databuffer;
unsigned char *parsedbuffer;
unsigned char *rowbuffer; unsigned char *rowbuffer2;
uint16_t rowbufindex=0, currentrowbuf=1;
char *commentbuffer; char* commentbuffer2;
bool rowflipping=false; //needed for 4-bit files from GIMP
printf("Pokitto BMP to Pokitto bitmap conversion utility\n");
if (((argc < 3) || (argc > 4)) && NOT_DEBUG)
{
print_usage(argv[0]);
exit(-1);
}
if ((strcmp(argv[2], "-2") == 0) && NOT_DEBUG)
{
use2 = 1;
}
else if ((strcmp(argv[2], "-4") == 0) && NOT_DEBUG)
{
use4 = 1;
}
else if ((strcmp(argv[2], "-8")) == 0 && NOT_DEBUG)
{
use8 = 1;
}
else if ((strcmp(argv[2], "-1")) == 0 && NOT_DEBUG)
{
use1 = 1;
}
else if (NOT_DEBUG)
{
print_usage(argv[0]);
exit(-1);
}
if ((argc == 4) && NOT_DEBUG)
{
if (strcmp(argv[3], "-p") == 0)
palout = 1;
else
{
print_usage(argv[0]);
exit(-1);
}
}
if (NOT_DEBUG) {
/* split the string to separate elementss */
_splitpath(argv[1], drive, dir, file, ext);
strcpy(ufile,file);
_strupr(ufile);
strcpy(file,argv[1]);
printf("File name: %s \n",file);
if (strlen(ext)==0) {
strcpy(justfile,argv[1]); // save a copy of bare filename
} else {
//printf("strlen file: %d " ,strlen(file));
memset((void*)justfile,0,_MAX_FNAME);
memcpy((void*)justfile,(void*)file,strlen(file)-4);
strcat(justfile,"\0");
//printf("justfile: %s ",justfile);
}
if (strlen(ext) == 0)
{
strcat(file,".bmp");
}
}
else
{
strcpy(file,"test4col.bmp");
strcpy(justfile, "test4col");
palout = 1;
use2 = 1;
use4 = 0;
use8 = 0;
}
/** numcol is the number of colors for output */
unsigned int numcol;
unsigned char pixperbyte;
if (use8) { numcol=256;pixperbyte=1;}
else if (use4) { numcol=16;pixperbyte=2;}
else if (use2) { numcol=4;pixperbyte=4;}
else if (use1) { numcol=1;pixperbyte=8;}
infile = fopen(file, "rb");
if (!infile)
{
printf("Error opening input file %s\n", file);
exit(-1);
}
if (fread(&bf,sizeof(bf),1,infile) == 1)
{
bytes_read += sizeof(bf);
}
else
{
printf("Error reading BMP header\n");
fclose(infile);
exit(-1);
}
uint16_t test = sizeof(bmi.bmiHeader);
if (fread(&bmi,test,1,infile) == 1)
{
if (bf.bfType != 0x4D42) {
printf("Bitmap file has an unrecognized format (4D42 id missing from beginning).\n");
printf("BMP2POK accepts .BMP files that have an indexed (1,-bit, 4-bit or 8-bit) color palette.\n");
fclose(infile);
exit(-1);
}
if (bmi.bmiHeader.biBitCount != 8 && bmi.bmiHeader.biBitCount != 4 && bmi.bmiHeader.biBitCount != 1)
{
printf("Only 8bpp, 4bpp & 1bpp BMP files are supported\n");
fclose(infile);
exit(-1);
}
int c = bmi.bmiHeader.biClrUsed;
if (c==0) c = 1 << bmi.bmiHeader.biBitCount; // from MS BMP specs. 0 means 2^n colors
bmi.bmiHeader.biClrUsed = c;
printf("Number of colours used: %d\n", c);
//fread(&bmi.bmiColors[0],c*4,1,infile);
/* seek to the beginning of the color table - because of gimp */
fseek(infile, bf.bfOffBits-c*4, SEEK_SET); //gfx data star minus color table
fread(&myColors[0],c*4,1,infile);
bytes_read += sizeof(bmi.bmiHeader)+c*4;
for (unsigned int j=0; j<bmi.bmiHeader.biClrUsed; j++) {
printf("Colour: %d",j);
if (j<10) printf(" ");
/*R*/
c=myColors[j].rgbRed;
printf(" r:%d",c);
if (c<10) printf(" ");
else if (c<100) printf(" ");
printf(" ");
/*G*/
c=myColors[j].rgbGreen;
printf("g:%d",c);
if (c<10) printf(" ");
else if (c<100) printf(" ");
printf(" ");
/*B*/
c=myColors[j].rgbBlue;
printf("b:%d",c);
if (c<10) printf(" ");
else if (c<100) printf(" ");
printf("\n");
}
}
else
{
printf("Error reading BMP info\n");
fclose(infile);
exit(-1);
}
/* Allocate buffer storage */
bmpsize = bmi.bmiHeader.biWidth * bmi.bmiHeader.biHeight*bmi.bmiHeader.biBitCount/8;
if (bmi.bmiHeader.biWidth%4) {
padbytes = bmi.bmiHeader.biWidth%4;
printf("Width is not divisible by 4\n");
printf("Ignoring %d padding bytes at each line\n", padbytes);
}
databuffer = (unsigned char *) malloc(bmpsize);
parsedbuffer = (unsigned char *) malloc (bmi.bmiHeader.biWidth * bmi.bmiHeader.biHeight+1000);
rowbuffer = (unsigned char *) malloc(bmi.bmiHeader.biWidth/pixperbyte);
rowbuffer2 = (unsigned char *) malloc(bmi.bmiHeader.biWidth/pixperbyte);
commentbuffer = (char *) malloc(bmi.bmiHeader.biWidth+16);
commentbuffer2 = (char *) malloc(bmi.bmiHeader.biWidth+16);
if (databuffer == NULL)
{
printf("Error allocating temporary data buffer, is image too big?\n");
fclose(infile);
exit(-1);
}
/* seek to the beginning of the data */
fseek(infile, bf.bfOffBits, SEEK_SET);
/* Read data into array */
int padcounter = bmi.bmiHeader.biWidth+padbytes;
fillindex = bmpsize - 1;
i = 0;
while (i < bmpsize)
{
unsigned char rgb;
if (fread(&rgb, sizeof(rgb), 1, infile) != 1)
{
printf("error reading BMP data\n");
fclose(infile);
free(databuffer);
exit(-1);
}
/* Store in color array for the correct type. BMPs are
stored in reverse, so save the data backwards */
if (padbytes) {
/* there are padding bytes */
if (padcounter>padbytes) {
/*not in padding section yet, so store */
databuffer[fillindex]=rgb;
fillindex--;
i++;
}
padcounter--;
if (padcounter==0) padcounter = bmi.bmiHeader.biWidth + padbytes; // reset padvounter at each line
} else {
/* no need to worry about padding */
databuffer[fillindex]=rgb;
fillindex--;
i++;
}
}
fclose(infile);
/** PARSING DATABUFFER to PURE BYTES (1-,4- bit sources to 8-bit) **/
// this is to make outputting from different bitness sources easier
uint16_t nextrow = (bmi.bmiHeader.biWidth+padbytes)*bmi.bmiHeader.biBitCount/8;
for (uint16_t k=0; k < bmpsize; k++) {
switch (bmi.bmiHeader.biBitCount) {
case 8:
parsedbuffer[k] = databuffer[k];break;
case 4:
parsedbuffer[k] = databuffer[k]>>4;
parsedbuffer[k+1] = databuffer[k]&0x0F;
k++; break;
case 1:
parsedbuffer[k] = (databuffer[k]>>7)&0x01;
parsedbuffer[k+nextrow] = (databuffer[k]>>6)&0x01;
parsedbuffer[k+2*nextrow] = (databuffer[k]>>5)&0x01;
parsedbuffer[k+3*nextrow] = (databuffer[k]>>4)&0x01;
parsedbuffer[k+4*nextrow] = (databuffer[k]>>3)&0x01;
parsedbuffer[k+5*nextrow] = (databuffer[k]>>2)&0x01;
parsedbuffer[k+6*nextrow] = (databuffer[k]>>1)&0x01;
parsedbuffer[k+7*nextrow] = (databuffer[k]>>0)&0x01;
k += 7; break;
}
}
/** FLIP ROWS for 4-BIT IMAGE SOURCES **/
uint16_t ind=0;
if (bmi.bmiHeader.biBitCount == 4) {
for (uint16_t l=0; l < bmi.bmiHeader.biHeight; l+=2) {
for (uint16_t m=0; m < bmi.bmiHeader.biWidth; m++,ind++) {
uint8_t temp = parsedbuffer[ind];
parsedbuffer[ind]=parsedbuffer[ind+bmi.bmiHeader.biWidth];
parsedbuffer[ind+bmi.bmiHeader.biWidth]=temp;
}
ind += bmi.bmiHeader.biWidth; //jump 1 row to next pair
}
}
//if (use555)
sprintf(of_name,justfile);
strcat(of_name,".c");
printf("Outputting c file: %s\n", of_name);
outfile = fopen(of_name,"w");
if (outfile == NULL)
{
printf("error opening out file %s\n", of_name);
free(databuffer);
exit(-1);
}
if (use8) fprintf (outfile,
"\n\n/*\n * BMP image as 8bpp (up to 256 color index) data\n");
else if (use4) fprintf (outfile,
"\n\n/*\n * BMP image as 4bpp (16 color index) data\n");
else if (use2) fprintf (outfile,
"\n\n/*\n * BMP image as 2bpp (4 color) data\n");
else if (use1) fprintf (outfile,
"\n\n/*\n * BMP image as 1bpp (2 color) data\n");
//if (swaprgb)
// fprintf (outfile, " * Red and green data swapped\n");
fprintf (outfile, " */\n\n");
fprintf(outfile,"#include <stdint.h>\n\n");
/* Palette data */
fprintf(outfile,"const uint16_t ");
fprintf(outfile,justfile);
fprintf(outfile,"_pal[] = {\n");
if (bmi.bmiHeader.biClrUsed>numcol) {
printf("WARNING! \n");
printf("Your image has %d different colors.\n",(int)bmi.bmiHeader.biClrUsed);
if (use4) printf("But 4bpp mode only allows 16 different colors in the palette.\n");
else if (use2) printf("But 2bpp mode only allows 4 different colors in the palette.\n");
printf("The program will automatically IGNORE any colors used above this limit.\n");
}
if (numcol>bmi.bmiHeader.biClrUsed) numcol = bmi.bmiHeader.biClrUsed;
for (unsigned int c=0;c<numcol;c++) {
unsigned int r,g,b,o;
r = myColors[c].rgbRed >> 3; // 5 bits
g = myColors[c].rgbGreen >> 2; // 6 bits
b = myColors[c].rgbBlue >> 3; // 5 bits
o = (r<<11)|(g<<5)|b;
fprintf(outfile,"%d",o);
if(c<numcol-1) fprintf(outfile,",");
else fprintf(outfile,"\n");
}
fprintf(outfile,"};\n\n");
/* Image data */
fprintf(outfile, "const uint8_t ");
fprintf(outfile,justfile);
fprintf(outfile,"[] = {\n%d,%d,\n",(int)bmi.bmiHeader.biWidth,(int)bmi.bmiHeader.biHeight);
fillindex = 0;
i = 0;
linei = bmi.bmiHeader.biWidth - 1;
nextlinei = 0;
int entrywidth = 0;
if (use8) entrywidth = 3;
else entrywidth = 2;
int bitcounter=0; // needed for packing bits
int prevvalue[8]; // needed for packing bits
uint16_t row=0;
while (i < bmpsize)
{
offset = nextlinei + linei;
/** OUTPUT DATA tO FILE **/
unsigned int temp=fillindex;
if (use8) {
if (databuffer[offset]<10) fprintf (outfile, "0%d", (unsigned char)databuffer[offset]);
else fprintf (outfile, "%d", databuffer[offset]);
fillindex++;
} else if (use4) {
/** OUTPUT as 4-BIT **/
if (bmi.bmiHeader.biBitCount == 8) {
/** 8-bit source image (photoshop) **/
if (bitcounter) {
fprintf (outfile, "%d", (unsigned char)(prevvalue[0]<<4 | (databuffer[offset]&0x0F)));
fillindex++;
} else {
prevvalue[0] = databuffer[offset];
}
bitcounter = !bitcounter;
} else if (bmi.bmiHeader.biBitCount == 4){
/** 4-bit (probably GIMP) **/
// you need to flip the rows arounf because in a 4-bit image pixels are packed in heiight direction also
if (row&0x1) fprintf (outfile, "%d", (unsigned char)databuffer[offset+(bmi.bmiHeader.biWidth+padbytes)/2]);
else fprintf (outfile, "%d", (unsigned char)databuffer[offset-(bmi.bmiHeader.biWidth+padbytes)/2]);
fillindex++;
} else if (bmi.bmiHeader.biBitCount == 1){
/** 1-bit source image, 8 pixels per byte are written as 4 bytes with 2 pixels per byte **/
fprintf (outfile, "%d,", (((databuffer[offset]>>7)&0x1)<<4) | ((databuffer[offset]>>6)&0x1)) ; //source bits 7-8 to 2 bytes
fprintf (outfile, "%d,", (((databuffer[offset]>>5)&0x1)<<4) | ((databuffer[offset]>>4)&0x1)) ; //source bits 5-6 to 2 bytes
fprintf (outfile, "%d,", (((databuffer[offset]>>3)&0x1)<<4) | ((databuffer[offset]>>2)&0x1)) ; //source bits 5-6 to 2 bytes
fprintf (outfile, "%d,", (((databuffer[offset]>>1)&0x1)<<4) | ((databuffer[offset]>>0)&0x1)) ; //source bits 5-6 to 2 bytes
fillindex++;
}
} else if (use2) {
unsigned char outputbyte;
if (bmi.bmiHeader.biBitCount == 8) {
/** 8-bit source **/
switch(bitcounter) {
case 0:
prevvalue[0] = databuffer[offset];
bitcounter +=2;
break;
case 2:
prevvalue[1] = databuffer[offset];
bitcounter +=2;
break;
case 4:
prevvalue[2] = databuffer[offset];
bitcounter +=2;
break;
default:
outputbyte = (prevvalue[0] << 6); //pixel 0 is highest half nibble..
outputbyte |= (prevvalue[1] << 4); //pixel 1
outputbyte |= (prevvalue[2] << 2); //pixel 2
outputbyte |= databuffer[offset]; //pixel 3
fprintf (outfile, "%d", outputbyte);
fillindex ++;
bitcounter = 0;
}
} else if (bmi.bmiHeader.biBitCount == 4) {
rowflipping=true;
/** 4-bit source **/
switch(bitcounter) {
case 0:
prevvalue[0] = databuffer[offset]&0x0F; // first pixel is first byte lower nibble
linei++; i--;// dont jump to next byte yet
bitcounter +=2;
break;
case 2:
prevvalue[1] = databuffer[offset]>>4; // 2nd pixel is first byte higher nibble
bitcounter +=2;
break;
case 4:
prevvalue[2] = databuffer[offset]&0x0F; // third pixel is second byte lower nibble
linei++; i--;// dont jump to next byte yet
bitcounter +=2;
break;
default:
//outputbyte |= databuffer[offset]>>4;
/*outputbyte = ((uint8_t)prevvalue[0] << 6); //pixel 0 is highest half nibble
outputbyte |= ((uint8_t)prevvalue[1] << 4); //pixel 1
outputbyte |= ((uint8_t)prevvalue[2] << 2); //pixel 2
outputbyte |= databuffer[offset]>>4; // 4th pixel is second byte higher nibble*/
prevvalue[3] = databuffer[offset]>>4;
outputbyte = ((uint8_t)prevvalue[1] << 6); //pixel 0 is highest half nibble
outputbyte |= ((uint8_t)prevvalue[0] << 4); //pixel 1
outputbyte |= ((uint8_t)prevvalue[3] << 2); //pixel 2
outputbyte |= ((uint8_t)prevvalue[2] << 0); // 4th pixel is second byte higher nibble*/
//if (row&3>2) outputbyte=0;
//fprintf (outfile, "%d", outputbyte);
if (currentrowbuf==1) rowbuffer[rowbufindex++]=outputbyte;
else rowbuffer2[rowbufindex++]=outputbyte;
fillindex ++;
bitcounter = 0;
}
} else if (bmi.bmiHeader.biBitCount == 1){
/** 1-bit source image, 8 pixels per byte are written as 4 bytes with 2 pixels per byte **/
fprintf (outfile, "%d,", (((databuffer[offset]>>7)&0x1)<<4) | ((databuffer[offset]>>6)&0x1)) ; //source bits 7-8 to 2 bytes
fprintf (outfile, "%d,", (((databuffer[offset]>>5)&0x1)<<4) | ((databuffer[offset]>>4)&0x1)) ; //source bits 5-6 to 2 bytes
fprintf (outfile, "%d,", (((databuffer[offset]>>3)&0x1)<<4) | ((databuffer[offset]>>2)&0x1)) ; //source bits 5-6 to 2 bytes
fprintf (outfile, "%d,", (((databuffer[offset]>>1)&0x1)<<4) | ((databuffer[offset]>>0)&0x1)) ; //source bits 5-6 to 2 bytes
fillindex++;
}
}
if (temp!=fillindex) {
/** A new entry was written to file **/
if (fillindex == bmi.bmiHeader.biWidth/pixperbyte) //line change
{
if (rowflipping) {
/** flip rows**/
/** this is where row flipping happens **/
/** it was needed to change order of rows in a 4-bit source from GIMP */
if (currentrowbuf==2) {
/** output row 2 **/
for (int z=0;z<fillindex;z++) {
fprintf (outfile, "0x");
if (rowbuffer2[z]<0x10) fprintf (outfile, "0");
fprintf (outfile, "%X,",rowbuffer2[z]);
}
fprintf (outfile, "\n");
/** output row 1 **/
for (int z=0;z<fillindex;z++) {
fprintf (outfile, "0x");
if (rowbuffer[z]<0x10) fprintf (outfile, "0");
fprintf (outfile, "%X,",rowbuffer[z]);
}
fprintf (outfile, "\n");
currentrowbuf=1;
} else {
currentrowbuf=002;
}
rowbufindex=0;
fillindex = 0;
row++;
} else {
/** no row flipping **/
fprintf (outfile, ",\n");
fillindex = 0;
row++;
}
}
else {
if (rowflipping==false) fprintf (outfile, ","); // add comma after entry
}
}
i++;
if (linei == 0)
{
linei = bmi.bmiHeader.biWidth;
nextlinei += bmi.bmiHeader.biWidth;
}
linei--;
}
fprintf (outfile, "};");
free(databuffer);
fclose(outfile);
return 0;
}