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

// system include files
//
#include <string.h>

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

#include "image_analysis_constants.h"

// method: sd_compute_cc
//
// arguments:
//  char_1* image_fname: (input) the image filename
//
// return: a logical_1 value indicating status
//
// this method computes a histogram on a PPM image file.
//
logical_1 Image_analysis::sd_compute_cc(char_1* image_fname_a) {

  // declare a ppm object
  //
  Ppm ppm;

  // open the ppm file
  //
  if (ppm.open_cc(image_fname_a, ISIP_READ_ONLY) == ISIP_FALSE) {
    error_handler_cc((char_1*)"hist_compute_cc",
		     (char_1*)"error opening the image file");
    return ISIP_FALSE;
  }

  // read the dimensions of the image
  //
  int_4 width = ppm.get_width_cc();
  int_4 height = ppm.get_height_cc();

  // declaring the buffers for reading the ppm image
  //
  uint_1 b_red[width * height];           
  uint_1 b_green[width * height];         
  uint_1 b_blue[width * height];
  
  // read the ppm file 
  //
  if (ppm.read_cc(b_red, b_green, b_blue, IMAGA_EDGE_WOFF, IMAGA_EDGE_HOFF,
		  width, height) == ISIP_FALSE) {
    error_handler_cc((char_1*)"hist_compute_cc",
		     (char_1*)"error while reading the file");
    return ISIP_FALSE;
  }

  // compute the mean of each bins
  //
  if (sd_mean_cc(b_red, b_green, b_blue, width, height) == ISIP_FALSE) {
    error_handler_cc((char_1*)"sd_compute_cc",
		     (char_1*)"error calculating the mean value");
    return ISIP_FALSE;
  }

  // compute the standard deviation
  //
  if (sd_assign_cc(b_red, b_green, b_blue, width, height) == ISIP_FALSE) {
    error_handler_cc((char_1*)"sd_compute_cc",
		     (char_1*)"error computing the standard deviation");
    return ISIP_FALSE;
  }
  
  // close the ppm object
  //
  if (ppm.close_cc() == ISIP_FALSE) {
    error_handler_cc((char_1*)"ent_compute_cc",
		     (char_1*)"error closing the file");
    return ISIP_FALSE;
  }
  
  // increment the counters
  //
  sd_r_nimages_d += (int_4)1;

  // exit gracefully
  //
  return ISIP_TRUE;
}
