-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbitstream.c
47 lines (40 loc) · 1020 Bytes
/
bitstream.c
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
/*
* pngview - a standalone PNG viewer
* Copyright 2013 Vegard Edvardsen
*/
#include "bitstream.h"
unsigned char peek_byte(bitstream_t *bitstream)
{
return bitstream->data[bitstream->current_byte];
}
unsigned char read_byte(bitstream_t *bitstream)
{
bitstream->current_bit = 0;
return bitstream->data[bitstream->current_byte++];
}
unsigned int peek_bit(bitstream_t *bitstream)
{
return (peek_byte(bitstream) >> bitstream->current_bit) & 1;
}
unsigned int read_bit(bitstream_t *bitstream)
{
if (bitstream->current_bit < 7) {
return (peek_byte(bitstream) >> bitstream->current_bit++) & 1;
} else {
return (read_byte(bitstream) >> 7) & 1;
}
}
unsigned int read_bits(bitstream_t *bitstream, int bits)
{
unsigned int value = 0;
for (int bit = 0; bit < bits; bit++) {
value |= read_bit(bitstream) << bit;
}
return value;
}
void skip_to_next_byte(bitstream_t *bitstream)
{
if (bitstream->current_bit > 0) {
read_byte(bitstream);
}
}