-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitStream.cpp
More file actions
88 lines (73 loc) · 1.59 KB
/
Copy pathBitStream.cpp
File metadata and controls
88 lines (73 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "BitStream.h"
// CInBitStream
ubyte CInBitStream::ReadBit()
{
if( position / 8 >= buffer.size() ) {
return 0;
}
ubyte result = (buffer[position / 8] >> (position % 8)) & 1;
++position;
return result;
}
ubyte CInBitStream::ReadByte()
{
if( position / 8 >= buffer.size() ) {
return 0;
}
ubyte result = 0;
if( !(position % 8) ) {
result = buffer[position / 8];
position += 8;
return result;
}
result = buffer[position / 8] >> (position % 8);
result |= buffer[position / 8 + 1] << (8 - position % 8);
position += 8;
return result;
}
void CInBitStream::PushBack( ubyte _byte )
{
buffer.push_back( _byte );
}
const std::vector<ubyte>& CInBitStream::GetBuffer() const
{
return buffer;
}
void CInBitStream::Clear()
{
buffer.clear();
position = 0;
}
// COutBitStream
void COutBitStream::WriteBit( ubyte _bit )
{
if( bitsCount + 1 > buffer.size() * 8 ) {
buffer.push_back( 0 );
}
if( _bit == 1 ) {
buffer.back() |= 1 << (bitsCount % 8);
}
++bitsCount;
}
void COutBitStream::WriteByte( ubyte _byte )
{
if( bitsCount == buffer.size() * 8 ) {
buffer.push_back( _byte );
bitsCount += 8;
return;
}
ubyte leftPart = _byte << (bitsCount % 8);
buffer.back() |= leftPart;
ubyte rightPart = _byte >> (8 - bitsCount % 8);
buffer.push_back( rightPart );
bitsCount += 8;
}
const std::vector<ubyte>& COutBitStream::GetBuffer() const
{
return buffer;
}
void COutBitStream::Clear()
{
buffer.clear();
bitsCount = 0;
}