// Executes all of the canny stages
void ImageProcessor::Canny() {
  Gaussian();
  Sobel();
  NonMaxSuppression();
  HysteresisThresholding();
}
int main( int argc, char* argv[] )
{
  char nameImageIn[256];
  char nameImageOut[256];
  int nbNames = 0;
  int status, i;

  void *bufferIn = (void*)NULL;
  void *bufferOut = (void*)NULL;
  int bufferDims[3] = {0,0,0};
  int nbytes;
  bufferType TYPE = UCHAR;
  double low = 1.0;
  double high = 1.0;
  int size = 1;

  strcpy( program, argv[0] );

  if ( argc == 1 ) ErrorMessage( "\n", 1 );

  for ( i=1; i<argc; i++ ) {
    if ( argv[i][0] == '-' ) {

      if ( (strcmp ( argv[i], "-help" ) == 0) || 
	   (strcmp ( argv[i], "-h" ) == 0) ) {
	ErrorMessage( "help message\n", 1 );
      }
      
      else if ( (strcmp ( argv[i], "-verbose" ) == 0) || 
		(strcmp ( argv[i], "-v" ) == 0) ) {
	Connexe_verbose();
      }

      else if ( (strcmp ( argv[i], "-no-verbose" ) == 0) || 
		(strcmp ( argv[i], "-nv" ) == 0) ) {
	Connexe_noverbose();
      }

      else if ( (strcmp ( argv[i], "-low-threshold" ) == 0) || 
		(strcmp ( argv[i], "-lt" ) == 0) ) {
	i += 1;
	if ( i >= argc)    ErrorMessage( "parsing -low-threshold...\n", 0 );
	status = sscanf( argv[i], "%lf", &low );
	if ( status <= 0 ) ErrorMessage( "parsing -low-threshold...\n", 0 );
      }

      else if ( (strcmp ( argv[i], "-high-threshold" ) == 0) || 
		(strcmp ( argv[i], "-ht" ) == 0) ) {
	i += 1;
	if ( i >= argc)    ErrorMessage( "parsing -high-threshold...\n", 0 );
	status = sscanf( argv[i],"%lf", &high );
	if ( status <= 0 ) ErrorMessage( "parsing -high-threshold...\n", 0 );
      }

      else if ( (strcmp ( argv[i], "-minimal-size" ) == 0) || 
		(strcmp ( argv[i], "-ms" ) == 0) ) {
	i += 1;
	if ( i >= argc)    ErrorMessage( "parsing -minimal-size...\n", 0 );
	status = sscanf( argv[i],"%d", &size );
	if ( status <= 0 ) ErrorMessage( "parsing -minimal-size...\n", 0 );
      }

      else {
	sprintf( nameImageIn, "unknown option %s\n", argv[i] );
	ErrorMessage( nameImageIn, 0);
      }
    }

    else if ( argv[i][0] != 0 ) {
      if ( nbNames == 0 ) {
	strcpy( nameImageIn, argv[i] );
      } 
      else if ( nbNames == 1 ) {
	strcpy( nameImageOut, argv[i] );
      } 
      else {
	sprintf( nameImageIn, "too many image name (%s)\n", argv[i] );
	ErrorMessage( nameImageIn, 0);
      }
      nbNames ++;
    }
  }

  
  bufferIn = _readPnmImage( nameImageIn, &bufferDims[0], &bufferDims[1], &bufferDims[2], &nbytes );
  if ( nbytes == 2 ) TYPE = USHORT;

  bufferOut = (void*)malloc( bufferDims[0] * bufferDims[1] * bufferDims[2] * sizeof(unsigned char) );


  fprintf( stderr, "%s: processing with thresholds = %f %f\n", argv[0], low, high );
  /*
   * to change the connectivity -> Connexe_SetConnectivity
   * to change the minimal size of the connected components to be kept.
   *                            -> Connexe_SetMinimumSizeOfComponents
   * to change the maximum number of the connected components to be kept.
   *                            -> Connexe_SetMaximumNumberOfComponents
   */
  Connexe_SetMinimumSizeOfComponents( size );
  if ( HysteresisThresholding( bufferIn, TYPE,
			       bufferOut, UCHAR,
			       bufferDims,
			       low, high ) < 0 ) {
    fprintf( stderr, " processing failed.\n" );
    exit( 1 );
  }

  /* there exists a more complete function (with more parameters)
     => HysteresisThresholdingWithAllParams()
     see connexe.h
  */


  _writePnmImage( nameImageOut, bufferDims[0], bufferDims[1], bufferDims[2], 1, bufferOut );
  
}