/*

Na zacatku tohoto souboru je nastaveni chovani bloku. Pote jsou zde funkce
nezvytne k vytvoreni instance COMu, a funkce, ktera vraci ukazatele na
rozhrani bloku. Prozatim se predpokladaji pouze 2 pridavna rozhrani k tomuto
objektu. 

-- IBlockConfig --
je urcene ke konfiguraci bloku. Je pouzito
v pripade, kdyz uzivatel klikne pravym tlacitkem mysi na blok a vybere
volbu "properties...". Melo by se otevrit okno s nastavenim, ktere musi
byt implementovano

-- IBlockAutoTrigger --
slouzi k automatickemu spousteni procesu. Vyuzije se napriklad pri realtime
zpracovani zvuku a videa, kdy potrebujeme pravidelne zpracovavat useky dat

*/


#include "stdafx.h"
#include "imp_block.h"
#include "imp_config.h"
#include "imp_autotrigger.h"
#include <deque>

// jmeno, popis a cesta k bloku
const LPWSTR g_pName = L"RGB to Grayscale";
const LPWSTR g_pDesc = L"Converts color bitmap [24, 32bit] to grayscale bitmap";
const LPWSTR g_pPath = L"Conversion";
// jestli ma blok rohrani, odkomentujte prislusny radek
#define __BLOCKHAVECONFIGWINDOW
//#define __BLOCKHAVEAUTOTRIGGER
// pocty vstupnich a vystupnich portu
const DWORD g_dwInPortCount = 1;
const DWORD g_dwOutPortCount = 1;
// nazvy vstupnich a vystupnich portu
const WCHAR *g_pInputNameArray[] = {L"BMP/RGB24"};
const WCHAR *g_pOutputNameArray[] = {L"BMP/RGB8"};

// Tyto funkce nemenit !!!
// funkce volana klientskym programem k vytvoreni instance tridy

HRESULT CreateBlockInstance(REFIID riid, void **ppv)
{
	return CImpBlock::CreateInstance(riid, ppv);
}
// staticka funkce, ktera vytvari instanci tridy
HRESULT CImpBlock::CreateInstance(const IID &riid, void **ppv)
{
	CImpBlock* pObj; 
	pObj = new CImpBlock();
	if (pObj == NULL) 
		return E_OUTOFMEMORY; 
	HRESULT hr; 
	if ( FAILED(hr = pObj->QueryInterface(riid, (void**)ppv))) 
		delete pObj ; 
	return hr; 
}
// funkce, ktera vraci adresu rozhrani
void* CImpBlock::GetInterface(REFIID riid)
{
#ifdef __BLOCKHAVECONFIGWINDOW
	if ( IID_IBlockConfig == riid )
		return m_pxConfig;
#endif
#ifdef __BLOCKHAVEAUTOTRIGGER
	if ( IID_IBlockAutoTrigger == riid )
		return m_pxAutoTrigger;
#endif
	return NULL;
}
// konstruktor
CImpBlock::CImpBlock() : m_pxConfig(NULL), m_pxAutoTrigger(NULL)
{
#ifdef __BLOCKHAVECONFIGWINDOW
	m_pxConfig = new CImpBlockConfig(this);
#endif
#ifdef __BLOCKHAVEAUTOTRIGGER
	m_pxAutoTrigger = new CImpBlockAutoTrigger(this);
#endif
	m_dwInPortCount = g_dwInPortCount;
	m_dwOutPortCount = g_dwOutPortCount;
	m_pName = g_pName;
	m_pDesc = g_pDesc;
	m_pPath = g_pPath;
	m_pInputNameArray = g_pInputNameArray;
	m_pOutputNameArray = g_pOutputNameArray;

	// inicializace promennych

	m_fRed = 0.333f;
	m_fGreen = 0.334f;
	m_fBlue = 0.333f;
}
// destruktor
CImpBlock::~CImpBlock()
{
	if ( m_pxAutoTrigger )
		delete ( m_pxAutoTrigger );
	if ( m_pxConfig )
		delete ( m_pxConfig );
}

///////////////////////////////
// TYTO FUNKCE IMPLEMENTOVAT //
///////////////////////////////

STDMETHODIMP CImpBlock::InitInstance()
{
	CBlock::InitInstance();
	return S_OK;
}

STDMETHODIMP CImpBlock::ExitInstance()
{
	CBlock::ExitInstance();
	return S_OK;
}

STDMETHODIMP CImpBlock::ProcessRev(const DWORD dwProcessId, REVDATAMAP &rRevDataMap)
{
	return S_OK;
}

STDMETHODIMP CImpBlock::ClearRevMap(REVDATAMAP &rRevDataMapClean)
{
	return S_OK;
}

