OpenJPH
Open-source implementation of JPEG2000 Part-15
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
78 virtual ~outfile_base() {}
79
80 virtual size_t write(const void *ptr, size_t size) = 0;
81 virtual si64 tell() { return 0; }
82 virtual void flush() {}
83 virtual void close() {}
84 };
85
88 {
89 public:
90 j2c_outfile() { fh = 0; }
91 ~j2c_outfile() override { if (fh) fclose(fh); }
92
93 void open(const char *filename);
94 size_t write(const void *ptr, size_t size) override;
95 si64 tell() override;
96 void flush() override;
97 void close() override;
98
99 private:
100 FILE *fh;
101 };
102
103 //*************************************************************************/
117 {
118 public:
120 mem_outfile();
122 ~mem_outfile() override;
123
132 void open(size_t initial_size = 65536);
133
142 size_t write(const void *ptr, size_t size) override;
143
149 si64 tell() override { return cur_ptr - buf; }
150
155 void close() override;
156
164 const ui8* get_data() { return buf; }
165
173 const ui8* get_data() const { return buf; }
174
175 private:
177 size_t buf_size;
180 };
181
184 {
185 public:
186 enum seek : int {
187 OJPH_SEEK_SET = SEEK_SET,
188 OJPH_SEEK_CUR = SEEK_CUR,
189 OJPH_SEEK_END = SEEK_END
190 };
191
192 virtual ~infile_base() {}
193
194 //read reads size bytes, returns the number of bytes read
195 virtual size_t read(void *ptr, size_t size) = 0;
196 //seek returns 0 on success
197 virtual int seek(si64 offset, enum infile_base::seek origin) = 0;
198 virtual si64 tell() = 0;
199 virtual bool eof() = 0;
200 virtual void close() {}
201 };
202
205 {
206 public:
207 j2c_infile() { fh = 0; }
208 ~j2c_infile() override { if (fh) fclose(fh); }
209
210 void open(const char *filename);
211
212 //read reads size bytes, returns the number of bytes read
213 size_t read(void *ptr, size_t size) override;
214 //seek returns 0 on success
215 int seek(si64 offset, enum infile_base::seek origin) override;
216 si64 tell() override;
217 bool eof() override { return feof(fh) != 0; }
218 void close() override;
219
220 private:
221 FILE *fh;
222 };
223
226 {
227 public:
228 mem_infile() { close(); }
229 ~mem_infile() override { }
230
231 void open(const ui8* data, size_t size);
232
233 //read reads size bytes, returns the number of bytes read
234 size_t read(void *ptr, size_t size) override;
235 //seek returns 0 on success
236 int seek(si64 offset, enum infile_base::seek origin) override;
237 si64 tell() override { return cur_ptr - data; }
238 bool eof() override { return cur_ptr >= data + size; }
239 void close() override { data = cur_ptr = NULL; size = 0; }
240
241 private:
242 const ui8 *data, *cur_ptr;
243 size_t size;
244 };
245
246
247}
248
249#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:200
virtual ~infile_base()
Definition: ojph_file.h:192
virtual si64 tell()=0
virtual size_t read(void *ptr, size_t size)=0
~j2c_infile() override
Definition: ojph_file.h:208
bool eof() override
Definition: ojph_file.h:217
~j2c_outfile() override
Definition: ojph_file.h:91
const ui8 * cur_ptr
Definition: ojph_file.h:242
bool eof() override
Definition: ojph_file.h:238
~mem_infile() override
Definition: ojph_file.h:229
void close() override
Definition: ojph_file.h:239
si64 tell() override
Definition: ojph_file.h:237
mem_outfile stores encoded j2k codestreams in memory
Definition: ojph_file.h:117
si64 tell() override
Definition: ojph_file.h:149
const ui8 * get_data()
Definition: ojph_file.h:164
const ui8 * get_data() const
Definition: ojph_file.h:173
virtual void flush()
Definition: ojph_file.h:82
virtual ~outfile_base()
Definition: ojph_file.h:78
virtual void close()
Definition: ojph_file.h:83
virtual si64 tell()
Definition: ojph_file.h:81
virtual size_t write(const void *ptr, size_t size)=0
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:85