// ConertImageType1.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"


/*
内容:
	图像类型转换1;
相关函数:
	rviConvert
		说明:
			指定类型转换, 转换图像对象格式;
		参数:
			src			-	图像对象
			code		-	转换方式
			dest		-	目标图像对象, 默认为NULL
	
	rviCast
		说明:
			转换图像对象格式;
		参数:
			image		-	图像对象
			type		-	转换图像格式

*/
HWND m_hWnd =NULL,  m_hWnd1 =NULL, m_hWnd2=NULL;
CCanvasWindow m_cw, m_cw1, m_cw2;	

void InitAuxWnd(){
	HINSTANCE hInstance = InitInstance(NULL, FALSE);

	int  cx   =   GetSystemMetrics(  SM_CXFULLSCREEN ); 
	int  cy   =   GetSystemMetrics(  SM_CYFULLSCREEN );

	int w = 600;
	int h = 500;
	int  x = (cx - w)/2;
	int  y = (cy - h)/2;

	// 执行应用程序初始化: 
	  m_hWnd = CreateAuxilliaryWindow(hInstance, _T("创建图像对象" ), x,y,w,h);
	  assert(m_hWnd);

	  m_hWnd1 = CreateAuxilliaryWindow(hInstance, _T("Cast Src To Bin" ), x / 3,y / 3,w,h);
	  assert(m_hWnd1);
	
	  m_hWnd2 = CreateAuxilliaryWindow(hInstance, _T("Convert Src To Gray" ), x + x / 3,y + y / 3,w,h);
	  assert(m_hWnd2);

	RECT rect; 
	
	::GetClientRect(m_hWnd, &rect);
	m_cw.Create(  m_hWnd, _T("FVI canvas window" ), rect.left, rect.top, rect.right -rect.left, rect.bottom- rect.top);

	::GetClientRect(m_hWnd1, &rect);
	m_cw1.Create(  m_hWnd1, _T("Cast" ), rect.left, rect.top, rect.right -rect.left, rect.bottom- rect.top);

	::GetClientRect(m_hWnd2, &rect);
	m_cw2.Create(  m_hWnd2, _T("Cast" ), rect.left, rect.top, rect.right -rect.left, rect.bottom- rect.top);

}

void ReleaseAuxWnd()
{
	m_cw.Destroy();
	m_cw1.Destroy();
	m_cw2.Destroy();
}

int _tmain(int argc, _TCHAR* argv[])
{
	InitAuxWnd();
	

	RvImage imSrc = NULL;
	RvImage imCast = NULL;
	RvImage imConvert  =NULL;

	const char* filePath = _T("..\\samples\\colorwave.jpg");
	imSrc= rvLoadImage(filePath);

	if(imSrc != NULL)
	{
		int nSrcW = rviGetWidth(imSrc);
		int nSrcH = rviGetHeight(imSrc);
		RV_IMAGE_TYPE tySrcType = rviGetImageType(imSrc);
		 
		imCast = rviClone(imSrc, imCast);		
		if(imCast != NULL  )
		{
			 rviCast(imCast, RIT_BIN);
		} 
		
		imConvert  = rviClone(imSrc, NULL);
		if(imConvert != NULL)
		{ 
			rviConvert(imSrc, RV_CVT_RGB2GRAY, imConvert);
		}	
	}
	
	//显示
	if(imSrc) m_cw.Show(imSrc);
	if(imCast) m_cw1.Show(imCast);	
	if(imConvert) m_cw2.Show(imConvert);	

	//消息处理
	DoEvents();

	//销毁
	if(imConvert) rvDestroyImage(imConvert);
	if(imSrc) rvDestroyImage(imSrc);
	if(imCast) rvDestroyImage(imCast);

	//释放窗口对象
	ReleaseAuxWnd();

	return 0;
}

