?? adaptive arithmetic.cpp
字號(hào):
// adaptive arithmetic.cpp : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。
//
// Author: Shen Hongwei
// Date: Nov.10, 2005
// Location: Beijing, China
//
#include "stdafx.h"
#include "AdaArithCoder.h"
#define _MY_DEBUG_
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <io.h>
#include "stdio.h"
#define WINDOWS_OS
#ifdef WINDOWS_OS
// Global function, only for Windows OS.
unsigned long
GetFileLength(char * fname)
{
unsigned long flength;
int fh = _open( fname, _O_RDONLY );
if( fh == -1 )
return 0L;
else
{
flength = _filelength(fh);
_close( fh );
return flength;
}
}
#endif
void PrintHelp(char * app_name);
int _tmain(int argc, _TCHAR* argv[])
{
//#define TEST0
/* #ifdef TEST0
int op_flag = 0; // 0 for compression; 1 for decompression;
char * in_name = "e:\\tmp\\a.txt";
char * out_name = "e:\\tmp\\a.txt.out";
#else
int op_flag = 1; // 0 for compression; 1 for decompression;
char * in_name = "e:\\tmp\\a.txt.out";
char * out_name = "e:\\tmp\\a.txt.decode";
#endif
*/
// #define TEST1
/* #ifdef TEST1
int op_flag = 0; // 0 for compression; 1 for decompression;
char * in_name = "e:\\tmp\\05.mpga";
char * out_name = "e:\\tmp\\05.mpga.out";
#else
int op_flag = 1; // 0 for compression; 1 for decompression;
char * in_name = "e:\\tmp\\05.mpga.out";
char * out_name = "e:\\tmp\\05.mpga.decode";
#endif
*/
//#define TEST2
/*#ifdef TEST2
int op_flag = 0; // 0 for compression; 1 for decompression;
char * in_name = "e:\\tmp\\1.doc";
char * out_name = "e:\\tmp\\1.doc.out";
#else
int op_flag = 1; // 0 for compression; 1 for decompression;
char * in_name = "e:\\tmp\\1.doc.out";
char * out_name = "e:\\tmp\\1.doc.decode";
#endif
*/
//#define TEST3
/*#ifdef TEST3
int op_flag = 0; // 0 for compression; 1 for decompression;
char * in_name = "e:\\tmp\\1.ppt";
char * out_name = "e:\\tmp\\1.ppt.out";
#else
int op_flag = 1; // 0 for compression; 1 for decompression;
char * in_name = "e:\\tmp\\1.ppt.out";
char * out_name = "e:\\tmp\\1.ppt.decode";
#endif
*/
/* #define TEST4
#ifdef TEST4
int op_flag = 0; // 0 for compression; 1 for decompression;
char * in_name = "e:\\tmp\\1.iso";
char * out_name = "e:\\tmp\\1.iso.out";
#else
int op_flag = 1; // 0 for compression; 1 for decompression;
char * in_name = "e:\\tmp\\1.iso.out";
char * out_name = "e:\\tmp\\1.iso.decode";
#endif
*/
//#define TEST5
/*#ifdef TEST5
int op_flag = 0; // 0 for compression; 1 for decompression;
char * in_name = "e:\\tmp\\Lena24bit.bmp";
char * out_name = "e:\\tmp\\lena.bmp.out";
#else
int op_flag = 1; // 0 for compression; 1 for decompression;
char * in_name = "e:\\tmp\\lena.bmp.out";
char * out_name = "e:\\tmp\\lena.bmp";
#endif
*/
#define TEST6
#ifdef TEST6
int op_flag = 0; // 0 for compression; 1 for decompression;
char * in_name = "e:\\tmp\\Authrall.htm";
char * out_name = "e:\\tmp\\Authrall.htm.out";
#else
int op_flag = 1; // 0 for compression; 1 for decompression;
char * in_name = "e:\\tmp\\Authrall.htm.out";
char * out_name = "e:\\tmp\\Authrall.htm.decode";
#endif
/* int op_flag = 0; // 0 for compression; 1 for decompression;
char * in_name = NULL;
char * out_name = NULL;
*/
int debug_mode = 0; // 0: don't output debug info; 1: output debug info
for(int i = 1; i < argc; ++i )
{
if( !strcmp( argv[i], "-op" ) )
{
op_flag = atoi(argv[++i]);
}
else if( !strcmp( argv[i], "-i" ) )
{
in_name = argv[++i];
}
else if( !strcmp( argv[i], "-o" ) )
{
out_name = argv[++i];
}
else if( !strcmp( argv[i], "-debug" ) )
{
debug_mode = 1;
}
else if( !strcmp( argv[i], "-?" ) )
{
PrintHelp(argv[0]);
}
}
if (in_name == NULL || out_name == NULL)
{
PrintHelp(argv[0]);
return(-1);
}
unsigned long infile_leng = GetFileLength(in_name);
if (infile_leng == 0L) {
printf("The file %s doesn't exist or is empty.\n", in_name);
return -1;
}
fstream fs_in, fs_out;
fs_in.open(in_name, ios_base::in | ios_base::binary); // becomes "r" (open existing file for reading)
fs_out.open(out_name, ios_base::out | ios_base::trunc | ios_base::binary); // becomes "w" (truncate existing file or create for writing).
CompressionHeader in_header, out_header;
FileBitIO * in = new FileBitIO();
FileBitIO * out = new FileBitIO();
if (op_flag == 1) {
in_header.Init(&fs_in);
in_header.ReadHeader();
in->Init(&fs_in, 8*1024L * 1024, 0, in_header.length);
} else {
out_header.Init(&fs_out);
out_header.length = 0;
out_header.origin_len = infile_leng;
out_header.WriteHeader();
in->Init(&fs_in, 8L*1024 * 1024L, 0, infile_leng*8);
}
out->Init(&fs_out, 8L*1024 * 1024L, 1);
Coder * coder;
if (op_flag == 0) { // compression
coder = new AdaArithCoder();
} else { // uncompression
coder = new AdaArithDecoder();
}
coder->SetDebugMode(debug_mode);
if (op_flag == 0)
coder->Init(in, out, infile_leng);
else
coder->Init(in, out, in_header.origin_len);
coder->Run();
if (op_flag == 0) {
out_header.length = coder->GetWriterBitsNum(); // fileheader may include more info, SHW,to do
out_header.WriteHeader();
}
fs_in.close();
fs_out.close();
delete coder;
delete in;
delete out;
cout << "Press Enter key to continue..." << endl;
getchar();
return 0;
}
void PrintHelp(char * app_name)
{
printf("%s -op operator_flag -i input_file -o output_file [-test]\n", app_name);
printf("%t operator_flag: 0 for compression; 1 for decompression;\n");
printf("%t -debug: if you use this parameter, the application will output debug infomation\n");
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -