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

📄 diffusefilter.cpp

📁 visual c++数字图像与图形处理中的光盘内容
💻 CPP
字号:
/////////////////////////////////////////////////////////////////////////////////
//
// DiffuseFilter.cpp: implementation of the CDiffuseFilter class.
//
////////////////////////////////////////////////////////////////////////////////
// 版权所有(2002)
// Copyright(2002)
// 编写者: 向世明
// Author: Xiang Shiming

#include "stdafx.h"
#include "DiffuseFilter.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
IMPLEMENT_DYNAMIC(CDiffuseFilter, CImageAreaProcess)
CDiffuseFilter::CDiffuseFilter()
{
	m_dwOperation = IMAGE_DIFFUSE_PLOT_FILTER;
	m_nRows = 3;
	m_nCols = 3;
}

CDiffuseFilter::~CDiffuseFilter()
{

}

#ifdef _DEBUG
void CDiffuseFilter::Dump(CDumpContext& dc) const
{
	CImageAreaProcess::Dump(dc);
}

void CDiffuseFilter::AssertValid() const
{
	CImageAreaProcess::AssertValid();
}
#endif


//设置扩散小块的大小:宽度和高度, 

void CDiffuseFilter::SetDiffuseSize(int nWidth,  int nHeight)
{
	//内部用行数和列数来表示高度和宽度
	m_nRows = 2 * (nHeight / 2) + 1;
	m_nCols = 2 * (nWidth / 2) + 1;
}

PIXELCOLORRGB CDiffuseFilter::Diffuse(BYTE *pbyRed,  BYTE *pbyGreen,  BYTE *pbyBlue,  int nNum)
{
	float fPoint = ((float)rand() / RAND_MAX);
	int index = (int)((float)(nNum - 1) * fPoint);
	PIXELCOLORRGB rgb;
	rgb.red = pbyRed[index];
	rgb.green = pbyGreen[index];
	rgb.blue = pbyBlue[index];
	return rgb;
}


//x, y, nWidth,  int nHeight, 定义区域的宽度
//nScanWidth,  int nScanHeight, 扫描宽度和扫描高度
//lpbyBits32----传递源像素值, 回载结果值

