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

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

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

#include "image_analysis_constants.h"

// method: dent_compute_cc
//
// arguments:
//  char_1* image_fname: (input) the image file name
//  char_1* seg_fname: (input) the segmentation file name
//
// return:
//  a logical_1 value indicating status
//
// this method computes a histogram on some prescribed region of a PPM image
//
logical_1 Image_analysis::dent_compute_cc(char_1* image_fname_a,
					  char_1* seg_fname_a) {

  // declare a ppm object
  //
  Ppm ppm;

  // load the segmentation data as polygons
  //
  if (load_polygons_cc(seg_fname_a) == ISIP_FALSE) {
    error_handler_cc((char_1*)"dent_compute_cc",
		     (char_1*)"error reading the segmentation data");
    return ISIP_FALSE;
  }
  
  // if no region corresponding to the transcription was found in the
  // image, no computation needed
  //
  if (polygon_list_d == (Link_list*)NULL) {
    return ISIP_TRUE;
  }
  else if ((polygon_list_d->get_head_cc()) == (Link_node*)NULL) {
    return ISIP_TRUE;
  }

  // open the ppm file
  //
  if (ppm.open_cc(image_fname_a, ISIP_READ_ONLY) == ISIP_FALSE) {
    error_handler_cc((char_1*)"hist_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*)"hist_compute_cc",
		     (char_1*)"error while reading the file");
    return ISIP_FALSE;
  }

  // assign the bins for the red buffer
  //
  if (dent_red_assign_cc(b_red, width, height) == ISIP_FALSE) {
    error_handler_cc((char_1*)"dent_compute_cc",
		     (char_1*)"error assigning the bins");
    return ISIP_FALSE;
  }

  // assign the bins for the green buffer
  //
  if (dent_grn_assign_cc(b_green, width, height) == ISIP_FALSE) {
    error_handler_cc((char_1*)"dent_compute_cc",
		     (char_1*)"error assigning the bins");
    return ISIP_FALSE;
  }

  // assign the bins for the blue buffer
  //
  if (dent_blu_assign_cc(b_blue, width, height) == ISIP_FALSE) {
    error_handler_cc((char_1*)"dent_compute_cc",
		     (char_1*)"error assigning the bins");
    return ISIP_FALSE;
  }

  // compute the sum of the entropies of each of the color
  //
  if (dent_sum_cc() == ISIP_FALSE) {
    error_handler_cc((char_1*)"dent_compute_cc",
		     (char_1*)"error assigning the bins");
    return ISIP_FALSE;
  }
  
  // close the ppm object
  //
  if (ppm.close_cc() == ISIP_FALSE) {
    error_handler_cc((char_1*)"dent_compute_cc",
		     (char_1*)"error closing the file");
    return ISIP_FALSE;
  }

  // increment the counters
  //
  dent_r_nimages_d += (int_4)1;

  // exit gracefully
  //
  return ISIP_TRUE;
}
