home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / packery / bwt / src / unmtf.cpp < prev    next >
C/C++ Source or Header  |  1996-06-17  |  3KB  |  103 lines

  1. //
  2. //  UNMTF.CPP
  3. //
  4. //  Mark Nelson
  5. //  March 8, 1996
  6. //  http://web2.airmail.net/markn
  7. //
  8. // DESCRIPTION
  9. // -----------
  10. //
  11. //  This program performs a Move To Front decoding function on an
  12. //  input file/stream, and sends the result to an output file
  13. //  or stream.  The MTF decoder keeps an array of 256 characters
  14. //  int the order that they have appeared.  Each time the encoder
  15. //  sends a number, the decoder uses it to look up a character in
  16. //  the corresponding position of the array, and outputs it.  That
  17. //  character is then moved up to position 0 in the array, and all
  18. //  the in-between characters are moved down a spot.
  19. //
  20. //  Both the encoder and decoder have to start with the order array
  21. //  initialized to the same values.
  22. //
  23. //  This program takes two arguments: an input file and an output
  24. //  file.  You can leave off one argument and send your output to
  25. //  stdout.  Leave off two arguments and read your input from stdin
  26. //  as well.
  27. //
  28. //  This program accompanies my article "Data Compression with the
  29. //  Burrows-Wheeler Transform."
  30. //
  31. // Build Instructions
  32. // ------------------
  33. //
  34. //  Define the constant unix for UNIX or UNIX-like systems.  The
  35. //  use of this constant turns off the code used to force the MS-DOS
  36. //  file system into binary mode.  g++ already does this, your UNIX
  37. //  C++ compiler might also.
  38. //
  39. //  Borland C++ 4.5 16 bit    : bcc -w mtf.cpp
  40. //  Borland C++ 4.5 32 bit    : bcc32 -w mtf.cpp
  41. //  Microsoft Visual C++ 1.52 : cl /W4 mtf.cpp
  42. //  Microsoft Visual C++ 2.1  : cl /W4 mtf.cpp
  43. //  g++                       : g++ -o unmtf unmtf.cpp
  44. //
  45. // Typical Use
  46. // -----------
  47. //
  48. //  unari < compressed-file | unrle | unmtf | unbwt | unrle > raw-file
  49. //
  50. //
  51.  
  52. #defien unix
  53.  
  54. #include <stdio.h>
  55. #if !defined( unix )
  56. #include <io.h>
  57. #endif
  58. #include <fcntl.h>
  59.  
  60. int main( int argc, char *argv[] )
  61. {
  62.     int c;
  63.  
  64.     fprintf( stderr, "Performing MTF decoding on " );
  65.     if ( argc > 1 ) {
  66.         freopen( argv[ 1 ], "rb", stdin );
  67.         fprintf( stderr, "%s",  argv[ 1 ] );
  68.     } else
  69.         fprintf( stderr, "stdin" );
  70.     fprintf( stderr, " to " );
  71.     if ( argc > 2 ) {
  72.         freopen( argv[ 2 ], "wb", stdout );
  73.         fprintf( stderr, "%s",  argv[ 2 ] );
  74.     } else
  75.         fprintf( stderr, "stdout" );
  76.     fprintf( stderr, "\n" );
  77. #if !defined( unix )
  78.     setmode( fileno( stdin ), O_BINARY );
  79.     setmode( fileno( stdout ), O_BINARY );
  80. #endif
  81.  
  82.     unsigned char order[ 256 ];
  83.     int i;
  84.     for ( i = 0 ; i < 256 ; i++ )
  85.         order[ i ] = (unsigned char) i;
  86.     while ( ( i = getc( stdin ) ) >= 0 )  {
  87. //
  88. // Find the char
  89. //
  90.         putc( order[ i ], stdout );
  91.         c = order[ i ];
  92. //
  93. // Now shuffle the order array
  94. //
  95.         int j;
  96.         for ( j = i ; j > 0 ; j-- )
  97.             order[ j ] = order[ j - 1 ];
  98.         order[ 0 ] = (unsigned char) c;
  99.     }
  100.     return 1;
  101. }
  102.  
  103.