Merge pull request #2316 from jwillemsen/jwi-taskcommenttypo
[ACE_TAO.git] / TAO / tao / Compression / rle / RLECompressor.cpp
blobb15368c778eb822ccfc5ca266cdd3dba138bc266
1 /*
2 * Run-length encoding (RLE) is a very simple form of data compression
3 * in which runs of data (that is, sequences in which the same data value
4 * occurs in many consecutive data elements) are stored as a single data
5 * value and count, rather than as the original run. This is most useful
6 * on data that contains many such runs: for example, simple graphic
7 * images such as icons, line drawings, and animations. It is not useful
8 * with files that don't have many runs as it could slightly increase the
9 * output size.
10 * ALGORITHM: This algorithm is an optimized version of the traditional
11 * RLE algorithm in that it behaves better with very few runs.
13 * With a run of a character where that run is >= 3 this is
14 * replaced with the repeat indicator 0X80 and then the repeat count OR'd
15 * over this ident. This repeat count therefore has a maximum value
16 * of 127 (0x7F) which is to be interpreted as the next character repeated
17 * another 'repeat count' times (i.e. a maximum of 128 characters can be
18 * represented in any single dupal). if the repeat ident is not present
19 * then the count is to be interpreted as a copy of the next repeat count
20 * characters + 1.
22 * EXAMPLE: the following arbitary string of 67 bytes:-
23 * WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBWWWWWWWWWWWWWW
24 * will produce (as a HEXDUMP) of 14 bytes
25 * 8B 57 00 42 8B 57 82 42 97 57 00 42 8D 57 .W.B.W.B.W.B.W
28 #include "RLECompressor.h"
29 #include "ace/Compression/rle/RLECompressor.h"
31 TAO_BEGIN_VERSIONED_NAMESPACE_DECL
33 namespace TAO
35 RLECompressor::RLECompressor(::Compression::CompressorFactory_ptr compressor_factory)
36 : BaseCompressor (compressor_factory, 0)
40 void
41 RLECompressor::compress(const ::Compression::Buffer &source, ::Compression::Buffer &target)
43 // Ensure maximum is at least X 1.1 input length.
44 target.length(static_cast<CORBA::ULong>((source.length() * 1.1) + 32U));
46 ACE_UINT64 out_len = ACE_RLECompression::instance()->compress( source.get_buffer(),
47 source.length(),
48 target.get_buffer(),
49 target.maximum() );
50 if (ACE_UINT64(-1) == out_len) { // Overrun
51 throw ::Compression::CompressionException();
54 target.length(static_cast< CORBA::ULong>(out_len)); // Set Output Buffer to the right size now.
56 // Update statistics for this compressor
57 this->update_stats(source.length(), target.length());
60 void
61 RLECompressor::decompress(const ::Compression::Buffer &source, ::Compression::Buffer &target)
63 ACE_UINT64 out_len = ACE_RLECompression::instance()->decompress(source.get_buffer(),
64 source.length(),
65 target.get_buffer(),
66 target.maximum() );
67 if (ACE_UINT64(-1) == out_len) { // Overrun
68 throw ::Compression::CompressionException();
71 target.length(static_cast<CORBA::ULong>(out_len));
76 TAO_END_VERSIONED_NAMESPACE_DECL