// file: $USFS/class/image_analysis/v3.0/imaga_fract_03.cc
//

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

// isip include files
//
#include "image_analysis.h"
#include <ppm.h>

#include "image_analysis_constants.h"

// method: fract_compute_cc
//
// arguments:
//  char_1* image_fname: (input) the image file name
//
// return:
//  a logical_1 value indicating status
//
// this method computes the fractal dimension of each of the color
//
logical_1 Image_analysis::fract_compute_cc(char_1* image_fname_a) {

  // declare a ppm object
  //
  Ppm ppm;

  // open the ppm file
  //
  if (ppm.open_cc(image_fname_a, ISIP_READ_ONLY) == ISIP_FALSE) {
    error_handler_cc((char_1*)"fract_compute_cc",
		     (char_1*)"error opening the image file");
    return ISIP_FALSE;
  }

  // read the dimensions of the image
  //
  int_4 width = ppm.get_width_cc();
  int_4 height = ppm.get_height_cc();

  // declaring the buffers for reading the ppm image
  //
  uint_1 b_red[width * height];           
  uint_1 b_green[width * height];         
  uint_1 b_blue[width * height];
  
  // read the ppm file 
  //
  if (ppm.read_cc(b_red, b_green, b_blue, IMAGA_EDGE_WOFF, IMAGA_EDGE_HOFF,
		  width, height) == ISIP_FALSE) {
    error_handler_cc((char_1*)"fract_compute_cc",
		     (char_1*)"error while reading the file");
    return ISIP_FALSE;
  }

  // declaring buffers for the floating point value
  //
  float_4* red_b = new float_4[width * height];
  float_4* grn_b = new float_4[width * height];
  float_4* blu_b = new float_4[width * height];

  for (int_4 i=0; i < (width * height); i++) {
    red_b[i] = (float_4)b_red[i];
    grn_b[i] = (float_4)b_green[i];
    blu_b[i] = (float_4)b_blue[i];
  }

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

      // read the coordinates and compute the area
      //
      if (fract_tri_cc(i, j, width, height, 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, height, 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 fractaldimension
  //
  if (fract_dim_cc() == ISIP_FALSE) {
    error_handler_cc((char_1*)"fract_compute_cc",
		     (char_1*)"error computing the fractal dimension");
    return ISIP_FALSE;
  }
  
  // close the ppm object
  //
  if (ppm.close_cc() == ISIP_FALSE) {
    error_handler_cc((char_1*)"fract_compute_cc",
		     (char_1*)"error closing the file");
    return ISIP_FALSE;
  }
  
  // increment the counters
  //
  fract_r_nimages_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;
}
