?? dipdoc.cpp
字號:
// dipDoc.cpp : implementation of the CDipDoc class
//
#include "stdafx.h"
#include "dip.h"
#include "afx.h"
#include "dipDoc.h"
#include "GreyDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CDipDoc
IMPLEMENT_DYNCREATE(CDipDoc, CDocument)
BEGIN_MESSAGE_MAP(CDipDoc, CDocument)
//{{AFX_MSG_MAP(CDipDoc)
ON_COMMAND(ID_grey, Ongrey)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDipDoc construction/destruction
CDipDoc::CDipDoc()
{
// TODO: add one-time construction code here
//初始化圖像數據和調色板
ImgWidth=0;
ImgHeight=0;
ImgData=NULL;
palette=NULL;
}
CDipDoc::~CDipDoc()
{
//釋放所分配的空間
if (ImgData) delete []ImgData;
if (palette) delete []palette;
}
BOOL CDipDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
// TODO: add reinitialization code here
// (SDI documents will reuse this document)
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CDipDoc serialization
void CDipDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
}
else
{
// TODO: add loading code here
}
}
/////////////////////////////////////////////////////////////////////////////
// CDipDoc diagnostics
#ifdef _DEBUG
void CDipDoc::AssertValid() const
{
CDocument::AssertValid();
}
void CDipDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CDipDoc commands
//打開文件名為filename的文件,并從中讀取信息
void CDipDoc::FileOpen(CString filename)
{
CString promt;//提示字符串
int flag=true;//標志是否成功的打開文件
CFile fp; //新建一個文件
BYTE num[4];
//打開文件
fp.Open(filename,CFile::modeRead|CFile::typeBinary);
if (fp==NULL)
{
promt="Can't Open file "+filename;
AfxMessageBox(promt);
flag=false;
}
//保存圖像信息
fp.Read(header,14); //位圖文件頭
fp.Read(infoheader,40);
//位圖信息頭
//讀取biClrUsed色彩數目
fp.Seek(46,CFile::begin);
fp.Read(num,4);
colorused=(num[3]<<24)|(num[2]<<16)|(num[1]<<8)|num[0];
//如果biClrUsed為0,則讀取biBitCount像素位數目
if (!colorused)
{
fp.Seek(28,CFile::begin);
fp.Read(num,2);
colorused=(num[1]<<8)|num[0];
colorused=1<<colorused; //色彩數目為2的像素位數目次方
}
//刪除原有調色板
if (palette) delete []palette;
palette=new RGBQUAD[colorused];
//讀取新調色板
fp.Seek(54,CFile::begin);
fp.Read(palette,colorused*sizeof(palette));
//判斷是否是BMP圖像
WORD ImgType;
fp.SeekToBegin();
fp.Read(&ImgType,1);
if (ImgType!=((('M'+127)*256)+'B'))
{
promt="file "+filename+" is not a BMP file";
AfxMessageBox(promt);
flag=false;
}
//判斷是否是256色位圖
BYTE ImgBitCount;
fp.Seek(28,CFile::begin);
fp.Read(&ImgBitCount,1);
if (ImgBitCount!=8)
{
promt="file "+filename+" is not a Greyscale";
AfxMessageBox(promt);
flag=false;
}
//符合條件,則讀取文件信息
if (flag)
{
//更新文件名信息
FileName=filename;
//讀取圖像的寬度和高度
fp.Seek(18,CFile::begin);
fp.Read(num,4);
ImgWidth=(num[3]<<24)|(num[2]<<16)|(num[1]<<8)|num[0];
fp.Read(num,4);
ImgHeight=(num[3]<<24)|(num[2]<<16)|(num[1]<<8)|num[0];
//刪除原有的數據區,并根據圖像大小重新分配數據區
if (ImgData) delete [] ImgData;
ImgData=new BYTE[ImgWidth*ImgHeight];
//讀區數據區開始的偏移量
LONG offset;
fp.Seek(10,CFile::begin);
fp.Read(num,4);
offset=(num[3]<<24)|(num[2]<<16)|(num[1]<<8)|num[0];
fp.Seek(offset,CFile::begin);
//每次讀取一行的像素點,并刪除添加的冗余點
int i;
int temp=ImgWidth%4;
if (temp) temp=4-temp;
//注意,是從下往上,從左往右來
for(i=0;i<ImgHeight;i++)
{
fp.Read(ImgData+(ImgHeight-i-1)*ImgWidth,ImgWidth);
fp.Seek(temp,CFile::current);
}
}
fp.Close(); //讀取結束,關閉文件
}
//計算圖像的灰度直方圖
void CDipDoc::Ongrey()
{
// TODO: Add your command handler code here
int i;
CGreyDlg greyDlg;
for(i=0;i<ImgWidth*ImgHeight;i++)
greyDlg.grey[ImgData[i]]++; //統計灰度信息
greyDlg.DoModal(); //顯示灰度直方圖對話框
}
//將現有的圖像存入文件filename中
int CDipDoc::FileSave(CString filename)
{
CFile fp;
//打開文件
fp.Open(filename,CFile::modeWrite|CFile::typeBinary|CFile::modeCreate);
if (!fp)
{
AfxMessageBox("Write error!");
return(0); //出錯返回
}
//寫入原圖像的頭部信息
fp.Write(header,14);
fp.Write(infoheader,40);
fp.Write(palette,colorused*4);
//寫入當前圖像的數據信息
int i;
char c[4]={0,0,0,0};
int temp=ImgWidth%4;
if (temp) temp=4-temp;
for(i=0;i<ImgHeight;i++)
{
fp.Write(ImgData+(ImgHeight-i-1)*ImgWidth,ImgWidth);
if (temp)
fp.Write(c,temp); //添加冗余字段
}
//更新圖像的大小信息
DWORD length;
BYTE temp1;
fp.Seek(2,CFile::begin); //更新bfSize文件大小
length=fp.GetLength();
for(i=0;i<4;i++)
{
temp1=(BYTE)length%256;
length=length/256;
fp.Write(&temp1,1);
}
fp.Seek(18,CFile::begin); //更新biWidth圖像寬度
length=ImgWidth;
for(i=0;i<4;i++)
{
temp1=(BYTE)length%256;
length=length/256;
fp.Write(&temp1,1);
}
fp.Seek(22,CFile::begin); //更新biHeight圖像高度
length=ImgHeight;
for(i=0;i<4;i++)
{
temp1=(BYTE)length%256;
length=length/256;
fp.Write(&temp1,1);
}
fp.Close(); //關閉文件
return(1);
}
//重新打開默認的文件,并從中讀取圖像信息
void CDipDoc::FileOpen()
{
CString promt;//提示字符串
int flag=true;//標志是否成功的打開文件
CFile fp;
//打開文件
fp.Open(FileName,CFile::modeRead|CFile::typeBinary);
if (fp==NULL)
{
promt="Can't Open file "+FileName;
AfxMessageBox(promt);
flag=false;
}
//如果打開成功
if (flag)
{
BYTE num[4];
//讀取圖像的寬度和高度
fp.Seek(18,CFile::begin);
fp.Read(num,4);
ImgWidth=(num[3]<<24)|(num[2]<<16)|(num[1]<<8)|num[0];
fp.Read(num,4);
ImgHeight=(num[3]<<24)|(num[2]<<16)|(num[1]<<8)|num[0];
//刪除原有的數據區,并根據圖像大小重新分配數據區
if (ImgData) delete [] ImgData;
ImgData=new BYTE[ImgWidth*ImgHeight];
//讀區數據區開始的偏移量
LONG offset;
fp.Seek(10,CFile::begin);
fp.Read(num,4);
offset=(num[3]<<24)|(num[2]<<16)|(num[1]<<8)|num[0];
fp.Seek(offset,CFile::begin);
//每次讀取一行的像素點,并刪除添加的冗余點
int i;
int temp=ImgWidth%4;
if (temp) temp=4-temp;
for(i=0;i<ImgHeight;i++)
{
fp.Read(ImgData+(ImgHeight-i-1)*ImgWidth,ImgWidth);
fp.Seek(temp,CFile::current);
}
}
fp.Close();//讀取結束關閉文件
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -