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