OpenJPH: ojph::mem_outfile Class Reference

OpenJPH Open-source implementation of JPEG2000 Part-15

Loading...
Searching...
No Matches
ojph::mem_outfile Class Reference

mem_outfile stores encoded j2k codestreams in memory More...

`#include <ojph_file.h>`

Inheritance diagram for ojph::mem_outfile:

## Public Member Functions
| mem_outfile ()
| ~mem_outfile () override
| mem_outfile (mem_outfile const &)=delete
mem_outfile & | operator= (mem_outfile const &)=delete
| mem_outfile (mem_outfile &&) noexcept
mem_outfile & | operator= (mem_outfile &&) noexcept
void | open (size_t initial_size=65536, bool clear_mem=false)
| Call this function to open a memory file.
size_t | write (const void *ptr, size_t size) override
| Call this function to write data to the memory file.
si64 | tell () override
| Call this function to know the file size (i.e., number of bytes used to store the file).
int | seek (si64 offset, enum outfile_base::seek origin) override
| Call this function to change write pointer location; the function can expand file storage.
void | close () override
const ui8 * | get_data ()
| Call this function to access memory file data.
const ui8 * | get_data () const
| Call this function to access memory file data (for const objects).
void | write_to_file (const char *file_name) const
| Call this function to write the memory file data to a file.
size_t | get_used_size () const
| Call this function to get the used size of the memory file.
size_t | get_buf_size () const
| Call this function to get the total buffer size of the memory file including unused space (this is the allocated memory).
Public Member Functions inherited from ojph::outfile_base
virtual | ~outfile_base ()
virtual void | flush ()

## Private Member Functions
void | swap (mem_outfile &other) noexcept
| A utility function to swap the contents of two instances.
void | expand_storage (size_t new_size, bool clear_all)
| This function expands storage by x1.5 needed space.

## Private Attributes
bool | is_open
bool | clear_mem
size_t | buf_size
size_t | used_size
ui8 * | buf
ui8 * | cur_ptr

## Static Private Attributes
static const size_t | ALIGNED_ALLOC_MASK = 4096 - 1

## Additional Inherited Members
Public Types inherited from ojph::outfile_base
enum | seek : int { OJPH_SEEK_SET = SEEK_SET , OJPH_SEEK_CUR = SEEK_CUR , OJPH_SEEK_END = SEEK_END }

## Detailed Description

mem_outfile stores encoded j2k codestreams in memory

This code was first developed by Chris Hafey https://github.com/chafey I took the code and integrated with OpenJPH, with some modifications.

This class serves as a memory-based file storage. For example, generated j2k codestream is stored in memory instead of a conventional file. The memory buffer associated with this class grows with the addition of new data.

memory data can be accessed using get_data()

Definition at line 126 of file ojph_file.h.

## Constructor & Destructor Documentation

## ◆ mem_outfile() [1/3]

ojph::mem_outfile::mem_outfile | ( |  | ) |

A constructor

Definition at line 108 of file ojph_file.cpp.

References buf, buf_size, clear_mem, cur_ptr, is_open, and used_size.

Referenced by mem_outfile(), mem_outfile(), operator=(), operator=(), and swap().

## ◆ ~mem_outfile()

ojph::mem_outfile::~mem_outfile | ( |  | ) |
| override

A destructor

Definition at line 126 of file ojph_file.cpp.

References buf, buf_size, clear_mem, cur_ptr, is_open, ojph::ojph_aligned_free(), and used_size.

## ◆ mem_outfile() [2/3]

ojph::mem_outfile::mem_outfile | ( | mem_outfile const & |  | ) |
| delete

References mem_outfile().

## ◆ mem_outfile() [3/3]

ojph::mem_outfile::mem_outfile | ( | mem_outfile && | rhs | ) |
| noexcept

Move construction leaves the moved-from value in default constructed state and transfers ownership of the internal state to the moved-to instance.

