Geant4  10.03.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4RTOutBitStream.cc
Go to the documentation of this file.
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
26 //
27 // $Id: G4RTOutBitStream.cc 66373 2012-12-18 09:41:34Z gcosmo $
28 //
29 //
30 //
31 
32 #include <string.h>
33 #include "G4RTJpeg.hh"
34 #include "G4RTOutBitStream.hh"
35 
36 
37 
39 {
40  if(size < 1)
41  throw( G4MemoryError( size, "G4OutBitStream" ) );
42 
43  mHeadOfBuf = mBuf = new u_char[size];
44  if( mHeadOfBuf == 0 )
45  throw( G4MemoryError( size, "G4OutBitStream" ) );
46 
47  mEndOfBuf = mBuf + size;
48 
49  memset( mHeadOfBuf, 0, size );
50 
51  mBitPos = 7;
52  mWriteFlag = 1;
53 }
54 
56 {
57  delete mBuf;
58 }
59 
60 void
62 {
63  if( ++mBuf >= mEndOfBuf )
64  mWriteFlag = 0;
65 }
66 
67 
68 
69 void
70 G4OutBitStream::SetBits(int v, int numBits)
71 {
72  if( numBits == 0 )
73  return;
74  if( numBits > 16 )
75  throw( G4BufferError( "SetBits:Max Bit Over" ) );
76  if( numBits > 8 ){
77  Set8Bits( u_char(v>>8), numBits-8 );
78  numBits = 8;
79  }
80  Set8Bits( u_char(v), numBits );
81 }
82 
83 void
85 {
86  v &= BitFullMaskT[numBits-1];
87  *mBuf |= v << (mBitPos + 1 - numBits);
88  if( (mBitPos -= numBits) < 0 ){
89  if( *mBuf == 0xff ){
90  IncBuf();
91  *mBuf = 0;
92  }
93  IncBuf();
94  mBitPos = 7;
95  }
96 }
97 
98 void
100 {
101  v &= BitFullMaskT[numBits-1];
102  int nextBits = numBits - (mBitPos + 1);
103  *mBuf |= ( v >> nextBits ) & BitFullMaskT[mBitPos];
104  if( *mBuf == 0xff ){
105  IncBuf();
106  *mBuf = 0;
107  }
108  IncBuf();
109 
110  *mBuf = v << (8 - nextBits);
111  mBitPos = 7 - nextBits;
112 }
113 
114 void
116 {
117  if( mBitPos + 1 >= numBits )
118  SetFewBits( (u_char)v, numBits );
119  else
120  SetBits2Byte( (u_char)v, numBits );
121 }
122 
123 
124 void
126 {
127  if( mBitPos != 7 )
128  SetFewBits( BitFullMaskT[mBitPos], mBitPos+1 );
129 }
130 
131 void
133 {
134  if( mWriteFlag ){
135  FullBit();
136  *mBuf = dat;
137  IncBuf();
138  return;
139  }
140  throw( G4BufferError( "SetByte" ) );
141 }
142 
143 void
145 {
146  if( mWriteFlag ){
147  FullBit();
148  *mBuf = (dat >> 8) & 0xff;
149  IncBuf();
150  *mBuf = dat & 0xff;
151  IncBuf();
152  return;
153  }
154  throw( G4BufferError( "SetWord" ) );
155 }
156 
157 void
158 G4OutBitStream::CopyByte(const char* src, int n)
159 {
160  if( mBuf+n < mEndOfBuf ){
161  FullBit();
162  memcpy( mBuf, src, n );
163  mBuf += n;
164  return;
165  }
166  throw( G4BufferError( "CopyByte" ) );
167 }
168 
unsigned int u_int
Definition: G4RTJpeg.hh:41
void SetFewBits(u_char v, int numBits)
G4OutBitStream(int size)
static const u_char BitFullMaskT[8]
void SetWord(u_int dat)
void SetByte(u_char dat)
const G4int n
tuple v
Definition: test.py:18
unsigned char u_char
Definition: G4RTJpeg.hh:40
void CopyByte(const char *src, int n)
void SetBits2Byte(u_char v, int numBits)
void SetBits(int v, int numBits)
void Set8Bits(u_char v, int numBits)