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

// isip include files
//
#include "image_analysis.h"
#include "image_analysis_constants.h"

// method: sd_assign_cc
//
// arguments:
//  uint_1* b_red: (input) red buffer
//  uint_1* b_green: (input) green buffer
//  uint_1* b_blue: (input) blue buffer
//  int_4 width: (input) width of the image
//  int_4 height: (input) height of the image
//
// return:
//  a logical_1 value indicating the status
//
// this method assigns data points to bins in the histogram.
//
logical_1 Image_analysis::sd_assign_cc(uint_1* b_red_a, uint_1* b_green_a,
				       uint_1* b_blue_a, int_4 width_a,
				       int_4 height_a) {

  // total number of pixels in the image
  //
  int_4 n_pixels = width_a * height_a;

  // declare local variables
  //
  float_8 diff_r = 0.0;
  float_8 diff_g = 0.0;
  float_8 diff_b = 0.0;
  float_8 sum_r = 0.0;
  float_8 sum_g = 0.0;
  float_8 sum_b = 0.0;
  
  // loop over all the pixels
  //
  for (int_4 i = 0; i < n_pixels; i++) {

    // find the diff of the pixel value from the mean value
    //
    diff_r += b_red_a[i] - sd_r_mean_red_d;
    diff_g += b_green_a[i] - sd_r_mean_grn_d;
    diff_b += b_blue_a[i] - sd_r_mean_blu_d;
    
    // compute the square of the diff for the standard deviation
    //
    sum_r += diff_r * diff_r;
    sum_g += diff_g * diff_g;
    sum_b += diff_b * diff_b; 
  }
  
  // compute the scaling factor
  //
  float_8 scale = 1 / (float_8)(sd_r_npxls_d - 1);

  // compute the standard deviation
  //
  sd_t_red_d = sqrt (sum_r * scale);
  sd_t_grn_d = sqrt (sum_g * scale);
  sd_t_blu_d = sqrt (sum_b * scale);
  
  // exit gracefully
  //
  return ISIP_TRUE;
}
