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

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

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

// method: fract_compute_cc
//
// arguments:
//  uint_1* blk_r: (input) buffer for red color
//  uint_1* blk_g: (input) buffer for green color
//  uint_1* blk_b: (input) buffer for blue color
//  int_4 offset_w: (input) offset in width for the image block
//  int_4 offset_h: (input) offset in height for the image block
//  int_4 width: (input) width of the image block
//  int_4 height: (input) height of the image block
//
// return:
//  a logical_1 value indicating status
//
// this method computes the fractal dimension of each of the color on an
// image block
//
logical_1 Image_analysis::fract_compute_cc(uint_1* blk_r_a, uint_1* blk_g_a,
				           uint_1* blk_b_a, int_4 offset_w_a,
				           int_4 offset_h_a, int_4 width_a,
				           int_4 height_a) {

  // declare local variable
  //
  logical_1 process_block = ISIP_TRUE;
  
  // check if segmentation data needed
  //
  if (check_segflag_cc() == ISIP_TRUE) {

    // if no region corresponding to the transcription was found in the
    // image, no computation needed
    //
    if (polygon_list_d == (Link_list*)NULL) {
      process_block = ISIP_FALSE;
    }
    else if ((polygon_list_d->get_head_cc()) == (Link_node*)NULL) {
      process_block = ISIP_FALSE;
    }

    // else check if the block is inside the prescribed region
    //
    else {

      // compute the coordinates for the block center
      //
      int_4 center_x = offset_w_a + width_a / 2;
      int_4 center_y = offset_h_a + height_a / 2;

      // if the center of the block is not inside the prescibed region, no
      // computation needed
      //
      if (is_inside_cc(center_x, center_y) == ISIP_FALSE) {
        process_block = ISIP_FALSE;
      }
    }
  }

  if (process_block == ISIP_TRUE) {
    
    // declaring buffers for the floating point value
    //
    float_4* red_b = new float_4[width_a * height_a];
    float_4* grn_b = new float_4[width_a * height_a];
    float_4* blu_b = new float_4[width_a * height_a];

    for (int_4 i=0; i<(width_a * height_a); i++) {
      red_b[i] = (float_4)blk_r_a[i];
      grn_b[i] = (float_4)blk_g_a[i];
      blu_b[i] = (float_4)blk_b_a[i];
    }

    // finding the area of the fractals
    //
    for (int_4 i=0; i<height_a; i++) {
      for (int_4 j=0; j<width_a; j++) {

          // read the coordinates and compute the area
          //
          if (fract_tri_cc(i, j, width_a, height_a, red_b,
		           grn_b, blu_b, IMAGA_FRACT_FDIST) == ISIP_FALSE) {
	    error_handler_cc((char_1*)"fract_compute_cc",
			     (char_1*)"error computing the fractal dimension");
	    return ISIP_FALSE;
          }

          if (fract_tri_cc(i, j, width_a, height_a, red_b,
		           grn_b, blu_b, IMAGA_FRACT_SDIST) == ISIP_FALSE) {
	    error_handler_cc((char_1*)"fract_compute_cc",
			     (char_1*)"error computing the fractal dimension");
	    return ISIP_FALSE;
          }

          // increment the pixel numbers
          //
          fract_r_npxls_d++;
      }
    }

    // computing the fractal dimension
    //
    if (fract_dim_cc() == ISIP_FALSE) {
      error_handler_cc((char_1*)"fract_compute_cc",
		       (char_1*)"error computing the fractal dimension");
      return ISIP_FALSE;
    }
  
    // increment the counters
    //
    fract_r_nblks_d += (int_4)1;

    // delete the memory
    //
    if (red_b != (float_4*)NULL) {
      delete [] red_b;
    }

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

    if (blu_b != (float_4*)NULL) {
      delete [] blu_b;
    }
  }
  
  // exit gracefully
  //
  return ISIP_TRUE;
}
