home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / l / lds_10.zip / HUF / DHUFF.C next >
C/C++ Source or Header  |  1990-08-15  |  5KB  |  166 lines

  1. /**************************************************************************
  2.  *                                                                        *
  3.  *  DHUFF.C:    Huffman Decompression Program.                            *
  4.  *              14-August-1990    Bill Demas          Version 1.0         *
  5.  *                                                 *
  6.  *   This program decompresses a file previously compressed with the      *
  7.  *   HUFF1 program.                                                       *
  8.  *                                                                        *
  9.  *           USAGE:   DHUFF <input file> <output file>                    *
  10.  *                                                 *
  11.  *   (DISK to DISK:  Input direct from disk, output direct to disk)       *
  12.  **************************************************************************/
  13.  
  14.  
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17.  
  18. #define    VERBOSE                          /* If defined, prints verbose
  19.                                                program progress when it's
  20.                                                running...                   */
  21.  
  22. short           decomp_tree[512];
  23. unsigned short  code[256];
  24. unsigned long   file_size;
  25. unsigned char   code_length[256];
  26.  
  27. FILE            *ifile, *ofile;
  28.  
  29.  
  30.  
  31. /**************************************************************************
  32.  
  33.  MAIN ()
  34.  
  35.  This is the main program. It performs the Huffman decoding procedure in
  36.  2 separate steps.
  37.  
  38.  I know that this program can be made more compact & faster, but I was more
  39.  interested in UNDERSTANDABILITY !!!
  40.  
  41.  **************************************************************************/
  42.  
  43. void main (argc, argv)
  44. int   argc;
  45. char  *argv[];
  46. {
  47.    void  build_decomp_tree (), decompress_image ();
  48.  
  49.  
  50.    if (argc == 3)
  51.    {
  52.       printf ("\nDHUFF:  Huffman Code Decompression Program.");
  53.       printf ("\n        14-Aug-90  Bill Demas.  Version 1.0\n\n");
  54.  
  55.  
  56.       if ((ifile = fopen (argv[1], "rb")) != NULL)
  57.       {
  58.          fread (&file_size, sizeof (file_size), 1, ifile);
  59.          fread (code, 2, 256, ifile);
  60.          fread (code_length, 1, 256, ifile);
  61.  
  62.          #ifdef VERBOSE
  63.             printf ("(1) Building the tree.\n");
  64.          #endif
  65.  
  66.          build_decomp_tree ();
  67.  
  68.          #ifdef VERBOSE
  69.             printf ("(2) Decompressing & Creating the Output File.\n");
  70.          #endif
  71.  
  72.          if ((ofile = fopen (argv[2], "wb")) != NULL)
  73.          {
  74.             decompress_image();
  75.             fclose (ofile);
  76.          }
  77.          else
  78.             printf ("\nERROR:  Couldn't create output file %s\n", argv[2]);
  79.  
  80.          fclose (ifile);
  81.       }
  82.       else
  83.          printf ("\nERROR:  %s -- File not found!\n", argv[1]);
  84.    }
  85.    else
  86.       printf ("Usage:  DHUFF <input filename> <output filename>\n\n");
  87. }
  88.  
  89.  
  90. /**************************************************************************
  91.  
  92.  BUILD_DECOMP_TREE ()
  93.  
  94.  This function builds the decompression tree.
  95.  **************************************************************************/
  96.  
  97. void  build_decomp_tree ()
  98. {
  99.    register unsigned short  loop1;
  100.    register unsigned short  current_index;
  101.  
  102.    unsigned short  loop;
  103.    unsigned short  current_node = 1;
  104.  
  105.  
  106.    decomp_tree[1] = 1;
  107.  
  108.    for (loop = 0; loop < 256; loop++)
  109.    {
  110.       if (code_length[loop])
  111.       {
  112.      current_index = 1;
  113.      for (loop1 = code_length[loop] - 1; loop1 > 0; loop1--)
  114.      {
  115.         current_index = (decomp_tree[current_index] << 1) +
  116.                 ((code[loop] >> loop1) & 1);
  117.         if (!(decomp_tree[current_index]))
  118.            decomp_tree[current_index] = ++current_node;
  119.      }
  120.      decomp_tree[(decomp_tree[current_index] << 1) +
  121.        (code[loop] & 1)] = -loop;
  122.       }
  123.    }
  124. }
  125.  
  126.  
  127. /**************************************************************************
  128.  
  129.  DECOMPRESS_IMAGE ()
  130.  
  131.  This function decompresses the compressed image.
  132.  **************************************************************************/
  133.  
  134. void  decompress_image ()
  135. {
  136.    register unsigned short  cindex = 1;
  137.    register char            curchar;
  138.    register short           bitshift;
  139.  
  140.    unsigned long  charcount = 0L;
  141.  
  142.  
  143.    while (charcount < file_size)
  144.    {
  145.       curchar = (char) getc (ifile);
  146.  
  147.       for (bitshift = 7; bitshift >= 0; --bitshift)
  148.       {
  149.      cindex = (cindex << 1) + ((curchar >> bitshift) & 1);
  150.  
  151.      if (decomp_tree[cindex] <= 0)
  152.      {
  153.         putc ((int) (-decomp_tree[cindex]), ofile);
  154.  
  155.         if ((++charcount) == file_size)
  156.                bitshift = 0;
  157.             else
  158.                cindex = 1;
  159.      }
  160.      else
  161.         cindex = decomp_tree[cindex];
  162.       }
  163.    }
  164. }
  165.  
  166.