📄 convertbmpview.cpp
字号:
// convertBMPView.cpp : implementation of the CConvertBMPView class
//
#include "stdafx.h"
#include "convertBMP.h"
#include "convertBMPDoc.h"
#include "convertBMPView.h"
#include "InputBmpId.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CConvertBMPView
#define BYTELENGTH 8
#define START_STRING_FOR_BMP "const RM_BITMAP_T bmpItem = {"
IMPLEMENT_DYNCREATE(CConvertBMPView, CScrollView)
BEGIN_MESSAGE_MAP(CConvertBMPView, CScrollView)
//{{AFX_MSG_MAP(CConvertBMPView)
ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
ON_COMMAND(ID_EDIT_CONVERT, OnEditConvert)
ON_COMMAND(ID_EDIT_ADDTOFILE, OnEditAddtofile)
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CConvertBMPView construction/destruction
CConvertBMPView::CConvertBMPView()
{
// TODO: add construction code here
}
CConvertBMPView::~CConvertBMPView()
{
}
BOOL CConvertBMPView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CConvertBMPView drawing
void CConvertBMPView::OnDraw(CDC* pDC)
{
CConvertBMPDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
BeginWaitCursor();
m_dibResource.UsePalette(pDC); // should be in palette
m_dibFile.UsePalette(pDC); // message handlers, not here
CSize sizeResourceDib = m_dibResource.GetDimensions();
sizeResourceDib.cx *= 30;
sizeResourceDib.cy *= -30;
m_dibResource.Draw(pDC, CPoint(0, -800), sizeResourceDib);
CSize sizeFileDib = m_dibFile.GetDimensions();
sizeFileDib.cx *= 30;
sizeFileDib.cy *= -30;
m_dibFile.Draw(pDC, CPoint(1800, -800), sizeFileDib);
EndWaitCursor();
}
void CConvertBMPView::OnInitialUpdate()
{
CScrollView::OnInitialUpdate();
CSize sizeTotal(30000, 40000); // 30-by-40 cm
CSize sizeLine = CSize(sizeTotal.cx / 100, sizeTotal.cy / 100);
SetScrollSizes(MM_HIMETRIC, sizeTotal, sizeTotal, sizeLine);
/*LPVOID lpvResource = (LPVOID) ::LoadResource(NULL,
::FindResource(NULL, MAKEINTRESOURCE(IDB_REDBLOCKS),
RT_BITMAP));*/
// m_dibResource.AttachMemory(lpvResource); // no need for
// ::LockResource
CClientDC dc(this);
TRACE("bits per pixel = %d\n", dc.GetDeviceCaps(BITSPIXEL));
}
/////////////////////////////////////////////////////////////////////////////
// CConvertBMPView printing
BOOL CConvertBMPView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CConvertBMPView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CConvertBMPView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CConvertBMPView diagnostics
#ifdef _DEBUG
void CConvertBMPView::AssertValid() const
{
CView::AssertValid();
}
void CConvertBMPView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CConvertBMPDoc* CConvertBMPView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CConvertBMPDoc)));
return (CConvertBMPDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CConvertBMPView message handlers
/////////////////////////////////////////////////////////////////////////////
//#define MEMORY_MAPPED_FILES
void CConvertBMPView::OnFileOpen()
{
CConvertBMPDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CFileDialog dlg(TRUE, "bmp", "*.bmp",OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,NULL,NULL);
if (dlg.DoModal() != IDOK) {
return;
}
// seve the filename to doc file
fileName = dlg.GetPathName();
CFile file;
file.Open(dlg.GetPathName(), CFile::modeRead);
if (m_dibFile.Read(&file) == TRUE) {
Invalidate();
}
CClientDC dc(this);
m_dibFile.SetSystemPalette(&dc);
}
BOOL CConvertBMPView::FindAllBMPFile(CArray<CString,CString>& file_name_array,CString csPath)
{
CFileFind cffd;
BOOL moreFile;
CString openName;
//Find the open file
if(!cffd.FindFile(csPath+"*.bmp"))
{
AfxMessageBox("Can not find file");
return FALSE;
}
moreFile=TRUE;
while(moreFile)
{
moreFile=cffd.FindNextFile();
if(cffd.GetFileName().Compare(".")==0 || cffd.GetFileName().Compare("..")==0)
continue;
openName=cffd.GetFilePath();
if(!isBMPFile(openName))
continue;
file_name_array.Add(openName);
}
cffd.Close();
return TRUE;
}
CString CConvertBMPView::ParseFilePath(CString fullName)
{
int i;
CString csPath;
i=fullName.GetLength();
if(i==0)
return "";
else
i-=1;
while(i>0)
{
if(fullName.GetAt(i)=='\\')
break;
else
i-=1;
}
if(i==0)
return "";
csPath=fullName.Left(i+1);
return csPath;
}
BOOL CConvertBMPView::isBMPFile(CString fileName)
{
FILE* fp;
WORD is_bmp;
if(fileName.GetLength()==0)
return FALSE;
if((fp=fopen(fileName,"rb"))==NULL)
return FALSE;
if(fread(&is_bmp,sizeof(WORD),1,fp)!=1)
{
fclose(fp);
return FALSE;
}
fclose(fp);
return is_bmp == 0x4d42;
}
CString CConvertBMPView::RemoveFileTail(CString name)
{
int i;
if(name.GetLength() == 0)
return "";
for(i=name.GetLength()-1;i>=0;i--)
{
if(name.GetAt(i) == '.')
break;
}
if(i<=0)
return name;
else
return name.Left(i);
}
CString CConvertBMPView::ParseFileName(CString fullName)
{
int i;
if(fullName.GetLength()<=0)
return "";
i=fullName.GetLength()-1;
while(i>0)
{
if(fullName.GetAt(i)=='\\')
break;
else
i-=1;
}
if(i==0)
return fullName;
else
return fullName.Right(fullName.GetLength()-(i+1));
}
/* BITMAPFILEHEADER+BITMAPINFOHEADER+RGBQUAD array+Corlor Index array*/
bool CConvertBMPView::ConvertBmp(CString szFileName, char *sError)
{
CArray<CString,CString> file_name_array;
int i;
CString csFileName;
CString csOpenPath;
CString csSavePath;
CString csSaveName;
bool bres;
csOpenPath = ParseFilePath(fileName);
//Find all the bmp file and save them in an array
if(!FindAllBMPFile(file_name_array,csOpenPath))
{
AfxMessageBox("no bmp files");
return false;
}
csSavePath = ParseFilePath(szFileName);
bres = true;
for(i=0;i<file_name_array.GetSize();i++)
{
csFileName = file_name_array.GetAt(i);
csSaveName = ParseFileName(csFileName);
csSaveName = RemoveFileTail(csSaveName);
szFileName.Format("%s%s.c",csSavePath,csSaveName);
if(!ConvertBmp_single(csFileName,szFileName,sError))
{
CString cs;
cs.Format("%s: %s",csFileName,sError);
AfxMessageBox(cs);
bres = false;
}
}
return bres;
}
bool CConvertBMPView::ConvertBmp_single(CString csOpen,CString szFileName, char *sError)
{
CString FileName ;
int iTmp;
int iCount = 0;
FILE* Bitmap = fopen(csOpen,"rb");
if (Bitmap == NULL)
{
strcpy(sError, "File Not Exist");
return false;
}
BITMAPFILEHEADER bmpFileHeader;
BITMAPINFOHEADER bmpInfoHeader;
RGBQUAD *bmiColors;
fread(&bmpFileHeader, sizeof(bmpFileHeader), 1,Bitmap);
if (bmpFileHeader.bfType != (('M'<<BYTELENGTH) | 'B')) // not BITMAP file
{
strcpy(sError , "This is not a BMP File!");
fclose(Bitmap);
return false;
}
fread(&bmpInfoHeader, sizeof(bmpInfoHeader), 1,Bitmap);
if (bmpInfoHeader.biCompression != BI_RGB)
{
strcpy(sError , "This Bitmap is not 'BI_RGB' mode");
fclose(Bitmap);
return false;
}
if (bmpInfoHeader.biSize < sizeof(bmpInfoHeader)) // not MS bitmap
{
strcpy(sError , "Some Errors in this Bitmap File");
fclose(Bitmap);
return false;
}
//tree ----- delete for test.don't forget to modify after test
if ( (bmpInfoHeader.biBitCount != 1)
&& (bmpInfoHeader.biBitCount != 4)
&& (bmpInfoHeader.biBitCount != 8)
&& (bmpInfoHeader.biBitCount != 16)
&& (bmpInfoHeader.biBitCount != 24) )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -