a811ab0ba7f6915cce376ec6b2a7180ee1d04d5f

Author
Robby Russell <robby@planetargon.com>
Committer
Robby Russell <robby@planetargon.com>
Date

Message

Merge pull request #2331 from posva/catimg

Catimg plugin

Diff

  1diff --git a/plugins/catimg/catimg.plugin.zsh b/plugins/catimg/catimg.plugin.zsh
  2new file mode 100644
  3index 0000000000000000000000000000000000000000..cb92f5986bed461891798be5019ee45b3b53c3d2
  4--- /dev/null
  5+++ b/plugins/catimg/catimg.plugin.zsh
  6@@ -0,0 +1,17 @@
  7+################################################################################
  8+# catimg script by Eduardo San Martin Morote aka Posva                         #
  9+# http://posva.net                                                             #
 10+#                                                                              #
 11+# Ouput the content of an image to the stdout using the 256 colors of the      #
 12+# terminal.                                                                    #
 13+# Github: https://github.com/posva/catimg                                      #
 14+################################################################################
 15+
 16+
 17+function catimg() {
 18+  if [[ -x  `which convert` ]]; then
 19+    zsh $ZSH/plugins/catimg/catimg.sh $@
 20+  else
 21+    echo "catimg need convert (ImageMagick) to work)"
 22+  fi
 23+}
 24diff --git a/plugins/catimg/catimg.sh b/plugins/catimg/catimg.sh
 25new file mode 100755
 26index 0000000000000000000000000000000000000000..cd0f2e3335e537083ed6e1211c8e96d3e2a4d0e4
 27--- /dev/null
 28+++ b/plugins/catimg/catimg.sh
 29@@ -0,0 +1,88 @@
 30+################################################################################
 31+# catimg script by Eduardo San Martin Morote aka Posva                         #
 32+# http://posva.net                                                             #
 33+#                                                                              #
 34+# Ouput the content of an image to the stdout using the 256 colors of the      #
 35+# terminal.                                                                    #
 36+# Github: https://github.com/posva/catimg                                      #
 37+################################################################################
 38+
 39+function help() {
 40+  echo "Usage catimg [-h] [-w width] [-c char] img"
 41+  echo "By default char is \"  \" and w is the terminal width"
 42+}
 43+
 44+# VARIABLES
 45+COLOR_FILE=$(dirname $0)/colors.png
 46+CHAR="  "
 47+
 48+WIDTH=""
 49+IMG=""
 50+
 51+while getopts qw:c:h opt; do
 52+  case "$opt" in
 53+    w) WIDTH="$OPTARG" ;;
 54+    c) CHAR="$OPTARG" ;;
 55+    h) help; exit ;;
 56+    *) help ; exit 1;;
 57+    esac
 58+  done
 59+
 60+while [ "$1" ]; do
 61+  IMG="$1"
 62+  shift
 63+done
 64+
 65+if [ "$IMG" = "" -o ! -f "$IMG" ]; then
 66+  help
 67+  exit 1
 68+fi
 69+
 70+if [ ! "$WIDTH" ]; then
 71+  COLS=$(expr $(tput cols) "/" $(echo -n "$CHAR" | wc -c))
 72+else
 73+  COLS=$(expr $WIDTH "/" $(echo -n "$CHAR" | wc -c))
 74+fi
 75+WIDTH=$(convert "$IMG" -print "%w\n" /dev/null)
 76+if [ "$WIDTH" -gt "$COLS" ]; then
 77+  WIDTH=$COLS
 78+fi
 79+
 80+REMAP=""
 81+if convert "$IMG" -resize $COLS\> +dither -remap $COLOR_FILE /dev/null ; then
 82+  REMAP="-remap $COLOR_FILE"
 83+else
 84+  echo "The version of convert is too old, don't expect good results :(" >&2
 85+  #convert "$IMG" -colors 256 PNG8:tmp.png
 86+  #IMG="tmp.png"
 87+fi
 88+
 89+# Display the image
 90+I=0
 91+convert "$IMG" -resize $COLS\> +dither `echo $REMAP` txt:- 2>/dev/null |
 92+sed -e 's/.*none.*/NO NO NO/g' -e '1d;s/^.*(\(.*\)[,)].*$/\1/g;y/,/ /' |
 93+while read R G B f; do
 94+  if [ ! "$R" = "NO" ]; then
 95+    if [ "$R" -eq "$G" -a "$G" -eq "$B" ]; then
 96+      ((
 97+      I++,
 98+      IDX = 232 + R * 23 / 255
 99+      ))
100+    else
101+      ((
102+      I++,
103+      IDX = 16
104+      + R * 5 / 255 * 36
105+      + G * 5 / 255 * 6
106+      + B * 5 / 255
107+      ))
108+    fi
109+    #echo "$R,$G,$B: $IDX"
110+    echo -ne "\e[48;5;${IDX}m${CHAR}"
111+  else
112+    (( I++ ))
113+    echo -ne "\e[0m${CHAR}"
114+  fi
115+  # New lines
116+  (( $I % $WIDTH )) || echo -e "\e[0m"
117+done
118diff --git a/plugins/catimg/colors.png b/plugins/catimg/colors.png
119new file mode 100644
120index 0000000000000000000000000000000000000000..5f2c8126bc0d8911a9de5d3ffbd4e37372b41671
121Binary files /dev/null and b/plugins/catimg/colors.png differ