📄 lcd256doc.cpp
字号:
// LCD256Doc.cpp : implementation of the CLCD256Doc class
//
#include "stdafx.h"
#include "LCD256.h"
#include "LCD256Doc.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CLCD256Doc
IMPLEMENT_DYNCREATE(CLCD256Doc, CDocument)
BEGIN_MESSAGE_MAP(CLCD256Doc, CDocument)
//{{AFX_MSG_MAP(CLCD256Doc)
ON_COMMAND(IDM_GENERATE, OnGenerate)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CLCD256Doc construction/destruction
CLCD256Doc::CLCD256Doc()
{
// TODO: add one-time construction code here
::memset(this->m_DesFileName,0,100);
ScreenCX=::GetSystemMetrics(SM_CXMAXIMIZED);
ScreenCY=::GetSystemMetrics(SM_CYMAXIMIZED);
m_bSourceLoad = FALSE;
}
CLCD256Doc::~CLCD256Doc()
{
if(imageR!=NULL)
delete [] imageR;
if(imageG!=NULL)
delete [] imageG;
if(imageB!=NULL)
delete [] imageB;
if(image256!=NULL)
delete [] imageB;
}
BOOL CLCD256Doc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
// TODO: add reinitialization code here
// (SDI documents will reuse this document)
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CLCD256Doc serialization
void CLCD256Doc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
}
else
{
// TODO: add loading code here
}
}
/////////////////////////////////////////////////////////////////////////////
// CLCD256Doc diagnostics
#ifdef _DEBUG
void CLCD256Doc::AssertValid() const
{
CDocument::AssertValid();
}
void CLCD256Doc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CLCD256Doc commands
BOOL CLCD256Doc::OnOpenDocument(LPCTSTR lpszPathName)
{
if (!CDocument::OnOpenDocument(lpszPathName))
return FALSE;
// TODO: Add your specialized creation code here
strcpy(m_SourceFileName,lpszPathName);
strcpy(m_DesFileName,m_SourceFileName);
strncpy(this->m_DesFileName+strlen(lpszPathName)-4,"_des.c",8);
CFile f;
CFileException e;
CString s;
if(!f.Open(lpszPathName,CFile::modeRead,&e))
{
AfxMessageBox("Can not open file!");
}
sizeBmpFileSource=f.GetLength();
lpBmpFileSource=new char[sizeBmpFileSource];
f.Read(lpBmpFileSource,sizeBmpFileSource);
f.Close();
pbfhSource=(BITMAPFILEHEADER*)lpBmpFileSource;
if((lpBmpFileSource[0]!='B')||(lpBmpFileSource[1]!='M'))
{
AfxMessageBox("File format ERROR!");
//PostMessage(GetParent(NULL),WM_QUIT,NULL,NULL);
return FALSE;
}
pbihSource=(BITMAPINFOHEADER*)(lpBmpFileSource+sizeof(BITMAPFILEHEADER));
if(pbihSource->biSize!=sizeof(BITMAPINFOHEADER))
{
AfxMessageBox("BMP file format ERROR!");
PostMessage(GetParent(NULL),WM_QUIT,NULL,NULL);
return FALSE;
}
maxheight=pbihSource->biHeight;
maxwidth=pbihSource->biWidth;
imageR = new unsigned char[maxheight*maxwidth];
imageG = new unsigned char[maxheight*maxwidth];
imageB = new unsigned char[maxheight*maxwidth];
image256 = new unsigned char[maxheight*maxwidth];
OpenDibSource();
this->ReadBmpFile(this->m_SourceFileName);
this->UpdateAllViews(NULL);
return TRUE;
}
void CLCD256Doc::OpenDibSource()
{
this->m_hSourceDD=DrawDibOpen();
m_bSourceLoad = TRUE;
}
void CLCD256Doc::DrawSource(CDC *pDC)
{
if(this->m_bSourceLoad==TRUE)
{
int DispWidth=ScreenCX/4;
double myFactor=((double)(pbihSource->biHeight))/(pbihSource->biWidth);
DrawDibRealize(m_hSourceDD,pDC->GetSafeHdc(),TRUE);
DrawDibDraw(m_hSourceDD,pDC->GetSafeHdc(),ScreenCX/8,ScreenCX/20,
DispWidth,(int)(DispWidth*myFactor),
pbihSource,NULL,0,0,
pbihSource->biWidth,pbihSource->biHeight,DDF_BACKGROUNDPAL);
}
}
void CLCD256Doc::ReadBmpFile(char filename[100])
{
int i,j;
int scanwidth;
FILE* fp;
if((fp=fopen(filename,"rb"))==NULL)
{
::MessageBox(NULL,filename,"Message",MB_OK);
}
fread(&fileheader,sizeof(BITMAPFILEHEADER),1,fp);
fread(&infoheader,sizeof(BITMAPINFOHEADER),1,fp);
imagewidth=infoheader.biWidth;
imageheight=infoheader.biHeight;
scanwidth=imagewidth;
if(infoheader.biBitCount==24)
{
for(i=imageheight-1;i>=0;i--)
{
for(j=0;j<scanwidth;j++)
{
fread(imageG+i*imagewidth+j,sizeof(unsigned char),1,fp);
fread(imageB+i*imagewidth+j,sizeof(unsigned char),1,fp);
fread(imageR+i*imagewidth+j,sizeof(unsigned char),1,fp);
}
if(scanwidth%4!=0)
{
unsigned char rgb;
int k;
for(k=0;k<scanwidth%4;k++)
{
fread(&rgb,sizeof(unsigned char),1,fp);
}
}
}
} else {
AfxMessageBox("Please use 24-bit bmp image!");
}
fclose(fp);
return;
}
void CLCD256Doc::CloseDibSource()
{
if(m_hSourceDD)
{
DrawDibClose(m_hSourceDD);
m_hSourceDD=0;
}
}
/************************************************
**************************************************/
void CLCD256Doc::OnCloseDocument()
{
// TODO: Add your specialized code here and/or call the base class
CloseDibSource();
if(lpBmpFileSource)
{
delete lpBmpFileSource;
lpBmpFileSource=NULL;
}
if(imageR != NULL) {
delete [] imageR;
imageR = NULL;
}
if(imageG != NULL) {
delete [] imageG;
imageG = NULL;
}
if(imageB != NULL) {
delete [] imageB;
imageB = NULL;
}
CDocument::OnCloseDocument();
}
void CLCD256Doc::OnGenerate()
{
// TODO: Add your command handler code here
GenerateArrayFile();
UpdateAllViews(NULL);
}
void CLCD256Doc::ConvertTo256()
{
unsigned int r, g, b;
unsigned char color256;
for (int i = 0; i < maxheight; i++) {
for (int j = 0; j < maxwidth; j++) {
r = *(imageR + i * maxwidth + j);
g = *(imageG + i * maxwidth + j);
b = *(imageB + i * maxwidth + j);
r = r / 32;
g = g / 32;
b = b / 64;
color256 = (r << 5) | (g << 2) | b;
*(image256 + i * maxwidth + j) = color256;
}
}
}
void CLCD256Doc::GenerateArrayFile()
{
ConvertTo256();
FILE *f;
unsigned char color256;
unsigned int format_c = 0;
f = fopen(m_DesFileName,"w");
for (int i = maxheight - 1; i >= 0; i--) {
for (int j = 0; j < maxwidth; j++) {
color256 = *(image256 + i * maxwidth + j);
fprintf(f,"%4d,", color256);
if (++format_c % 16 == 0)
fprintf(f,"\n");
}
}
fclose(f);
AfxMessageBox("Array File Generated!");
char command[100];
memset(command, 0, 100);
strcat(command, "Notepad.exe ");
strcat(command, m_DesFileName);
::WinExec(command, SW_SHOW);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -