#!/bin/bash

# c2017 by Paul Sherman http://www.pcbypaul.com 
# uses pngquant, imagemagick to reduce colors, yet output an rgb file, 
# and finally exiftool to put in ps, wpclipart stuff.

if [ "$1" == "" ]; then
  $dialog --title "No PNG to convert" --msgbox "\nThis utility needs to be passed the filename of a PNG image, including the path.\n" 0 0
  exit 0
fi

selection=$(zenity --list "256" "64" "32" "24" "16" --column="number of colors" --text="Please Choose the" --title="PNG Color Reduction")

case "$selection" in
"256")num=256;;
"64")num=64;;
"32")num=32;;
"24")num=24;;
"16")num=16;;
*)exit;;
esac

pth=`dirname "$1"`
cd $pth
newname=`echo "$1" | sed 's/\.png/ZZZ\.png/'`
Xdialog --title "It begins" --infobox "hang on a second.\n" 12 50 1000
pngquant --ext ZZZ.png $num "$1"
exiftool -Comment="Edited by Paul Sherman for WPClipart, Public Domain" -overwrite_original_in_place "$newname"
mogrify -modulate 100,110,100 -define png:big-depth=8 -colors $num -define png:color-type=2 "$newname"
mv -f "$newname" "$1"
Xdialog --title "DONE" --infobox "$1 \nhas been reduced to $num colors\n" 16 90 4000
paplay /usr/share/sounds/opcomp.wav