2008 July 1st Tuesday (七月 一日 火曜日)

本文分享了作者取消假期专心工作的心得,并详细记录了一个LZW编码器的实现过程。作者计划深入学习日语及Visual C++,考虑到无锡地区日语的重要性超过英语。通过这个LZW编码器实例,读者可以了解基本的压缩算法原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

   I cancelled my holiday appealled by me.  I have to do more in order to pave with the steps of my workmates.
That project is still in design.  I must read some design documents, and some reports.

  In the future, Japanese is my main study goal.  In Wuxi, Japanese is more important than English.  You can
lost your morther languages, you had better not forget Japanese.

  Why?  The reason is so simply, there are many a Japanese corporation, or many a Korean company.  So, I had
a plan to study Visual C++.  After second thought, I made a decision to put off that until I have time or
I settle down.

  Yesterday I wrote a lzw encoder example according to some lzw technique article.

#include <stdio.h>
#include <string.h>

#include <iostream>

#define CLEAR 256
#define END 257
#define CELL_NUM (2^6)
#define TARGET_INIT_VAL 258

using namespace std;

// defined types
typedef unsigned char byte_t;

// yalzw namespace
namespace yalzw{

//Cell
class Cell{
private:
byte_t prefix;
byte_t suffix;
int tag;

public:
Cell(){
prefix = 0xff;
suffix = 0xff;
tag = -1;
}

Cell(byte_t x, byte_t y){
prefix = x;
suffix = y;
tag = -1;
}

byte_t getPrefix() const{ return prefix; }
byte_t getSuffix() const{ return suffix; }
int getTag() const{ return tag; }

void setPrefix(byte_t pre) { prefix = pre; }
void setSuffix(byte_t suf) { suffix = suf; }
void setTag(int tag_val) { tag = tag_val; }

Cell& operator= (const Cell& cell) {
prefix = cell.getPrefix();
suffix = cell.getSuffix();
tag = cell.getTag();

return (*this);
}

bool equal(const Cell& cell) {
return ( prefix == cell.getPrefix() ) && ( suffix == cell.getSuffix() );
}
};

//Table
class Table{
private:
int target;
int tail;   // next position
Cell cells[CELL_NUM];

public:
Table(){
tail = 0;
target = TARGET_INIT_VAL;
}

int getTarget(){
int ret = target;
target++;

return ret;
}

void add(Cell& entry) {
cells[tail] = entry;
cells[tail].setTag(getTarget()); // set a target for a new cell.
if ( tail < CELL_NUM )
tail++;
}

// look up a entry in table.
// if found, return the target of a cell in the table;
// otherwise, return 0.
int lookup(Cell& entry) {
int i;

for (i = 0; i < tail; i++){
if (cells[i].equal(entry))
break;
}

if (i < tail)
return cells[i].getTag();
else
return 0;

}

void dump(){
for (int i = 0; i < tail; i++){
cout<<i<<"  ("<<cells[i].getPrefix()<<", "<<cells[i].getSuffix()<<") "<<cells[i].getTag()<<endl;
}
}

~Table(){}

};

};

using namespace yalzw;

int main(int argc, char *argv[]){
FILE *in, *out;
char pre, suf;
int tag;

if (argc != 3){
printf("%s filename lzw-filename/n", argv[0]);
exit(1);
}

//initialization
Table table;
Cell entry;

in = fopen(argv[1], "rb");
out = fopen(argv[2], "wb");
if (in && out){
pre = fgetc(in);
suf = fgetc(in);

while ( suf != EOF ){
entry.setPrefix(pre);
entry.setSuffix(suf);
entry.setTag(-1);

tag = table.lookup(entry);
if (tag != 0){
pre = tag;
fprintf(out, "%d", pre);  // output code.
}
else{ //no found
table.add(entry);
pre = suf;
fprintf(out, "%c", pre);  // output char.
}

suf = fgetc(in);
}//while
}

if (in) fclose(in);
if (out) fclose(out);

return 0;
}

  The above code is just used to get across how to encode data in lzw way.  There is still some problem.
Especially, there is not a decoder.  But the decoder is so simple as to implement if you unstand the principle
of the encoder.

  Today is the Party Day.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值