// file: $USFS/class/image_analysis/v6.1/imaga_sd_04.cc
//

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

// method: sd_mean_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 finds the mean of the total image or the prescribed region
//
logical_1 Image_analysis::sd_mean_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 npixels = width_a * height_a;
  
  // declare local variables
  //
  float_8 sum_r= 0.0;
  float_8 sum_g = 0.0;
  float_8 sum_b = 0.0;
  
  // loop over all the data to sum the pixel values
  // 
  for (int_4 i=0; i<npixels; i++) {

    sum_r += b_red_a[i];
    sum_g += b_green_a[i];
    sum_b += b_blue_a[i];

    // increment the pixels
    //
    sd_r_npxls_d++;
  }
  
  // compute the mean of each of the color
  //
  sd_r_mean_red_d = sum_r / sd_r_npxls_d;
  sd_r_mean_grn_d = sum_g / sd_r_npxls_d;
  sd_r_mean_blu_d = sum_b / sd_r_npxls_d;

  // provide some debugging information
  //
  if (debug_level_d > ISIP_DEBUG_BRIEF) {
    fprintf(stdout, "<%s>\n", "Image_analysis::sd_mean_cc");
    
    fprintf(stdout, "%s\n", "the mean values of each of the colors are:");
    fprintf(stdout, "%f\t%f\t%f\n", sd_r_mean_red_d, sd_r_mean_grn_d, 
	    sd_r_mean_blu_d);
  }

  // exit gracefully
  //
  return ISIP_TRUE;
}
 
