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-1.C < prev    next >
C/C++ Source or Header  |  1990-06-02  |  4KB  |  149 lines

  1. /*
  2.  * Listing 7 -- expand-1.c
  3.  *
  4.  * This listing is the driver program for the decompression routine
  5.  * using the order 0 fixed context model.  It follows the model
  6.  * shown in BILL.C for a decompression program, by reading in
  7.  * high, low, range values from the arithmetic coder, converting
  8.  * them to integer symbols using the model code.  Finally, the
  9.  * character is removed from the input code, and the model is updated.
  10.  * When an EOF is detected, the program exits.
  11.  *
  12.  * To build this program:
  13.  *
  14.  * Turbo C:     tcc -w expand-1.c model-1.c bitio.c coder.c
  15.  * QuickC:      qcl /W3 expand-1.c model-1.c bitio.c coder.c
  16.  * Zortech:     ztc expand-1.c model-1.c bitio.c coder.c
  17.  * *NIX:        cc -o expand-1 expand-1.c model-1.c bitio.c coder.c
  18.  *
  19.  * Command line options:
  20.  *
  21.  *  -f text_file_name  [defaults to test.inp]
  22.  *  -c compressed_file_name [defaults to test.cmp]
  23.  */
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include "coder.h"
  28. #include "model.h"
  29. #include "bitio.h"
  30.  
  31. /*
  32.  * Local function prototypes.
  33.  */
  34. void initialize_options( int argc, char **argv );
  35. void print_compression(void);
  36.  
  37. /*
  38.  * The two file names are used by several routines in this module.
  39.  */
  40. FILE *text_file;
  41. FILE *compressed_file;
  42.  
  43. /*
  44.  * The main procedure here is a simple straight-line piece of code.
  45.  * first, the files, model, bit-oriented I/O, and the arithmetic
  46.  * decoder are all initialized.  Then, the program sits in a loop
  47.  * reading in symbol ranges, converting them, writing them out to
  48.  * the text file, and updating the model.  When this is all done,
  49.  * a little clean up activity takes place, and the program exits.
  50.  */
  51. void main( int argc, char *argv[] )
  52. {
  53.     SYMBOL s;
  54.     int c;
  55.     long int counter = 0;
  56.     int count;
  57.  
  58.     initialize_options( --argc, ++argv );
  59.     initialize_model();
  60.     initialize_input_bitstream();
  61.     initialize_arithmetic_decoder( compressed_file );
  62.     for ( ; ; )
  63.     {
  64.     get_symbol_scale( &s );
  65.     count = get_current_count( &s );
  66.     c = convert_symbol_to_int( count, &s );
  67.     remove_symbol_from_stream( compressed_file, &s );
  68.     if ( c == EOF )
  69.             break;
  70.         if ( ( ++counter & 0xff ) == 0 )
  71.         print_compression();
  72.         putc( (char) c, text_file );
  73.     update_model( c );
  74.     }
  75.     print_compression();
  76.     fputc( '\n', stderr );
  77.     exit( 0 );
  78. }
  79.  
  80. /*
  81.  * This routine reads in the command line options and opens the input
  82.  * and output files.  The only command line options for this piece of
  83.  * code are the input and output file names.
  84.  *
  85.  */
  86. void initialize_options( int argc, char **argv )
  87. {
  88.     char text_file_name[ 81 ];
  89.     char compressed_file_name[ 81 ];
  90.  
  91.     strcpy( compressed_file_name, "test.cmp" );
  92.     strcpy( text_file_name, "test.out" );
  93.     while ( argc-- > 0 )
  94.     {
  95.         if ( strcmp( *argv, "-f" ) == 0 )
  96.     {
  97.         argc--;
  98.         strcpy( text_file_name, *++argv );
  99.     }
  100.         else if ( strcmp( *argv, "-c" ) == 0 )
  101.     {
  102.         argc--;
  103.         strcpy( compressed_file_name, *++argv );
  104.     }
  105.     else
  106.     {
  107.             fprintf( stderr,
  108.               "\nUsage: EXPAND-1 [-f text file] [-c compressed file]\n" );
  109.         exit( -1 );
  110.     }
  111.     argc--;
  112.     argv++;
  113.     }
  114.     text_file = fopen( text_file_name, "wb" );
  115.     compressed_file = fopen( compressed_file_name, "rb" );
  116.  
  117.     if ( text_file==NULL || compressed_file==NULL )
  118.     {
  119.         printf( "Had trouble opening one of the files!\n" );
  120.         exit( -1 );
  121.     }
  122.     setvbuf( text_file, NULL, _IOFBF, 4096 );
  123.     setbuf( stdout, NULL );
  124.     printf( "Expanding %s to %s\n", compressed_file_name, text_file_name );
  125. }
  126.  
  127. /*
  128.  * This is a pacifier routine that is called to print out the
  129.  * compression ratio once every 256 characters.  It prints out the
  130.  * input byte count, the output byte count, and the current ratio
  131.  * of bits per byte.
  132.  */
  133. void print_compression()
  134. {
  135.     long input_bytes;
  136.     long output_bytes;
  137.  
  138.     output_bytes = ftell( text_file );
  139.     input_bytes = bit_ftell_input( compressed_file );
  140.     if ( output_bytes == 0 )
  141.         output_bytes = 1;
  142.     fprintf( stderr,"\r%ld/%ld, %2.3f     ",
  143.          input_bytes,
  144.          output_bytes,
  145.              8.0 * input_bytes / output_bytes );
  146.  
  147. }
  148.  
  149.