📄 contrastprocess.cpp
字号:
/////////////////////////////////////////////////////////////////////////////////
//
// ContrastProcess.cpp: implementation of the CContrastProcess class.
//
////////////////////////////////////////////////////////////////////////////////
// 版权所有(2002)
// Copyright(2002)
// 编写者: 向世明
// Author: Xiang Shiming
#include "stdafx.h"
#include "ContrastProcess.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
IMPLEMENT_DYNAMIC(CContrastProcess, CImagePointProcess)
CContrastProcess::CContrastProcess()
{
m_dwOperation = IMAGE_CONTRAST_ADJUSTMENT;
m_nIncrement = 0;
}
CContrastProcess::~CContrastProcess()
{
}
#ifdef _DEBUG
void CContrastProcess::Dump(CDumpContext& dc) const
{
CImagePointProcess::Dump(dc);
}
void CContrastProcess::AssertValid() const
{
CImagePointProcess::AssertValid();
}
#endif
void CContrastProcess::SetContrastIncrement(int nIncrement)
{
m_nIncrement = nIncrement;
}
//增加对比度
void CContrastProcess::IncreaseContrast(BYTE *pbyBlue, BYTE *pbyGreen, BYTE *pbyRed, int nHigh, int nStretch)
{
//in this case, m_nIncrement >= 0
//nStretch = 255 - 2 * m_nIncrement;
//high = 255 - m_nIncrement;
//线性扩展
//蓝色
if(*pbyBlue <= m_nIncrement) *pbyBlue = 0;
else if(*pbyBlue > nHigh) *pbyBlue = 255;
else
*pbyBlue = (BYTE)((((int)*pbyBlue - m_nIncrement) * 255) / nStretch );
//绿色
if(*pbyGreen <= m_nIncrement) *pbyGreen = 0;
else if(*pbyGreen > nHigh) *pbyGreen = 255;
else
*pbyGreen = (BYTE)((((int)*pbyGreen - m_nIncrement) * 255) / nStretch );
//红色
if(*pbyRed <= m_nIncrement) *pbyRed = 0;
else if(*pbyRed > nHigh) *pbyRed = 255;
else
*pbyRed = (BYTE)((((int)*pbyRed - m_nIncrement) * 255) / nStretch );
}
void CContrastProcess::DecreaseContrast(BYTE *pbyBlue, BYTE *pbyGreen, BYTE *pbyRed, int nStretch)
{
//in this case m_nIncrement < 0
//nStretch = 255 + 2 * m_nIncrement;
//线性压缩
*pbyBlue = (BYTE)((((int)(*pbyBlue) * nStretch) / 255) - m_nIncrement);
*pbyGreen = (BYTE)((((int)(*pbyGreen) * nStretch) / 255) - m_nIncrement);
*pbyRed = (BYTE)((((int)(*pbyRed) * nStretch) / 255) - m_nIncrement);
}
//改变指定区域(x, y, nWidth, nHeight)的对比度.
//nScanWidth表示数据扫描宽度, 以像素为单位, 而不是以字节为单位
//nScanHeight表示可扫描高度, 该参数主要用于合法性检验, 保证被处理的区域是合法区域.
//lpbyBits32--源数据----32位
//本来, 处理成DWORD数据最直观, 但显然速度没有BYTE快
//对应的DWORD格式应为A--R--G--B(从高位到低位), 这与通用的标准是一致的, 如Java
//BYTE型数据格式(依次): B--G--R--A
BOOL CContrastProcess::ChangeContrast(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;
//开始数据基索引
DWORD dwBaseIndex = y * dwWidthBytes + 4 * x;
//第二步, 对比度修改
//被扩展或压缩到的区间的左端点
int nHigh = 255 - m_nIncrement;
//对于极端情况加以处理
if(nHigh < m_nIncrement)
{
nHigh = 127;
m_nIncrement = 120;
}
if(m_nIncrement < -127)m_nIncrement = -120;
//扩展或压缩区间的长度
int nStretch = 255;
if(m_nIncrement >= 0)
nStretch = 255 - 2 * m_nIncrement;
else
nStretch = 255 + 2 * m_nIncrement;
for(int i = 0;i < h;i++)
{
BYTE* pbyRsc = lpbyBits32 + dwBaseIndex;
for(int j = 0;j < w;j++)
{
BYTE* pbyBlue = pbyRsc++;
BYTE* pbyGreen = pbyRsc++;
BYTE* pbyRed = pbyRsc++;
pbyRsc++;
if(m_nIncrement >= 0)
IncreaseContrast(pbyBlue, pbyGreen, pbyRed, nHigh, nStretch);
else
DecreaseContrast(pbyBlue, pbyGreen, pbyRed, nStretch);
}
dwBaseIndex += dwWidthBytes;
}
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -