// file: $USFS/class/image_analysis/v6.2/imaga_edge_16.cc
//

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

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

// method: write_cc
//
// arguments:
//  float_4* out_image: (input) the data of the image to be written
//  char_1* fname: (input) the output image file name
//  int_4 width: (input) width of the image
//  int_4 height: (input) height of the image
//
// return: a logical_1 value indicating status
//
// this method writes the edge detected data into a file
//
logical_1 Image_analysis::write_cc(float_4* out_image_a, char_1* fname_a,
					int_4 width_a, int_4 height_a) {
  
  // declare local variables
  //
  FILE* fp = (FILE*)NULL;
  
  // opening the output file
  //
  fp = fopen((char*)fname_a, "w");
  
  // checking the file pointer
  //
  if (fp == (FILE*)NULL) {
    fprintf(stdout, "error opening the output file");
  }
  
  // writing the header in the pgm file
  //
  fprintf(fp, "%s\n%d %d\n%ld\n", IMAGA_EDGE_MNUM, 1536,
	  1024, IMAGA_EDGE_COLMAX);
  
  for (int_4 i=0; i<height_a; i++) {
    for (int_4 j=0; j<width_a; j++) {
      
      // checking for the out of range data
      //
      if ((out_image_a[i * width_a + j] < (int_4)IMAGA_EDGE_COLMAX)
	  || (out_image_a[i * width_a + j] > (int_4)IMAGA_EDGE_COLMIN)) {
	if (out_image_a[i * width_a + j] < (int_4)IMAGA_EDGE_COLMIN) {
	  out_image_a[i * width_a + j] = IMAGA_EDGE_COLMIN;
	}
	else if (out_image_a[i * width_a + j] > (int_4)IMAGA_EDGE_COLMAX) {
	  out_image_a[i * width_a + j] = IMAGA_EDGE_COLMAX;
	}
      }
      
      // writing the data into a file
      //
      putc((uint_1)(out_image_a[i * width_a + j]), fp);
    }
  }

  // closing the file
  //
  fclose(fp);
  
  // exit gracefully
  //
  return ISIP_TRUE;
}
