#!/bin/bash

# c2016 by Paul Sherman http://www.pcbypaul.com 
# uses imagemagick, then pngcrush,
# 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

pth=`dirname "$1"`
cd $pth
ret=`identify -verbose "$1" | grep Colorspace`

Xdialog --title "It begins" --infobox "hang on a second.\n" 12 50 1000
tmpfile="/tmp/1.png"
#convert "$1" +dither -posterize 32 "$tmpfile"
convert "$1" -dither Riemersma -colors 32 "$tmpfile"
mogrify -define png:format=png32 -format png "$tmpfile"
pngcrush -reduce -brute -q "$tmpfile" "$1"  2>/dev/null
exiftool -Comment="Edited by Paul Sherman for WPClipart, Public Domain" -overwrite_original_in_place "$1"
Xdialog --title "DONE" --infobox "$1 \nhas been reduced to 32 colors\n" 16 90 4000
paplay /usr/share/sounds/opcomp.wav
