#####################################################################
;# NAME:   makeJpegSet.tcl
;# AUTHOR: Clif Flynt
;# DATE:   2/24/2007
;# DESC:   Extracts a frame from each of N JPEG images, places
;#         the smaller images in a subdirectory and makes a
;#         title page based on file names and hardcoded values.
;#
;# PARAMETERS:  
;#	wish makeJpegSet.tcl width height xoffset yoffset subdirName
;#
#####################################################################
# This code is written and copyrighted by Clif Flynt, 2007
#
# It is licenced for public use with these terms:
#
#  This licence agreement must remain intact with the code.
# 
#  You may use this code freely for commercial or non-commercial purposes.
#
#  You may examine and modify the code to your heart's content.
#
#  You may not claim to have authored the code (aside from modifications
#  you may have made.)
#
#  You may not distribute the code without this license agreement
#
#  You may sell this code or distribute it with a commercial product
#      without arrangements with Noumena Corporation.  Noumena and
#      Clif Flynt would like to be informed of the applications migratory
#      tendencies.
#
#  Maintenance, extension, modification, etc can be performed by:
#
#   Noumena Corporation
#   9300 Fleming Rd.
#   Dexter, MI  48130
#
#   Contact: clif@noucorp.com
#
#####################################################################

set rev(makeJpegSet.tcl) {$Header$}

package require Img

################################################################
# proc getFileName {{val 0}}--
#    Return a sequential file name between 000 and 999
# Arguments
#   Default value - do not reset
# 
# Results
#   Updates proc to return a new filename
# 
proc getFileName {{val 0}} {
  incr val
  proc getFileName "{val $val}" [info body getFileName]
  return [format %03d $val]
}

################################################################
# proc fadeOut {imgName destDir}--
#    Fade an image, write to requested directory
# Arguments
#   imgName 	Name of the image to fade to black
#   destDir	Directory to receive new files
# 
# Results
#   New jpeg image files are created.
#   Text on canvas is deleted.

proc fadeOut {imgName destDir} {
  puts "fadeOut $imgName $destDir"
  
  set im $imgName
  .c delete text

  for {set i 9} {$i >= 2} {incr i -1} {
      set val ".$i"
      $im configure -gamma $val
      update idle
  
      set tmp [image create photo -data .c]
      frame copy $tmp
      frame write $destDir/[getFileName].jpg -format jpeg
      image delete $tmp
    }
}



################################################################
# proc makeTitle {imgName destDir largeLines smallLines }--
#    
# Arguments
#   imgName 	Name of an image that contains the background to use
#   destDir 	Directory to receive new image filees
#   largeLines	List of lines for large text
#   smallLines	List of lines for small text
# 
# Results
#   New Jpeg images are made in the target directory
#   Filenames will be base-2.jpg, base-25.jpg, base-3.jpg, etc.
# 

proc makeTitle {imgName destDir largeLines smallLines } {
  puts "makeTitle $imgName $destDir largeLines smallLines"
  # Load the background image.

#  set im [image create photo -file $srcName]
  set im $imgName
  set wd [image width $im]
  set ht [image height $im]

  .c delete text

  # Calculate font sizes based on image width.

  set fontSize1 [expr {$wd/20}]
  set font1 "arial $fontSize1 bold"
  set fontSize2 [expr {$wd / 30}]
  set font2 "arial $fontSize2 bold"
  
  # Calculate the steps between lines of text based
  #  on font size.

  set step1 [font metrics $font1 -linespace]
  set step2 [font metrics $font2 -linespace]
  incr step1 10
  incr step2 5
  
  # Draw hardcoded text in hardcoded colors.

  set c [expr {$wd/2}]
  set top [expr {$ht/4}]
  foreach line $largeLines {
    .c create text $c $top -text $line -font $font1 -fill #6ff6f6 -tag text
    incr top $step1
  }

  foreach line $smallLines {
    .c create text $c $top -text $line -font $font2 -fill #6ff666 -tag text
    incr top $step2
  }
  
  
  # Set gamma to make the image mostly black, then step to nearly 
  # full brightness, saving at each step.

  for {set i 2} {$i <= 9} {incr i} {

    set val ".$i"
    $im configure -gamma $val
puts "    $im configure -gamma $val"
    update idle

    set tmp [image create photo -data .c]
    frame copy $tmp
    frame write $destDir/[getFileName].jpg -format jpeg
    image delete $tmp

    set val ".${i}5"
    $im configure -gamma $val
    update idle

    set tmp [image create photo -data .c]
    frame copy $tmp
    frame write $destDir/[getFileName].jpg -format jpeg
    image delete $tmp
  }
}

# 
# Mainline code starts here.
#

if {[llength $argv] < 5} {
  puts "wish extractSubset wd ht x y dirName"
  exit
}

foreach {wd ht x y dirName} $argv {break}

image create photo frame -height $ht -width $wd
image create photo title -height $ht -width $wd
  
pack [canvas .c -height $ht -width $wd -borderwidth 0]
.c create image 0 0 -image title -anchor nw

# Clean out any old files.

foreach fl [glob -nocomplain $dirName/*.jpg] {
  file delete $fl
}
catch {file delete tmp.jpg}

# Calculate the bottom right corner of the frame
#  to acquire.

set x2 [expr {$x + $wd}]
set y2 [expr {$y + $ht}]

set files [lsort -dict [glob 0*.jpg]]
foreach {mo1 da1 hr1 mn1} [split [file root [file tail [lindex $files 0]]] -.] {break}
foreach {mo2 da2 hr2 mn2} [split [file root [file tail [lindex $files end]]] -.] {break}

set prevDay 99
foreach fl $files {
  foreach {mm dd hh mn} [split [file root [file tail $fl]] -.] {break}
  if {![string match $prevDay $dd]} {
    if {[info exists prevDate]} {
      lappend timeStamps $prevDate
    }
    lappend timeStamps [list $mm $dd $hh $mn]
    set prevDay $dd
  }
  set prevDate [list $mm $dd $hh $mn]
}
lappend timeStamps $prevDate
puts "$timeStamps"

set fl [lindex $files 0]

title read $fl -from $x $y $x2 $y2

makeTitle title $dirName \
    [list {American Chestnut Seedlings} {Time Lapse Images} \
    "$mo1/$da1 $hr1:$mn1 - $mo2/$da2 $hr2:$mn2"] \
    {{Collected by Clif Flynt} {clif@cflynt.com}}


# file delete temp1.jpg
set prevDay $da1

foreach fl $files {
   puts -nonewline "."; flush stdout
   foreach {mm dd hh mn} [split [file root [file tail $fl]] -.] {break}

   if {![string match $dd $prevDay]} {
      title read $prevFile -from $x $y $x2 $y2
      fadeOut title $dirName
      set p [lsearch $timeStamps "$mm $dd*"]
      set start [lindex $timeStamps $p]
      incr p
      set end [lindex $timeStamps $p]
      foreach {mo1 da1 hr1 mn1} $start {break;}
      foreach {mo2 da2 hr2 mn2} $end {break;}

      title read $fl -from $x $y $x2 $y2

      makeTitle title $dirName \
        {{American Chestnut Seedling}} \
        [list "$mo1/$da1 $hr1:$mn1 - $mo2/$da2 $hr2:$mn2"]
	set prevDay $dd
   }
   frame read $fl -from $x $y $x2 $y2
   set fnm [getFileName]
   frame write $dirName/$fnm.jpg -format jpeg
   set prevFile $fl
}
 

exit 0
