www.pudn.com > LlxDip.rar > DlgTr.cpp


// DlgTr.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "LlxDip.h" 
#include "DlgTr.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CDlgTr dialog 
 
 
CDlgTr::CDlgTr(CWnd* pParent /*=NULL*/) 
	: CDialog(CDlgTr::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CDlgTr) 
		// NOTE: the ClassWizard will add member initialization here 
	//}}AFX_DATA_INIT 
} 
 
 
void CDlgTr::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CDlgTr) 
		// NOTE: the ClassWizard will add DDX and DDV calls here 
	//}}AFX_DATA_MAP 
} 
 
 
BEGIN_MESSAGE_MAP(CDlgTr, CDialog) 
	//{{AFX_MSG_MAP(CDlgTr) 
	ON_WM_PAINT() 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CDlgTr message handlers 
BOOL CDlgTr::OnInitDialog()  
{ 
	// 指向源图像象素的指针 
	unsigned char * lpSrc; 
	 
	// 循环变量 
	LONG i; 
	LONG j; 
	 
	// 调用默认OnInitDialog函数 
	CDialog::OnInitDialog(); 
	 
	// 重置计数为0 
	for (i = 0; i < 256; i ++) 
	{ 
		// 清零 
		m_lCount[i] = 0; 
		m_lfCount[i] = 0; 
		m_lfTr[i] = 0; 
	} 
	 
	// 图像每行的字节数 
	LONG lLineBytes; 
	 
	// 计算图像每行的字节数 
	lLineBytes = WIDTHBYTES(m_lWidth * 8); 
	 
	// 计算各个灰度值的计数 
	for (i = 0; i < m_lHeight; i ++) 
	{ 
		for (j = 0; j < m_lWidth; j ++) 
		{ 
			lpSrc = (unsigned char *)m_lpDIBBits + lLineBytes * i + j; 
			 
			// 计数加1 
			m_lCount[*(lpSrc)]++; 
		} 
	} 
	 
	// 计算变换曲线 
	for (i = 0; i < 256; i++) 
	{ 
		// 原频率 
		m_lfCount[i] = (double)m_lCount[i] / (double)( m_lHeight * m_lWidth ); 
 
		// 目标频率 
		for (j = 0; j <= i; j++) 
		{ 
			m_lfTr[i] += m_lfCount[j]; 
		} 
	} 
 
	return TRUE;  // return TRUE unless you set the focus to a control 
	              // EXCEPTION: OCX Property Pages should return FALSE 
} 
 
void CDlgTr::OnPaint()  
{ 
	// 字符串 
	CString str; 
	 
	// 循环变量 
	LONG i; 
	 
	// 最大计数 
	LONG lMaxCount = 0; 
	 
	// 设备上下文 
	CPaintDC dc(this); 
	 
	// 获取绘制坐标的文本框 
	CWnd* pWnd = GetDlgItem(IDC_COORD); 
	 
	// 指针 
	CDC* pDC = pWnd->GetDC(); 
	pWnd->Invalidate(); 
	pWnd->UpdateWindow(); 
	 
	pDC->Rectangle(0,0,330,300); 
	 
	// 创建画笔对象 
	CPen* pPenRed = new CPen; 
	 
	// 红色画笔 
	pPenRed->CreatePen(PS_SOLID,1,RGB(255,0,0)); 
	 
	// 创建画笔对象 
	CPen* pPenBlue = new CPen; 
	 
	// 蓝色画笔 
	pPenBlue->CreatePen(PS_SOLID,1,RGB(0,0, 255)); 
	 
	// 选中当前红色画笔,并保存以前的画笔 
	CGdiObject* pOldPen = pDC->SelectObject(pPenRed); 
	 
	// 绘制坐标轴 
	pDC->MoveTo(30,10); 
	 
	// 垂直轴 
	pDC->LineTo(30,280); 
	 
	// 水平轴 
	pDC->LineTo(320,280); 
 
	// 写Y轴刻度值 
	str.Format("0.0"); 
	pDC->TextOut(5, 270, str); 
	str.Format("0.1"); 
	pDC->TextOut(5, 250, str); 
	str.Format("0.2"); 
	pDC->TextOut(5, 230, str); 
	str.Format("0.3"); 
	pDC->TextOut(5, 210, str); 
	str.Format("0.4"); 
	pDC->TextOut(5, 190, str); 
	str.Format("0.5"); 
	pDC->TextOut(5, 170, str); 
	str.Format("0.6"); 
	pDC->TextOut(5, 150, str); 
	str.Format("0.7"); 
	pDC->TextOut(5, 130, str); 
	str.Format("0.8"); 
	pDC->TextOut(5, 110, str); 
	str.Format("0.9"); 
	pDC->TextOut(5, 90, str); 
	str.Format("1.0"); 
	pDC->TextOut(5, 70, str); 
 
	// 写X轴刻度值 
	str.Format("0"); 
	pDC->TextOut(30, 283, str); 
	str.Format("50"); 
	pDC->TextOut(80, 283, str); 
	str.Format("100"); 
	pDC->TextOut(130, 283, str); 
	str.Format("150"); 
	pDC->TextOut(180, 283, str); 
	str.Format("200"); 
	pDC->TextOut(230, 283, str); 
	str.Format("255"); 
	pDC->TextOut(285, 283, str); 
 
	// 绘制XY轴刻度 
	for (i = 0; i < 256; i += 5) 
	{ 
		if ((i & 1) == 0) 
		{ 
			// 10的倍数 
			// X 
			pDC->MoveTo(i + 30, 280); 
			pDC->LineTo(i + 30, 284); 
			// Y 
			pDC->MoveTo(26, 280 - i); 
			pDC->LineTo(30, 280 - i); 
		} 
		else 
		{ 
			// 10的倍数 
			// X 
			pDC->MoveTo(i + 30, 280); 
			pDC->LineTo(i + 30, 282); 
			// Y 
			pDC->MoveTo(28, 280 - i); 
			pDC->LineTo(30, 280 - i); 
		} 
	} 
	 
	// 绘制X轴箭头 
	pDC->MoveTo(315,275); 
	pDC->LineTo(320,280); 
	pDC->LineTo(315,285); 
	 
	// 绘制Y轴箭头 
	pDC->MoveTo(30,10); 
	pDC->LineTo(25,15); 
	pDC->MoveTo(30,10); 
	pDC->LineTo(35,15); 
	 
	// 更改成蓝色画笔 
	pDC->SelectObject(pPenBlue);	 
	 
	// 绘制变换曲线 
	pDC->MoveTo(30,280-(int)(m_lfTr[0]*200+0.5)); 
	for (i = 1; i < 256; i++) 
	{ 
		pDC->LineTo(i+30,280-(int)(m_lfTr[i]*200+0.5)); 
	} 
 
	// 恢复以前的画笔 
	pDC->SelectObject(pOldPen); 
	 
	// 删除新的画笔 
	delete pPenRed; 
	delete pPenBlue; 
	 
	// Do not call CDialog::OnPaint() for painting messages 
} 
 
int CDlgTr::DoModal()  
{ 
	// TODO: Add your specialized code here and/or call the base class 
	 
	return CDialog::DoModal(); 
}