LZW-master.zip

  • 心中的余火
    了解作者
  • C/C++
    开发工具
  • 4KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • 10 积分
    下载积分
  • 2
    下载次数
  • 2020-10-04 23:08
    上传日期
基于字典的数据压缩算法, 基本的lzw压缩算法
LZW-master.zip
  • LZW-master
  • lzw.c
    890B
  • array.c
    681B
  • algorithms.c
    3.8KB
  • file.c
    1.3KB
  • README
    134B
  • dictionary.c
    2.7KB
内容介绍
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "dictionary.c" #include "file.c" // binary file write & read //#include "list.c" #include "array.c" // a faster array for decompression enum { dictionarySize = 4095, // maximum number of entries defined for the dictionary (2^12 = 4096) codeLength = 12, // the codes which are taking place of the substrings maxValue = dictionarySize - 1 }; // function declarations void compress(FILE *inputFile, FILE *outputFile); void decompress(FILE *inputFile, FILE *outputFile); int decode(int code, FILE * outputFile); // compression void compress(FILE *inputFile, FILE *outputFile) { int prefix = getc(inputFile); if (prefix == EOF) { return; } int character; int nextCode; int index; // LZW starts out with a dictionary of 256 characters (in the case of 8 codeLength) and uses those as the "standard" // character set. nextCode = 256; // next code is the next available string code dictionaryInit(); // while (there is still data to be read) while ((character = getc(inputFile)) != (unsigned)EOF) { // ch = read a character; // if (dictionary contains prefix+character) if ((index = dictionaryLookup(prefix, character)) != -1) prefix = index; // prefix = prefix+character else { // ...no, try to add it // encode s to output file writeBinary(outputFile, prefix); // add prefix+character to dictionary if (nextCode < dictionarySize) dictionaryAdd(prefix, character, nextCode++); // prefix = character prefix = character; //... output the last string after adding the new one } } // encode s to output file writeBinary(outputFile, prefix); // output the last code if (leftover > 0) fputc(leftoverBits << 4, outputFile); // free the dictionary here dictionaryDestroy(); } // decompression // to reconstruct a string from an index we need to traverse the dictionary strings backwards, following each // successive prefix index until this prefix index is the empty index void decompress(FILE * inputFile, FILE * outputFile) { // int prevcode, currcode int previousCode; int currentCode; int nextCode = 256; // start with the same dictionary of 256 characters int firstChar; // prevcode = read in a code previousCode = readBinary(inputFile); if (previousCode == 0) { return; } fputc(previousCode, outputFile); // while (there is still data to read) while ((currentCode = readBinary(inputFile)) > 0) { // currcode = read in a code if (currentCode >= nextCode) { fputc(firstChar = decode(previousCode, outputFile), outputFile); // S+C+S+C+S exception [2.] //printf("%c", firstChar); //appendCharacter(firstChar = decode(previousCode, outputFile)); } else firstChar = decode(currentCode, outputFile); // first character returned! [1.] // add a new code to the string table if (nextCode < dictionarySize) dictionaryArrayAdd(previousCode, firstChar, nextCode++); // prevcode = currcode previousCode = currentCode; } //printf("\n"); } int decode(int code, FILE * outputFile) { int character; int temp; if (code > 255) { // decode character = dictionaryArrayCharacter(code); temp = decode(dictionaryArrayPrefix(code), outputFile); // recursion } else { character = code; // ASCII temp = code; } fputc(character, outputFile); //printf("%c", character); //appendCharacter(character); return temp; }
评论
    相关推荐
    • LZW编码.rar
      这个文档是我大学时候多媒体的一个实验作业,里面要求实现LZW压缩方法。大家可以在文档中自行查阅。
    • LZW.rar
      压缩算法,LZW ,数据压缩的LZW算法,c语言实现。
    • lzw.zip
      lzw编码实现对英文文本的压缩和解压,基于c语言
    • lzw.zip
      a LZW algorithm using matlab code
    • LZW.zip
      LZW encoder and decoder
    • lzw.rar
      lzw coding and decoding for compression
    • c语言实现lzw算法
      信息论作业 B.6输入:任意的数据文件 输出:压缩后的数据
    • LZW相关.rar
      Lossless compression algorithm LZw related papers
    • LZW.zip
      本代码通过利用LZW编码,对数据进行了加解密的功能。
    • lzw_soft.zip
      lzw压缩解压算法源码