]> src.twobees.de Git - dotfiles.git/blob - stow/oh-my-zsh/.oh-my-zsh/plugins/catimg/catimg.sh
f583924282c3e14c7cb92738375587c8e75752a2
[dotfiles.git] / stow / oh-my-zsh / .oh-my-zsh / plugins / catimg / catimg.sh
1 ################################################################################
2 # catimg script by Eduardo San Martin Morote aka Posva                         #
3 # https://posva.net                                                            #
4 #                                                                              #
5 # Output the content of an image to the stdout using the 256 colors of the     #
6 # terminal.                                                                    #
7 # GitHub: https://github.com/posva/catimg                                      #
8 ################################################################################
9
10 function help() {
11   echo "Usage catimg [-h] [-w width] [-c char] img"
12   echo "By default char is \"  \" and w is the terminal width"
13 }
14
15 # VARIABLES
16 COLOR_FILE=$(dirname $0)/colors.png
17 CHAR="  "
18
19 WIDTH=""
20 IMG=""
21
22 while getopts qw:c:h opt; do
23   case "$opt" in
24     w) WIDTH="$OPTARG" ;;
25     c) CHAR="$OPTARG" ;;
26     h) help; exit ;;
27     *) help ; exit 1;;
28     esac
29   done
30
31 while [ "$1" ]; do
32   IMG="$1"
33   shift
34 done
35
36 if [ "$IMG" = "" -o ! -f "$IMG" ]; then
37   help
38   exit 1
39 fi
40
41 if [ ! "$WIDTH" ]; then
42   COLS=$(expr $(tput cols) "/" $(echo -n "$CHAR" | wc -c))
43 else
44   COLS=$(expr $WIDTH "/" $(echo -n "$CHAR" | wc -c))
45 fi
46 WIDTH=$(convert "$IMG" -print "%w\n" /dev/null)
47 if [ "$WIDTH" -gt "$COLS" ]; then
48   WIDTH=$COLS
49 fi
50
51 REMAP=""
52 if convert "$IMG" -resize $COLS\> +dither -remap $COLOR_FILE /dev/null ; then
53   REMAP="-remap $COLOR_FILE"
54 else
55   echo "The version of convert is too old, don't expect good results :(" >&2
56   #convert "$IMG" -colors 256 PNG8:tmp.png
57   #IMG="tmp.png"
58 fi
59
60 # Display the image
61 I=0
62 convert "$IMG" -resize $COLS\> +dither `echo $REMAP` txt:- 2>/dev/null |
63 sed -e 's/.*none.*/NO NO NO/g' -e '1d;s/^.*(\(.*\)[,)].*$/\1/g;y/,/ /' |
64 while read R G B f; do
65   if [ ! "$R" = "NO" ]; then
66     if [ "$R" -eq "$G" -a "$G" -eq "$B" ]; then
67       ((
68       I++,
69       IDX = 232 + R * 23 / 255
70       ))
71     else
72       ((
73       I++,
74       IDX = 16
75       + R * 5 / 255 * 36
76       + G * 5 / 255 * 6
77       + B * 5 / 255
78       ))
79     fi
80     #echo "$R,$G,$B: $IDX"
81     echo -ne "\e[48;5;${IDX}m${CHAR}"
82   else
83     (( I++ ))
84     echo -ne "\e[0m${CHAR}"
85   fi
86   # New lines
87   (( $I % $WIDTH )) || echo -e "\e[0m"
88 done