www.pudn.com > resize.rar > resizeView.cpp
// resizeView.cpp : implementation of the CResizeView class
//
#include "stdafx.h"
#include "resize.h"
#include "resizeDoc.h"
#include "resizeView.h"
#include "ProptDlg.h"
#include "Mmsystem.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#pragma comment (lib,"Winmm.lib")
/////////////////////////////////////////////////////////////////////////////
// CResizeView
IMPLEMENT_DYNCREATE(CResizeView, CView)
BEGIN_MESSAGE_MAP(CResizeView, CView)
//{{AFX_MSG_MAP(CResizeView)
ON_COMMAND(ID_LOAD, OnLoad)
ON_COMMAND(ID_RESIZE, OnResize)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CResizeView construction/destruction
CResizeView::CResizeView()
{
// TODO: add construction code here
bload=false;
}
CResizeView::~CResizeView()
{
if (bload)
FreeMemory();
}
BOOL CResizeView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CResizeView drawing
void CResizeView::OnDraw(CDC* pDC)
{
CResizeDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
if (!bload)
return;
StretchDIBits(pDC->m_hDC,0,0,nWidth,nHeight,0,0,nWidth,nHeight,
lpbit,lpbih,DIB_RGB_COLORS,SRCCOPY);
}
/////////////////////////////////////////////////////////////////////////////
// CResizeView diagnostics
#ifdef _DEBUG
void CResizeView::AssertValid() const
{
CView::AssertValid();
}
void CResizeView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CResizeDoc* CResizeView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CResizeDoc)));
return (CResizeDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CResizeView message handlers
#define QWIDTHBYTES(cx, bit) (((bit * cx + 31) & ~31) >> 3)
void CResizeView::OnLoad()
{
// TODO: Add your command handler code here
CFile fp;
CString strName;
CFileDialog dlg(true);
dlg.m_ofn.lpstrFilter = "图象数据文件 (*.bmp)\0*.bmp\0\0";
if (dlg.DoModal() != 1)
return;
if (bload)
FreeMemory();
Invalidate(0);
//Sleep(500);
strName = dlg.GetPathName();
fp.Open(strName,CFile::modeRead);
lpbfh = new BITMAPFILEHEADER;
fp.Read(lpbfh,sizeof(BITMAPFILEHEADER));
lpbih = (BITMAPINFO *)new BYTE [sizeof(BITMAPINFOHEADER)+256*sizeof(RGBQUAD)];
fp.Read(lpbih,sizeof(BITMAPINFOHEADER)+256*sizeof(RGBQUAD));
nWidth = lpbih->bmiHeader.biWidth;
nHeight = lpbih->bmiHeader.biHeight;
nWidth = QWIDTHBYTES(nWidth,8);
int bmpsize = nWidth*nHeight;
lpbit = new BYTE [bmpsize];
fp.ReadHuge(lpbit,bmpsize);
fp.Close();
Invalidate(1);
bload=true;
}
void CResizeView::FreeMemory()
{
delete lpbfh;
lpbfh=NULL;
delete [] lpbih;
lpbih=NULL;
delete [] lpbit;
lpbit=NULL;
}
void CResizeView::OnResize()
{
// TODO: Add your command handler code here
if (!bload)
return;
CProptDlg dlg;
if (dlg.DoModal()!=true)
return;
if(dlg.m_prpt==100)
return;
DWORD a = timeGetTime();
float fProportion = (float)dlg.m_prpt/100.f;
int tempw=nWidth,temph=nHeight;
BYTE * temp = new BYTE [tempw*temph];
memcpy(temp,lpbit,tempw*temph);
delete [] lpbit;
lpbit=NULL;
nWidth = (int)((float)nWidth*fProportion+0.5);
nWidth = QWIDTHBYTES(nWidth,8);
nHeight = (int)((float)nHeight*fProportion+0.5);
lpbit = new BYTE [nWidth*nHeight];
for (int i=0;i=temph || w>=tempw)
*(lpbit+k)=0;
else
*(lpbit+k)=*(temp+l);
}
delete [] temp;
lpbih->bmiHeader.biHeight=nHeight;
lpbih->bmiHeader.biWidth=nWidth;
DWORD b = timeGetTime();
CString str;
str.Format("time:%d",b-a);
AfxMessageBox(str);
Invalidate(1);
}