BufferedStream.h
Go to the documentation of this file.
/*
* Copyright (C) 2016-2023 Grok Image Compression Inc.
*
* This source code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This source code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*
* This source code incorporates work covered by the BSD 2-clause license.
* Please see the LICENSE file in the root directory for details.
*
*/
#pragma once
#include "grk_config_private.h"
#include "IBitIO.h"
namespace grk
{
#define GROK_STREAM_STATUS_OUTPUT 0x1U
#define GROK_STREAM_STATUS_INPUT 0x2U
#define GROK_STREAM_STATUS_END 0x4U
#define GROK_STREAM_STATUS_ERROR 0x8U
struct BufferedStream
{
friend GrkObjectWrapperImpl<BufferedStream>;
BufferedStream(uint8_t* buffer, size_t buffer_size, bool l_is_input);
static BufferedStream* getImpl(grk_stream* stream);
grk_stream* getWrapper(void);
void setUserData(void* data, grk_stream_free_user_data_fn freeUserDataFun);
void* getUserData(void);
void setUserDataLength(uint64_t len);
uint32_t getStatus(void);
void setReadFunction(grk_stream_read_fn fn);
void setZeroCopyReadFunction(grk_stream_zero_copy_read_fn fn);
void setWriteFunction(grk_stream_write_fn fn);
void setSeekFunction(grk_stream_seek_fn fn);
size_t read(uint8_t* buffer, size_t p_size);
// low-level write methods (endian taken into account)
bool writeShort(uint16_t value);
bool write24(uint32_t value);
bool writeInt(uint32_t value);
bool write64(uint64_t value);
// low-level write methods (endian NOT taken into account)
bool writeByte(uint8_t value);
size_t writeBytes(const uint8_t* buffer, size_t p_size);
bool flush();
bool skip(int64_t p_size);
uint64_t tell(void);
uint64_t numBytesLeft(void);
bool seek(uint64_t offset);
bool hasSeek();
bool supportsZeroCopy();
uint8_t* getZeroCopyPtr();
void setFormat(GRK_CODEC_FORMAT format);
GRK_CODEC_FORMAT getFormat(void);
private:
~BufferedStream();
bool write_skip(int64_t p_size);
bool read_skip(int64_t p_size);
bool read_seek(uint64_t offset);
bool write_seek(uint64_t offset);
void writeIncrement(size_t p_size);
template<typename TYPE>
bool write(TYPE value, uint8_t numBytes);
void invalidate_buffer();
bool isMemStream();
grk_object obj;
void* user_data_;
grk_stream_free_user_data_fn free_user_data_fn_;
uint64_t user_data_length_;
grk_stream_read_fn read_fn_;
grk_stream_zero_copy_read_fn zero_copy_read_fn_;
grk_stream_write_fn write_fn_;
grk_stream_seek_fn seek_fn_;
uint32_t status_;
grk_buf8* buf_;
// number of bytes read in, or slated for write
size_t buffered_bytes_;
// number of seekable bytes in buffer. This will equal
// the number of bytes
// read in the last media read.
// We always have buffered_bytes_ <= read_bytes_seekable_
size_t read_bytes_seekable_;
// number of bytes read/written from the beginning of the stream
uint64_t stream_offset_;
GRK_CODEC_FORMAT format_;
};
template<typename TYPE>
void grk_write(uint8_t* buffer, TYPE value, uint32_t numBytes)
{
if(numBytes == 0)
return;
assert(numBytes <= sizeof(TYPE));
#if defined(GROK_BIG_ENDIAN)
const uint8_t* dataPtr = ((const uint8_t*)&value) + sizeof(TYPE) - numBytes;
memcpy(buffer, dataPtr, numBytes);
#else
const uint8_t* dataPtr = ((const uint8_t*)&value) + (size_t)(numBytes - 1);
for(uint32_t i = 0; i < numBytes; ++i)
*(buffer++) = *(dataPtr--);
#endif
}
template<typename TYPE>
void grk_write(uint8_t* buffer, TYPE value)
{
grk_write<TYPE>(buffer, value, sizeof(TYPE));
}
template<typename TYPE>
void grk_read(const uint8_t* buffer, TYPE* value, uint32_t numBytes)
{
assert(numBytes > 0 && numBytes <= sizeof(TYPE));
#if defined(GROK_BIG_ENDIAN)
auto dataPtr = ((uint8_t*)value);
*value = 0;
memcpy(dataPtr + sizeof(TYPE) - numBytes, buffer, numBytes);
#else
auto dataPtr = ((uint8_t*)value) + numBytes - 1;
*value = 0;
for(uint32_t i = 0; i < numBytes; ++i)
*(dataPtr--) = *(buffer++);
#endif
}
template<typename TYPE>
void grk_read(const uint8_t* buffer, TYPE* value)
{
grk_read<TYPE>(buffer, value, sizeof(TYPE));
}
} // namespace grk
IBitIO.h
grk::GrkObjectWrapperImpl
Definition GrkObjectWrapper.h:23
GRK_CODEC_FORMAT
enum _GRK_CODEC_FORMAT GRK_CODEC_FORMAT
Supported JPEG 2000 formats.
grk_stream_read_fn
size_t(* grk_stream_read_fn)(uint8_t *buffer, size_t numBytes, void *user_data)
Definition grok_private.h:29
grk_stream_seek_fn
bool(* grk_stream_seek_fn)(uint64_t numBytes, void *user_data)
Definition grok_private.h:38
grk_stream_free_user_data_fn
void(* grk_stream_free_user_data_fn)(void *user_data)
Definition grok_private.h:42
grk_stream_write_fn
size_t(* grk_stream_write_fn)(const uint8_t *buffer, size_t numBytes, void *user_data)
Definition grok_private.h:34
grk
Copyright (C) 2016-2023 Grok Image Compression Inc.
Definition ICacheable.h:20
grk::grk_read
void grk_read(const uint8_t *buffer, TYPE *value, uint32_t numBytes)
Definition BufferedStream.h:239
grk::grk_stream_zero_copy_read_fn
size_t(* grk_stream_zero_copy_read_fn)(uint8_t **buffer, size_t numBytes, void *user_data)
Definition MemStream.h:30
grk::grk_write
void grk_write(uint8_t *buffer, TYPE value, uint32_t numBytes)
Definition BufferedStream.h:217
_grk_object
Grok ref-counted object.
Definition grok.h:126
grk::BufferedStream
Definition BufferedStream.h:35
grk::BufferedStream::getZeroCopyPtr
uint8_t * getZeroCopyPtr()
Definition BufferedStream.cpp:357
grk::BufferedStream::status_
uint32_t status_
Stream status flags.
Definition BufferedStream.h:197
grk::BufferedStream::setUserData
void setUserData(void *data, grk_stream_free_user_data_fn freeUserDataFun)
Definition BufferedStream.cpp:55
grk::BufferedStream::setReadFunction
void setReadFunction(grk_stream_read_fn fn)
Definition BufferedStream.cpp:72
grk::BufferedStream::buf_
grk_buf8 * buf_
Definition BufferedStream.h:199
grk::BufferedStream::getFormat
GRK_CODEC_FORMAT getFormat(void)
Definition BufferedStream.cpp:51
grk::BufferedStream::write24
bool write24(uint32_t value)
Definition BufferedStream.cpp:219
grk::BufferedStream::getStatus
uint32_t getStatus(void)
Definition BufferedStream.cpp:68
grk::BufferedStream::getUserData
void * getUserData(void)
Definition BufferedStream.cpp:60
grk::BufferedStream::obj
grk_object obj
Definition BufferedStream.h:160
grk::BufferedStream::write_skip
bool write_skip(int64_t p_size)
Skip bytes in write stream.
Definition BufferedStream.cpp:372
grk::BufferedStream::flush
bool flush()
Flush stream to disk.
Definition BufferedStream.cpp:319
grk::BufferedStream::seek_fn_
grk_stream_seek_fn seek_fn_
Pointer to actual seek function (if available).
Definition BufferedStream.h:193
grk::BufferedStream::getWrapper
grk_stream * getWrapper(void)
Definition BufferedStream.cpp:488
grk::BufferedStream::writeShort
bool writeShort(uint16_t value)
Definition BufferedStream.cpp:215
grk::BufferedStream::read_bytes_seekable_
size_t read_bytes_seekable_
Definition BufferedStream.h:208
grk::BufferedStream::write64
bool write64(uint64_t value)
Definition BufferedStream.cpp:227
grk::BufferedStream::writeInt
bool writeInt(uint32_t value)
Definition BufferedStream.cpp:223
grk::BufferedStream::skip
bool skip(int64_t p_size)
Skip bytes in stream.
Definition BufferedStream.cpp:388
grk::BufferedStream::invalidate_buffer
void invalidate_buffer()
Definition BufferedStream.cpp:346
grk::BufferedStream::seek
bool seek(uint64_t offset)
Seek bytes from the stream (absolute)
Definition BufferedStream.cpp:466
grk::BufferedStream::writeByte
bool writeByte(uint8_t value)
Definition BufferedStream.cpp:211
grk::BufferedStream::isMemStream
bool isMemStream()
Definition BufferedStream.cpp:478
grk::BufferedStream::~BufferedStream
~BufferedStream()
Definition BufferedStream.cpp:41
grk::BufferedStream::read_seek
bool read_seek(uint64_t offset)
Absolute seek in read stream.
Definition BufferedStream.cpp:396
grk::BufferedStream::read_skip
bool read_skip(int64_t p_size)
Skip bytes in read stream.
Definition BufferedStream.cpp:362
grk::BufferedStream::stream_offset_
uint64_t stream_offset_
Definition BufferedStream.h:211
grk::BufferedStream::writeIncrement
void writeIncrement(size_t p_size)
Definition BufferedStream.cpp:308
grk::BufferedStream::zero_copy_read_fn_
grk_stream_zero_copy_read_fn zero_copy_read_fn_
Pointer to actual zero copy read function (nullptr at initialization).
Definition BufferedStream.h:185
grk::BufferedStream::setWriteFunction
void setWriteFunction(grk_stream_write_fn fn)
Definition BufferedStream.cpp:80
grk::BufferedStream::setUserDataLength
void setUserDataLength(uint64_t len)
Definition BufferedStream.cpp:64
grk::BufferedStream::supportsZeroCopy
bool supportsZeroCopy()
Definition BufferedStream.cpp:353
grk::BufferedStream::write_seek
bool write_seek(uint64_t offset)
Absolute seek in write stream.
Definition BufferedStream.cpp:442
grk::BufferedStream::setSeekFunction
void setSeekFunction(grk_stream_seek_fn fn)
Definition BufferedStream.cpp:84
grk::BufferedStream::buffered_bytes_
size_t buffered_bytes_
Definition BufferedStream.h:202
grk::BufferedStream::setFormat
void setFormat(GRK_CODEC_FORMAT format)
Definition BufferedStream.cpp:47
grk::BufferedStream::hasSeek
bool hasSeek()
Check if stream is seekable.
Definition BufferedStream.cpp:473
grk::BufferedStream::writeBytes
size_t writeBytes(const uint8_t *buffer, size_t p_size)
Write bytes to stream (no correction for endian!).
Definition BufferedStream.cpp:259
grk::BufferedStream::write_fn_
grk_stream_write_fn write_fn_
Pointer to actual write function (nullptr at initialization).
Definition BufferedStream.h:189
grk::BufferedStream::user_data_length_
uint64_t user_data_length_
User data length.
Definition BufferedStream.h:177
grk::BufferedStream::setZeroCopyReadFunction
void setZeroCopyReadFunction(grk_stream_zero_copy_read_fn fn)
Definition BufferedStream.cpp:76
grk::BufferedStream::read
size_t read(uint8_t *buffer, size_t p_size)
Reads some bytes from the stream.
Definition BufferedStream.cpp:89
grk::BufferedStream::write
bool write(TYPE value, uint8_t numBytes)
Definition BufferedStream.cpp:232
grk::BufferedStream::user_data_
void * user_data_
user data
Definition BufferedStream.h:165
grk::BufferedStream::read_fn_
grk_stream_read_fn read_fn_
Pointer to actual read function (nullptr at initialization).
Definition BufferedStream.h:181
grk::BufferedStream::numBytesLeft
uint64_t numBytesLeft(void)
Get the number of bytes left before end of stream.
Definition BufferedStream.cpp:383
grk::BufferedStream::format_
GRK_CODEC_FORMAT format_
Definition BufferedStream.h:213
grk::BufferedStream::free_user_data_fn_
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
grk::BufferedStream::tell
uint64_t tell(void)
Tells byte offset of stream (similar to ftell).
Definition BufferedStream.cpp:379
grk::BufferedStream::getImpl
static BufferedStream * getImpl(grk_stream *stream)
Definition BufferedStream.cpp:483
grk::grk_buf< uint8_t, AllocatorVanilla >