#!/bin/sh
# De volgende lijn start het script opnieuw op met wish \
exec wish "$0" "$@"

#----------------------------------------------------------------
#  tkmuscan - TCL/TK frontend to the scanner driver for 
#  Mustek Paragon 6000CX scanner
#  Copyright (C) 1995 Chel van Gennip
#
#  This program uses muscan by Torsten Eichner <eichner@rhrk.uni-kl.de>
#  to do the actual scanning. 
#  This is a beta testversion, it is still under construction
#  it uses an extended version of muscan to generate deskjet output
#  for fast copying to the printer device.
#
#  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.
#
#  email:      chel$vangennip.nl
#----------------------------------------------------------------

eval destroy [winfo child .]
wm title . "Scanner program for Mustek Paragon scanner"
#wm geometry . 600x600

#----------------------------------------------------------------
# The main window
#----------------------------------------------------------------
set font -*-Helvetica-Medium-R-Normal--*-140-*-*-*-*-*-*
set tp_x 0.0
set tp_y 0.0
set bp_x [expr 8.3 * 18.0 ]
set bp_y [expr 11.7 * 18.0 ]
set speed 5
set grain 4
set mark 0
set debug "-v"
set debug " "

frame .menuBar
    pack .menuBar -side top -fill x
    menubutton .menuBar.file -text File -menu .menuBar.file.m -underline 0
    menu .menuBar.file.m
        .menuBar.file.m add command -label "Quit" -command "exit" -underline 0
    pack .menuBar.file -side left

canvas .sep1  -relief sunken -height 0 -borderwidth 2
    pack .sep1 -side top -fill x   

frame .b
    pack .b -side bottom
    button .b.quit -text quit  -command "exit"
    button .b.preview -text preview -command "do_preview"
    button .b.scan -text scan -command "do_scan"
    button .b.copy -text copy -command "do_copy"
    pack .b.quit .b.preview .b.scan .b.copy -side left -fill x

proc do_preview { } {
    exec mscan -b -r 18 -x 8 -y 10 -f /tmp/.scan.preview
    image create photo prv -file /tmp/.scan.preview
}


if { ![file exists /tmp/.scan.preview ] } {
    do_preview 
}

frame .p
    pack .p -side left -anchor nw
    canvas .p.c -relief sunken -width 160
    pack .p.c -side top
    image create photo prv -file /tmp/.scan.preview
    set rc [.p.c create rectangle $tp_x $tp_y $bp_x $bp_y  \
        -width 1 -outline blue]
    set im [.p.c create image 0 0  -image prv -anchor nw]

    bind .p.c <ButtonPress> "set tp_x %x ; set tp_y %y ; do_settop "
    bind .p.c <ButtonRelease> "set bp_x %x ; set bp_y %y ; do_setbottom "
    bind .p.c <Motion> "set bp_x %x ; set bp_y %y ; do_locate "

frame .m -relief sunken
pack .m -side right -anchor nw -fill x

frame .m.l1 
    pack .m.l1 -side top -anchor nw
    label .m.l1.modetxt -text "Mode:      "
    menubutton .m.l1.mode -text "Lineart" -menu .m.l1.mode.m -relief raised
        menu .m.l1.mode.m -tearoff false
        .m.l1.mode.m add command -label Color -command "setmode color"
        .m.l1.mode.m add command -label Gray -command "setmode gray"
        .m.l1.mode.m add command -label Halftone -command "setmode halftone"
        .m.l1.mode.m add command -label Lineart -command "setmode lineart"
    menubutton .m.l1.res -text "300" -menu .m.l1.res.m -relief raised
        menu .m.l1.res.m -tearoff false
        .m.l1.res.m add command -label 600 -command "setres 600"
        .m.l1.res.m add command -label 500 -command "setres 500"
        .m.l1.res.m add command -label 400 -command "setres 400"
        .m.l1.res.m add command -label 300 -command "setres 300"
        .m.l1.res.m add command -label 200 -command "setres 200"
        .m.l1.res.m add command -label 150 -command "setres 150"
        .m.l1.res.m add command -label 100 -command "setres 100"
        .m.l1.res.m add command -label 75 -command "setres 75"
    pack .m.l1.modetxt .m.l1.mode .m.l1.res  -side left
 
