#!/bin/csh
##
## Valeur des constantes du script
##
##
## Fin des constantes
##

##
## La syntaxe de la commande:
##

if ($# == 0 | "$1" == "-h" | "$1" == "-help") then
    echo ""
    echo "     usage: $0 numero_premier numero_dernier prefixe nom_film"
    echo ""
    echo "
    echo "numero_premier : numero du premier fichier png"
    echo "numero_dernier : numero du dernier fichier png"
    echo ""
    echo "Le format du nom des fichiers png est: prefixe???.png"
    echo "i.e. un format de longueur 3 pour le numero." 
    echo ""
    echo "Pour finir un mpeg nommer nom_film.mpg sera cree."
    echo ""    
    ##
else
    ##
    ## on traite l'argv 
    ##

    set debut = $1
    set fin   = $2
    set prefixe = $3
    set nom_film = $4

    set i=$debut
##
## On suppose que l'on a un format de nom de fichier de la forme: prefixe???.png
## i.e. un format de longueur 3 pour le numero. On va le transformer si necessaire
##

    set neuf = 9
    set quatrevingtdixneuf = 99
    while  ($i <= $fin)
        if($i<= $quatrevingtdixneuf) then
          if($i<= $neuf) then
	    convert $prefixe"00"$i.png $prefixe$i.ppm
            echo convert $prefixe"00"$i.png $prefixe$i.ppm
          else
	    convert $prefixe"0"$i.png $prefixe$i.ppm
            echo convert $prefixe"0"$i.png $prefixe$i.ppm
          endif
        else
	  convert $prefixe$i.png $prefixe$i.ppm
          echo convert $prefixe$i.png $prefixe$i.ppm
        endif
	set i = `expr $i + 1 `
    end
    
    ##
    ## on cree le fichier de donne de ppmtompeg et on fait le mpeg
    ##
    ##
    ## D'abord l'epilogue du fichier param
    ##
    rm -f film_fin.param
    echo "##### PAS TOUCHER EN BAS #####" > film_fin.param
    echo "GOP_SIZE        15" >> film_fin.param
    echo "SLICES_PER_FRAME        1" >> film_fin.param
    
    echo "BASE_FILE_FORMAT        PPM" >> film_fin.param
    echo "YUV_SIZE        352x240" >> film_fin.param
    
    echo "INPUT_CONVERT   *" >> film_fin.param
    
    echo "INPUT_DIR       ./" >> film_fin.param
    
    echo "PATTERN         IBBPBBPBBPBBPBBPBBPBBP" >> film_fin.param
    
    echo "# quality parameters" >> film_fin.param
    
    echo "IQSCALE         8" >> film_fin.param
    echo "PQSCALE         10" >> film_fin.param
    echo "BQSCALE         25" >> film_fin.param
    
    echo "# motion vector search parameters" >> film_fin.param
    
    echo "PIXEL           HALF" >> film_fin.param
    
    echo "RANGE           10 4" >> film_fin.param
    
    echo "# LOGARITHMIC, TWOLEVEL, SUBSAMPLE, EXHAUSTIVE" >> film_fin.param
    echo "PSEARCH_ALG      LOGARITHMIC" >> film_fin.param
    
    echo "# CROSS2, SIMPLE" >> film_fin.param
    echo "BSEARCH_ALG     CROSS2" >> film_fin.param
    
    echo "# DECODED or ORIGINAL" >> film_fin.param
    echo "REFERENCE_FRAME ORIGINAL" >> film_fin.param

    echo "INPUT" > film.$prefixe.param
    echo "$prefixe*.ppm [$debut-$fin]" >> film.$prefixe.param
    echo "END_INPUT" >> film.$prefixe.param
    echo "OUTPUT ./$nom_film.mpg" >> film.$prefixe.param
    cat film_fin.param >> film.$prefixe.param
##
## on execute 
##
    ppmtompeg film.$prefixe.param

##
## diagnostik a la sortie:
##
    if (-e ./$nom_film.mpg) then
        echo ""
        echo "Film cree sur $nom_film.mpg"
    else
        echo "ECHEC LORS DE LA CREATION DE $nom_film.mpg"
    endif


##
## on detruit les images ppm
##
    echo -n "Conserver les ppm film_fin.param et film.$prefixe.param ? (o/n) "
    set PROCEED = $<
    if ($PROCEED == "n") then
      rm -f film_fin.param film.$prefixe.param $prefixe*.ppm
    endif
endif
