-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdecode.cpp
105 lines (96 loc) · 1.87 KB
/
decode.cpp
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "decode.h"
Decode::Decode(void)
{
buffer = 0;
bits_in_buf = 0;
end_decoding = false;
low = 0;
high = MAX_VALUE;
}
Decode::~Decode(void)
{
}
void Decode::load_first_value(void)
{
value = 0;
for (int i = 1; i <= CODE_VALUE; i++)
value = 2 * value + get_bit();
}
void Decode::decode(char *infile, char *outfile)
{
in.open(infile, ios::binary | ios::in);
out.open(outfile, ios::binary | ios::out);
if (!in || !out)
{
cout<<"Error: Can`t open file"<<endl;
return;
}
load_first_value();
while (true)
{
int ch;
int sym_index;
sym_index = decode_symbol();
if ((sym_index == EOF_SYMBOL) || end_decoding)
break;
ch = index_to_char[sym_index];
out.put(ch);
update_tables(sym_index);
}
cout<<"Decoding is done"<<endl;
in.close();
out.close();
}
int Decode::decode_symbol(void)
{
int range;
int cum;
int symbol_index;
range = high - low;
cum = ((((value - low) + 1) * cum_freq[0] - 1) / range); // +- Íóæíî äëÿ òî ëè îêðóãëåíèÿ òî ëè ïðåîáðàçîâàíèÿ, íî èíà÷å íå ðàáîòàåò
for (symbol_index = 1; cum_freq[symbol_index] > cum; symbol_index++);
high = low + (range * cum_freq[symbol_index - 1]) / cum_freq[0];
low = low + (range * cum_freq[symbol_index]) / cum_freq[0];
for (;;)
{
if (high < HALF)
{
}
else if (low >= HALF)
{
value -= HALF;
low -= HALF;
high -= HALF;
}
else if (low >= FIRST_QTR && high < THIRD_QTR)
{
value -= FIRST_QTR;
low -= FIRST_QTR;
high -= FIRST_QTR;
}
else
break;
low = 2 * low;
high = 2 * high;
value = 2 * value + get_bit();
}
return symbol_index;
}
int Decode::get_bit(void)
{
int t;
if (bits_in_buf == 0)
{
buffer = in.get();
if (buffer == EOF)
{
end_decoding = true;
return -1;
}
bits_in_buf= 8;
}
t = buffer & 1;
buffer >>= 1;
bits_in_buf--;
return t;
}