Grok 10.0.5
BufferedStream.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2016-2023 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 "grk_config_private.h"
25#include "IBitIO.h"
26
27namespace grk
28{
29#define GROK_STREAM_STATUS_OUTPUT 0x1U
30#define GROK_STREAM_STATUS_INPUT 0x2U
31#define GROK_STREAM_STATUS_END 0x4U
32#define GROK_STREAM_STATUS_ERROR 0x8U
33
34struct BufferedStream
35{
37 BufferedStream(uint8_t* buffer, size_t buffer_size, bool l_is_input);
38
39 static BufferedStream* getImpl(grk_stream* stream);
40 grk_stream* getWrapper(void);
41
42 void setUserData(void* data, grk_stream_free_user_data_fn freeUserDataFun);
43 void* getUserData(void);
44 void setUserDataLength(uint64_t len);
45 uint32_t getStatus(void);
46 void setReadFunction(grk_stream_read_fn fn);
47 void setZeroCopyReadFunction(grk_stream_zero_copy_read_fn fn);
48 void setWriteFunction(grk_stream_write_fn fn);
49 void setSeekFunction(grk_stream_seek_fn fn);
58 size_t read(uint8_t* buffer, size_t p_size);
59
60 // low-level write methods (endian taken into account)
61 bool writeShort(uint16_t value);
62 bool write24(uint32_t value);
63 bool writeInt(uint32_t value);
64 bool write64(uint64_t value);
65
66 // low-level write methods (endian NOT taken into account)
67 bool writeByte(uint8_t value);
76 size_t writeBytes(const uint8_t* buffer, size_t p_size);
77
83 bool flush();
90 bool skip(int64_t p_size);
96 uint64_t tell(void);
102 uint64_t numBytesLeft(void);
109 bool seek(uint64_t offset);
113 bool hasSeek();
114 bool supportsZeroCopy();
115 uint8_t* getZeroCopyPtr();
116
117 void setFormat(GRK_CODEC_FORMAT format);
118 GRK_CODEC_FORMAT getFormat(void);
119
120 private:
121 ~BufferedStream();
128 bool write_skip(int64_t p_size);
129
136 bool read_skip(int64_t p_size);
137
144 bool read_seek(uint64_t offset);
145
151 bool write_seek(uint64_t offset);
152
153 void writeIncrement(size_t p_size);
154 template<typename TYPE>
155 bool write(TYPE value, uint8_t numBytes);
156 void invalidate_buffer();
157
158 bool isMemStream();
159
161
185 grk_stream_zero_copy_read_fn zero_copy_read_fn_;
197 uint32_t status_;
198
199 grk_buf8* buf_;
200
201 // number of bytes read in, or slated for write
203
204 // number of seekable bytes in buffer. This will equal
205 // the number of bytes
206 // read in the last media read.
207 // We always have buffered_bytes_ <= read_bytes_seekable_
209
210 // number of bytes read/written from the beginning of the stream
212
214};
215
216template<typename TYPE>
217void grk_write(uint8_t* buffer, TYPE value, uint32_t numBytes)
218{
219 if(numBytes == 0)
220 return;
221 assert(numBytes <= sizeof(TYPE));
222#if defined(GROK_BIG_ENDIAN)
223 const uint8_t* dataPtr = ((const uint8_t*)&value) + sizeof(TYPE) - numBytes;
224 memcpy(buffer, dataPtr, numBytes);
225#else
226 const uint8_t* dataPtr = ((const uint8_t*)&value) + (size_t)(numBytes - 1);
227 for(uint32_t i = 0; i < numBytes; ++i)
228 *(buffer++) = *(dataPtr--);
229#endif
230}
231
232template<typename TYPE>
233void grk_write(uint8_t* buffer, TYPE value)
234{
235 grk_write<TYPE>(buffer, value, sizeof(TYPE));
236}
237
238template<typename TYPE>
239void grk_read(const uint8_t* buffer, TYPE* value, uint32_t numBytes)
240{
241 assert(numBytes > 0 && numBytes <= sizeof(TYPE));
242#if defined(GROK_BIG_ENDIAN)
243 auto dataPtr = ((uint8_t*)value);
244 *value = 0;
245 memcpy(dataPtr + sizeof(TYPE) - numBytes, buffer, numBytes);
246#else
247 auto dataPtr = ((uint8_t*)value) + numBytes - 1;
248 *value = 0;
249 for(uint32_t i = 0; i < numBytes; ++i)
250 *(dataPtr--) = *(buffer++);
251#endif
252}
253
254template<typename TYPE>
255void grk_read(const uint8_t* buffer, TYPE* value)
256{
257 grk_read<TYPE>(buffer, value, sizeof(TYPE));
258}
259
260} // namespace grk
BufferedStream * stream
Definition BitIO.h:88
size_t offset
Definition BitIO.h:80
size_t read_bytes_seekable_
Definition BufferedStream.h:208
grk_stream_write_fn write_fn_
Pointer to actual write function (nullptr at initialization).
Definition BufferedStream.h:189
uint64_t stream_offset_
Definition BufferedStream.h:211
grk_stream_zero_copy_read_fn zero_copy_read_fn_
Pointer to actual zero copy read function (nullptr at initialization).
Definition BufferedStream.h:185
uint32_t status_
Stream status flags.
Definition BufferedStream.h:197
grk_object obj
Definition BufferedStream.h:160
grk_stream_seek_fn seek_fn_
Pointer to actual seek function (if available).
Definition BufferedStream.h:193
GRK_CODEC_FORMAT format_
Definition BufferedStream.h:213
uint64_t user_data_length_
User data length.
Definition BufferedStream.h:177
grk_stream_free_user_data_fn free_user_data_fn_
Pointer to function to free user_data_ (nullptr at initialization) when destroying the stream.
Definition BufferedStream.h:171
friend GrkObjectWrapperImpl< BufferedStream >
Definition BufferedStream.h:36
size_t buffered_bytes_
Definition BufferedStream.h:202
grk_stream_read_fn read_fn_
Pointer to actual read function (nullptr at initialization).
Definition BufferedStream.h:181
void * user_data_
user data
Definition BufferedStream.h:165
uint32_t len
Definition Codeblock.h:38
uint8_t * data
Definition Codeblock.h:61
uint8_t * buffer
packet header storage original buffer
Definition PPMMarker.h:64
T value
Definition TagTree.h:37
T * buf_
Definition buffer.h:223
enum _GRK_CODEC_FORMAT GRK_CODEC_FORMAT
Supported JPEG 2000 formats.
size_t(* grk_stream_read_fn)(uint8_t *buffer, size_t numBytes, void *user_data)
Definition grok_private.h:29
bool(* grk_stream_seek_fn)(uint64_t numBytes, void *user_data)
Definition grok_private.h:38
void(* grk_stream_free_user_data_fn)(void *user_data)
Definition grok_private.h:42
size_t(* grk_stream_write_fn)(const uint8_t *buffer, size_t numBytes, void *user_data)
Definition grok_private.h:34
Copyright (C) 2016-2023 Grok Image Compression Inc.
Definition ICacheable.h:20
void grk_read(const uint8_t *buffer, TYPE *value, uint32_t numBytes)
Definition BufferedStream.h:239
void grk_write(uint8_t *buffer, TYPE value, uint32_t numBytes)
Definition BufferedStream.h:217
Grok ref-counted object.
Definition grok.h:126