Definition at line 136 of file ojph_file.cpp.

References mem_outfile(), and swap().

## Member Function Documentation

## ◆ close()

void ojph::mem_outfile::close | ( |  | ) |
| overridevirtual

Call this function to close the file and deallocate memory

The object can be used again after calling close

Reimplemented from ojph::outfile_base.

Definition at line 166 of file ojph_file.cpp.

References buf, cur_ptr, and is_open.

## ◆ expand_storage()

void ojph::mem_outfile::expand_storage | ( | size_t | new_size,
|  | bool | clear_all )
| private

This function expands storage by x1.5 needed space.

It sets cur_ptr correctly, and clears the extended area of the buffer. It optionally clear the whole buffer

Parameters

  new_size | New size of the buffer
  clear_all | Set to true to clear whole buffer, not just expansion

Definition at line 233 of file ojph_file.cpp.

References ALIGNED_ALLOC_MASK, buf, buf_size, clear_mem, cur_ptr, ojph::ojph_aligned_free(), ojph::ojph_aligned_malloc(), OJPH_ERROR, tell(), and used_size.

Referenced by open(), seek(), and write().

## ◆ get_buf_size()

size_t ojph::mem_outfile::get_buf_size | ( |  | ) | const
| inline

Call this function to get the total buffer size of the memory file including unused space (this is the allocated memory).

Returns
  the full size of the memory file in bytes.

Definition at line 233 of file ojph_file.h.

References buf_size.

## ◆ get_data() [1/2]

const ui8 * ojph::mem_outfile::get_data | ( |  | ) |
| inline

Call this function to access memory file data.

It is not recommended to store the returned value because buffer storage address can change between write calls.

Returns
  a constant pointer to the data.

Definition at line 201 of file ojph_file.h.

References buf.

## ◆ get_data() [2/2]

const ui8 * ojph::mem_outfile::get_data | ( |  | ) | const
| inline

Call this function to access memory file data (for const objects).

This is similar to the above function, except that it can be used with constant objects.

Returns
  a constant pointer to the data.

Definition at line 212 of file ojph_file.h.

References buf.

## ◆ get_used_size()

size_t ojph::mem_outfile::get_used_size | ( |  | ) | const
| inline

Call this function to get the used size of the memory file.

Returns
  the used size of the memory file in bytes.

Definition at line 225 of file ojph_file.h.

References used_size.

## ◆ open()

void ojph::mem_outfile::open | ( | size_t | initial_size = 65536,
|  | bool | clear_mem = false )

Call this function to open a memory file.

This function creates a memory buffer to be used for storing the generated j2k codestream.

Parameters

  initial_size | is the initial memory buffer size. The default value is 2^16.
  clear_mem | if set to true, all allocated memory is reset to 0

Definition at line 152 of file ojph_file.cpp.

References buf, clear_mem, cur_ptr, expand_storage(), is_open, and used_size.

Referenced by LLVMFuzzerTestOneInput(), and operator=().

## ◆ operator=() [1/2]

mem_outfile & ojph::mem_outfile::operator= | ( | mem_outfile && | rhs | ) |
| noexcept

move assignment with the same ownership transfer semantics as move construction.

Definition at line 142 of file ojph_file.cpp.

References mem_outfile(), and swap().

## ◆ operator=() [2/2]

mem_outfile & ojph::mem_outfile::operator= | ( | mem_outfile const & |  | ) |
| delete

References clear_mem, mem_outfile(), open(), and write().

## ◆ seek()

int ojph::mem_outfile::seek | ( | si64 | offset,
|  | enum outfile_base::seek | origin )
| overridevirtual

Call this function to change write pointer location; the function can expand file storage.

Returns
  0 on success, non-zero otherwise (not used).

The seek function expands the buffer whenever offset goes beyond the buffer end

Reimplemented from ojph::outfile_base.

Definition at line 175 of file ojph_file.cpp.

