⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 effect.cpp

📁 一个国人自己实现图像库的程序(有参考价值)
💻 CPP
📖 第 1 页 / 共 4 页
字号:
		}
	}
	else
	{
		FCDibEffect		block ;
		this->GetSubBlock (&block, m_SelRect) ;
		block.AdjustRGB (iValueR, iValueG, iValueB) ;
		this->__BltBlock (block, m_SelRect.left, m_SelRect.top) ;
	}
}
//===================================================================
void  FCDibEffect::Flip ()	// 所有位色有效
{
	if (this->GetHandle() == NULL)
		return ;
	if (m_UndoFlag)
		this->AddToUndoList () ;

	if (m_hSelRgn == NULL) // 整图
	{
		int		iLine = this->Height () / 2 ;
		BYTE	* pUp = this->GetBits (0),
				* pDown = this->GetBits (this->Height () - 1) ;
		DWORD	dwPitch = this->GetPitch () ;
		BYTE	* pbTemp = new BYTE [dwPitch] ;
		for (int y = 0 ; y < iLine ; y++)
		{
			CopyMemory (pbTemp, pUp, dwPitch) ;
			CopyMemory (pUp, pDown, dwPitch) ;
			CopyMemory (pDown, pbTemp, dwPitch) ;
			pUp -= dwPitch ; pDown += dwPitch ;
		}
		delete[] pbTemp ;
	}
	else
	{
		FCDibEffect		block ;
		this->GetSubBlock (&block, m_SelRect) ;
		block.Flip () ;
		this->__BltBlock (block, m_SelRect.left, m_SelRect.top) ;
	}
}
//===================================================================
void  FCDibEffect::AddFrame (int iLeft, int iTop, int iRight, int iBottom)
{
	if ((this->ColorBits() < 8) || (iLeft < 0) || (iTop < 0) || (iRight < 0) || (iBottom < 0))
		return ;

	FCDib		* Old = NULL ;
	if (m_UndoFlag) // 保存当前图象
	{
		this->AddToUndoList () ;
		Old = m_UndoList.back () ; // return FCDib*
	}	
	else
	{
		Old = new FCDib ;
		* Old = * (FCDib *) this ; // 保存当前图象
	}

	if (this->Create (this->Width() + iLeft + iRight, this->Height() + iTop + iBottom, Old->ColorBits()))
	{
		DWORD		dwPitch = Old->Width() * Old->ColorBits() / 8 ;
		for (int i=0 ; i < Old->Height() ; i++)
			CopyMemory (this->GetBits (iLeft, i + iTop), Old->GetBits (i), dwPitch) ;
		this->__CopyPalette (*Old) ;
	}
	if (!m_UndoFlag)
		delete Old ;
}
//===================================================================
void  FCDibEffect::EraseFrame (int iLeft, int iTop, int iRight, int iBottom)
{
	if ((this->ColorBits() < 8) || (iLeft < 0) || (iTop < 0) || (iRight < 0) || (iBottom < 0)
		|| (iLeft + iRight >= Width()) || (iTop + iBottom >= Height()))
		return ;

	FCDib		* Old = NULL ;
	if (m_UndoFlag) // 保存当前图象
	{
		this->AddToUndoList () ;
		Old = m_UndoList.back () ; // return FCDib*
	}	
	else
	{
		Old = new FCDib ;
		* Old = * (FCDib *) this ; // 保存当前图象
	}

	if (this->Create (Old->Width() - iLeft - iRight, Old->Height() - iTop - iBottom, Old->ColorBits()))
	{
		DWORD		dwPitch = this->Width() * Old->ColorBits() / 8 ;
		for (int i=0 ; i < this->Height() ; i++)
			CopyMemory (this->GetBits (i), Old->GetBits (iLeft, iTop + i), dwPitch) ;
		this->__CopyPalette (*Old) ;
	}
	if (!m_UndoFlag)
		delete Old ;
}
//===================================================================
void  FCDibEffect::Negate ()
{
	if (this->ColorBits() == 16)
		return ;
	if (m_UndoFlag)
		this->AddToUndoList () ;

	if (m_hSelRgn == NULL) // 整图
		if (this->ColorBits() > 8) // 24, 32-Bits
		{
			DWORD		* pPixel = (DWORD *) this->GetMemStart () ;
			int			nMax = this->GetPitch () * Height() / 4 ;
			for (int i=0 ; i < nMax ; i++, pPixel++)
				*pPixel = ~(*pPixel) ;
		}
		else // 调色板图象
		{
			int			nNum = 1 << this->ColorBits() ;
			RGBQUAD		* pPalette = new RGBQUAD [nNum] ;
			this->GetColorTable (0, nNum, pPalette) ;
			for (int i=0 ; i < nNum ; i++)
				* (DWORD *) &pPalette[i] = ~(* (DWORD *) &pPalette[i]) ;
			this->SetColorTable (0, nNum, pPalette) ;
			delete[] pPalette ;
		}
	else
	{
		FCDibEffect		block ;
		this->GetSubBlock (&block, m_SelRect) ;
		block.Negate () ;
		this->__BltBlock (block, m_SelRect.left, m_SelRect.top) ;
	}
}
//===================================================================
void  FCDibEffect::Mosaic (int iBlockLen)
{
	if ((this->ColorBits() < 24) || (iBlockLen < 2) || (iBlockLen >= this->Width()) || (iBlockLen >= this->Height()))
		return ;
	if (m_UndoFlag)
		this->AddToUndoList () ;

	if (m_hSelRgn == NULL) // 整图
	{
		// x, y块的个数及边界宽度
		int		iXblk = Width() / iBlockLen + ((Width() % iBlockLen) ? 1 : 0),
				iXborder = Width() % iBlockLen,
				iYblk = Height() / iBlockLen + ((Height() % iBlockLen) ? 1 : 0),
				iYborder = Height() % iBlockLen,
				nSpan = this->ColorBits() / 8, smX, smY ;
		DWORD	dwPitch = this->GetPitch(),
				dwWidthSpan = nSpan * iBlockLen ;
		if (iXborder == 0)	iXborder = iBlockLen ;
		if (iYborder == 0)	iYborder = iBlockLen ;

		for (int y=0 ; y < iYblk ; y++)
		{
			BYTE	* pStart = this->GetBits (y*iBlockLen) ; // 块左上角
			int		iCurrBlkY = (y == iYblk-1) ? iYborder : iBlockLen ;
			for (int x=0 ; x < iXblk ; x++, pStart += dwWidthSpan)
			{
				int		dwSumR = 0, dwSumG = 0, dwSumB = 0 ;
				int		iCurrBlkX = (x == iXblk-1) ? iXborder : iBlockLen ;
				DWORD	dwXPitch = nSpan * iCurrBlkX + dwPitch ;
				int		iNum = iCurrBlkY * iCurrBlkX ;
				// 计算子块平均值
				BYTE	* pCurr = pStart ;
				for (smY=0 ; smY < iCurrBlkY ; smY++, pCurr = pCurr - dwXPitch)
					for (smX=0 ; smX < iCurrBlkX ; smX++, pCurr += nSpan)
					{
						dwSumB += pCurr[0] ;
						dwSumG += pCurr[1] ;
						dwSumR += pCurr[2] ;
					}
				dwSumB = min (0xFF, dwSumB / iNum) ;
				dwSumG = min (0xFF, dwSumG / iNum) ;
				dwSumR = min (0xFF, dwSumR / iNum) ;
				// 设置像素新值
				pCurr = pStart ;
				for (smY=0 ; smY < iCurrBlkY ; smY++, pCurr = pCurr - dwXPitch)
					for (smX=0 ; smX < iCurrBlkX ; smX++, pCurr += nSpan)
					{
						pCurr[0] = dwSumB ;
						pCurr[1] = dwSumG ;
						pCurr[2] = dwSumR ;
					}
			} // End of X
		} // End of Y
	}
	else
	{
		FCDibEffect		block ;
		this->GetSubBlock (&block, m_SelRect) ;
		block.Mosaic (iBlockLen) ;
		this->__BltBlock (block, m_SelRect.left, m_SelRect.top) ;
	}
}
//===================================================================
void  FCDibEffect::Sphere ()
{
	if (this->ColorBits() < 16)
		return ;
	if (m_UndoFlag)
		this->AddToUndoList () ;

	if (m_hSelRgn == NULL)
	{
		FCDibEffect		Old (* (FCDibEffect *) this) ; // 备份原图
		Old.Stretch (Width()*LIB_PI/2.0, Height()*LIB_PI/2.0) ;

	}
	else
	{
		FCDibEffect		block ;
		this->GetSubBlock (&block, m_SelRect) ;
		block.Sphere () ;
		this->__BltBlock (block, m_SelRect.left, m_SelRect.top) ;
	}
}
//===================================================================
void  FCDibEffect::Column ()
{
	if (this->ColorBits() < 16)
		return ;
	if (m_UndoFlag)
		this->AddToUndoList () ;

	if (m_hSelRgn == NULL)
	{
		double		R = (Width() % 2 == 0) ? (Width() - 1)/2 : Width()/2 ;

		FCDibEffect		Old (* (FCDibEffect *) this) ; // 备份原图
		Old.Stretch (max (LIB_PI * R, Width()), Height(), FALSE) ;

		//	投影列在原图中的位置
		DWORD		dwNewMid = (Old.Width() % 2 == 0) ? (Old.Width() - 1)/2 : Old.Width()/2 ;
		for (int x=0 ; x < this->Width() ; x++)
		{
			DWORD	dwIndex = min (Old.Width()-1, max (dwNewMid - R * asin ((R-x)/R), 0)) ;
			//	copy投影列
			this->__CopyCol (*this, x, Old, dwIndex) ;
		}
	}
	else
	{
		FCDibEffect		block ;
		this->GetSubBlock (&block, m_SelRect) ;
		block.Column () ;
		this->__BltBlock (block, m_SelRect.left, m_SelRect.top) ;
	}
}
//===================================================================
void  FCDibEffect::ConeSunken (int iHeight)
{
	if (this->ColorBits() < 16)
		return ;
	if (m_UndoFlag)
		this->AddToUndoList () ;

	if (m_hSelRgn == NULL)
	{
		POINT		center ;
		center.x = (Width() % 2 == 0) ? (Width() - 1)/2 : Width()/2 ;
		center.y = (Height() % 2 == 0) ? (Height() - 1)/2 : Height()/2 ;

		int			R = 400 ;
		FCDibEffect		Old (* (FCDibEffect *) this) ; // 备份原图
		Old.Stretch (this->Width()*LIB_PI/2, this->Height()*LIB_PI/2, TRUE) ;
		POINT		cenOld ;
		cenOld.x = (Old.Width() % 2 == 0) ? (Old.Width() - 1)/2 : Old.Width()/2 ;
		cenOld.y = (Old.Height() % 2 == 0) ? (Old.Height() - 1)/2 : Old.Height()/2 ;

		//	投影x,y在原图中的位置
		RGBQUAD	aaa;
		for (int y=0 ; y < R ; y++)
			for (int x=0 ; x < R ; x++)
			{
				int		spanX = x - center.x ;
				int		spanY = y - center.y ;
				int		newX = R*asin (_hypot (spanX, spanY)/R)*spanX/_hypot (spanX, spanY) ;
				int		newY = R*asin (_hypot (spanX, spanY)/R)*spanY/_hypot (spanX, spanY) ;
				newX = cenOld.x + newX ;
				newY = cenOld.y + newY ;
				Old.GetPixelColor (newX, newY, &aaa) ;
				this->SetPixelColor (x, y, aaa) ;
			}
	}
	else
	{
		FCDibEffect		block ;
		this->GetSubBlock (&block, m_SelRect) ;
		block.ConeSunken (iHeight) ;
		this->__BltBlock (block, m_SelRect.left, m_SelRect.top) ;
	}
}
//===================================================================
void  FCDibEffect::Glass (int iBlockLen)
{
	if ((this->ColorBits() != 32) || (iBlockLen < 2) || (iBlockLen > 100)
		|| (iBlockLen >= min (this->Width(), this->Height())))
		return ;
	
	if (m_UndoFlag)
		this->AddToUndoList () ;

	if (m_hSelRgn == NULL) // 整图
	{
		int			iMid = iBlockLen / 2,
					iMaxW = this->Width () - iBlockLen,
					iMaxH = this->Height () - iBlockLen ;
		RGBQUAD		* pCurr, * pSrc ;
		int			x, y ;

		FCDib		OldDIB ; // 保存原图
		OldDIB = * (FCDib *) this ;

		//	玻璃化主图
		for (y = 0 ; y <= iMaxH ; y++)
		{
			pCurr = (RGBQUAD *) this->GetBits (iMid, iMid + y) ;
			pSrc = (RGBQUAD *) OldDIB.GetBits (y) ;
			for (x = 0 ; x <= iMaxW ; x++, pCurr++, pSrc++)
				CopyMemory (pCurr, pSrc - this->Width() * (rand() % iBlockLen) + (rand() % iBlockLen),
							sizeof(RGBQUAD)) ;
		}

		//	玻璃化四周
		if (iBlockLen == 2)
			return ;
		//	上
/*		for (y = 0 ; y < iMid ; y++)
		{
			pCurr = (RGBQUAD *) this->GetBits (y) ;
			pSrc = (RGBQUAD *) OldDIB.GetBits (y) ;
			for (x = 0 ; x <= iMaxW ; x++, pCurr++, pSrc++)
			{
				int	a1,a2 ;
				a1 = rand() % iBlockLen - iMid ;
				a2 = rand() % iBlockLen - iMid ;
				a1=max(a1,0);
				a2=max (a2,0);
				CopyMemory (pCurr, pSrc - this->Width() * a1 + a2,
							sizeof(RGBQUAD)) ;
			}
		}*/
		//	下
/*		for (y = this->Height() - iBlockLen ; y < this->Height() ; y++)
		{
			pCurr = (RGBQUAD *) this->GetBits (iBlockLen, y) ;
			pSrc = (RGBQUAD *) OldDIB.GetBits (iBlockLen, y) ;
			for (x = iBlockLen ; x < this->Width() ; x++, pCurr++, pSrc++)
				CopyMemory (pCurr, pSrc - this->Width() * (rand() % iBlockLen) + (rand() % iBlockLen),
							sizeof(RGBQUAD)) ;
		}*/

		//	上
		
		//	上

	}
	else
	{
		FCDibEffect		block ;
		this->GetSubBlock (&block, m_SelRect) ;
		block.Glass (iBlockLen) ;
		this->__BltBlock (block, m_SelRect.left, m_SelRect.top) ;
	}
}
//===================================================================
void  FCDibEffect::Emboss (int iStep, int nDirection)
{
	if (this->ColorBits() < 24)
		return ;
	if (m_UndoFlag)
		this->AddToUndoList () ;

	if (m_hSelRgn == NULL) // 整图
	{
		int			nSpan = this->ColorBits() / 8 ; // 每象素字节数3, 4
		for (int y = 0 ; y < Height() ; y++)
		{
			BYTE	* pPixel = this->GetBits (y) ;
			for (int x = 0 ; x < Width() - 1 ; x++, pPixel += nSpan)
			{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -