// file: $USFS/class/image_analysis/v6.0/imaga_cstr_0.cc
//

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

#ifndef __ISIP_POLYGON
#include <polygon.h>
#endif

// method: default destructor
//
// arguments: none
//
// return: none
//
Image_analysis::~Image_analysis() {
  
  // general data needing cleanup
  //
  if (param_version_d != (char_1*)NULL) {
    delete [] param_version_d;
  }
  
  if (param_fname_d != (char_1*)NULL) {
    delete [] param_fname_d;
  }

  if (model_version_d != (char_1*)NULL) {
    delete [] model_version_d;
  }

  if (model_fname_d != (char_1*)NULL) {
    delete [] model_fname_d;
  }

  if (model_name_d != (char_1*)NULL) {
    delete [] model_name_d;
  }

  if (sel_vec_d != (int_4*)NULL) {
    delete [] sel_vec_d;
  }
  
  if (transcription_d != (char_1*)NULL) {
    delete [] transcription_d;
  }

  // clean the linked list of polygons
  //
  if (polygon_list_d != (Link_list*)NULL) {

    // local variable
    //
    Link_node* curr_node = (Link_node*)NULL;
    Link_node* next_node = (Link_node*)NULL;
    Polygon* polygon = (Polygon*)NULL;
    Memory_manager* manager = polygon_list_d->get_manager_cc();
    
    // removing all nodes
    //
    for (curr_node = polygon_list_d->get_head_cc();
	 curr_node != (Link_node*)NULL; curr_node = next_node) {
      next_node = curr_node->get_next_cc();
      polygon = (Polygon*)(curr_node->get_item_cc());
      polygon_list_d->remove_cc(curr_node);
      if (polygon != (Polygon*)NULL) {
	delete polygon;
      }
    }
    delete polygon_list_d;

    // delete the memory manager
    //
    delete manager;
  }
  
  // histogram-related parameters needing cleaning
  //
  if (hist_r_rpdf_d != (float_8*)NULL) {
    delete [] hist_r_rpdf_d;
  }
  
  if (hist_r_gpdf_d != (float_8*)NULL) {
    delete [] hist_r_gpdf_d;
  }
  
  if (hist_r_bpdf_d != (float_8*)NULL) {
    delete [] hist_r_bpdf_d;
  }

  if (hist_r_ylwpdf_d != (float_8*)NULL) {
    delete [] hist_r_ylwpdf_d;
  }
  
  if (hist_r_brnpdf_d != (float_8*)NULL) {
    delete [] hist_r_brnpdf_d;
  }

  if (hist_r_rcval_d != (float_8*)NULL) {
    delete [] hist_r_rcval_d;
  }
  
  if (hist_r_gcval_d != (float_8*)NULL) {
    delete [] hist_r_gcval_d;
  }
  
  if (hist_r_bcval_d != (float_8*)NULL) {
    delete [] hist_r_bcval_d;
  }
  
  if (hist_r_ylwcval_d != (float_8*)NULL) {
    delete [] hist_r_ylwcval_d;
  }
  
  if (hist_r_brncval_d != (float_8*)NULL) {
    delete [] hist_r_brncval_d;
  }
  
  if (hist_t_rpdf_d != (float_8*)NULL) {
    delete [] hist_t_rpdf_d;
  }
  
  if (hist_t_gpdf_d != (float_8*)NULL) {
    delete [] hist_t_gpdf_d;
  }
  
  if (hist_t_bpdf_d != (float_8*)NULL) {
    delete [] hist_t_bpdf_d;
  }

  if (hist_t_ylwpdf_d != (float_8*)NULL) {
    delete [] hist_t_ylwpdf_d;
  }
  
  if (hist_t_brnpdf_d != (float_8*)NULL) {
    delete [] hist_t_brnpdf_d;
  }
  
  if (hist_t_rcval_d != (float_8*)NULL) {
    delete [] hist_t_rcval_d;
  }
  
  if (hist_t_gcval_d != (float_8*)NULL) {
    delete [] hist_t_gcval_d;
  }
  
  if (hist_t_bcval_d != (float_8*)NULL) {
    delete [] hist_t_bcval_d;
  }

  if (hist_t_ylwcval_d != (float_8*)NULL) {
    delete [] hist_t_ylwcval_d;
  }
  
  if (hist_t_brncval_d != (float_8*)NULL) {
    delete [] hist_t_brncval_d;
  }

  // clean edge detection related parameters
  //
  if (edge_r_lpdf_d != (float_8*)NULL) {
    delete [] edge_r_lpdf_d;
  }
  
  if (edge_r_lcval_d != (float_8*)NULL) {
    delete [] edge_r_lcval_d;
  }
  
  if (edge_t_lpdf_d != (float_8*)NULL) {
    delete [] edge_t_lpdf_d;
  }
  
  if (edge_t_lcval_d != (float_8*)NULL) {
    delete [] edge_t_lcval_d;
  }

  // clean pca related parameters
  //
  if (pca_t_data_d != (float_4*)NULL) {
    delete [] pca_t_data_d;
  }

  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;
  }

  // clean differential entropy related parameters
  //
  if (dent_t_red_d != (float_8*)NULL) {
    delete [] dent_t_red_d;
  }

  if (dent_t_grn_d != (float_8*)NULL) {
    delete [] dent_t_grn_d;
  }
  
  if (dent_t_blu_d != (float_8*)NULL) {
    delete [] dent_t_blu_d;
  }

  // clean entropy related parameters
  //
  if (huf_r_rpdf_d != (float_8*)NULL) {
    delete [] huf_r_rpdf_d;
  }

  if (huf_r_gpdf_d != (float_8*)NULL) {
    delete [] huf_r_gpdf_d;
  }
  
  if (huf_r_bpdf_d != (float_8*)NULL) {
    delete [] huf_r_bpdf_d;
  }

  // clean rle related parameters
  //
  if (rle_r_cos_d != (float_4**)NULL) {
    for (int_4 i=0; i<IMAGA_RLE_BLK_SIZE; i++) {
      if (rle_r_cos_d[i] != (float_4*)NULL) {
	delete [] rle_r_cos_d[i];
      }
    }
    delete [] rle_r_cos_d;
  }

  if (rle_r_red_d != (float_4*)NULL) {
    delete [] rle_r_red_d;
  }

  if (rle_r_grn_d != (float_4*)NULL) {
    delete [] rle_r_grn_d;
  }
  
  if (rle_r_blu_d != (float_4*)NULL) {
    delete [] rle_r_blu_d;
  }

  // clean dct related parameters
  //
  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];
      }
    }
    delete [] dct_r_cos_d;
  }

  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];
      }
    }
    delete [] dct_norm_d;
  }
  
  if (dct_r_red_d != (float_4*)NULL) {
    delete [] dct_r_red_d;
  }

  if (dct_r_grn_d != (float_4*)NULL) {
    delete [] dct_r_grn_d;
  }
  
  if (dct_r_blu_d != (float_4*)NULL) {
    delete [] dct_r_blu_d;
  }

  if (dct_t_red_filter_d != (float_4*)NULL) {
    delete [] dct_t_red_filter_d;
  }

  if (dct_t_grn_filter_d != (float_4*)NULL) {
    delete [] dct_t_grn_filter_d;
  }

  if (dct_t_blu_filter_d != (float_4*)NULL) {
    delete [] dct_t_blu_filter_d;
  }

  // clean gabor features related parameters
  //
  if (gabor_feature_d != (float_8*)NULL) {
    delete [] gabor_feature_d;
  }
  if (gabor_rad_frq_d != (float_8*)NULL) {
    delete [] gabor_rad_frq_d;
  }
  if (gabor_orient_d != (float_8*)NULL) {
    delete [] gabor_orient_d;
  }
  
  // exit gracefully
  //
}
