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

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

// method: 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::hist_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*)"hist_write_cc",
		     (char_1*)"error opening the model file");
    return ISIP_FALSE;
  }
  
  for (int_4 i=0; i < IMAGA_HIST_MODEL_LINES; i++) {
    
    if (counter == 0) {
      
      // write the version into the file
      //
      fprintf(fp, "%s\n\n", (char*)IMAGA_MODEL_VERSION);
    }
    
    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_HISTOGRAM_NAME);
    }
    
    else if (counter == 2) {
      
      // write the parameters of the histogram
      //
      fprintf(fp, (char*)IMAGA_HIST_COMMENT_FORMAT, IMAGA_HIST_COMMENT,
	      (char*)IMAGA_HIST_PARAM_COMMENT, ISIP_POUND);
      fprintf(fp, "min_value = %ld\nmax_val = %ld\nnum_bins = %ld\n\n",
	      (int_4)IMAGA_HIST_MIN, (int_4)IMAGA_HIST_MAX, IMAGA_HIST_NBINS);
    }
    
    else if (counter == 3) {
      
      // write the pointers of each of the colors into the file
      //
      fprintf(fp, (char*)IMAGA_HIST_COMMENT_FORMAT, IMAGA_HIST_COMMENT,
	      (char*)IMAGA_HIST_COLOR_COMMENT, ISIP_POUND);
      fprintf(fp, "red_ptr = %ld\ngreen_ptr = %ld\nblue_ptr = %ld\n\n",
	      IMAGA_HIST_RPTR, IMAGA_HIST_GPTR, IMAGA_HIST_BPTR);
    }
    
    else if (counter == 4) {

      // write the number of pixels and the images processed
      //
      fprintf(fp, (char*)IMAGA_HIST_COMMENT_FORMAT, IMAGA_HIST_COMMENT,
	      (char*)IMAGA_HIST_DATA_COMMENT, ISIP_POUND);
      fprintf(fp, "num_pixels = %ld\nnum_blocks_processed = %ld\n\n",
	      hist_t_npxls_d, hist_t_nblks_d);
    }
    
    else if (counter == 5) {
      
      // write the color weights
      //
      fprintf(fp, (char*)IMAGA_HIST_COMMENT_FORMAT, IMAGA_HIST_COMMENT,
	      (char*)IMAGA_HIST_CWTS_COMMENT, ISIP_POUND);
      fprintf(fp, "weight_red = %0.2f\nweight_green = %0.2f\nweight_blue = %0.2f\n\n",
	      IMAGA_HIST_WRED, IMAGA_HIST_WGRN, IMAGA_HIST_WBLU);
    }
    
    else if (counter == 6) {
      
      // write the scaling values
      //
      fprintf(fp, (char*)IMAGA_HIST_COMMENT_FORMAT, IMAGA_HIST_COMMENT,
	      (char*)IMAGA_HIST_SCAL_COMMENT, ISIP_POUND);
      fprintf(fp, "scale_a = %ld\nscale_b = %ld\n\n", IMAGA_HIST_ASCL,
	      IMAGA_HIST_BSCL);
    }
    
    else if (counter == 7) {

      // write the frequency distributions
      //
      fprintf(fp, (char*)IMAGA_HIST_COMMENT_FORMAT, IMAGA_HIST_COMMENT,
	      (char*)IMAGA_HIST_VALUES_COMMENT, ISIP_POUND);
      
      for (int_4 j=0; j<IMAGA_HIST_NBINS; j++) {
	fprintf(fp, "%ld  %3ld  %3.3f  %3ld\n", IMAGA_HIST_RPTR, j,
		hist_t_rcval_d[j], (int_4)hist_t_rpdf_d[j]);
      }
      
      for (int_4 j=0; j<IMAGA_HIST_NBINS; j++) {
	fprintf(fp, "%ld  %3ld  %3.3f  %3ld\n", IMAGA_HIST_GPTR, j,
		hist_t_gcval_d[j], (int_4)hist_t_gpdf_d[j]);
      }
      
      for (int_4 j=0; j<IMAGA_HIST_NBINS; j++) {
	fprintf(fp, "%ld  %3ld  %3.3f  %3ld\n", IMAGA_HIST_BPTR, j,
		hist_t_bcval_d[j], (int_4)hist_t_bpdf_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;
}



