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

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

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

// method: dct_init_cc
//
// arguments: none
//
// return: a logical_1 value indicating status
//
// this method initializes the necessary parameters for the dct-based feature
// extraction
//
logical_1 Image_analysis::dct_init_cc() {

  // deallocate memory if necessary
  //
  if (dct_r_red_d != (float_4*)NULL) {
    delete [] dct_r_red_d;
    dct_r_red_d = (float_4*)NULL;
  }

  if (dct_r_grn_d != (float_4*)NULL) {
    delete [] dct_r_grn_d;
    dct_r_grn_d = (float_4*)NULL;
  }
  
  if (dct_r_blu_d != (float_4*)NULL) {
    delete [] dct_r_blu_d;
    dct_r_blu_d = (float_4*)NULL;
  }
    
  if (dct_r_cos_d != (float_4**)NULL) {
    for (int_4 i=0; i<dct_blk_size_d; i++) {
      if (dct_r_cos_d[i] != (float_4*)NULL) {
	delete [] dct_r_cos_d[i];
	dct_r_cos_d[i] = (float_4*)NULL;
      }
    }
    delete [] dct_r_cos_d;
    dct_r_cos_d = (float_4**)NULL;    
  }

  if (dct_norm_d != (float_4**)NULL) {
    for (int_4 i=0; i<dct_blk_size_d; i++) {
      if (dct_norm_d[i] != (float_4*)NULL) {
	delete [] dct_norm_d[i];
	dct_norm_d[i] = (float_4*)NULL;
      }
    }
    delete [] dct_norm_d;
    dct_norm_d = (float_4**)NULL;    
  }

  if (dct_t_red_filter_d != (float_4*)NULL) {
    delete [] dct_t_red_filter_d;
    dct_t_red_filter_d = (float_4*)NULL;
  }
  
  if (dct_t_grn_filter_d != (float_4*)NULL) {
    delete [] dct_t_grn_filter_d;
    dct_t_grn_filter_d = (float_4*)NULL;
  }
  
  if (dct_t_blu_filter_d != (float_4*)NULL) {
    delete [] dct_t_blu_filter_d;
    dct_t_blu_filter_d = (float_4*)NULL;
  }
  
  // allocate memory and clear
  //
  dct_r_red_d = new float_4[dct_blk_size_d * dct_blk_size_d];
  dct_r_grn_d = new float_4[dct_blk_size_d * dct_blk_size_d];
  dct_r_blu_d = new float_4[dct_blk_size_d * dct_blk_size_d];
  
  memset(dct_r_red_d, 0,
	 (dct_blk_size_d * dct_blk_size_d * sizeof(float_4)));
  memset(dct_r_grn_d, 0,
	 (dct_blk_size_d * dct_blk_size_d * sizeof(float_4)));
  memset(dct_r_blu_d, 0,
	 (dct_blk_size_d * dct_blk_size_d * sizeof(float_4)));

  dct_r_cos_d = new float_4*[dct_blk_size_d];
  for (int_4 i=0; i<dct_blk_size_d; i++) {
    dct_r_cos_d[i] = new float_4[dct_blk_size_d];
    memset(dct_r_cos_d[i], 0, (dct_blk_size_d * sizeof(float_4)));
  }

  dct_norm_d = new float_4*[dct_blk_size_d];
  for (int_4 i=0; i<dct_blk_size_d; i++) {
    dct_norm_d[i] = new float_4[dct_blk_size_d];
    memset(dct_norm_d[i], 0, (dct_blk_size_d * sizeof(float_4)));
  }

  dct_t_red_filter_d = new float_4[IMAGA_DCT_FEATURE_SIZE];
  dct_t_grn_filter_d = new float_4[IMAGA_DCT_FEATURE_SIZE];
  dct_t_blu_filter_d = new float_4[IMAGA_DCT_FEATURE_SIZE];

  memset(dct_t_red_filter_d, 0, IMAGA_DCT_FEATURE_SIZE * sizeof(float_4));
  memset(dct_t_grn_filter_d, 0, IMAGA_DCT_FEATURE_SIZE * sizeof(float_4));
  memset(dct_t_blu_filter_d, 0, IMAGA_DCT_FEATURE_SIZE * sizeof(float_4));
  
  // clear the counters
  //
  dct_r_npxls_d = (int_4)0;
  dct_r_nimages_d = (int_4)0;
  dct_r_nblks_d = (int_4)0;
  
  // exit gracefully
  //
  return ISIP_TRUE;
}
