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

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

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

// method: dent_grn_assign_cc
//
// arguments:
//  uint_1* b_green: (input) red buffer
//  int_4 width: (input) width of the image
//  int_4 height: (input) height_a of the image
//
// return: a logical_1 value indicating the status
//
// this method assigns data points to bins in the histogram.
// note that the histogram is assumed to extend from:
//
logical_1 Image_analysis::dent_grn_assign_cc(uint_1* b_green, int_4 width_a,
					    int_4 height_a) {

  // declare local variables
  //
  int_4 index;
  
  // loop over all the image
  //
  for (int_4 i=0; i < height_a; i++) {
    for (int_4 j=0; j < width_a; j++) {
      
      // for the first pixel
      //
      if ((i == 0) && (j == 0)) {
	
	  // find the difference
	  //
	  index = abs((int)(b_green[i*width_a + (j+1)] - b_green[i*width_a+j]));
	  dent_t_grn_d[index]++;
	  index = abs((int)(b_green[(i+1)*width_a + j] - b_green[i*width_a+j]));
	  dent_t_grn_d[index]++;
	  index = abs((int)(b_green[(i+1)*width_a + (j+1)] - b_green[i*width_a+j]));
	  dent_t_grn_d[index]++;
	}

      // for the first line
      //
      else if ((i == 0) && ((j > 0) && (j < (width_a - 1)))) {
	
	  // find the diff of the adjacent five pixels
	//
	  index = abs((int)(b_green[i*width_a + (j-1)] - b_green[i*width_a+j]));
	  dent_t_grn_d[index]++;
	  index = abs((int)(b_green[i*width_a + (j+1)] - b_green[i*width_a+j]));
	  dent_t_grn_d[index]++;
	  index = abs((int)(b_green[(i+1)*width_a + (j-1)] - b_green[i*width_a+j]));
	  dent_t_grn_d[index]++;
	  index = abs((int)(b_green[(i+1)*width_a + j] - b_green[i*width_a+j]));
	  dent_t_grn_d[index]++;
	  index = abs((int)(b_green[(i+1)*width_a + (j+1)] - b_green[i*width_a+j]));
	  dent_t_grn_d[index]++;
	}

      // last pixel in the first row
      //
      else if ((i == 0) && (j == (width_a - 1))) {
	
	index = abs((int)(b_green[i*width_a + (j-1)] - b_green[i*width_a+j]));
	dent_t_grn_d[index]++;
	index = abs((int)(b_green[(i+1)*width_a + (j-1)] - b_green[i*width_a+j]));
	dent_t_grn_d[index]++;
	index = abs((int)(b_green[(i+1)*width_a + j] - b_green[i*width_a+j]));
	dent_t_grn_d[index]++;
      }

      // for the pixels in the first column
      //
      else if ((j == 0) && ((i > 0) && ( i < (height_a - 1)))) {
	
	// find the diff of the adjacent five pixels
	//
	index = abs((int)(b_green[(i-1)*width_a + j] - b_green[i*width_a+j]));
	dent_t_grn_d[index]++;
	index = abs((int)(b_green[(i-1)*width_a + (j+1)] - b_green[i*width_a+j]));
	dent_t_grn_d[index]++;
	index = abs((int)(b_green[i*width_a + (j+1)] - b_green[i*width_a+j]));
	dent_t_grn_d[index]++;
	index = abs((int)(b_green[(i+1)*width_a + j] - b_green[i*width_a+j]));
	dent_t_grn_d[index]++;
	index = abs((int)(b_green[(i+1)*width_a + (j+1)] - b_green[i*width_a+j]));
	dent_t_grn_d[index]++;
      }

      // for the last pixel in the first column
      //
      else if ((j == 0) && (i == (height_a - 1))) {
	
	// find the diff of the adjacent five pixels
	//
	index = abs((int)(b_green[(i-1)*width_a + j] - b_green[i*width_a+j]));
	dent_t_grn_d[index]++;
	index = abs((int)(b_green[(i-1)*width_a + (j+1)] - b_green[i*width_a+j]));
	dent_t_grn_d[index]++;
	index = abs((int)(b_green[i*width_a + (j+1)] - b_green[i*width_a+j]));
	dent_t_grn_d[index]++;
      }

      // for the last pixel
      //
      else if ((i == (height_a - 1)) && (j == (width_a - 1))) {
	
	index = abs((int)(b_green[(i-1)*width_a + (j-1)] - b_green[i*width_a+j]));
	dent_t_grn_d[index]++;
	index = abs((int)(b_green[(i-1)*width_a + j] - b_green[i*width_a+j]));
	dent_t_grn_d[index]++;
	index = abs((int)(b_green[i*width_a + (j-1)] - b_green[i*width_a+j]));
	dent_t_grn_d[index]++;
      }

      // for the last column pixels
      //
      else if ((j == (width_a - 1)) && ((i > 0) && (i < (height_a - 1)))) {

	index = abs((int)(b_green[(i-1)*width_a + (j-1)] - b_green[i*width_a+j]));
	dent_t_grn_d[index]++;
	index = abs((int)(b_green[(i-1)*width_a + j] - b_green[i*width_a+j]));
	dent_t_grn_d[index]++;
	index = abs((int)(b_green[i*width_a + (j-1)] - b_green[i*width_a+j]));
	dent_t_grn_d[index]++;
	index = abs((int)(b_green[(i+1)*width_a + (j-1)] - b_green[i*width_a+j]));
	dent_t_grn_d[index]++;
	index = abs((int)(b_green[(i+1)*width_a + j] - b_green[i*width_a+j]));
	dent_t_grn_d[index]++;
      }

      // for the last row pixels
      //
      else if ((i == (height_a - 1)) && ((j > 0) && (j < (width_a - 1)))) {
	
	index = abs((int)(b_green[(i-1)*width_a + (j-1)] - b_green[i*width_a+j]));
	dent_t_grn_d[index]++;
	index = abs((int)(b_green[(i-1)*width_a + j] - b_green[i*width_a+j]));
	dent_t_grn_d[index]++;
	index = abs((int)(b_green[(i-1)*width_a + (j+1)] - b_green[i*width_a+j]));
	dent_t_grn_d[index]++;
	index = abs((int)(b_green[i*width_a + (j-1)] - b_green[i*width_a+j]));
	dent_t_grn_d[index]++;
	index = abs((int)(b_green[i*width_a + (j+1)] - b_green[i*width_a+j]));
	dent_t_grn_d[index]++;
      }

      else {

	index = abs((int)(b_green[(i-1)*width_a + (j-1)] - b_green[i*width_a+j]));
	dent_t_grn_d[index]++;
	index = abs((int)(b_green[(i-1)*width_a + j] - b_green[i*width_a+j]));
	dent_t_grn_d[index]++;
	index = abs((int)(b_green[(i-1)*width_a + (j+1)] - b_green[i*width_a+j]));
	dent_t_grn_d[index]++;
	index = abs((int)(b_green[i*width_a + (j-1)] - b_green[i*width_a+j]));
	dent_t_grn_d[index]++;
	index = abs((int)(b_green[i*width_a + (j+1)] - b_green[i*width_a+j]));
	dent_t_grn_d[index]++;
	index = abs((int)(b_green[(i+1)*width_a + (j-1)] - b_green[i*width_a+j]));
	dent_t_grn_d[index]++;
	index = abs((int)(b_green[(i+1)*width_a + j] - b_green[i*width_a+j]));
	dent_t_grn_d[index]++;
	index = abs((int)(b_green[(i+1)*width_a + (j+1)] - b_green[i*width_a+j]));
	dent_t_grn_d[index]++;
      }
    }
  }

  // exit gracefully
  //
  return ISIP_TRUE;
}
