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_arch.h"
44#include "ojph_file.h"
45
46namespace ojph {
47
49 //defined elsewhere
51 struct coded_lists;
52
53 namespace local {
54
55
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
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
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);
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
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
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
virtual size_t read(void *ptr, size_t size)=0
void get_buffer(ui32 needed_bytes, coded_lists *&p)
Definition ojph_mem.cpp:113
static bool bb_read_chunk(bit_read_buf *bbp, ui32 num_bytes, coded_lists *&cur_coded_list, mem_elastic_allocator *elastic)
static bool bb_read_bit(bit_read_buf *bbp, ui32 &bit)
static bool bb_read_bits(bit_read_buf *bbp, int num_bits, ui32 &bits)
static bool bb_skip_sop(bit_read_buf *bbp)
static bool bb_terminate(bit_read_buf *bbp, bool uses_eph)
static bool bb_read(bit_read_buf *bbp)
static void bb_init(bit_read_buf *bbp, ui32 bytes_left, infile_base *file)
static void bb_skip_eph(bit_read_buf *bbp)
static ui16 swap_bytes_if_le(ui16 t)
Definition ojph_arch.h:397
uint16_t ui16
Definition ojph_defs.h:52
uint32_t ui32
Definition ojph_defs.h:54
uint8_t ui8
Definition ojph_defs.h:50
#define ojph_min(a, b)
Definition ojph_defs.h:76