textPixel

Display images in terminal
git clone https://noulin.net/git/textPixel.git
Log | Files | Refs | README

textPixel.c (2958B)


      1 #! /usr/bin/env sheepy
      2 /* or direct path to sheepy: #! /usr/local/bin/sheepy */
      3 
      4 /* Libsheepy documentation: https://spartatek.se/libsheepy/ */
      5 #include "libsheepyObject.h"
      6 
      7 int argc; char **argv;
      8 
      9 /* enable/disable logging */
     10 /* #undef pLog */
     11 /* #define pLog(...) */
     12 
     13 typ struct {u16 rows; u16 cols;} winSizet;
     14 winSizet wsize (void);
     15 u32 text2Color(char *s);
     16 
     17 int main(int ARGC, char** ARGV) {
     18 
     19   argc = ARGC; argv = ARGV;
     20 
     21   initLibsheepy(ARGV[0]);
     22   setLogMode(LOG_FUNC);
     23   //openProgLogFile();
     24   //setLogSymbols(LOG_UTF8);
     25   //disableLibsheepyErrorLogs;
     26 
     27   if (argc < 2) {
     28     logE("Image name missing.");
     29     logI("Usage: textPixel imagePath [-w width]\nwidth must be a positive integer.");
     30     XFAILURE;
     31   }
     32 
     33   if (not isPath(argv[1])) {
     34     logE("File not found '%s'.", argv[1]);
     35     logI("Usage: textPixel imagePath [-w width]\nwidth must be a positive integer.");
     36     XFAILURE;
     37   }
     38 
     39   i32 width = 0;
     40 
     41   if (argc > 2) {
     42     if (argc != 4) {
     43       logE("Usage: textPixel imagePath [-w width]\nwidth must be a positive integer.");
     44       XFAILURE;
     45     }
     46     if (not eqG(argv[2], "-w")) {
     47       logE("Usage: textPixel imagePath [-w width]\nwidth must be a positive integer.");
     48       XFAILURE;
     49     }
     50     width = parseInt(argv[3]);
     51     if (not width) {
     52       logE("Usage: textPixel imagePath [-w width]\nwidth must be a positive integer.");
     53       XFAILURE;
     54     }
     55   }
     56 
     57   // find terminal size
     58   winSizet sz = wsize(); // window cols
     59 
     60   if (not width) width = sz.cols;
     61 
     62   var r = execOutf("convert %s -resize %d txt:-", argv[1], width);
     63   //logG(r);
     64 
     65   // r is a list of pixels like this:
     66   // 99,132: (22616,22616,22616)  #585858  srgb(88,88,88)
     67 
     68   rune rectangle = 0x2584;
     69   char rect[6] = init0Var;
     70   bRune2CodeUTF8(rect, rectangle);
     71   //printf("%K%k%s"RST, 0xFFFFFF, 0x99BBFF, rect); // print unicode character for 2 pixels
     72 
     73 
     74   // delete first line (not a pixel)
     75   delElemG(&r, 0);
     76 
     77   u64 count    = 0;
     78   u64 line     = 0;
     79   bool skip100 = no;
     80   var len      = lenG(r);
     81   // loop on pixels
     82   forEachS(r, s) {
     83     if (not skip100) {
     84       #define bottomPixel (count+ line*(width*2) +width)
     85       if (bottomPixel >= len) break;
     86       u32 colorUp   = text2Color(s);
     87       u32 colorDown = text2Color(r[bottomPixel]);
     88       printf("%K%k%s"RST, colorUp, colorDown, rect);
     89     }
     90     count++;
     91     if (count == width) {
     92       if (not skip100) {
     93         //printf("$"); // end of line
     94         put;
     95 	line++;
     96       }
     97       count = 0;
     98       skip100 = skip100 ? no : yes;
     99     }
    100   } // pixel loop
    101 }
    102 
    103 u32 text2Color(char *s) {
    104   char *color = findG(s, "#")-1;
    105   color[0] = '0';
    106   color[1] = 'x';
    107   char *end   = findG(color, " ");
    108   *end = 0;
    109   //logVarG(color);
    110   u32 r = parseHex(color);
    111   //printf("%x\n",hexColor);
    112   ret r;
    113 }
    114 
    115 /* terminal window size */
    116 #include <sys/ioctl.h>
    117 #include <unistd.h>
    118 
    119 winSizet wsize (void)
    120 {
    121     struct winsize w;
    122     ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
    123 
    124     return (winSizet){w.ws_row, w.ws_col};
    125 }
    126 
    127 // vim: set expandtab ts=2 sw=2: