#!/bin/sh  
# \
exec oagtclsh "$0" "$@"

#
# $Log: not supported by cvs2svn $
# Revision 1.3  2004/12/14 23:08:39  soliday
# Added the single column mode.
#
# Revision 1.2  2004/12/13 17:10:31  soliday
# Changed from using ppm2tiff to pnmtotiff because pnmtotiff comes from the
# Netpbm package which can be compiled on all major operating systems.
#
# Revision 1.1  2004/12/10 22:51:01  soliday
# avi2sdds is used to take an AVI file and convert it to a
# series of PPM image files using ffmpeg. These PPM images are then
# converted to uncompressed TIFF images using ppm2tiff. Then these
# TIFF images are converted into SDDS files using tiff2sdds. These
# SDDS files are then combined using sddscombine to form one SDDS file.
#
#
#

# set the path to pick up OAG libraries
set auto_path [linsert $auto_path 0  /usr/local/oag/apps/lib/$env(HOST_ARCH)]
set auto_path [linsert $auto_path 0 /usr/local/oag/lib_patch/$env(HOST_ARCH)]
set CVSRevisionAuthor "\$Revision: 1.4 $ \$Author: soliday $"

#########################################################################
#
#  ConvertAVI2SDDS is used to take an AVI file and convert it to a 
#  series of PPM image files using ffmpeg. These PPM images are then
#  converted to uncompressed TIFF images using pnmtotiff. Then these
#  TIFF images are converted into SDDS files using tiff2sdds. These 
#  SDDS files are then combined using sddscombine to form one SDDS file.
# 
#  There seem to be issues with ffmpeg where it creates too many
#  images for a given duration with some AVI files. To work around
#  this I read the frames per second (fps) from the AVI file
#  and calculate how many images I am supposed to end up with.
#
#########################################################################
proc ConvertAVI2SDDS {args} {
    set duration ""
    set position ""
    set singleColumnMode 0
    set query 0
    APSParseArguments {input output duration position query singleColumnMode}
    if {![file exists $input]} {
	puts stderr "input file $input does not exist"
	exit 1
    }
    if {$query} {
        set duration .001
        set position 0.0
    }
    set timespan ""
    if {[llength $duration]} {
        set timespan $duration
        set duration "-t $duration"
    }
    if {[llength $position]} {
        set position "-ss $position"
    }
    set tiff2sddsOptions ""
    if {$singleColumnMode == 1} {
        set tiff2sddsOptions "-singleColumnMode"
    }

    set tmpFiles /tmp/[APSTmpString]
    if {[catch {eval exec ffmpeg \
                  -y -i $input ${tmpFiles}\\\%d.ppm $duration $position} result]} {
    }
    if {$query} {
        set i1 [string first Input $result ]
        set i2 [string first Output $result ]
        puts [string range $result $i1 [expr {$i2 - 1}]]
        set files [glob -nocomplain ${tmpFiles}*.ppm ${tmpFiles}*.tiff ${tmpFiles}*.sdds]
        if {[llength $files]} {
            eval file delete -force $files
        }
        return
    }
    if {[llength $timespan]} {
        set fps [lindex $result [expr {[lsearch -exact $result fps] - 1}]]
        set framesExpected [expr {int($fps * $timespan) + 1}]
    } else {
        set j 0
        set i [expr {[lsearch -exact $result "frame="] + 1}]
        while {$i != 0} {
            set j [expr {$i + $j}]
            set i [expr {[lsearch -exact [lrange $result $j end] "frame="] + 1}]
        }
        set framesExpected [lindex $result $j]
    }

    set sddsFiles ""
    for {set i 1} {$i <= $framesExpected} {incr i} {
        file delete -force ${tmpFiles}${i}.tiff
        if {[catch {exec pnmtotiff -none -color -truecolor ${tmpFiles}${i}.ppm > ${tmpFiles}${i}.tiff} results]} {
            puts stderr "Error: $results"
            exit 1            
        }
        file delete -force ${tmpFiles}${i}.ppm
        if {[catch {eval exec tiff2sdds $tiff2sddsOptions ${tmpFiles}${i}.tiff ${tmpFiles}${i}.sdds} results]} {
            puts stderr "Error: $results"
            exit 1
        }
        file delete -force ${tmpFiles}${i}.tiff
        lappend sddsFiles ${tmpFiles}${i}.sdds
    }
    if {[catch {eval exec sddscombine $sddsFiles $output -overwrite} results]} {
        puts stderr "Error: $results"
        exit 1
    }

    set files [glob -nocomplain ${tmpFiles}*.ppm]
    if {[llength $files]} {
        eval file delete -force $files
    }
    set files [glob -nocomplain ${tmpFiles}*.tiff]
    if {[llength $files]} {
        eval file delete -force $files
    }
    set files [glob -nocomplain ${tmpFiles}*.sdds]
    if {[llength $files]} {
        eval file delete -force $files
    }
    return
}

set input ""
set output ""
set duration ""
set position ""
set singleColumnMode 0
set query 0
set args $argv
APSStrictParseArguments {input output duration position query singleColumnMode}
if {(![llength $input]) || (($query == 0) && (![llength $output]))} {
    puts stderr "usage: avi2sdds -input <input> -output <output>
       \[-duration <seconds>\] \[-offset <seconds>\]
       \[-singleColumnMode <1|0>\]
       \[-query <1|0>\] (Used to return info about AVI file)
       [join [split $CVSRevisionAuthor $] ""]"
    exit 1
}

ConvertAVI2SDDS -input $input -output $output -duration $duration -position $position -query $query -singleColumnMode $singleColumnMode

