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

  1. //
  2. //  UNRLE.CPP
  3. //
  4. //  Mark Nelson
  5. //  March 8, 1996
  6. //  http://web2.airmail.net/markn
  7. //
  8. // DESCRIPTION
  9. // -----------
  10. //
  11. //  This program reverses the Run Length Encoding function on an
  12. //  input file/stream, and sends the result to an output file
  13. //  or stream.  In the input stream, any two consecutive
  14. //  characters with the same value flag a run.  A byte following
  15. //  those two characters gives the count of *additional*
  16. //  repeat characters, which can be anything from 0 to 255.
  17. //  This isn't an optimal RLE scheme, but it you have to like
  18. //  its simplicity.
  19. //
  20. //  Using the RLE program as a front end to BWT avoids
  21. //  pathologically slow sorts that occur when the input stream
  22. //  has long sequences of identical characters.  The RLE has
  23. //  also been used as a heuristic postprocessor from the MTF
  24. //  program, before sending the stream to the entropy encoder.
  25. //
  26. //  This program takes two arguments: an input file and an output
  27. //  file.  You can leave off one argument and send your output to
  28. //  stdout.  Leave off two arguments and read your input from stdin
  29. //  as well.
  30. //
  31. //  This program accompanies my article "Data Compression with the
  32. //  Burrows-Wheeler Transform."
  33. //
  34. // Build Instructions
  35. // ------------------
  36. //
  37. //  Define the constant unix for UNIX or UNIX-like systems.  The
  38. //  use of this constant turns off the code used to force the MS-DOS
  39. //  file system into binary mode.  g++ already does this, your
  40. //  UNIX C++ compiler might do this also.
  41. //
  42. //  Borland C++ 4.5 16 bit    : bcc -w unrle.cpp
  43. //  Borland C++ 4.5 32 bit    : bcc32 -w unrle.cpp
  44. //  Microsoft Visual C++ 1.52 : cl /W4 unrle.cpp
  45. //  Microsoft Visual C++ 2.1  : cl /W4 unrle.cpp
  46. //  g++                       : g++ -o unrle.cpp unrle.cpp
  47. //
  48. // Typical Use
  49. // -----------
  50. //
  51. //  unari < compressed-file | unrle | unmtf | unbwt | unrle > raw-file
  52. //
  53. //
  54.  
  55. #define unix
  56.  
  57. #include <stdio.h>
  58. #include <fcntl.h>
  59. #if !defined( unix )
  60. #include <io.h>
  61. #endif
  62.  
  63. main( int argc, char *argv[] )
  64. {
  65.     fprintf( stderr, "Run length decoding " );
  66.     if ( argc > 1 ) {
  67.         freopen( argv[ 1 ], "rb", stdin );
  68.         fprintf( stderr, "%s", argv[ 1 ] );
  69.     } else
  70.         fprintf( stderr, "stdin" );
  71.     fprintf( stderr, " to " );
  72.     if ( argc > 2 ) {
  73.         freopen( argv[ 2 ], "wb", stdout );
  74.         fprintf( stderr, "%s", argv[ 2 ] );
  75.     } else
  76.         fprintf( stderr, "stdout" );
  77.     fprintf( stderr, "\n" );
  78. #if !defined( unix )
  79.     setmode( fileno( stdin ), O_BINARY );
  80.     setmode( fileno( stdout ), O_BINARY );
  81. #endif
  82.  
  83.     int last = 0;
  84.     int c;
  85.     int count;
  86.     while ( ( c = getc( stdin ) ) >= 0 )  {
  87.         putc( (char) c, stdout );
  88.         if ( c == last ) {
  89.             count = getc( stdin );
  90.             while ( count-- > 0 )
  91.                 putc( (char) c, stdout );
  92.         }
  93.         last = c;
  94.     }
  95.     return 1;
  96. }
  97.  
  98.