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

📄 logicoperation.cpp

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


#include "stdafx.h"
#include "LogicOperation.h"


//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
IMPLEMENT_DYNAMIC(CLogicOperation, CImageComposite)
CLogicOperation::CLogicOperation()
{
	m_dwOperation = IMAGE_LOGIC_AND;
}

CLogicOperation::~CLogicOperation()
{

}

#ifdef _DEBUG
void CLogicOperation::Dump(CDumpContext& dc) const
{
	CImageComposite::Dump(dc);
}

void CLogicOperation::AssertValid() const
{
	CImageComposite::AssertValid();
}
#endif

//(nXSrc, nYSrc, nWidth, nHeight)	表示合成输入源的哪部分区域:位置和大小
//nXDst, nYDst					表示从目的地什么位置开始, 大多数情况下, 这两个参数均取0值
//lpbyBitsSrc					表示输入源图像像素字节
//nWidthImgSrc, nHeightImgSrc, 	分别表示输入源图像的宽度和高度
//lpbyBitsDst					表示目的地图像的像素字节
//nWidthImgDst, nHeightImgDst	分别表示目的地图像的宽度和高度.

//将输入图像与目的地图像作逻辑与运算
BOOL CLogicOperation::And(int nXSrc,  int nYSrc,  int nWidth,  int nHeight,  int nXDst,  int nYDst,  LPBYTE lpbyBitsSrc32,  int nWidthImgSrc,  int nHeightImgSrc,  LPBYTE lpbyBitsDst32,  int nWidthImgDst,  int nHeightImgDst)
{
	//第一步, 参数合法性检测
	ASSERT(lpbyBitsSrc32);
	ASSERT(lpbyBitsDst32);

	if((nXSrc > (nWidthImgSrc - 1)) || (nYSrc > (nHeightImgSrc - 1))) return FALSE;
	if((nXDst > (nWidthImgDst - 1)) || (nYDst > (nHeightImgDst - 1))) return FALSE;

	m_dwOperation = IMAGE_LOGIC_AND;

	//有效区域的宽度和高度
	int w = min(nWidth, (nWidthImgSrc - nXSrc));
	int h = min(nHeight, (nHeightImgSrc - nYSrc));
	w = min(w, (nWidthImgDst - nXDst));
	h = min(h, (nHeightImgDst - nYDst));


	//行字节数:扫描
	DWORD dwWidthBytesSrc = (DWORD)nWidthImgSrc * 4;
	DWORD dwWidthBytesDst = (DWORD)nWidthImgDst * 4;

	//开始数据基索引
	DWORD dwBaseIndexSrc = nYSrc * dwWidthBytesSrc + 4 * nXSrc;
	DWORD dwBaseIndexDst = nYDst * dwWidthBytesDst + 4 * nXDst;

	//第二步, 逻辑运算
	for(int i = 0;i < h;i++)
	{
		BYTE* pbySrc = lpbyBitsSrc32 + dwBaseIndexSrc;
		BYTE* pbyDst = lpbyBitsDst32 + dwBaseIndexDst;

		for(int j = 0;j < w;j++)
		{
			//源
			BYTE byBlue = *pbySrc++;
			BYTE byGreen = *pbySrc++;
			BYTE byRed = *pbySrc++;
			pbySrc++;

			//目的地
			BYTE* pbyBlue = pbyDst++;
			BYTE* pbyGreen = pbyDst++;
			BYTE* pbyRed = pbyDst++;
			pbyDst++;
			
			//目的地是被加数, 源是加数
			BYTE r =  ((*pbyRed) & byRed);
			BYTE g =  ((*pbyGreen) & byGreen);
			BYTE b =  ((*pbyBlue) & byBlue);

			*pbyBlue = b;
			*pbyGreen = g;
			*pbyRed = r;
		}
		dwBaseIndexSrc += dwWidthBytesSrc;
		dwBaseIndexDst += dwWidthBytesDst;
	}
	return TRUE;

}

