#!/bin/tcsh -ef
#
# ugppm2mpg: converts a series of ppm-files to an mpeg-clip
# (adapted from ugm2mpeg)
# 
# Michael Lampe, Oct 1998
# 

set help = "usage: ugppm2mpg <basename> -f <first> <last> [-fi <increment>]"

if ( $#argv != 4 && $#argv != 6 ) then
	echo "$help"
	exit
endif

#
# defaults
#

@ fi = 1		# frame increment

#
# parse commandline
#

set base = $argv[1]

if ( "$argv[2]" == "-f" ) then
	@ fs = $argv[3]
	@ fe = $argv[4]
else
	echo "$help"
	exit
endif

if ( $#argv > 4 ) then
	if ( "$argv[5]" == "-fi" )  then
		@ fi = $argv[6]
	else
		echo "$help"
		exit
	endif
endif

#
# convert PPMs to YUVs
#

echo ""

@ i = $fs
@ j = 1
while ( $i <= $fe )
	set infile = `echo $base $i | awk '{printf "%s.%04d",$1,$2}'`  
	set outfile = $base.$j
	echo "Converting PPM to YUV: $infile"
	ppmtoyuvsplit $outfile $infile >& /dev/null
	echotc up
	@ i += $fi
	@ j++
end
echo "Converting PPM to YUV: done                                 "

#
# Now convert the YUVs to an MPG
#

echo "Converting YUV to MPG: wait..."

#
# get image size
#

set header = `awk '! /^#/ {print; if (++lines==3) exit;}' $infile`
@ width  = $header[2]
@ height = $header[3]

#
#  if #rows/columns is odd, ppmtoyuvsplit chopped off a row/column
#

if ( $width  % 2 ) @ width--
if ( $height % 2 ) @ height--

#
#  mpeg needs an option if width/height is not divisible by 16
#

set PF = ""
if ( $width % 16 || $height % 16 ) set PF = "-PF"

#
# make MPG
#

@ j--
mpeg -a 1 -b $j -h $width -v $height $PF $base. -s $base.mpg >& /dev/null

#
# clean up
#

rm -f $base.*.[YUV]

echotc up
echo "Converting YUV to MPG: done   "
echotc bl
echo ""
echo "$base.mpg created." 
echo ""
