Grok  7.6.6
BitIO.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016-2021 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 isCompressor);
37  BitIO(IBufferedStream *stream, bool isCompressor);
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  bool sim_out;
89 
91 
92  bool read0xFF;
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  */
117  bool byteout_stream();
118  /*
119  Read a byte
120  @param bio BIO handle
121  */
122  void bytein();
123 
124 };
125 
126 }
127 
Definition: BitIO.h:32
size_t numbytes()
Number of bytes written.
Definition: BitIO.cpp:106
bool read0xFF
Definition: BitIO.h:92
void read(uint32_t *bits, uint32_t n)
Read bits.
Definition: BitIO.cpp:119
void inalign()
Passes the ending bits (coming from flushing)
Definition: BitIO.cpp:142
bool byteout_stream()
Definition: BitIO.cpp:62
bool flush()
Flush bits.
Definition: BitIO.cpp:132
bool putbit(uint8_t b)
Definition: BitIO.cpp:88
uint8_t ct
Definition: BitIO.h:86
bool write(uint32_t v, uint32_t n)
Write bits.
Definition: BitIO.cpp:110
bool byteout()
Definition: BitIO.cpp:46
uint8_t * start
Definition: BitIO.h:78
void putcommacode(int32_t n)
Definition: BitIO.cpp:148
void simulateOutput(bool doSimulate)
Definition: BitIO.h:66
uint8_t buf
Definition: BitIO.h:84
void putnumpasses(uint32_t n)
Definition: BitIO.cpp:164
size_t offset
Definition: BitIO.h:80
void getbit(uint32_t *bits, uint8_t pos)
Definition: BitIO.cpp:98
void bytein()
Definition: BitIO.cpp:70
size_t buf_len
Definition: BitIO.h:81
BitIO(uint8_t *bp, uint64_t len, bool isCompressor)
Definition: BitIO.cpp:25
void getnumpasses(uint32_t *numpasses)
Definition: BitIO.cpp:177
bool sim_out
Definition: BitIO.h:88
IBufferedStream * stream
Definition: BitIO.h:90
void getcommacode(uint32_t *n)
Definition: BitIO.cpp:154
Bit input/output.
Definition: IBitIO.h:25
Copyright (C) 2016-2021 Grok Image Compression Inc.
Definition: BitIO.cpp:23
Definition: IBufferedStream.h:21