//逻辑或
BOOL CLogicOperation::Or(int nXSrc,  int nYSrc,  int nWidth,  int nHeight,  int nXDst,  int nYDst,  LPBYTE lpbyBitsSrc32,  int nWidthImgSrc,  int nHeightImgSrc,  LPBYTE lpbyBitsDst32,  int nWidthImgDst,  int nHeightImgDst)
{
	//第一步, 参数合法性检测
	ASSERT(lpbyBitsSrc32);
	ASSERT(lpbyBitsDst32);

	if((nXSrc > (nWidthImgSrc - 1)) || (nYSrc > (nHeightImgSrc - 1))) return FALSE;
	if((nXDst > (nWidthImgDst - 1)) || (nYDst > (nHeightImgDst - 1))) return FALSE;

	m_dwOperation = IMAGE_LOGIC_OR;

	//有效区域的宽度和高度
	int w = min(nWidth, (nWidthImgSrc - nXSrc));
	int h = min(nHeight, (nHeightImgSrc - nYSrc));
	w = min(w, (nWidthImgDst - nXDst));
	h = min(h, (nHeightImgDst - nYDst));


	//行字节数:扫描
	DWORD dwWidthBytesSrc = (DWORD)nWidthImgSrc * 4;
	DWORD dwWidthBytesDst = (DWORD)nWidthImgDst * 4;

	//开始数据基索引
	DWORD dwBaseIndexSrc = nYSrc * dwWidthBytesSrc + 4 * nXSrc;
	DWORD dwBaseIndexDst = nYDst * dwWidthBytesDst + 4 * nXDst;

	//第二步, 逻辑运算
	for(int i = 0;i < h;i++)
	{
		BYTE* pbySrc = lpbyBitsSrc32 + dwBaseIndexSrc;
		BYTE* pbyDst = lpbyBitsDst32 + dwBaseIndexDst;

		for(int j = 0;j < w;j++)
		{
			//源
			BYTE byBlue = *pbySrc++;
			BYTE byGreen = *pbySrc++;
			BYTE byRed = *pbySrc++;
			pbySrc++;


			//目的地
			BYTE* pbyBlue = pbyDst++;
			BYTE* pbyGreen = pbyDst++;
			BYTE* pbyRed = pbyDst++;
			pbyDst++;
			
			//目的地是被加数, 源是加数
			BYTE r =  ((*pbyRed) | byRed);
			BYTE g =  ((*pbyGreen) | byGreen);
			BYTE b =  ((*pbyBlue) | byBlue);

			*pbyBlue = b;
			*pbyGreen = g;
			*pbyRed = r;
		}
		dwBaseIndexSrc += dwWidthBytesSrc;
		dwBaseIndexDst += dwWidthBytesDst;
	}
	return TRUE;

}

//Bitwise-Exclusive-OR , 异或
BOOL CLogicOperation::Xor(int nXSrc,  int nYSrc,  int nWidth,  int nHeight,  int nXDst,  int nYDst,  LPBYTE lpbyBitsSrc32,  int nWidthImgSrc,  int nHeightImgSrc,  LPBYTE lpbyBitsDst32,  int nWidthImgDst,  int nHeightImgDst)
{
	//第一步, 参数合法性检测
	ASSERT(lpbyBitsSrc32);
	ASSERT(lpbyBitsDst32);

	if((nXSrc > (nWidthImgSrc - 1)) || (nYSrc > (nHeightImgSrc - 1))) return FALSE;
	if((nXDst > (nWidthImgDst - 1)) || (nYDst > (nHeightImgDst - 1))) return FALSE;

	m_dwOperation = IMAGE_LOGIC_XOR;

	//有效区域的宽度和高度
	int w = min(nWidth, (nWidthImgSrc - nXSrc));
	int h = min(nHeight, (nHeightImgSrc - nYSrc));
	w = min(w, (nWidthImgDst - nXDst));
	h = min(h, (nHeightImgDst - nYDst));


	//行字节数:扫描
	DWORD dwWidthBytesSrc = (DWORD)nWidthImgSrc * 4;
	DWORD dwWidthBytesDst = (DWORD)nWidthImgDst * 4;

	//开始数据基索引
	DWORD dwBaseIndexSrc = nYSrc * dwWidthBytesSrc + 4 * nXSrc;
	DWORD dwBaseIndexDst = nYDst * dwWidthBytesDst + 4 * nXDst;

	//第二步, 逻辑运算
	for(int i = 0;i < h;i++)
	{
		BYTE* pbySrc = lpbyBitsSrc32 + dwBaseIndexSrc;
		BYTE* pbyDst = lpbyBitsDst32 + dwBaseIndexDst;

		for(int j = 0;j < w;j++)
		{
			//源
			BYTE byBlue = *pbySrc++;
			BYTE byGreen = *pbySrc++;
			BYTE byRed = *pbySrc++;
			pbySrc++;

			
			//目的地
			BYTE* pbyBlue = pbyDst++;
			BYTE* pbyGreen = pbyDst++;
			BYTE* pbyRed = pbyDst++;
			pbyDst++;
			
			//目的地是被加数, 源是加数
			BYTE r =  ((*pbyRed) ^ byRed);
			BYTE g =  ((*pbyGreen) ^ byGreen);
			BYTE b =  ((*pbyBlue) ^ byBlue);

			*pbyBlue = b;
			*pbyGreen = g;
			*pbyRed = r;
		}
		dwBaseIndexSrc += dwWidthBytesSrc;
		dwBaseIndexDst += dwWidthBytesDst;
	}
	return TRUE;
}

⌨️ 快捷键说明

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