This repository was archived by the owner on Sep 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgolomb.cpp
More file actions
76 lines (61 loc) · 1.38 KB
/
golomb.cpp
File metadata and controls
76 lines (61 loc) · 1.38 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
#include "golomb.h"
static void UnsignedGolombEncode(const unsigned int in, BitOutputManager& bitManger)
{
unsigned int M = 0;
unsigned int info;
unsigned int val2 = in + 1;
while (val2>1) //get the log base 2 of val.
{
val2 >>= 1;
++M;
}
info = in - (1<<M) + 1;
for(unsigned int i = 1; i <= M ; ++i)
{
bitManger.OutputBit(0);
}
bitManger.OutputBit(1);
for(unsigned int i = 0 ; i < M; ++i)
{
bitManger.OutputBit((info & (1<<i)) != 0);
}
}
size_t GolombEncode(const unsigned int* in, BYTE* out, size_t length)
{
BitOutputManager bitManager(out);
for(unsigned int i = 0; i < length; i++)
{
UnsignedGolombEncode(*(in + i), bitManager);
}
bitManager.Flush();
return bitManager.Size();
}
static size_t UnsignedGolombDecode(BitInputManager& bitManger)
{
unsigned int M = 0;
unsigned int info = 0;
bool bit = 0;
do
{
bit = bitManger.InputBit();
M += !bit;
}
while(!bit && M < 64);//terminate if the number is too big
for(unsigned int i = 0 ; i < M; ++i)
{
if(bitManger.InputBit())
{
info |= (1<<i);
}
}
return (1<<M) - 1 + info;
}
size_t GolombDecode(const BYTE* in, unsigned int* out, size_t length)
{
BitInputManager bitManager(in);
for(size_t i = 0; i < length; i++)
{
*(out + i) = UnsignedGolombDecode(bitManager);
}
return bitManager.ByteOffset();
}