Loading...
Searching...
No Matches
ojph_file.h
Go to the documentation of this file.
//***************************************************************************/
// This software is released under the 2-Clause BSD license, included
// below.
//
// Copyright (c) 2019, Aous Naman
// Copyright (c) 2019, Kakadu Software Pty Ltd, Australia
// Copyright (c) 2019, The University of New South Wales, Australia
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//***************************************************************************/
// This file is part of the OpenJPH software implementation.
// File: ojph_file.h
// Author: Aous Naman
// Date: 28 August 2019
//***************************************************************************/
#ifndef OJPH_FILE_H
#define OJPH_FILE_H
#include <cstdlib>
#include <cstdio>
#include "ojph_arch.h"
namespace ojph {
#ifdef OJPH_OS_WINDOWS
int inline ojph_fseek(FILE* stream, si64 offset, int origin)
{
return _fseeki64(stream, offset, origin);
}
si64 inline ojph_ftell(FILE* stream)
{
return _ftelli64(stream);
}
#else
int inline ojph_fseek(FILE* stream, si64 offset, int origin)
{
return fseeko(stream, offset, origin);
}
si64 inline ojph_ftell(FILE* stream)
{
return ftello(stream);
}
#endif
class OJPH_EXPORT outfile_base
{
public:
public:
enum seek : int {
OJPH_SEEK_SET = SEEK_SET,
OJPH_SEEK_CUR = SEEK_CUR,
OJPH_SEEK_END = SEEK_END
};
virtual ~outfile_base() {}
virtual size_t write(const void *ptr, size_t size) = 0;
virtual si64 tell() { return 0; }
virtual int seek(si64 offset, enum outfile_base::seek origin)
{
ojph_unused(offset); ojph_unused(origin);
return -1; /* always fail, to remind you to write an implementation */
}
virtual void flush() {}
virtual void close() {}
};
class OJPH_EXPORT j2c_outfile : public outfile_base
{
public:
j2c_outfile() { fh = 0; }
~j2c_outfile() override { if (fh) fclose(fh); }
void open(const char *filename);
size_t write(const void *ptr, size_t size) override;
si64 tell() override;
void flush() override;
void close() override;
private:
FILE *fh;
};
//*************************************************************************/
class OJPH_EXPORT mem_outfile : public outfile_base
{
public:
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);
size_t write(const void *ptr, size_t size) override;
si64 tell() override { return cur_ptr - buf; }
int seek(si64 offset, enum outfile_base::seek origin) override;
void close() override;
const ui8* get_data() { return buf; }
const ui8* get_data() const { return buf; }
void write_to_file(const char *file_name) const;
size_t get_used_size() const { return used_size; }
size_t get_buf_size() const { return buf_size; }
private:
void swap(mem_outfile& other) noexcept;
void expand_storage(size_t new_size, bool clear_all);
private:
bool is_open;
bool clear_mem;
size_t buf_size;
size_t used_size;
ui8 *buf;
ui8 *cur_ptr;
private:
static const size_t ALIGNED_ALLOC_MASK = 4096 - 1;
};
class OJPH_EXPORT infile_base
{
public:
enum seek : int {
OJPH_SEEK_SET = SEEK_SET,
OJPH_SEEK_CUR = SEEK_CUR,
OJPH_SEEK_END = SEEK_END
};
virtual ~infile_base() {}
//read reads size bytes, returns the number of bytes read
virtual size_t read(void *ptr, size_t size) = 0;
//seek returns 0 on success
virtual int seek(si64 offset, enum infile_base::seek origin) = 0;
virtual si64 tell() = 0;
virtual bool eof() = 0;
virtual void close() {}
};
class OJPH_EXPORT j2c_infile : public infile_base
{
public:
j2c_infile() { fh = 0; }
~j2c_infile() override { if (fh) fclose(fh); }
void open(const char *filename);
//read reads size bytes, returns the number of bytes read
size_t read(void *ptr, size_t size) override;
//seek returns 0 on success
int seek(si64 offset, enum infile_base::seek origin) override;
si64 tell() override;
bool eof() override { return feof(fh) != 0; }
void close() override;
private:
FILE *fh;
};
class OJPH_EXPORT mem_infile : public infile_base
{
public:
mem_infile() { close(); }
~mem_infile() override { }
mem_infile(mem_infile const&) = delete;
mem_infile& operator=(mem_infile const&) = delete;
mem_infile(mem_infile &&) noexcept;
mem_infile& operator=(mem_infile&&) noexcept;
void open(const ui8* data, size_t size);
//read reads size bytes, returns the number of bytes read
size_t read(void *ptr, size_t size) override;
//seek returns 0 on success
int seek(si64 offset, enum infile_base::seek origin) override;
si64 tell() override { return cur_ptr - data; }
bool eof() override { return cur_ptr >= data + size; }
void close() override { data = cur_ptr = NULL; size = 0; }
private:
// swap the contents of two instances
void swap(mem_infile&) noexcept;
const ui8 *data, *cur_ptr;
size_t size;
};
}
#endif // !OJPH_FILE_H
ojph::infile_base
Definition ojph_file.h:267
ojph::infile_base::seek
virtual int seek(si64 offset, enum infile_base::seek origin)=0
ojph::infile_base::eof
virtual bool eof()=0
ojph::infile_base::seek
seek
Definition ojph_file.h:269
ojph::infile_base::OJPH_SEEK_END
@ OJPH_SEEK_END
Definition ojph_file.h:272
ojph::infile_base::OJPH_SEEK_SET
@ OJPH_SEEK_SET
Definition ojph_file.h:270
ojph::infile_base::OJPH_SEEK_CUR
@ OJPH_SEEK_CUR
Definition ojph_file.h:271
ojph::infile_base::close
virtual void close()
Definition ojph_file.h:283
ojph::infile_base::~infile_base
virtual ~infile_base()
Definition ojph_file.h:275
ojph::infile_base::tell
virtual si64 tell()=0
ojph::infile_base::read
virtual size_t read(void *ptr, size_t size)=0
ojph::j2c_infile::j2c_infile
j2c_infile()
Definition ojph_file.h:290
ojph::j2c_infile::fh
FILE * fh
Definition ojph_file.h:304
ojph::j2c_infile::~j2c_infile
~j2c_infile() override
Definition ojph_file.h:291
ojph::j2c_infile::eof
bool eof() override
Definition ojph_file.h:300
ojph::j2c_outfile::fh
FILE * fh
Definition ojph_file.h:110
ojph::j2c_outfile::j2c_outfile
j2c_outfile()
Definition ojph_file.h:100
ojph::j2c_outfile::~j2c_outfile
~j2c_outfile() override
Definition ojph_file.h:101
ojph::mem_infile
Definition ojph_file.h:309
ojph::mem_infile::mem_infile
mem_infile()
Definition ojph_file.h:311
ojph::mem_infile::cur_ptr
const ui8 * cur_ptr
Definition ojph_file.h:342
ojph::mem_infile::eof
bool eof() override
Definition ojph_file.h:335
ojph::mem_infile::read
size_t read(void *ptr, size_t size) override
Definition ojph_file.cpp:345
ojph::mem_infile::size
size_t size
Definition ojph_file.h:343
ojph::mem_infile::open
void open(const ui8 *data, size_t size)
Definition ojph_file.cpp:337
ojph::mem_infile::~mem_infile
~mem_infile() override
Definition ojph_file.h:312
ojph::mem_infile::seek
int seek(si64 offset, enum infile_base::seek origin) override
Definition ojph_file.cpp:360
ojph::mem_infile::operator=
mem_infile & operator=(mem_infile const &)=delete
ojph::mem_infile::data
const ui8 * data
Definition ojph_file.h:342
ojph::mem_infile::close
void close() override
Definition ojph_file.h:336
ojph::mem_infile::mem_infile
mem_infile(mem_infile const &)=delete
ojph::mem_infile::tell
si64 tell() override
Definition ojph_file.h:334
ojph::mem_outfile
mem_outfile stores encoded j2k codestreams in memory
Definition ojph_file.h:127
ojph::mem_outfile::get_used_size
size_t get_used_size() const
Call this function to get the used size of the memory file.
Definition ojph_file.h:225
ojph::mem_outfile::mem_outfile
mem_outfile()
Definition ojph_file.cpp:108
ojph::mem_outfile::used_size
size_t used_size
Definition ojph_file.h:257
ojph::mem_outfile::buf
ui8 * buf
Definition ojph_file.h:258
ojph::mem_outfile::tell
si64 tell() override
Call this function to know the file size (i.e., number of bytes used to store the file).
Definition ojph_file.h:177
ojph::mem_outfile::ALIGNED_ALLOC_MASK
static const size_t ALIGNED_ALLOC_MASK
Definition ojph_file.h:262
ojph::mem_outfile::write
size_t write(const void *ptr, size_t size) override
Call this function to write data to the memory file.
Definition ojph_file.cpp:200
ojph::mem_outfile::open
void open(size_t initial_size=65536, bool clear_mem=false)
Call this function to open a memory file.
Definition ojph_file.cpp:152
ojph::mem_outfile::get_data
const ui8 * get_data()
Call this function to access memory file data.
Definition ojph_file.h:201
ojph::mem_outfile::is_open
bool is_open
Definition ojph_file.h:254
ojph::mem_outfile::mem_outfile
mem_outfile(mem_outfile const &)=delete
ojph::mem_outfile::operator=
mem_outfile & operator=(mem_outfile const &)=delete
ojph::mem_outfile::buf_size
size_t buf_size
Definition ojph_file.h:256
ojph::mem_outfile::cur_ptr
ui8 * cur_ptr
Definition ojph_file.h:259
ojph::mem_outfile::get_data
const ui8 * get_data() const
Call this function to access memory file data (for const objects).
Definition ojph_file.h:212
ojph::mem_outfile::get_buf_size
size_t get_buf_size() const
Call this function to get the total buffer size of the memory file including unused space (this is th...
Definition ojph_file.h:233
ojph::mem_outfile::clear_mem
bool clear_mem
Definition ojph_file.h:255
ojph::outfile_base
Definition ojph_file.h:75
ojph::outfile_base::seek
seek
Definition ojph_file.h:78
ojph::outfile_base::OJPH_SEEK_CUR
@ OJPH_SEEK_CUR
Definition ojph_file.h:80
ojph::outfile_base::OJPH_SEEK_END
@ OJPH_SEEK_END
Definition ojph_file.h:81
ojph::outfile_base::OJPH_SEEK_SET
@ OJPH_SEEK_SET
Definition ojph_file.h:79
ojph::outfile_base::flush
virtual void flush()
Definition ojph_file.h:92
ojph::outfile_base::~outfile_base
virtual ~outfile_base()
Definition ojph_file.h:83
ojph::outfile_base::close
virtual void close()
Definition ojph_file.h:93
ojph::outfile_base::tell
virtual si64 tell()
Definition ojph_file.h:86
ojph::outfile_base::write
virtual size_t write(const void *ptr, size_t size)=0
ojph::outfile_base::seek
virtual int seek(si64 offset, enum outfile_base::seek origin)
Definition ojph_file.h:87
ojph
Definition ojph_img_io.h:52
ojph::ojph_fseek
int ojph_fseek(FILE *stream, si64 offset, int origin)
Definition ojph_file.h:61
ojph::ojph_ftell
si64 ojph_ftell(FILE *stream)
Definition ojph_file.h:66
ojph::si64
int64_t si64
Definition ojph_defs.h:57
ojph::ui8
uint8_t ui8
Definition ojph_defs.h:50
ojph_arch.h
OJPH_EXPORT
#define OJPH_EXPORT
Definition ojph_arch.h:135
ojph_unused
#define ojph_unused(x)
Definition ojph_defs.h:78
ojph::size
Definition ojph_base.h:48