#!/bin/bash
#
# Identifies all png's in the current directory that contain transparent,colored pixels (which can be problematic). 
# See http://trac.wildfiregames.com/ticket/2823
#
# requires 'idenify' of the image magick commandline tool http://www.imagemagick.org/script/identify.php
# available on debian and ubuntu via sudo apt-get install imagemagick

for filename in *.png; do

	# first grep for histogram lines, like
	#        26: (  2,  1,  1,  0) #02010100 srgba(2,1,1,0)
	# then extract text of each line between the sixth = last comma and the first parenthesis after that
	for alpha in `identify -verbose $filename | grep 'srgba(' | cut -d ',' -f 7 | cut -d ')' -f 1`; do

		if [ "$alpha" == "0" ]
		then
			echo "Fixing $filename"
			mv $filename "${filename}_old.png";
			convert  "${filename}_old.png" -fx 'a==0 ? 0 : u' "$filename";
			continue 2;
		fi

	done

	#echo "$filename is ok.";
done
