
/* Copyright (c) 1991, 1992 Cornell University.  All Rights Reserved.
 *
 * Use, reproduction and preparation of derivative works of this software is
 * permitted.  Any copy of this software or of any derivative work must
 * include both the above copyright notices of Cornell University and this
 * paragraph.  This software is made available AS IS, and Cornell University
 * disclaims all warranties, express or implied.
 */

static char rcsid[] = "bin-gregk/threshold.c";

/*   Greg Klanderman    Spring 1993
     
     this program is a UNIX interface to the threshold functions
     in support-gregk/threshold.c
*/

#include <stdio.h>
#include <math.h>

#include "clarg.h"
#include "threshold.h"
#include "strings.h"
#include "string-support.h"


static char *USAGE =
  "\n%s:  Threshold the input PNM+ file.\n"
  "\n"
  "Arguments are:\n"
  "   -input file:          Specify the input file name\n"
  "   -output file:         Specify the output file name\n"
  "   -threshold t1 [t2]    Specify the thresholds\n"
  "   -mutate               Output the original, with pixels that don't pass\n"
  "                         the threshold criteria replaced by zero.\n"
  "\n"
  "Thresholds the image, using a two threshold hysteresis if both thresholds\n"
  "are specified.  Argument names may be abreviated.  If no input file is\n"
  "specified, input is from stdin, and if no output file is specified, output\n"
  "is to stdout.\n\n";


#define INPUT 	"i.nput"
#define OUTPUT 	"o.utput"
#define THRESH  "t.hreshold"
#define HACK    "m.utate"


static CLINIT clinit[] = {{INPUT,  1,  1, NULL,    0},
			  {OUTPUT, 1,  1, NULL,    0},
			  {THRESH, 1,  2, cl_real, 0},
			  {HACK,   0,  0, NULL,    0},
			  {NULL,   1,  1, NULL,    0}};


static char *depend[] = {"!(UNNAMED && "INPUT")", NULL};


int main(int argc, char **argv, char **envp)
{
  float t1, t2;
  AnyImage img;
  char *infile;
  String newdoc;
  

  if (cl_init(argc, argv, envp, clinit, depend, USAGE, CL_NORESTRICT))  /* parse args */
    return 0;

  infile = (cl_opt(INPUT)
	    ? cl_arg1(INPUT)
	    : cl_opt(CL_UNNAMED) ? cl_arg1(CL_UNNAMED) : NULL);

  t1 = cl_opt(THRESH) ? atof(cl_arg1(THRESH)) : 1.0;
  t2 = (cl_opt(THRESH)
	? cl_numarg(THRESH) == 2 ? atof(cl_arg2(THRESH)) : THRESH_NONE
	: THRESH_NONE);

  if ((img = (AnyImage) imLoad(IMAGE_ANY, infile)) == NULL) {
    fprintf(stderr, "THRESHOLD: Error reading input image\n");
    return -1;
  }

  newdoc = strNewComment(argc, argv, envp, imDocString(img), NULLSTRING);
  
  if (cl_opt(HACK)) {
    if (threshImageMutate(img, t1, t2)) {
      fprintf(stderr, "THRESHOLD: Invalid input image type\n");
      return -1;
    }
  }
  else {
    AnyImage out;
    
    if ((out = (AnyImage) threshImage(img, t1, t2)) == NULL) {
      fprintf(stderr, "THRESHOLD: Invalid input image type\n");
      return -1;
    }

    imFree(img);
    img = out;
  }

  imDocString(img) = newdoc;

  if (imSave(img, (cl_opt(OUTPUT) ? cl_arg1(OUTPUT) : NULL))) {
    fprintf(stderr, "THRESHOLD: Error saving thresholded image\n");
    return -1;
  }
  
  imFree(img);

  
  return 0;
}



