📄 algebraoperation.cpp
字号:
/////////////////////////////////////////////////////////////////////////////////
//
// AlgebraOperation.cpp: implementation of the CAlgebraOperation class.
//
////////////////////////////////////////////////////////////////////////////////
// 版权所有(2002)
// Copyright(2002)
// 编写者: 向世明
// Author: Xiang Shiming
#include "stdafx.h"
#include "AlgebraOperation.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
IMPLEMENT_DYNAMIC(CAlgebraOperation, CImageComposite)
CAlgebraOperation::CAlgebraOperation()
{
m_dwOperation = IMAGE_ALGEBRA_ADD;
}
CAlgebraOperation::~CAlgebraOperation()
{
}
#ifdef _DEBUG
void CAlgebraOperation::Dump(CDumpContext& dc) const
{
CImageComposite::Dump(dc);
}
void CAlgebraOperation::AssertValid() const
{
CImageComposite::AssertValid();
}
#endif
//(nXSrc, nYSrc, nWidth, nHeight) 表示合成输入源的哪部分区域:位置和大小
//nXDst, nYDst 表示从目的地什么位置开始, 大多数情况下, 这两个参数均取0值
//lpbyBitsSrc 表示输入源图像像素字节
//nWidthImgSrc, nHeightImgSrc, 分别表示输入源图像的宽度和高度
//lpbyBitsDst 表示目的地图像的像素字节
//nWidthImgDst, nHeightImgDst 分别表示目的地图像的宽度和高度.
//将输入源加至目的地图像
//目的地 + 源
BOOL CAlgebraOperation::Add(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_ALGEBRA_ADD;
//有效区域的宽度和高度
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++;
//第22条语句
//目的地
BYTE* pbyBlue = pbyDst++;
BYTE* pbyGreen = pbyDst++;
BYTE* pbyRed = pbyDst++;
pbyDst++;
//目的地是被加数, 源是加数
int r = (int)(*pbyRed) + (int)byRed;
int g = (int)(*pbyGreen) + (int)byGreen;
int b = (int)(*pbyBlue) + (int)byBlue;
r = (BYTE)BOUND(r, 0, 255);
g = (BYTE)BOUND(g, 0, 255);
b = (BYTE)BOUND(b, 0, 255);
*pbyBlue = b;
*pbyGreen = g;
*pbyRed = r;
}
dwBaseIndexSrc += dwWidthBytesSrc;
dwBaseIndexDst += dwWidthBytesDst;
}
return TRUE;
}
//目的地 - 源
BOOL CAlgebraOperation::Subtract(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_ALGEBRA_SUBTRACT;
//有效区域的宽度和高度
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++;
//目的地是被减数, 源是减数
int r = (int)(*pbyRed) - (int)byRed;
int g = (int)(*pbyGreen) - (int)byGreen;
int b = (int)(*pbyBlue) - (int)byBlue;
r = (BYTE)BOUND(r, 0, 255);
g = (BYTE)BOUND(g, 0, 255);
b = (BYTE)BOUND(b, 0, 255);
*pbyBlue = b;
*pbyGreen = g;
*pbyRed = r;
}
dwBaseIndexSrc += dwWidthBytesSrc;
dwBaseIndexDst += dwWidthBytesDst;
}
return TRUE;
}
//目的地 * 源
BOOL CAlgebraOperation::Multiply(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_ALGEBRA_MULTIPLY;
//有效区域的宽度和高度
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++;
//目的地是被乘数, 源是乘数
int r = ((int)(*pbyRed) * (int)byRed) / 255;
int g = ((int)(*pbyGreen) * (int)byGreen) / 255;
int b = ((int)(*pbyBlue) * (int)byBlue) / 255;
r = (BYTE)BOUND(r, 0, 255);
g = (BYTE)BOUND(g, 0, 255);
b = (BYTE)BOUND(b, 0, 255);
*pbyBlue = b;
*pbyGreen = g;
*pbyRed = r;
}
dwBaseIndexSrc += dwWidthBytesSrc;
dwBaseIndexDst += dwWidthBytesDst;
}
return TRUE;
}
BOOL CAlgebraOperation::Divide(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_ALGEBRA_DIVIDE;
//有效区域的宽度和高度
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++;
if(byRed == 0) byRed = 1;
if(byGreen == 0) byGreen = 1;
if(byBlue == 0) byBlue = 1;
//目的地是被除数, 源是除数
int r = ((int)(*pbyRed) * 255) / (int)byRed;
int g = ((int)(*pbyGreen) * 255) / (int)byGreen;
int b = ((int)(*pbyBlue) * 255) / (int)byBlue;
r = (BYTE)BOUND(r, 0, 255);
g = (BYTE)BOUND(g, 0, 255);
b = (BYTE)BOUND(b, 0, 255);
*pbyBlue = b;
*pbyGreen = g;
*pbyRed = r;
}
dwBaseIndexSrc += dwWidthBytesSrc;
dwBaseIndexDst += dwWidthBytesDst;
}
return TRUE;
}
BOOL CAlgebraOperation::Average(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_ALGEBRA_AVERAGE;
//有效区域的宽度和高度
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 = (BYTE)(((int)(*pbyRed) + (int)byRed) / 2);
BYTE g = (BYTE)(((int)(*pbyGreen) + (int)byGreen) / 2);
BYTE b = (BYTE)(((int)(*pbyBlue) + (int)byBlue) / 2);
*pbyBlue = b;
*pbyGreen = g;
*pbyRed = r;
}
dwBaseIndexSrc += dwWidthBytesSrc;
dwBaseIndexDst += dwWidthBytesDst;
}
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -