example/std-imagedumper: add -braille-art flag

This commit is contained in:
Nigel Tao committed 2024-09-10 07:46:07 +10:00
1 parent 9e76d545b6
commit 8aa51dfa49
1 file changed
+193 -15
+193 -15
View File
@@ -45,7 +45,24 @@ of four: JPEG and PNG. Wuffs' reimplementation decodes all four demo images.
Define the USE_ONLY_JPEG macro to limit the variety of image file formats that Define the USE_ONLY_JPEG macro to limit the variety of image file formats that
Wuffs decodes to just JPEG, for smaller binaries and faster compiles. Wuffs decodes to just JPEG, for smaller binaries and faster compiles.
Pass the -ascii-art flag to print ASCII art instead of ANSI color codes. Pass the -a or -ascii-art flag to print ASCII art instead of ANSI color codes.
Pass the -b or -braille-art flag to print Unicode Braille Pattern characters
instead of ANSI color codes. For example:
----
$ stb-imagedumper -braille-art test/data/pjw-thumbnail.png
test/data/pjw-thumbnail.png
⣿⣿⣿⣿⡿⡋⠉⠉⠉⠙⠻⢿⣿⣿⣿⣿
⣿⣿⣿⡿⣷⣿⣿⣿⣆⠀⠀⠀⠈⠹⣿⣿
⣿⣿⠃⢸⡿⢿⣿⣿⣿⠦⠀⠀⠀⠀⢼⣿
⣿⡁⠀⠀⣫⣁⡡⣅⢀⠀⠀⠀⠀⠀⣽⣿
⣿⣷⡤⣺⣟⣫⠽⠿⠌⢑⠑⠀⠀⣶⣿⣿
⣿⣿⣇⢻⣟⣛⣛⣂⠀⢁⢁⠀⠀⣿⣿⣿
⣿⣿⣿⣿⣯⢽⣶⣤⠆⠈⠠⢬⣽⣿⣿⣿
⣿⣿⣿⣿⣷⡩⡂⠀⠀⠀⠀⣾⣿⣿⣿⣿
----
To run: To run:
@@ -397,11 +414,128 @@ const size_t g_src_len__mona_lisa_21_32_png = 1600;
// ---------------- // ----------------
// g_braille holds the Unicode Code Point bit masks for the 8 dots in a Unicode
// Braille Pattern character (a character in U+2800 ..= U+28FF).
const uint8_t g_braille[4][2] = {
{0x01, 0x08},
{0x02, 0x10},
{0x04, 0x20},
{0x40, 0x80},
};
// g_noise is a 32×32 blue noise texture, based on "LDR_LLL1_42.png" (but
// tweaked so that the values range in 0x00 ..= 0xfe) from the zip file linked
// to from http://momentsingraphics.de/BlueNoise.html (the files within are
// mirrored at https://github.com/Calinou/free-blue-noise-textures) and whose
// LICENSE.txt says "To the extent possible under law, Christoph Peters has
// waived all copyright and related or neighboring rights".
const uint8_t g_noise[32][32] = {
{0x3c, 0xd4, 0xb6, 0x77, 0x64, 0xab, 0xef, 0x03, 0x7d, 0x2f, 0xc0,
0x24, 0x79, 0x3e, 0x87, 0x9a, 0x36, 0x7f, 0xbc, 0xf0, 0x42, 0xaf,
0x2d, 0x63, 0x22, 0xc3, 0x4e, 0x33, 0xe9, 0x67, 0x9c, 0x01},
{0xa9, 0x23, 0x50, 0xdd, 0x36, 0x26, 0x4b, 0x94, 0xdc, 0x14, 0xfb,
0x47, 0x5f, 0xf2, 0x13, 0x53, 0xe7, 0xd7, 0x28, 0x8a, 0x07, 0xc6,
0x54, 0xe5, 0xb5, 0x0e, 0x78, 0xae, 0x14, 0x84, 0xc0, 0x20},
{0x7d, 0x95, 0xea, 0x06, 0x8a, 0xfa, 0xc3, 0x70, 0x5c, 0xa6, 0x88,
0xcc, 0x09, 0xba, 0xa6, 0x68, 0x1e, 0xb3, 0x5a, 0xa5, 0x68, 0xfd,
0x18, 0x90, 0x70, 0x3a, 0xf0, 0x5f, 0xca, 0x44, 0x2b, 0x58},
{0x12, 0xbd, 0x43, 0x61, 0xb0, 0x9d, 0x0e, 0x3f, 0xec, 0xb6, 0x36,
0x6e, 0x94, 0xde, 0x2f, 0xce, 0x77, 0x92, 0x11, 0xe2, 0x37, 0x81,
0xa0, 0x46, 0xcd, 0xdf, 0xa3, 0x05, 0x97, 0xdc, 0x73, 0xe4},
{0x35, 0x6d, 0xf2, 0xc7, 0x1d, 0x56, 0x7b, 0xce, 0x2a, 0x17, 0x54,
0xe9, 0x21, 0x7e, 0x4e, 0xeb, 0x3b, 0xf9, 0x47, 0xca, 0x75, 0xb9,
0xd9, 0x0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, 0x11,
0xfd, 0x99, 0xc4, 0x37, 0x8e, 0x65, 0xf0, 0x96, 0x87, 0x1d},
{0xa8, 0x98, 0x21, 0xaf, 0x72, 0x16, 0xdf, 0x28, 0x4c, 0xa9, 0x1e,
0x56, 0x32, 0xb4, 0xec, 0x7b, 0x25, 0x41, 0x86, 0xb5, 0x21, 0x69,
0x84, 0x28, 0xeb, 0x76, 0xe1, 0x53, 0x05, 0xc5, 0x35, 0x71},
{0xdc, 0x80, 0xc1, 0x57, 0x31, 0x90, 0xb9, 0xf5, 0x0e, 0xce, 0x75,
0xf8, 0xdb, 0x49, 0x91, 0x5b, 0xe1, 0x10, 0xf6, 0x33, 0xe6, 0xa6,
0xbd, 0x5c, 0x48, 0xaa, 0x16, 0xba, 0xa2, 0xe4, 0x5e, 0x0f},
{0x2e, 0x4a, 0xf7, 0x0a, 0xe6, 0x40, 0x5b, 0x9c, 0x6a, 0x3a, 0x8d,
0x07, 0x66, 0x19, 0x2b, 0x6f, 0xab, 0xc9, 0x9d, 0x74, 0x50, 0x0d,
0xdb, 0x3c, 0x07, 0xd0, 0x6b, 0x27, 0x40, 0x7a, 0xb1, 0xf2},
{0x8c, 0x6b, 0x18, 0x9f, 0xcb, 0x82, 0x1e, 0xd3, 0xb4, 0x52, 0xe5,
0x9f, 0xaf, 0xd4, 0xc3, 0xde, 0x02, 0x4a, 0x64, 0x1b, 0xd2, 0x93,
0x7b, 0xf3, 0x9e, 0x86, 0xfa, 0x92, 0xd6, 0x1c, 0x51, 0xc9},
};
// ----------------
struct { struct {
int remaining_argc; int remaining_argc;
char** remaining_argv; char** remaining_argv;
bool ascii_art; bool ascii_art;
bool braille_art;
bool demo; bool demo;
} g_flags = {0}; } g_flags = {0};
@@ -437,6 +571,10 @@ parse_flags(int argc, char** argv) {
g_flags.ascii_art = true; g_flags.ascii_art = true;
continue; continue;
} }
if (!strcmp(arg, "b") || !strcmp(arg, "braille-art")) {
g_flags.braille_art = true;
continue;
}
if (!strcmp(arg, "demo")) { if (!strcmp(arg, "demo")) {
g_flags.demo = true; g_flags.demo = true;
continue; continue;
@@ -465,7 +603,8 @@ handle(const char* filename, const uint8_t* src_ptr, const size_t src_len) {
int w = 0; int w = 0;
int h = 0; int h = 0;
int channels_in_file = 0; int channels_in_file = 0;
int bytes_per_pixel = g_flags.ascii_art ? STBI_grey : STBI_rgb; int bytes_per_pixel =
(g_flags.ascii_art || g_flags.braille_art) ? STBI_grey : STBI_rgb;
unsigned char* pixels = NULL; unsigned char* pixels = NULL;
if (src_len > 0) { if (src_len > 0) {
@@ -485,30 +624,69 @@ handle(const char* filename, const uint8_t* src_ptr, const size_t src_len) {
return; return;
} }
unsigned char* src = pixels;
static char buffer[MAX_INCL_DIMENSION * BYTES_PER_COLOR_PIXEL]; static char buffer[MAX_INCL_DIMENSION * BYTES_PER_COLOR_PIXEL];
for (int y = 0; y < h; y++) { if (g_flags.ascii_art) {
char* dst = buffer; unsigned char* src = pixels;
for (int x = 0; x < w; x++) { for (int y = 0; y < h; y++) {
if (g_flags.ascii_art) { char* dst = buffer;
for (int x = 0; x < w; x++) {
*dst++ = "-:=+IOX@"[(src[0] & 0xFF) >> 5]; *dst++ = "-:=+IOX@"[(src[0] & 0xFF) >> 5];
} else { src += bytes_per_pixel;
}
*dst++ = '\n';
fwrite(buffer, sizeof(char), dst - buffer, stdout);
}
} else if (g_flags.braille_art) {
// Each 2×4 pixel block becomes one Unicode Braille Pattern character. We
// also apply blue noise dithering, similar to what's described at
// https://surma.dev/things/ditherpunk/
for (int y = 0; y < h; y += 4) {
char* dst = buffer;
for (int x = 0; x < w; x += 2) {
dst[0] = 0xE2; // The 3-byte UTF-8 encoding of U+2800
dst[1] = 0xA0; // BRAILLE PATTERN BLANK. We'll then set
dst[2] = 0x80; // dst[1] and dst[2]'s low 2 and 6 bits.
for (int dy = 0; (dy < 4) && ((y + dy) < h); dy++) {
size_t ty = y + dy;
for (int dx = 0; (dx < 2) && ((x + dx) < w); dx++) {
size_t tx = x + dx;
uint8_t pixel = pixels[(ty * (size_t)w) + tx];
uint8_t threshold = g_noise[ty & 31][tx & 31];
if (pixel > threshold) {
uint8_t b = g_braille[dy][dx];
dst[1] |= b >> 6;
dst[2] |= b & 0x3f;
}
}
}
dst += 3;
}
*dst++ = '\n';
fwrite(buffer, sizeof(char), dst - buffer, stdout);
}
} else {
unsigned char* src = pixels;
for (int y = 0; y < h; y++) {
char* dst = buffer;
for (int x = 0; x < w; x++) {
// "\xE2\x96\x88" is U+2588 FULL BLOCK. Before that is a true color // "\xE2\x96\x88" is U+2588 FULL BLOCK. Before that is a true color
// terminal escape code. // terminal escape code.
dst += sprintf(dst, "\x1B[38;2;%d;%d;%dm\xE2\x96\x88", // dst += sprintf(dst, "\x1B[38;2;%d;%d;%dm\xE2\x96\x88", //
(uint8_t)src[0], (uint8_t)src[1], (uint8_t)src[2]); (uint8_t)src[0], (uint8_t)src[1], (uint8_t)src[2]);
src += bytes_per_pixel;
} }
src += bytes_per_pixel; *dst++ = '\n';
fwrite(buffer, sizeof(char), dst - buffer, stdout);
} }
*dst++ = '\n'; const char* reset_color_str = "\x1B[0m";
fwrite(buffer, sizeof(char), dst - buffer, stdout); const size_t reset_color_len = 4;
fwrite(reset_color_str, sizeof(char), reset_color_len, stdout);
} }
stbi_image_free(pixels);
const char* reset_color_str = "\x1B[0m";
const size_t reset_color_len = 4;
fwrite(reset_color_str, sizeof(char), reset_color_len, stdout);
fflush(stdout); fflush(stdout);
stbi_image_free(pixels);
} }
int // int //