Code:
// DialogBrightnessContrast.cpp : implementation file
//
#include "stdafx.h"
#include <math.h>
#include "DialogBrightnessContrast.h"
// CDialogBrightnessContrast dialog
IMPLEMENT_DYNAMIC(CDialogBrightnessContrast, CDialog)
CDialogBrightnessContrast::CDialogBrightnessContrast(CWnd* pParent /*=NULL*/)
: CDialog(CDialogBrightnessContrast::IDD, pParent)
, m_nBrightness(100)
, nBrightness(0)
, m_nContrast(100)
, nContrast(0)
, m_bPreview(FALSE)
{
}
CDialogBrightnessContrast::~CDialogBrightnessContrast()
{
}
void CDialogBrightnessContrast::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Text(pDX, IDC_EDIT_BRIGHTNESS, nBrightness);
DDX_Slider(pDX, IDC_SLIDER_BRIGHTNESS, m_nBrightness);
DDX_Text(pDX, IDC_EDIT_CONTRAST, nContrast);
DDX_Slider(pDX, IDC_SLIDER_CONTRAST, m_nContrast);
DDV_MinMaxInt(pDX, nContrast, -100, 100);
DDV_MinMaxInt(pDX, nBrightness, -100, 100);
DDX_Check(pDX, IDC_CHECK_PREVIEW, m_bPreview);
}
BEGIN_MESSAGE_MAP(CDialogBrightnessContrast, CDialog)
ON_WM_HSCROLL()
ON_BN_CLICKED(IDC_CHECK_PREVIEW, OnBnClickedCheckPreview)
ON_EN_CHANGE(IDC_EDIT_BRIGHTNESS, OnEnChangeEdit)
ON_EN_CHANGE(IDC_EDIT_CONTRAST, OnEnChangeEdit)
ON_WM_PAINT()
END_MESSAGE_MAP()
// CDialogBrightnessContrast message handlers
BOOL CDialogBrightnessContrast::OnInitDialog()
{
CDialog::OnInitDialog();
CSliderCtrl * pSlider1 = (CSliderCtrl*)GetDlgItem(IDC_SLIDER_BRIGHTNESS);
pSlider1->SetRange(0, 200);
pSlider1->SetPos(100);
pSlider1->SetTicFreq(10);
pSlider1->SetTic(10);
CSliderCtrl * pSlider2 = (CSliderCtrl*)GetDlgItem(IDC_SLIDER_CONTRAST);
pSlider2->SetRange(0, 200);
pSlider2->SetPos(100);
pSlider2->SetTicFreq(10);
pSlider2->SetTic(10);
CPaintDC dc(this);
// I am loading from IDB_YY resource
// do some thing to get a sample from your full image here.
// Source
m_bmSource.LoadBitmap(IDB_YY);
m_dcSource.CreateCompatibleDC(&dc);
m_dcSource.SelectObject(&m_bmSource);
// Off-Screen
m_bmOffScreen.LoadBitmap(IDB_YY);
m_dcOffScreen.CreateCompatibleDC(&dc);
m_dcOffScreen.SelectObject(m_bmOffScreen);
m_bmSource.GetBitmap(&m_bmp);
UpdateData(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CDialogBrightnessContrast::OnOK()
{
UpdateData(TRUE);
CDialog::OnOK();
}
void CDialogBrightnessContrast::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
UpdateData(TRUE);
nBrightness = m_nBrightness - 100;
nContrast = m_nContrast - 100;
UpdateData(FALSE);
BrightnessAndContrast();
CDialog::OnHScroll(nSBCode, nPos, pScrollBar);
}
void CDialogBrightnessContrast::OnBnClickedCheckPreview()
{
UpdateData(TRUE);
}
void CDialogBrightnessContrast::OnEnChangeEdit()
{
UpdateData(TRUE);
m_nBrightness = nBrightness + 100;
m_nContrast = nContrast + 100;
UpdateData(FALSE);
BrightnessAndContrast();
}
void CDialogBrightnessContrast::OnPaint()
{
CPaintDC dc(this);
// I am doing it at 5, 5
// you can create a frame in the dialog resource and use that frame Rect here.
dc.BitBlt(5, 5, m_bmp.bmWidth, m_bmp.bmHeight, &m_dcOffScreen, 0, 0, SRCCOPY);
}
void CDialogBrightnessContrast::BrightnessAndContrast()
{
const int GREY = 0x7f;
int nStepB = (int)floor(nBrightness * 0.01 * GREY);
double dStepC = m_nContrast * 0.01;
if (m_nContrast > 99)
{
dStepC = dStepC * dStepC * dStepC;
}
TRACE("nBrightness(%d) Contrast(%f)\n", nStepB, dStepC );
for (m_nY = 0; m_nY < m_bmp.bmHeight; m_nY++) {
for (m_nX = 0; m_nX < m_bmp.bmWidth; m_nX++) {
m_crC = m_dcSource.GetPixel(m_nX, m_nY);
// RED
m_nC = GetRValue(m_crC) + nStepB; // for brightness
m_nC = (int)floor((m_nC - GREY) * dStepC) + GREY; // for contrast
// add tint, invert.. whatever here
m_nR = (m_nC < 0x00) ? 0x00 : (m_nC > 0xff) ? 0xff : m_nC;
// GREEN
m_nC = GetGValue(m_crC) + nStepB;
m_nC = (int)floor((m_nC - GREY) * dStepC) + GREY;
m_nG = (m_nC < 0x00) ? 0x00 : (m_nC > 0xff) ? 0xff : m_nC;
// BLUE
m_nC = GetBValue(m_crC) + nStepB;
m_nC = (int)floor((m_nC - GREY) * dStepC) + GREY;
m_nB = (m_nC < 0x00) ? 0x00 : (m_nC > 0xff) ? 0xff : m_nC;
m_crC = m_nR + (m_nG << 8) + (m_nB << 16);
m_dcOffScreen.SetPixel(m_nX, m_nY, m_crC);
}
}
RedrawWindow(NULL, NULL, RDW_INVALIDATE);
}