// file: $USFS/class/image_analysis/v3.0/image_edge_00.cc
//

// isip include files
//
#include "image_analysis.h"

#include "image_analysis_constants.h"
#include <integral_constants.h>

// method: edge_load_cc
//
// arguments: none
//
// return: a logical_1 value indicating the status
//
// this method loads the model file
//
logical_1 Image_analysis::edge_load_cc() {
  
  // declare a scratch buffer
  //
  char_1 buf[ISIP_MAX_STRING_LENGTH];
  
  // open the model file for the histogram
  //
  FILE* fp = open_cc(model_fname_d, ISIP_READ_ONLY);
  if (fp == (FILE*)NULL) {
    error_handler_cc((char_1*)"edge_load_cc",
		     (char_1*)"error opening the edge model file");
    return ISIP_FALSE;
  }
  
  // read the version used
  //
  if (fscanf(fp, "%s", (char*)buf) == (int)0) {
    error_handler_cc((char_1*)"edge_load_cc",
		     (char_1*)"incorrect version");
    return ISIP_FALSE;
  }
  
  // check the version of the model file
  //
  if (strcmp((char*)buf, (char*)IMAGA_MODEL_VERSION) != (int)0) {
    error_handler_cc((char_1*)"edge_load_cc",
		     (char_1*)"incorrect model version");
    return ISIP_FALSE;
  }
  
  // save the model version
  //
  if (model_version_d != (char_1*)NULL) {
    delete [] model_version_d;
  }
  
  model_version_d = new char_1[strlen((char*)buf) + (int_4)1];
  strcpy((char*)model_version_d, (char*)buf);
  
  // loop over all the lines in the file and collect the model
  //
  char_1 str_key[ISIP_MAX_STRING_LENGTH];
  char_1 str_val[ISIP_MAX_STRING_LENGTH];
  
  int_4 i;
  int_4 counter = 0;
  
  float_4 val;
  float_4 cen_val;
  
  while(fgets((char*)buf, ISIP_MAX_STRING_LENGTH, fp) != (char*)NULL) {
    
    // checking for a comment line and/or a blank line
    //
    char_1 nonwhitespace[2];
    
    if ((buf[0] != ISIP_POUND) &&
	(sscanf((char*)buf, "%1s", nonwhitespace) > (int)0)) {
      
      // parse the line
      //
      if (counter <= IMAGA_EDGE_NPARAMS) {
	sscanf((char*)buf, (char*)IMAGA_FORMAT_0, str_key, str_val);
      }

      // read the algorithm
      //
      if (counter == 0) {
	if ((strcmp((char*)str_key, (char*)IMAGA_ALG) != (int)0) ||
	    (strcmp((char*)str_val, (char*)IMAGA_ALG_EDGEDET_NAME) != (int)0)){
	  error_handler_cc((char_1*)"edge_load_cc",
			   (char_1*)"incorrect type of model file");
	  return ISIP_FALSE;
	}
      }

      // read the number of bins
      //
      else if (counter == 1) {
	edge_r_nbins_d = atoi((char*)str_val);

	// delete memory if necessary
	//
	if (edge_r_lpdf_d != (float_8*)NULL) {
	  delete [] edge_r_lpdf_d;
	  edge_r_lpdf_d = (float_8*)NULL;
	}
	if (edge_r_lcval_d != (float_8*)NULL) {
	  delete [] edge_r_lcval_d;
	  edge_r_lcval_d = (float_8*)NULL;
	}

	// initialize space for pdfs
	//
	edge_r_lpdf_d = new float_8[edge_r_nbins_d];
	edge_r_lcval_d = new float_8[edge_r_nbins_d];
      }
      
      // read the xmean for the gaussian mask
      //
      else if (counter == 2) {
	edge_r_xmean_d = atof((char*)str_val);
      }

      // read the ymean for the gaussian mask
      //
      else if (counter == 3) {
	edge_r_ymean_d = atof((char*)str_val);
      }

      // read the sigma for the gaussian mask
      //
      else if (counter == 4) {
	edge_r_sigma_d = atof((char*)str_val);
      }

      // read the gaussian mask size
      //
      else if (counter == 5) {
	edge_r_msize_d = atoi((char*)str_val);
      }

      // read the ratio factor
      //
      else if (counter == 6) {
	edge_r_rfact_d = atof((char*)str_val);
      }

      // read the total number of lines
      //
      else if (counter == 7) {
	edge_t_tlines_d = atoi((char*)str_val);
      }

      // read the number of blocks processed
      //
      else if (counter == 8) {
	edge_t_nblks_d = atoi((char*)str_val);
      }

      // read the scale factor
      //
      else if (counter == 9) {
	edge_ascale_d = atof((char*)str_val);
      }
      
      // read the scale factor
      //
      else if (counter == 10) {
	edge_bscale_d = atof((char*)str_val);
      }

      // read the weights
      //
      else if (counter == 11) {
	edge_lwgt_d = atof((char*)str_val);
      }
      
      else if (counter == 12) {
	edge_swgt_d = atof((char*)str_val);
      }
      
      // load the pdfs
      //
      else if (counter >= IMAGA_EDGE_NPARAMS) {
	sscanf((char*)buf, "%ld%f%f", &i, &cen_val, &val);
	edge_r_lcval_d[i] = cen_val;
	edge_r_lpdf_d[i] = val / (float_8)edge_t_tlines_d;
      }
      counter++;
    }
  }

  // close the file
  //
  if (close_cc(fp) == ISIP_FALSE) {
    error_handler_cc((char_1*)"edge_load_cc",
		     (char_1*)"error closing the file");
    return ISIP_FALSE;
  }

  // exit gracefully
  //
  return ISIP_TRUE;
}