frame .m.l2
    pack .m.l2 -side top -anchor nw
    label .m.l2.brighttxt -text "Brightnes:"
    entry .m.l2.brightval -relief sunken -width 4 -textvariable brightval
    scale .m.l2.brightscale -sliderlength 0.3c -tickinterval 0 \
         -showvalue false -orient horizontal  -length 3c \
         -from -12 -to 12 -variable brightval
    pack .m.l2.brighttxt .m.l2.brightval .m.l2.brightscale -side left

frame .m.l3
    pack .m.l3 -side top -anchor nw
    label .m.l3.contrttxt -text "Contrast: "
    entry .m.l3.contrval -relief sunken -width 4 -textvariable contrval
    scale .m.l3.contrcale -sliderlength 0.3c -tickinterval 0 -showvalue false \
        -orient horizontal  -length 3c -from -12 -to 12 -variable contrval
    pack .m.l3.contrttxt .m.l3.contrval .m.l3.contrcale -side left 

frame .m.l4
    pack .m.l4 -side top -anchor nw
    label .m.l4.txt -text "Grain:      "
    entry .m.l4.grain_val -relief sunken -width 4 -textvariable grain
    scale .m.l4.grain -sliderlength 0.3c -tickinterval 0 -showvalue false  \
        -orient horizontal  -length 3c -from 3 -to 8 -variable grain
    pack  .m.l4.txt .m.l4.grain_val .m.l4.grain -side left

frame .m.l5
    pack .m.l5 -side top -anchor nw
    label .m.l5.txt -text "Speed :    "
    entry .m.l5.speed_val -relief sunken -width 4 -textvariable speed
    scale .m.l5.speed -sliderlength 0.3c -tickinterval 0 -showvalue false  \
        -orient horizontal  -length 3c -from 1 -to 5 -variable speed
    pack  .m.l5.txt .m.l5.speed_val .m.l5.speed -side left

frame .m.l6
    pack .m.l6 -side top -anchor nw
    label .m.l6.txt -text "Backtrace :   "
    radiobutton .m.l6.yes -text Yes -variable backtrace -relief flat -value " " 
    radiobutton .m.l6.no -text No -variable backtrace -relief flat  -value "-b "
    pack  .m.l6.txt .m.l6.yes .m.l6.no -side left


frame .m.l7
    pack .m.l7 -side top -anchor nw
    label .m.l7.txt -text "Top :        "
    entry .m.l7.t_x -relief sunken -width 6 -textvariable t_x
    entry .m.l7.t_y -relief sunken -width 6 -textvariable t_y
    pack  .m.l7.txt .m.l7.t_x .m.l7.t_y -side left

frame .m.l8
    pack .m.l8 -side top -anchor nw
    label .m.l8.txt -text "Bottom :  "
    entry .m.l8.b_x -relief sunken -width 6 -textvariable b_x
    entry .m.l8.b_y -relief sunken -width 6 -textvariable b_y
    pack  .m.l8.txt .m.l8.b_x .m.l8.b_y -side left

proc setmode { m } {
    global mode 

    set mode $m
    if { $m == "color" } {
        .m.l1.mode configure -text Color
    } 
    if { $m == "gray" } {
        .m.l1.mode configure -text Gray
    }
    if { $m == "halftone" } {
        .m.l1.mode configure -text Halftone
    }
    if { $m == "lineart" } {
        .m.l1.mode configure -text Lineart
    }
}

proc setres { r } {
    global res

    set res $r
    .m.l1.res configure -text $r
}

