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

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

// method: hist_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::hist_compare_cc(float_8& sbe_a) {
  
  // declare local variables
  //
  float_8 sum_r = 0.0;
  float_8 sum_g = 0.0;
  float_8 sum_b = 0.0;
  float_8 rms_scaled = 0.0;
  
  // compare the data from the model file with the computed data
  //
  for (int_4 i=0; i<hist_r_nbins_d; i++) {
    
    // compute the differences
    //
    float_8 diff_r = hist_r_rpdf_d[i] - hist_t_rpdf_d[i];
    float_8 diff_g = hist_r_gpdf_d[i] - hist_t_gpdf_d[i];
    float_8 diff_b = hist_r_bpdf_d[i] - hist_t_bpdf_d[i];
    
    // accumulate
    //
    sum_r += diff_r * diff_r;
    sum_g += diff_g * diff_g;
    sum_b += diff_b * diff_b;
  }

  // convert to rms
  //
  float_8 scale_n = (float_8)1.0 / hist_r_nbins_d;

  sum_r = sqrt(sum_r * scale_n);
  sum_g = sqrt(sum_g * scale_n);
  sum_b = sqrt(sum_b * scale_n);
  
  // print the individual rms errors of the colors
  //
  //fprintf(stdout, "%3.3f\t%3.3f\t%3.3f\t", sum_r, sum_g, sum_b);

  // weight the contributions from each color
  //
  //rms_scaled = hist_rwgt_d * sum_r + hist_gwgt_d * sum_g +
  //hist_bwgt_d * sum_b;
  
  if (debug_level_d > ISIP_DEBUG_DETAILED) {
    fprintf(stdout,"<%s> rms error = %f: (r) %f (g) %f (b) %f\n",
	    "Image_analysis::hist_compare_cc",
	    rms_scaled, sum_r, sum_g, sum_b);
  }
  
  // compute the scaled sbe
  //
  //sbe_a = hist_ascale_d * rms_scaled + hist_bscale_d;
  
  sbe_a = sum_g * hist_ascale_d + hist_bscale_d;
  
  if (debug_level_d > ISIP_DEBUG_BRIEF) {
    fprintf(stdout,"<%s> sbe = %f\n",
	    "Image_analysis::hist_compare_cc", sbe_a);
  }
  
  // exit gracefully
  //
  return ISIP_TRUE;
}