BOOL CDiffuseFilter::Filter(LPBYTE lpbyBits32,  int x,  int y,  int nWidth,  int nHeight,  int nScanWidth,  int nScanHeight)
{
	//第一步, 参数合法性检测
	ASSERT(lpbyBits32);

	if((x > (nScanWidth - 1)) || (y > (nScanHeight - 1))) return FALSE;
	//有效区域的宽度和高度
	int w = min(nWidth, nScanWidth - x);
	int h = min(nHeight, nScanHeight - y);

	//行字节数
	DWORD dwWidthBytes = (DWORD)nScanWidth * 4;

	//建立一份拷贝
	BYTE* pbySrcCopy =  new BYTE[ (dwWidthBytes * nScanHeight) ];
	if(pbySrcCopy == NULL) return FALSE;
	::CopyMemory(pbySrcCopy, lpbyBits32, dwWidthBytes * nScanHeight);

	int i, j;

	//处理上下边界
	//上下边界的高度:
	int nBorderH = (m_nRows - 1) / 2;
	for(i = 0;i < nBorderH;i++)
	{
		//当前像素y坐标
		int yy = y + i;

		//将改变源数据
		BYTE* pbyDst = lpbyBits32 + yy * dwWidthBytes + 4 * x;
		for(j = 0;j < w;j++)
		{
			//当前像素x坐标
			int xx = x + j;

			//进行卷积操作
			PIXELCOLORRGB rgb = FilterPixelOnBorder(pbySrcCopy, xx, yy, nScanWidth, nScanHeight);
	
			//写向目的数据缓冲区
			*pbyDst++ = rgb.blue;
			*pbyDst++ = rgb.green;
			*pbyDst++ = rgb.red;
			pbyDst++;
		}
	}
	
	
	//下边界Y坐标
	int nYBottom = (y + h - 1);
	//处理下边界
	for(i = 0;i < nBorderH;i++)
	{
		//当前像素y坐标
		int yy = nYBottom - i;

		//指向目的地址
		BYTE* pbyDst = lpbyBits32 + yy * dwWidthBytes + 4 * x;
		for(j = 0;j < w;j++)
		{
			//当前像素x坐标
			int xx = x + j;

			//进行卷积操作
			PIXELCOLORRGB rgb = FilterPixelOnBorder(pbySrcCopy, xx, yy, nScanWidth, nScanHeight);
	
			//写向目的数据缓冲区
			*pbyDst++ = rgb.blue;
			*pbyDst++ = rgb.green;
			*pbyDst++ = rgb.red;
			pbyDst++;
		}
	}

	//垂直边界宽度
	int nBorderW = (m_nCols - 1) / 2;

	
	//剩余高度 + 1 + nBorderH: 
	int nRemnantH = (h - nBorderH);
	
	//处理左边界
	for(i = nBorderH; i < nRemnantH;i++)
	{
		//当前像素y坐标
		int yy = y + i;
		//指向目的地址
		BYTE* pbyDst = lpbyBits32 + yy * dwWidthBytes + 4 * x;

		for(j = 0;j < nBorderW;j++)
		{
			//当前像素x坐标
			int xx = x + j;
			//进行卷积操作
			PIXELCOLORRGB rgb = FilterPixelOnBorder(pbySrcCopy, xx, yy, nScanWidth, nScanHeight);
	
			//写向目的数据缓冲区
			*pbyDst++ = rgb.blue;
			*pbyDst++ = rgb.green;
			*pbyDst++ = rgb.red;
			pbyDst++;
		}
	}

	//右边界x坐标起点
	int nXRight = x + w - nBorderW; 

	//处理右边界
	for(i = nBorderH; i < nRemnantH;i++)
	{
		//当前像素y坐标
		int yy = y + i;
		//指向目的地址
		BYTE* pbyDst = lpbyBits32 + yy * dwWidthBytes + 4 * nXRight;

		for(j = 0;j < nBorderW;j++)
		{
			//当前像素x坐标
			int xx = nXRight + j;
			//进行卷积操作
			PIXELCOLORRGB rgb = FilterPixelOnBorder(pbySrcCopy, xx, yy, nScanWidth, nScanHeight);
	
			//写向目的数据缓冲区
			*pbyDst++ = rgb.blue;
			*pbyDst++ = rgb.green;
			*pbyDst++ = rgb.red;
			pbyDst++;
		}
	}

	//内部宽度 - nBorderW + 1
	int nInnerW = w - nBorderW;
	
	//处理内部
	for(i = nBorderH; i < nRemnantH;i++)
	{
		//当前像素y坐标
		int yy = y + i;
		//指向目的地址
		BYTE* pbyDst = lpbyBits32 + yy * dwWidthBytes + 4 * x;

		for(j = 0;j < nInnerW;j++)
		{
			//当前像素x坐标
			int xx = x + j;
			//进行卷积操作
			PIXELCOLORRGB rgb = FilterPixelInner(pbySrcCopy, xx, yy, nScanWidth, nScanHeight);
	
			//写向目的数据缓冲区
			*pbyDst++ = rgb.blue;
			*pbyDst++ = rgb.green;
			*pbyDst++ = rgb.red;
			pbyDst++;
		}
	}
	
	delete[] pbySrcCopy ;

	return TRUE;
}

//lpbyBitsSrc32----源像素值
//x, y当前像素的绝对地址, 其坐标是相对于(0, 0)点的.
//nScanWidth,  int nScanHeight, 扫描宽度和扫描高度

