#!/bin/sh
# the next line restarts using wish \
exec wish "$0" "$@"

# cam (c) 1997 by Till Harbaum (harbaum@ibr.cs.tu-bs.de)
# requires camediaplay, convert (from ImageMagick) and tcl/tk

# V0.1 23.9.97  quick and dirty

# CONFIGURE BELOW
set camedia    "../build/camediaplay"
set glob_flags "-b115200 -u"
set device     "cua0"
set tempname   "/tmp/camth%d_%%05d"

# size of thumbnail area
set width  6
set height 4
#CONFIGURE ABOVE

# size of one thumbnail
set tw 80
set th 60

proc disp_pic { temp no } {
    global tw th width height picno tagflag taganz

    set taganz 0
    set xpos [expr {(($tw+2)*(($no-1)-((($no-1)/$width)*$width)))+3}]
    set ypos [expr {($th+2)*(($no-1)/$width)+3}]

    exec convert [format $temp.jpg $no] [format $temp.gif $no]
    image create photo mypic($no) -file [format $temp.gif $no]
    set id [.canvas.c create image $xpos $ypos -image mypic($no) -anchor nw]
    set picno($id) $no    
    set tagflag($picno($id)) 0
    update idletasks
}

proc disp_thumbs { } {
    global tw th width height camedia glob_flags device tempname thumbs

    set temp [format $tempname [pid]] 

    # and now get all thumbnails
    set msg [.canvas.c create text [ expr {(($tw+2)*$width)/2}] [expr {(($th+2)*$height)/2}] \
	    -text "Please wait, getting thumbnails ..."]
    update idletasks
    puts [ eval exec $camedia $glob_flags -a -g -t -N$temp.jpg $device [format 2>/tmp/camedia%d.out [pid]]]
    .canvas.c delete $msg

    set i 1
    while { 0 != [file exists [format $temp.jpg $i]] } { 
	disp_pic $temp $i
	# delete thumbnail from hd
	exec rm [format $temp.jpg $i]
	exec rm [format $temp.gif $i]
	incr i
    }

    set thumbs [expr {$i-1}]

    # delete camedia output
    exec rm [format /tmp/camedia%d.out [pid]]

    # auf Mausklicks reagieren
    .canvas.c bind all <1> "SelectPicture .canvas.c"
    .canvas.c bind all <Double-1> "SavePicture .canvas.c"

    set sc_w [expr {$width*($tw+2)}]
    set sc_h [expr {$i/$width*($th+2)}]
    .canvas.c configure -scrollregion "0 0 $sc_w $sc_h "
}

proc SaveFile { no filename } {
    global camedia glob_flags picno device

    catch [eval exec $camedia $glob_flags -n$no -g -N$filename $device [format 2>/tmp/camedia%d.out [pid]]]
    exec rm [format /tmp/camedia%d.out [pid]]
}

proc SaveAllTag {} {
    global taganz thumbs tagflag

    if { $taganz == 0 } {
	for {set i 1} {$i <= $thumbs } { incr i} {
	   SaveFile $i pic%05d.jpg
	}
    } else {
	for {set i 1} {$i <= $thumbs } { incr i} {
	   if { $tagflag($i) == 1} {
	      SaveFile $i pic%05d.jpg
	   }
	}
    }
}

proc SavePicture canvas {
    global picno camedia glob_flags device tagflag taganz flag

    set types {
	{"JPEG image"	                        {.jpg .jpeg} }
	{"Graphics Interchange File Format"	{.gif} }
	{"Encapsulated Postscript"	        {.eps} }
	{"Targa"                                {.tga .targa}}
	{"Photo CD"	                        {.pcd} }
	{"Portable Network Graphics"	        {.png} }
	{"Portable Pixmap Format"	        {.ppm} }
	{"Microsoft Bitmap"	                {.bmp} }
	{"Tagged Image File Format"	        {.tiff .tif} }
	{"X Windows pixmap"	                {.xpm} }
	{"All files"	{*} } 
    }

    set id [$canvas find withtag current]

    if {$tagflag($picno($id)) == 1} {
	set tagflag($picno($id)) 0
	.canvas.c delete $flag($id)
	set taganz [expr {$taganz -1}]
	if { $taganz == 0 } {
	    .buttons.code configure -text "Save all"
	}
    }

    set file [tk_getSaveFile -parent .canvas -filetypes $types  -defaultextension .jpg \
	    -initialfile [format "pic%05d.jpg" $picno($id)] ]

    if [string compare $file ""] {
	catch [eval exec $camedia $glob_flags -n$picno($id) -g -N$file.jpg $device [format 2>/tmp/camedia%d.out [pid]]]
	exec convert $file.jpg $file
	exec rm $file.jpg
	exec xv $file "&"
	exec rm [format /tmp/camedia%d.out [pid]]
    }
}

proc SelectPicture canvas {
    global picno tagflag tw th width height flag taganz

    set id [$canvas find withtag current]

    set tagflag($picno($id)) [expr {! $tagflag($picno($id))}]

    if {$tagflag($picno($id)) == 1} {
	set xpos [expr {(($tw+2)*(($picno($id)-1)-((($picno($id)-1)/$width)*$width)))+3}]
	set ypos [expr {($th+2)*(($picno($id)-1)/$width)+3}]
	set flag($id) [.canvas.c create rectangle $xpos $ypos [expr {$xpos+10}] [expr {$ypos+10}] -fill green]
	incr taganz
    } else {
	.canvas.c delete $flag($id)
	set taganz [expr {$taganz-1}]
    }

    if { $taganz == 0 } {
	.buttons.code configure -text "Save all"
    } else {
	.buttons.code configure -text "Save tagged"
    }
}

#
# Create all windows, and pack them
#
proc createwindows { } {
    global tw th width height

    frame .canvas -relief raised -borderwidth 2
    canvas .canvas.c -width [expr {($tw+2)*$width}] -height [ expr {($th+2)*$height}] \
	    -scrollregion {0 0 1000 1000} -yscrollcommand ".canvas.scroll set" 
    scrollbar .canvas.scroll -command ".canvas.c yview"

    # show canvas
    pack .canvas
    pack .canvas.c
    grid .canvas.c -padx 1 -in .canvas -pady 1 \
	    -row 0 -column 0 -rowspan 1 -columnspan 1 -sticky news
    grid .canvas.scroll -in .canvas -padx 1 -pady 1 \
	    -row 0 -column 1 -rowspan 1 -columnspan 1 -sticky news

    frame .buttons
    pack  .buttons -side bottom -fill x -pady 2m
    button .buttons.dismiss -text Quit -command "destroy .; exit"
    button .buttons.code -text "Save all" -command SaveAllTag
    pack .buttons.dismiss .buttons.code -side left -expand 1

    pack .canvas .buttons -side top -fill both -ipady 5 -expand yes -fill both

    # Let the user resize our window
    # wm minsize . 10 10
}

##############################################################################
# Main program

# set tk_strictMotif 1

createwindows
disp_thumbs

#
# Now, wait for user actions...
#






