OSG::BitPacker Class Reference

BitPacker class. packs and unpacks UInt32. More...

#include <OSGNFIOBitPacker.h>

List of all members.


Public Types

Types


typedef std::vector< UInt8 > BufferType

Public Member Functions

Constructor


 BitPacker (UInt32 size, UInt32 max)
Pack/get


void pack (UInt32 value)
const BufferTypegetBuffer (void) const
BufferTypegetBuffer (void)

Private Attributes

UInt32 _numBitsToPack
Int32 _nextBit
BufferType _buffer

Static Private Attributes

static const Int32 BITS_PER_WORD = 32

Detailed Description

packs and unpacks UInt32.

Definition at line 55 of file OSGNFIOBitPacker.h.


Member Typedef Documentation

typedef std::vector<UInt8> OSG::BitPacker::BufferType

Definition at line 63 of file OSGNFIOBitPacker.h.


Constructor & Destructor Documentation

BitPacker::BitPacker ( UInt32  size,
UInt32  max 
)

Definition at line 70 of file OSGNFIOBitPacker.cpp.

References _buffer, and _numBitsToPack.

00071     : _numBitsToPack(1),
00072       _nextBit      (0)
00073 {
00074     while(true)
00075     {
00076         UInt32 maxValue = (UInt32) (1 << _numBitsToPack) - 1;
00077         if (maxValue >= max)
00078             break;
00079 
00080         ++_numBitsToPack;
00081     }
00082 
00083     // calc buffer size.
00084     UInt32 lenInBytes = (_numBitsToPack * size + 7) / 8;
00085 
00086     _buffer.resize(lenInBytes);
00087     memset(&_buffer[0], 0, lenInBytes * sizeof(UInt8)); // unneccessary ?
00088 }


Member Function Documentation

void BitPacker::pack ( UInt32  value  ) 

Definition at line 90 of file OSGNFIOBitPacker.cpp.

References _buffer, _nextBit, _numBitsToPack, and BITS_PER_WORD.

Referenced by OSG::OSBTypedGeoIntegralPropertyElement< GeoPropertyTypeT >::write(), and OSG::NFIOGeometry::writePackedIndices().

00091 {
00092     UInt32 numBitsToPack = _numBitsToPack;
00093 
00094     // Scoot the value bits up to the top of the word; this makes
00095     // them easier to work with.
00096     value <<= (BITS_PER_WORD - numBitsToPack);
00097 
00098     // First we do the hard part: pack bits into the first u8,
00099     // which may already have bits in it.
00100 
00101     Int32 byteIndex     = (_nextBit / 8);
00102     Int32 bitIndex      = (_nextBit % 8);
00103     Int32 spaceCurrByte = (8 - bitIndex) & 0x7;
00104 
00105     // Update _nextBit for the next call; we don't need
00106     // the old value any more
00107     _nextBit += numBitsToPack;
00108 
00109     UInt8 *dest = &_buffer[0] + byteIndex;
00110 
00111     if(spaceCurrByte)
00112     {
00113         Int32 toCopy = spaceCurrByte;
00114 
00115         if(toCopy > numBitsToPack)
00116         {
00117             // We don't have enough bits to fill up this u8.
00118             toCopy = numBitsToPack;
00119         }
00120 
00121         UInt32 fillBits = value >> (BITS_PER_WORD - spaceCurrByte);
00122         *dest |= fillBits;
00123 
00124         numBitsToPack -= toCopy;
00125         dest++;
00126         value <<= toCopy;
00127     }
00128 
00129     // Now we do the fast and easy part for what is hopefully
00130     // the bulk of the data.
00131     while(value)
00132     {
00133         *dest++ = value >> (BITS_PER_WORD - 8);
00134         value <<= 8;
00135     }
00136 }

const BitPacker::BufferType & BitPacker::getBuffer ( void   )  const

Definition at line 139 of file OSGNFIOBitPacker.cpp.

References _buffer.

Referenced by OSG::OSBTypedGeoIntegralPropertyElement< GeoPropertyTypeT >::write(), and OSG::NFIOGeometry::writePackedIndices().

00140 {
00141     return _buffer;
00142 }

BitPacker::BufferType & BitPacker::getBuffer ( void   ) 

Definition at line 145 of file OSGNFIOBitPacker.cpp.

References _buffer.

00146 {
00147     return _buffer;
00148 }


Member Data Documentation

Definition at line 87 of file OSGNFIOBitPacker.h.

Referenced by BitPacker(), and pack().

Int32 OSG::BitPacker::_nextBit [private]

Definition at line 88 of file OSGNFIOBitPacker.h.

Referenced by pack().

Definition at line 89 of file OSGNFIOBitPacker.h.

Referenced by BitPacker(), getBuffer(), and pack().

const Int32 BitPacker::BITS_PER_WORD = 32 [static, private]

Definition at line 91 of file OSGNFIOBitPacker.h.

Referenced by pack().


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