www.pudn.com > partsumhist.rar > practiceView.cpp
// practiceView.cpp : implementation of the CPracticeView class
//
#include "stdafx.h"
#include "practice.h"
#include "practiceDoc.h"
#include "practiceView.h"
#include "HSI.h"
#include "math.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define BITMAP_ID 0x4D42 // universal id for a bitmap
///////////////////////////////////
// GLOBALS
HBITMAP g_hBitmap=NULL;
CString g_csFileName; // current open file name
int g_nMapWidth=0;
int g_nMapHeight=0;
//bool g_bImgBufferChanged;
//RGB spec_rgb = { 0xff, 0x00, 0x00 }; // 标识色
RGB * g_pImgBuffer; // 全局的图像数据
//BYTE * g_pSobelResult; // Soble运算的结果
HSI * g_pHSIBuffer; // 全局的HSI数据
FLAGS * g_pFlags; // 全局的标志量
FLAGS * g_pFlagsBack; // 全局的标志量
/*bool g_bDir4Ero=false;
bool g_bDir4Dil=false;
long g_nCellCount=0;
long g_nCellTotArea=0;*/
CPoint scroll_lefttop;
/////////////////////////////////////////////////////////////////////////////
// CPracticeView
IMPLEMENT_DYNCREATE(CPracticeView, CScrollView)
BEGIN_MESSAGE_MAP(CPracticeView, CScrollView)
//{{AFX_MSG_MAP(CPracticeView)
ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
ON_COMMAND(ID_proc_histogram, OnProcHistogram)
ON_COMMAND(ID_proc_sumhist, Onprocsumhist)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CScrollView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPracticeView construction/destruction
CPracticeView::CPracticeView()
{
// TODO: add construction code here
g_csFileName = "";
g_pImgBuffer = NULL;
// g_pOrgImgBuffer = NULL;
// g_pImgBufferBack = NULL;
g_pHSIBuffer = NULL;
g_pFlags = NULL;
g_pFlagsBack = NULL;
CSize sz(g_nMapWidth,g_nMapHeight);
SetScrollSizes(MM_TEXT, sz);
}
CPracticeView::~CPracticeView()
{
if(g_hBitmap)
DeleteObject(g_hBitmap);
if(g_pImgBuffer)
delete[] g_pImgBuffer;
// if (g_pOrgImgBuffer)
// delete[] g_pOrgImgBuffer;
// if (g_pImgBufferBack)
// delete[] g_pImgBufferBack;
if(g_pHSIBuffer)
delete[] g_pHSIBuffer;
if(g_pFlags)
delete[] g_pFlags;
if (g_pFlagsBack)
delete[] g_pFlagsBack;
}
BOOL CPracticeView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CPracticeView drawing
void CPracticeView::OnDraw(CDC* pDC)
{
CPracticeDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
if(g_hBitmap)
{
scroll_lefttop=GetScrollPosition();
RECT rect;
GetClientRect(&rect);
int drawwd,drawht;
if (g_nMapWidth>rect.right-rect.left)
drawwd=rect.right-rect.left+1;
else drawwd=g_nMapWidth;
if (g_nMapHeight>rect.bottom-rect.top)
drawht=rect.bottom-rect.top+1;
else drawht=g_nMapHeight;
CDC *pdc = GetDC();
HDC dc = pdc->m_hDC;
HDC memdc = ::CreateCompatibleDC(dc);
::SelectObject(memdc, g_hBitmap);
::BitBlt(dc, 0, 0, drawwd, drawht, memdc, scroll_lefttop.x, scroll_lefttop.y, SRCCOPY);
ReleaseDC(pdc);
::DeleteDC(memdc);
}
}
/////////////////////////////////////////////////////////////////////////////
// CPracticeView printing
BOOL CPracticeView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CPracticeView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CPracticeView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CPracticeView diagnostics
#ifdef _DEBUG
void CPracticeView::AssertValid() const
{
CView::AssertValid();
}
void CPracticeView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CPracticeDoc* CPracticeView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CPracticeDoc)));
return (CPracticeDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CPracticeView message handlers
void CPracticeView::FlipBitmapData(RGB *buffer)
{
RGB *tempmem;
int bytes_per_line=g_nMapWidth*sizeof(RGB);
tempmem=new RGB[g_nMapWidth*g_nMapHeight];
memcpy(tempmem,buffer,bytes_per_line*g_nMapHeight);
for (int i=0;iDoModal() == IDOK)
{
szFilename = FileDlg->GetPathName();
if(!szFilename.IsEmpty())
{
HANDLE handle;
handle = LoadImage(theApp.m_hInstance, szFilename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
if(handle)
{
if(g_hBitmap)
DeleteObject(g_hBitmap);
InvalidateRect(0, TRUE);
g_hBitmap = (struct HBITMAP__ *) handle;
g_csFileName = FileDlg->GetFileName();
theApp.m_pMainWnd->SetWindowText((LPCTSTR) (g_csFileName + " #ImageProc#"));
}
else
{
MessageBox("无法打开文件");
return;
}
CDC *pdc = GetDC();
HDC dc = pdc->m_hDC;
HDC memdc = ::CreateCompatibleDC(dc);
CBitmap::FromHandle(g_hBitmap)->GetBitmap(&bmp);
::SelectObject(memdc, g_hBitmap);
g_nMapWidth=bmp.bmWidth;
g_nMapHeight=bmp.bmHeight;
// 设置滚动条
CSize sz(g_nMapWidth,g_nMapHeight);
SetScrollSizes(MM_TEXT, sz);
// fill g_pImgBuffer
if (LoadImageBuffer(memdc,(LPSTR)(LPCTSTR)g_csFileName)==0)
return;
::BitBlt(dc, 0, 0, g_nMapWidth, g_nMapHeight, memdc, 0, 0, SRCCOPY);
ReleaseDC(pdc);
::DeleteDC(memdc);
}
}
delete FileDlg;
}
void CPracticeView::OnProcHistogram()
{
if(g_hBitmap)
{
CHistogramDlg hDlg;
if(hDlg.DoModal() == IDOK)
{
// backup
// memcpy(g_pImgBufferBack,g_pImgBuffer,
// sizeof(RGB)*g_nMapWidth*g_nMapHeight);
memcpy(g_pFlagsBack,g_pFlags,
sizeof(FLAGS)*g_nMapWidth*g_nMapHeight);
// g_bImgBufferChanged=true;
FLAGS *cur_flag = g_pFlags;
RGB *cur=g_pImgBuffer;
for(int j = 0; j < g_nMapHeight; j++)
{
for(int i = 0; i < g_nMapWidth; i++)
{
if(!(cur_flag->marked))
{
if ( (cur->b >= 0 && cur->b <= 255)
&& (cur->r >= 0 && cur->r <= 255)
&& (cur->g >= 0 && cur->g <= 255))
{
cur_flag->marked = 1;
// cur->r=cur->g=cur->b=0; // 不改变了原图了!
}
}
cur_flag++;
cur++;
}
}
InvalidateRect(0, TRUE);
}
}
else MessageBox("请先打开图像文件!");
}
void CPracticeView::Onprocsumhist()
{
// TODO: Add your command handler code here
if(g_hBitmap)
{
CHistogramDlg hDlg;
if(hDlg.DoModal() == IDOK)
{
// backup
// memcpy(g_pImgBufferBack,g_pImgBuffer,
// sizeof(RGB)*g_nMapWidth*g_nMapHeight);
memcpy(g_pFlagsBack,g_pFlags,
sizeof(FLAGS)*g_nMapWidth*g_nMapHeight);
// g_bImgBufferChanged=true;
FLAGS *cur_flag = g_pFlags;
RGB *cur=g_pImgBuffer;
for(int j = 0; j < g_nMapHeight; j++)
{
for(int i = 0; i < g_nMapWidth; i++)
{
if(!(cur_flag->marked))
{
if ( (cur->b >= 0 && cur->b <= 255)
&& (cur->r >= 0 && cur->r <= 255)
&& (cur->g >= 0 && cur->g <= 255))
{
cur_flag->marked = 1;
// cur->r=cur->g=cur->b=0; // 不改变了原图了!
}
}
cur_flag++;
cur++;
}
}
InvalidateRect(0, TRUE);
}
}
else MessageBox("请先打开图像文件!");
}