| 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 |
|
| 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 |
|
| 203 |
|
| 204 |
goto Main; |
| 205 |
|
| 206 |
JustCode: |
| 207 |
*a1++ = RLE_ID; |
| 208 |
*a1++ = 0; |
| 209 |
*a1++ = RLE_ID; |
| 210 |
goto NextByte; |
| 211 |
|
| 212 |
Equal: |
| 213 |
d1 = d0; |
| 214 |
d4++; |
| 215 |
if (d4 >= 255) |
| 216 |
goto FlushBytes; |
| 217 |
goto NextByte; |
| 218 |
|
| 219 |
FlushBytes: |
| 220 |
if (d4 >= 3) |
| 221 |
goto FourOrMore; |
| 222 |
NotFour: |
| 223 |
*a1++ = (uint8_t)d1; |
| 224 |
if (--d4 != -1) goto NotFour; |
| 225 |
d4 = 0; |
| 226 |
goto NextByte; |
| 227 |
FourOrMore: |
| 228 |
*a1++ = RLE_ID; |
| 229 |
*a1++ = (uint8_t)d4; |
| 230 |
*a1++ = (uint8_t)d1; |
| 231 |
d4 = 0; |
| 232 |
d0++; |
| 233 |
goto NextByte; |
| 234 |
|
| 235 |
Main: |
| 236 |
ToPtr = (uint8_t *)malloc(DataLen); |
| 237 |
a0 = DataPtr; |
| 238 |
a1 = ToPtr; |
| 239 |
d7 = DataLen; |
| 240 |
d4 = 0; |
| 241 |
|
| 242 |
EqLoop: |
| 243 |
d0 = *a0++; |
| 244 |
if (d0 == RLE_ID) |
| 245 |
goto JustCode; |
| 246 |
|
| 247 |
if (d7 == 1) |
| 248 |
goto endskip; |
| 249 |
|
| 250 |
if (d0 == *a0) |
| 251 |
goto Equal; |
| 252 |
endskip: |
| 253 |
if (d4 > 0) |
| 254 |
goto FlushBytes; |
| 255 |
*a1++ = (uint8_t)d0; |
| 256 |
d4 = 0; |
| 257 |
NextByte: |
| 258 |
d7--; |
| 259 |
if (d7 > 0) goto EqLoop; |
| 260 |
|
| 261 |
if (d4 == 0) |
| 262 |
goto endok; |
| 263 |
|
| 264 |
if (d4 >= 3) |
| 265 |
goto FourOrMore2; |
| 266 |
NotFour2: |
| 267 |
*a1++ = (uint8_t)d0; |
| 268 |
if (--d4 != -1) goto NotFour2; |
| 269 |
goto endok; |
| 270 |
FourOrMore2: |
| 271 |
*a1++ = RLE_ID; |
| 272 |
*a1++ = (uint8_t)d4; |
| 273 |
*a1++ = (uint8_t)d0; |
| 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 |
} |