⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lsw.txt

📁 C语言实现的LSW编码
💻 TXT
字号:
 //   LZW   compression   
    
  //   Based   on   ORIGSRC\COdLZW.C   written   by   David   Bourgin   
    
  #include   "stdafx.h"   
  #include   <stdio.h>   
  #include   <malloc.h>   
  #include   <stdlib.h>   
    
  #define   EXPORTING   
  #include   "comprlib.h"   
  #include   "lzw.h"   
    
  static   unsigned   long   val_to_read   =   0,   val_to_write   =   0;   
  static   unsigned   char   bit_counter_to_read   =   0,   bit_counter_to_write   =   0;   
    
  #define   CHAR_DIC_VAL(ptr_dic)   ((*(ptr_dic)).character)   
  #define   CODE_DIC_VAL(ptr_dic)   ((*(ptr_dic)).code)   
  #define   PLEFT_DIC_VAL(ptr_dic)   ((*(ptr_dic)).left_ptr)   
  #define   PRIGHT_DIC_VAL(ptr_dic)   ((*(ptr_dic)).right_ptr)   
    
  //   Enforces   including   marker   codes   and   initalization_code   and   
  //   end_information_code.   Comment   out   to   not   do   that.   
  #define   TYPE_GIF_ENCODING   
    
  //   Word   counter   already   known   in   dictionary   
  static   unsigned   int   index_dic;   
  //   Bit   counter   in   encoding   
  static   unsigned   char   bit_counter_encoding;   
    
  //   2   ^   EXP2_DIC_MAX   gives   the   maximum   word   counter   in   the   dictionary   
  //   during   all   the   compressions.       Possible   values:   3   to   25   
  //   Note:   Higher   than   12   can   make   some   memory   allocation   errors   depending   
  //   on   compiler   and   computer.   
  #define   EXP2_DIC_MAX         12   
    
  //   Maxium   word   counter   in   dictionary   during   one   compression.     Ranges   from   
  //   end_information_code   to   2   ^   EXP2_DIC_MAX   
  static   unsigned   int   index_dic_max;   
    
  //   Bit   counter   for   each   data   input.     If   input_bit_counter   =   1,   we   can   
  //   compress/decompress   monochrome   pictures   if   with   input_bit_counter   =   8   
  //   256-color   (8-bit)   pictures   or   any   kind   of   files   can   be   handled.   
  static   unsigned   char   input_bit_counter;   
    
  //   Bit   counter   to   encode   "initalization_code"   
  static   unsigned   char   bit_counter_min_encoding;   
      
  //   Both   are   consecutive   coming   up   just   after   the   last   known   word   in   the   
  //   inital   dictionary.   
  static   unsigned   int   initialization_code,   end_information_code;   
    
  static   p_dic_val   dictionary[1   <<   EXP2_DIC_MAX];   
    
  //   First   initalization   of   the   dictionary   
  static   void   init_dictionary1()   
  {   
          register   unsigned   int   i;   
    
          index_dic_max   =   1   <<   12;     //   Possible   values:   2   ^   3   to   2   ^   EXP2_DIC_MAX   
          input_bit_counter   =   8;         //   Can   be   1   to   EXP2_DIC_MAX   -   1   
    
          if   (input_bit_counter   ==   1)   
                  bit_counter_min_encoding   =   3;   
          else   
                  bit_counter_min_encoding   =   input_bit_counter   +   1;   
          initialization_code   =   1   <<   (bit_counter_min_encoding   -   1);   
    
  #ifdef   TYPE_GIF_ENCODING   
          end_information_code   =   initialization_code   +   1;   
  #else   
          end_information_code   =   initialization_code   -   1;   
  #endif   
    
          for   (i   =   0;   i   <=   end_information_code;   i++)   
          {   
                  if   ((dictionary[i]   =   (p_dic_val)malloc(sizeof(t_dic_val)))   ==   NULL)   
                  {   
                          while   (i)   
                          {   
                                  --i;   
                                  free   (dictionary[i]);   
                          }   
                          EXCEPTION   (ERR_MEMORY,   "Memory   allocation   for   dictionary   failed",   
                                                                        "init_dictionary1()");   
                  }   
                  CHAR_DIC_VAL   (dictionary[i])   =   i;   
                  CODE_DIC_VAL   (dictionary[i])   =   i;   
                  PLEFT_DIC_VAL(dictionary[i])   =   NULL;   
                  PRIGHT_DIC_VAL(dictionary[i])   =   NULL;   
          }   
          for   (;   i   <   index_dic_max;   i++)   
                  dictionary[i]   =   NULL;   
          index_dic   =   end_information_code   +   1;   
          bit_counter_encoding   =   bit_counter_min_encoding;   
  }   
    
  //   Initalization   of   the   dictionary   during   encoding   
  static   void   init_dictionary2   ()   
  {   
          register   unsigned   int   i;   
          for   (i   =   0;   i   <   index_dic_max;   i++)   
                  PLEFT_DIC_VAL(dictionary[i])   =   PRIGHT_DIC_VAL(dictionary[i])   =   NULL;   
          index_dic   =   end_information_code   +   1;   
          bit_counter_encoding   =   bit_counter_min_encoding;   
  }   
    
  //   Frees   memory   allocated   for   the   dictionary   
  static   void   remove_dictionary   ()   
  {   
          register   unsigned   int   i;   
          for   (i   =   0;   (i   <   index_dic_max)   &&   (dictionary[i]   !=   NULL);   i++)   
                  free   (dictionary[i]);   
  }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -