Created
May 7, 2020 15:02
-
-
Save qookei/a3f857909bbd29c44b446b995baaf616 to your computer and use it in GitHub Desktop.
Terminal image viewer using Imlib2
This file contains hidden or 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 <iostream> | |
#include <Imlib2.h> | |
int main(int argc, char **argv) { | |
if(argc != 2) { | |
fprintf(stderr, "%s: usage: <image>\n", argv[0]); | |
return 1; | |
} | |
Imlib_Image img; | |
img = imlib_load_image_immediately(argv[1]); | |
if (!img) { | |
fprintf(stderr, "%s: failed to open image\n", argv[0]); | |
return 1; | |
} | |
imlib_context_set_image(img); | |
int w = imlib_image_get_width(), | |
h = imlib_image_get_height(); | |
uint32_t *pixels = imlib_image_get_data(); | |
auto set_color = [&](bool fg, uint32_t c) { | |
uint8_t r = ((c >> 16) & 0xFF); | |
uint8_t g = ((c >> 8) & 0xFF); | |
uint8_t b = (c & 0xFF); | |
printf("\x1B[%d;2;%d;%d;%dm", fg ? 38 : 48, r, g, b); | |
}; | |
for (int y = 0; y < (h + 1) / 2; y++) { | |
for (int x = 0; x < w; x++) { | |
int py = y * 2; | |
set_color(true, pixels[x + py * w]); | |
if (py + 1 < h) | |
set_color(false, pixels[x + (py + 1) * w]); | |
printf("▀"); | |
} | |
printf("\x1B[0m\n"); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment