LZW Data Compression Algorithm

LZW is a “dictionary”-based data compression algorithm, created by Lempel and Ziv in 1978 (LZ78) and further refined by Welch in 1984. LZW algorithm uses a dynamically generated dictionary and and encodes strings by a reference to the dictionary.

The Basic Encoding Algorithm

Step 1. Initialize dictionary to contain one entry for each byte. Initialize the encoded string with the first byte of the input stream.

Step 2. Read the next byte from the input stream.

Step 3. If the byte is an EOF goto step 6.

Step 4. If concatenating the byte to the encoded string produces a string that is in the dictionary, then concatenate the the byte to the encoded string and go to step 2.

Step 5. If concatenating the byte to the encoded string produces a string that is not in the dictionary, then add the new sting to the dictionary. write the code for the encoded string to the output stream. set the encoded string equal to the new byte and go to step 2.

Step 6. Write out code for encoded string and exit.

void compress(ifstream& ifile, ofstream& ofile){
  unordered_map<string,unsigned short>dictionary;
  //Build the dictionary
  unsigned short Dictsize=256;
  for(unsigned short i=0;i<Dictsize;i++) 
    dictionary[string(1,i)]=i;

  char z;
  string S; //empty string

  ifile.seekg(0, ifile.beg);
  while(!ifile.eof()){
    ifile.read((char*)&z,sizeof(char));

    if(dictionary.count(S+z))//can find S
      S+=z;
    else{//S is not in dictionary D
      //ofile<<dictionary[S];   //output pointer (S,D)
      unsigned short tmp = dictionary[S];
      ofile.write((char*)&tmp, sizeof(unsigned short));
      dictionary[S+z] = Dictsize++; //add to dictionary
      S = z;
    }
  }
  if(!S.empty()){
    unsigned short tmp = dictionary[S];
    ofile.write((char*)&tmp, sizeof(unsigned short));
    //ofile<<dictionary[S];
  }

  ofile.flush();
}

The Basic Decoding Algorithm

Step 1. Initialize dictionary to contain one entry for each byte.

Step 2. Read the first code word from the input stream and write out the byte it encodes.

Step 3. Read the next code word from the input stream.

Step 4. If the code word is an EOF exit.

Step 5. Write out the string encoded by the code word.

Step 6. Concatenate the first character in the new code word to the string produced by the previous code word and add the resulting string to the dictionary.

Step 7. Go to step 3.

void decompress(ifstream& ifile, ofstream& ofile){
  //Build the dictionary
  unordered_map<unsigned short,string>inv_dictionary;
  unsigned short Dictsize=256;
  for(unsigned short i=0;i<Dictsize;i++)
    inv_dictionary[i] = string(1,i);

  unsigned short k;
  string S;
  string entry;
  Dictsize--;

  ifile.seekg(0, ifile.beg);
  while(!ifile.eof()){
    ifile.read((char*)&k,sizeof(unsigned short));
    if(inv_dictionary.count(k))
      entry = inv_dictionary[k];
    else if(k==Dictsize)
      entry = S+ S[0];
    else
      throw "Bad compression code";

    ofile<<entry;
    inv_dictionary[Dictsize++] = S + entry[0];
    S = entry;
  }
  ofile.flush();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值