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

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

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

// method: sd_load_cc
//
// arguments: none
//
// return: a logical_1 value indicating status
//
// this method loads a model of a histogram.
//
logical_1 Image_analysis::sd_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*)"sd_load_cc",
		     (char_1*)"error opening the model file");
    return ISIP_FALSE;
  }
  
  // read the version used
  //
  if (fscanf(fp, "%s", (char*)buf) == (int)0) {
    error_handler_cc((char_1*)"sd_load_cc",
		     (char_1*)"incorrect version");
    return ISIP_FALSE;
  }
  
  // check the version
  //
  if (strcmp((char*)buf, (char*)IMAGA_MODEL_VERSION) != (int)0) {
    error_handler_cc((char_1*)"sd_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 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 counter = 0;
  
   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_SD_NPARAMS) {
	sscanf((char*)buf, (char*)IMAGA_FORMAT_0 , str_key, str_val);	
      }
      
      if (counter == 0) {
	if ((strcmp((char*)str_key, (char*)IMAGA_ALG) != (int)0) ||
	    (strcmp((char*)str_val,
		    (char*)IMAGA_ALG_SD_NAME)!= (int)0)) {
	  error_handler_cc((char_1*)"sd_load_cc",
			   (char_1*)"incorrect type of model file");
	  return ISIP_FALSE;
	}
      }
      
      else if (counter == 1) {
	sd_r_npxls_d = atoi((char*)str_val);
      }
      
      else if (counter == 2) {
	sd_r_nblks_d = atoi((char*)str_val);
      }


      else if (counter == 3) {
	sd_rwgt_d = atof((char*)str_val);
      }

      else if (counter == 4) {
	sd_gwgt_d = atof((char*)str_val);
      }

      else if (counter == 5) {
	sd_bwgt_d = atof((char*)str_val);
      }
      
      else if (counter == 6) {
	sd_ascale_d = atoi((char*)str_val);
      }
      
      else if (counter == 7) {
	sd_bscale_d = atoi((char*)str_val);
      }

      else if (counter == 8) {
	sd_r_red_d = atof((char*)str_val) / sd_r_npxls_d;
      }

      else if (counter == 9) {
	sd_r_grn_d = atof((char*)str_val) / sd_r_npxls_d;
      }

      else if (counter == 10) {
	sd_r_blu_d = atof((char*)str_val) / sd_r_npxls_d;
      }
      counter++;
    }
   }
   
   // close the file
   //
   if (close_cc(fp) == ISIP_FALSE) {
     error_handler_cc((char_1*)"ent_load_cc",
		      (char_1*)"error closing the file");
     return ISIP_FALSE;
   }
   
   // exit gracefully
   //
   return ISIP_TRUE;
}
