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
47namespace 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
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
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 //*************************************************************************/
127 {
128 public:
130 mem_outfile();
132 ~mem_outfile() override;
133
134 mem_outfile(mem_outfile const&) = delete;
136
141 mem_outfile(mem_outfile &&) noexcept;
146 mem_outfile& operator=(mem_outfile&&) noexcept;
147
158 void open(size_t initial_size = 65536, bool clear_mem = false);
159
169 size_t write(const void *ptr, size_t size) override;
170
177 si64 tell() override { return cur_ptr - buf; }
178
185 int seek(si64 offset, enum outfile_base::seek origin) override;
186
191 void close() override;
192
201 const ui8* get_data() { return buf; }
202
212 const ui8* get_data() const { return buf; }
213
218 void write_to_file(const char *file_name) const;
219
225 size_t get_used_size() const { return used_size; }
226
233 size_t get_buf_size() const { return buf_size; }
234
235 private:
236
240 void swap(mem_outfile& other) noexcept;
241
251 void expand_storage(size_t new_size, bool clear_all);
252
253 private:
256 size_t buf_size;
257 size_t used_size;
260
261 private:
262 static const size_t ALIGNED_ALLOC_MASK = 4096 - 1;
263 };
264
267 {
268 public:
269 enum seek : int {
270 OJPH_SEEK_SET = SEEK_SET,
271 OJPH_SEEK_CUR = SEEK_CUR,
272 OJPH_SEEK_END = SEEK_END
273 };
274
275 virtual ~infile_base() {}
276
277 //read reads size bytes, returns the number of bytes read
278 virtual size_t read(void *ptr, size_t size) = 0;
279 //seek returns 0 on success
280 virtual int seek(si64 offset, enum infile_base::seek origin) = 0;
281 virtual si64 tell() = 0;
282 virtual bool eof() = 0;
283 virtual void close() {}
284 };
285
288 {
289 public:
290 j2c_infile() { fh = 0; }
291 ~j2c_infile() override { if (fh) fclose(fh); }
292
293 void open(const char *filename);
294
295 //read reads size bytes, returns the number of bytes read
296 size_t read(void *ptr, size_t size) override;
297 //seek returns 0 on success
298 int seek(si64 offset, enum infile_base::seek origin) override;
299 si64 tell() override;
300 bool eof() override { return feof(fh) != 0; }
301 void close() override;
302
303 private:
304 FILE *fh;
305 };
306
309 {
310 public:
312 ~mem_infile() override { }
313
314 mem_infile(mem_infile const&) = delete;
315 mem_infile& operator=(mem_infile const&) = delete;
316
321 mem_infile(mem_infile &&) noexcept;
326 mem_infile& operator=(mem_infile&&) noexcept;
327
328 void open(const ui8* data, size_t size);
329
330 //read reads size bytes, returns the number of bytes read
331 size_t read(void *ptr, size_t size) override;
332 //seek returns 0 on success
333 int seek(si64 offset, enum infile_base::seek origin) override;
334 si64 tell() override { return cur_ptr - data; }
335 bool eof() override { return cur_ptr >= data + size; }
336 void close() override { data = cur_ptr = NULL; size = 0; }
337
338 private:
339 // swap the contents of two instances
340 void swap(mem_infile&) noexcept;
341
342 const ui8 *data, *cur_ptr;
343 size_t size;
344 };
345
346
347}
348
349#endif // !OJPH_FILE_H
virtual int seek(si64 offset, enum infile_base::seek origin)=0
virtual bool eof()=0
virtual void close()
Definition ojph_file.h:283
virtual ~infile_base()
Definition ojph_file.h:275
virtual si64 tell()=0
virtual size_t read(void *ptr, size_t size)=0
~j2c_infile() override
Definition ojph_file.h:291
bool eof() override
Definition ojph_file.h:300
~j2c_outfile() override
Definition ojph_file.h:101
const ui8 * cur_ptr
Definition ojph_file.h:342
bool eof() override
Definition ojph_file.h:335
size_t read(void *ptr, size_t size) override
void open(const ui8 *data, size_t size)
~mem_infile() override
Definition ojph_file.h:312
int seek(si64 offset, enum infile_base::seek origin) override
mem_infile & operator=(mem_infile const &)=delete
const ui8 * data
Definition ojph_file.h:342
void close() override
Definition ojph_file.h:336
mem_infile(mem_infile const &)=delete
si64 tell() override
Definition ojph_file.h:334
mem_outfile stores encoded j2k codestreams in memory
Definition ojph_file.h:127
size_t get_used_size() const
Call this function to get the used size of the memory file.
Definition ojph_file.h:225
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
static const size_t ALIGNED_ALLOC_MASK
Definition ojph_file.h:262
size_t write(const void *ptr, size_t size) override
Call this function to write data to the memory file.
void open(size_t initial_size=65536, bool clear_mem=false)
Call this function to open a memory file.
const ui8 * get_data()
Call this function to access memory file data.
Definition ojph_file.h:201
mem_outfile(mem_outfile const &)=delete
mem_outfile & operator=(mem_outfile const &)=delete
const ui8 * get_data() const
Call this function to access memory file data (for const objects).
Definition ojph_file.h:212
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
virtual void flush()
Definition ojph_file.h:92
virtual ~outfile_base()
Definition ojph_file.h:83
virtual void close()
Definition ojph_file.h:93
virtual si64 tell()
Definition ojph_file.h:86
virtual size_t write(const void *ptr, size_t size)=0
virtual int seek(si64 offset, enum outfile_base::seek origin)
Definition ojph_file.h:87
int ojph_fseek(FILE *stream, si64 offset, int origin)
Definition ojph_file.h:61
si64 ojph_ftell(FILE *stream)
Definition ojph_file.h:66
int64_t si64
Definition ojph_defs.h:57
uint8_t ui8
Definition ojph_defs.h:50
#define OJPH_EXPORT
Definition ojph_arch.h:123
#define ojph_unused(x)
Definition ojph_defs.h:78