// file: $USFS/class/image_analysis/v6.1/imaga_edge_15.cc
//

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

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

// method: edge_write_cc
//
// arguments: none
//
// return: a logical_1 value indicating status
//
// this method writes the frequency distribution of each of the color
// and the parameters related to the histogram in the model file
//
logical_1 Image_analysis::edge_write_cc() {
  
  // define local variables
  //
  int_4 counter = 0;
  
  // open the file for writing the histogram model file
  //
  FILE* fp = open_cc(model_fname_d, ISIP_WRITE_ONLY);
  if (fp == (FILE*)NULL) {
    error_handler_cc((char_1*)"edge_model_write_cc",
		     (char_1*)"error opening the model file");
    return ISIP_FALSE;
  }
  
  for (int_4 i=0; i < IMAGA_EDGE_MODEL_LINES; i++) {

    // write the version of the model file
    //
    if (counter == 0) {
      
      // write the version into the file
      //
      fprintf(fp, "%s\n\n", (char*)IMAGA_MODEL_VERSION);
    }

    // write the type of the algorithm along with the comment
    //
    else if (counter == 1) {
      
      // write the type of the algorithm
      //
      fprintf(fp, (char*)IMAGA_HIST_COMMENT_FORMAT, IMAGA_HIST_COMMENT,
	      (char*)IMAGA_HIST_ALG_COMMENT, ISIP_POUND);
      fprintf(fp, "algorithm = %s\n\n", (char*)IMAGA_ALG_EDGEDET_NAME);
    }

    // write the parameters used for edge detection along with the comment
    //
    else if (counter == 2) {
      
      // write the parameters of the edgedetection
      //
      fprintf(fp, (char*)IMAGA_HIST_COMMENT_FORMAT, IMAGA_HIST_COMMENT,
	      (char*)IMAGA_EDGE_PARAM_COMMENT, ISIP_POUND);
      fprintf(fp, "num_bins = %ld\ngauss_xmean = %f\ngauss_ymean = %f\n",
	      IMAGA_EDGE_NBINS, IMAGA_EDGE_XMEAN, IMAGA_EDGE_YMEAN);
      fprintf(fp, "gauss_sigma = %f\ngauss_mask_size = %ld\nratio_factor = %f\n\n",
	      IMAGA_EDGE_SIGMA, IMAGA_EDGE_MSIZE, IMAGA_EDGE_FACTOR);
    }

    // write the number of pixels and the images processed
    //
    else if (counter == 3) {
      
      fprintf(fp, (char*)IMAGA_HIST_COMMENT_FORMAT, IMAGA_HIST_COMMENT,
	      (char*)IMAGA_EDGE_DATA_COMMENT, ISIP_POUND);
      fprintf(fp, "num_lines = %ld\nnum_blocks_processed = %ld\n\n",
	      edge_t_tlines_d, edge_t_nblks_d);
    }
    
    // write the scaling factors along with the comment
    //
    else if (counter == 4) {
      fprintf(fp, (char*)IMAGA_HIST_COMMENT_FORMAT, IMAGA_HIST_COMMENT,
	      (char*)IMAGA_HIST_SCAL_COMMENT, ISIP_POUND);
      fprintf(fp, "scale_a = %f\nscale_b = %f\n\n", IMAGA_EDGE_ASCL,
	      IMAGA_EDGE_BSCL);
    }

    // write the line weights
    //
    else if (counter == 5) {
      
      fprintf(fp, (char*)IMAGA_HIST_COMMENT_FORMAT, IMAGA_HIST_COMMENT,
	      (char*)IMAGA_EDGE_LWGTS_COMMENT, ISIP_POUND);
      fprintf(fp, "weight_long = %0.2f\nweight_short = %0.2f\n\n",
	      IMAGA_EDGE_LWGT, IMAGA_EDGE_SWGT);
    }

    // write the frequency distributions
    //
    else if (counter == 6) {

      fprintf(fp, (char*)IMAGA_HIST_COMMENT_FORMAT, IMAGA_HIST_COMMENT,
	      (char*)IMAGA_EDGE_VALUES_COMMENT, ISIP_POUND);
      
      for (int_4 j=0; j<IMAGA_EDGE_NBINS; j++) {
	fprintf(fp, "%ld  %3.3f  %3f\n", j, edge_t_lcval_d[j],
		edge_t_lpdf_d[j]);
      }
    }
    counter++;
  }

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