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

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

// method: edge_compare_cc
//
// arguments:
//  float_8& sbe_a: (output) sbe value
//
// return: a logical_1 value indicating status
//
// this method computes an sbe from two histograms.
//
logical_1 Image_analysis::edge_compare_cc(float_8& sbe_a) {
  
  // declare local variables
  //
  float_8 sum_l = (float_8)0.0;
  float_8 frac_l = (float_8)0.0;
  float_8 prod_l = (float_8)0.0;
  
  // compute the constant for the base 2 logarithm
  //
  float_8 scale = 1 / log(2);
  
  // compare the data from the model file with the computed data
  //
  for (int_4 i=0; i<edge_r_nbins_d; i++) {
    
    // compute the relative entropy
    //
    if (edge_r_lpdf_d[i] != (int)0) {
      if (edge_t_lpdf_d[i] != (int)0) {
	frac_l = edge_t_lpdf_d[i] / edge_r_lpdf_d[i];
	prod_l = edge_t_lpdf_d[i] * log(frac_l) * scale;
	sum_l += prod_l;
      }
    }
  }
  
  // print the individual entropies of red, green and blue
  //
  fprintf (stdout, "%3.3f\t", sum_l);
  
  // weight the contributions from each color
  //
  float_8 rms_scaled = edge_lwgt_d * sum_l;
  
  if (debug_level_d > ISIP_DEBUG_DETAILED) {
    fprintf(stdout,"<%s> rms error = %f: (l) %f\n",
	    "Image_analysis::hist_compare_cc", rms_scaled, sum_l);
  }
  
  // compute the scaled sbe
  //
  sbe_a = edge_ascale_d * rms_scaled + edge_bscale_d;
  
  if (debug_level_d > ISIP_DEBUG_BRIEF) {
    fprintf(stdout,"<%s> sbe = %f\n",
	    "Image_analysis::edge_compare_cc", sbe_a);
  }
  
  // exit gracefully
  //
  return ISIP_TRUE;
}


