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

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

// method: init_cc
//
// arguments: none
//
// return: a logical_1 value indicating status
//
// this method initializes an image_analysis object. for algorithms using
// color histogram and/or edge detection only, this method will only be used
// in training mode. for algorithms using other features, this method should
// be called before any computation both in training and test mode.
// note the initialization should only be executed once. refer to
// image_segmentation::train_cc/test_cc for details as to the usage.
//
logical_1 Image_analysis::init_cc() {

  // check for the mode
  //
  if (((mode_d == IMAGA_MODE_TRAIN) && (algorithm_d == IMAGA_ALG_HISTOGRAM))
      || ((mode_d == IMAGA_MODE_TRAIN) && (algorithm_d == IMAGA_ALG_FACTOR))) {
      
    // initialize the private data
    //
    hist_r_min_d = IMAGA_HIST_MIN;
    hist_r_max_d = IMAGA_HIST_MAX;
    hist_r_nbins_d = IMAGA_HIST_NBINS;
    hist_r_redp_d = IMAGA_HIST_RPTR;
    hist_r_grnp_d = IMAGA_HIST_GPTR;
    hist_r_blup_d = IMAGA_HIST_BPTR;
      
    if (hist_init_cc() == ISIP_FALSE) {
      error_handler_cc((char_1*)"init_cc",
		       (char_1*)"error while initializing the histogram");
      return ISIP_FALSE;
    }
      
    float_8 range = (hist_r_max_d - hist_r_min_d) / (float_8)(hist_r_nbins_d - 1);
      
    // initializing the center values
    //
    hist_t_rcval_d[0] = hist_r_min_d;
    hist_t_gcval_d[0] = hist_r_min_d;
    hist_t_bcval_d[0] = hist_r_min_d;
    hist_t_ylwcval_d[0] = hist_r_min_d;
    hist_t_brncval_d[0] = hist_r_min_d;
      
    for (int_4 i=1; i < hist_r_nbins_d; i++) {
      hist_t_rcval_d[i] = hist_t_rcval_d[i-1] + range;
      hist_t_gcval_d[i] = hist_t_gcval_d[i-1] + range;
      hist_t_bcval_d[i] = hist_t_bcval_d[i-1] + range;
      hist_t_ylwcval_d[i] = hist_t_ylwcval_d[i-1] + range;
      hist_t_brncval_d[i] = hist_t_brncval_d[i-1] + range;
    }
  }
    
  if (((mode_d == IMAGA_MODE_TRAIN) && (algorithm_d == IMAGA_ALG_EDGEDET)) ||
      ((mode_d == IMAGA_MODE_TRAIN) && (algorithm_d == IMAGA_ALG_FACTOR))) {
      
    // initialize the private data
    //
    edge_r_nbins_d = IMAGA_EDGE_NBINS;
    edge_r_rfact_d = IMAGA_EDGE_FACTOR;
    edge_r_msize_d = IMAGA_EDGE_MSIZE;
    edge_r_xmean_d = IMAGA_EDGE_XMEAN;
    edge_r_ymean_d = IMAGA_EDGE_YMEAN;
    edge_r_lmin_d = IMAGA_EDGE_LMIN;
    edge_r_lmax_d = IMAGA_EDGE_LMAX;
    edge_lwgt_d = IMAGA_EDGE_LWGT;
    edge_r_sigma_d = IMAGA_EDGE_SIGMA;
      
    if (edge_init_cc() == ISIP_FALSE) {
      error_handler_cc((char_1*)"init_cc",
		       (char_1*)"error while initializing the edge detection");
      return ISIP_FALSE;
    }
      
    float_4 range = (edge_r_lmax_d - edge_r_lmin_d) / (int_4)(edge_r_nbins_d - 1);
      
    // initializing the center values
    //
    edge_t_lcval_d[0] = 0.0;
      
    for (int_4 i=1; i < edge_r_nbins_d; i++) {
      edge_t_lcval_d[i] = edge_t_lcval_d[i-1] + range;
    }
  }
    
  // initialising the private data
  //
  if ((mode_d == IMAGA_MODE_TRAIN)
      && (algorithm_d == IMAGA_ALG_FACTOR)) {
      
    pca_t_num_vectors_d = IMAGA_HE_VECTORS_VALUE;
      
    // deallocating memory if necessary
    //
    if (covar_t_data_d != (float_4*)NULL) {
      delete [] covar_t_data_d; 
    }
      
    if (mean_t_data_d != (float_4*)NULL) {
      delete [] mean_t_data_d;
    }

    covar_t_data_d = new float_4[pca_t_values_read_d * pca_t_values_read_d];
    mean_t_data_d = new float_4[pca_t_values_read_d];
      
    // intializing the array to zero
    //
    for (int i=0; i < (pca_t_values_read_d * pca_t_values_read_d); i++) {
      covar_t_data_d[i] = 0.0;
    }
      
    // intializing the mean array to zero
    //
    for (int_4 i=0; i < pca_t_values_read_d; i++) {
      mean_t_data_d[i] = 0.0;
    }
      
    if (pca_init_cc() == ISIP_FALSE) {
      error_handler_cc((char_1*)"init_cc",
		       (char_1*)"error initialising the data");
      return ISIP_FALSE;
    }      
  }

  if (((mode_d == IMAGA_MODE_TRAIN) && (algorithm_d == IMAGA_ALG_DIFF)) ||
      (algorithm_d == IMAGA_ALG_FACTOR)) {
      
    // initialize the private data
    //
    dent_r_min_d = IMAGA_DIFF_MIN;
    dent_r_max_d = IMAGA_DIFF_MAX;
    dent_r_nbins_d = dent_r_max_d - dent_r_min_d + (int_4)1;
      
    if (dent_init_cc() == ISIP_FALSE) {
      error_handler_cc((char_1*)"init_cc",
		       (char_1*)"error initializing the fft data");
      return ISIP_FALSE;
    }
  }

  if (((mode_d == IMAGA_MODE_TRAIN) && (algorithm_d == IMAGA_ALG_SD)) ||
      (algorithm_d == IMAGA_ALG_FACTOR)) {
      
    // initialize the private data
    //
    if (sd_init_cc() == ISIP_FALSE) {
      error_handler_cc((char_1*)"init_cc",
		       (char_1*)"error initializing the sd data");
      return ISIP_FALSE;
    }
  }

  if (((mode_d == IMAGA_MODE_TRAIN) && (algorithm_d == IMAGA_ALG_ENT)) ||
      (algorithm_d == IMAGA_ALG_FACTOR)) {
      
    // initialize the private data
    //
    huf_r_min_d = IMAGA_ENT_MIN;
    huf_r_max_d = IMAGA_ENT_MAX;
    huf_r_nbins_d = huf_r_max_d - huf_r_min_d + 1;
      
    if (ent_init_cc() == ISIP_FALSE) {
      error_handler_cc((char_1*)"init_cc",
		       (char_1*)"error initializing the sd data");
      return ISIP_FALSE;
    }
  }
    
  if (((mode_d == IMAGA_MODE_TRAIN) && (algorithm_d == IMAGA_ALG_RLE)) ||
      (algorithm_d == IMAGA_ALG_FACTOR)) {

    // initialize the private data
    //
    if (rle_init_cc() == ISIP_FALSE) {
      error_handler_cc((char_1*)"init_cc",
		       (char_1*)"error initializing the sd data");
      return ISIP_FALSE;
    }
  }
    
  if ((mode_d == IMAGA_MODE_TRAIN) && (algorithm_d == IMAGA_ALG_FRACT)) {
      
    // initialize the private data
    //
    if (fract_init_cc() == ISIP_FALSE) {
      error_handler_cc((char_1*)"init_cc",
		       (char_1*)"error initializing the fractal data");
      return ISIP_FALSE;
    }
  }
    
  // exit gracefully
  // 
  return ISIP_TRUE;
}
