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

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

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

// method: sd_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::sd_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*)"sd_write_cc",
		     (char_1*)"error opening the model file");
    return ISIP_FALSE;
  }
  
  for (int_4 i=0; i < IMAGA_SD_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_SD_NAME);
    }
    
    else if (counter == 2) {
      
      // 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",
	      sd_r_npxls_d, sd_r_nblks_d);
    }

    else if (counter == 3) {
      
      // 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_SD_WRED, IMAGA_SD_WGRN, IMAGA_SD_WBLU);
    }
    
    else if (counter == 4) {
      
      // 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_SD_ASCL,
	      IMAGA_SD_BSCL);
    }
    
    else if (counter == 5) {
      
      // write the standard deviations 
      //
      fprintf(fp, (char*)IMAGA_HIST_COMMENT_FORMAT, IMAGA_HIST_COMMENT,
	      (char*)IMAGA_SD_VALUES_COMMENT, ISIP_POUND);
      fprintf(fp, "std_red = %f\nstd_green = %f\nstd_blue = %f\n\n",
	      sd_t_red_d, sd_t_grn_d, sd_t_blu_d);
    }
    counter++;
  }
  
  // close the file
  //
  if (close_cc(fp) == ISIP_FALSE) {
    error_handler_cc((char_1*)"sd_write_cc",
		     (char_1*)"error closing the file");
  } 
  
  // exit gracefully
  //
  return ISIP_TRUE;
}
