#!/usr/bin/perl

#
#  Copyright (C) 1999-2001 Ricardo Ueda Karpischek
#
#  This is free software; you can redistribute it and/or modify
#  it under the terms of the version 2 of the GNU General Public
#  License as published by the Free Software Foundation.
#
#  This software 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 software; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
#  USA.
#

#
# selthresh: Select best thesholds.
#

#
# Simple script to choose the "best" threshold when converting
# PGM to PBM.
#
# Usage:
#
#     selthres [-y res] [-w path] [-l ll rl] file1.pgm [file2.pgm ...]
#
# where
#
#     res is the resolution (defaults to 600 dpi)
#     path is the path of the work directory (defaults to "./")
#     [ll,lr] is the interval to analyse (defaults to [0.4,0.6])
#
# BUGS:
#
# 1. won't distinguish two files with identical names but on
# different directories. Example:
#
#     selthresh /home/foo/foo.pgm /tmp/foo.pgm
#
# WARNINGS:
#
# 1. selthresh destroys the file selthresh.tem.pbm on the work
# directory (if one such file exists), without asking confirmation!
#
# 2. Make sure that clara, pnmenlarge and pgmtopbm are on the
# current path.
#
# 3. Write permission on the work directory is required. Two files
# will be created there (selthresh.out and selthresh.temp.pbm). The
# file selthresh.out contains all results, please do not remove
# it.
#
# 4. The paths of file1.pgm, file2.pgm, etc, must be relative to
# the work directory.
#
# 5. The results are affected by MD (see the -P flag), more than
# should be. So the threshold quality measured by selthresh is
# somewhat "unstable".
#
# 6. Floating comparisons '<=' and '>=' include a 0.001 delta to
# avoid failures due to the inexact binary representation of
# some thresholds.
#

use strict;

# threshold
my($t);

# PGM input file and PBM output file
my($b,$g,$gn,$f);

# interval to search
my($ll,$rl);

# other
my($i,$some,$wd,$badl,$badr);

# density
my($DENSITY,$N);

# results
my(%BS,%BT);

# strategies
my($clean,$small);

#
# display a message and exit cleanly.
#
sub fatal
{
    if ($_[0] ne '') {
        print(STDERR "$_[0]\n");
    }
    if ($f ne '') {
        system("rm -f $f");
    }
    close(O);
    exit(1);
}

#
# Handles Control-C. Note that most Control-C's will be catched
# by the subprocesses, not by selthresh. The mysystem sub will
# take care of Control-C's catchedre ofsearch
my($ll,$rl);

# other
my($i,$some,$wd,$badl,$badr);

# density
my($DENSITY,$N);

# results
my(%BS,%BT);

# strategies
my($clean,$small);

#
# display a message and exit cleanly.
#
sub fatal
{
    if ($_[0] ne '') {
        print(STDERR "$_[0]\n");
    }
    if ($f ne '') {
        system("rm -f $f");
    }
    close(O);
    exit(1);
}

#
# Handles Control-C. Note that most Control-C's will be catched
# by the subprocesses, not by selthresh. The mysystem sub will
# take care of Control-C's catchedre ofsearch
my($ll,$rl);

# other
my($i,$some,$wd,$badl,$badr);

# density
my($DENSITY,$N);

# results
my(%BS,%BT);

# strategies
my($clean,$small);

#
# display a message and exit cleanly.
#
sub fatal
{
    if ($_[0] ne '') {
        print(STDERR "$_[0]\n");
    }
    if ($f ne '') {
        system("rm -f $f");
    }
    close(O);
    exit(1);
}

#
# Handles Control-C. Note that most Control-C's will be catched
# by the subprocesses, not by selthresh. The mysystem sub will
# take care of Control-C's catchedre ofsearch
my($ll,$rl);

# other
my($i,$some,$wd,$badl,$badr);

# density
my($DENSITY,$N);

# results
my(%BS,%BT);

# strategies
my($clean,$small);

