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

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

// method: load_cc
//
// arguments:
//  char_1* param_file: (input) the parameter file to be read
//
// return: a logical_1 value indicating status
//
// this method opens the parameter file and reads the mode, algorithm,
// and other appropriate information.
//
logical_1 Image_analysis::load_cc(char_1* param_file_a) {

  // open the parameter file
  //
  FILE* fp = open_cc(param_file_a, ISIP_READ_ONLY);
  if (fp == (FILE*)NULL) {
    error_handler_cc((char_1*)"load_cc",
		     (char_1*)"error loading parameter file");
    return ISIP_FALSE;
  }
  
  // read the version used (first line in the file)
  //
  char_1* buf = new char_1[ISIP_MAX_STRING_LENGTH];

  if (fscanf(fp, "%s", (char*)buf) == (int)0) {
    error_handler_cc((char_1*)"load_cc", (char_1*)"version not found");
    return ISIP_FALSE;
  }
  
  // check the version
  //
  if (strcmp ((char*)buf, (char*)IMAGA_VERSION) != (int)0) {
    error_handler_cc((char_1*)"load_cc", (char_1*)"incorrect version");
    return ISIP_FALSE;
  }
  if (param_version_d != (char_1*)NULL) {
    delete [] param_version_d;
    param_version_d = (char_1*)NULL;
  }
  param_version_d = new char_1[strlen((char*)buf) + (int_4)1];
  strcpy((char*)param_version_d, (char*)buf);
  
  // get the mode
  //
  if (get_mode_cc(fp) == ISIP_FALSE) {
    error_handler_cc((char_1*)"load_cc", (char_1*)"invalid mode");
    return ISIP_FALSE;
  }
  
  // get the algorithm
  //
  if (get_algorithm_cc(fp) == ISIP_FALSE) {
    error_handler_cc((char_1*)"load_cc", (char_1*)"invalid algorithm");
    return ISIP_FALSE;
  }

  // get the segmentation flag
  //
  if (get_segflag_cc(fp) == ISIP_FALSE) {
    error_handler_cc((char_1*)"load_cc", (char_1*)"invalid segmentation flag");
    return ISIP_FALSE;
  }

  // get the transcription
  //
  if (get_trans_cc(fp) == ISIP_FALSE) {
    error_handler_cc((char_1*)"load_cc", (char_1*)"invalid transcription");
    return ISIP_FALSE;
  }

  // get the frame size
  //
  if (get_frmsize_cc(fp) == ISIP_FALSE) {
    error_handler_cc((char_1*)"load_cc", (char_1*)"invalid frame size");
    return ISIP_FALSE;
  }

  // get the window size
  //
  if (get_winsize_cc(fp) == ISIP_FALSE) {
    error_handler_cc((char_1*)"load_cc", (char_1*)"invalid window size");
    return ISIP_FALSE;
  }

  // get the dct block size
  //
  if (get_dct_blksize_cc(fp) ==  ISIP_FALSE) {
    error_handler_cc((char_1*)"load_cc", (char_1*)"invalid dct block size");
    return ISIP_FALSE;
  }

  // get the dct filter size
  //
  if (get_dct_filtersize_cc(fp) == ISIP_FALSE) {
    error_handler_cc((char_1*)"load_cc", (char_1*)"invalid dct filter size");
    return ISIP_FALSE;
  }

  // get the sigma_x value for computing gabor features
  //
  if (get_gabor_sigma_x_cc(fp) == ISIP_FALSE) {
    error_handler_cc((char_1*)"load_cc",
		     (char_1*)"invalid gabor sigma_x value");
    return ISIP_FALSE;
  }

  // get the sigma_y value for computing gabor features
  //
  if (get_gabor_sigma_y_cc(fp) == ISIP_FALSE) {
    error_handler_cc((char_1*)"load_cc",
		     (char_1*)"invalid gabor sigma_y value");
    return ISIP_FALSE;
  }

  // get the alpha value for computing gabor features
  //
  if (get_gabor_alpha_cc(fp) == ISIP_FALSE) {
    error_handler_cc((char_1*)"load_cc",
		     (char_1*)"invalid gabor alpha value");
    return ISIP_FALSE;
  }

  // get the win_scale value for computing gabor features
  //
  if (get_gabor_win_scale_cc(fp) == ISIP_FALSE) {
    error_handler_cc((char_1*)"load_cc",
		     (char_1*)"invalid gabor win_scale value");
    return ISIP_FALSE;
  }
  
  // get the model filename (must be specified in all cases)
  //
  if (get_modelfile_cc(fp) == ISIP_FALSE) {
    error_handler_cc((char_1*)"load_cc", (char_1*)"invalid model filename");
    return ISIP_FALSE;
  }

  // allocate memory for the model name and the selection vector
  //
  if (model_name_d != (char_1*)NULL) {
    delete [] model_name_d;
    model_name_d = (char_1*)NULL;
  }
  model_name_d = new char_1[ISIP_MAX_STRING_LENGTH];

  if (sel_vec_d != (int_4*)NULL) {
    delete [] sel_vec_d;
    sel_vec_d = (int_4*)NULL;
  }
  sel_vec_d = new int_4[IMAGA_HE_VECTORS_VALUE];
  
  // load the model only if it is test or update
  //
  if ((mode_d == IMAGA_MODE_TEST) || (mode_d == IMAGA_MODE_UPDATE)) {
    if (load_model_cc() == ISIP_FALSE) {
      error_handler_cc((char_1*)"load_cc",
		       (char_1*)"error loading the reference model");
      return ISIP_FALSE;
    }
  }
  
  // get the selection vector
  //
  if (mode_d == IMAGA_MODE_TRAIN) {
    if (get_selvec_cc(fp) == ISIP_FALSE) {
      error_handler_cc((char_1*)"load_cc", (char_1*)"invalid vector");
      return ISIP_FALSE;
    }
  }
  
  // close the file
  //
  if (close_cc(fp) == ISIP_FALSE) {
    error_handler_cc((char_1*)"load_cc", (char_1*)"error closing the file");
    return ISIP_FALSE;
  }
  
  // provide some debugging information
  //
  if (debug_level_d > ISIP_DEBUG_BRIEF) {
    fprintf(stdout,
	    "<%s> version: %s algorithm: %ld\n",
	    "Image_analysis::load_cc", param_version_d, algorithm_d);
    fprintf(stdout, "mode = %ld model_filename: %s\n",
	    mode_d, model_fname_d);
  }
  
  // dump the model
  //
  if (debug_level_d > ISIP_DEBUG_DETAILED) {
    display_model_cc(stdout);
  }

  // release memory
  //
  delete [] buf;

  // exit gracefully
  //
  return ISIP_TRUE;
}
