// ARFrameGrabber.h: interface for the ARFrameGrabber class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_ARFRAMEGRABBER_H__C5553937_4BAB_4FEF_B4A6_1693AB0C99E3__INCLUDED_)
#define AFX_ARFRAMEGRABBER_H__C5553937_4BAB_4FEF_B4A6_1693AB0C99E3__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <AR/config.h>

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <dshow.h>
//#include <streams.h>
#include <atlbase.h>
#include <qedit.h>

class ART_API_EXPORT SmartString
{
public:
	SmartString():str(NULL) {}

	SmartString(char* pStr):str(NULL)
	{
		if (pStr)
		{
			int size = int(strlen(pStr));
			str = new char[size+1];
			strcpy(str, pStr);
		}
	}

	SmartString(SmartString& sStr)
	{
		SetString(sStr.GetBuffer());
	}

	~SmartString()
	{
		if (str)
			delete[] str;
	}

	SmartString& operator =(char* pStr)
	{
		SetString(pStr);
		return *this;
	}

	SmartString& operator =(SmartString& sStr)
	{
		SetString(sStr.GetBuffer());
		return *this;
	}

	char* GetBuffer() {return str;}

protected:
	void SetString(char *pStr)
	{
		if (str)
			delete[] str;

		if (!pStr)
		{
			str = NULL;
		}
		else
		{
			int size = int(strlen(pStr));
			str = new char[size + 1];
			strcpy(str, pStr);
		}

	}


	char* str;
};

class ART_API_EXPORT DeviceInfo
{
public:
	DeviceInfo():next(NULL), deviceId(-1)
	{
	} 
	~DeviceInfo()
	{
		if (next)
			delete next;
	}


	SmartString friendlyName;
	int			deviceId;	
	DeviceInfo* next;
};


class ART_API_EXPORT ARFrameGrabber  
{
public:
	ARFrameGrabber();
	virtual ~ARFrameGrabber();

	void Init(int deviceId, bool displayProperties = true);
	void BindFilter(int deviceId, IBaseFilter **pFilter);
	void GrabFrame(long* size, long** pBuffer);
	void GrabFrame();
	void Grab32BitFrame();


	long  GetBufferSize() {return bufferSize;}
	long* GetBuffer() {return pBuffer;}
	
	void SetFlippedImage(bool flag) {flipImage = flag;}

	void DisplayProperties();
	void EnumDevices(DeviceInfo *head);
    int GetWidth();
    int GetHeight();

protected:
	CComPtr<IGraphBuilder> pGraph;
	CComPtr<IBaseFilter> pDeviceFilter;
	CComPtr<IMediaControl> pMediaControl;
	CComPtr<IBaseFilter> pSampleGrabberFilter;
	CComPtr<ISampleGrabber> pSampleGrabber;
	CComPtr<IPin> pGrabberInput;
	CComPtr<IPin> pGrabberOutput;
	CComPtr<IPin> pCameraOutput;
	CComPtr<IMediaEvent> pMediaEvent;
	CComPtr<IBaseFilter> pNullFilter;
	CComPtr<IPin> pNullInputPin;

	void FlipImage(long* pBuf);

private:
	void ReportError(char *msg);

	bool flipImage;
	long bufferSize;
	long *pBuffer;
    bool connected;
    int width;
    int height;
};

#endif // !defined(AFX_ARFRAMEGRABBER_H__C5553937_4BAB_4FEF_B4A6_1693AB0C99E3__INCLUDED_)
