| 1 |
// dear imgui |
| 2 |
// (binary_to_compressed_c.cpp) |
| 3 |
// Helper tool to turn a file into a C array, if you want to embed font data in your source code. |
| 4 |
|
| 5 |
// The data is first compressed with stb_compress() to reduce source code size, |
| 6 |
// then encoded in Base85 to fit in a string so we can fit roughly 4 bytes of compressed data into 5 bytes of source code (suggested by @mmalex) |
| 7 |
// (If we used 32-bit constants it would require take 11 bytes of source code to encode 4 bytes, and be endianness dependent) |
| 8 |
// Note that even with compression, the output array is likely to be bigger than the binary file.. |
| 9 |
// Load compressed TTF fonts with ImGui::GetIO().Fonts->AddFontFromMemoryCompressedTTF() |
| 10 |
|
| 11 |
// Build with, e.g: |
| 12 |
// # cl.exe binary_to_compressed_c.cpp |
| 13 |
// # g++ binary_to_compressed_c.cpp |
| 14 |
// # clang++ binary_to_compressed_c.cpp |
| 15 |
// You can also find a precompiled Windows binary in the binary/demo package available from https://github.com/ocornut/imgui |
| 16 |
|
| 17 |
// Usage: |
| 18 |
// binary_to_compressed_c.exe [-base85] [-nocompress] [-nostatic] <inputfile> <symbolname> |
| 19 |
// Usage example: |
| 20 |
// # binary_to_compressed_c.exe myfont.ttf MyFont > myfont.cpp |
| 21 |
// # binary_to_compressed_c.exe -base85 myfont.ttf MyFont > myfont.cpp |
| 22 |
|
| 23 |
#define _CRT_SECURE_NO_WARNINGS |
| 24 |
#include <stdio.h> |
| 25 |
#include <string.h> |
| 26 |
#include <stdlib.h> |
| 27 |
#include <assert.h> |
| 28 |
|
| 29 |
// stb_compress* from stb.h - declaration |
| 30 |
typedef unsigned int stb_uint; |
| 31 |
typedef unsigned char stb_uchar; |
| 32 |
stb_uint stb_compress(stb_uchar* out, stb_uchar* in, stb_uint len); |
| 33 |
|
| 34 |
static bool binary_to_compressed_c(const char* filename, const char* symbol, bool use_base85_encoding, bool use_compression, bool use_static); |
| 35 |
|
| 36 |
int main(int argc, char** argv) |
| 37 |
{ |
| 38 |
if (argc < 3) |
| 39 |
{ |
| 40 |
printf("Syntax: %s [-base85] [-nocompress] [-nostatic] <inputfile> <symbolname>\n", argv[0]); |
| 41 |
return 0; |
| 42 |
} |
| 43 |
|
| 44 |
int argn = 1; |
| 45 |
bool use_base85_encoding = false; |
| 46 |
bool use_compression = true; |
| 47 |
bool use_static = true; |
| 48 |
while (argn < (argc - 2) && argv[argn][0] == '-') |
| 49 |
{ |
| 50 |
if (strcmp(argv[argn], "-base85") == 0) { use_base85_encoding = true; argn++; } |
| 51 |
else if (strcmp(argv[argn], "-nocompress") == 0) { use_compression = false; argn++; } |
| 52 |
else if (strcmp(argv[argn], "-nostatic") == 0) { use_static = false; argn++; } |
| 53 |
else |
| 54 |
{ |
| 55 |
fprintf(stderr, "Unknown argument: '%s'\n", argv[argn]); |
| 56 |
return 1; |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
bool ret = binary_to_compressed_c(argv[argn], argv[argn + 1], use_base85_encoding, use_compression, use_static); |
| 61 |
if (!ret) |
| 62 |
fprintf(stderr, "Error opening or reading file: '%s'\n", argv[argn]); |
| 63 |
return ret ? 0 : 1; |
| 64 |
} |
| 65 |
|
| 66 |
char Encode85Byte(unsigned int x) |
| 67 |
{ |
| 68 |
x = (x % 85) + 35; |
| 69 |
return (char)((x >= '\\') ? x + 1 : x); |
| 70 |
} |
| 71 |
|
| 72 |
bool binary_to_compressed_c(const char* filename, const char* symbol, bool use_base85_encoding, bool use_compression, bool use_static) |
| 73 |
{ |
| 74 |
// Read file |
| 75 |
FILE* f = fopen(filename, "rb"); |
| 76 |
if (!f) return false; |
| 77 |
int data_sz; |
| 78 |
if (fseek(f, 0, SEEK_END) || (data_sz = (int)ftell(f)) == -1 || fseek(f, 0, SEEK_SET)) { fclose(f); return false; } |
| 79 |
char* data = new char[data_sz + 4]; |
| 80 |
if (fread(data, 1, data_sz, f) != (size_t)data_sz) { fclose(f); delete[] data; return false; } |
| 81 |
memset((void*)(((char*)data) + data_sz), 0, 4); |
| 82 |
fclose(f); |
| 83 |
|
| 84 |
// Compress |
| 85 |
int maxlen = data_sz + 512 + (data_sz >> 2) + sizeof(int); // total guess |
| 86 |
char* compressed = use_compression ? new char[maxlen] : data; |
| 87 |
int compressed_sz = use_compression ? stb_compress((stb_uchar*)compressed, (stb_uchar*)data, data_sz) : data_sz; |
| 88 |
if (use_compression) |
| 89 |
memset(compressed + compressed_sz, 0, maxlen - compressed_sz); |
| 90 |
|
| 91 |
// Output as Base85 encoded |
| 92 |
FILE* out = stdout; |
| 93 |
fprintf(out, "// File: '%s' (%d bytes)\n", filename, (int)data_sz); |
| 94 |
fprintf(out, "// Exported using binary_to_compressed_c.cpp\n"); |
| 95 |
const char* static_str = use_static ? "static " : ""; |
| 96 |
const char* compressed_str = use_compression ? "compressed_" : ""; |
| 97 |
if (use_base85_encoding) |
| 98 |
{ |
| 99 |
fprintf(out, "%sconst char %s_%sdata_base85[%d+1] =\n \"", static_str, symbol, compressed_str, (int)((compressed_sz + 3) / 4)*5); |
| 100 |
char prev_c = 0; |
| 101 |
for (int src_i = 0; src_i < compressed_sz; src_i += 4) |
| 102 |
{ |
| 103 |
// This is made a little more complicated by the fact that ??X sequences are interpreted as trigraphs by old C/C++ compilers. So we need to escape pairs of ??. |
| 104 |
unsigned int d = *(unsigned int*)(compressed + src_i); |
| 105 |
for (unsigned int n5 = 0; n5 < 5; n5++, d /= 85) |
| 106 |
{ |
| 107 |
char c = Encode85Byte(d); |
| 108 |
fprintf(out, (c == '?' && prev_c == '?') ? "\\%c" : "%c", c); |
| 109 |
prev_c = c; |
| 110 |
} |
| 111 |
if ((src_i % 112) == 112 - 4) |
| 112 |
fprintf(out, "\"\n \""); |
| 113 |
} |
| 114 |
fprintf(out, "\";\n\n"); |
| 115 |
} |
| 116 |
else |
| 117 |
{ |
| 118 |
fprintf(out, "%sconst unsigned int %s_%ssize = %d;\n", static_str, symbol, compressed_str, (int)compressed_sz); |
| 119 |
fprintf(out, "%sconst unsigned int %s_%sdata[%d/4] =\n{", static_str, symbol, compressed_str, (int)((compressed_sz + 3) / 4)*4); |
| 120 |
int column = 0; |
| 121 |
for (int i = 0; i < compressed_sz; i += 4) |
| 122 |
{ |
| 123 |
unsigned int d = *(unsigned int*)(compressed + i); |
| 124 |
if ((column++ % 12) == 0) |
| 125 |
fprintf(out, "\n 0x%08x, ", d); |
| 126 |
else |
| 127 |
fprintf(out, "0x%08x, ", d); |
| 128 |
} |
| 129 |
fprintf(out, "\n};\n\n"); |
| 130 |
} |
| 131 |
|
| 132 |
// Cleanup |
| 133 |
delete[] data; |
| 134 |
if (use_compression) |
| 135 |
delete[] compressed; |
| 136 |
return true; |
| 137 |
} |
| 138 |
|
| 139 |
// stb_compress* from stb.h - definition |
| 140 |
|
| 141 |
//////////////////// compressor /////////////////////// |
| 142 |
|
| 143 |
static stb_uint stb_adler32(stb_uint adler32, stb_uchar *buffer, stb_uint buflen) |
| 144 |
{ |
| 145 |
const unsigned long ADLER_MOD = 65521; |
| 146 |
unsigned long s1 = adler32 & 0xffff, s2 = adler32 >> 16; |
| 147 |
unsigned long blocklen, i; |
| 148 |
|
| 149 |
blocklen = buflen % 5552; |
| 150 |
while (buflen) { |
| 151 |
for (i=0; i + 7 < blocklen; i += 8) { |
| 152 |
s1 += buffer[0], s2 += s1; |
| 153 |
s1 += buffer[1], s2 += s1; |
| 154 |
s1 += buffer[2], s2 += s1; |
| 155 |
s1 += buffer[3], s2 += s1; |
| 156 |
s1 += buffer[4], s2 += s1; |
| 157 |
s1 += buffer[5], s2 += s1; |
| 158 |
s1 += buffer[6], s2 += s1; |
| 159 |
s1 += buffer[7], s2 += s1; |
| 160 |
|
| 161 |
buffer += 8; |
| 162 |
} |
| 163 |
|
| 164 |
for (; i < blocklen; ++i) |
| 165 |
s1 += *buffer++, s2 += s1; |
| 166 |
|
| 167 |
s1 %= ADLER_MOD, s2 %= ADLER_MOD; |
| 168 |
buflen -= blocklen; |
| 169 |
blocklen = 5552; |
| 170 |
} |
| 171 |
return (s2 << 16) + s1; |
| 172 |
} |
| 173 |
|
| 174 |
static unsigned int stb_matchlen(stb_uchar *m1, stb_uchar *m2, stb_uint maxlen) |
| 175 |
{ |
| 176 |
stb_uint i; |
| 177 |
for (i=0; i < maxlen; ++i) |
| 178 |
if (m1[i] != m2[i]) return i; |
| 179 |
return i; |
| 180 |
} |
| 181 |
|
| 182 |
// simple implementation that just takes the source data in a big block |
| 183 |
|
| 184 |
static stb_uchar *stb__out; |
| 185 |
static FILE *stb__outfile; |
| 186 |
static stb_uint stb__outbytes; |
| 187 |
|
| 188 |
static void stb__write(unsigned char v) |
| 189 |
{ |
| 190 |
fputc(v, stb__outfile); |
| 191 |
++stb__outbytes; |
| 192 |
} |
| 193 |
|
| 194 |
//#define stb_out(v) (stb__out ? *stb__out++ = (stb_uchar) (v) : stb__write((stb_uchar) (v))) |
| 195 |
#define stb_out(v) do { if (stb__out) *stb__out++ = (stb_uchar) (v); else stb__write((stb_uchar) (v)); } while (0) |
| 196 |
|
| 197 |
static void stb_out2(stb_uint v) { stb_out(v >> 8); stb_out(v); } |
| 198 |
static void stb_out3(stb_uint v) { stb_out(v >> 16); stb_out(v >> 8); stb_out(v); } |
| 199 |
static void stb_out4(stb_uint v) { stb_out(v >> 24); stb_out(v >> 16); stb_out(v >> 8 ); stb_out(v); } |
| 200 |
|
| 201 |
static void outliterals(stb_uchar *in, int numlit) |
| 202 |
{ |
| 203 |
while (numlit > 65536) { |
| 204 |
outliterals(in,65536); |
| 205 |
in += 65536; |
| 206 |
numlit -= 65536; |
| 207 |
} |
| 208 |
|
| 209 |
if (numlit == 0) ; |
| 210 |
else if (numlit <= 32) stb_out (0x000020 + numlit-1); |
| 211 |
else if (numlit <= 2048) stb_out2(0x000800 + numlit-1); |
| 212 |
else /* numlit <= 65536) */ stb_out3(0x070000 + numlit-1); |
| 213 |
|
| 214 |
if (stb__out) { |
| 215 |
memcpy(stb__out,in,numlit); |
| 216 |
stb__out += numlit; |
| 217 |
} else |
| 218 |
fwrite(in, 1, numlit, stb__outfile); |
| 219 |
} |
| 220 |
|
| 221 |
static int stb__window = 0x40000; // 256K |
| 222 |
|
| 223 |
static int stb_not_crap(int best, int dist) |
| 224 |
{ |
| 225 |
return ((best > 2 && dist <= 0x00100) |
| 226 |
|| (best > 5 && dist <= 0x04000) |
| 227 |
|| (best > 7 && dist <= 0x80000)); |
| 228 |
} |
| 229 |
|
| 230 |
static stb_uint stb__hashsize = 32768; |
| 231 |
|
| 232 |
// note that you can play with the hashing functions all you |
| 233 |
// want without needing to change the decompressor |
| 234 |
#define stb__hc(q,h,c) (((h) << 7) + ((h) >> 25) + q[c]) |
| 235 |
#define stb__hc2(q,h,c,d) (((h) << 14) + ((h) >> 18) + (q[c] << 7) + q[d]) |
| 236 |
#define stb__hc3(q,c,d,e) ((q[c] << 14) + (q[d] << 7) + q[e]) |
| 237 |
|
| 238 |
static unsigned int stb__running_adler; |
| 239 |
|
| 240 |
static int stb_compress_chunk(stb_uchar *history, |
| 241 |
stb_uchar *start, |
| 242 |
stb_uchar *end, |
| 243 |
int length, |
| 244 |
int *pending_literals, |
| 245 |
stb_uchar **chash, |
| 246 |
stb_uint mask) |
| 247 |
{ |
| 248 |
(void)history; |
| 249 |
int window = stb__window; |
| 250 |
stb_uint match_max; |
| 251 |
stb_uchar *lit_start = start - *pending_literals; |
| 252 |
stb_uchar *q = start; |
| 253 |
|
| 254 |
#define STB__SCRAMBLE(h) (((h) + ((h) >> 16)) & mask) |
| 255 |
|
| 256 |
// stop short of the end so we don't scan off the end doing |
| 257 |
// the hashing; this means we won't compress the last few bytes |
| 258 |
// unless they were part of something longer |
| 259 |
while (q < start+length && q+12 < end) { |
| 260 |
int m; |
| 261 |
stb_uint h1,h2,h3,h4, h; |
| 262 |
stb_uchar *t; |
| 263 |
int best = 2, dist=0; |
| 264 |
|
| 265 |
if (q+65536 > end) |
| 266 |
match_max = (stb_uint)(end-q); |
| 267 |
else |
| 268 |
match_max = 65536; |
| 269 |
|
| 270 |
#define stb__nc(b,d) ((d) <= window && ((b) > 9 || stb_not_crap((int)(b),(int)(d)))) |
| 271 |
|
| 272 |
#define STB__TRY(t,p) /* avoid retrying a match we already tried */ \ |
| 273 |
if (p ? dist != (int)(q-t) : 1) \ |
| 274 |
if ((m = stb_matchlen(t, q, match_max)) > best) \ |
| 275 |
if (stb__nc(m,q-(t))) \ |
| 276 |
best = m, dist = (int)(q - (t)) |
| 277 |
|
| 278 |
// rather than search for all matches, only try 4 candidate locations, |
| 279 |
// chosen based on 4 different hash functions of different lengths. |
| 280 |
// this strategy is inspired by LZO; hashing is unrolled here using the |
| 281 |
// 'hc' macro |
| 282 |
h = stb__hc3(q,0, 1, 2); h1 = STB__SCRAMBLE(h); |
| 283 |
t = chash[h1]; if (t) STB__TRY(t,0); |
| 284 |
h = stb__hc2(q,h, 3, 4); h2 = STB__SCRAMBLE(h); |
| 285 |
h = stb__hc2(q,h, 5, 6); t = chash[h2]; if (t) STB__TRY(t,1); |
| 286 |
h = stb__hc2(q,h, 7, 8); h3 = STB__SCRAMBLE(h); |
| 287 |
h = stb__hc2(q,h, 9,10); t = chash[h3]; if (t) STB__TRY(t,1); |
| 288 |
h = stb__hc2(q,h,11,12); h4 = STB__SCRAMBLE(h); |
| 289 |
t = chash[h4]; if (t) STB__TRY(t,1); |
| 290 |
|
| 291 |
// because we use a shared hash table, can only update it |
| 292 |
// _after_ we've probed all of them |
| 293 |
chash[h1] = chash[h2] = chash[h3] = chash[h4] = q; |
| 294 |
|
| 295 |
if (best > 2) |
| 296 |
assert(dist > 0); |
| 297 |
|
| 298 |
// see if our best match qualifies |
| 299 |
if (best < 3) { // fast path literals |
| 300 |
++q; |
| 301 |
} else if (best > 2 && best <= 0x80 && dist <= 0x100) { |
| 302 |
outliterals(lit_start, (int)(q-lit_start)); lit_start = (q += best); |
| 303 |
stb_out(0x80 + best-1); |
| 304 |
stb_out(dist-1); |
| 305 |
} else if (best > 5 && best <= 0x100 && dist <= 0x4000) { |
| 306 |
outliterals(lit_start, (int)(q-lit_start)); lit_start = (q += best); |
| 307 |
stb_out2(0x4000 + dist-1); |
| 308 |
stb_out(best-1); |
| 309 |
} else if (best > 7 && best <= 0x100 && dist <= 0x80000) { |
| 310 |
outliterals(lit_start, (int)(q-lit_start)); lit_start = (q += best); |
| 311 |
stb_out3(0x180000 + dist-1); |
| 312 |
stb_out(best-1); |
| 313 |
} else if (best > 8 && best <= 0x10000 && dist <= 0x80000) { |
| 314 |
outliterals(lit_start, (int)(q-lit_start)); lit_start = (q += best); |
| 315 |
stb_out3(0x100000 + dist-1); |
| 316 |
stb_out2(best-1); |
| 317 |
} else if (best > 9 && dist <= 0x1000000) { |
| 318 |
if (best > 65536) best = 65536; |
| 319 |
outliterals(lit_start, (int)(q-lit_start)); lit_start = (q += best); |
| 320 |
if (best <= 0x100) { |
| 321 |
stb_out(0x06); |
| 322 |
stb_out3(dist-1); |
| 323 |
stb_out(best-1); |
| 324 |
} else { |
| 325 |
stb_out(0x04); |
| 326 |
stb_out3(dist-1); |
| 327 |
stb_out2(best-1); |
| 328 |
} |
| 329 |
} else { // fallback literals if no match was a balanced tradeoff |
| 330 |
++q; |
| 331 |
} |
| 332 |
} |
| 333 |
|
| 334 |
// if we didn't get all the way, add the rest to literals |
| 335 |
if (q-start < length) |
| 336 |
q = start+length; |
| 337 |
|
| 338 |
// the literals are everything from lit_start to q |
| 339 |
*pending_literals = (int)(q - lit_start); |
| 340 |
|
| 341 |
stb__running_adler = stb_adler32(stb__running_adler, start, (stb_uint)(q - start)); |
| 342 |
return (int)(q - start); |
| 343 |
} |
| 344 |
|
| 345 |
static int stb_compress_inner(stb_uchar *input, stb_uint length) |
| 346 |
{ |
| 347 |
int literals = 0; |
| 348 |
stb_uint len,i; |
| 349 |
|
| 350 |
stb_uchar **chash; |
| 351 |
chash = (stb_uchar**) malloc(stb__hashsize * sizeof(stb_uchar*)); |
| 352 |
if (chash == nullptr) return 0; // failure |
| 353 |
for (i=0; i < stb__hashsize; ++i) |
| 354 |
chash[i] = nullptr; |
| 355 |
|
| 356 |
// stream signature |
| 357 |
stb_out(0x57); stb_out(0xbc); |
| 358 |
stb_out2(0); |
| 359 |
|
| 360 |
stb_out4(0); // 64-bit length requires 32-bit leading 0 |
| 361 |
stb_out4(length); |
| 362 |
stb_out4(stb__window); |
| 363 |
|
| 364 |
stb__running_adler = 1; |
| 365 |
|
| 366 |
len = stb_compress_chunk(input, input, input+length, length, &literals, chash, stb__hashsize-1); |
| 367 |
assert(len == length); |
| 368 |
|
| 369 |
outliterals(input+length - literals, literals); |
| 370 |
|
| 371 |
free(chash); |
| 372 |
|
| 373 |
stb_out2(0x05fa); // end opcode |
| 374 |
|
| 375 |
stb_out4(stb__running_adler); |
| 376 |
|
| 377 |
return 1; // success |
| 378 |
} |
| 379 |
|
| 380 |
stb_uint stb_compress(stb_uchar *out, stb_uchar *input, stb_uint length) |
| 381 |
{ |
| 382 |
stb__out = out; |
| 383 |
stb__outfile = nullptr; |
| 384 |
|
| 385 |
stb_compress_inner(input, length); |
| 386 |
|
| 387 |
return (stb_uint)(stb__out - out); |
| 388 |
} |