#include"resource.h"
#include"function.h"
#include<time.h>
//USES_CONVERSION;
/**主窗口相关变量声明*/
WNDCLASSEX stdWndClass;
HINSTANCE g_hInstance;
HWND hWinMain;
WNDCLASSEX stdWndClass;
TCHAR MyIcon[] = _T("Icon");
TCHAR MenuName[] = _T("Menu");
TCHAR szClassName[] = _T("MyClass");
TCHAR szCaptionMain[] = _T("图片压缩器");
TCHAR Author[] = _T("AUTHORDLG");
TCHAR View[] = _T("VIEWDLG");
//HWND hAuthorDlg;
/**打开文件相关参数定义*/
HBITMAP g_hBitmap=NULL; //位图句柄
OPENFILENAME g_ofn; // 公共对话框结构。
#define MAXSIZE 260
TCHAR fileTitle[MAXSIZE];
TCHAR open_buffer[MAXSIZE]; //保存获取文件名称的缓冲区。
FILE *fp; //文件指针
BITMAPFILEHEADER bmfh; //保存位图文件头
BITMAPINFOHEADER bmih; //保存位图信息头
BitmapData * colors; //保存像素信息
/**压缩操作相关参数定义*/
TCHAR Threshold[] = _T("THRESHOLDDLG");
int num[4]={0,5,10,20};
unsigned short threshold; ///压缩阙值
QuadTree QT = NULL;
rect area;
int row,column;//改变为标准可压缩图片的大小后的行列数
int i,j;
unsigned int leaves;//统计叶子数目
unsigned char maxHeight;//统计四叉树最大高度
clock_t pressTime;//统计压缩时间
clock_t startTime,finishTime;
double scale;//统计压缩比
TCHAR com_buffer[MAXSIZE];
/**解压缩操作相关参数*/
TCHAR decom_buffer[MAXSIZE];
TCHAR Dtime[30]=_T("解压成功!解压时间:");
int stride;//扫描行对其参数
BitmapData tempColor;//用于扫描行对齐
//查看四叉树信息相关参数定义
//TCHAR View[] = _T("VIEWDLG");
char tempString[10];
//TCHAR QTreeName[20] = TEXT("第一项");
TCHAR Blank[20]=_T("");
TCHAR QTreeThreshold[20];
TCHAR QTreeLeaves[20];
TCHAR QTreeHeight[20];
TCHAR QTreeTime[20];
TCHAR QTreeScale[20];
//关于作者对话框
BOOL CALLBACK ProcWinAuthor(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam)
{
switch(Msg)
{
case WM_COMMAND:
if (lParam != 0)
{
if(HIWORD(wParam) == BN_CLICKED)
{
switch(LOWORD(wParam))
{
case IDOK:
{
EndDialog(hWnd,IDOK);
break;
}
}
}
}
break;
}
return 0;
}
void InitComboBox(HWND hComboBox)
{
int i;
TCHAR* threshold[4] = {TEXT("0"),TEXT("5"),TEXT("10"),TEXT("20")};
for (i=0;i<4;i++)
{
SendMessage(hComboBox,CB_ADDSTRING,0,(LPARAM)threshold[i]);
}
}
//阙值选择对话框
BOOL CALLBACK ProcWinThreshold(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam)
{
switch(Msg)
{
case WM_INITDIALOG:
InitComboBox(GetDlgItem(hWnd,IDC_THRESHOLD));
break;
case WM_COMMAND:
if (lParam != 0)
{
if(HIWORD(wParam) == BN_CLICKED)
{
switch(LOWORD(wParam))
{
case IDOK:
threshold = num[ComboBox_GetCurSel(GetDlgItem(hWnd,IDC_THRESHOLD))];
EndDialog(hWnd,IDOK);
break;
case IDCANCEL:
threshold = -1;
EndDialog(hWnd,IDCANCEL);
break;
}
}
}
break;
case WM_CLOSE:
threshold = -1;
EndDialog(hWnd,0);
break;
}
return 0;
}
void InitViewList(HWND hViewList)
{
SendMessage(hViewList,LB_ADDSTRING,0,(LPARAM)fileTitle);
SendMessage(hViewList,LB_ADDSTRING,0,(LPARAM)Blank);
SendMessage(hViewList,LB_ADDSTRING,0,(LPARAM)QTreeThreshold);
SendMessage(hViewList,LB_ADDSTRING,0,(LPARAM)Blank);
SendMessage(hViewList,LB_ADDSTRING,0,(LPARAM)QTreeLeaves);
SendMessage(hViewList,LB_ADDSTRING,0,(LPARAM)Blank);
SendMessage(hViewList,LB_ADDSTRING,0,(LPARAM)QTreeHeight);
SendMessage(hViewList,LB_ADDSTRING,0,(LPARAM)Blank);
SendMessage(hViewList,LB_ADDSTRING,0,(LPARAM)QTreeTime);
SendMessage(hViewList,LB_ADDSTRING,0,(LPARAM)Blank);
SendMessage(hViewList,LB_ADDSTRING,0,(LPARAM)QTreeScale);
}
//查看四叉树信息对话框
BOOL CALLBACK ProcWinView(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam)
{
switch(Msg)
{
case WM_INITDIALOG:
InitViewList(GetDlgItem(hWnd,IDC_VIEWLIST));
break;
case WM_COMMAND:
if (lParam != 0)
{
if(HIWORD(wParam) == BN_CLICKED)
{
switch(LOWORD(wParam))
{
case IDOK:
EndDialog(hWnd,IDOK);
break;
}
}
}
break;
case WM_CLOSE:
EndDialog(hWnd,0);
break;
}
return 0;
}
LRESULT CALLBACK ProcWinMain( HWND hWnd,
UINT Msg,
WPARAM wParam,
LPARAM lParam
)
{
switch(Msg)
{
case WM_COMMAND:
{
switch(LOWORD(wParam))
{
case IDM_OPEN://打开图片
g_ofn.lStructSize = sizeof(g_ofn);//初始化选择文本对话框
g_ofn.hInstance = g_hInstance;
g_ofn.lpstrFilter = _T("BMP Files\0*.bmp\0");
g_ofn.lpstrFile = open_buffer;
g_ofn.nMaxFile = MAXSIZE;
g_ofn.Flags = OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST |OFN_LONGNAMES|OFN_EXPLORER|OFN_HIDEREADONLY|OFN_NOCHANGEDIR;
g_ofn.lpstrFileTitle = fileTitle;
g_ofn.lpstrTitle=_T("打开图片");
g_ofn.lpstrInitialDir=_T("..\\Image");
GetOpenFileName(&g_ofn);
g_hBitmap = (HBITMAP)LoadImage(NULL,open_buffer,IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
if(!(fp=_wfopen(open_buffer,_T("rb"))))
{
MessageBox(hWnd,_T("文件打开失败"),szCaptionMain,MB_OK|MB_ICONERROR);
break;
}
if(GetImageData(fp, &bmfh, &bmih, &colors)==FALSE)
//获取位图文件的文件头,位图信息头以及像素信息
{
MessageBox(hWnd,_T("您选择的文件不符合要求,请重新选择"),szCaptionMain,MB_OK|MB_ICONERROR);
g_hBitmap = NULL;
}
InvalidateRect(hWnd,NULL,TRUE);
ImagePaint(hWnd,g_hBitmap);
fclose(fp);
break;
case IDM_CLOSE:
if(g_hBitmap==NULL)
{
MessageBox(hWnd,_T("没有需要关闭的图片"),szCaptionMain,MB_OK|MB_ICONERROR);
break;
}
if(MessageBox(hWnd,_T("您确认要关闭吗?"),szCaptionMain,MB_YESNO|MB_ICONQUESTION|MB_DEFBUTTON2)==IDYES)
{
g_hBitmap=NULL;
InvalidateRect(hWnd,NULL,TRUE);
}
break;
case IDM_EXIT:
if(MessageBox(hWnd,_T("您确认要退出吗?"),szCaptionMain,MB_YESNO|MB_ICONQUESTION|MB_DEFBUTTON2)==IDYES)DestroyWindow(hWnd);
break;
case IDM_COMPRESS:
if(g_hBitmap!=NULL)
{
row = bmih.biHeight;column = bmih.biWidth;
SizeChange(&row);SizeChange(&column);
area.bottom = 0;
area.top = (unsigned short)row-1;
area.left = 0;
area.right = (unsigned short)column-1;
g_ofn.lStructSize = sizeof(g_ofn);//初始化选择文本对话框
g_ofn.hInstance = g_hInstance;
g_ofn.lpstrFilter = _T("QT Files\0*.qt\0");
g_ofn.lpstrFile = com_buffer;
g_ofn.lpstrTitle=_T("压缩为");
g_ofn.lpstrInitialDir=_T("Image\\");
g_ofn.lpstrDefExt=_T("qt");
g_ofn.nMaxFile = MAXSIZE;
g_ofn.lpstrFileTitle = fileTitle;
g_ofn.Flags = OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST |OFN_LONGNAMES|OFN_EXPLORER|OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT|OFN_NOCHANGEDIR;
DialogBox(g_hInstance,Threshold,hWnd,(DLGPROC)ProcWinThreshold);
if(threshold < 0)break;
GetSaveFileName(&g_ofn);
if(!(fp = _wfopen(com_buffer,_T("wb+"))))
{
MessageBox(hWnd,_T("压缩失败"),szCaptionMain,MB_OK|MB_ICONERROR);
break;
}
leaves = 0;maxHeight = 0;
startTime = clock();
CreatQuadTree(&QT, colors, area, column, 0);
finishTime = clock();
pressTime = finishTime - startTime;
scale = ((double)sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + (double)4*leaves+19) / ((double)sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)+(double)3*bmih.biHeight*bmih.biWidth)*100;