#!/usr/bin/env bash
ME="[makeslides]:"
. tovid-init

# makeslides
# Part of the tovid suite
# =======================
# This script converts one or more still images in
# nearly any format into still-image MPEG video
# files for use as a VCD or SVCD slideshow.
#
# Project homepage: http://www.tovid.org
#
#
# Copyright (C) 2005 tovid.org <http://www.tovid.org>
# 
# This program is free software; you can redistribute it and/or 
# modify it under the terms of the GNU General Public License 
# as published by the Free Software Foundation; either 
# version 2 of the License, or (at your option) any later 
# version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. Or see:
#
#           http://www.gnu.org/licenses/gpl.txt

SCRIPTNAME=`cat << EOF
--------------------------------
makeslides
A script to create VCD MPEG1 stills from images
Part of the tovid suite, version $TOVID_VERSION
$TOVID_HOME_PAGE
--------------------------------
EOF`

USAGE=`cat << EOF
Usage: makeslides [OPTIONS] {IMAGES}

Where OPTIONS are any of the following:

  -ntsc      Generate NTSC (704x480) output (default)
  -pal       Generate PAL (704x576) output
  -verbose   Print extra output from subprocesses

  IMAGES is a single image or list of images to be
    converted to still MPEGs. The output names will
    be the same as the input name, with a .mpg extension.

See the makeslides manual page ('man makeslides') for additional documentation.

EOF`

# Print usage notes and optional error message, then exit.
# Args: $@ == text string containing error message
usage_error ()
{
  echo "$USAGE"
  echo "$SEPARATOR"
  echo "$@"
  exit 1
}

if mplex 2>&1 | grep -q "version 1.6.2"; then
    CHROMA_MODE="420_mpeg2"
else
    CHROMA_MODE="420mpeg2"
fi

# ***********************************
# DEFAULTS AND FUNCTION DEFINITIONS
# ***********************************

SEPARATOR="========================================================="

# Set defaults
WIDTH="704"
HEIGHT="480"
MPEG2ENC_FMT="-a 2 -f 6 -T 120"
PPM_OPTS="-S $CHROMA_MODE -A 10:11 -F 30000:1001"
VERBOSE_MPEG2ENC="-v 0"
VERBOSE_PPM="-v 0"
VERBOSE_IM=""
VERBOSE="false"

echo $"$SCRIPTNAME"

assert_dep "$magick" "You are missing dependencies required for making VCD stills!"

if test $# -eq 0; then usage_error "*** Please specify at least one image."; fi

while test x"${1:0:1}" = "x-"; do
  case "$1" in
     # PAL or NTSC
     "-pal" )
        HEIGHT="576"
        PPM_OPTS="-S $CHROMA_MODE -A 59:54 -F 25:1"
        ;;
     "-ntsc" | "-ntscfilm" )
        HEIGHT="480"
        PPM_OPTS="-S $CHROMA_MODE -A 10:11 -F 30000:1001"
        ;;
     "-verbose" )
        VERBOSE_MPEG2ENC="-v 1"
        VERBOSE_PPM="-v 1"
        VERBOSE_IM="-verbose"
        VERBOSE=:
        ;;
     * )
        usage_error "Error: Unrecognized command-line option $1"
        ;;
  esac

  # Get next argument
  shift
done

# Create a black background to composite each image over
# Use unusual filename to prevent overwriting anything important
BLACK_BG="makeslides.$PPID.black.png"
if $VERBOSE; then 
  echo $SEPARATOR
  echo "Making a black background for images..."
fi
convert $VERBOSE_IM -size ${WIDTH}x${HEIGHT} xc:black $BLACK_BG

# Process remaining arguments as image filenames
while test $# -gt 0; do
  CUR_IMAGE=$1
  EXT=$(echo "$CUR_IMAGE" | awk -F '.' '{ print $NF }')
  FILENAME=$(basename "$CUR_IMAGE" ."$EXT")
  if $VERBOSE; then echo $SEPARATOR; fi
  
  echo "Processing image: $CUR_IMAGE -> $FILENAME.mpg"
  if $VERBOSE; then
    echo $SEPARATOR
    echo "Resizing and centering $CUR_IMAGE on background..."
  fi
  composite $VERBOSE_IM -resize ${WIDTH}x${HEIGHT} -gravity center \
    "$CUR_IMAGE" $BLACK_BG -depth 8 "$FILENAME.ppm"
  
  if $VERBOSE; then 
    echo $SEPARATOR
    echo "Making an MPEG still from $CUR_IMAGE..."
  fi
  ppmtoy4m -n 1 $PPM_OPTS "$FILENAME.ppm" $VERBOSE_PPM | \
    mpeg2enc $MPEG2ENC_FMT -o "$FILENAME.mpg" $VERBOSE_MPEG2ENC
  
  rm "$FILENAME.ppm"
  shift
done

rm $BLACK_BG
echo "Done"
exit 0
