Created
January 13, 2018 07:54
-
-
Save calzoneman/fa8bc79db35d18eaa925388506653c40 to your computer and use it in GitHub Desktop.
Display images (in true color!) in a compatible terminal emulator using block characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <gd.h> | |
#include <stdio.h> | |
#include <string.h> | |
void process(gdImagePtr img) { | |
for (int j = 0; j < img->sy/2; j++) { | |
for (int i = 0; i < img->sx; i++) { | |
int top = gdImageGetPixel(img, i, j*2 ); | |
int bottom = gdImageGetPixel(img, i, j*2 + 1); | |
printf( | |
"\x1b[48;2;%d;%d;%dm\x1b[38;2;%d;%d;%dm\u2584", | |
(top >> 16) & 0xff, (top >> 8) & 0xff, top & 0xff, | |
(bottom >> 16) & 0xff, (bottom >> 8) & 0xff, bottom & 0xff | |
); | |
} | |
printf("\x1b[0m\n"); | |
} | |
} | |
int main(int argc, char *argv[]) { | |
if (argc != 2) { | |
fprintf(stderr, "Usage: %s <input file>\n", argv[0]); | |
return 1; | |
} | |
FILE *in; | |
if (strcmp(argv[1], "-") == 0) { | |
in = stdin; | |
} else { | |
in = fopen(argv[1], "rb"); | |
} | |
gdImagePtr img = gdImageCreateFromPng(in); | |
if (img == NULL) { | |
fprintf(stderr, "Cannot load %s: not a PNG\n", argv[1]); | |
return 1; | |
} | |
process(img); | |
fclose(in); | |
gdImageDestroy(img); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment