// file: $USFS/class/image_eval/v1.0/imgev_hist_0.cc
//

// isip include files
//
#include "image_eval.h"

#include <integral_constants.h>
#include "image_eval_constants.h"

// method: histogram_cc
//
// arguments:
//  float_4* bins_midpts_a: (output) containing the midpoint of each bin
//  float_4* bins_freqs_a: (output) containing the frequency of data in each
//                         bin
//  float_4 min_a: (input) containing the midpoint of the first bin
//  float_4 max_a: (input) containing the midpoint of the last bin
//  int_4& num_bin_a: (input) indicating how many bins
//  char_1** ref_fnames_a: (input) filenames of reference file
//  float_4* ref_values_a: (input) scores of reference file
//  int_4 ref_num_items_a: (input) number of items in reference file
//  char_1** tst_fnames_a: (input) filenames of test file
//  float_4* tst_values_a: (input) scores of test file
//  int_4 tst_num_items_a: (input) number of items in test file
//
// return:
//  logical_1 to indicate status
//
// This method calculates the histogram of a set of difference scores, given
// a number of bins, and minimum and maximum range.
//
//
logical_1 Image_eval::histogram_cc(float_4* bins_midpts_a, int_4* bins_freqs_a,
				   float_4 min_a, float_4 max_a,
				   int_4 num_bins_a, char_1** ref_fnames_a,
				   float_4* ref_values_a,
				   int_4 ref_num_items_a,
				   char_1** tst_fnames_a,
				   float_4* tst_values_a,
				   int_4 tst_num_items_a) {

  if (debug_level_d > ISIP_DEBUG_BRIEF) {
    fprintf(stdout, "\ncalculating histogram:\n");
  }

  // calculate the range and midpoint of each bin
  //
  float_4 range = (max_a - min_a) / (num_bins_a - 1);

  bins_midpts_a[0] = min_a;
  bins_midpts_a[num_bins_a-1] = max_a;

  for (int_4 i=1; i<(num_bins_a-1); i++) {
    bins_midpts_a[i] = bins_midpts_a[i-1] + range;
  }

  // sort the reference filename in ascending order
  //
  int_4 ref_index[ref_num_items_a];
  
  if (qsort_cc(ref_index, ref_fnames_a, ref_num_items_a) == ISIP_FALSE) {
    error_handler_cc((char_1*)"qsort_cc",
		     (char_1*)"error occurred while sorting");
    return ISIP_FALSE;
  }
  
  // calculate the difference between reference and test value
  //
  float_4 diff[tst_num_items_a];

  // dummy variable
  //
  int_4 tmp=0;
  
  if (diff_cc(diff, tmp, ref_fnames_a, ref_values_a, ref_num_items_a,
	      ref_index, tst_fnames_a, tst_values_a, tst_num_items_a)
      == ISIP_FALSE) {
    error_handler_cc((char_1*)"qsort_cc",
		     (char_1*)"error occurred while calculating differences of scores");
    return ISIP_FALSE;
  }

  // determine what score goes to which bin
  //
  for (int_4 i=0; i<tst_num_items_a; i++) {

    // limit the data
    //
    float_4 num = diff[i];

    if (num < min_a) {
      num = min_a;
    }
    else if (num > max_a) {
      num = max_a;
    }

    // calculate which bin the data should go and increment the count
    //
    int_4 index = (int_4)rint((num-min_a)/(max_a-min_a) * num_bins_a);
    bins_freqs_a[index]++;
    
    if (debug_level_d > ISIP_DEBUG_BRIEF) {
      fprintf(stdout, IMGEV_DEBUG_FORMAT_K, IMAGE_EVAL_CLASS_NAME,
	      "histogram_cc", index, bins_midpts_a[index], num);
    }
  } 
  
  // exit gracefully
  //
  return ISIP_TRUE;
}





