Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer << 3 // * DISCLAIMER * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th << 5 // * The following disclaimer summarizes all the specific disclaimers * 6 // * the Geant4 Collaboration. It is provided << 6 // * of contributors to this software. The specific disclaimers,which * 7 // * conditions of the Geant4 Software License << 7 // * govern, are listed with their locations in: * 8 // * LICENSE and available at http://cern.ch/ << 8 // * http://cern.ch/geant4/license * 9 // * include a list of copyright holders. << 10 // * 9 // * * 11 // * Neither the authors of this software syst 10 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 11 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 12 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 13 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file << 14 // * use. * 16 // * for the full disclaimer and the limitatio << 17 // * 15 // * * 18 // * This code implementation is the result << 16 // * This code implementation is the intellectual property of the * 19 // * technical work of the GEANT4 collaboratio << 17 // * GEANT4 collaboration. * 20 // * By using, copying, modifying or distri << 18 // * By copying, distributing or modifying the Program (or any work * 21 // * any work based on the software) you ag << 19 // * based on the Program) you indicate your acceptance of this * 22 // * use in resulting scientific publicati << 20 // * statement, and all its terms. * 23 // * acceptance of all terms of the Geant4 Sof << 24 // ******************************************* 21 // ******************************************************************** 25 // 22 // 26 // 23 // >> 24 // $Id: G4RTOutBitStream.cc,v 1.4 2001/07/11 10:09:04 gunter Exp $ >> 25 // GEANT4 tag $Name: geant4-05-01-patch-01 $ 27 // 26 // 28 // 27 // 29 // 28 // 30 29 31 #include <string.h> 30 #include <string.h> 32 #include "G4RTJpeg.hh" 31 #include "G4RTJpeg.hh" 33 #include "G4RTOutBitStream.hh" 32 #include "G4RTOutBitStream.hh" 34 33 35 34 36 35 37 G4OutBitStream::G4OutBitStream(int size) 36 G4OutBitStream::G4OutBitStream(int size) 38 { 37 { 39 if(size < 1) 38 if(size < 1) 40 throw( G4MemoryError( size, "G4OutBitS 39 throw( G4MemoryError( size, "G4OutBitStream" ) ); 41 40 42 mHeadOfBuf = mBuf = new u_char[size]; 41 mHeadOfBuf = mBuf = new u_char[size]; 43 if( mHeadOfBuf == 0 ) 42 if( mHeadOfBuf == 0 ) 44 throw( G4MemoryError( size, "G4OutBitS 43 throw( G4MemoryError( size, "G4OutBitStream" ) ); 45 44 46 mEndOfBuf = mBuf + size; 45 mEndOfBuf = mBuf + size; 47 46 48 memset( mHeadOfBuf, 0, size ); 47 memset( mHeadOfBuf, 0, size ); 49 48 50 mBitPos = 7; 49 mBitPos = 7; 51 mWriteFlag = 1; 50 mWriteFlag = 1; 52 } 51 } 53 52 54 G4OutBitStream::~G4OutBitStream() << 55 { << 56 delete mBuf; << 57 } << 58 << 59 void 53 void 60 G4OutBitStream::IncBuf( void ) 54 G4OutBitStream::IncBuf( void ) 61 { 55 { 62 if( ++mBuf >= mEndOfBuf ) 56 if( ++mBuf >= mEndOfBuf ) 63 mWriteFlag = 0; 57 mWriteFlag = 0; 64 } 58 } 65 59 66 60 67 61 68 void 62 void 69 G4OutBitStream::SetBits(int v, int numBits) 63 G4OutBitStream::SetBits(int v, int numBits) 70 { 64 { 71 if( numBits == 0 ) 65 if( numBits == 0 ) 72 return; 66 return; 73 if( numBits > 16 ) 67 if( numBits > 16 ) 74 throw( G4BufferError( "SetBits:Max Bit 68 throw( G4BufferError( "SetBits:Max Bit Over" ) ); 75 if( numBits > 8 ){ 69 if( numBits > 8 ){ 76 Set8Bits( u_char(v>>8), numBits-8 ); 70 Set8Bits( u_char(v>>8), numBits-8 ); 77 numBits = 8; 71 numBits = 8; 78 } 72 } 79 Set8Bits( u_char(v), numBits ); 73 Set8Bits( u_char(v), numBits ); 80 } 74 } 81 75 82 void 76 void 83 G4OutBitStream::SetFewBits(u_char v, int numBi 77 G4OutBitStream::SetFewBits(u_char v, int numBits) 84 { 78 { 85 v &= BitFullMaskT[numBits-1]; 79 v &= BitFullMaskT[numBits-1]; 86 *mBuf |= v << (mBitPos + 1 - numBits); 80 *mBuf |= v << (mBitPos + 1 - numBits); 87 if( (mBitPos -= numBits) < 0 ){ 81 if( (mBitPos -= numBits) < 0 ){ 88 if( *mBuf == 0xff ){ 82 if( *mBuf == 0xff ){ 89 IncBuf(); 83 IncBuf(); 90 *mBuf = 0; 84 *mBuf = 0; 91 } 85 } 92 IncBuf(); 86 IncBuf(); 93 mBitPos = 7; 87 mBitPos = 7; 94 } 88 } 95 } 89 } 96 90 97 void 91 void 98 G4OutBitStream::SetBits2Byte(u_char v, int num 92 G4OutBitStream::SetBits2Byte(u_char v, int numBits) 99 { 93 { 100 v &= BitFullMaskT[numBits-1]; 94 v &= BitFullMaskT[numBits-1]; 101 int nextBits = numBits - (mBitPos + 1); 95 int nextBits = numBits - (mBitPos + 1); 102 *mBuf |= ( v >> nextBits ) & BitFullMaskT[mB 96 *mBuf |= ( v >> nextBits ) & BitFullMaskT[mBitPos]; 103 if( *mBuf == 0xff ){ 97 if( *mBuf == 0xff ){ 104 IncBuf(); 98 IncBuf(); 105 *mBuf = 0; 99 *mBuf = 0; 106 } 100 } 107 IncBuf(); 101 IncBuf(); 108 102 109 *mBuf = v << (8 - nextBits); << 103 *mBuf = v << 8 - nextBits; 110 mBitPos = 7 - nextBits; 104 mBitPos = 7 - nextBits; 111 } 105 } 112 106 113 void 107 void 114 G4OutBitStream::Set8Bits(u_char v, int numBits 108 G4OutBitStream::Set8Bits(u_char v, int numBits) 115 { 109 { 116 if( mBitPos + 1 >= numBits ) 110 if( mBitPos + 1 >= numBits ) 117 SetFewBits( (u_char)v, numBits ); 111 SetFewBits( (u_char)v, numBits ); 118 else 112 else 119 SetBits2Byte( (u_char)v, numBits ); 113 SetBits2Byte( (u_char)v, numBits ); 120 } 114 } 121 115 122 116 123 void 117 void 124 G4OutBitStream::FullBit( void ) 118 G4OutBitStream::FullBit( void ) 125 { 119 { 126 if( mBitPos != 7 ) 120 if( mBitPos != 7 ) 127 SetFewBits( BitFullMaskT[mBitPos], mBi 121 SetFewBits( BitFullMaskT[mBitPos], mBitPos+1 ); 128 } 122 } 129 123 130 void 124 void 131 G4OutBitStream::SetByte(u_char dat) 125 G4OutBitStream::SetByte(u_char dat) 132 { 126 { 133 if( mWriteFlag ){ 127 if( mWriteFlag ){ 134 FullBit(); 128 FullBit(); 135 *mBuf = dat; 129 *mBuf = dat; 136 IncBuf(); 130 IncBuf(); 137 return; 131 return; 138 } 132 } 139 throw( G4BufferError( "SetByte" ) ); 133 throw( G4BufferError( "SetByte" ) ); 140 } 134 } 141 135 142 void 136 void 143 G4OutBitStream::SetWord(u_int dat) 137 G4OutBitStream::SetWord(u_int dat) 144 { 138 { 145 if( mWriteFlag ){ 139 if( mWriteFlag ){ 146 FullBit(); 140 FullBit(); 147 *mBuf = (dat >> 8) & 0xff; 141 *mBuf = (dat >> 8) & 0xff; 148 IncBuf(); 142 IncBuf(); 149 *mBuf = dat & 0xff; 143 *mBuf = dat & 0xff; 150 IncBuf(); 144 IncBuf(); 151 return; 145 return; 152 } 146 } 153 throw( G4BufferError( "SetWord" ) ); 147 throw( G4BufferError( "SetWord" ) ); 154 } 148 } 155 149 156 void 150 void 157 G4OutBitStream::CopyByte(const char* src, int 151 G4OutBitStream::CopyByte(const char* src, int n) 158 { 152 { 159 if( mBuf+n < mEndOfBuf ){ 153 if( mBuf+n < mEndOfBuf ){ 160 FullBit(); 154 FullBit(); 161 memcpy( mBuf, src, n ); 155 memcpy( mBuf, src, n ); 162 mBuf += n; 156 mBuf += n; 163 return; 157 return; 164 } 158 } 165 throw( G4BufferError( "CopyByte" ) ); 159 throw( G4BufferError( "CopyByte" ) ); 166 } 160 } 167 161 168 162