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

  1. //
  2. //  MTF.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 encoding function on an
  12. //  input file/stream, and sends the result to an output file
  13. //  or stream.  An MTF encoder encodes each character using the
  14. //  count of distinct previous characters seen since the characters
  15. //  last appearance.  This is implemented by keeping an array of
  16. //  characters.  Each new input character is encoded with its
  17. //  current position in the array.  The character is then moved to
  18. //  position 0 in the array, and all the higher order characters
  19. //  are moved down by one position to make roon.
  20. //
  21. //  Both the encoder and decoder have to start with the order array
  22. //  initialized to the same values.
  23. //
  24. //  This program takes two arguments: an input file and an output
  25. //  file.  You can leave off one argument and send your output to
  26. //  stdout.  Leave off two arguments and read your input from stdin
  27. //  as well.
  28. //
  29. //  This program accompanies my article "Data Compression with the
  30. //  Burrows-Wheeler Transform."
  31. //
  32. // Build Instructions
  33. // ------------------
  34. //
  35. //  Define the constant unix for UNIX or UNIX-like systems.  The
  36. //  use of this constant turns off the code used to force the MS-DOS
  37. //  file system into binary mode.  g++ already does this, your
  38. //  UNIX C++ compiler might also.
  39. //
  40. //  Borland C++ 4.5 16 bit    : bcc -w mtf.cpp
  41. //  Borland C++ 4.5 32 bit    : bcc32 -w mtf.cpp
  42. //  Microsoft Visual C++ 1.52 : cl /W4 mtf.cpp
  43. //  Microsoft Visual C++ 2.1  : cl /W4 mtf.cpp
  44. //  g++                       : g++ -o mtf mtf.cpp
  45. //
  46. // Typical Use
  47. // -----------
  48. //
  49. //  rle < raw-file | bwt | mtf | rle | ari > compressed-file
  50. //
  51. //
  52.  
  53. #define unix
  54.  
  55. #include <stdio.h>
  56. #if !defined( unix )
  57. #include <io.h>
  58. #endif
  59. #include <fcntl.h>
  60.  
  61. main( int argc, char *argv[] )
  62. {
  63.     fprintf( stderr, "Performing MTF encoding on " );
  64.     if ( argc > 1 ) {
  65.         freopen( argv[ 1 ], "rb", stdin );
  66.         fprintf( stderr, "%s", argv[ 1 ] );
  67.     } else
  68.         fprintf( stderr, "stdin" );
  69.     fprintf( stderr, " to " );
  70.     if ( argc > 2 ) {
  71.         freopen( argv[ 2 ], "wb", stdout );
  72.         fprintf( stderr, "%s", argv[ 2 ] );
  73.     } else
  74.         fprintf( stderr, "stdin" );
  75.     fprintf( stderr, "\n" );
  76. #if !defined( unix )
  77.     setmode( fileno( stdin ), O_BINARY );
  78.     setmode( fileno( stdout ), O_BINARY );
  79. #endif
  80.  
  81.     unsigned char order[ 256 ];
  82.     int i;
  83.     for ( i = 0 ; i < 256 ; i++ )
  84.         order[ i ] = (unsigned char) i;
  85.     int c;
  86.     while ( ( c = getc( stdin ) ) >= 0 )  {
  87. //
  88. // Find the char, and output it
  89. //
  90.         for ( i = 0 ; i < 256 ; i++ )
  91.             if ( order[ i ] == ( c & 0xff ) )
  92.                 break;
  93.         putc( (char) i, stdout );
  94. //
  95. // Now shuffle the order array
  96. //
  97.         for ( int j = i ; j > 0 ; j-- )
  98.             order[ j ] = order[ j - 1 ];
  99.         order[ 0 ] = (unsigned char) c;
  100.     }
  101.     return 1;
  102. }
  103.  
  104.