[834] | 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 | // |
---|
[849] | 27 | // $Id: G4RTOutBitStream.cc,v 1.6 2008/04/04 13:52:48 allison Exp $ |
---|
[1347] | 28 | // GEANT4 tag $Name: geant4-09-04-ref-00 $ |
---|
[834] | 29 | // |
---|
| 30 | // |
---|
| 31 | // |
---|
| 32 | |
---|
| 33 | #include <string.h> |
---|
| 34 | #include "G4RTJpeg.hh" |
---|
| 35 | #include "G4RTOutBitStream.hh" |
---|
| 36 | |
---|
| 37 | |
---|
| 38 | |
---|
| 39 | G4OutBitStream::G4OutBitStream(int size) |
---|
| 40 | { |
---|
| 41 | if(size < 1) |
---|
| 42 | throw( G4MemoryError( size, "G4OutBitStream" ) ); |
---|
| 43 | |
---|
| 44 | mHeadOfBuf = mBuf = new u_char[size]; |
---|
| 45 | if( mHeadOfBuf == 0 ) |
---|
| 46 | throw( G4MemoryError( size, "G4OutBitStream" ) ); |
---|
| 47 | |
---|
| 48 | mEndOfBuf = mBuf + size; |
---|
| 49 | |
---|
| 50 | memset( mHeadOfBuf, 0, size ); |
---|
| 51 | |
---|
| 52 | mBitPos = 7; |
---|
| 53 | mWriteFlag = 1; |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | void |
---|
| 57 | G4OutBitStream::IncBuf( void ) |
---|
| 58 | { |
---|
| 59 | if( ++mBuf >= mEndOfBuf ) |
---|
| 60 | mWriteFlag = 0; |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | |
---|
| 64 | |
---|
| 65 | void |
---|
| 66 | G4OutBitStream::SetBits(int v, int numBits) |
---|
| 67 | { |
---|
| 68 | if( numBits == 0 ) |
---|
| 69 | return; |
---|
| 70 | if( numBits > 16 ) |
---|
| 71 | throw( G4BufferError( "SetBits:Max Bit Over" ) ); |
---|
| 72 | if( numBits > 8 ){ |
---|
| 73 | Set8Bits( u_char(v>>8), numBits-8 ); |
---|
| 74 | numBits = 8; |
---|
| 75 | } |
---|
| 76 | Set8Bits( u_char(v), numBits ); |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | void |
---|
| 80 | G4OutBitStream::SetFewBits(u_char v, int numBits) |
---|
| 81 | { |
---|
| 82 | v &= BitFullMaskT[numBits-1]; |
---|
| 83 | *mBuf |= v << (mBitPos + 1 - numBits); |
---|
| 84 | if( (mBitPos -= numBits) < 0 ){ |
---|
| 85 | if( *mBuf == 0xff ){ |
---|
| 86 | IncBuf(); |
---|
| 87 | *mBuf = 0; |
---|
| 88 | } |
---|
| 89 | IncBuf(); |
---|
| 90 | mBitPos = 7; |
---|
| 91 | } |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | void |
---|
| 95 | G4OutBitStream::SetBits2Byte(u_char v, int numBits) |
---|
| 96 | { |
---|
| 97 | v &= BitFullMaskT[numBits-1]; |
---|
| 98 | int nextBits = numBits - (mBitPos + 1); |
---|
| 99 | *mBuf |= ( v >> nextBits ) & BitFullMaskT[mBitPos]; |
---|
| 100 | if( *mBuf == 0xff ){ |
---|
| 101 | IncBuf(); |
---|
| 102 | *mBuf = 0; |
---|
| 103 | } |
---|
| 104 | IncBuf(); |
---|
| 105 | |
---|
[849] | 106 | *mBuf = v << (8 - nextBits); |
---|
[834] | 107 | mBitPos = 7 - nextBits; |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | void |
---|
| 111 | G4OutBitStream::Set8Bits(u_char v, int numBits) |
---|
| 112 | { |
---|
| 113 | if( mBitPos + 1 >= numBits ) |
---|
| 114 | SetFewBits( (u_char)v, numBits ); |
---|
| 115 | else |
---|
| 116 | SetBits2Byte( (u_char)v, numBits ); |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | |
---|
| 120 | void |
---|
| 121 | G4OutBitStream::FullBit( void ) |
---|
| 122 | { |
---|
| 123 | if( mBitPos != 7 ) |
---|
| 124 | SetFewBits( BitFullMaskT[mBitPos], mBitPos+1 ); |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | void |
---|
| 128 | G4OutBitStream::SetByte(u_char dat) |
---|
| 129 | { |
---|
| 130 | if( mWriteFlag ){ |
---|
| 131 | FullBit(); |
---|
| 132 | *mBuf = dat; |
---|
| 133 | IncBuf(); |
---|
| 134 | return; |
---|
| 135 | } |
---|
| 136 | throw( G4BufferError( "SetByte" ) ); |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | void |
---|
| 140 | G4OutBitStream::SetWord(u_int dat) |
---|
| 141 | { |
---|
| 142 | if( mWriteFlag ){ |
---|
| 143 | FullBit(); |
---|
| 144 | *mBuf = (dat >> 8) & 0xff; |
---|
| 145 | IncBuf(); |
---|
| 146 | *mBuf = dat & 0xff; |
---|
| 147 | IncBuf(); |
---|
| 148 | return; |
---|
| 149 | } |
---|
| 150 | throw( G4BufferError( "SetWord" ) ); |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | void |
---|
| 154 | G4OutBitStream::CopyByte(const char* src, int n) |
---|
| 155 | { |
---|
| 156 | if( mBuf+n < mEndOfBuf ){ |
---|
| 157 | FullBit(); |
---|
| 158 | memcpy( mBuf, src, n ); |
---|
| 159 | mBuf += n; |
---|
| 160 | return; |
---|
| 161 | } |
---|
| 162 | throw( G4BufferError( "CopyByte" ) ); |
---|
| 163 | } |
---|
| 164 | |
---|