#
# display a message and exit cleanly.
#
sub fatal
{
    if ($_[0] ne '') {
        print(STDERR "$_[0]\n");
    }
    if ($f ne '') {
        system("rm -f $f");
    }
    close(O);
    exit(1);
}

#
# Handles Control-C. Note that most Control-C's will be catched
# by the subprocesses, not by selthresh. The mysystem sub will
# take care of Control-C's catchedre ofsearch
my($ll,$rl);

# other
my($i,$some,$wd,$badl,$badr);

# density
my($DENSITY,$N);

# results
my(%BS,%BT);

# strategies
my($clean,$small);

#
# display a message and exit cleanly.
#
sub fatal
{
    if ($_[0] ne '') {
        print(STDERR "$_[0]\n");
    }
    if ($f ne '') {
        system("rm -f $f");
    }
    close(O);
    exit(1);
}

#
# Handles Control-C. Note that most Control-C's will be catched
# by the subprocesses, not by selthresh. The mysystem sub will
# take care of Control-C's catchedre ofsearch
my($ll,$rl);

# other
my($i,$some,$wd,$badl,$badr);

# density
my($DENSITY,$N);

# results
my(%BS,%BT);

# strategies
my($clean,$small);

#
# display a message and exit cleanly.
#
sub fatal
{
    if ($_[0] ne '') {
        print(STDERR "$_[0]\n");
    }
    if ($f ne '') {
        system("rm -f $f");
    }
    close(O);
    exit(1);
}

#
# Handles Control-C. Note that most Control-C's will be catched
# by the subprocesses, not by selthresh. The mysystem sub will
# take care of Control-C's catchedre ofsearch
my($ll,$rl);

# other
my($i,$some,$wd,$badl,$badr);

# density
my($DENSITY,$N);

# results
my(%BS,%BT);

# strategies
my($clean,$small);

#
# display a message and exit cleanly.
#
sub fatal
{
    if ($_[0] ne '') {
        print(STDERR "$_[0]\n");
    }
    if ($f ne '') {
        system("rm -f $f");
    }
    close(O);
    exit(1);
}

#
# Handles Control-C. Note that most Control-C's will be catched
# by the subprocesses, not by selthresh. The mysystem sub will
# take care of Control-C's catchedre ofsearch
my($ll,$rl);

# other
my($i,$some,$wd,$badl,$badr);

# density
my($DENSITY,$N);

# results
my(%BS,%BT);

# strategies
my($clean,$small);

#
# display a message and exit cleanly.
#
sub fatal
{
    if ($_[0] ne '') {
        print(STDERR "$_[0]\n");
    }
    if ($f ne '') {
        system("rm -f $f");
    }
    close(O);
    exit(1);
}

#
# Handles Control-C. Note that most Control-C's will be catched
# by the subprocesses, not by selthresh. The mysystem sub will
# take care of Control-C's catchedre ofsearch
my($ll,$rl);

# other
my($i,$some,$wd,$badl,$badr);

# density
my($DENSITY,$N);

# results
my(%BS,%BT);

# strategies
my($clean,$small);

#
# display a message and exit cleanly.
#
sub fatal
{
    if ($_[0] ne '') {
        print(STDERR "$_[0]\n");
    }
    if ($f ne '') {
        system("rm -f $f");
    }
    close(O);
    exit(1);
}

#
# Handles Control-C. Note that most Control-C's will be catched
# by the subprocesses, not by selthresh. The mysystem sub will
# take care of Control-C's catchedre ofsearch
my($ll,$rl);

# other
my($i,$some,$wd,$badl,$badr);

# density
my($DENSITY,$N);

# results
my(%BS,%BT);

# strategies
my($clean,$small);

#
# display a message and exit cleanly.
#
sub fatal
{
    if ($_[0] ne '') {
        print(STDERR "$_[0]\n");
    }
    if ($f ne '') {
        system("rm -f $f");
    }
    close(O);
    exit(1);
}

#
# Handles Control-C. Note that most Control-C's will be catched
# by the subprocesses, not by selthresh. The mysystem sub will
# take care of Control-C's catchedre ofsearch
my($ll,$rl);

# other
my($i,$some,$wd,$badl,$badr);

# density
my($DENSITY,$N);

# results
my(%BS,%BT);

# strategies
my($clean,$small);

#
# display a message and exit cleanly.
#
sub fatal
{
    if ($_[0] ne '') {
        print(STDERR "$_[0]\n");
    }
    if ($f ne '') {
        system("rm -f $f");
    }
    close(O);
    exit(1);
}

#
# Handles Control-C. Note that most Control-C's will be catched
# by the subprocesses, not by selthresh. The mysystem sub will
# take care of Control-C's catchedre ofsearch
my($ll,$rl);

# other
my($i,$some,$wd,$badl,$badr);

# density
my($DENSITY,$N);

# results
my(%BS,%BT);

# strategies
my($clean,$small);

#
# display a message and exit cleanly.
#
sub fatal
{
    if ($_[0] ne '') {
        print(STDERR "$_[0]\n");
    }
    if ($f ne '') {
        system("rm -f $f");
    }
    close(O);
    exit(1);
}

#
# Handles Control-C. Note that most Control-C's will be catched
# by the subprocesses, not by selthresh. The mysystem sub will
# take care of Control-C's catchedre ofsearch
my($ll,$rl);

# other
my($i,$some,$wd,$badl,$badr);

# density
my($DENSITY,$N);

# results
my(%BS,%BT);

# strategies
my($clean,$small);

#
# display a message and exit cleanly.
#
sub fatal
{
    if ($_[0] ne '') {
        print(STDERR "$_[0]\n");
    }
    if ($f ne '') {
        system("rm -f $f");
    }
    close(O);
    exit(1);
}

#
# Handles Control-C. Note that most Control-C's will be catched
# by the subprocesses, not by selthresh. The mysystem sub will
# take care of Control-C's catchedre ofsearch
my($ll,$rl);

# other
my($i,$some,$wd,$badl,$badr);

# density
my($DENSITY,$N);

# results
my(%BS,%BT);

# strategies
my($clean,$small);

#
# display a message and exit cleanly.
#
sub fatal
{
    if ($_[0] ne '') {
        print(STDERR "$_[0]\n");
    }
    if ($f ne '') {
        system("rm -f $f");
    }
    close(O);
    exit(1);
}

#
# Handles Control-C. Note that most Control-C's will be catched
# by the subprocesses, not by selthresh. The mysystem sub will
# take care of Control-C's catchedre ofsearch
my($ll,$rl);

# other
my($i,$some,$wd,$badl,$badr);

# density
my($DENSITY,$N);

# results
my(%BS,%BT);

# strategies
my($clean,$small);

#
# display a message and exit cleanly.
#
sub fatal
{
    if ($_[0] ne '') {
        print(STDERR "$_[0]\n");
    }
    if ($f ne '') {
        system("rm -f $f");
    }
    close(O);
    exit(1);
}

#
# Handles Control-C. Note that most Control-C's will be catched
# by the subprocesses, not by selthresh. The mysystem sub will
# take care of Control-C's catchedre ofsearch
my($ll,$rl);

# other
my($i,$some,$wd,$badl,$badr);

# density
my($DENSITY,$N);

# results
my(%BS,%BT);

# strategies
my($clean,$small);

#
# display a message and exit cleanly.
#
sub fatal
{
    if ($_[0] ne '') {
        print(STDERR "$_[0]\n");
    }
    if ($f ne '') {
        system("rm -f $f");
    }
    close(O);
    exit(1);
}

#
# Handles Control-C. Note that most Control-C's will be catched
# by the subprocesses, not by selthresh. The mysystem sub will
# take care of Control-C's catchedre ofsearch
my($ll,$rl);

# other
my($i,$some,$wd,$badl,$