📄 bmpcompressdlg.cpp
字号:
// BmpCompressDlg.cpp : implementation file
#include "stdafx.h"
#include "BmpCompress.h"
#include "BmpCompressDlg.h"
#include <afxdlgs.h>
#include "windowsx.h"
#include <stdlib.h>
#include <stdio.h>
#include <process.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define WIDTHBYTES(bits) (((bits) + 31) / 32 * 4)
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CBmpCompressDlg dialog
CBmpCompressDlg::CBmpCompressDlg(CWnd* pParent /*=NULL*/)
: CDialog(CBmpCompressDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CBmpCompressDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_ratio=1;
m_Dib.m_pData =NULL;
m_CompressFile.SetFilePath("");
}
void CBmpCompressDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CBmpCompressDlg)
DDX_Control(pDX, IDC_SCALE, m_scale);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CBmpCompressDlg, CDialog)
//{{AFX_MSG_MAP(CBmpCompressDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDLOAD, OnLoad)
ON_BN_CLICKED(IDCOMPRESS, OnCompress)
ON_BN_CLICKED(IDDECOMPRESS, OnDecompress)
ON_NOTIFY(NM_OUTOFMEMORY, IDC_SCALE, OnOutofmemoryScaleDisplayBMP)
ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_SCALE, OnReleasedcaptureScale)
ON_BN_CLICKED(IDDDISPLAY, OnDisplay)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CBmpCompressDlg message handlers
BOOL CBmpCompressDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
m_scale.SetRange(10,400,FALSE);
return TRUE; // return TRUE unless you set the focus to a control
}
void CBmpCompressDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CBmpCompressDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CBmpCompressDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CBmpCompressDlg::OnLoad()
{
// TODO: Add your control notification handler code here
char strFileName[256];
CString str;
CString FileExtName;
CFileDialog dlgLoad(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
"位图文件(*.bmp)|*.bmp|压缩文件(*.dat)|*.dat|All files|*.*||");
if((dlgLoad.DoModal())!=IDOK)
return ;
FileExtName=dlgLoad.GetFileExt();
if(FileExtName=="bmp")
{
m_Dib.m_pData=NULL;
::DeleteObject(&m_Dib);
OnHideBMP();
strcpy(strFileName,dlgLoad.GetPathName());
m_Dib.LoadFile(strFileName);//装载位图文件
//写入压缩后的数据编码文件(十进制)
char strFileName[256];
strcpy(strFileName , dlgLoad.GetFileName());
strcat(strFileName,".txt");
FILE *fp;
BYTE *buffer;
buffer=new BYTE[m_Dib.size];
buffer=m_Dib.GetData();
fp=fopen(strFileName,"w");
UINT irow=0;//换行显示计数标志
char c='\n';
for(DWORD i=0;i<m_Dib.GetSize();i++)
{
fprintf(fp,"%d ",buffer[i]);irow++;
if(irow==m_Dib.GetWidth())
{fprintf(fp,"%c",c);irow=0;}
}
fclose(fp);
//显示文件信息
m_filename=m_Dib.GetFileTle();
m_bfsize=m_Dib.bitmapFileHeader.bfSize;
m_numberofcolor=m_Dib.GetNumberOfColors();
str="位图文件名称->";
if(m_CompressFile.GetFilePath()!="")
::DeleteObject(&m_CompressFile);
}
else if(FileExtName=="dat")
{
str="压缩文件名称->";
m_CompressFile.Open(dlgLoad.GetPathName(),CFile::modeRead );
m_filename=m_CompressFile.GetFileName();
m_bfsize=m_CompressFile.GetLength();
m_Dib.m_pData=NULL;
::DeleteObject(&m_Dib);
OnHideBMP();
}
OnDrawTEXT(str);
}
void CBmpCompressDlg::OnCompress()
{
// TODO: Add your control notification handler code here
//Compress the FileBMP;display the fileinfo after compression;
//notice:压缩后文件的名字(最好与原文件不同)位置及格式
// TODO: Add your control notification handler code here
//写入压缩后的数据编码文件(十进制)
char strFileName[256];
strcpy(strFileName ,"compressed");
strcat(strFileName,".txt");
FILE *fp;
BYTE *buffer;
buffer=new BYTE[m_Dib.size];
buffer=m_Dib.GetData();
fp=fopen(strFileName,"w");
UINT irow=0;//换行显示计数标志
char c='\n';
for(DWORD i=0;i<m_Dib.GetSize();i++)
{
fprintf(fp,"%d ",buffer[i]);irow++;
if(irow==m_Dib.GetWidth())
{fprintf(fp,"%c",c);irow=0;}
}
fclose(fp);
if(m_Dib.GetData()!=NULL)
{
static int nIndex=1;
CString PathName;
CString m_txtString;
CString FileName;
CString FileExtName;
char szFilter[]="压缩文件(.dat)|*.dat|All Files|*.*||";
CFileDialog dlgSavefile(FALSE, NULL, NULL, OFN_HIDEREADONLY,szFilter);
dlgSavefile.m_ofn.nFilterIndex=(DWORD)nIndex;
if((dlgSavefile.DoModal())==IDOK)
{
PathName=dlgSavefile.GetPathName();
nIndex = (int) dlgSavefile.m_ofn.nFilterIndex;
FileName=dlgSavefile.GetFileName();
FileExtName=dlgSavefile.GetFileExt();
if(FileExtName=="")
PathName+=".dat";
CFile CompressionFile(PathName,CFile::modeCreate | CFile::modeWrite );
if(CompressBMP(CompressionFile)==TRUE)
MessageBox("压缩成功!", "系统提示" ,
MB_ICONINFORMATION | MB_OK);
m_Dib.m_pData=NULL;
::DeleteObject(&m_Dib);
OnHideBMP();
}
}
else
{
MessageBox("请先装载要压缩的位图文件!", "系统提示" ,
MB_ICONINFORMATION | MB_OK);
}
}
void CBmpCompressDlg::OnDecompress()
{
// TODO: Add your control notification handler code here
//Dompress the FileBMP;display the fileinfo after compression;
//notice:解压缩后文件的名字,位置格式
if(m_CompressFile.GetFilePath()!="")
{static int nIndex=1;
CString PathName;
CString m_txtString;
CString FileName;
CString FileExtName;
char szFilter[]="位图文件(.bmp)|*.bmp|All Files|*.*||";
CFileDialog dlgSavefile(FALSE, NULL, NULL, OFN_HIDEREADONLY,szFilter);
dlgSavefile.m_ofn.nFilterIndex=(DWORD)nIndex;
if((dlgSavefile.DoModal())==IDOK)
{
PathName=dlgSavefile.GetPathName();
nIndex = (int) dlgSavefile.m_ofn.nFilterIndex;
FileName=dlgSavefile.GetFileName();
FileExtName=dlgSavefile.GetFileExt();
if(FileExtName=="")
{PathName+=".bmp";FileName+=".bmp";}
m_filename=FileName;
CFile DecompressionFile(PathName,CFile::modeCreate | CFile::modeWrite );
if(DeCompressBmp(DecompressionFile)==TRUE)
MessageBox("解压缩成功!", "系统提示" ,
MB_ICONINFORMATION | MB_OK);
m_CompressFile.Close();
m_CompressFile.SetFilePath("");
::DeleteObject(&m_CompressFile);
}
}
else
{
MessageBox("请先装载要解压缩的文件(.dat)!", "系统提示" ,
MB_ICONINFORMATION | MB_OK);
}
}
CPalette * CBmpCompressDlg::CreateBitmpPalette(CDib *pBitmap)
{
LPLOGPALETTE lpPal; // pointer to a logical palette
UINT i; // loop index
LPRGBQUAD pRGBTable=pBitmap->GetRGB();
UINT numberOfColors=pBitmap->GetNumberOfColors();
if (numberOfColors != 0)
{
/* allocate memory block for logical palette */
/* if not enough memory, clean up and return NULL */
lpPal = (LPLOGPALETTE)GlobalAllocPtr(GHND, sizeof(LOGPALETTE)
+ sizeof(PALETTEENTRY)
* numberOfColors);
if (lpPal == 0) return FALSE;
/* set version and number of palette entries */
lpPal->palVersion = 0x300;
lpPal->palNumEntries = numberOfColors;
for (i = 0; i < numberOfColors; i++)
{
lpPal->palPalEntry[i].peRed = pRGBTable[i].rgbRed;
lpPal->palPalEntry[i].peGreen =pRGBTable[i].rgbGreen;
lpPal->palPalEntry[i].peBlue = pRGBTable[i].rgbBlue;
lpPal->palPalEntry[i].peFlags = 0;
}
/* create the palette and get handle to it */
m_hPalette=new CPalette;
m_hPalette->CreatePalette(lpPal);
GlobalFreePtr(lpPal);
delete m_hPalette;
}
return m_hPalette;
}
void CBmpCompressDlg::OnDrawBMP()
{
//刷新显示区
CRect rect;
CWnd *pDisplay=GetDlgItem(IDC_BMPINFO);
pDisplay->GetWindowRect(rect);
ScreenToClient(rect);
CDC * pDC=GetDC();
CBrush brush(RGB(255,255,255));
rect.SetRect(rect.left+5,rect.top+5,rect.right-5,rect.bottom-5);
pDC->FillRect(&rect,&brush);
//处理图像数据
BYTE *pBitmapData= m_Dib.GetData();
LPBITMAPINFO pBitmapInfo=m_Dib.GetInfo();
int bitmapHeight=m_Dib.GetHeight();
int bitmapWidth=m_Dib.GetWidth();
int scaledWidth=(int)(bitmapWidth * m_ratio);
int scaledHeight=(int)(bitmapHeight * m_ratio);
scaledWidth=(scaledWidth>rect.Width())?(rect.Width()-5):scaledWidth;
scaledHeight=(scaledHeight>rect.Height())?(rect.Height()-5):scaledHeight;
if(m_Dib.GetRGB()) //has color table
{
CPalette *hPalette=CreateBitmpPalette(&m_Dib);
//将已创建的调色版用到设备上下文中
CPalette *hOldPalette=pDC->SelectPalette(hPalette,true);
//实现调色版
pDC->RealizePalette();
::StretchDIBits(pDC->GetSafeHdc(),rect.left+5 ,rect.top+5 ,scaledWidth,scaledHeight,
0 ,0,bitmapWidth,bitmapHeight,pBitmapData,pBitmapInfo,
DIB_RGB_COLORS, SRCCOPY);
pDC->SelectPalette(hOldPalette,true);
::DeleteObject(m_hPalette);
}
else //has no color table
{
::StretchDIBits(pDC->GetSafeHdc(),rect.left+5 ,rect.top+5 ,scaledWidth,scaledHeight,
0 ,0,bitmapWidth,bitmapHeight,pBitmapData,pBitmapInfo,
DIB_PAL_COLORS, SRCCOPY);
}
}
void CBmpCompressDlg::OnDrawTEXT(CString &strTitle)
{
//刷新显示区
CRect rect;
CWnd *pDisplay=GetDlgItem(IDC_FILEINFO);
pDisplay->GetWindowRect(rect);
ScreenToClient(rect);
int left=rect.left+5;
int top=rect.top+5;
rect.SetRect(left,top,rect.right-5,rect.bottom-5);
CBrush brush(RGB(255,255,255));
CDC *pDC=GetDC();
pDC->FillRect(&rect,&brush);
//显示文件信息
CString str;
char strChar[30],strChar1[20];
strcpy(strChar,strTitle);
strcat(strChar,m_filename);
pDC->TextOut(left+5,top+5,strChar);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -