Add a -color flag to example/gifplayer

This commit is contained in:
Nigel Tao committed 2018-05-25 11:34:40 +10:00
1 parent 6a494e4c4f
commit 97e73c10c8
1 file changed
+107 -8
+107 -8
View File
@@ -19,11 +19,16 @@ play Eadweard Muybridge's iconic galloping horse animation, run:
$cc gifplayer.c && ./a.out < ../../test/data/muybridge.gif; rm -f a.out
for a C compiler $cc, such as clang or gcc.
Add the -color flag to a.out to get 24 bit color ("true color") terminal output
(in the UTF-8 format) instead of plain ASCII output. Not all terminal emulators
support true color: https://gist.github.com/XVilka/8346728
*/
#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
@@ -91,12 +96,39 @@ const char* read_stdin() {
return "input is too large";
}
// ----
// BYTES_PER_COLOR_PIXEL is long enough to contain "?\x1B[38;2;255;255;255mABC"
// plus a few bytes of slack. The first byte is the overall length - a Pascal
// style string. After that is a true color terminal escape code. ABC is three
// bytes for the UTF-8 representation "\xE2\x96\x88" of "█", U+2588 FULL BLOCK.
#define BYTES_PER_COLOR_PIXEL 32
const char* full_block_pascal_string = "\x03\xE2\x96\x88";
const char* reset_color_pascal_string = "\x04\x1B[0m";
uint8_t* append_pascal_string(uint8_t* dst, const char* s) {
uint8_t len = *s++;
while (len--) {
*dst++ = *s++;
}
return dst;
}
bool color_flag = false;
const int stdout_fd = 1;
char palette_as_ascii_art[256];
char palette_as_color_art[256][BYTES_PER_COLOR_PIXEL]; // Pascal style strings.
void reset_palette_as_ascii_art() {
memset(palette_as_ascii_art, '-', 256);
}
void reset_palette_as_color_art() {
memset(palette_as_color_art, 0, 256 * BYTES_PER_COLOR_PIXEL);
}
// update_palette_as_ascii_art calculates a grayscale value and therefore an
// ASCII art character for each (red, green, blue) palette entry.
void update_palette_as_ascii_art(wuffs_base__slice_u8 palette) {
@@ -118,7 +150,25 @@ void update_palette_as_ascii_art(wuffs_base__slice_u8 palette) {
}
}
void show_ascii_art(wuffs_base__image_buffer* ib) {
// update_palette_as_color_art calculates a terminal escape code for each (red,
// green, blue) palette entry.
void update_palette_as_color_art(wuffs_base__slice_u8 palette) {
if (!palette.ptr || (palette.len != 256 * 4)) {
reset_palette_as_color_art();
return;
}
uint32_t i;
for (i = 0; i < 256; i++) {
int b = palette.ptr[4 * i + 0];
int g = palette.ptr[4 * i + 1];
int r = palette.ptr[4 * i + 2];
palette_as_color_art[i][0] =
sprintf(palette_as_color_art[i] + 1, "\x1B[38;2;%d;%d;%dm", r, g, b);
}
}
void print_ascii_art(wuffs_base__image_buffer* ib) {
wuffs_base__image_config* ic = wuffs_base__image_buffer__image_config(ib);
uint32_t width = wuffs_base__image_config__width(ic);
uint32_t height = wuffs_base__image_config__height(ic);
@@ -134,12 +184,45 @@ void show_ascii_art(wuffs_base__image_buffer* ib) {
}
*p++ = '\n';
}
const int stdout_fd = 1;
write(stdout_fd, print_buffer, print_len);
write(stdout_fd, print_buffer, p - print_buffer);
}
void print_color_art(wuffs_base__image_buffer* ib) {
wuffs_base__image_config* ic = wuffs_base__image_buffer__image_config(ib);
uint32_t width = wuffs_base__image_config__width(ic);
uint32_t height = wuffs_base__image_config__height(ic);
int32_t previous_palette_index = -1;
uint8_t* d = dst_buffer;
uint8_t* p = print_buffer;
*p++ = '\n';
p = append_pascal_string(p, reset_color_pascal_string);
uint32_t y;
for (y = 0; y < height; y++) {
uint32_t x;
for (x = 0; x < width; x++) {
uint8_t palette_index = *d++;
if (palette_index != previous_palette_index) {
p = append_pascal_string(p, palette_as_color_art[palette_index]);
previous_palette_index = palette_index;
}
p = append_pascal_string(p, full_block_pascal_string);
}
*p++ = '\n';
}
p = append_pascal_string(p, reset_color_pascal_string);
write(stdout_fd, print_buffer, p - print_buffer);
}
// ----
const char* play() {
reset_palette_as_ascii_art();
if (color_flag) {
reset_palette_as_color_art();
} else {
reset_palette_as_ascii_art();
}
wuffs_gif__decoder dec;
wuffs_gif__decoder__initialize(&dec, WUFFS_VERSION, 0);
@@ -170,8 +253,9 @@ const char* play() {
return "could not allocate dst buffer";
}
uint64_t plen = 1 + ((uint64_t)(width) + 1) * (uint64_t)(height);
if (plen <= (uint64_t)SIZE_MAX) {
print_len = (size_t)plen;
uint64_t bytes_per_print_pixel = color_flag ? BYTES_PER_COLOR_PIXEL : 1;
if (plen <= ((uint64_t)SIZE_MAX) / bytes_per_print_pixel) {
print_len = (size_t)(plen * bytes_per_print_pixel);
print_buffer = malloc(print_len);
}
if (!print_buffer) {
@@ -203,7 +287,11 @@ const char* play() {
}
if (wuffs_base__image_buffer__palette_changed(&ib)) {
update_palette_as_ascii_art(wuffs_base__image_buffer__palette(&ib));
if (color_flag) {
update_palette_as_color_art(wuffs_base__image_buffer__palette(&ib));
} else {
update_palette_as_ascii_art(wuffs_base__image_buffer__palette(&ib));
}
}
#ifdef _POSIX_TIMERS
@@ -229,7 +317,11 @@ const char* play() {
(1000 * wuffs_base__image_buffer__duration(&ib)) /
WUFFS_BASE__FLICKS_PER_MILLISECOND;
show_ascii_art(&ib);
if (color_flag) {
print_color_art(&ib);
} else {
print_ascii_art(&ib);
}
// TODO: should a zero duration mean to show this frame forever?
}
@@ -244,6 +336,13 @@ int fail(const char* msg) {
}
int main(int argc, char** argv) {
int i;
for (i = 1; i < argc; i++) {
if (!strcmp(argv[i], "-color")) {
color_flag = true;
}
}
const char* msg = read_stdin();
if (msg) {
return fail(msg);