📄 dipview.cpp
字号:
// DipView.cpp : implementation of the CDipView class
//
#include "stdafx.h"
#include "Dip.h"
#include "DipDoc.h"
#include "DipView.h"
#include "PointPro.h"
#include "DlgIntensity.h"
#include "memory.h"
#include "math.h"
#include "stdio.h"
#include "string.h"
#include "direct.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//macro definition
#define WIDTHBYTES(i) ((i+31)/32*4)
//global variable declaration
BITMAPFILEHEADER bf;
BITMAPINFOHEADER bi;
DWORD NumColors;
DWORD LineBytes;
DWORD ImgWidth=0 , ImgHeight=0;
BOOL bGray;
/////////////////////////////////////////////////////////////////////////////
// CDipView
IMPLEMENT_DYNCREATE(CDipView, CScrollView)
BEGIN_MESSAGE_MAP(CDipView, CScrollView)
//{{AFX_MSG_MAP(CDipView)
ON_COMMAND(ID_WIDTH_HEIGHT, OnWidthHeight)
ON_COMMAND(ID_ColorBits, OnColorBits)
ON_COMMAND(ID_FILENAME, OnFilename)
ON_COMMAND(ID_VIEW_HIST, OnViewHist)
ON_UPDATE_COMMAND_UI(ID_VIEW_HIST, OnUpdateViewHist)
ON_COMMAND(ID_COLOR_BIT, OnColorBit)
ON_COMMAND(ID_TXTYPE, OnTxtype)
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CDipView construction/destruction
CDipView::CDipView()
{
mpImgData=NULL;
mpPalette=NULL;
}
CDipView::~CDipView()
{
if(mpImgData!=NULL)
delete mpImgData;
if(mpPalette!=NULL)
delete mpPalette;
}
BOOL CDipView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CScrollView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CDipView drawing
void CDipView::OnDraw(CDC* pDC)
{
CDipDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
if( !pDoc->m_bImageLoaded)
{
pDoc->LoadImageToDocument();
}
//滚动窗口
CSize sizeTotal;
sizeTotal.cx = pDoc->m_pImageObject->GetWidth();
sizeTotal.cy = pDoc->m_pImageObject->GetHeight();
SetScrollSizes (MM_TEXT, sizeTotal);
//获取客户区尺寸
OnPrepareDC(pDC);
CRect Rect;
GetClientRect( &Rect );
//获取图像宽度及高度
int nImageWidth, nImageHeight;
nImageWidth = pDoc->m_pImageObject->GetWidth();
nImageHeight = pDoc->m_pImageObject->GetHeight();
//当图像实际尺寸小于窗口尺寸时,将图像放在客户区中间
int nX, nY;
if( nImageWidth < Rect.Width() )
nX = (Rect.Width() - nImageWidth ) / 2;
else
nX = 0;
if( nImageHeight < Rect.Height() )
nY = (Rect.Height() - nImageHeight ) / 2;
else
nY = 0;
pDoc->m_pImageObject->Draw(pDC, nX, nY);
}
void CDipView::OnInitialUpdate()
{
CScrollView::OnInitialUpdate();
CSize sizeTotal;
// TODO: calculate the total size of this view
sizeTotal.cx = sizeTotal.cy = 100;
SetScrollSizes(MM_TEXT, sizeTotal);
}
/////////////////////////////////////////////////////////////////////////////
// CDipView printing
BOOL CDipView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CDipView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CDipView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CDipView diagnostics
#ifdef _DEBUG
void CDipView::AssertValid() const
{
CScrollView::AssertValid();
}
void CDipView::Dump(CDumpContext& dc) const
{
CScrollView::Dump(dc);
}
CDipDoc* CDipView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDipDoc)));
return (CDipDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CDipView message handlers
void CDipView::OnWidthHeight()
{
int m_nImageHeight; //定义图象的宽度为整形变量
int m_nImageWidth; //定义图象的高度为整形变量
CDipDoc* pDoc = GetDocument();
m_nImageWidth = pDoc->m_pImageObject->GetWidth();//获取宽度
m_nImageHeight = pDoc->m_pImageObject->GetHeight();//获取高度
CString str1;
str1.Format("该图像的宽度×高度为:%d×%d 象素",m_nImageWidth,m_nImageHeight);
MessageBox(str1,"宽度和高度",MB_OK);
}
void CDipView::OnColorBits()
{
CDipDoc* pDoc = GetDocument();
int m_ColorBits;
m_ColorBits=pDoc->m_pImageObject->GetNumColors();
CString str4;
str4.Format(" 该图像的颜色数为:%d\n",m_ColorBits);
MessageBox(str4,"颜色数",MB_OK);
}
void CDipView::OnFilename()
{
CDipDoc* pDoc = GetDocument();
char *m_Filename;
m_Filename=pDoc->m_pImageObject->GetImageName();
CString str;
str.Format("图像的文件名为:%s",m_Filename);
MessageBox(str,"图象名称",MB_OK);
}
BOOL CDipView::LoadBmpFile(char *BmpFileName)
{
HFILE hf;
DWORD ImgSize;
if((hf=_lopen(BmpFileName,OF_READ))==HFILE_ERROR)
{
MessageBox("错误!!\nBMP文件转化TXT文件失败!");
return FALSE;
}
_lread(hf,(LPSTR)&bf,sizeof(BITMAPFILEHEADER));
_lread(hf,(LPSTR)&bi,sizeof(BITMAPINFOHEADER));
ImgWidth=bi.biWidth;
ImgHeight=bi.biHeight;
LineBytes=(DWORD)WIDTHBYTES(bi.biWidth*bi.biBitCount);
ImgSize=(DWORD)LineBytes*bi.biHeight;
if(bi.biClrUsed!=0)
NumColors=(DWORD)bi.biClrUsed;
else
switch(bi.biBitCount)
{
case 1:
NumColors=2;break;
case 4:
NumColors=16;break;
case 8:
NumColors=256;break;
case 16:
NumColors=0;break;
case 24:
NumColors=0;break;
case 32:
NumColors=0;break;
default:
AfxMessageBox("错误!无效的颜色数!");
_lclose(hf);
return FALSE;
}
if(bf.bfOffBits!=(DWORD)(NumColors*sizeof(RGBQUAD)+sizeof(BITMAPFILEHEADER)
+sizeof(BITMAPINFOHEADER)))
{
AfxMessageBox("错误!无效的颜色数!");
_lclose(hf);
return FALSE;
}
bf.bfSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+NumColors*sizeof(RGBQUAD)+ImgSize;
if((mpImgData=new BYTE[sizeof(BITMAPINFOHEADER)+NumColors*sizeof(RGBQUAD)+ImgSize])==NULL)
{
AfxMessageBox("错误!内存分配不够!");
_lclose(hf);
return FALSE;
}
_llseek(hf,sizeof(BITMAPFILEHEADER),FILE_BEGIN);
_hread(hf,(BYTE *)mpImgData,(long)sizeof(BITMAPINFOHEADER)
+(long)NumColors*sizeof(RGBQUAD)+ImgSize);
_lclose(hf);
mpPalette=new BYTE[NumColors*sizeof(RGBQUAD)];
memcpy(mpPalette,mpImgData+sizeof(BITMAPINFOHEADER),NumColors*sizeof(RGBQUAD));
bGray=TRUE;
int r,g,b;
BYTE *ptr=mpPalette;
for(DWORD i=0;i<NumColors;i++)
{
b=*ptr++;
g=*ptr++;
r=*ptr++;
if( !((r==g) &&(g==b)))
{
bGray=FALSE;break;
}
ptr++;
}
return TRUE;
}
BOOL CDipView::Bmp2TxtChar(char *BmpFileName)
{
DWORD OffBits;
BYTE *lpPtr,*pPal;
HFILE hf;
DWORD i, j, gindex,r,g,b;
int len;
char buf[655360],s[64];
if(mpImgData!=NULL)
delete mpImgData;
LoadBmpFile(BmpFileName);
OffBits=sizeof(BITMAPINFOHEADER)+NumColors*sizeof(RGBQUAD);
len=strlen(BmpFileName);
BmpFileName[len-4]=0;
strcat(BmpFileName,".txt");
hf=_lcreat(BmpFileName,0);
memset(buf,0,65536);
for(i=0;i<ImgHeight;i++){
lpPtr=(BYTE *)mpImgData+OffBits+(ImgHeight-i-1)*LineBytes;
for(j=0;j<ImgWidth;j++){
gindex=*lpPtr++;
if(bGray){ //灰度图
sprintf(s,"%3d,",gindex);
strcat(buf,s);
}
else{ //彩色图
pPal=mpPalette+gindex*sizeof(RGBQUAD);
b=*pPal++;
g=*pPal++;
r=*pPal++;
sprintf(s,"(%3d,%3d,%3d) ",r,g,b);
strcat(buf,s);
}
}
strcat(buf,"\r\n");
}
_lwrite(hf,buf,strlen(buf));
_lclose(hf);
return TRUE;
}
void CDipView::OnViewHist()
{
CDipDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
//判断当前是否有图像对象
if( pDoc->m_pImageObject == NULL ) return;
//在点处理CPointPro类中创建用来绘制直方图的数据
CPointPro PointOperation( pDoc->m_pImageObject );
int *pHistogram = PointOperation.GetHistogram();
//生成一个对话框CHistDlg类的实例
CDlgIntensity HistDlg;
//将绘制直方图的数据传递给CHistDlg对话框类的公有成员变量m_pnHistogram
if( pHistogram != NULL )
{
//设置直方图数据指针
HistDlg.m_pnHistogram = pHistogram;
//设置当前像素值为0的像素数
HistDlg.m_nCurrentPixelsNum = pHistogram[0];
//设置是否为256级灰度图像
HistDlg.m_bIsGray256 = PointOperation.IsGray256();
}
//显示对话框
if ( HistDlg.DoModal() != IDOK)
return;
delete [] pHistogram;
}
void CDipView::OnUpdateViewHist(CCmdUI* pCmdUI)
{
CDipDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
pCmdUI->Enable( pDoc->m_pImageObject->GetNumBits() >= 8);
}
void CDipView::OnColorBit()
{
CDipDoc* pDoc = GetDocument();
//ColorBit = pDoc->m_pImageObject->GetNumColors();
m_ColorBit = pDoc->m_pImageObject->GetPlanes();
m_picture.Format("位平面数为%d ",m_ColorBit);
MessageBox(m_picture,"位平面数:");
}
void CDipView::OnTxtype()
{
// TODO: Add your command handler code here
CDipDoc* pDoc = GetDocument();
int m_bfType; // 定义获取图象类型函数GetImageType()的返回值为整形
m_bfType=pDoc->m_pImageObject->GetImageType();
CString str;
switch(m_bfType)
{
case 0: str.Format("其它类型");break;
case 1: str.Format("文件类型为位图(.BMP)");break;
case 2: str.Format("文件类型为GIF图象(.GIF)");break;
case 3: str.Format("文件类型为PCX图象(.PCX)");break;
case 4: str.Format("文件类型为TGA图象(.TGA)");break;
case 5: str.Format("文件类型为JPG图象(.JPG)");break;
case 6: str.Format("文件类型为TIF图象(.TIF)");break;
default:str.Format("文件类型错!");break;
}
MessageBox(str,"类型信息:",MB_OK);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -