⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 imagedoc.cpp

📁 segmentation sample good luck
💻 CPP
字号:
// ImageDoc.cpp : implementation of the CImageDoc class
//

#include "stdafx.h"
#include "Segment.h"

#include "ImageDoc.h"
#include "MonochromeBitmap.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CImageDoc

IMPLEMENT_DYNAMIC(CImageDoc, CDocument)

BEGIN_MESSAGE_MAP(CImageDoc, CDocument)
	//{{AFX_MSG_MAP(CImageDoc)
	ON_COMMAND(ID_FILE_EXPORT, OnFileExport)
	ON_UPDATE_COMMAND_UI(ID_FILE_EXPORT, OnUpdateFileExport)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CImageDoc construction/destruction

CImageDoc::CImageDoc()
{
	// TODO: add one-time construction code here

}

CImageDoc::~CImageDoc()
{
}

/////////////////////////////////////////////////////////////////////////////
// CImageDoc diagnostics

#ifdef _DEBUG
void CImageDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CImageDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CImageDoc commands



int CImageDoc::Width()
{
	 return GetMonochromeBitmap().Width();
}

int CImageDoc::Height()
{
	return GetMonochromeBitmap().Height();
}



void CImageDoc::OnFileExport() 
{
	CFileDialog dlg(FALSE,"bmp",NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,
					"Bitmap Files (*.bmp)|*.bmp||");
	if (dlg.DoModal()) {
		try {
			CFile file(dlg.GetPathName(),
				CFile::modeCreate|CFile::modeWrite|CFile::shareExclusive);

			// First, construct the file header and write it.

			BITMAPFILEHEADER bmfh;
			const int nRasterWidth = GetRasterWidthInBytes();
			bmfh.bfType = 0x4d42; // this is "BM", the cookie.
			bmfh.bfSize = sizeof(BITMAPFILEHEADER) +
						  sizeof(BITMAPINFOHEADER) +
						  2 * sizeof(RGBQUAD) +
						  nRasterWidth * Height();
			bmfh.bfReserved1 = 0;
			bmfh.bfReserved2 = 0;
			bmfh.bfOffBits = sizeof(BITMAPINFOHEADER) + 2 * sizeof(RGBQUAD);

			file.Write(&bmfh,sizeof(bmfh));

			// Construct the bitmap info header and write it

			BITMAPINFOHEADER bmih;
			bmih.biSize = sizeof(BITMAPINFOHEADER);
			bmih.biWidth = Width();
			bmih.biHeight = Height();
			bmih.biPlanes = 1;
			bmih.biBitCount = 1;
			bmih.biCompression = BI_RGB;
			bmih.biSizeImage = 0;
			bmih.biXPelsPerMeter = 1000;
			bmih.biYPelsPerMeter = 1000;
			bmih.biClrUsed = 0;
			bmih.biClrImportant = 0;

			file.Write(&bmih,sizeof(bmih));

			// Construct the two RGBs and write them.

			RGBQUAD rgb[2];
			memset(rgb,0,sizeof(rgb[0]));
			memset(rgb + 1,-1,sizeof(rgb[1]));
			rgb[1].rgbReserved = 0;

			file.Write(rgb,sizeof(rgb));

			// Write the bits. The first row is the bottom one.
			
			CMonochromeBitmap &monobmp = GetMonochromeBitmap();
			for (int i = Height() - 1; i >= 0; i--) {
				file.Write(monobmp.GetRaster(i),nRasterWidth);
			}
		} catch(CFileException *e) {
			CString cszBuffer;
			UINT uHelpContext;
			e->GetErrorMessage(cszBuffer.GetBuffer(1024),1024,&uHelpContext);
			cszBuffer.ReleaseBuffer();
			AfxMessageBox(cszBuffer,MB_OK|MB_ICONEXCLAMATION,uHelpContext);
		}
	}
}

void CImageDoc::OnUpdateFileExport(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable(! IsEmpty());	
}

int CImageDoc::GetRasterWidthInBytes()
{
	// We have to do a little arithmetic to get the size of
	// the monochrome raster. It must be a multiple of 4 bytes.
	int nMonochromeWidth = (Width() + 7) >> 3;
	nMonochromeWidth = (((nMonochromeWidth + 3) >> 2) << 2);
	return nMonochromeWidth;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -