/*******************************************************************************
* Program: threshold
* Purpose: This program will threshold an image. This process will set all pixel
* values that are less than a threshold value to zero and every other pixel to
* the value 255. The program works with RAW PGM format images.
* Name: Michael Heath, University of South Florida
* To Compile: gcc -o threshold threshold.c imageio.c 
* Date: 1/9/2000
*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include "imageio.h"

int main(int argc, char *argv[])
{
   char *input_filename=NULL, *output_filename=NULL;
   unsigned char **image=NULL, **imageout=NULL;
   int threshold;
   int rows, cols;
   char comment_string[80];

   int threshold_image(unsigned char **image, int rows, int cols, int threshold, 
      unsigned char ***imageout);

   if(argc != 4){
      fprintf(stderr, "\n\n");
      fprintf(stderr, "********************************************************************************\n");
      fprintf(stderr, "This program will threshold an image. Thresholding is a process of examining\n");
      fprintf(stderr, "the value of each pixel in an image and comparing it to a threshold value. If\n");
      fprintf(stderr, "the pixel value is less than the threshold, the pixel value is set to zero and\n");
      fprintf(stderr, "if it is above the threshold value it is set to 255.\n");
      fprintf(stderr, "********************************************************************************\n");
      fprintf(stderr, "\n<USAGE> %s inputimage.pgm threshold outputimage.pgm\n", argv[0]);
      fprintf(stderr, "   inputimage.pgm   - The name of a file in RAW PGM format to process.\n");
      fprintf(stderr, "   threshold        - An integer in the range 0->255.\n");
      fprintf(stderr, "   outputimage.pgm  - The name of a file to store the thresholded image.\n\n");
      exit(1);
   }
   input_filename = argv[1];
   threshold = atoi(argv[2]);
   output_filename = argv[3];

   /****************************************************************************
   * Check to see that the threshold value is in the proper range.
   ****************************************************************************/
   if((threshold < 0) || (threshold > 255)){
      fprintf(stderr, "\nError! The threshold value of %d is not in the range 0->255.\n\n",
         threshold);
      exit(1);
   }

   /****************************************************************************
   * Read in the image from the file.
   ****************************************************************************/
   if(read_pgm_image(input_filename, &image, &rows, &cols) == 0) exit(1);

   /****************************************************************************
   * Call the function to threshold the image.
   ****************************************************************************/
   if(threshold_image(image, rows, cols, threshold, &imageout) == 0) exit(1);

   /****************************************************************************
   * Free the input image because it is no longer needed. 
   ****************************************************************************/
   free_image(image, rows);

   /****************************************************************************
   * Write the thresholded image out to a file.
   ****************************************************************************/
   sprintf(comment_string, "Image %s thresholded at %d.", input_filename, threshold);
   if(write_pgm_image(output_filename, imageout, rows, cols, comment_string, 255) == 0)
      exit(1);

   /****************************************************************************
   * Free the thresholded image because it is no longer needed. 
   ****************************************************************************/
   free_image(imageout, rows);
}

/*******************************************************************************
* Function: threshold_image
* Purpose: To threshold an image with the supplied threshold value. All pixels
* less than the threshold value are set to zero and all the rest of the pixels
* are set to 255.
* Name: Michael Heath, University of South Florida
* Date: 1/9/2000
*******************************************************************************/
int threshold_image(unsigned char **image, int rows, int cols, int threshold, 
   unsigned char ***imageout)
{
   unsigned char **imagetmp=NULL;
   int r, c;

   /****************************************************************************
   * Allocate memory for the thresholded image.
   ****************************************************************************/
   if((imagetmp = allocate_image(rows, cols)) == NULL) return(0);
   *imageout = imagetmp;

   /****************************************************************************
   * Scan through the image and threshold each pixel.
   ****************************************************************************/
   for(r=0;r<rows;r++){
      for(c=0;c<cols;c++){
         if(image[r][c] < threshold) imagetmp[r][c] = 0;
         else imagetmp[r][c] = 255;
      }
   }

   return(1); 
}
