// file: $USFS/util/segment_image/v1.0/segment_image.cc
//

// isip include files
//
#include "segment_image.h"
#include "segment_image_constants.h"

// main program
//
int main(int_4 argc, char_1** argv) {
  
  // declare command line arguments
  //
  char_1* param_filelist = (char_1*)NULL;
  char_1* output_ext = (char_1*)NULL;
  char_1* output_datafile = (char_1*)NULL;
  char_1* seg_filelist = (char_1*)NULL;
  char_1* image_file = (char_1*)NULL;
  char_1* seg_file = (char_1*)NULL;
  char_1* output_imagefile = (char_1*)NULL;
  
  // declare a debug parameter
  //
  int_4 debug_level = ISIP_DEBUG_NONE;
  
  // declare the position of the first file in the command line list
  //
  int_4 pos = 0;
  
  // parse the command line
  // 
  if (get_parameters_cc(param_filelist, output_ext, output_datafile,
			debug_level, seg_filelist,
			pos, argc, argv) == ISIP_FALSE) {
    fprintf(stdout, "%s: error in command line arguments\n", argv[0]);
    return ISIP_ERROR;
  }
  
  if (param_filelist == (char_1*)NULL) {
    fprintf(stdout, "%s: no parameter file\n", argv[0]);
    return ISIP_ERROR;
  }
  else {    
    fprintf(stdout, SI_PRINT_FORMAT_A, param_filelist);      
  }     
  
  // declare an Image_segmentation object
  // 
  Image_segmentation is;

  // set the memory manager for the Link_list class
  //
  Memory_manager* manager = new Memory_manager(1000, 1000);
  Link_list::set_manager_cc(manager);
  
  // get the model file names from the file list
  //
  if (is.get_nmods_cc(param_filelist) == ISIP_FALSE) {
    fprintf(stdout, "error in reading the parameter files\n");
    return ISIP_ERROR;
  }

  // initialization
  //
  if (is.init_cc() == ISIP_FALSE) {
    fprintf(stdout, "error in initilizing the Image_segment object\n");
    return ISIP_ERROR;
  }

  // set the debug parameter
  //
  if (is.set_debug_cc(debug_level) == ISIP_FALSE) {
    fprintf(stdout, "error in setting the debug parameter\n");
    return ISIP_ERROR;
  }
  
  // load the model files
  //
  if (is.load_models_cc() == ISIP_FALSE) {
    fprintf(stdout, "error in loading model files");
    return ISIP_ERROR;
  }

  // define counter to keep up with the number of files processed successfully
  //
  int_4 counter_before = (int_4)0;
  int_4 counter_after = (int_4)0;
  
  // declare the memory for the image file and the segmentation file
  //
  image_file = new char_1[ISIP_MAX_STRING_LENGTH];
    
  // fetch the image file name and loop through all the images
  //
  while (get_next_filename_cc(image_file, argc, argv, pos)
	 == ISIP_TRUE) {
    
    // fetch the segmentation file name from the file list
    //
    if (seg_filelist != (char_1*)NULL) {
      seg_file = new char_1[ISIP_MAX_STRING_LENGTH];
      if (get_next_segfname_cc(seg_file, image_file, seg_filelist)
	  == ISIP_FALSE) {
	fprintf(stdout, "error in reading the segmentation file list\n");
	return ISIP_ERROR;
      }
    }

    // get the output file name
    //
    if (output_ext != (char_1*)NULL) {
      output_imagefile = new char_1[ISIP_MAX_STRING_LENGTH];
      if(get_next_outfname_cc(image_file, output_ext, output_imagefile)
	 == ISIP_FALSE) {
	fprintf(stdout, "error in getting the output file name\n");
	return ISIP_ERROR;
      }
    }
    
    // process the file
    //
    counter_before++;

    if (is.process_cc(image_file, seg_file, output_imagefile, output_datafile)
	== ISIP_FALSE) {
      fprintf(stdout, "error in processing the image file\n");
      return ISIP_ERROR;
    }

    // statistics
    //
    counter_after++;
  }

  // write to the model files if the mode is train
  //
  if (is.write_models_cc() == ISIP_FALSE) {
    fprintf(stdout, "error in writing the model file\n");
    return ISIP_ERROR;
  }
  
  
  // print how many successful files were processed
  //
  fprintf(stdout, "\n<%s: %ld of %ld files processed successfully>\n",
	  argv[0], counter_after, counter_before);
  
  // delete allocated memory
  //
  if (param_filelist != (char_1*)NULL) {
    delete [] param_filelist;
  }
  if (output_ext != (char_1*)NULL) {
    delete [] output_ext;
  }
  if (seg_filelist != (char_1*)NULL) {
    delete [] seg_filelist;
  }
  if (image_file != (char_1*)NULL) {
    delete [] image_file;
  }
  if (seg_file != (char_1*)NULL) {
    delete [] seg_file;
  }
  if (output_imagefile != (char_1*)NULL) {
    delete [] output_imagefile;
  }
  if (output_datafile != (char_1*)NULL) {
    delete [] output_datafile;
  }
  
  // exit gracefully
  //
  return ISIP_NO_ERROR;
}
