OpenJPH: /startdir/src/OpenJPH/src/core/codestream/ojph_bitbuffer_read.h Source File

OpenJPH Open-source implementation of JPEG2000 Part-15

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_file.h"
44
45namespace ojph {
46
48 //defined elsewhere
49 class mem_elastic_allocator;
50 struct coded_lists;
51
52 namespace local {
53
54
56 struct bit_read_buf
57 {
58 infile_base *file;
59 ui32 tmp;
60 int avail_bits;
61 bool unstuff;
62 ui32 bytes_left;
63 };
64
66 static inline
67 void bb_init(bit_read_buf *bbp, ui32 bytes_left, infile_base* file)
68 {
69 bbp->avail_bits = 0;
70 bbp->file = file;
71 bbp->bytes_left = bytes_left;
72 bbp->tmp = 0;
73 bbp->unstuff = false;
74 }
75
77 static inline
78 bool bb_read(bit_read_buf *bbp)
79 {
80 if (bbp->bytes_left > 0)
81 {
82 ui32 t = 0;
83 if (bbp->file->read(&t, 1) != 1)
84 throw "error reading from file";
85 bbp->tmp = t;
86 bbp->avail_bits = 8 - bbp->unstuff;
87 bbp->unstuff = (t == 0xFF);
88 --bbp->bytes_left;
89 return true;
90 }
91 else
92 {
93 bbp->tmp = 0;
94 bbp->avail_bits = 8 - bbp->unstuff;
95 bbp->unstuff = false;
96 return false;
97 }
98 }
99
101 static inline
102 bool bb_read_bit(bit_read_buf *bbp, ui32& bit)
103 {
104 bool result = true;
105 if (bbp->avail_bits == 0)
106 result = bb_read(bbp);
107 bit = (bbp->tmp >> --bbp->avail_bits) & 1;
108 return result;
109 }
110
112 static inline
113 bool bb_read_bits(bit_read_buf *bbp, int num_bits, ui32& bits)
114 {
115 assert(num_bits <= 32);
116
117 bits = 0;
118 bool result = true;
119 while (num_bits) {
120 if (bbp->avail_bits == 0)
121 result = bb_read(bbp);
122 int tx_bits = ojph_min(bbp->avail_bits, num_bits);
123 bits <<= tx_bits;
124 bbp->avail_bits -= tx_bits;
125 num_bits -= tx_bits;
126 bits |= (bbp->tmp >> bbp->avail_bits) & ((1 << tx_bits) - 1);
127 }
128 return result;
129 }
130
132 static inline
133 bool bb_read_chunk(bit_read_buf *bbp, ui32 num_bytes,
134 coded_lists*& cur_coded_list,
135 mem_elastic_allocator *elastic)
136 {
137 assert(bbp->avail_bits == 0 && bbp->unstuff == false);
138 elastic->get_buffer(num_bytes + coded_cb_header::prefix_buf_size
139 + coded_cb_header::suffix_buf_size, cur_coded_list);
140 ui32 bytes = ojph_min(num_bytes, bbp->bytes_left);
141 ui32 bytes_read = (ui32)bbp->file->read(
142 cur_coded_list->buf + coded_cb_header::prefix_buf_size, bytes);
143 if (num_bytes > bytes_read)
144 memset(
145 cur_coded_list->buf + coded_cb_header::prefix_buf_size + bytes_read,
146 0, num_bytes - bytes_read);
147 bbp->bytes_left -= bytes_read;
148 return bytes_read == bytes;
149 }
150
152 static inline
153 void bb_skip_eph(bit_read_buf *bbp)
154 {
155 if (bbp->bytes_left >= 2)
156 {
157 ui8 marker[2];
158 if (bbp->file->read(marker, 2) != 2)
159 throw "error reading from file";
160 bbp->bytes_left -= 2;
161 if ((int)marker[0] != (EPH >> 8) || (int)marker[1] != (EPH & 0xFF))
162 throw "should find EPH, but found something else";
163 }
164 }
165
167 static inline
168 bool bb_terminate(bit_read_buf *bbp, bool uses_eph)
169 {
170 bool result = true;
171 if (bbp->unstuff)
172 result = bb_read(bbp);
173 assert(bbp->unstuff == false);
174 if (uses_eph)
175 bb_skip_eph(bbp);
176 bbp->tmp = 0;
177 bbp->avail_bits = 0;
178 return result;
179 }
180
182 static inline
183 bool bb_skip_sop(bit_read_buf *bbp)
184 {
185 if (bbp->bytes_left >= 2)
186 {
187 ui8 marker[2];
188 if (bbp->file->read(marker, 2) != 2)
189 throw "error reading from file";
190 if ((int)marker[0] == (SOP >> 8) && (int)marker[1] == (SOP & 0xFF))
191 {
192 bbp->bytes_left -= 2;
193 if (bbp->bytes_left >= 4)
194 {
195 ui16 com_len;
196 if (bbp->file->read(&com_len, 2) != 2)
197 throw "error reading from file";
198 com_len = swap_byte(com_len);
199 if (com_len != 4)
200 throw "something is wrong with SOP length";
201 int result =
202 bbp->file->seek(com_len - 2, infile_base::OJPH_SEEK_CUR);
203 if (result != 0)
204 throw "error seeking file";
205 bbp->bytes_left -= com_len;
206 }
207 else
208 throw "precinct truncated early";
209 return true;
210 }
211 else
212 {
213 //put the bytes back
214 if (bbp->file->seek(-2, infile_base::OJPH_SEEK_CUR) != 0)
215 throw "error seeking file";
216 return false;
217 }
218 }
219
220 return false;
221 }
222
223 }
224}
225
226#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:52
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:133
ojph::local::bb_read_bit
static bool bb_read_bit(bit_read_buf *bbp, ui32 &bit)
Definition ojph_bitbuffer_read.h:102
ojph::local::bb_read_bits
static bool bb_read_bits(bit_read_buf *bbp, int num_bits, ui32 &bits)
Definition ojph_bitbuffer_read.h:113
ojph::local::bb_skip_sop
static bool bb_skip_sop(bit_read_buf *bbp)
Definition ojph_bitbuffer_read.h:183
ojph::local::bb_terminate
static bool bb_terminate(bit_read_buf *bbp, bool uses_eph)
Definition ojph_bitbuffer_read.h:168
ojph::local::bb_read
static bool bb_read(bit_read_buf *bbp)
Definition ojph_bitbuffer_read.h:78
ojph::local::SOP
@ SOP
Definition ojph_params_local.h:144
ojph::local::EPH
@ EPH
Definition ojph_params_local.h:145
ojph::local::swap_byte
static ui16 swap_byte(ui16 t)
Definition ojph_codestream_local.h:58
ojph::local::bb_init
static void bb_init(bit_read_buf *bbp, ui32 bytes_left, infile_base *file)
Definition ojph_bitbuffer_read.h:67
ojph::local::bb_skip_eph
static void bb_skip_eph(bit_read_buf *bbp)
Definition ojph_bitbuffer_read.h:153
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_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:57
ojph::local::bit_read_buf::unstuff
bool unstuff
Definition ojph_bitbuffer_read.h:61
ojph::local::bit_read_buf::bytes_left
ui32 bytes_left
Definition ojph_bitbuffer_read.h:62
ojph::local::bit_read_buf::avail_bits
int avail_bits
Definition ojph_bitbuffer_read.h:60
ojph::local::bit_read_buf::file
infile_base * file
Definition ojph_bitbuffer_read.h:58
ojph::local::bit_read_buf::tmp
ui32 tmp
Definition ojph_bitbuffer_read.h:59
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