#include "bresline.h"
#include "util.h"
#include <math.h>
#include <im.h>
#include <im_capture.h>
#include <im_format_avi.h>
#include <im_image.h>
#include <im_util.h>

//void desenhaEpipolar( int x1, int y1, int x2, int y2, char *img_entrada, char *img_saida , int faixa )
int encontraRelacaoEpipolar( int x1, int y1, int x2, int y2, imImage *img, int faixa , int *x_proj, int *y_proj )
{
	int dX = abs(x2-x1);	
	int dY = abs(y2-y1);
	
	int CurrentX = x1;		
	int CurrentY = y1;
	
	int Xincr, Yincr;

	unsigned char* R;
	unsigned char* G;
	unsigned char* B;

	int w,h;

	R = (imbyte*)img->data[0];
	G = (imbyte*)img->data[1];
	B = (imbyte*)img->data[2];   

	w = img->width;
	h = img->height;

	// Determina qual a direção para incrementar x e y
	if (x1 > x2)
	{
		Xincr=-1;
	}
	else
	{
		Xincr=1;
	}
	
	if (y1 > y2)
	{
		Yincr=-1;
	}
	else
	{
		Yincr=1;
	}	
	
	//------------------------------------------------------------------------
	// DETERMINE INDEPENDENT VARIABLE (ONE THAT ALWAYS INCREMENTS Y2 1 (OR -1) )
	// AND INITIATE APPROPRIATE LINE DRAWING ROUTINE (BASED ON FIRST OCTANT
	// ALWAYS). THE X AND Y'S MAY BE FLIPPED IF Y IS THE INDEPENDENT VARIABLE.
	//------------------------------------------------------------------------
	if (dX >= dY)	// if X is the independent variable
	{           
		int dPr 	= dY<<1;   						// amount to increment decision if right is chosen (alwy1s)
		int dPru 	= dPr - (dX<<1);				// amount to increment decision if up is chosen
		int P 		= dPr - dX;						// decision variable start value

		for (; dX>=0; dX--)							// process each point in the line one at a time (just use dX)
		{
			if ( B[(CurrentY*w)+CurrentX] == faixa )
			{
				*x_proj = CurrentX;
				*y_proj = CurrentY;

				return 1;
			}
			//cdPixel(CurrentX, CurrentY, cdEncodeColor(R , G , B ) );	// plot the pixel
			if (P > 0)                              // is the pixel going right AND up?
			{ 
				CurrentX+=Xincr;					// increment independent variable
				CurrentY+=Yincr; 					// increment dependent variable
				P+=dPru;							// increment decision (for up)
			}
			else									// is the pixel just going right?
			{
				CurrentX+=Xincr;					// increment independent variable
				P+=dPr;								// increment decision (for right)
			}
		}		
	}
	else			// if Y is the independent variable
	{
		int dPr 	= dX<<1;   						// amount to increment decision if right is chosen (alwy1s)
		int dPru 	= dPr - (dY<<1);    				// amount to increment decision if up is chosen
		int P 		= dPr - dY;						// decision variable start value

		for (; dY>=0; dY--)							// process each point in the line one at a time (just use dY)
		{
			if ( B[(CurrentY*w)+CurrentX] == faixa )
			{
				*x_proj = CurrentX;
				*y_proj = CurrentY;

				return 1;
			}
			//cdPixel(CurrentX, CurrentY, cdEncodeColor(R , G , B ) );	// plot the pixel
			if (P > 0)                              // is the pixel going up AND right?
			{ 
				CurrentX+=Xincr; 					// increment dependent variable
				CurrentY+=Yincr;					// increment independent variable
				P+=dPru;							// increment decision (for up)
			}
			else									// is the pixel just going up?
			{
				CurrentY+=Yincr;					// increment independent variable
				P+=dPr;								// increment decision (for right)
			}
		}		
	}		
	
	return 0;
}


void BresLine( int x1, int y1, int x2, int y2, int faixa , imbyte *R , imbyte *G , imbyte *B )
{
	int dX = abs(x2-x1);	
	int dY = abs(y2-y1);
	
	int CurrentX = x1;		
	int CurrentY = y1;
	
	int Xincr, Yincr;

	// Determina qual a direção para incrementar x e y
	if (x1 > x2)
	{
		Xincr=-1;
	}
	else
	{
		Xincr=1;
	}
	
	if (y1 > y2)
	{
		Yincr=-1;
	}
	else
	{
		Yincr=1;
	}	
	
	//------------------------------------------------------------------------
	// DETERMINE INDEPENDENT VARIABLE (ONE THAT ALWAYS INCREMENTS Y2 1 (OR -1) )
	// AND INITIATE APPROPRIATE LINE DRAWING ROUTINE (BASED ON FIRST OCTANT
	// ALWAYS). THE X AND Y'S MAY BE FLIPPED IF Y IS THE INDEPENDENT VARIABLE.
	//------------------------------------------------------------------------
	if (dX >= dY)	// if X is the independent variable
	{           
		int dPr 	= dY<<1;   						// amount to increment decision if right is chosen (alwy1s)
		int dPru 	= dPr - (dX<<1);				// amount to increment decision if up is chosen
		int P 		= dPr - dX;						// decision variable start value

		for (; dX>=0; dX--)							// process each point in the line one at a time (just use dX)
		{
			if ( B[CurrentY*3072+CurrentX] == faixa )
			{
				R[CurrentY*3072+CurrentX] = 0 ;
				G[CurrentY*3072+CurrentX] = 255 ;
				B[CurrentY*3072+CurrentX] = 0 ;
			}
			else
			{
				R[CurrentY*3072+CurrentX] = 255 ;
				G[CurrentY*3072+CurrentX] = 255 ;
				B[CurrentY*3072+CurrentX] = 255 ;
			}
			//cdPixel(CurrentX, CurrentY, cdEncodeColor(R , G , B ) );	// plot the pixel
			if (P > 0)                              // is the pixel going right AND up?
			{ 
				CurrentX+=Xincr;					// increment independent variable
				CurrentY+=Yincr; 					// increment dependent variable
				P+=dPru;							// increment decision (for up)
			}
			else									// is the pixel just going right?
			{
				CurrentX+=Xincr;					// increment independent variable
				P+=dPr;								// increment decision (for right)
			}
		}		
	}
	else			// if Y is the independent variable
	{
		int dPr 	= dX<<1;   						// amount to increment decision if right is chosen (alwy1s)
		int dPru 	= dPr - (dY<<1);    				// amount to increment decision if up is chosen
		int P 		= dPr - dY;						// decision variable start value

		for (; dY>=0; dY--)							// process each point in the line one at a time (just use dY)
		{
			//cdPixel(CurrentX, CurrentY, cdEncodeColor(R , G , B ) );	// plot the pixel
			if (P > 0)                              // is the pixel going up AND right?
			{ 
				CurrentX+=Xincr; 					// increment dependent variable
				CurrentY+=Yincr;					// increment independent variable
				P+=dPru;							// increment decision (for up)
			}
			else									// is the pixel just going up?
			{
				CurrentY+=Yincr;					// increment independent variable
				P+=dPr;								// increment decision (for right)
			}
		}		
	}

	imSaveRGB(3072, 2048, IM_RGB, R , G , B, "out\\linha_epipolar.bmp" );
}