// file: $USFS/util/detect_edge/v6.2/detect_edge.cc
//
// this program detects edges and lines for the image files provided
// in a file list
//

// system include files
//
#include <string.h>

// isip include files
//
#include "detect_edge.h"
#include "detect_edge_constants.h"

// main program
//
int main(int_4 argc, char_1** argv) {

  // declare command line arguments
  //
  char_1* in_img_filelist = (char_1*)NULL;
  char_1* out_data_filelist = (char_1*)NULL;
  char_1* out_img_filelist = (char_1*)NULL;

  int_4 pos = 0;

  // parse the command line
  //
  if (get_parameters_cc(in_img_filelist, out_data_filelist, out_img_filelist,
			pos, argc, argv) == ISIP_FALSE) {
    fprintf(stdout, "%s: error parsing command line arguments\n", argv[0]);
    return ISIP_ERROR;
  }

  if (in_img_filelist == (char_1*)NULL) {
    fprintf(stdout, "%s: no input image file list\n", argv[0]);
    return ISIP_ERROR;
  }

  if (out_data_filelist == (char_1*)NULL) {
    fprintf(stdout, "%s: no output data file list\n", argv[0]);
    return ISIP_ERROR;
  }

  // open files
  //
  FILE* fp_in_imglist = (FILE*)NULL;
  FILE* fp_out_datalist = (FILE*)NULL;
  FILE* fp_out_imglist = (FILE*)NULL;
  
  if ((fp_in_imglist = fopen((char*)in_img_filelist, "r")) == (FILE*)NULL) {
    fprintf(stdout, "%s: error opening file %s\n", argv[0], in_img_filelist);
    return ISIP_ERROR;
  }

  if ((fp_out_datalist = fopen((char*)out_data_filelist, "r")) == (FILE*)NULL) {
    fprintf(stdout, "%s: error opening file %s\n", argv[0], out_data_filelist);
    return ISIP_ERROR;
  }

  if (out_img_filelist != (char_1*)NULL) {
    if ((fp_out_imglist = fopen((char*)out_img_filelist, "r")) == (FILE*)NULL) {
      fprintf(stdout, "%s: error opening %s\n", argv[0], out_img_filelist);
      return ISIP_ERROR;
    }
  }

  // declare an Image_analysis object
  //
  Image_analysis ia;

  // declare local variables
  //
  char_1* in_img_fname = (char_1*)NULL;
  char_1* out_data_fname = (char_1*)NULL;
  char_1* out_img_fname = (char_1*)NULL;

  in_img_fname = new char_1[ISIP_MAX_STRING_LENGTH];
  out_data_fname = new char_1[ISIP_MAX_STRING_LENGTH];

  memset(in_img_fname, 0, ISIP_MAX_STRING_LENGTH);
  memset(out_data_fname, 0, ISIP_MAX_STRING_LENGTH);

  if (fp_out_imglist != (FILE*)NULL) {
    out_img_fname = new char_1[ISIP_MAX_STRING_LENGTH];
  }
  
  // read the file lists and process each file
  //
  while (fscanf(fp_in_imglist, "%s", in_img_fname) != EOF) {

    // read the output data file list
    //
    if (fscanf(fp_out_datalist, "%s", out_data_fname) == EOF) {
      fprintf(stdout, "%s: file lists not matching in length\n", argv[0]);
      return ISIP_ERROR;
    }

    // read the output image file list if necessary
    //
    if (fp_out_imglist != (FILE*)NULL) {
      memset(out_img_fname, 0, ISIP_MAX_STRING_LENGTH);
      if (fscanf(fp_out_imglist, "%s", out_img_fname) == EOF) {
	fprintf(stdout, "%s: file lists not matching in length\n", argv[0]);
	return ISIP_ERROR;
      }
    }

    // detect edges and lines
    //
    if (ia.detect_lines_cc(in_img_fname, out_data_fname, out_img_fname)
	== ISIP_FALSE) {
      fprintf(stdout, "%s: error computing edges\n", argv[0]);
      return ISIP_ERROR;
    }

    // prepare for the next loop
    //
    memset(in_img_fname, 0, ISIP_MAX_STRING_LENGTH);
    memset(out_data_fname, 0, ISIP_MAX_STRING_LENGTH);
  }

  // close files
  //
  fclose(fp_in_imglist);
  fclose(fp_out_datalist);

  if (fp_out_imglist != (FILE*)NULL) {
    fclose(fp_out_imglist);
  }

  // release memory
  //
  if (in_img_filelist != (char_1*)NULL) {
    delete [] in_img_filelist;
  }
  if (out_data_filelist != (char_1*)NULL) {
    delete [] out_data_filelist;
  }
  if (out_img_filelist != (char_1*)NULL) {
    delete [] out_img_filelist;
  }

  delete [] in_img_fname;
  delete [] out_data_fname;

  if (out_img_fname != (char_1*)NULL) {
    delete [] out_img_fname;
  }
  
  // exit gracefully
  //
  return ISIP_NO_ERROR;
}
