VC++的源程序如下;
// txclView.cpp : implementation of the CTxclView class //
#include \#include \
#include \#include \
#ifdef _DEBUG
#define new DEBUG_NEW #undef THIS_FILE
static char THIS_FILE[] = __FILE__; #endif
///////////////////////////////////////////////////////////////////////////// // CTxclView
int m_yuan[256],m_hist[256];
IMPLEMENT_DYNCREATE(CTxclView, CView)
BEGIN_MESSAGE_MAP(CTxclView, CView) //{{AFX_MSG_MAP(CTxclView) ON_COMMAND(OPEN, OnOPEN) ON_COMMAND(His, OnHis) //}}AFX_MSG_MAP // Standard printing commands ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint) ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint) ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview) END_MESSAGE_MAP()
///////////////////////////////////////////////////////////////////////////// // CTxclView construction/destruction
CTxclView::CTxclView() { // TODO: add construction code here }
CTxclView::~CTxclView()
{ }
BOOL CTxclView::PreCreateWindow(CREATESTRUCT& cs) { // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs return CView::PreCreateWindow(cs); }
///////////////////////////////////////////////////////////////////////////// // CTxclView drawing
void CTxclView::OnDraw(CDC* pDC) { CTxclDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); // TODO: add draw code for native data here if(m_dib.m_bLoaded==true) //判断是否加载图像 {
//获取图像宽和高
int nw=m_dib.GetDIBWidth(); int nh=m_dib.GetDIBHeight();
// 显示图像(具体的参数见CDIB类的该函数说明)
m_dib.ShowDIB(pDC,10,10,nw,nh,m_dib.m_pDIBData,m_dib.m_pBMI);
m_dib.ShowDIB(pDC,400,10,nw,nh,m_dib.m_pDumpDIBData,m_dib.m_pBMI); } }
///////////////////////////////////////////////////////////////////////////// // CTxclView printing
BOOL CTxclView::OnPreparePrinting(CPrintInfo* pInfo) { // default preparation return DoPreparePrinting(pInfo); }
void CTxclView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add extra initialization before printing
}
void CTxclView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add cleanup after printing }
///////////////////////////////////////////////////////////////////////////// // CTxclView diagnostics
#ifdef _DEBUG
void CTxclView::AssertValid() const { CView::AssertValid(); }
void CTxclView::Dump(CDumpContext& dc) const { CView::Dump(dc); }
CTxclDoc* CTxclView::GetDocument() // non-debug version is inline { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTxclDoc))); return (CTxclDoc*)m_pDocument; }
#endif //_DEBUG
///////////////////////////////////////////////////////////////////////////// // CTxclView message handlers
void CTxclView::OnOPEN() { // TODO: Add your command handler code here
static char szFilter[]=\文件(*.bmp)|*.bmp||\ //定义过滤文件的类型 CFileDialog dlg(TRUE,\ OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,szFilter);//定义文件对话框对象 CString filename;
int ret=dlg.DoModal(); //运行打开文件对方框 if(ret==IDOK) { filename=dlg.GetFileName(); //获取所选择图像的路径 m_dib.LoadFromFile(filename); //加载图像
if(!m_dib.m_bLoaded) //判断是否加载图像成功 { AfxMessageBox(\图像打不开\ return; } }
Invalidate(1); //刷新屏幕 }
void CTxclView::OnHis() { // TODO: Add your command handler code here //////////////////////////
//功能:实现直方图均衡化 //////////////////////////
//判断图像是否打开,没打开,则弹出提示框并退出函数 if(!m_dib.m_bLoaded) { AfxMessageBox(\别急呀,还没打开原图呢,啦啦啦啦~\ return; }
//获取图像宽和高 int nw=m_dib.GetDIBWidth(); int nh=m_dib.GetDIBHeight(); int i,j,k; int count[256]={0};//定义一个数组,存放灰度级个数 float p[256]={0};//定义一个数组,存放灰度级频率 for(i=0;i for(k=0;k<256;k++) p[k]=count[k]/(nw*nh*1.0f);//计算灰度级频率 float c[256]={0}; float sum=0.0; int ngray[256]={0}; for(k=0;k<256;k++) { sum+=p[k]; } c[k]=sum; //计算累计直方图 ngray[k]=(int)(255*c[k]+0.5);//确定映射关系 for(i=0;i m_dib.m_pdata[i*nw+j]=ngray[k]; //均衡化 } if(m_dib.m_bLoaded==true) { int nw=m_dib.GetDIBWidth(); int nh=m_dib.GetDIBHeight(); for(int j=0;j //将修改的m_pdata的数据赋值给m_pDIBData,以显示修改的结果 m_dib.UpdateData(); //刷新屏幕 Invalidate(); } CDIB程序; #if !defined (__DIB_H__) #define __DIB_H__ #if _MSC_VER>=1000 #pragma once