Grok  9.5.0
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  BitIO(uint8_t* bp, uint64_t len, bool isCompressor);
36  BitIO(IBufferedStream* stream, bool isCompressor);
37 
38  /*
39  Number of bytes written.
40  @return the number of bytes written
41  */
42  size_t numBytes();
43 
44  /*
45  Write bits
46  @param v Value of bits
47  @param n Number of bits to write
48  */
49  bool write(uint32_t v, uint32_t n);
50  /*
51  Read bits
52  @param n Number of bits to read
53  */
54  void read(uint32_t* bits, uint32_t n);
55  /*
56  Flush bits
57  @return true if successful, returns false otherwise
58  */
59  bool flush();
60  /*
61  Passes the ending bits (coming from flushing)
62  */
63  void inalign();
64 
65  bool putcommacode(uint8_t n);
66  void getcommacode(uint8_t* n);
67  bool putnumpasses(uint32_t n);
68  void getnumpasses(uint32_t* numpasses);
69 
70  private:
71  /* pointer to the start of the buffer */
72  uint8_t* start;
73 
74  size_t offset;
75  size_t buf_len;
76 
77  /* temporary place where each byte is read or written */
78  uint8_t buf;
79  /* coder : number of bits free to write. decoder : number of bits read */
80  uint8_t ct;
81 
83 
84  bool read0xFF;
85 
86  /*
87  Write a bit
88  @param bio BIO handle
89  @param b Bit to write (0 or 1)
90  */
91  bool putbit(uint8_t b);
92  /*
93  Read a bit
94  @param bio BIO handle
95  */
96  void getbit(uint32_t* bits, uint8_t pos);
97  /*
98  Write a byte
99  @param bio BIO handle
100  @return true if successful, returns false otherwise
101  */
102  bool writeByte();
103  /*
104  Read a byte
105  @param bio BIO handle
106  */
107  void bytein();
108 };
109 
110 } // namespace grk
Definition: BitIO.h:33
bool read0xFF
Definition: BitIO.h:84
void getcommacode(uint8_t *n)
Definition: BitIO.cpp:155
size_t numBytes()
Number of bytes written.
Definition: BitIO.cpp:98
void read(uint32_t *bits, uint32_t n)
Read bits.
Definition: BitIO.cpp:114
void inalign()
Passes the ending bits (coming from flushing)
Definition: BitIO.cpp:137
bool writeByte()
Definition: BitIO.cpp:37
bool flush()
Flush bits.
Definition: BitIO.cpp:129
bool putnumpasses(uint32_t n)
Definition: BitIO.cpp:167
bool putbit(uint8_t b)
Definition: BitIO.cpp:80
uint8_t ct
Definition: BitIO.h:80
bool write(uint32_t v, uint32_t n)
Write bits.
Definition: BitIO.cpp:103
uint8_t * start
Definition: BitIO.h:72
uint8_t buf
Definition: BitIO.h:78
size_t offset
Definition: BitIO.h:74
void getbit(uint32_t *bits, uint8_t pos)
Definition: BitIO.cpp:89
void bytein()
Definition: BitIO.cpp:57
size_t buf_len
Definition: BitIO.h:75
BitIO(uint8_t *bp, uint64_t len, bool isCompressor)
Definition: BitIO.cpp:25
void getnumpasses(uint32_t *numpasses)
Definition: BitIO.cpp:198
bool putcommacode(uint8_t n)
Definition: BitIO.cpp:144
IBufferedStream * stream
Definition: BitIO.h:82
Bit input/output.
Definition: IBitIO.h:26
Copyright (C) 2016-2021 Grok Image Compression Inc.
Definition: ICacheable.h:20
Definition: IBufferedStream.h:20