/**
 * \file main_optimum.c
 * \brief This file is used to profile the clusterflow under CodeAnanlyst
 *
 * \date Jun 10, 2009
 * \author Alexander Pimenov
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "global.h"
#include "g12buffer.h"
#include "integral.h"
#include "loadimage.h"
#include "flow.h"
#include "viflow.h"
#include "signatures.h"
#include "signatures_sse.h"

int epsilonLow = 1;
int epsilonHigh = 10;
int sdevLow = 2;
int sdevHigh = 100;

int zonesize = 4;
int local = 2;
int maxdisp = 5;
int maxareadisp = 5;

int cutout = 7;

uint64_t timeval_subtract (struct timeval * x, struct timeval * y)
{
  /* Perform the carry for the later subtraction by updating y. */
  if (x->tv_usec < y->tv_usec) {
    uint64_t nsec = (y->tv_usec - x->tv_usec) / 1000000 + 1;
    y->tv_usec -= 1000000 * nsec;
    y->tv_sec += nsec;
  }
  if (x->tv_usec - y->tv_usec > 1000000) {
    uint64_t nsec = (y->tv_usec - x->tv_usec) / 1000000;
    y->tv_usec += 1000000 * nsec;
    y->tv_sec -= nsec;
  }
  /* Return 1 if result is negative. */
  return ((uint64_t)x->tv_sec - (uint64_t)y->tv_sec) * 1000000 + (uint64_t)x->tv_usec - (uint64_t)y->tv_usec;
}

int main (int argC, char **ArgV)
{

	// Load two images
	if (argC <=1 || argC >3)
	{
		printf("Wrong usage. Should be called as:\n"
			   "  simulate_pc <first file> <second file>\n");
		return EXIT_FAILURE;
	}

	G12Buffer *I1 = g12BufferCreateFromFile(ArgV[1]);
	if (I1 == NULL)
	{
		printf("Could not open the input file %s", ArgV[1]);
		return EXIT_FAILURE;
	}
	G12Buffer *I2 = g12BufferCreateFromFile(ArgV[2]);
	if (I2 == NULL)
	{
		printf("Could not open the input file %s", ArgV[2]);
		return EXIT_FAILURE;
	}
	printf("Images Loaded\n");
	fflush(stdout);

	struct timeval t1;
	struct timeval st1;
    gettimeofday(&t1, NULL);

    int density = 0;
    int counter;

	FlowBuffer *flow = NULL;

	integralBuffer *II1 = integralBufferCreateFromG12(I1);
	integralBuffer *II2 = integralBufferCreateFromG12(I2);

    for (counter = 0; counter < 100; counter++)
    {

		//signaturesBuffer *S1 = signaturesBufferCreateFromIntegralx86fast(II1, epsilonLow, epsilonHigh, sdevLow, sdevHigh);
		//signaturesBuffer *S2 = signaturesBufferCreateFromIntegralx86fast(II2, epsilonLow, epsilonHigh, sdevLow, sdevHigh);

		//signaturesBuffer *S1 = signaturesBufferCreateFromIntegralx86fastIP(II1, epsilonLow, epsilonHigh, sdevLow, sdevHigh);
		//signaturesBuffer *S2 = signaturesBufferCreateFromIntegralx86fastIP(II2, epsilonLow, epsilonHigh, sdevLow, sdevHigh);

		signaturesBuffer *S1 = signaturesBufferCreateFromIntegralx86fastSSE(II1, epsilonLow, epsilonHigh, sdevLow, sdevHigh);
		signaturesBuffer *S2 = signaturesBufferCreateFromIntegralx86fastSSE(II2, epsilonLow, epsilonHigh, sdevLow, sdevHigh);


/*		FlowBuffer *flow1 = calculateTheFlowSimple(S1, S2, cutout);

		if (flow != NULL)
			flowBufferDelete(flow);
		flow = filterFlowSimple(flow1, zonesize, maxdisp, local);

		integralBufferDelete(II1);
		integralBufferDelete(II2);*/
		signaturesBufferDelete(S1);
		signaturesBufferDelete(S2);
		//flowBufferDelete(flow1);
		//density = flowCountDensity(flow);
    }
    //density = flowCountDensity(flow);
	printf("Density: %d it is %3.2lf %% of %d\n", density, (((double)density)  * 100.0) / (I1->h * I1->w), (I1->h * I1->w) );

    gettimeofday(&st1, NULL);
	printf("Signature calculation finished in %lu us\n ", timeval_subtract(&st1, &t1));

	return 0;
}


