OpenJPH Open-source implementation of JPEG2000 Part-15

Loading...
Searching...
No Matches
ojph_file.h
Go to the documentation of this file.
1//***************************************************************************/
2// This software is released under the 2-Clause BSD license, included
3// below.
4//
5// Copyright (c) 2019, Aous Naman
6// Copyright (c) 2019, Kakadu Software Pty Ltd, Australia
7// Copyright (c) 2019, The University of New South Wales, Australia
8//
9// Redistribution and use in source and binary forms, with or without
10// modification, are permitted provided that the following conditions are
11// met:
12//
13// 1. Redistributions of source code must retain the above copyright
14// notice, this list of conditions and the following disclaimer.
15//
16// 2. Redistributions in binary form must reproduce the above copyright
17// notice, this list of conditions and the following disclaimer in the
18// documentation and/or other materials provided with the distribution.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
26// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31//***************************************************************************/
32// This file is part of the OpenJPH software implementation.
33// File: ojph_file.h
34// Author: Aous Naman
35// Date: 28 August 2019
36//***************************************************************************/
37
38
39#ifndef OJPH_FILE_H
40#define OJPH_FILE_H
41
42#include <cstdlib>
43#include <cstdio>
44
45#include "ojph_arch.h"
46
47 namespace ojph {
48
50#ifdef OJPH_OS_WINDOWS
51 int inline ojph_fseek(FILE* stream, si64 offset, int origin)
52 {
53 return _fseeki64(stream, offset, origin);
54 }
55
56 si64 inline ojph_ftell(FILE* stream)
57 {
58 return _ftelli64(stream);
59 }
60#else
61 int inline ojph_fseek(FILE* stream, si64 offset, int origin)
62 {
63 return fseeko(stream, offset, origin);
64 }
65
66 si64 inline ojph_ftell(FILE* stream)
67 {
68 return ftello(stream);
69 }
70#endif
71
72
74 class OJPH_EXPORT outfile_base
75 {
76 public:
77 public:
78 enum seek : int {
79 OJPH_SEEK_SET = SEEK_SET,
80 OJPH_SEEK_CUR = SEEK_CUR,
81 OJPH_SEEK_END = SEEK_END
82 };
83 virtual ~outfile_base() {}
84
85 virtual size_t write(const void *ptr, size_t size) = 0;
86 virtual si64 tell() { return 0; }
87 virtual int seek(si64 offset, enum outfile_base::seek origin)
88 {
89 ojph_unused(offset); ojph_unused(origin);
90 return -1; /* always fail, to remind you to write an implementation */
91 }
92 virtual void flush() {}
93 virtual void close() {}
94 };
95
97 class OJPH_EXPORT j2c_outfile : public outfile_base
98 {
99 public:
100 j2c_outfile() { fh = 0; }
101 ~j2c_outfile() override { if (fh) fclose(fh); }
102
103 void open(const char *filename);
104 size_t write(const void *ptr, size_t size) override;
105 si64 tell() override;
106 void flush() override;
107 void close() override;
108
109 private:
110 FILE *fh;
111 };
112
113 //*************************************************************************/
126 class OJPH_EXPORT mem_outfile : public outfile_base
127 {
128 public:
130 mem_outfile();
132 ~mem_outfile() override;
133
144 void open(size_t initial_size = 65536, bool clear_mem = false);
145
155 size_t write(const void *ptr, size_t size) override;
156
163 si64 tell() override { return cur_ptr - buf; }
164
171 int seek(si64 offset, enum outfile_base::seek origin) override;
172
177 void close() override;
178
187 const ui8* get_data() { return buf; }
188
198 const ui8* get_data() const { return buf; }
199
204 void write_to_file(const char *file_name) const;
205
211 size_t get_used_size() const { return used_size; }
212
219 size_t get_buf_size() const { return buf_size; }
220
221 private:
231 void expand_storage(size_t new_size, bool clear_all);
232
233 private:
234 bool is_open;
235 bool clear_mem;
236 size_t buf_size;
237 size_t used_size;
238 ui8 *buf;
239 ui8 *cur_ptr;
240
241 private:
242 static const size_t ALIGNED_ALLOC_MASK = 4096 - 1;
243 };
244
246 class OJPH_EXPORT infile_base
247 {
248 public:
249 enum seek : int {
250 OJPH_SEEK_SET = SEEK_SET,
251 OJPH_SEEK_CUR = SEEK_CUR,
252 OJPH_SEEK_END = SEEK_END
253 };
254
255 virtual ~infile_base() {}
256
257 //read reads size bytes, returns the number of bytes read
258 virtual size_t read(void *ptr, size_t size) = 0;
259 //seek returns 0 on success
260 virtual int seek(si64 offset, enum infile_base::seek origin) = 0;
261 virtual si64 tell() = 0;
262 virtual bool eof() = 0;
263 virtual void close() {}
264 };
265
267 class OJPH_EXPORT j2c_infile : public infile_base
268 {
269 public:
270 j2c_infile() { fh = 0; }
271 ~j2c_infile() override { if (fh) fclose(fh); }
272
273 void open(const char *filename);
274
275 //read reads size bytes, returns the number of bytes read
276 size_t read(void *ptr, size_t size) override;
277 //seek returns 0 on success
278 int seek(si64 offset, enum infile_base::seek origin) override;
279 si64 tell() override;
280 bool eof() override { return feof(fh) != 0; }
281 void close() override;
282
283 private:
284 FILE *fh;
285 };
286
288 class OJPH_EXPORT mem_infile : public infile_base
289 {
290 public:
291 mem_infile() { close(); }
292 ~mem_infile() override { }
293
294 void open(const ui8* data, size_t size);
295
296 //read reads size bytes, returns the number of bytes read
297 size_t read(void *ptr, size_t size) override;
298 //seek returns 0 on success
299 int seek(si64 offset, enum infile_base::seek origin) override;
300 si64 tell() override { return cur_ptr - data; }
301 bool eof() override { return cur_ptr >= data + size; }
302 void close() override { data = cur_ptr = NULL; size = 0; }
303
304 private:
305 const ui8 *data, *cur_ptr;
306 size_t size;
307 };
308
309
310}
311
312#endif // !OJPH_FILE_H
ojph::infile_base
Definition ojph_file.h:247
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:249
ojph::infile_base::OJPH_SEEK_END
@ OJPH_SEEK_END
Definition ojph_file.h:252
ojph::infile_base::OJPH_SEEK_SET
@ OJPH_SEEK_SET
Definition ojph_file.h:250
ojph::infile_base::OJPH_SEEK_CUR
@ OJPH_SEEK_CUR
Definition ojph_file.h:251
ojph::infile_base::close
virtual void close()
Definition ojph_file.h:263
ojph::infile_base::~infile_base
virtual ~infile_base()
Definition ojph_file.h:255
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:270
ojph::j2c_infile::fh
FILE * fh
Definition ojph_file.h:284
ojph::j2c_infile::~j2c_infile
~j2c_infile() override
Definition ojph_file.h:271
ojph::j2c_infile::eof
bool eof() override
Definition ojph_file.h:280
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::mem_infile
mem_infile()
Definition ojph_file.h:291
ojph::mem_infile::cur_ptr
const ui8 * cur_ptr
Definition ojph_file.h:305
ojph::mem_infile::eof
bool eof() override
Definition ojph_file.h:301
ojph::mem_infile::size
size_t size
Definition ojph_file.h:306
ojph::mem_infile::~mem_infile
~mem_infile() override
Definition ojph_file.h:292
ojph::mem_infile::data
const ui8 * data
Definition ojph_file.h:305
ojph::mem_infile::close
void close() override
Definition ojph_file.h:302
ojph::mem_infile::tell
si64 tell() override
Definition ojph_file.h:300
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:211
ojph::mem_outfile::mem_outfile
mem_outfile()
Definition ojph_file.cpp:103
ojph::mem_outfile::used_size
size_t used_size
Definition ojph_file.h:237
ojph::mem_outfile::buf
ui8 * buf
Definition ojph_file.h:238
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:163
ojph::mem_outfile::ALIGNED_ALLOC_MASK
static const size_t ALIGNED_ALLOC_MASK
Definition ojph_file.h:242
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:167
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:121
ojph::mem_outfile::get_data
const ui8 * get_data()
Call this function to access memory file data.
Definition ojph_file.h:187
ojph::mem_outfile::is_open
bool is_open
Definition ojph_file.h:234
ojph::mem_outfile::buf_size
size_t buf_size
Definition ojph_file.h:236
ojph::mem_outfile::cur_ptr
ui8 * cur_ptr
Definition ojph_file.h:239
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:198
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:219
ojph::mem_outfile::clear_mem
bool clear_mem
Definition ojph_file.h:235
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:123
ojph_unused
#define ojph_unused(x)
Definition ojph_defs.h:78
ojph::size
Definition ojph_base.h:48