-
Notifications
You must be signed in to change notification settings - Fork 3
/
Bits.cpp
353 lines (299 loc) · 8.19 KB
/
Bits.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/* RLZ compress
* Copyright (C) 2011 Shanika Kuruppu
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Bits.cpp: Implements a bit writer that writes to an output file and a
* bit reader that reads from an input stream of bits. Implements Golomb
* coding and binary codig on top of the bit reader and bit writer.
* Author: Shanika Kuruppu ([email protected])
*/
#include <iostream>
#include <cstdlib>
#include "Bits.h"
#define BITSPERBYTE 8
#define BITSPERWORD 32
using namespace std;
BitWriter::BitWriter() :
outfile(*(ofstream*)NULL) { }
BitWriter::BitWriter(ofstream &outfile) :
outfile(outfile)
{
// Set all the bits to zeros
this->currbyte = 0;
// All bits in currbyte are available
this->remaining = BITSPERBYTE;
}
void BitWriter::write_bit(unsigned int bit)
{
if (bit > 2)
{
cerr << "write_bit(): Bit can only have either a 0 or 1. Input " << bit << endl;
exit(1);
}
// Put the current bit at the back of the buffer
currbyte <<= 1;
currbyte |= (unsigned char)bit;
remaining--;
// If no space left on the buffer, flush it to the output stream
if (remaining == 0)
{
flush();
}
}
void BitWriter::flush()
{
if (remaining < BITSPERBYTE)
{
// Shifts the bits to the left so that the first bit that arrived is
// at the left-most end of the byte
currbyte <<= remaining;
// Write the byte
outfile.write((char*)&currbyte, sizeof(unsigned char));
// Re-initialize the buffer and the remaining bit counter
currbyte = 0;
remaining = BITSPERBYTE;
}
}
void BitWriter::int_to_binary(uint64_t n, uint64_t length)
{
uint64_t i;
unsigned int bit;
// Check if the int is possible to be represented with length bits
if (n >= ((unsigned)0x01 << length))
{
cerr << "Warning: " << length << " bits is not enough to encode "
<< n << '.' << endl;
}
// Write out the bit representation of n
for (i=length; i>0; i--)
{
bit = n & (0x01 << (i-1));
bit >>= (i-1);
write_bit(bit);
}
}
BitReader::BitReader() :
infile(*(ifstream*)NULL) {}
BitReader::BitReader(ifstream &infile) :
infile(infile)
{
// Set all the bits to zeros
this->currbyte = 0;
// All bits in currbyte are available
this->remaining = 0;
}
unsigned int BitReader::read_bit()
{
BitsUnexpectedException uexp;
BitsEOFException eofexp;
// Check if it's EOF
if (infile.eof())
{
currbyte = 0;
remaining = 0;
// Throw an exception to indicate that it's EOF
throw eofexp;
}
// Check if the file is readable
if (!infile.good())
{
currbyte = 0;
remaining = 0;
// Throw an exception to indicate that there's an error
throw uexp;
}
// Check if currbyte has any more bits left to be read. If not then
// read a new byte from the input file.
if (remaining == 0)
{
// The infile object may have exceptions set up to be raised
// when an IO error occurs so try to catch them if possible
try
{
infile.read((char*)&currbyte, sizeof(unsigned char));
remaining = BITSPERBYTE;
}
catch (ifstream::failure e)
{
if (infile.eof())
throw eofexp;
else
throw uexp;
}
}
// Store the next bit in the stream into the output variable
unsigned int bit;
bit = currbyte & mask;
bit >>= BITSPERBYTE-1;
// Shift by one bit to the left so that the next bit in the stream
// is in front
currbyte <<= 1;
// Update remaining to the number of bits yet to be read from
// currbyte
remaining --;
return bit;
}
uint64_t BitReader::binary_to_int(uint64_t length)
{
uint64_t i, n = 0;
unsigned int bit;
// Read the integer
for (i=length; i>0; i--)
{
bit = read_bit();
n |= (bit << (i-1));
}
return n;
}
GolombCoder::GolombCoder(BitWriter &bwriter) :
bwriter(bwriter), breader(*(BitReader*)NULL)
{
// Set the default divisor
this->b = 64;
// Set the number of bits needed to represent the default divisor
this->log2b = 6;
}
GolombCoder::GolombCoder(BitWriter &bwriter, unsigned int b) :
bwriter(bwriter), breader(*(BitReader*)NULL)
{
// Check to see if the divisor given is a power of two
if ((this->log2b = is_power_of_two(b)) > 0)
{
this->b = b;
}
// Otherwise it's an error
else
{
cerr << "The divisor b must be a power of 2." << endl;
exit(1);
}
}
GolombCoder::GolombCoder(BitReader &breader) :
bwriter(*(BitWriter*)NULL), breader(breader)
{
// Set the default divisor
this->b = 64;
// Set the number of bits needed to represent the default divisor
this->log2b = 6;
}
GolombCoder::GolombCoder(BitReader &breader, unsigned int b) :
bwriter(*(BitWriter*)NULL), breader(breader)
{
// Check to see if the divisor given is a power of two
if ((this->log2b = is_power_of_two(b)) > 0)
{
this->b = b;
}
// Otherwise it's an error
else
{
cerr << "The divisor b must be a power of 2." << endl;
exit(1);
}
}
void GolombCoder::golomb_encode(uint64_t n, unsigned int b, unsigned int log2b)
{
uint64_t quotient, remainder, i;
// Get the quotient and the remainder from dividing n by b
quotient = n >> log2b;
remainder = (n << ((sizeof(uint64_t)<<3)-log2b)) >>
((sizeof(uint64_t)<<3)-log2b);
// Encode the quotient
for (i = 0; i<quotient; i++)
{
bwriter.write_bit(1);
}
bwriter.write_bit(0);
// Encode the remainder
bwriter.int_to_binary(remainder, log2b);
return;
}
void GolombCoder::golomb_encode(uint64_t n, unsigned int b)
{
unsigned int log2b;
// Check if the divisor is large enough and its a power of two
if (b < 2 || (log2b = is_power_of_two(b)) == 0)
{
cerr << "The divisor b must be a power of 2." << endl;
exit(1);
}
golomb_encode(n, b, log2b);
return;
}
void GolombCoder::golomb_encode(uint64_t n)
{
golomb_encode(n, this->b, this->log2b);
return;
}
int GolombCoder::is_power_of_two(unsigned int b)
{
unsigned int i, bit;
// Check that b is a power of 2
for (i=0; i<BITSPERWORD; i++)
{
bit = b & 0x01;
b >>= 1;
if (bit)
{
// b is a power of 2
if (i>0 && !b)
return i;
else
return 0;
}
}
return false;
}
uint64_t GolombCoder::golomb_decode(unsigned int b, unsigned int log2b)
{
uint64_t quotient, remainder;
unsigned int bit;
// Decode the quotient
quotient = 0;
bit = breader.read_bit();
while (bit == 1)
{
quotient ++;
bit = breader.read_bit();
}
// Decode the remainder
remainder = breader.binary_to_int(log2b);
// Calculate the original value
return quotient*b+remainder;
}
uint64_t GolombCoder::golomb_decode(unsigned int b)
{
unsigned int log2b;
// Check if the divisor is large enough and its a power of two
if (b < 2 || (log2b = is_power_of_two(b)) == 0)
{
cerr << "The divisor b must be a power of 2." << endl;
exit(1);
}
return golomb_decode(b, log2b);
}
uint64_t GolombCoder::golomb_decode()
{
return golomb_decode(this->b, this->log2b);
}
const char* BitsUnexpectedException::what() const throw()
{
return "Unexpected error in file.\n";
}
const char* BitsEOFException::what() const throw()
{
return "End of file.\n";
}