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 / COMP / EXPAND-2.C < prev    next >
C/C++ Source or Header  |  1990-06-02  |  5KB  |  164 lines

  1. /*
  2.  * Listing 11 -- expand-2.c
  3.  *
  4.  * This module is the driver program for a variabler order finite
  5.  * context expansion program.  The maximum order is determined by
  6.  * command line option.  This particular version can respond to
  7.  * the FLUSH code inserted in the bit stream by the compressor.
  8.  *
  9.  * To build this program:
  10.  *
  11.  * Turbo C:     tcc -w -mc expand-2.c model-2.c bitio.c coder.c
  12.  * QuickC:      qcl /W3 /AC expand-2.c model-2.c bitio.c coder.c
  13.  * Zortech:     ztc -mc expand-2.c model-2.c bitio.c coder.c
  14.  * *NIX:        cc -o expand-2 expand-2.c model-2.c bitio.c coder.c
  15.  *
  16.  *
  17.  * Command line options:
  18.  *
  19.  *  -f text_file_name  [defaults to test.inp]
  20.  *  -c compressed_file_name [defaults to test.cmp]
  21.  *  -o order [defaults to 3 for model-2]
  22.  */
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include "coder.h"
  27. #include "model.h"
  28. #include "bitio.h"
  29.  
  30. /*
  31.  * Declarations for local procedures.
  32.  */
  33. void initialize_options( int argc, char **argv );
  34. void print_compression( void );
  35.  
  36. /*
  37.  * Variables used throughout this particular file.
  38.  */
  39. FILE *text_file;
  40. FILE *compressed_file;
  41.  
  42. /*
  43.  * The main loop for expansion is very similar to the expansion routine
  44.  * used in the simpler compression program, EXPAND-1.C.  The routine
  45.  * first has to initialize the standard i/o, the bit oriented i/o,
  46.  * the arithmetic coder, and the model.  The decompression loop
  47.  * differs in a couple of respects.  First of all, it handles the
  48.  * special ESCAPE character, by removing them from the input
  49.  * bit stream but just throwing them away otherwise.  Secondly,
  50.  * it handles the special FLUSH character.  Once the main decoding
  51.  * loop is done, the cleanup code is called, and the program exits.
  52.  */
  53. void main( int argc, char *argv[] )
  54. {
  55.     SYMBOL s;
  56.     int c;
  57.     int count;
  58.     long int counter = 0;
  59.  
  60.     initialize_options( --argc, ++argv );
  61.     initialize_model();
  62.     initialize_input_bitstream();
  63.     initialize_arithmetic_decoder( compressed_file );
  64.     for ( ; ; )
  65.     {
  66.         do
  67.         {
  68.             get_symbol_scale( &s );
  69.             count = get_current_count( &s );
  70.             c = convert_symbol_to_int( count, &s );
  71.         remove_symbol_from_stream( compressed_file, &s );
  72.         } while ( c == ESCAPE );
  73.         if ( c == DONE )
  74.             break;
  75.         if ( ( ++counter & 0xff ) == 0 )
  76.         print_compression();
  77.     if ( c != FLUSH )
  78.         putc( (char) c, text_file );
  79.     else
  80.     {
  81.         fprintf( stderr, "\rFlushing...      \r" );
  82.         flush_model();
  83.     }
  84.     update_model( c );
  85.     add_character_to_model( c );
  86.     }
  87.     print_compression();
  88.     fputc( '\n', stderr );
  89.     exit( 0 );
  90. }
  91.  
  92. /*
  93.  * This routine checks for command line options, and opens the
  94.  * input and output files.  The only other command line option
  95.  * besides the input and output file names is the order of the model,
  96.  * which defaults to 3.
  97.  */
  98. void initialize_options( int argc, char **argv )
  99. {
  100.     char text_file_name[ 81 ];
  101.     char compressed_file_name[ 81 ];
  102.  
  103.     strcpy( compressed_file_name, "test.cmp" );
  104.     strcpy( text_file_name, "test.out" );
  105.     while ( argc-- > 0 )
  106.     {
  107.         if ( strcmp( *argv, "-f" ) == 0 )
  108.     {
  109.         argc--;
  110.         strcpy( text_file_name, *++argv );
  111.     }
  112.         else if ( strcmp( *argv, "-c" ) == 0 )
  113.     {
  114.         argc--;
  115.         strcpy( compressed_file_name, *++argv );
  116.     }
  117.         else if ( strcmp( *argv, "-o" ) == 0 )
  118.     {
  119.         argc--;
  120.             max_order = atoi( *++argv );
  121.     }
  122.     else
  123.     {
  124.             fprintf( stderr, "\nUsage: EXPAND-2 [-o order] " );
  125.             fprintf( stderr, "[-f text file] [-c compressed file]\n" );
  126.             exit( -1 );
  127.     }
  128.     argc--;
  129.     argv++;
  130.     }
  131.     text_file = fopen( text_file_name, "wb" );
  132.     compressed_file = fopen( compressed_file_name, "rb" );
  133.     setvbuf( text_file, NULL, _IOFBF, 4096 );
  134.     setbuf( stdout, NULL );
  135.     printf( "Decoding %s to %s, order %d.\n",
  136.             compressed_file_name ,
  137.             text_file_name,
  138.             max_order );
  139. }
  140.  
  141. /*
  142.  * This routine is called to print the current compression ratio.
  143.  * It prints out the number of input bytes, the number of output bytes,
  144.  * and the bits per byte compression ratio.   This is done both as a
  145.  * pacifier and as a seat-of-the-pants diagnostice.  A better version
  146.  * of this routine would also print the local compression ratio.
  147.  */
  148. void print_compression()
  149. {
  150.     long input_bytes;
  151.     long output_bytes;
  152.  
  153.     output_bytes = ftell( text_file );
  154.     input_bytes = bit_ftell_input( compressed_file );
  155.     if ( output_bytes == 0 )
  156.         output_bytes = 1;
  157.     fprintf( stderr,
  158.              "\r%ld/%ld, %2.3f    ",
  159.          input_bytes,
  160.          output_bytes,
  161.              8.0 * input_bytes / output_bytes );
  162. }
  163.  
  164.