// FilterLightness.cpp: implementation of the CFilterLightness class.
//
//////////////////////////////////////////////////////////////////////

/** \file FilterLightness.cpp
	\brief implementace třídy CFilterLightness
*/

#include "stdafx.h"
#include "FilterLightness.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
//#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CFilterLightness::CFilterLightness(float CameraFocus, float DistanceToCamera, const char* name, double param)
	: CFilterBase(CameraFocus, DistanceToCamera, name)
{
	TRACE("-- Entering CFilterLightness constructor --\n");

	m_paramLightness = param;
	m_type = 2;
}

CFilterLightness::~CFilterLightness()
{
	TRACE("-- Entering CFilterLightness destructor --\n");
	
	#ifdef _DEBUG
		Dump(afxDump);
	#endif // _DEBUG
	
}


CStructures::iPoint CFilterLightness::GetNextSize() const
{
	TRACE("-- Entering CFilterLightness::GetNextSize --\n");
	
	CStructures::iPoint nextSize;

	nextSize = CFilterBase::GetSize();
	//nextSize.X = m_Width;
	//nextSize.Y = m_Height;

	TRACE("Rozliseni nasledujiciho filtru: <%d x %d>\n", nextSize.X, nextSize.Y);

	return nextSize;
}



void CFilterLightness::Transform(CPixelArray* pSource, CPixelArray* pTarget)
{
	TRACE("Entering CFilterLightness::Transform\n");

	CStructures::iPoint point, size;
	int x, y;
	int xMax, yMax;
	//float value, newValue;
	CStructures::ARGBPixel pixelValue;

	size = CFilterBase::GetSize();

	xMax = (size.X/2)-1;
	yMax = (size.Y/2)-1;

	long total = (size.X * size.Y)/100;
	long currPix = 0;

	TRACE("Processing CFilterLightness... <%d x %d>\r\n",size.X, size.Y);	
	printf(" <%d x %d> \n",size.X, size.Y);	

	/*Ziskani normalizacniho koeficientu*/
	CStructures::iPoint maxPoint;
	maxPoint.X = size.X/2;
	maxPoint.Y = size.Y/2;

	float rNorm = GetDistanceToCenter(maxPoint); //Maximalni vzdalenost od stredu pro normalizaci

	for(x = -xMax; x < xMax; x++)
		for(y = -yMax; y < yMax; y++)
		{
			point.X = x;
			point.Y = y;

			float distance = GetDistanceToCenter(point);					//Zjisteni vzdalenosti pozadovaneho pixelu od stredu
			//value = pSource->GetGrayPixel(point);							//Ziskani jasove hodnoty pixelu ve zdrojovem poli
			pixelValue = pSource->GetRGBPixel(point);

			float pom;

			//pom = cos(1);
			pom = cos(fabs(distance/rNorm));
			//newValue = value*(m_paramLightness*pom);	//prepocitame jas 
			pixelValue.R = pixelValue.R*(m_paramLightness*pom);
			pixelValue.G = pixelValue.G*(m_paramLightness*pom);
			pixelValue.B = pixelValue.B*(m_paramLightness*pom);

			//pTarget->SetGrayPixel(point, newValue);							//Ulozime do ciloveho pole
			pTarget->SetRGBPixel(point, pixelValue);

			currPix++;
			if((currPix % total) == 0) printf("\r%d%% done", currPix/total);
		}
		
		printf("\n");

	
	//#ifdef _OPENCV
	//	pTarget->Save("Output\\3Lightness.jpg");
	//	pTarget->Display("Lightness");
	//#endif //_OPENCV

}


void CFilterLightness::Show()
{
	CFilterBase::Show();
	cout << "Lightness parameter: " << m_paramLightness << "\n";
}

//dumping objektu
#ifdef _DEBUG
void CFilterLightness::Dump(CDumpContext& dc) const
{
	CFilterBase::Dump(dc);
	dc << "Lightness parameter: " << m_paramLightness << "\n";
	
}
#endif // _DEBUG