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

📄 effect.cpp

📁 一个国人自己实现图像库的程序(有参考价值)
💻 CPP
📖 第 1 页 / 共 4 页
字号:
				int		iTemp = (pPixel[0] - pPixel[nSpan] +
								 pPixel[1] - pPixel[nSpan+1] +
								 pPixel[2] - pPixel[nSpan+2]) / 3 ;
				pPixel[0] = max (min (iStep * iTemp + 128, 0xFF), 0) ;
				pPixel[1] = max (min (iStep * iTemp + 128, 0xFF), 0) ;
				pPixel[2] = max (min (iStep * iTemp + 128, 0xFF), 0) ;
			}
			pPixel[0] = pPixel[1] = pPixel[2] = 128 ;
		}
	}
	else
	{
		FCDibEffect		block ;
		this->GetSubBlock (&block, m_SelRect) ;
		block.Emboss (iStep, nDirection) ;
		this->__BltBlock (block, m_SelRect.left, m_SelRect.top) ;
	}
}
//===================================================================
void  FCDibEffect::Sharp (int nStep)
{
	if ((this->ColorBits() < 24) || (nStep < 1))
		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)
			{
				if (pPixel[0] != pPixel[nSpan])		pPixel[0] = max (min ((pPixel[0] - pPixel[nSpan]) * nStep + pPixel[0], 255), 0) ;
				if (pPixel[1] != pPixel[nSpan+1])	pPixel[1] = max (min ((pPixel[1] - pPixel[nSpan+1]) * nStep + pPixel[1], 255), 0) ;
				if (pPixel[2] != pPixel[nSpan+2])	pPixel[2] = max (min ((pPixel[2] - pPixel[nSpan+2]) * nStep + pPixel[2], 255), 0) ;
			}
		}
	}
	else
	{
		FCDibEffect		block ;
		this->GetSubBlock (&block, m_SelRect) ;
		block.Sharp (nStep) ;
		this->__BltBlock (block, m_SelRect.left, m_SelRect.top) ;
	}
}
//===================================================================
void  FCDibEffect::Stripe (int iSwing, int iFrequency)
{
	if (this->ColorBits() < 8)
		return ;
	if (m_UndoFlag)
		this->AddToUndoList () ;

	if (m_hSelRgn == NULL) // 整图
	{
		int		iDelta = iSwing * this->Height() * 95/100/100 ; // 上移量,最大为高度的95%
		this->ShiftUp (iDelta) ;
		FillMemory (this->GetMemStart(), this->GetPitch() * iDelta, 0xFF) ;

		double		duAngleSpan = iFrequency * LIB_PI / this->Width() / 10 ; // 求出两象素间角度增量
		double		duCurrAngle = 0 ;
		iDelta = - iDelta / 2 ;
		for (int i=0 ; i < this->Width() ; i++, duCurrAngle += duAngleSpan)
			this->ColShiftDown (i, (cos(duCurrAngle) - 1)*iDelta) ;
	}
	else
	{
		FCDibEffect		block ;
		this->GetSubBlock (&block, m_SelRect) ;
		block.Stripe (iSwing, iFrequency) ;
		this->__BltBlock (block, m_SelRect.left, m_SelRect.top) ;
	}
}
//===================================================================
void  FCDibEffect::Windy (int nStep, int nDirection, bool bSmooth)
{
	if ((this->ColorBits() < 24) || (nStep < 1) || (nStep > Width() - 2))
		return ;
	if (m_UndoFlag)
		this->AddToUndoList () ;

	if (m_hSelRgn == NULL) // 整图
		for (int y = 0 ; y < Height() ; y++)
			this->WindySoftLine (y, bSmooth ? nStep : max (1, rand () % nStep),
								 nDirection) ;
	else
	{
		FCDibEffect		block ;
		this->GetSubBlock (&block, m_SelRect) ;
		block.Windy (nStep, nDirection, bSmooth) ;
		this->__BltBlock (block, m_SelRect.left, m_SelRect.top) ;
	}
}
//===================================================================
void  FCDibEffect::WindySoftLine (int nLine, int nStep, int nDirection)
{
	if ((this->ColorBits() < 24) || (nStep < 1))
		return ;

	int			iDiv = nStep + 1 ; // 平均象素个数
	int			nSpan = this->ColorBits() / 8 ; // 每象素字节数3, 4
	int			B=0, G=0, R=0 ;
	BYTE		* pCurr ;
	RGBQUAD		rgb ; // 边缘象素值

	switch (nDirection)
	{
		case DIRECT_LEFT :
			this->GetPixelColor (Width()-1, nLine, &rgb) ;
			pCurr = this->GetBits (nLine) ;
			break ;
		case DIRECT_RIGHT :
			this->GetPixelColor (0, nLine, &rgb) ;
			pCurr = this->GetBits (Width()-1, nLine) ;
			nSpan = -nSpan ;
			break ;
	}
	int			iBlk = iDiv*nSpan ; // 块长度
	for (int m=0 ; m < iDiv ; m++)
	{
		int		iMul = m*nSpan ;
		B += pCurr[iMul] ;
		G += pCurr[iMul + 1] ;
		R += pCurr[iMul + 2] ;
	}
	pCurr[0] = max (0, min (0xFF, B / iDiv)) ;
	pCurr[1] = max (0, min (0xFF, G / iDiv)) ;
	pCurr[2] = max (0, min (0xFF, R / iDiv)) ;

	//	移动方阵
	int			iXmax = Width() - nStep - 1 ;
	for (int x=0 ; x < Width() - 2 ; x++) // "-2"两边象素不用处理
	{
		if (x >= iXmax) // 处理右边缘象素
		{
			B = B - pCurr[0] + rgb.rgbBlue ;
			G = G - pCurr[1] + rgb.rgbGreen ;
			R = R - pCurr[2] + rgb.rgbRed ;
		}
		else
		{
			B = B - pCurr[0] + pCurr[iBlk] ;
			G = G - pCurr[1] + pCurr[iBlk + 1] ;
			R = R - pCurr[2] + pCurr[iBlk + 2] ;
		}
		pCurr += nSpan ;
		pCurr[0] = max (0, min (0xFF, B / iDiv)) ;
		pCurr[1] = max (0, min (0xFF, G / iDiv)) ;
		pCurr[2] = max (0, min (0xFF, R / iDiv)) ;
	}
}
//===================================================================
void  FCDibEffect::SmoothAverage (int iBlockLen)
{
	if ((this->ColorBits() < 24) || (iBlockLen < 2) || (iBlockLen >= this->Width()) || (iBlockLen >= this->Height()))
		return ;
	if (m_UndoFlag)
		this->AddToUndoList () ;

	int		iUp, iLeft, iRight, iDown ;
	iUp = iLeft = iBlockLen / 2 ;
	iDown = iRight = iUp - (iBlockLen % 2)^1 ;

	BOOL	bBak = this->m_UndoFlag ;
	this->m_UndoFlag = FALSE ;
	this->DuplicateFrame (iLeft, iUp, iRight, iDown) ;
	this->SmoothAverageNoEdge (iBlockLen) ;
	this->EraseFrame (iLeft, iUp, iRight, iDown) ;
	this->m_UndoFlag = bBak ;
}
//===================================================================
void  FCDibEffect::SmoothAverageNoEdge (int iBlockLen)
{
	if ((this->ColorBits() < 24) || (iBlockLen < 2) || (iBlockLen >= this->Width()) || (iBlockLen >= this->Height()))
		return ;
	if (m_UndoFlag)
		this->AddToUndoList () ;

	if (m_hSelRgn == NULL) // 整图
	{
		FCDib	* old ;
		if (m_UndoFlag) // 当前图象已经保存
			old = m_UndoList.back () ; // return FCDib*
		else
		{
			old = new FCDib ;
			* old = * (FCDib *) this ; // 保存当前图象
		}

		// 从上往下开始计算
		int			iXStart, iYStart, iXEnd, iYEnd,
					nSpan = this->ColorBits() / 8,
					iNum = iBlockLen * iBlockLen ; // 方阵象素个数
		iXStart = iYStart = iBlockLen/2 ; // 左上角的起始位置
		iXEnd = Width() - iBlockLen ;	// X结束位置
		iYEnd = Height() - iBlockLen ;	// Y结束位置

		int			iFirstR = 0, iFirstG = 0, iFirstB = 0 ; // 每行第一子块RGB和
		DWORD		dwPitch = this->GetPitch() ;
		for (int y = 0 ; y <= iYEnd ; y++)
		{
			if (y == 0) // 计算第一个块 (左上角)
				for (int ny=0 ; ny < iBlockLen ; ny++)
				{
					BYTE	* pPixel = old->GetBits (ny) ;
					for (int nx=0 ; nx < iBlockLen ; nx++, pPixel += nSpan)
					{
						iFirstB += pPixel[0] ;
						iFirstG += pPixel[1] ;
						iFirstR += pPixel[2] ;
					}
				}
			else // y方向下移块
			{
				BYTE	* pUp = old->GetBits (y - 1),
						* pDown = old->GetBits (y - 1 + iBlockLen) ;
				// 减上行加下行
				for (int nx=0 ; nx < iBlockLen ; nx++, pUp += nSpan, pDown += nSpan)
				{
					iFirstB = iFirstB - pUp[0] + pDown[0] ;
					iFirstG = iFirstG - pUp[1] + pDown[1] ;
					iFirstR = iFirstR - pUp[2] + pDown[2] ;
				}
			}

			// 设置每行第一个象素
			BYTE	* pWrite = this->GetBits (iXStart, y + iYStart) ;
			pWrite[0] = iFirstB / iNum ;
			pWrite[1] = iFirstG / iNum ;
			pWrite[2] = iFirstR / iNum ;
			pWrite += nSpan ;

			// x方向推移块
			int		iCurrR = iFirstR, iCurrG = iFirstG, iCurrB = iFirstB ;
			BYTE	* pLeft = old->GetBits (y),
					* pRight = old->GetBits (iBlockLen, y) ;
			for (int x = 1 ; x <= iXEnd ; x++)
			{
				// 减左列加右列
				BYTE	* pTempL = pLeft, * pTempR = pRight ;
				for (int iy=0 ; iy < iBlockLen ; iy++, pTempL -= dwPitch, pTempR -= dwPitch)
				{
					iCurrB = iCurrB - pTempL[0] + pTempR[0] ;
					iCurrG = iCurrG - pTempL[1] + pTempR[1] ;
					iCurrR = iCurrR - pTempL[2] + pTempR[2] ;
				}
				// 设置象素值
				pWrite[0] = iCurrB / iNum ;
				pWrite[1] = iCurrG / iNum ;
				pWrite[2] = iCurrR / iNum ;
				pWrite += nSpan ;
				pLeft += nSpan ; pRight += nSpan ;
			}
		}
		if (!m_UndoFlag)
			delete old ;
	}
	else
	{
		FCDibEffect		block ;
		this->GetSubBlock (&block, m_SelRect) ;
		block.SmoothAverage (iBlockLen) ;
		this->__BltBlock (block, m_SelRect.left, m_SelRect.top) ;
	}
}
//===================================================================
void  FCDibEffect::Rotate90 ()
{
	if (this->ColorBits() < 8)
		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->Height(), Old->Width(), Old->ColorBits()))
	{
		DWORD	dwPitch = this->GetPitch (),
				dwOldPitch = Old->GetPitch () ;
		for (int y = 0 ; y < this->Height() ; y++)
		{
			BYTE	* pDest = (BYTE *) this->GetBits (y) ;
			BYTE	* pSrc = (BYTE *) Old->GetBits (y, Old->Height() - 1) ;
			for (int x = 0 ; x < this->Width() ; x++, pSrc += dwOldPitch)
				this->__SetPixelData (pDest, x, pSrc) ;
		}
		this->__CopyPalette (*Old) ;
	}
	if (!m_UndoFlag)
		delete Old ;
}
//===================================================================
void  FCDibEffect::operator << (int iWidth)
{
	if ((this->ColorBits() < 8) || (iWidth <= 0) || (iWidth >= Width()))
		return ;
	if (m_UndoFlag)
		this->AddToUndoList () ;

	if (m_hSelRgn == NULL)
	{
		DWORD	dwInterval = this->ColorBits() * iWidth / 8,
				dwCopy = this->ColorBits() * (Width() - iWidth) / 8 ;
		for (int i=0 ; i < this->Height() ; i++)
		{
			BYTE	* pPixel = this->GetBits (i) ;
			MoveMemory (pPixel, &pPixel[dwInterval], dwCopy) ;
			FillMemory (&pPixel[dwCopy], dwInterval, 0xFF) ;
		}
	}
	else
	{
		FCDibEffect		block ;
		this->GetSubBlock (&block, m_SelRect) ;
		block.operator << (iWidth) ;
		this->__BltBlock (block, m_SelRect.left, m_SelRect.top) ;
	}
}
void  FCDibEffect::operator >> (int iWidth)
{
	if ((this->ColorBits() < 8) || (iWidth <= 0) || (iWidth >= Width()))
		return ;
	if (m_UndoFlag)
		this->AddToUndoList () ;

	if (m_hSelRgn == NULL)
	{
		DWORD	dwInterval = this->ColorBits() * iWidth / 8,
				dwCopy = this->ColorBits() * (Width() - iWidth) / 8 ;
		for (int i=0 ; i < this->Height() ; i++)
		{
			BYTE	* pPixel = this->GetBits (i) ;
			MoveMemory (&pPixel[dwInterval], pPixel, dwCopy) ;
			FillMemory (pPixel, dwInterval, 0xFF) ;
		}
	}
	else
	{
		FCDibEffect		block ;
		this->GetSubBlock (&block, m_SelRect) ;
		block.operator >> (iWidth) ;
		this->__BltBlock (block, m_SelRect.left, m_SelRect.top) ;
	}
}
void  FCDibEffect::ShiftUp (int iHeight)
{
	if ((this->GetHandle() == NULL) || (iHeight <= 0) || (iHeight >= Height()))
		return ;
	if (m_UndoFlag)
		this->AddToUndoList () ;

	if (m_hSelRgn == NULL)
	{
		BYTE	* pDest = this->GetBits (Height() - 1 - iHeight),
				* pSrc = this->GetBits (Height() - 1) ;
		DWORD	dwPitch = this->GetPitch () ;
		MoveMemory (pDest, pSrc, (Height() - iHeight) * dwPitch) ;
		FillMemory (pSrc, dwPitch * iHeight, 0xFF) ;
	}
	else
	{
		FCDibEffect		block ;
		this->GetSubBlock (&block, m_SelRect) ;
		block.ShiftUp (iHeight) ;
		this->__BltBlock (block, m_SelRect.left, m_SelRect.top) ;
	}
}
void  FCDibEffect::ShiftDown (int iHeight)
{
	if ((this->GetHandle() == NULL) || (iHeight <= 0) || (iHeight >= Height()))
		return ;
	if (m_UndoFlag)

⌨️ 快捷键说明

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