OpenJPH
Open-source implementation of JPEG2000 Part-15
Toggle main menu visibility
Loading...
Searching...
No Matches
ojph_bitbuffer_read.h
Go to the documentation of this file.
1
//***************************************************************************/
2
// This software is released under the 2-Clause BSD license, included
3
// below.
4
//
5
// Copyright (c) 2019, Aous Naman
6
// Copyright (c) 2019, Kakadu Software Pty Ltd, Australia
7
// Copyright (c) 2019, The University of New South Wales, Australia
8
//
9
// Redistribution and use in source and binary forms, with or without
10
// modification, are permitted provided that the following conditions are
11
// met:
12
//
13
// 1. Redistributions of source code must retain the above copyright
14
// notice, this list of conditions and the following disclaimer.
15
//
16
// 2. Redistributions in binary form must reproduce the above copyright
17
// notice, this list of conditions and the following disclaimer in the
18
// documentation and/or other materials provided with the distribution.
19
//
20
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22
// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23
// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24
// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
//***************************************************************************/
32
// This file is part of the OpenJPH software implementation.
33
// File: ojph_bitbuffer_read.h
34
// Author: Aous Naman
35
// Date: 28 August 2019
36
//***************************************************************************/
37
38
39
#ifndef OJPH_BITBUFFER_READ_H
40
#define OJPH_BITBUFFER_READ_H
41
42
#include "
ojph_defs.h
"
43
#include "
ojph_arch.h
"
44
#include "
ojph_file.h
"
45
46
namespace
ojph
{
47
49
//defined elsewhere
50
class
mem_elastic_allocator
;
51
struct
coded_lists
;
52
53
namespace
local
{
54
55
57
struct
bit_read_buf
58
{
59
infile_base
*
file
;
60
ui32
tmp
;
61
int
avail_bits
;
62
bool
unstuff
;
63
ui32
bytes_left
;
64
};
65
67
static
inline
68
void
bb_init
(
bit_read_buf
*bbp,
ui32
bytes_left,
infile_base
* file)
69
{
70
bbp->
avail_bits
= 0;
71
bbp->
file
= file;
72
bbp->
bytes_left
= bytes_left;
73
bbp->
tmp
= 0;
74
bbp->
unstuff
=
false
;
75
}
76
78
static
inline
79
bool
bb_read
(
bit_read_buf
*bbp)
80
{
81
if
(bbp->
bytes_left
> 0)
82
{
83
ui8
t = 0;
84
if
(bbp->
file
->
read
(&t, 1) != 1)
85
throw
"error reading from file"
;
86
bbp->
tmp
= t;
87
bbp->
avail_bits
= 8 - bbp->
unstuff
;
88
bbp->
unstuff
= (t == 0xFF);
89
--bbp->
bytes_left
;
90
return
true
;
91
}
92
else
93
{
94
bbp->
tmp
= 0;
95
bbp->
avail_bits
= 8 - bbp->
unstuff
;
96
bbp->
unstuff
=
false
;
97
return
false
;
98
}
99
}
100
102
static
inline
103
bool
bb_read_bit
(
bit_read_buf
*bbp,
ui32
& bit)
104
{
105
bool
result =
true
;
106
if
(bbp->
avail_bits
== 0)
107
result =
bb_read
(bbp);
108
bit = (bbp->
tmp
>> --bbp->
avail_bits
) & 1;
109
return
result;
110
}
111
113
static
inline
114
bool
bb_read_bits
(
bit_read_buf
*bbp,
int
num_bits,
ui32
& bits)
115
{
116
assert(num_bits <= 32);
117
118
bits = 0;
119
bool
result =
true
;
120
while
(num_bits) {
121
if
(bbp->
avail_bits
== 0)
122
result =
bb_read
(bbp);
123
int
tx_bits =
ojph_min
(bbp->
avail_bits
, num_bits);
124
bits <<= tx_bits;
125
bbp->
avail_bits
-= tx_bits;
126
num_bits -= tx_bits;
127
bits |= (bbp->
tmp
>> bbp->
avail_bits
) & ((1 << tx_bits) - 1);
128
}
129
return
result;
130
}
131
133
static
inline
134
bool
bb_read_chunk
(
bit_read_buf
*bbp,
ui32
num_bytes,
135
coded_lists
*& cur_coded_list,
136
mem_elastic_allocator
*elastic)
137
{
138
assert(bbp->
avail_bits
== 0 && bbp->
unstuff
==
false
);
139
elastic->
get_buffer
(num_bytes +
coded_cb_header::prefix_buf_size
140
+
coded_cb_header::suffix_buf_size
, cur_coded_list);
141
ui32
bytes =
ojph_min
(num_bytes, bbp->
bytes_left
);
142
ui32
bytes_read = (
ui32
)bbp->
file
->
read
(
143
cur_coded_list->
buf
+
coded_cb_header::prefix_buf_size
, bytes);
144
if
(num_bytes > bytes_read)
145
memset(
146
cur_coded_list->
buf
+
coded_cb_header::prefix_buf_size
+ bytes_read,
147
0, num_bytes - bytes_read);
148
bbp->
bytes_left
-= bytes_read;
149
return
bytes_read == bytes;
150
}
151
153
static
inline
154
void
bb_skip_eph
(
bit_read_buf
*bbp)
155
{
156
if
(bbp->
bytes_left
>= 2)
157
{
158
ui8
marker[2];
159
if
(bbp->
file
->
read
(marker, 2) != 2)
160
throw
"error reading from file"
;
161
bbp->
bytes_left
-= 2;
162
if
((
int
)marker[0] != (
EPH
>> 8) || (
int
)marker[1] != (
EPH
& 0xFF))
163
throw
"should find EPH, but found something else"
;
164
}
165
}
166
168
static
inline
169
bool
bb_terminate
(
bit_read_buf
*bbp,
bool
uses_eph)
170
{
171
bool
result =
true
;
172
if
(bbp->
unstuff
)
173
result =
bb_read
(bbp);
174
assert(bbp->
unstuff
==
false
);
175
if
(uses_eph)
176
bb_skip_eph
(bbp);
177
bbp->
tmp
= 0;
178
bbp->
avail_bits
= 0;
179
return
result;
180
}
181
183
static
inline
184
bool
bb_skip_sop
(
bit_read_buf
*bbp)
185
{
186
if
(bbp->
bytes_left
>= 2)
187
{
188
ui8
marker[2];
189
if
(bbp->
file
->
read
(marker, 2) != 2)
190
throw
"error reading from file"
;
191
if
((
int
)marker[0] == (
SOP
>> 8) && (
int
)marker[1] == (
SOP
& 0xFF))
192
{
193
bbp->
bytes_left
-= 2;
194
if
(bbp->
bytes_left
>= 4)
195
{
196
ui16
com_len;
197
if
(bbp->
file
->
read
(&com_len, 2) != 2)
198
throw
"error reading from file"
;
199
com_len = swap_bytes_if_le(com_len);
200
if
(com_len != 4)
201
throw
"something is wrong with SOP length"
;
202
int
result =
203
bbp->
file
->
seek
(com_len - 2,
infile_base::OJPH_SEEK_CUR
);
204
if
(result != 0)
205
throw
"error seeking file"
;
206
bbp->
bytes_left
-= com_len;
207
}
208
else
209
throw
"precinct truncated early"
;
210
return
true
;
211
}
212
else
213
{
214
//put the bytes back
215
if
(bbp->
file
->
seek
(-2,
infile_base::OJPH_SEEK_CUR
) != 0)
216
throw
"error seeking file"
;
217
return
false
;
218
}
219
}
220
221
return
false
;
222
}
223
224
}
225
}
226
227
#endif
// !OJPH_BITBUFFER_READ_H
ojph::infile_base
Definition
ojph_file.h:267
ojph::infile_base::seek
seek
Definition
ojph_file.h:269
ojph::infile_base::OJPH_SEEK_CUR
@ OJPH_SEEK_CUR
Definition
ojph_file.h:271
ojph::infile_base::read
virtual size_t read(void *ptr, size_t size)=0
ojph::mem_elastic_allocator
Definition
ojph_mem.h:218
ojph::mem_elastic_allocator::get_buffer
void get_buffer(ui32 needed_bytes, coded_lists *&p)
Definition
ojph_mem.cpp:113
ojph::local
Definition
ojph_bitbuffer_read.h:53
ojph::local::bb_read_chunk
static bool bb_read_chunk(bit_read_buf *bbp, ui32 num_bytes, coded_lists *&cur_coded_list, mem_elastic_allocator *elastic)
Definition
ojph_bitbuffer_read.h:134
ojph::local::bb_read_bit
static bool bb_read_bit(bit_read_buf *bbp, ui32 &bit)
Definition
ojph_bitbuffer_read.h:103
ojph::local::bb_read_bits
static bool bb_read_bits(bit_read_buf *bbp, int num_bits, ui32 &bits)
Definition
ojph_bitbuffer_read.h:114
ojph::local::bb_skip_sop
static bool bb_skip_sop(bit_read_buf *bbp)
Definition
ojph_bitbuffer_read.h:184
ojph::local::bb_terminate
static bool bb_terminate(bit_read_buf *bbp, bool uses_eph)
Definition
ojph_bitbuffer_read.h:169
ojph::local::bb_read
static bool bb_read(bit_read_buf *bbp)
Definition
ojph_bitbuffer_read.h:79
ojph::local::SOP
@ SOP
Definition
ojph_params_local.h:144
ojph::local::EPH
@ EPH
Definition
ojph_params_local.h:145
ojph::local::bb_init
static void bb_init(bit_read_buf *bbp, ui32 bytes_left, infile_base *file)
Definition
ojph_bitbuffer_read.h:68
ojph::local::bb_skip_eph
static void bb_skip_eph(bit_read_buf *bbp)
Definition
ojph_bitbuffer_read.h:154
ojph
Definition
ojph_img_io.h:52
ojph::ui16
uint16_t ui16
Definition
ojph_defs.h:52
ojph::ui32
uint32_t ui32
Definition
ojph_defs.h:54
ojph::ui8
uint8_t ui8
Definition
ojph_defs.h:50
ojph_arch.h
ojph_defs.h
ojph_min
#define ojph_min(a, b)
Definition
ojph_defs.h:76
ojph_file.h
ojph::coded_lists
Definition
ojph_mem.h:202
ojph::coded_lists::buf
ui8 * buf
Definition
ojph_mem.h:213
ojph::local::bit_read_buf
Definition
ojph_bitbuffer_read.h:58
ojph::local::bit_read_buf::unstuff
bool unstuff
Definition
ojph_bitbuffer_read.h:62
ojph::local::bit_read_buf::bytes_left
ui32 bytes_left
Definition
ojph_bitbuffer_read.h:63
ojph::local::bit_read_buf::avail_bits
int avail_bits
Definition
ojph_bitbuffer_read.h:61
ojph::local::bit_read_buf::file
infile_base * file
Definition
ojph_bitbuffer_read.h:59
ojph::local::bit_read_buf::tmp
ui32 tmp
Definition
ojph_bitbuffer_read.h:60
ojph::local::coded_cb_header::suffix_buf_size
static const int suffix_buf_size
Definition
ojph_codeblock.h:124
ojph::local::coded_cb_header::prefix_buf_size
static const int prefix_buf_size
Definition
ojph_codeblock.h:123
src
core
codestream
ojph_bitbuffer_read.h
Generated by
1.18.0