// truecolorto8bitView.cpp : implementation of the CTruecolorto8bitView class
//
#include "stdafx.h"
#include "truecolorto8bit.h"
#include "truecolorto8bitDoc.h"
#include "truecolorto8bitView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CTruecolorto8bitView
IMPLEMENT_DYNCREATE(CTruecolorto8bitView, CView)
BEGIN_MESSAGE_MAP(CTruecolorto8bitView, CView)
//{{AFX_MSG_MAP(CTruecolorto8bitView)
ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
ON_COMMAND(ID_M8BITCOLOR, OnM8bitcolor)
ON_COMMAND(ID_M2COLOR, OnM2color)
ON_COMMAND(ID_FILE_SAVE, OnFileSave)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTruecolorto8bitView construction/destruction
CTruecolorto8bitView::CTruecolorto8bitView()
{
// TODO: add construction code here
bShow=false;
}
CTruecolorto8bitView::~CTruecolorto8bitView()
{
}
BOOL CTruecolorto8bitView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CTruecolorto8bitView drawing
//老胡作品
//akiy@eyou.com
void CTruecolorto8bitView::OnDraw(CDC* pDC)
{
CTruecolorto8bitDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// if(bShow!=false)
// {
if(bitmap.m_hObject==NULL) return;
CBitmap *pOldBitmap;
CDC tempDC;
tempDC.CreateCompatibleDC(NULL);
pOldBitmap = tempDC.SelectObject(&bitmap);
pDC->BitBlt(0,0,bm.bmWidth,bm.bmHeight,&tempDC,0,0,SRCCOPY);
tempDC.SelectObject(pOldBitmap);
tempDC.DeleteDC();
// }
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// CTruecolorto8bitView printing
BOOL CTruecolorto8bitView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CTruecolorto8bitView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CTruecolorto8bitView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CTruecolorto8bitView diagnostics
#ifdef _DEBUG
void CTruecolorto8bitView::AssertValid() const
{
CView::AssertValid();
}
void CTruecolorto8bitView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CTruecolorto8bitDoc* CTruecolorto8bitView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTruecolorto8bitDoc)));
return (CTruecolorto8bitDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CTruecolorto8bitView message handlers
void CTruecolorto8bitView::OnFileOpen()
{
// TODO: Add your command handler code here
CFileDialog dlg(TRUE,NULL,NULL,NULL);
if(dlg.DoModal()!=IDOK) return;
str.Empty();
str=dlg.GetPathName();
HBITMAP hBitmap=(HBITMAP)LoadImage(NULL,str, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE|LR_CREATEDIBSECTION);
// CDC tempDC;
if(hBitmap==NULL) return;
bitmap.DeleteObject();
bitmap.Attach(hBitmap);
bitmap.GetObject(sizeof(BITMAP),&bm);
bShow=true;
Invalidate();
}
typedef BOOL (*MYPROC)(BITMAPINFO* pBMISrc,LPBYTE pBitsSrc,BITMAPINFO* pBMIDest,LPBYTE pBitsDest);
//老胡作品
//akiy@eyou.com
void CTruecolorto8bitView::OnM8bitcolor()
{
// TODO: Add your command handler code here
//加载位图文件
BITMAPFILEHEADER bf;
BITMAPINFO *binfoin,*binfoout;
//创建两个信息头
CFile file;
if(!file.Open(str,CFile::modeRead,NULL)) return;
binfoin=(BITMAPINFO *)malloc(sizeof(BITMAPINFOHEADER)+256*sizeof(RGBQUAD)+10);
binfoout=(BITMAPINFO *)malloc(sizeof(BITMAPINFOHEADER)+256*sizeof(RGBQUAD)+10);
file.Read(&bf,sizeof(BITMAPFILEHEADER));
file.Read(binfoin,sizeof(BITMAPINFOHEADER));
int nline=((binfoin->bmiHeader.biWidth* binfoin->bmiHeader.biBitCount+31)/8) /4*4;
int nsize=nline*(binfoin->bmiHeader.biHeight);
int nline8=((binfoin->bmiHeader.biWidth* 8+31)/8) /4*4;
int nsize8=nline8*(binfoin->bmiHeader.biHeight);
unsigned char *pdata,*pdata8;
//为源数据开辟缓冲区
pdata=(unsigned char *)malloc(nsize+nline+10);//注意这里必须申请一块大一点的空间,不然会益处
//为目标数据开辟缓冲区
pdata8=(unsigned char *)malloc(nsize8+10);
file.Read(pdata,nsize);
file.Close();
//调用dll库中的函数
HINSTANCE hinstLib;
MYPROC ProcAdd;
hinstLib = LoadLibrary("demcolordll");
if (hinstLib != NULL)
{
ProcAdd = (MYPROC) GetProcAddress(hinstLib, "RGB2256HiQuality");
if(ProcAdd !=NULL)
{
ProcAdd(binfoin,pdata,binfoout,pdata8);
FreeLibrary(hinstLib);
//将得到的数据写进文件中
if(file.Open("temp.bmp",CFile::modeCreate|CFile::modeWrite,NULL))
{
bf.bfOffBits=bf.bfOffBits+1024;
bf.bfSize=bf.bfOffBits+nsize8;
file.Write(&bf,sizeof(BITMAPFILEHEADER));
file.Write(binfoout,sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*256);
file.Write(pdata8,nsize8);
file.Close();
}
}
}
free(binfoin);
free(binfoout);
free(pdata);
free(pdata8);
HBITMAP hBitmap=(HBITMAP)LoadImage(NULL,"temp.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE|LR_CREATEDIBSECTION);
// CDC tempDC;
if(hBitmap==NULL) return;
bitmap.DeleteObject();
bitmap.Attach(hBitmap);
bitmap.GetObject(sizeof(BITMAP),&bm);
bShow=true;
Invalidate();
}
//老胡作品
//akiy@eyou.com
void CTruecolorto8bitView::OnM2color()
{
//加载位图文件
BITMAPFILEHEADER bf;
BITMAPINFO *binfoin,*binfoout;
//创建两个信息头
CFile file;
if(!file.Open(str,CFile::modeRead,NULL)) return;
binfoin=(BITMAPINFO *)malloc(sizeof(BITMAPINFOHEADER)+256*sizeof(RGBQUAD)+10);
binfoout=(BITMAPINFO *)malloc(sizeof(BITMAPINFOHEADER)+2*sizeof(RGBQUAD)+10);
file.Read(&bf,sizeof(BITMAPFILEHEADER));
file.Read(binfoin,sizeof(BITMAPINFOHEADER));
int nline=((binfoin->bmiHeader.biWidth* binfoin->bmiHeader.biBitCount+31)/8) /4*4;
int nsize=nline*(binfoin->bmiHeader.biHeight);
int nline1=((binfoin->bmiHeader.biWidth+31)/8) /4*4;
int nsize1=nline1*(binfoin->bmiHeader.biHeight);
unsigned char *pdata,*pdata1;
//为源数据开辟缓冲区
pdata=(unsigned char *)malloc(nsize+nline+10);//注意这里必须申请一块大一点的空间,不然会益处
//为目标数据开辟缓冲区
pdata1=(unsigned char *)malloc(nsize1+10);
file.Read(pdata,nsize);
file.Close();
//调用dll库中的函数
HINSTANCE hinstLib;
MYPROC ProcAdd;
hinstLib = LoadLibrary("demcolordll");
if (hinstLib != NULL)
{
ProcAdd = (MYPROC) GetProcAddress(hinstLib, "RGB21HiQuality");
if(ProcAdd !=NULL)
{
ProcAdd(binfoin,pdata,binfoout,pdata1);
FreeLibrary(hinstLib);
//将得到的数据写进文件中
if(file.Open("temp.bmp",CFile::modeCreate|CFile::modeWrite,NULL))
{
bf.bfOffBits=bf.bfOffBits+sizeof(RGBQUAD)*2;
bf.bfSize=bf.bfOffBits+nsize1;
file.Write(&bf,sizeof(BITMAPFILEHEADER));
file.Write(binfoout,sizeof(BITMAPINFOHEADER)+sizeof(RGBQUAD)*2);
file.Write(pdata1,nsize1);
file.Close();
}
}
}
free(binfoin);
free(binfoout);
free(pdata);
free(pdata1);
HBITMAP hBitmap=(HBITMAP)LoadImage(NULL,"temp.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE|LR_CREATEDIBSECTION);
// CDC tempDC;
if(hBitmap==NULL) return;
bitmap.DeleteObject();
bitmap.Attach(hBitmap);
bitmap.GetObject(sizeof(BITMAP),&bm);
bShow=true;
Invalidate();
}
//老胡作品
//akiy@eyou.com
void CTruecolorto8bitView::OnF