home *** CD-ROM | disk | FTP | other *** search
/ RBBS in a Box Volume 1 #3.1 / RBBSIABOX31.cdr / sspl / sq_usq.txt < prev    next >
Text File  |  1984-12-09  |  14KB  |  301 lines

  1.  
  2.                      Data Compression Techniques
  3.                           Using SQ and USQ
  4.  
  5.                            Mark DiVecchio
  6.                   San Diego IBM PC Users Group
  7.  
  8.          In any computer system, efficient management of the file
  9.          storage space is important.  The two programs SQ and USQ
  10.          reduce the size of data files by using the Huffman Data
  11.          Compression Algorithm.
  12.  
  13.          A file is composed of a sequence of bytes.  A byte is
  14.          composed of 8 bits.  That byte can have a decimal value
  15.          between 0 and 255.  A typical text file, like a C language
  16.          program, is composed of the ASCII character set (decimal 0 to
  17.          127).  This character set only requires seven of the eight
  18.          bits in a byte.  Just think -- if there were a way to use
  19.          that extra bit for another character, you could reduce the
  20.          size of the file by 12.5%.  For those of you who use upper
  21.          case characters only, you use only about 100 of the ASCII
  22.          codes from 0 to 255.  You could save 60% of your storage if
  23.          the bits not needed within each byte could be used for other
  24.          characters.  What if you could encode the most frequently
  25.          used characters in the file with only one bit?  For example,
  26.          if you could store the letter "e" (the most commonly used
  27.          letter in the English language) using only one bit :  "1",
  28.          you could save 87.5% of the space that the "e" would normally
  29.          take if it were stored as the ASCII "0110 0101" binary.
  30.  
  31.          The answer to these questions is the SQ and USQ programs.
  32.  
  33.          The SQ program uses the Huffman coding technique to search
  34.          for the frequency of use of each of the 256 possible byte
  35.          patterns, and it then assigns a translation for each
  36.          character to a bit string.  All of these bit strings are
  37.          placed end to end and written onto a disk file.  The encoding
  38.          information is also put on the file since the USQ program
  39.          needs to know the character distribution of the original
  40.          file.
  41.  
  42.          The USQ program reads in the encoding information and then
  43.          reads in the encoded file.  It is a simple matter to scan the
  44.          encoded file and produce an output file which is identical to
  45.          the file that SQ started with.
  46.  
  47.          Huffman Coding Technique
  48.  
  49.          This is by far the most popular encoding technique in use
  50.          today.  The Huffman encoding replaces fixed bit characters
  51.          with variable length bit strings.  The length of the bit
  52.          string is roughly inversely proportional to the frequency of
  53.          occurrence of the character.  For those of you inclined to
  54.          such symbolism:
  55.  
  56.          Length of bit  ~= log  (character
  57.               string          2  probability)
  58.  
  59.          The implementation of the algorithm which we will discuss
  60.          encodes fixed bit strings of length 8.
  61.  
  62.          This algorithm requires two passes through the input file.
  63.          The first pass builds a table of 256 entries showing the
  64.          frequency of each occurrence of each of the 256 possible
  65.          values for a byte of information.
  66.  
  67.          Once the counting is complete, the algorithm must decide
  68.          which bit strings to associate with each of the 256
  69.          characters that were found in the file.  Note that if a
  70.          particular byte value was never used, no string association
  71.          is needed.
  72.  
  73.          The second pass through the input file converts each byte
  74.          into its encoded string.  Remember that when the output file
  75.          is created, the information used for encoding must also be
  76.          written on the file for use by the decoding program.
  77.  
  78.          The decoding program reads the encoding information from the
  79.          file and then starts reading the bit strings.  As soon as
  80.          enough bits are read to interpret a character, that character
  81.          is written onto the final output file.  See the next two
  82.          sections on how SQ and USQ actually implement this.
  83.  
  84.          Even though this article primarily has addresses ASCII input
  85.          files, there is nothing which restricts this algorithm to
  86.          ASCII.  It will work on binary files (.COM or .EXE) as well.
  87.          But since the length of the encoded bit string is
  88.          approximately equal to the inverse of the frequency of
  89.          occurrence of each 8 bit byte, a binary file may not compress
  90.          very much.  This is because a binary file most likely has a
  91.          uniform distribution over the 256 values in a byte.  A
  92.          machine language program is not like the English language
  93.          where the letter "e" is used far more than other letters.  If
  94.          the distribution is uniform, the encoded bit strings will all
  95.          be the same length and the encoded file could be longer than
  96.          the original (because of the encoding information on the
  97.          front of the file).  All of this has to be qualified, because
  98.          machine language programs tend to use a lot of "MOV"
  99.          instructions and have a lot of bytes of zeros so that
  100.          encoding .COM and .EXE files does save some disk space.
  101.  
  102.                 SQ
  103.  
  104.          The SQ program is an example of the Huffman algorithm.
  105.  
  106.          The first thing that SQ does is read through the input file
  107.          and create a distribution array for the 256 possible
  108.          characters.  This array contains counts of the number of
  109.          occurrences of each of the 256 characters.  The program
  110.          counts these values in a 16 bit number.  It makes sure that,
  111.          if you are encoding a big file, counts do not exceed a 16 bit
  112.          value.  This is highly unlikely, but it must be accounted
  113.          for.
  114.  
  115.          At the same time, SQ removes strings of identical characters
  116.          and replaces them with the ASCII character DLE followed by a
  117.          character count of 2-255.  SQ replaces the ASCII DLE with the
  118.          pair of characters:  DLE DLE.  This is not related to the
  119.          Huffman algorithm but just serves to compress the file a
  120.          little more.
  121.  
  122.          Once SQ has scanned the input file, it creates a binary tree
  123.          structure containing this frequency occurrence information.
  124.          The most frequently occurring characters have the shortest
  125.          path from the root to the node, and the least frequently
  126.          occurring characters have the longest path.  For example, if
  127.          your file were:
  128.  
  129.          ABRACADABRA   (a very simple and
  130.                         magical example)
  131.  
  132.          The table of frequency of occurrences would be:
  133.  
  134.          Letter         # of Occurrences
  135.          ------         ---------------
  136.            A                 5
  137.            B                 2
  138.            C                 1
  139.            D                 1
  140.            R                 2
  141.            all the rest      0
  142.  
  143.          Since the letter "A" occurs most often, it should have the
  144.          shortest encoded bit string.  The letters "C" and "D" should
  145.          have the longest.  The other characters which don't appear in
  146.          the input file don't need to be considered.
  147.  
  148.          SQ would create a binary tree to represent this information.
  149.          The tree might look something like this (for purposes of
  150.          discussion only):
  151.  
  152.              root    <---Computer trees are
  153.           /    \      always upside down!
  154.         1       0   <-- This is called a
  155.       /       /   \        node.
  156.      A      1       0
  157.           /       /   \  <--This is called
  158.         B       1       0    branch.
  159.               /       /   \
  160.             C       1       0
  161.                   /           \
  162.                 D               R  <-This
  163.                                      is a
  164.                                      leaf
  165.  
  166.  
  167.          From this our encoded bit strings which are kept in a
  168.          translation table would be:
  169.  
  170.            Table Entry  Character  Binary
  171.            -----------  ---------  ------
  172.                 1           A      1
  173.                 2           B      01
  174.                 3           C      001
  175.                 4           D      0001
  176.                 5           R      0000
  177.  
  178.  
  179.          The output file would be:
  180.  
  181.  
  182.              A B  R    A C   A D    A B  R    A
  183.              ----------------------------------
  184.              1 01 0000 1 001 1 0001 1 01 0000 1
  185.              (binary)
  186.  
  187.              A1 31 A1
  188.              (hex)
  189.  
  190.  
  191.          We have reduced the size of your file from ten bytes to thre
  192.          bytes for a 70% savings.  For this simple example, things
  193.          aren't that well off since we must put the binary tree
  194.          encoding information onto the file as well.  So the file size
  195.          grew a lot.  But consider a file with the word ABRACADABRA
  196.          repeated 100,000 times.  Now the encoding information is
  197.          going to be a very very small percentage of the output file
  198.          and the file will shrink tremendously.
  199.  
  200.          SQ opens the output file and writes out the binary tree
  201.          information.  Then SQ rewinds the input file and rereads it
  202.          from the beginning.  As it reads each character, it looks
  203.          into the translation table and outputs the corresponding bit
  204.          string.
  205.  
  206.          SQ is a little more complicated than what I have outlined
  207.          since it must operate in the real world of hardware, but this
  208.          is a fairly complete description of the algorithm.
  209.  
  210.              USQ
  211.  
  212.          The USQ program is very straightforward.  It reads in the
  213.          encoding information written out by SQ and builds the
  214.          identical binary tree that SQ used to encode the file.
  215.  
  216.          USQ then reads the input file as if it were a string of bits.
  217.          Starting at the root of the tree, it traverses one branch of
  218.          the tree with each input bit.  If it has reached a leaf, it
  219.          has a character and that character is written to the output
  220.          file.  USQ then starts at the root again with the next bit
  221.          from the input file.
  222.  
  223.          What does it all mean?
  224.  
  225.          Now that we understand the algorithm and a little about how
  226.          the SQ and USQ programs work, we can use that knowledge to
  227.          help us run our systems a little more efficiently.  (So all
  228.          of this theory is worth something after all!).
  229.  
  230.          1.  Files must be above a threshold size, or else the output
  231.          file will be longer than the input file because of the
  232.          decoding information put at the beginning of the compressed
  233.          data.  We don't know the exact size of the threshold because
  234.          the encoding binary tree information depends on the
  235.          distribution of the characters in a file.  At least we know
  236.          to check the size of the encoded file after we run SQ to make
  237.          sure our file didn't grow.
  238.  
  239.          2.  Some files will not compress well if they have a uniform
  240.          distribution of byte values, for example, .COM or .EXE files.
  241.          This is because of the way SQ builds the tree.  Remember that
  242.          bytes with the same frequency of occurrence are at the same
  243.          depth (usually) in the tree.  So if all of the bytes have the
  244.          same depth, the output strings are all the same length.
  245.  
  246.          3.  SQ reads the input file twice.  If you can, use RAM disk
  247.          at least for the input file and for both files if you have
  248.          the room.  The next best case is to use two floppy drives,
  249.          one for input and one for output.  This will cause a lot of
  250.          disk starts and stops but not much head movement.  Worst case
  251.          is to use one floppy drive for both input and output.  This
  252.          will cause a lot of head movement as the programs alternate
  253.          between the input and output files.
  254.  
  255.          Other Compression Techniques
  256.  
  257.          RUN-LENGTH ENCODING
  258.  
  259.          Run-length encoding is a technique whereby sequences of
  260.          identical bytes are replaced by the repeated byte and a byte
  261.          count.  As you might guess, this method is effective only on
  262.          very specialized files.  One good candidate is a screen
  263.          display buffer.  A screen is made up mostly of "spaces".  A
  264.          completely blank line could be reduced from 80 bytes of
  265.          spaces to one space followed by a value of 80.  To go from 80
  266.          bytes down to two bytes is a savings of almost 98%.  You
  267.          might guess that for text files or binary files, this
  268.          technique does not work well at all.
  269.  
  270.          ADAPTIVE COMPRESSION
  271.  
  272.          This technique replaces strings of characters of code.  For
  273.          example, the string "ABRACADABRA" would be replaced by a
  274.          code.  Typical algorithms use a 12 bit code.  The algorithm
  275.          is unique in that it only requires a single pass through the
  276.          input file as the encoding is taking place.  The current
  277.          incarnation of this procedure is called the LZW method (after
  278.          co-inventors; A.  Lempel, J.  Ziv and T.  Welch).  This
  279.          algorithm claims a savings of 66% on machine language files
  280.          and up to 83% on COBOL files.
  281.  
  282.          Other Reading
  283.  
  284.          If you are interested in reading more about data compression
  285.          techniques, you may be interested in these articles:
  286.  
  287.  
  288.          H.K.  Reghbati, "An Overview of Data Compression Techniques,"
  289.          Computer Magazine, Vol.  14, No.  4, April 1981, pp.  71-76.
  290.  
  291.          T.A.  Welch, "A Technique for High-Performance Data
  292.          Compression", Computer Magazine, Vol 17, No.  6, June 1984,
  293.          pp.  8-19.
  294.  
  295.          J.  Ziv and A.  Lempel, "A Universal Algorithm for Sequential
  296.          Data Compression," IEEE Transactions on Information Theory,
  297.          Vol.  It-23, No.3, May 1977, pp. 337-343.
  298. 
  299.  
  300. Press ENTER to continue: ," IEEE Transactions on Information Theory,
  301.