1. 程式人生 > >C++實現影象的各種旋轉操作

C++實現影象的各種旋轉操作

說明:Buffer中存放的是要進行旋轉的原始影象,m_pImage中存放轉換後的影象資料。

      nHeight是影象的高度,我測試影象是1024;nWidth是影象的寬度,我測試影象是1400

      nImageSize = nHeight*nWidth;

unsigned char *m_pImage= new unsigned char[nIamgeSize];
	ZeroMemory(m_pImage,nIamgeSize);

	//--------------------------------------------------//
	LONG x,y;
	for(y=0;y<nHeight;y++)//逆時針90度翻轉
	{
		for(x=0;x<nWidth;x++)
		{
			m_pImage[y+(nWidth-x-1)*nHeight]=Buffer[x+y*nWidth];
		}
	}

	int Temp = nWidth;
	nWidth   = nHeight;
	nHeight  = Temp;
    //--------------------------------------------------//
	LONG x,y;
	for(y=0;y<nHeight;y++)//上下映象
	{
		for(x=0;x<nWidth;x++)
		{
			m_pImage[x+(nHeight-y-1)*nWidth]=Buffer[x+y*nWidth];
		}
	}

    //---------------------------------------------------//
	LONG x,y;
	for(y=0;y<nHeight;y++)//逆時針旋轉180度
	{
		for(x=0;x<nWidth;x++)
		{
			m_pImage[(nHeight-y)*nWidth-1-x]=Buffer[x+y*nWidth];
		}
	}

	//---------------------------------------------------//
	LONG x,y;
	for(y=0;y<nHeight;y++)//順時針旋轉90度
	{
		for(x=0;x<nWidth;x++)
		{
			m_pImage[nHeight*(x+1)-1-y]=Buffer[x+y*nWidth];
		}
	}

	int Temp = nWidth;
	nWidth   = nHeight;
	nHeight  = Temp;

	//--------------------------------------------------//
	LONG x,y;
	for(y=0;y<nHeight;y++)//水平映象
	{
		for(x=0;x<nWidth;x++)
		{
			m_pImage[nWidth*(y+1)-1-x]=Buffer[x+y*nWidth];
		}
	}