PIXELCOLORRGB CDiffuseFilter::FilterPixelOnBorder(LPBYTE lpbyBitsSrc32,  int x,  int y,  int nScanWidth,  int nScanHeight)
{
	//卷积核元素的总个数;
	int nNum = m_nRows * m_nCols;

	//行字节数
	DWORD dwWidthBytes = (DWORD)nScanWidth * 4;

	//存储一个像素邻域的RGB三分量
	BYTE* pbyRed = new BYTE[nNum];
	BYTE* pbyGreen = new BYTE[nNum];
	BYTE* pbyBlue = new BYTE[nNum];

	//邻域中的左上角点.
	int xx = x - ((m_nCols - 1) / 2);
	int yy = y - ((m_nRows - 1) / 2);

	//三个数组pnRed, pnGreen, pnBlue的索引值
	int index = 0;

	//用嵌套循环获取小邻域数据.
	
	for(int i = 0;i < m_nRows;i++)
	{
		//y坐标
		int nY = yy + i;

		//复制边界
		nY = (nY < 0) ? 0 : ((nY > (nScanHeight - 1)) ? (nScanHeight - 1) : nY);

		//指针, 指向行数据
		BYTE* pbySrc = lpbyBitsSrc32 + ((DWORD)nY) * dwWidthBytes; 

		for(int j = 0;j < m_nCols;j++)
		{
			//x坐标	
			int nX = xx + j;

			//复制边界
			nX = (nX < 0) ? 0 : ((nX > (nScanWidth - 1)) ? (nScanWidth - 1) : nX);
			
			BYTE* pbyRaw = pbySrc + 4 * nX;
			
			//记录颜色分量
			pbyBlue[index] = *pbyRaw++; 
			pbyGreen[index] = *pbyRaw++;
			pbyRed[index] = *pbyRaw++;
			index++;
		}
	}
	//RGB颜色结构, 在 IMG.H 中定义
	//计算卷积
	PIXELCOLORRGB rgb = Diffuse(pbyRed, pbyGreen, pbyBlue, nNum);

	delete[] pbyBlue;
	delete[] pbyGreen;
	delete[] pbyRed;
	return rgb;
}

//与FilterPixelOnBorder()函数相比, 该函数只是少了几个判断语句而已.
PIXELCOLORRGB CDiffuseFilter::FilterPixelInner(LPBYTE lpbyBitsSrc32,  int x,  int y,  int nScanWidth,  int nScanHeight)
{
	//卷积核元素的总个数;
	int nNum = m_nRows * m_nCols;

	//行字节数
	DWORD dwWidthBytes = (DWORD)nScanWidth * 4;

	//存储一个像素邻域的RGB三分量
	BYTE* pbyRed = new BYTE[nNum];
	BYTE* pbyGreen = new BYTE[nNum];
	BYTE* pbyBlue = new BYTE[nNum];

	//邻域中的左上角点.
	int xx = x - ((m_nCols - 1) / 2);
	int yy = y - ((m_nRows - 1) / 2);

	//三个数组pnRed, pnGreen, pnBlue的索引值
	int index = 0;

	//用嵌套循环获取小邻域数据.
	
	for(int i = 0;i < m_nRows;i++)
	{
		//y坐标
		int nY = yy + i;

		//指针, 指向行数据
		BYTE* pbySrc = lpbyBitsSrc32 + ((DWORD)nY) * dwWidthBytes + 4 * xx;

		for(int j = 0;j < m_nCols;j++)
		{
			//x坐标	
			//记录颜色分量
			pbyBlue[index] = *pbySrc++; 
			pbyGreen[index] = *pbySrc++;
			pbyRed[index] = *pbySrc++;
			pbySrc++;
			index++;
		}
	}

	//计算卷积
	PIXELCOLORRGB rgb = Diffuse(pbyRed, pbyGreen, pbyBlue, nNum);

	delete[] pbyBlue;
	delete[] pbyGreen;
	delete[] pbyRed;
	return rgb;
}

⌨️ 快捷键说明

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