References buf, cur_ptr, expand_storage(), ojph::outfile_base::OJPH_SEEK_CUR, ojph::outfile_base::OJPH_SEEK_END, ojph::outfile_base::OJPH_SEEK_SET, tell(), and used_size.

## ◆ swap()

void ojph::mem_outfile::swap | ( | mem_outfile & | other | ) |
| privatenoexcept

A utility function to swap the contents of two instances.

Definition at line 116 of file ojph_file.cpp.

References buf, buf_size, clear_mem, cur_ptr, is_open, mem_outfile(), and used_size.

Referenced by mem_outfile(), and operator=().

## ◆ tell()

si64 ojph::mem_outfile::tell | ( |  | ) |
| inlineoverridevirtual

Call this function to know the file size (i.e., number of bytes used to store the file).

Returns
  the file size.

Reimplemented from ojph::outfile_base.

Definition at line 177 of file ojph_file.h.

References buf, cur_ptr, and tell().

Referenced by expand_storage(), seek(), tell(), and write().

## ◆ write()

size_t ojph::mem_outfile::write | ( | const void * | ptr,
|  | size_t | new_size )
| overridevirtual

Call this function to write data to the memory file.

This function adds new data to the memory file. The memory buffer of the file grows as needed.

Parameters

  ptr | is a pointer to new data.
  size | the number of bytes in the new data.

Whenever the need arises, the buffer is expanded by a factor approx 1.5x

Implements ojph::outfile_base.

Definition at line 200 of file ojph_file.cpp.

References buf, buf_size, cur_ptr, expand_storage(), is_open, ojph_max, tell(), and used_size.

Referenced by operator=().

## ◆ write_to_file()

void ojph::mem_outfile::write_to_file | ( | const char * | file_name | ) | const

Call this function to write the memory file data to a file.

Definition at line 220 of file ojph_file.cpp.

References buf, is_open, OJPH_ERROR, and used_size.

## Member Data Documentation

## ◆ ALIGNED_ALLOC_MASK

const size_t ojph::mem_outfile::ALIGNED_ALLOC_MASK = 4096 - 1
| staticprivate

Definition at line 262 of file ojph_file.h.

Referenced by expand_storage().

## ◆ buf

ui8* ojph::mem_outfile::buf
| private

Definition at line 258 of file ojph_file.h.

Referenced by close(), expand_storage(), get_data(), get_data(), mem_outfile(), open(), seek(), swap(), tell(), write(), write_to_file(), and ~mem_outfile().

## ◆ buf_size

size_t ojph::mem_outfile::buf_size
| private

Definition at line 256 of file ojph_file.h.

Referenced by expand_storage(), get_buf_size(), mem_outfile(), swap(), write(), and ~mem_outfile().

## ◆ clear_mem

bool ojph::mem_outfile::clear_mem
| private

Definition at line 255 of file ojph_file.h.

Referenced by expand_storage(), mem_outfile(), open(), operator=(), swap(), and ~mem_outfile().

## ◆ cur_ptr

ui8* ojph::mem_outfile::cur_ptr
| private

Definition at line 259 of file ojph_file.h.

Referenced by close(), expand_storage(), mem_outfile(), open(), seek(), swap(), tell(), write(), and ~mem_outfile().

## ◆ is_open

bool ojph::mem_outfile::is_open
| private

Definition at line 254 of file ojph_file.h.

Referenced by close(), mem_outfile(), open(), swap(), write(), write_to_file(), and ~mem_outfile().

## ◆ used_size

size_t ojph::mem_outfile::used_size
| private

Definition at line 257 of file ojph_file.h.

Referenced by expand_storage(), get_used_size(), mem_outfile(), open(), seek(), swap(), write(), write_to_file(), and ~mem_outfile().

The documentation for this class was generated from the following files:

- /build/openjph/src/OpenJPH/src/core/openjph/ojph_file.h
- /build/openjph/src/OpenJPH/src/core/others/ojph_file.cpp