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

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

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

// method: rle_init_cc
//
// arguments: none
//
// return: a logical_1 value indicating status
//
// this method initializes the necessary parameters for the run length coding
//
logical_1 Image_analysis::rle_init_cc() {

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

  if (rle_r_grn_d != (float_4*)NULL) {
    delete [] rle_r_grn_d;
    rle_r_grn_d = (float_4*)NULL;
  }
  
  if (rle_r_blu_d != (float_4*)NULL) {
    delete [] rle_r_blu_d;
    rle_r_blu_d = (float_4*)NULL;
  }
    
  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];
	rle_r_cos_d[i] = (float_4*)NULL;
      }
    }
    delete [] rle_r_cos_d;
    rle_r_cos_d = (float_4**)NULL;    
  }
  
  // allocate memory and clear
  //
  rle_r_red_d = new float_4[IMAGA_RLE_BLK_SIZE * IMAGA_RLE_BLK_SIZE];
  rle_r_grn_d = new float_4[IMAGA_RLE_BLK_SIZE * IMAGA_RLE_BLK_SIZE];
  rle_r_blu_d = new float_4[IMAGA_RLE_BLK_SIZE * IMAGA_RLE_BLK_SIZE];
  
  memset(rle_r_red_d, 0,
	 (IMAGA_RLE_BLK_SIZE * IMAGA_RLE_BLK_SIZE * sizeof(float_4)));
  memset(rle_r_grn_d, 0,
	 (IMAGA_RLE_BLK_SIZE * IMAGA_RLE_BLK_SIZE * sizeof(float_4)));
  memset(rle_r_blu_d, 0,
	 (IMAGA_RLE_BLK_SIZE * IMAGA_RLE_BLK_SIZE * sizeof(float_4)));

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

  // initialize the DC value
  //
  rle_r_rpdc_d = 0;
  rle_r_gpdc_d = 0;
  rle_r_bpdc_d = 0;

  // initialize the bit values
  //
  rle_r_redbits_d = 0;
  rle_r_grnbits_d = 0;
  rle_r_blubits_d = 0;
  
  // clear the counters
  //
  rle_r_npxls_d = (int_4)0;
  rle_r_nimages_d = (int_4)0;
  rle_r_nblks_d = (int_4)0;
  rle_t_compression_d = (int_4)0;
  
  // exit gracefully
  //
  return ISIP_TRUE;
}
