LZW Compressor
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Pages
CodeWriter Class Reference

Variable binary width code writer. More...

Public Member Functions

 CodeWriter (std::ostream &os)
 Default constructor. More...
 
 ~CodeWriter ()
 Destructor. More...
 
std::size_t get_bits () const
 Getter for CodeWriter::bits.
 
void reset_bits ()
 Resets internal binary width. More...
 
void increase_bits ()
 Increases internal binary width by one. More...
 
bool write (CodeType k)
 Writes the code k with a binary width of CodeWriter::bits. More...
 

Private Attributes

std::ostream & os
 Output Stream.
 
std::size_t bits
 Binary width of codes.
 
ByteCache lo
 LeftOvers.
 

Detailed Description

Variable binary width code writer.

Definition at line 219 of file lzw_v6.cpp.

Constructor & Destructor Documentation

CodeWriter::CodeWriter ( std::ostream &  os)
inlineexplicit

Default constructor.

Parameters
[out]osOutput Stream to write codes to

Definition at line 226 of file lzw_v6.cpp.

226  : os(os), bits(CHAR_BIT + 1)
227  {
228  }
CodeWriter::~CodeWriter ( )
inline

Destructor.

Note
Writes MetaCode::Eof and flushes the last byte to the stream.

Definition at line 234 of file lzw_v6.cpp.

References ByteCache::data, lo, os, ByteCache::used, and write().

235  {
236  write(static_cast<CodeType> (MetaCode::Eof));
237 
238  // write the incomplete leftover byte as-is
239  if (lo.used != 0)
240  os.put(static_cast<char> (lo.data));
241  }

Member Function Documentation

void CodeWriter::increase_bits ( )
inline

Increases internal binary width by one.

Exceptions
std::overflow_errorinternal binary width cannot be increased
Remarks
The exception should never be thrown, under normal circumstances.

Definition at line 265 of file lzw_v6.cpp.

References bits.

Referenced by compress().

266  {
267 #ifdef TAKE_NO_RISKS
268  if (bits == SIZE_MAX)
269  throw std::overflow_error("CodeWriter::increase_bits()");
270 #endif
271  ++bits;
272  }
void CodeWriter::reset_bits ( )
inline

Resets internal binary width.

Note
Default value is CHAR_BIT + 1.

Definition at line 255 of file lzw_v6.cpp.

References bits.

Referenced by compress().

256  {
257  bits = CHAR_BIT + 1;
258  }
bool CodeWriter::write ( CodeType  k)
inline

Writes the code k with a binary width of CodeWriter::bits.

Parameters
kcode to be written
Returns
Whether or not the stream can be used for output.
Return values
truethe output stream can still be used
falsethe output stream can no longer be used

Definition at line 281 of file lzw_v6.cpp.

References bits, ByteCache::data, lo, os, and ByteCache::used.

Referenced by compress(), and ~CodeWriter().

282  {
283  std::size_t remaining_bits {bits};
284 
285  if (lo.used != 0)
286  {
287  lo.data |= k << lo.used;
288  os.put(static_cast<char> (lo.data));
289  k >>= CHAR_BIT - lo.used;
290  remaining_bits -= CHAR_BIT - lo.used;
291  lo.used = 0;
292  lo.data = 0x00;
293  }
294 
295  while (remaining_bits != 0)
296  if (remaining_bits >= CHAR_BIT)
297  {
298  os.put(static_cast<char> (k));
299  k >>= CHAR_BIT;
300  remaining_bits -= CHAR_BIT;
301  }
302  else
303  {
304  lo.used = remaining_bits;
305  lo.data = k;
306  break;
307  }
308 
309  return os;
310  }

The documentation for this class was generated from the following file: