LZW无损压缩


本程序实现了LZW的无损文本压缩,使用C语言实现,整个文件为VC的工程。
资源截图
代码片段和文件信息
#pragma warning(disable:4786)
//...
#include  
#include  
//...
#include 
#include “BitToByte.h“

using namespace std;
using namespace BitToByte;

void init_table();
void decode();
void init_decode_dictionary();


map dictionary;
map decode_dictionary; 
//int Buff[4000];

int num = 0;

int main()
{
init_table();

char ch;

FILE *fp;

int* bits = new int;

if((fp=fopen(“I have a dream.txt““r“))==NULL) 
{
printf(“not open“);
return 1;
}

int code = 0;

// map m;
// m.insert(pair(1“st“));
// dictionary.insert(pair(“student_three“ code));

unsigned char * r = new unsigned char[4];

string str;
string c;

ch = fgetc(fp);
str = ch;

unsigned char * cr = new unsigned char[4]; 

while (true) 
{
ch=fgetc(fp);

c = ch;
if (dictionary.find(str + c) != dictionary.end())
{
str += c;
}
else
{
// cout << (dictionary.find(str))->second << “ “;

int res = (dictionary.find(str))->second;
// cout << res << “ “;
intToCharPoint(res 12 cr);
addToBuff(cr 12);

// printBuff();
// cout << (int)Buff[i] << endl;
num++;

dictionary.insert(map::value_type(str + c dictionary.size()));
str = c;

if (ch == EOF)
break;
}

// putchar(ch);
}
// printBuff();


// cout << num;
saveAsFile(“LZW.txt“);

cout << endl;

decode();


/*
FILE *fp2;

if((fp2=fopen(“LZW.txt““r“))==NULL) 
printf(“not open“);



int k = 0;
while (k < 4884) 
{
k++;

ch=fgetc(fp2);
cout << ch << “ “;
}*/


return 0;
}

void decode()
{
FILE *fp;

if((fp=fopen(“LZW.txt““r“))==NULL) 
printf(“not open“);

init_decode_dictionary();

string str = ““;

string entry;


unsigned char* cp = new unsigned char[3];
int sizeOfByte = 0;
int res = 0;

int num = 0;

bool odd = true;
while (num <= BitToByte::size_of_file) /* 从文件读一字符,显示到屏幕*/
{

if (odd)
{
odd = false;
// sizeOfByte = fread(cp 1 3 fp);
*cp=buff[num];
num++;
*(cp+1)=buff[num];
num++;
*(cp+2)=buff[num];
num++;
// if ((*cp == EOF)||(*(cp+1) == EOF)||(*(cp+2) == EOF))
// break;
// if (sizeOfByte != 3 )
// break;

res = (*cp) * 16 + ((*(cp+1)) >> 4);
}
else
{
res = ((*(cp + 1)) & 0x0f) * 256 + *(cp+2);
odd = true;
}
// ch = Buff[i];
if (decode_dictionary.find(res) != decode_dictionary.end())
entry  = (decode_dictionary.find(res))->second;
else
entry = str + str[0];
cout << entry;

// cout << res << “ “;

if (str != ““)
{
decode_dictionary.insert(map::value_type(decode_dictionary.size() str + entry[0]));
}
str= entry;
i++;
// entry = dictionary
}

// cout << sizeOfByte << endl;

// cout << num << endl;

cout << endl;
}


void init_table()
{

char c

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       4501  2011-10-25 10:53  LZWBitToByte.h

     文件     589861  2011-10-25 10:53  LZWDebugLZW.exe

     文件     865968  2011-10-25 10:53  LZWDebugLZW.ilk

     文件    2575200  2011-10-24 16:58  LZWDebugLZW.pch

     文件    1238016  2011-10-25 10:53  LZWDebugLZW.pdb

     文件     377096  2011-10-25 10:53  LZWDebugmain.obj

     文件      99328  2011-10-25 10:53  LZWDebugvc60.idb

     文件     159744  2011-10-25 10:53  LZWDebugvc60.pdb

     文件       9134  2011-10-23 11:38  LZWI have a dream.txt

     文件       4313  2011-10-24 17:47  LZWLZW.dsp

     文件        529  2011-10-24 16:30  LZWLZW.dsw

     文件      50176  2011-10-25 10:53  LZWLZW.ncb

     文件      50688  2011-10-25 10:53  LZWLZW.opt

     文件       1242  2011-10-25 10:53  LZWLZW.plg

     文件       4885  2011-10-25 10:53  LZWLZW.txt

     文件       5370  2011-10-25 10:52  LZWmain.cpp

     文件          9  2011-10-24 17:43  LZW
es

     文件       3256  2011-10-24 21:50  LZW
es.txt

     文件         14  2011-10-24 17:20  LZW est - 副本.txt

     文件         15  2011-10-24 21:08  LZW est.txt

     目录          0  2011-10-25 10:53  LZWDebug

     目录          0  2011-10-25 10:53  LZW

----------- ---------  ---------- -----  ----

              6039345                    22


版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。

发表评论

评论列表(条)