Alright, this is probably my last version. It now checks the input
for format against all the available conversions (found by looking for
all the programs in the filesystem that start with ppmto and pnmto) and
if you didn't pick one, gives you all the choices!
Enter desired output format (jpeg, png, tiff, gif, pcx, bmp) ... : fooburger
Expecting one of : acad bmp ddif eyuv fiasco fits gif icr ilbm jpeg
leaf lj lss16 map
mitsu mpeg neo palm pcx pgm pi1 pict pj plainpnm png ps puzz rast
rgb3 rle sgi sir
sixel tga tiff tiffcmyk uil winicon xpm xwd yuv yuvsplit
Enter desired output format (jpeg, png, tiff, gif, pcx, bmp) ... : tiffcmyk
Of course if you pick one that eog doesn't know how to display it
will complain. If you pick one that requires extra command line
argument, it will complain. If you comment out the line that removes ps
and select ps as the format type, then look at the resultant postscript
file with a postscript viewer, it's rotated 90 degrees! Cool:) That
looks like a bug in pnmtops. It's a shame that pnmtotiff doesn't have a
-transparent argument since tiff supports transparency.
I also fixed a problem where the validation routines would go crazy
if you just hit enter when prompted for input. Also made it not prompt
for transparency if you already specified a format on the command line
that doesn't support it, and conversely if you asked for transparency on
the command line and specified a format that doesn't support it, notes
your user error and asks you to pick gif or png.
Thanks for inspiring me Jonathan. I'd like to make clear that all
of this script that does any useful work is Jonathan's original script.
My parts just validate input and add command line arguments. I've
developed some general purpose routines for this script that I've wished
to have for a long time and will reuse often.:)
I can't believe I spent most of my day on this <grin;>!
Patrick
#!/bin/bash
#*****************************************************#
# Script for making image files from lilypond source #
# suitable for use as musical examples to insert in a #
# document or web page. #
#*****************************************************#
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# setformatlist - get's the list of all the things that
# you can convert to
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
setformatlist()
{
currentdir=`pwd`
examp=`which ppmtojpeg`
echo $examp
if [ $? -eq 0 ] ; then
OUTDIR="`dirname $examp`"
cd $OUTDIR
ppmtos=`ls ppmto* | sed -n s/ppmto//p`
pnmtos=`ls pnmto* | sed -n s/pnmto//p`
alltos=`echo $ppmtos $pnmtos | tr " " "\n" | sort -u | tr "\n" " "`
fi
cd $currentdir
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# usage is called when we're called incorrectly.
# it never returns
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
usage()
{
echo "Usage: " $0 " [-t] [-rN] [-fFORMAT] filename"
echo " -t indicates transparency is desired"
echo " -r=N set resolution to N (usually 72-2000)"
echo " -f=FORMAT set format to FORMAT one of:"
echo " jpeg, png, tiff, gif, pcx, bmp"
echo " filename a lilypond file"
exit -1
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Set prompt to the prompt you want to give a user
# goodvals to the list of acceptable values
# call getval
# when it returns your value is in outval
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
getval()
{
flag="notdone"
[EMAIL PROTECTED]
until [ $flag == "done" ] ; do
echo -n $prompt " "
read inval
if [ A$inval == "A" ] ; then
echo You must enter a value
index=0
echo -n "Expecting one of : "
while [ "$index" -lt "$elementcount" ] ; do
echo -n "${goodvals["$index"]}" " "
let index++
done
echo
else
index=0
while [ "$index" -lt "$elementcount" ] ; do
if [ ${goodvals[$index]} == $inval ] ; then
flag="done"
outval=${goodvals[$index]}
fi
let index++
done
if [ $flag != "done" ] ; then
index=0
echo -n "Expecting one of : "
while [ "$index" -lt "$elementcount" ] ; do
echo -n "${goodvals["$index"]}" " "
let index++
done
echo
fi
fi
done
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Set prompt to the prompt you want to give a user
# call getnumval
# when it returns your value is in outval
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
getnumval()
{
flag="notdone"
until [ $flag == "done" ] ; do
echo -n $prompt " "
read inval
if [ A$inval == "A" ] ; then
echo You must enter a value, expecting a positive numeric value
else
case $inval in
*[^0-9]*) echo "Error: expecting positive numeric value" ;;
* ) flag="done" ;;
esac
fi
done
outval=$inval
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# validatearg()
# set inarg to the
# set goodvals to the list of acceptable values
# set prompt to the error message you'd like to give,
# for example "ERROR: bad value for transparency arg"
# this routine will append to it, " expecting: " and
# the list of values from goodvals, then call usage
# to exit
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
validatearg()
{
flag="notgood"
[EMAIL PROTECTED]
index=0
if [ "A"$inarg != "A" ] ; then
while [ "$index" -lt "$elementcount" ] ; do
if [ ${goodvals[$index]} == $inarg ] ; then
flag="good"
outval=${goodvals[$index]}
fi
let index++
done
fi
if [ $flag != "good" ] ; then
index=0
echo -n $prompt
echo -n " expecting one of : "
while [ "$index" -lt "$elementcount" ] ; do
echo -n "${goodvals["$index"]}" " "
let index++
done
echo
usage
fi
}
getopt_simple()
{
until [ -z "$1" ] ; do
if [ ${1:0:1} = '-' ] ; then
tmp=${1:1} # Strip off leading '-' . . .
if [ ${tmp:0:1} = '-' ] ; then
tmp=${tmp:1} # Allow double -
fi
parameter=${tmp%%=*} # Extract name.
value=${tmp##*=} # Extract value.
eval $parameter=$value
else
filename=$1
fi
shift
done
}
alltos=""
transparency='no'
t='no'
resolution=0
r=0
format='none'
f='none'
filename="none"
setformatlist
if [ $? -ne 0 ] ; then
echo "Sorry, you have to have the netpbm utilities installed to use this."
exit 0
fi
getopt_simple $*
if [ $filename == "none" ] ; then
usage
fi
if [ $t != 'no' ] ; then
transparency=$t
fi
case $r in
*[^0-9]*) echo "Error: resolution not numeric"; usage ;;
esac
if [ $r -ne 0 ] ; then
resolution=$r
fi
case $resolution in
*[^0-9]*) echo "Error: resolution must be positive numeric"; usage ;;
esac
if [ $f != 'none' ] ; then
format=$f
fi
if [ $format != "none" ] ; then
inarg=$format
goodvals=( $alltos )
prompt="Error: format arg incorrect"
validatearg
fi
# get filename from first argument
srcfile="`basename $filename`"
# get filename without .ly extension
STEM="`basename $filename .ly`"
# determine output directory
OUTDIR="`dirname $filename`"
if [[ $resolution -ne 0 ]] ; then
echo Resolution set to $resolution dots per inch.
RES=$resolution
else
# ask for output resolution
prompt="Enter output resolution in DPI (72, 100, 300, 600, etc.): "
getnumval
RES=$outval
fi
# ask for desired final output format
if [ "$transparency" == 'no' ] ; then
if [[ ( "$format" == 'gif') || ( "$format" == 'png' ) || ( "$format" ==
'none' ) ]] ; then
# if only prompt for transparency if format unset or set to gif or png
prompt="Transparency? y/N"
goodvals=("y" "Y" "n" "N")
getval
TRANSPARENCY=$outval
fi
else
TRANSPARENCY="Y"
fi
if [[ ( "$TRANSPARENCY" == "Y" ) || ( "$TRANSPARENCY" == "y" ) ]] ; then
transparency='yes'
if [[ ( "$format" != 'gif') && ( "$format" != 'png' ) ]] ; then
# if they ask for transparency and format's not gif or png
# then ask them what they want?
if [[ "$format" != 'none' ]] ; then
echo "You ask for transparency which doesn't work with" $format
fi
prompt="Enter desired output format (png, gif): "
goodvals=("png" "gif")
getval
FORMAT=$outval
else
FORMAT=$format
fi
else
if [[ $format != 'none' ]] ; then
echo Output format is $format
FORMAT=$format
else
prompt="Enter desired output format (jpeg, png, tiff, gif, pcx, bmp ...
) : "
goodvals=( $alltos )
getval
FORMAT=$outval
fi
fi
cd $OUTDIR
# run lilypond on file with png output for further processing...
lilypond --format=png -dresolution=$RES $srcfile
# The next commands crop the png file so that
# it only includes the example instead of an entire page.
# First convert image to pnm for processing with netpbm tools
pngtopnm $STEM.png > $STEM.pnm
# crop all the white space off
pnmcrop -white $STEM.pnm > $STEM-cropped.pnm
outcmd="invalid"
which >& /dev/null ppmto$FORMAT
if [ $? -eq 0 ] ; then
outcmd=ppmto$FORMAT
else
which >& /dev/null pnmto$FORMAT
if [ $? -eq 0 ] ; then
outcmd=pnmto$FORMAT
fi
fi
if [ $outcmd == "invalid" ] ; then
echo "Sorry, can't find a command for that format."
exit -1
fi
# convert to end format
if [[ $transparency != 'no' ]] ; then
$outcmd -transparent '#ffffff' $STEM-cropped.pnm > $STEM.$FORMAT
else
$outcmd $STEM-cropped.pnm > $STEM.$FORMAT
fi
# removes pnm and ps files
rm *.pnm $STEM.ps
# open final image as background process in "Eye of Gnome" Image Viewer
eog $STEM.$FORMAT &
_______________________________________________
lilypond-user mailing list
lilypond-user@gnu.org
http://lists.gnu.org/mailman/listinfo/lilypond-user