FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
ImageEncoder/BitStream.cpp at master · ThenTech/ImageEncoder · GitHub
ThenTech
/
ImageEncoder
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
ImageEncoder
/
BitStream.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
102 lines (73 loc) · 2.92 KB
Breadcrumbs
ImageEncoder
/
BitStream.cpp
Copy path
File metadata and controls
102 lines (73 loc) · 2.92 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#
include
"
BitStream.hpp
"
namespace
util
{
const
uint8_t
bitmasks[] = {
0
,
128
,
192
,
224
,
240
,
248
,
252
,
254
};
BitStreamReader::BitStreamReader
(
uint8_t
*b,
size_t
s) : BitStream(b, s,
0
,
false
) {}
BitStreamReader::~BitStreamReader
() {}
void
BitStreamReader::flush
() {
this
->
position
=
this
->
get_last_byte_position
() *
8u
;
}
uint8_t
BitStreamReader::get_bit
() {
const
size_t
current_start_byte =
this
->
position
/
8u
;
if
(current_start_byte >=
this
->
get_size
()) {
//
Prevent reading byte outside of array (-> Valgrind flagged)
return
0u
;
}
const
size_t
bits_taken =
this
->
position
%
8
;
const
uint8_t
value =
this
->
buffer
[current_start_byte];
this
->
position
++;
return
(value & (
1
<< (
7
- bits_taken))) !=
0
;
}
uint32_t
BitStreamReader::get
(
size_t
l) {
uint32_t
value =
0
;
for
(
size_t
i =
0
; i < l; i++) {
const
uint32_t
v =
this
->
get_bit
();
value |= v << (l - i -
1
);
//
printf("%s %d pos %d i %d v %d value %x\n", __func__, __LINE__, position, i, v, value);
}
return
value;
}
//
//////////////////////////////////////////////////////////////////////////////////
BitStreamWriter::BitStreamWriter
(
size_t
s)
: BitStream(util::allocArray<
uint8_t
>(s), s,
0
,
true
)
{
//
printf("Allocated buffer of size: %d\n", s);
}
BitStreamWriter::BitStreamWriter
(
uint8_t
*b,
size_t
s) : BitStream(b, s,
0
,
false
) {}
BitStreamWriter::~BitStreamWriter
() {}
void
BitStreamWriter::flush
() {
if
(
this
->
position
%
8
!=
0
) {
this
->
buffer
[
this
->
position
/
8
] &= bitmasks[
this
->
position
%
8
];
this
->
position
+=
8
- (
this
->
position
%
8
);
}
}
void
BitStreamWriter::put_bit
(
int8_t
value) {
const
size_t
bits_taken =
this
->
position
%
8
;
if
(value) {
this
->
buffer
[
this
->
position
/
8
] |=
1
<< (
7
- bits_taken);
}
else
{
this
->
buffer
[
this
->
position
/
8
] &= ~(
1
<< (
7
- bits_taken));
}
this
->
position
++;
}
void
BitStreamWriter::put
(
size_t
length,
uint32_t
value) {
for
(
size_t
p =
0
; p < length; p++) {
put_bit
(
1
& (value >> (length -
1
- p)));
}
}
void
write
(
FILE
*f,
const
BitStreamWriter &b) {
const
size_t
position = b.
get_position
();
const
uint8_t
*buffer = b.
get_buffer
();
fwrite
(buffer,
1
, position /
8
, f);
if
(position %
8
!=
0
) {
fwrite
(buffer + position /
8
,
1
,
1
, f);
}
}
void
write
(std::ofstream &fs,
const
BitStreamWriter &b) {
const
size_t
position = b.
get_position
();
const
uint8_t
*buffer = b.
get_buffer
();
fs.
write
(
reinterpret_cast
<
const
char
*>(buffer), position /
8
);
if
(position %
8
!=
0
) {
fs.
write
(
reinterpret_cast
<
const
char
*>(buffer + position /
8
),
1
);
}
}
}
Back
|
FazBrowse Home
|
New Git URL