STDMETHODIMP CImpBlock::ProcessData(const LPBLOCKDATA pInputData)
{
	BYTE* data = NULL;
	float fr, fg, fb;

	EnterCriticalSection(&m_cs);
	fr = m_fRed;
	fg = m_fGreen;
	fb = m_fBlue;
	LeaveCriticalSection(&m_cs);

	std::deque<std::pair<LPVOID, DWORD> > dq;
	DWORD dwUsedLayers = 0;

	for ( DWORD dwLayer = 0; dwLayer < pInputData->dwLayersArray[0]; dwLayer++ )
	{	// pro kazdou vrstvu
		if ( pInputData->dwDataLenArrayArray[0][dwLayer] >= sizeof(BITMAPFILEHEADER) )
		{	// jestli je velikost vetsi nebo rovna velikosti headeru
			BITMAPFILEHEADER* bfh = (BITMAPFILEHEADER*)pInputData->pDataArrayArray[0][dwLayer];
			if ( bfh->bfType == 0x4d42 )
			{	// jedna-li se o BMP soubor
				BITMAPINFO* bi = (BITMAPINFO*)&((BYTE*)bfh)[sizeof(BITMAPFILEHEADER)];
				if ( !(bi->bmiHeader.biBitCount != 24 && bi->bmiHeader.biBitCount != 32) )
				{	// je 24 nebo 32bit
					DWORD dwLen = bi->bmiHeader.biHeight * bi->bmiHeader.biWidth
						+ sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFO) + 256*sizeof(RGBQUAD);
					data = (BYTE*)malloc(dwLen);
					DWORD step = bi->bmiHeader.biBitCount / 8;
					UCHAR* ptrsrc = (&((BYTE*)bfh)[bfh->bfOffBits]);
					UCHAR* ptrtgt = &data[sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)];
					DWORD steps = bi->bmiHeader.biWidth * bi->bmiHeader.biHeight;

					((BITMAPFILEHEADER*)data)->bfType = 0x4d42;
					((BITMAPFILEHEADER*)data)->bfReserved1 = 0x0000;
					((BITMAPFILEHEADER*)data)->bfReserved2 = 0x0000;
					((BITMAPFILEHEADER*)data)->bfSize = dwLen;
					((BITMAPFILEHEADER*)data)->bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + 256*sizeof(RGBQUAD);

					((BITMAPINFO*)&data[sizeof(BITMAPFILEHEADER)])->bmiHeader.biBitCount = 8;
					((BITMAPINFO*)&data[sizeof(BITMAPFILEHEADER)])->bmiHeader.biClrImportant = 0;
					((BITMAPINFO*)&data[sizeof(BITMAPFILEHEADER)])->bmiHeader.biClrUsed = 0;
					((BITMAPINFO*)&data[sizeof(BITMAPFILEHEADER)])->bmiHeader.biCompression = 0;
					((BITMAPINFO*)&data[sizeof(BITMAPFILEHEADER)])->bmiHeader.biHeight = bi->bmiHeader.biHeight;
					((BITMAPINFO*)&data[sizeof(BITMAPFILEHEADER)])->bmiHeader.biPlanes = 1;
					((BITMAPINFO*)&data[sizeof(BITMAPFILEHEADER)])->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
					((BITMAPINFO*)&data[sizeof(BITMAPFILEHEADER)])->bmiHeader.biSizeImage = 0;
					((BITMAPINFO*)&data[sizeof(BITMAPFILEHEADER)])->bmiHeader.biWidth = bi->bmiHeader.biWidth;
					((BITMAPINFO*)&data[sizeof(BITMAPFILEHEADER)])->bmiHeader.biXPelsPerMeter = 0;
					((BITMAPINFO*)&data[sizeof(BITMAPFILEHEADER)])->bmiHeader.biYPelsPerMeter = 0;

					for ( DWORD i = 0; i < 256; i++ )
					{
						*ptrtgt = (UCHAR)i; ptrtgt++;	// BLUE
						*ptrtgt = (UCHAR)i; ptrtgt++;	// GREEN
						*ptrtgt = (UCHAR)i; ptrtgt++;	// RED
						*ptrtgt = 0; ptrtgt++;			// RESERVED
					}

					for ( DWORD i = 0; i < steps; i++ )
					{
						*ptrtgt = (UCHAR)(ptrsrc[2] * fr + ptrsrc[1] * fg + ptrsrc[0] * fb);
						//*ptrtgt = i / bi->bmiHeader.biWidth;
						ptrsrc += step;
						ptrtgt++;
					}
					dwUsedLayers ++;
					dq.push_back(std::pair<LPVOID, DWORD>(data, dwLen));
				}				
			}
	//		else
	//			return S_FALSE;
		}
	//	else
	//		return S_FALSE;
	}

	LockBlockData();
	DeleteBlockData();
	PrepareBlockData();
	NewBlockPortData(0, dwUsedLayers); // jedna vrstva
	for ( DWORD i = 0; i < dwUsedLayers; i++ )
	{
		SetDataPtr(0, i, dq.front().first, dq.front().second);
		dq.pop_front();
	}
	UnlockBlockData();

	return S_OK;
}

STDMETHODIMP_(void) CImpBlock::Serialize(CArchive &ar)
{
	if ( ar.IsStoring() )
	{
		ar << m_fRed << m_fGreen << m_fBlue;
	}
	else
	{
		ar >> m_fRed >> m_fGreen >> m_fBlue;;
	}
}