// file: $USFS/class/image_analysis/v6.2/imaga_gabor_00.cc
//

// system include files
//
#include <string.h>

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

// method: gabor_init_cc
//
// arguments:
//  int_4 width: (input) block width
//  int_4 height: (input) block height
//
// return: a logical_1 value indicating status
//
// this method initializes parameters for gabor filtering
//
logical_1 Image_analysis::gabor_init_cc(int_4 width_a, int_4 height_a) {

  // delete memory if necessary
  //
  if (gabor_rad_frq_d != (float_8*)NULL) {
    delete [] gabor_rad_frq_d;
    gabor_rad_frq_d = (float_8*)NULL;
  }
  if (gabor_orient_d != (float_8*)NULL) {
    delete [] gabor_orient_d;
    gabor_orient_d = (float_8*)NULL;
  }
  if (gabor_feature_d != (float_8*)NULL) {
    delete [] gabor_feature_d;
    gabor_feature_d = (float_8*)NULL;
  }

  gabor_num_col_d = width_a;
  gabor_num_row_d = height_a;
  gabor_num_rad_frq_d = IMAGA_GABOR_NUM_RAD_FRQ;
  gabor_num_orient_d = IMAGA_GABOR_NUM_ORIENT;
  gabor_feature_dim_d = gabor_num_rad_frq_d * gabor_num_orient_d;

  // set some parameters to default values if they are not given
  // in the parameter file
  //
  if (gabor_sigma_x_d == (float_8)(-1)) {
    gabor_sigma_x_d = IMAGA_GABOR_SIGMA_X;
  }
  if (gabor_sigma_y_d == (float_8)(-1)) {
    gabor_sigma_y_d = IMAGA_GABOR_SIGMA_Y;
  }
  if (gabor_alpha_d == (float_8)(-1)) {
    gabor_alpha_d = IMAGA_GABOR_ALPHA;
  }
  if (gabor_win_scale_d == (int_4)(-1)) {
    gabor_win_scale_d = IMAGA_GABOR_WIN_SCALE;
  }
  
  // allocate memory
  //
  gabor_rad_frq_d = new float_8[gabor_num_rad_frq_d];
  memset(gabor_rad_frq_d, 0, gabor_num_rad_frq_d * sizeof(float_8));
  gabor_orient_d = new float_8[gabor_num_orient_d];
  memset(gabor_orient_d, 0, gabor_num_orient_d * sizeof(float_8));
  gabor_feature_d = new float_8[gabor_feature_dim_d];
  memset(gabor_feature_d, 0, gabor_feature_dim_d * sizeof(float_8));

  // compute radial frequencies and orientations
  //
  int_4 i_end = (int_4)(log(gabor_num_col_d) / log(2.0) - 1);
  int_4 i_start = i_end - gabor_num_rad_frq_d;
  if (i_start < 0) {
    error_handler_cc((char_1*)"gabor_init_cc",
		     (char_1*)"number of radial frequencies too large");
    return ISIP_FALSE;
  }
  
  for (int_4 i = i_start; i < i_end; i++) {
    gabor_rad_frq_d[i - i_start] = pow(2.0, i) * sqrt(2.0) / gabor_num_col_d;
  }
  for (int_4 i = 0; i < gabor_num_orient_d; i++) {
    gabor_orient_d[i] = i * PI / gabor_num_orient_d;
  }
  
  // initialize counters
  //
  gabor_r_npxls_d = (int_4)0;
  gabor_r_nblks_d = (int_4)0;
    
  // exit gracefully
  //
  return ISIP_TRUE;
}
