Grok  7.6.0
BitIO.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016-2020 Grok Image Compression Inc.
3  *
4  * This source code is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Affero General Public License, version 3,
6  * as published by the Free Software Foundation.
7  *
8  * This source code is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Affero General Public License for more details.
12  *
13  * You should have received a copy of the GNU Affero General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 
16  *
17  * This source code incorporates work covered by the BSD 2-clause license.
18  * Please see the LICENSE file in the root directory for details.
19  *
20  */
21 
22 #pragma once
23 
24 #include "IBitIO.h"
25 #include "IBufferedStream.h"
26 
27 namespace grk {
28 
29 /*
30  Bit input/output
31  */
32 class BitIO: public IBitIO {
33 
34 public:
35 
36  BitIO(uint8_t *bp, uint64_t len, bool isEncoder);
37  BitIO(IBufferedStream *stream, bool isEncoder);
38 
39  /*
40  Number of bytes written.
41  @return the number of bytes written
42  */
43  size_t numbytes();
44 
45  /*
46  Write bits
47  @param v Value of bits
48  @param n Number of bits to write
49  */
50  bool write(uint32_t v, uint32_t n);
51  /*
52  Read bits
53  @param n Number of bits to read
54  */
55  void read(uint32_t *bits, uint32_t n);
56  /*
57  Flush bits
58  @return true if successful, returns false otherwise
59  */
60  bool flush();
61  /*
62  Passes the ending bits (coming from flushing)
63  */
64  void inalign();
65 
66  void simulateOutput(bool doSimulate) {
67  sim_out = doSimulate;
68  }
69 
70  void putcommacode(int32_t n);
71  void getcommacode(uint32_t *n);
72  void putnumpasses(uint32_t n);
73  void getnumpasses(uint32_t *numpasses);
74 
75 private:
76 
77  /* pointer to the start of the buffer */
78  uint8_t *start;
79 
80  size_t offset;
81  size_t buf_len;
82 
83  /* temporary place where each byte is read or written */
84  uint8_t buf;
85  /* coder : number of bits free to write. decoder : number of bits read */
86  uint8_t ct;
87 
88  size_t total_bytes;
89 
90  bool sim_out;
91 
93 
94  /*
95  Write a bit
96  @param bio BIO handle
97  @param b Bit to write (0 or 1)
98  */
99  bool putbit(uint8_t b);
100  /*
101  Read a bit
102  @param bio BIO handle
103  */
104  void getbit(uint32_t *bits, uint8_t pos);
105  /*
106  Write a byte
107  @param bio BIO handle
108  @return true if successful, returns false otherwise
109  */
110  bool byteout();
111 
112  /*
113  Write a byte
114  @param bio BIO handle
115  @return true if successful, returns false otherwise
116  */
118  /*
119  Read a byte
120  @param bio BIO handle
121  */
122  void bytein();
123 
124 };
125 
126 }
127 
grk::BitIO::buf
uint8_t buf
Definition: BitIO.h:84
grk::BitIO::ct
uint8_t ct
Definition: BitIO.h:86
grk::BitIO::total_bytes
size_t total_bytes
Definition: BitIO.h:88
grk::BitIO::write
bool write(uint32_t v, uint32_t n)
Write bits.
IBitIO.h
grk::BitIO::getcommacode
void getcommacode(uint32_t *n)
grk::BitIO::putcommacode
void putcommacode(int32_t n)
grk::BitIO::stream
IBufferedStream * stream
Definition: BitIO.h:92
grk::BitIO::offset
size_t offset
Definition: BitIO.h:80
grk::BitIO::flush
bool flush()
Flush bits.
grk::BitIO::start
uint8_t * start
Definition: BitIO.h:78
grk::BitIO::buf_len
size_t buf_len
Definition: BitIO.h:81
grk::BitIO::putbit
bool putbit(uint8_t b)
grk
Copyright (C) 2016-2020 Grok Image Compression Inc.
Definition: BitIO.h:27
grk::BitIO::getnumpasses
void getnumpasses(uint32_t *numpasses)
grk::BitIO
Definition: BitIO.h:32
grk::BitIO::inalign
void inalign()
Passes the ending bits (coming from flushing)
grk::BitIO::putnumpasses
void putnumpasses(uint32_t n)
grk::BitIO::numbytes
size_t numbytes()
Number of bytes written.
grk::BitIO::BitIO
BitIO(IBufferedStream *stream, bool isEncoder)
grk::BitIO::read
void read(uint32_t *bits, uint32_t n)
Read bits.
IBufferedStream.h
grk::IBitIO
Bit input/output.
Definition: IBitIO.h:25
grk::BitIO::byteout_stream
bool byteout_stream()
grk::BitIO::BitIO
BitIO(uint8_t *bp, uint64_t len, bool isEncoder)
grk::BitIO::simulateOutput
void simulateOutput(bool doSimulate)
Definition: BitIO.h:66
grk::IBufferedStream
Definition: IBufferedStream.h:21
grk::BitIO::bytein
void bytein()
grk::BitIO::byteout
bool byteout()
grk::BitIO::sim_out
bool sim_out
Definition: BitIO.h:90
grk::BitIO::getbit
void getbit(uint32_t *bits, uint8_t pos)