Grok  10.0.3
BitIO.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2016-2022 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(void) override;
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) override;
50  bool write(uint32_t v) override;
51  /*
52  Read bits
53  @param n Number of bits to read
54  */
55  void read(uint32_t* bits, uint8_t n) override;
56 
57  /*
58  Read bit
59  */
60  uint8_t read(void) override;
61  /*
62  Flush bits
63  @return true if successful, returns false otherwise
64  */
65  bool flush(void) override;
66  /*
67  Passes the ending bits (coming from flushing)
68  */
69  void inalign(void) override;
70 
71  bool putcommacode(uint8_t n);
72  uint8_t getcommacode(void);
73  bool putnumpasses(uint32_t n);
74  void getnumpasses(uint32_t* numpasses);
75 
76  private:
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 
89 
90  bool read0xFF;
91 
92  /*
93  Write a bit
94  @param bio BIO handle
95  @param b Bit to write (0 or 1)
96  */
97  bool putbit(uint8_t b);
98  /*
99  Read a bit
100  @param bio BIO handle
101  */
102  void getbit(uint32_t* bits, uint8_t pos);
103 
104  uint8_t getbit(void);
105 
106  /*
107  Write a byte
108  @param bio BIO handle
109  @return true if successful, returns false otherwise
110  */
111  bool writeByte(void);
112  /*
113  Read a byte
114  @param bio BIO handle
115  */
116  void bytein(void);
117 };
118 
119 } // namespace grk
Definition: BitIO.h:33
uint8_t getcommacode(void)
Definition: BitIO.cpp:174
bool read0xFF
Definition: BitIO.h:90
size_t numBytes(void) override
Number of bytes written.
Definition: BitIO.cpp:106
void inalign(void) override
Passes the ending bits (coming from flushing)
Definition: BitIO.cpp:155
bool flush(void) override
Flush bits.
Definition: BitIO.cpp:147
uint8_t getbit(void)
Definition: BitIO.cpp:96
bool putnumpasses(uint32_t n)
Definition: BitIO.cpp:187
bool putbit(uint8_t b)
Definition: BitIO.cpp:78
uint8_t ct
Definition: BitIO.h:86
void bytein(void)
Definition: BitIO.cpp:58
uint8_t * start
Definition: BitIO.h:78
uint8_t read(void) override
Read bit.
Definition: BitIO.cpp:127
uint8_t buf
Definition: BitIO.h:84
bool write(uint32_t v, uint32_t n) override
Write bits.
Definition: BitIO.cpp:111
size_t offset
Definition: BitIO.h:80
bool writeByte(void)
Definition: BitIO.cpp:37
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:218
bool putcommacode(uint8_t n)
Definition: BitIO.cpp:162
IBufferedStream * stream
Definition: BitIO.h:88
Bit input/output.
Definition: IBitIO.h:26
Copyright (C) 2016-2022 Grok Image Compression Inc.
Definition: ICacheable.h:20
const vfloat64m1_t v
Definition: rvv-inl.h:1742
Definition: IBufferedStream.h:20