DHUFF.C
Upload User: szqxhk
Upload Date: 2010-02-15
Package Size: 54k
Code Size: 5k
Development Platform:

Visual C++

  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. #include <stdio.h>
  14. #include <stdlib.h>
  15. #define    VERBOSE                          /* If defined, prints verbose
  16.                                                program progress when it's
  17.                                                running...                   */
  18. short           decomp_tree[512];
  19. unsigned short  code[256];
  20. unsigned long   file_size;
  21. unsigned char   code_length[256];
  22. FILE            *ifile, *ofile;
  23. /**************************************************************************
  24.  MAIN ()
  25.  This is the main program. It performs the Huffman decoding procedure in
  26.  2 separate steps.
  27.  I know that this program can be made more compact & faster, but I was more
  28.  interested in UNDERSTANDABILITY !!!
  29.  **************************************************************************/
  30. void main (argc, argv)
  31. int   argc;
  32. char  *argv[];
  33. {
  34.    void  build_decomp_tree (), decompress_image ();
  35.    if (argc == 3)
  36.    {
  37.       printf ("nDHUFF:  Huffman Code Decompression Program.");
  38.       printf ("n        14-Aug-90  Bill Demas.  Version 1.0nn");
  39.       if ((ifile = fopen (argv[1], "rb")) != NULL)
  40.       {
  41.          fread (&file_size, sizeof (file_size), 1, ifile);
  42.          fread (code, 2, 256, ifile);
  43.          fread (code_length, 1, 256, ifile);
  44.          #ifdef VERBOSE
  45.             printf ("(1) Building the tree.n");
  46.          #endif
  47.          build_decomp_tree ();
  48.          #ifdef VERBOSE
  49.             printf ("(2) Decompressing & Creating the Output File.n");
  50.          #endif
  51.          if ((ofile = fopen (argv[2], "wb")) != NULL)
  52.          {
  53.             decompress_image();
  54.             fclose (ofile);
  55.          }
  56.          else
  57.             printf ("nERROR:  Couldn't create output file %sn", argv[2]);
  58.          fclose (ifile);
  59.       }
  60.       else
  61.          printf ("nERROR:  %s -- File not found!n", argv[1]);
  62.    }
  63.    else
  64.       printf ("Usage:  DHUFF <input filename> <output filename>nn");
  65. }
  66. /**************************************************************************
  67.  BUILD_DECOMP_TREE ()
  68.  This function builds the decompression tree.
  69.  **************************************************************************/
  70. void  build_decomp_tree ()
  71. {
  72.    register unsigned short  loop1;
  73.    register unsigned short  current_index;
  74.    unsigned short  loop;
  75.    unsigned short  current_node = 1;
  76.    decomp_tree[1] = 1;
  77.    for (loop = 0; loop < 256; loop++)
  78.    {
  79.       if (code_length[loop])
  80.       {
  81.  current_index = 1;
  82.  for (loop1 = code_length[loop] - 1; loop1 > 0; loop1--)
  83.  {
  84.     current_index = (decomp_tree[current_index] << 1) +
  85.     ((code[loop] >> loop1) & 1);
  86.     if (!(decomp_tree[current_index]))
  87.        decomp_tree[current_index] = ++current_node;
  88.  }
  89.  decomp_tree[(decomp_tree[current_index] << 1) +
  90.    (code[loop] & 1)] = -loop;
  91.       }
  92.    }
  93. }
  94. /**************************************************************************
  95.  DECOMPRESS_IMAGE ()
  96.  This function decompresses the compressed image.
  97.  **************************************************************************/
  98. void  decompress_image ()
  99. {
  100.    register unsigned short  cindex = 1;
  101.    register char            curchar;
  102.    register short           bitshift;
  103.    unsigned long  charcount = 0L;
  104.    while (charcount < file_size)
  105.    {
  106.       curchar = (char) getc (ifile);
  107.       for (bitshift = 7; bitshift >= 0; --bitshift)
  108.       {
  109.  cindex = (cindex << 1) + ((curchar >> bitshift) & 1);
  110.  if (decomp_tree[cindex] <= 0)
  111.  {
  112.     putc ((int) (-decomp_tree[cindex]), ofile);
  113.     if ((++charcount) == file_size)
  114.                bitshift = 0;
  115.             else
  116.                cindex = 1;
  117.  }
  118.  else
  119.     cindex = decomp_tree[cindex];
  120.       }
  121.    }
  122. }