From 0185d9920b7d6c380277a4a15c9e4818df41700a Mon Sep 17 00:00:00 2001
From: Eduardo San Martin Morote <posva13@gmail.com>
Date: Mon, 9 Dec 2013 08:54:38 +0100
Subject: [PATCH] catimg plugin allow to print an image to the stdout using
 convert

---
 plugins/catimg/catimg.plugin.zsh |  16 ++++++
 plugins/catimg/catimg.sh         |  87 +++++++++++++++++++++++++++++++
 plugins/catimg/colors.png        | Bin 0 -> 353 bytes
 3 files changed, 103 insertions(+)
 create mode 100644 plugins/catimg/catimg.plugin.zsh
 create mode 100755 plugins/catimg/catimg.sh
 create mode 100644 plugins/catimg/colors.png

diff --git a/plugins/catimg/catimg.plugin.zsh b/plugins/catimg/catimg.plugin.zsh
new file mode 100644
index 000000000..01e81eca7
--- /dev/null
+++ b/plugins/catimg/catimg.plugin.zsh
@@ -0,0 +1,16 @@
+################################################################################
+# catimg script by Eduardo San Martin Morote aka Posva                         #
+# http://posva.net                                                             #
+#                                                                              #
+# Ouput the content of an image to the stdout using the 256 colors of the      #
+# terminal.                                                                    #
+# Github: https://github.com/posva/catimg                                      #
+################################################################################
+
+if [[ -x  `which convert` ]]; then
+  function catimg() {
+    source $(dirname $0)/catimg.sh $@
+  }
+else
+  echo "catimg need convert (ImageMagick) to work)"
+fi
diff --git a/plugins/catimg/catimg.sh b/plugins/catimg/catimg.sh
new file mode 100755
index 000000000..f9e067e34
--- /dev/null
+++ b/plugins/catimg/catimg.sh
@@ -0,0 +1,87 @@
+################################################################################
+# catimg script by Eduardo San Martin Morote aka Posva                         #
+# http://posva.net                                                             #
+#                                                                              #
+# Ouput the content of an image to the stdout using the 256 colors of the      #
+# terminal.                                                                    #
+# Github: https://github.com/posva/catimg                                      #
+################################################################################
+
+function help() {
+  echo "Usage catimg [-h] [-w width] [-c char] img"
+  echo "By default char is \"  \" and w is the terminal width"
+}
+
+# VARIABLES
+COLOR_FILE=$(dirname $0)/colors.png
+CHAR="  "
+
+WIDTH=""
+IMG=""
+
+while getopts qw:c:h opt; do
+  case "$opt" in
+    w) WIDTH="$OPTARG" ;;
+    c) CHAR="$OPTARG" ;;
+    h) help; exit ;;
+    *) help ; exit 1;;
+    esac
+  done
+
+while [ "$1" ]; do
+  IMG="$1"
+  shift
+done
+
+if [ "$IMG" = "" -o ! -f "$IMG" ]; then
+  help
+  exit 1
+fi
+
+if [ ! "$WIDTH" ]; then
+  COLS=$(expr $(tput cols) "/" $(echo -n "$CHAR" | wc -c))
+else
+  COLS=$(expr $WIDTH "/" $(echo -n "$CHAR" | wc -c))
+fi
+WIDTH=$(convert ${IMG} -print "%w\n" /dev/null)
+if [ "$WIDTH" -gt "$COLS" ]; then
+  WIDTH=$COLS
+fi
+
+REMAP=""
+if convert "$IMG" -resize $COLS\> +dither -remap $COLOR_FILE /dev/null ; then
+  REMAP="-remap $COLOR_FILE"
+else
+  echo "The version of convert is too old, don't expect good results :(" >&2
+  #convert "$IMG" -colors 256 PNG8:tmp.png
+  #IMG="tmp.png"
+fi
+
+# Display the image
+convert "$IMG" -resize $COLS\> +dither `echo $REMAP` txt:- |
+sed -e 's/.*none.*/NO NO NO/g' -e '1d;s/^.*(\(.*\)[,)].*$/\1/g;y/,/ /' |
+while read R G B f; do
+  if [ ! "$R" = "NO" ]; then
+    if [ "$R" -eq "$G" -a "$G" -eq "$B" ]; then
+      ((
+      I++,
+      IDX = 232 + R * 23 / 255
+      ))
+    else
+      ((
+      I++,
+      IDX = 16
+      + R * 5 / 255 * 36
+      + G * 5 / 255 * 6
+      + B * 5 / 255
+      ))
+    fi
+    #echo "$R,$G,$B: $IDX"
+    echo -ne "\e[48;5;${IDX}m${CHAR}"
+  else
+    (( I++ ))
+    echo -ne "\e[0m${CHAR}"
+  fi
+  # New lines
+  (( $I % $WIDTH )) || echo -e "\e[0m"
+done
diff --git a/plugins/catimg/colors.png b/plugins/catimg/colors.png
new file mode 100644
index 0000000000000000000000000000000000000000..5f2c8126bc0d8911a9de5d3ffbd4e37372b41671
GIT binary patch
literal 353
zcmV-n0iOPeP)<h;3K|Lk000e1NJLTq000mG000mO1^@s6AM^iV0003fNkl<ZcwS@t
z|NlP&BO~Mgcm@UrMIdGeVhtqB2$YCN69dUJ0u?s^#Tgme8UABc08~YcIOFx}4FAC<
z;?Tg!$-%(I#f1|yMnpvXU$KIn0jq7eoR4G~OSnEeL%lu&12$J&XCN%jvY`T^2xI_E
zLvk`R!+Q}ChQKZ^22enO2v7i5HlVr`q@M9VL;HV5BAkj6Gss@pu%3Z|7^fnK5lnq|
zH!}mv)2F-)?}4Fw5r`QyL8*WN6T~xM1!X{&u$=xZ&7koa5)RkfNe@iM|JT?5XT*^P
zHg9GI$3F`T3j-S)8<=fifEtjD*BP!u5)zpK$pQ*Q<S0aU4WR&L3D#zYCsRftL6TD>
zV|@Gn{~G8ykjRV#PQCxH|Hm1Kz--RQKmY&$#Kd628_Utk00000NkvXXu0mjflf#Hs

literal 0
HcmV?d00001