⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 adptive_huffman.cpp

📁 自适应huffman编码
💻 CPP
字号:
// shw, 2005.09.28,

// adptive_huffman.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include "AdaHuff.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[])
{
	int     op_flag = 0;    // 0 for compression; 1 for decompression;
	int     debug_mode = 0;  // 0: don't output debug info; 1: output debug info
	char *  in_name = "e:\\tmp\\05.mpga";
	char *  out_name = "e:\\tmp\\05.mpga.cmp.8M1";

//	char *  in_name = "e:\\tmp\\a_old.exe";
//	char *  out_name = "e:\\tmp\\a_old2.cmp";

	//	char *  in_name = "e:\\tmp\\a_old2.cmp";
//	char *  out_name = "e:\\tmp\\a.exe";

//	char *  in_name = "e:\\tmp\\05.mpga.cmp";
//	char *  out_name = "e:\\tmp\\05_out.mpga";

//	char *  in_name = "e:\\tmp\\a_old_out.exe";
//	char *  out_name = "e:\\tmp\\a_old_out.cmp";

    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]);
        }
	}

    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.WriteHeader();
	    in->Init(&fs_in, 8L*1024 * 1024L, 0, infile_leng);
	}

	out->Init(&fs_out, 8L*1024 * 1024L, 1);

	Coder * coder; 
	if (op_flag == 0) { // compression
		coder = new AdaHuffCoder();
	} else {       // uncompression
		coder = new AdaHuffDecoder();
	}

	coder->SetDebugMode(debug_mode);

	coder->Init(in, out);
	coder->Run();
//	fs_out.close();  // debug

	if (op_flag == 0) {
//		fs_out.open(out_name,  ios_base::app);  // becomes "a" 
	//	fs_out.seekg(-5);
//		fs_out.seekp(0);
//    	out_header.Init(&fs_out);
		out_header.length = coder->GetWriterBitsNum();
		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");
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -