// FilterAlias.cpp: implementation of the CFilterCamera class.
//
//////////////////////////////////////////////////////////////////////

/** \file FilterAlias.cpp
	\brief implementace třídy CFilterAlias
*/

#include "StdAfx.h"
#include "FilterAlias.h"

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

IMPLEMENT_DYNAMIC(CFilterAlias, CFilterBase)

CFilterAlias::CFilterAlias(float CameraFocus, float DistanceToCamera, const char* name)
	: CFilterBase(CameraFocus, DistanceToCamera, name)
{
	TRACE("-- Entering CFilterAlias constructor --\n");
	m_type = IDF_ALIAS;
}

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

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

	CStructures::iPoint currSize, nextSize;
	CStructures::Supersample supersample;

	currSize = CFilterBase::GetSize();
	supersample = CFilterBase::GetSupersample();

	nextSize.X = currSize.X * supersample.X;
	nextSize.Y = currSize.Y * supersample.Y;

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

	return nextSize;
}

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

	int x,y; //zacatek subpixelu
	int xMax, yMax;
	CStructures::iPoint out;
	CStructures::Supersample supersample;
	
	CStructures::iPoint size, overSize;
	CStructures::iPoint subPixIndex;
	CStructures::ARGBPixel pixelValue;
	CStructures::ARGBPixel sumPixel = {0,0,0,0};

	supersample = CFilterBase::GetSupersample();
	
	//rozliseni hrube mrizky
	size = CFilterBase::GetSize();

	out.X = - size.X/2 - 1;
	out.Y = - size.Y/2 - 1;

	//rozliseni jemne mrizky
	overSize.X = size.X * supersample.X;
	overSize.Y = size.Y * supersample.Y;

	//jemna mrizka
	xMax = (overSize.X/2);
	yMax = (overSize.Y/2);

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


	//pres celou jemnou mrizku
	for(x = -xMax; x <= xMax; x += supersample.X)
	{
 		out.X++;

		for(y = - yMax; y <= yMax; y+= supersample.Y)
		{
			out.Y++;

			//TRACE("Subpixel start at: <%d,%d>\n", x,y);
			CStructures::iPoint subPixel; //souradnice subpixelu v hrubem pixelu
			int stepX, stepY; //krok po subpixelech

			//plocha velkeho pixelu
			for(stepX = 0; stepX < supersample.X; stepX++)
				for(stepY = 0; stepY < supersample.Y; stepY++)
				{
					subPixel.X = x + stepX;
					subPixel.Y = y + stepY;
					//TRACE("\tsubpixels <%d,%d>\n", subPixel.X, subPixel.Y);

					pixelValue = pSource->GetRGBPixel(subPixel);
					sumPixel.R += pixelValue.R;
					sumPixel.G += pixelValue.G;
					sumPixel.B += pixelValue.B;
				}

			//prumerna hodnota vysledneho pixelu
			pixelValue.R = floor((sumPixel.R/(supersample.X * supersample.Y)) + 0.5);
			pixelValue.G = floor((sumPixel.G/(supersample.X * supersample.Y)) + 0.5);
			pixelValue.B = floor((sumPixel.B/(supersample.X * supersample.Y)) + 0.5);
			pixelValue.A = 255; 

			sumPixel.R = 0;
			sumPixel.G = 0;
			sumPixel.B = 0;

			//TRACE("Saving at <%d,%d>\n", out.X, out.Y);

			pTarget->SetRGBPixel(out, pixelValue);
		}

		out.Y = - size.Y/2 - 1;
	}
}


//dumping objektu
#ifdef _DEBUG
void CFilterAlias::Dump(CDumpContext& dc) const
{
	CFilterBase::Dump(dc);
	
}
#endif // _DEBUG

