/*
 * main.c
 *
 * Calls the function 'processImage()' 
 * on any one of the testcases defined in 'testdata.c'.
 *
 *
 * BUILD:
 *    gcc -Wall -ansi -DDEBUG \
 *    	lineacc.c      \
 *    	lmap.c         \
 *    	debug.c        \
 *    	processimage.c \
 *    	testdata.c     \
 *    	main.c         \
 *    	-o countcomponents
 *
 *
 */

/****************************************************************************
 * DEPENDENCIES:
 *
 *   +------------------------------------------------------------+
 *   |                  main.c = main() function                  |
 *   +-------------------------------------------+----------------+
 *   |         processimage.c, .h                | testdata.c, .h |
 *   +---------------+--------------+------------+                |
 *   | lineacc.c,.h  |  lmap.c,.h   | debug.c,.h |                |
 *   +---------------+--------------+------------+----------------+
 *   |              image.h, comp.h, label.h                      |
 *   +------------------------------------------------------------+
 *
 *  A block uses the blocks vertically below it.
 *
 *  processimage.c  ---  This file, contains the algorithm.
 *
 *  lineacc.c, .h   }--  Auxiliary datastructures used by processimage.c
 *  lmap.c, .h      }    for holding temporary data
 *
 *  debug.c, .h     ---  Optional debug output to stdout
 *
 *  testdata.c, .h  ---  Testcases, used by main.c
 *
 *  image.h         }--  Basic datastructures and typedefs used "everywhere"
 *  comp.h          }
 *  label.h         }
 *
 */



#include "image.h"         /* image_t */
#include "comp.h"          /* comp_t  */

#include "label.h"         /* label_t */
#include "lineacc.h"       /* lineAcc_t  */
#include "lmap.h"          /* lm_t  */

#include "processimage.h"  /* processImage()  */

#include "testdata.h"      /* G_aTests[] = test images  */

#include 
#include      




/* 
 * Our usrOutComp() function, called by processImage() for each 
 * completed a connected component it finds.
 *
 * This implementation simply prints the component data to stdout.
 */
void my_usrOutComp( comp_t const * pC )
	{
	printf( "OutComp: " );
	printf( "n=%d ", pC->pixels );
	printf( "bary=(%g,%g) ", pC->bary_x, pC->bary_y );
	printf( "bbox=(%d,%d)..(%d,%d) ", 
		pC->min_x, pC->min_y, 
		pC->max_x, pC->max_y );
	printf( "\n" );
	}




int main( int argc, char ** argv )
	{
	/* Selection of one of the test cases by cmd-line argument.
	 */
	int arg_nTest = 0;
	if ( argc > 1 )
		{
		if ( sscanf( argv[1], "%d", &arg_nTest ) != 1 )
			{ fprintf( stderr, "Usage: progname [testcaseNr]\n" ); return -1; }
		}

	image_t const * pImg1 = G_aTests[0];
	if ( (1 <= arg_nTest) && (arg_nTest <= G_nTests) )
		{
		pImg1 = G_aTests[ arg_nTest - 1 ];
		}



	/* 
	 * Allocate the temporary datastructures LineAccumulator and LabelMap, 
	 * both to a size equal to the width of the image in pixels.
	 */
 	lineAcc_t lineAcc;
	int laOK = lineAcc_alloc( &lineAcc, pImg1->size_x );
	assert( laOK ); /*crude handling, for testing only*/

	lm_t labelMap;
	int lmOK = lm_alloc( &labelMap, pImg1->size_x );
	assert( lmOK ); /*crude handling, for testing only*/



	/* 
 	 * Process the image to find the pixel counts, centers-of-mass,
	 * and bounding boxes of all four-connected components.
 	 */
	processImage(
		pImg1, 
		&lineAcc, 
		&labelMap, 
		my_usrOutComp );



	/* Clean up temporary datastructures.  
	 */
	lm_free( &labelMap );
	lineAcc_free( &lineAcc );
	
	return 0;
	}