G4RTOutBitStream.cc

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 //
00027 // $Id$
00028 //
00029 //
00030 //
00031 
00032 #include <string.h>
00033 #include "G4RTJpeg.hh"
00034 #include "G4RTOutBitStream.hh"
00035 
00036 
00037 
00038 G4OutBitStream::G4OutBitStream(int size)
00039 {
00040   if(size < 1)
00041         throw( G4MemoryError( size, "G4OutBitStream" ) );
00042 
00043   mHeadOfBuf = mBuf = new u_char[size];
00044   if( mHeadOfBuf == 0 )
00045         throw( G4MemoryError( size, "G4OutBitStream" ) );
00046 
00047   mEndOfBuf = mBuf + size;
00048 
00049   memset( mHeadOfBuf, 0, size );
00050 
00051   mBitPos = 7;
00052   mWriteFlag = 1;
00053 }
00054 
00055 G4OutBitStream::~G4OutBitStream()
00056 {
00057   delete mBuf;
00058 }
00059 
00060 void
00061 G4OutBitStream::IncBuf( void )
00062 {
00063   if( ++mBuf >= mEndOfBuf )
00064         mWriteFlag = 0;
00065 }
00066 
00067 
00068 
00069 void
00070 G4OutBitStream::SetBits(int v, int numBits)
00071 {
00072   if( numBits == 0 )
00073         return;
00074   if( numBits > 16 )
00075         throw( G4BufferError( "SetBits:Max Bit Over" ) );
00076   if( numBits > 8 ){
00077         Set8Bits( u_char(v>>8), numBits-8 );
00078         numBits = 8;
00079   }
00080   Set8Bits( u_char(v), numBits );
00081 }
00082 
00083 void
00084 G4OutBitStream::SetFewBits(u_char v, int numBits)
00085 {
00086   v &= BitFullMaskT[numBits-1];
00087   *mBuf |= v << (mBitPos + 1 - numBits);
00088   if( (mBitPos -= numBits) < 0 ){
00089         if( *mBuf == 0xff ){
00090           IncBuf();
00091           *mBuf = 0;
00092         }
00093         IncBuf();
00094         mBitPos = 7;
00095     }
00096 }
00097 
00098 void
00099 G4OutBitStream::SetBits2Byte(u_char v, int numBits)
00100 {
00101   v &= BitFullMaskT[numBits-1];
00102   int nextBits = numBits - (mBitPos + 1);
00103   *mBuf |= ( v >> nextBits ) & BitFullMaskT[mBitPos];
00104   if( *mBuf == 0xff ){
00105         IncBuf();
00106         *mBuf = 0;
00107   }
00108   IncBuf();
00109 
00110   *mBuf = v << (8 - nextBits);
00111   mBitPos = 7 - nextBits;
00112 }
00113 
00114 void
00115 G4OutBitStream::Set8Bits(u_char v, int numBits)
00116 {
00117   if( mBitPos + 1 >= numBits )
00118         SetFewBits( (u_char)v, numBits );
00119  else
00120         SetBits2Byte( (u_char)v, numBits );
00121 }
00122 
00123 
00124 void
00125 G4OutBitStream::FullBit( void )
00126 {
00127   if( mBitPos != 7 )
00128         SetFewBits( BitFullMaskT[mBitPos], mBitPos+1 );
00129 }
00130 
00131 void
00132 G4OutBitStream::SetByte(u_char dat)
00133 {
00134   if( mWriteFlag ){
00135         FullBit();
00136         *mBuf = dat;
00137         IncBuf();
00138         return;
00139   }
00140   throw( G4BufferError( "SetByte" ) );
00141 }
00142 
00143 void
00144 G4OutBitStream::SetWord(u_int dat)
00145 {
00146   if( mWriteFlag ){
00147         FullBit();
00148         *mBuf = (dat >> 8) & 0xff;
00149         IncBuf();
00150         *mBuf = dat & 0xff;
00151         IncBuf();
00152         return;
00153   }
00154   throw( G4BufferError( "SetWord" ) );
00155 }
00156 
00157 void
00158 G4OutBitStream::CopyByte(const char* src, int n)
00159 {
00160   if( mBuf+n < mEndOfBuf ){
00161         FullBit();
00162         memcpy( mBuf, src, n );
00163         mBuf += n;
00164         return;
00165   }
00166   throw( G4BufferError( "CopyByte" ) );
00167 }
00168 

Generated on Mon May 27 17:49:47 2013 for Geant4 by  doxygen 1.4.7