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.
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.