proc do_copy { } {
    global t_x t_y b_x b_y res mode grain backtrace speed 
    global contrval brightval pcmd debug

    if { $mode != "halftone" } {
        setmode lineart
    }

    set pcmd [format "-p %d:%d:0:%d" \
        [expr $brightval +12 ] [expr $contrval + 12] [expr 5 - $speed ] ]
    set l_x [format "%0.2f" [expr $b_x - $t_x]]
    set l_y [format "%0.2f" [expr $b_y - $t_y]]
    if { $mode == "lineart" } {
        exec sh -c "mscan -d $debug -a $backtrace $pcmd -r $res -x $l_x -y $l_y \
            -l $t_x -u $t_y | lpr -Prawlp" &
    }
    if { $mode == "halftone" } {
        set pcmd [format "-p %d:%d:%d:%d" \
            [expr $brightval +12 ] [expr $contrval + 12] \
            [expr 8 - $grain] [expr 5 - $speed ] ]
        exec sh -c "mscan -d $debug -m  $backtrace $pcmd -r $res -x $l_x -y $l_y \
            -l $t_x -u $t_y | lpr -Prawlp" &
    }
}

proc do_scan { } {
    global t_x t_y b_x b_y res mode grain backtrace speed 
    global contrval brightval pcmd debug

    set pcmd [format "-p %d:%d:0:%d" \
        [expr $brightval +12 ] [expr $contrval + 12] [expr 5 - $speed ] ]
    set l_x [format "%0.2f" [expr $b_x - $t_x]]
    set l_y [format "%0.2f" [expr $b_y - $t_y]]

    if { $mode == "lineart" } {
        exec sh -c "mscan $debug -a $backtrace $pcmd -r $res -x $l_x -y $l_y \
            -l $t_x -u $t_y | xv -" &
    }

    if { $mode == "halftone" } {
        set pcmd [format "-p %d:%d:%d:%d" \
            [expr $brightval +12 ] [expr $contrval + 12] \
            [expr 8 - $grain] [expr 5 - $speed ] ]
        exec sh -c "mscan $debug -m  $backtrace $pcmd -r $res -x $l_x -y $l_y \
            -l $t_x -u $t_y | xv -" &
    }
    if { $mode == "gray" } {
        exec sh -c "mscan $debug $backtrace $pcmd  -r $res -x $l_x -y $l_y \
            -l $t_x -u $t_y | xv -" &
    }
    if { $mode == "color" } {
        exec sh -c "mscan $debug -c $backtrace $pcmd  -r $res -x $l_x -y $l_y \
            -l $t_x -u $t_y | xv -" &
    }
}

proc do_setrect { } {
    global  bp_x bp_y tp_x tp_y  im rc

    .p.c delete $rc
    set rc [.p.c create rectangle $tp_x $tp_y $bp_x $bp_y  -width 1 \
        -outline blue  ]
}

proc do_settop { } {
    global t_x t_y tp_x tp_y mark

    set t_x [format "%0.2f"   [expr $tp_x * 300.0  / 18.0 / 300.0 ] ]
    set t_y [format "%0.2f"  [expr $tp_y * 300.0 / 18.0 / 300.0] ]
    do_setrect
    set mark 1
}

proc do_setbottom { } {
    global b_x b_y bp_x bp_y tp_x tp_y mark

    if { $tp_x < 0 } {
	set tp_x 0
    }
    if { $tp_y < 0 } {
	set tp_y 0
    }
    if { $bp_x < 0 } {
	set bp_x 0
    }
    if { $bp_y < 0 } {
	set bp_y 0
    }
    if { $tp_x > $bp_x } {
        set t $bp_x
        set bp_x $tp_x
        set tp_x $t
        do_settop
    }

    if { $tp_y > $bp_y } {
        set t $bp_y
        set bp_y $tp_y
        set tp_y $t
        do_settop
    }

    set b_x [format "%0.2f" [expr $bp_x * 300.0 / 18.0 / 300.0]]
    set b_y [format "%0.2f" [expr $bp_y * 300.0 / 18.0 / 300.0]]
    do_setrect
    set mark 0
}

proc do_locate { } {
    global mark

    if { $mark == 1 } do_setrect
}

setres 300
do_settop
do_setbottom
setmode lineart
set backtrace "-b "
