|
|
@ -15,7 +15,7 @@
|
|
|
|
// You can also find a precompiled Windows binary in the binary/demo package available from https://github.com/ocornut/imgui
|
|
|
|
// You can also find a precompiled Windows binary in the binary/demo package available from https://github.com/ocornut/imgui
|
|
|
|
|
|
|
|
|
|
|
|
// Usage:
|
|
|
|
// Usage:
|
|
|
|
// binary_to_compressed_c.exe [-base85] [-nocompress] <inputfile> <symbolname>
|
|
|
|
// binary_to_compressed_c.exe [-base85] [-nocompress] [-nostatic] <inputfile> <symbolname>
|
|
|
|
// Usage example:
|
|
|
|
// Usage example:
|
|
|
|
// # binary_to_compressed_c.exe myfont.ttf MyFont > myfont.cpp
|
|
|
|
// # binary_to_compressed_c.exe myfont.ttf MyFont > myfont.cpp
|
|
|
|
// # binary_to_compressed_c.exe -base85 myfont.ttf MyFont > myfont.cpp
|
|
|
|
// # binary_to_compressed_c.exe -base85 myfont.ttf MyFont > myfont.cpp
|
|
|
@ -31,23 +31,25 @@ typedef unsigned int stb_uint;
|
|
|
|
typedef unsigned char stb_uchar;
|
|
|
|
typedef unsigned char stb_uchar;
|
|
|
|
stb_uint stb_compress(stb_uchar* out, stb_uchar* in, stb_uint len);
|
|
|
|
stb_uint stb_compress(stb_uchar* out, stb_uchar* in, stb_uint len);
|
|
|
|
|
|
|
|
|
|
|
|
static bool binary_to_compressed_c(const char* filename, const char* symbol, bool use_base85_encoding, bool use_compression);
|
|
|
|
static bool binary_to_compressed_c(const char* filename, const char* symbol, bool use_base85_encoding, bool use_compression, bool use_static);
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (argc < 3)
|
|
|
|
if (argc < 3)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
printf("Syntax: %s [-base85] [-nocompress] <inputfile> <symbolname>\n", argv[0]);
|
|
|
|
printf("Syntax: %s [-base85] [-nocompress] [-nostatic] <inputfile> <symbolname>\n", argv[0]);
|
|
|
|
return 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int argn = 1;
|
|
|
|
int argn = 1;
|
|
|
|
bool use_base85_encoding = false;
|
|
|
|
bool use_base85_encoding = false;
|
|
|
|
bool use_compression = true;
|
|
|
|
bool use_compression = true;
|
|
|
|
if (argv[argn][0] == '-')
|
|
|
|
bool use_static = true;
|
|
|
|
|
|
|
|
while (argn < (argc - 2) && argv[argn][0] == '-')
|
|
|
|
{
|
|
|
|
{
|
|
|
|
if (strcmp(argv[argn], "-base85") == 0) { use_base85_encoding = true; argn++; }
|
|
|
|
if (strcmp(argv[argn], "-base85") == 0) { use_base85_encoding = true; argn++; }
|
|
|
|
else if (strcmp(argv[argn], "-nocompress") == 0) { use_compression = false; argn++; }
|
|
|
|
else if (strcmp(argv[argn], "-nocompress") == 0) { use_compression = false; argn++; }
|
|
|
|
|
|
|
|
else if (strcmp(argv[argn], "-nostatic") == 0) { use_static = false; argn++; }
|
|
|
|
else
|
|
|
|
else
|
|
|
|
{
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Unknown argument: '%s'\n", argv[argn]);
|
|
|
|
fprintf(stderr, "Unknown argument: '%s'\n", argv[argn]);
|
|
|
@ -55,7 +57,7 @@ int main(int argc, char** argv)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool ret = binary_to_compressed_c(argv[argn], argv[argn + 1], use_base85_encoding, use_compression);
|
|
|
|
bool ret = binary_to_compressed_c(argv[argn], argv[argn + 1], use_base85_encoding, use_compression, use_static);
|
|
|
|
if (!ret)
|
|
|
|
if (!ret)
|
|
|
|
fprintf(stderr, "Error opening or reading file: '%s'\n", argv[argn]);
|
|
|
|
fprintf(stderr, "Error opening or reading file: '%s'\n", argv[argn]);
|
|
|
|
return ret ? 0 : 1;
|
|
|
|
return ret ? 0 : 1;
|
|
|
@ -67,7 +69,7 @@ char Encode85Byte(unsigned int x)
|
|
|
|
return (x >= '\\') ? x + 1 : x;
|
|
|
|
return (x >= '\\') ? x + 1 : x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool binary_to_compressed_c(const char* filename, const char* symbol, bool use_base85_encoding, bool use_compression)
|
|
|
|
bool binary_to_compressed_c(const char* filename, const char* symbol, bool use_base85_encoding, bool use_compression, bool use_static)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// Read file
|
|
|
|
// Read file
|
|
|
|
FILE* f = fopen(filename, "rb");
|
|
|
|
FILE* f = fopen(filename, "rb");
|
|
|
@ -90,10 +92,11 @@ bool binary_to_compressed_c(const char* filename, const char* symbol, bool use_b
|
|
|
|
FILE* out = stdout;
|
|
|
|
FILE* out = stdout;
|
|
|
|
fprintf(out, "// File: '%s' (%d bytes)\n", filename, (int)data_sz);
|
|
|
|
fprintf(out, "// File: '%s' (%d bytes)\n", filename, (int)data_sz);
|
|
|
|
fprintf(out, "// Exported using binary_to_compressed_c.cpp\n");
|
|
|
|
fprintf(out, "// Exported using binary_to_compressed_c.cpp\n");
|
|
|
|
|
|
|
|
const char* static_str = use_static ? "static " : "";
|
|
|
|
const char* compressed_str = use_compression ? "compressed_" : "";
|
|
|
|
const char* compressed_str = use_compression ? "compressed_" : "";
|
|
|
|
if (use_base85_encoding)
|
|
|
|
if (use_base85_encoding)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
fprintf(out, "static const char %s_%sdata_base85[%d+1] =\n \"", symbol, compressed_str, (int)((compressed_sz + 3) / 4)*5);
|
|
|
|
fprintf(out, "%sconst char %s_%sdata_base85[%d+1] =\n \"", static_str, symbol, compressed_str, (int)((compressed_sz + 3) / 4)*5);
|
|
|
|
char prev_c = 0;
|
|
|
|
char prev_c = 0;
|
|
|
|
for (int src_i = 0; src_i < compressed_sz; src_i += 4)
|
|
|
|
for (int src_i = 0; src_i < compressed_sz; src_i += 4)
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -112,8 +115,8 @@ bool binary_to_compressed_c(const char* filename, const char* symbol, bool use_b
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
else
|
|
|
|
{
|
|
|
|
{
|
|
|
|
fprintf(out, "static const unsigned int %s_%ssize = %d;\n", symbol, compressed_str, (int)compressed_sz);
|
|
|
|
fprintf(out, "%sconst unsigned int %s_%ssize = %d;\n", static_str, symbol, compressed_str, (int)compressed_sz);
|
|
|
|
fprintf(out, "static const unsigned int %s_%sdata[%d/4] =\n{", symbol, compressed_str, (int)((compressed_sz + 3) / 4)*4);
|
|
|
|
fprintf(out, "%sconst unsigned int %s_%sdata[%d/4] =\n{", static_str, symbol, compressed_str, (int)((compressed_sz + 3) / 4)*4);
|
|
|
|
int column = 0;
|
|
|
|
int column = 0;
|
|
|
|
for (int i = 0; i < compressed_sz; i += 4)
|
|
|
|
for (int i = 0; i < compressed_sz; i += 4)
|
|
|
|
{
|
|
|
|
{
|
|
|
|