#!/bin/bash
#Copyright (C) E. Rosten 2005
#This file is licensed under the GNU General Public License

TMP="${TMP:-/tmp}"
num=0
filelist=""

function temp_file()
{
	local file
	file="$TMP/eps_to_artk-$((num++))-$$$RANDOM.tmp"

	filelist="$filelist $file"
	export  $1="$file"
}


if [ $# != 1 ]
then
cat << FOO
`basename $0`: error: incorrect arguments.

Usage:
`basename $0` infile.{ps|eps|epsi}

Converts encapsulated postscript files to ARTK markers: a pattern file and a 
printable marker file which contains the required thick black border.

Creates (in the current directory)
infile.marker.eps                 printable ARTK marker
infile.pat                        ARTK pattern file
infile.marker.opt.eps             printable optimized ARTK marker
infile.opt.pat                    optimized ARTK pattern file
FOO

exit
fi

file="$1"

#Attempt to get basename:
base="`basename "$file" | sed -e'
/\.ps$/s/\.ps$//
/\.eps$/s/\.eps$//
/\.epsi$/s/\.epsi$//'`"

marker="${base}.marker.eps"
pattern="${base}.pat"
opt_marker="${base}.marker.opt.eps"
opt_pattern="${base}.opt.pat"


################################################################################
#
# Convert eps file in to a marker eps (one with a large black border) and a
# a "pattern eps" which is in a suitable state for rasterizing.

temp_file pattern_eps


eps_to_marker_eps='
!/^%%/{
	row[NR] = $0
}
/^%%BoundingBox/ && !/end/ {
	left = $2
	bottom = $3
	right = $4
	top = $5
}

END{
	centre_x = 288   #page centre
	centre_y = 414   
	fsize    = 180	 #final size of white square
	border   = .0    #border around marker


	width = right - left
	height = top - bottom

	size = width > height ? width : height
	fs = fsize * (1-border);


	print "%!PS-Adobe-2.0 EPSF-2.0"												>	mout
	print "%%BoundingBox: " centre_x-fsize, centre_y-fsize, centre_x+fsize, centre_y + fsize > mout
	print "%%EndComments" 														>	mout
	print "1 dict begin /showpage {} def gsave"									> 	mout
	print  centre_x - fsize, centre_y - fsize, fsize*2, fsize*2, "rectfill"		>   mout
	print "1 setgray"															>   mout
	print  centre_x - fsize/2, centre_y - fsize/2, fsize, fsize, "rectfill"		>   mout
	print centre_x, centre_y, "translate"										>   mout
	print fs / size, fs / size, "scale"											>   mout
	print -width/2-left, -height/2-bottom, "translate"							>   mout

	for(x=1; x<=NR; x++)
		print row[x]															>  	mout
	print "grestore end showpage"												>	mout

	print "%!PS-Adobe-2.0 EPSF-2.0"												>	psout
	print "%%BoundingBox: 0 0 ", fsize, fsize 									>	psout
	print "%%EndComments" 														>	psout
	print "1 dict begin /showpage {} def gsave"									> 	psout
	print fsize/2, fsize/2, "translate"											>   psout
	print fs / size, fs / size, "scale"											>   psout
	print -width/2-left, -height/2-bottom, "translate"							>   psout

	for(x=1; x<=NR; x++)
		print row[x]															>  	psout
	print "grestore end showpage"												>	psout
}
' 

awk -v mout="$marker" -v psout="$pattern_eps"  "$eps_to_marker_eps" "$file"
################################################################################
#
# Now rasterize the "pattern eps" with 16x antialiasing
# 



temp_file tmpf1
temp_file tmpf2
temp_file tmpf3
temp_file tmpf4
temp_file tmpf5

#Rasterize
pstopnm -stdout -pgm -xsize 256 -ysize 256 -xborder 0 -yborder 0 "$pattern_eps" 2>/dev/null | pnmscale -xsize 16 > "$tmpf1"

#Create rotations
pnmflip -r90  "$tmpf1" > "$tmpf2"
pnmflip -r180 "$tmpf1" > "$tmpf3"
pnmflip -r270 "$tmpf1" > "$tmpf4"

#Create an empty pattern file
cat /dev/null > "$pattern"

for i in "$tmpf1" "$tmpf2" "$tmpf3" "$tmpf4"
do
	#We know the size is 16x16, and pnmnoraw has no comments and
	#at 16 pixels wide produces 1 line per line
	pnmnoraw $i | awk 'NR>3{for(i=1; i <= 16; i++)printf("%4s",$i);print""}' > "$tmpf5"

	#Append 3 copies to pattern (one red, one green, one blue)"
	cat "$tmpf5" "$tmpf5" "$tmpf5" >> "$pattern"
	echo >> "$pattern"
done


################################################################################
#
# Now create the "optimal" (quantized) pattern
#

pnmdepth 1 "$tmpf1" | pnmdepth 255 > "$tmpf5"
mv "$tmpf5" "$tmpf1"
#Create rotations
pnmflip -r90  "$tmpf1" > "$tmpf2"
pnmflip -r180 "$tmpf1" > "$tmpf3"
pnmflip -r270 "$tmpf1" > "$tmpf4"

#Create an empty pattern file
cat /dev/null > "$opt_pattern"

for i in "$tmpf1" "$tmpf2" "$tmpf3" "$tmpf4"
do
	#We know the size is 16x16, and pnmnoraw has no comments and
	#at 16 pixels wide produces 1 line per line
	pnmnoraw $i | awk 'NR>3{for(i=1; i <= 16; i++)printf("%4s",$i);print""}' > "$tmpf5"

	#Append 3 copies to pattern (one red, one green, one blue)"
	cat "$tmpf5" "$tmpf5" "$tmpf5" >> "$opt_pattern"
	echo >> "$opt_pattern"
done

#Now turn the quantised bitmap back in to a postscript file
#pnmtops seems to introduce a rotation...
pnmtops -scale 30 -noturn -center "$tmpf4" > "$tmpf5"
awk -v mout="$opt_marker" -v psout="/dev/null"  "$eps_to_marker_eps" "$tmpf5"



rm -f $filelist
