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 
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 fseek(stream, offset, origin);
64  }
65 
66  si64 inline ojph_ftell(FILE* stream)
67  {
68  return ftell(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 
87  class j2c_outfile : public outfile_base
88  {
89  public:
91  j2c_outfile() { fh = 0; }
93  ~j2c_outfile() { if (fh) fclose(fh); }
94 
96  void open(const char *filename);
98  virtual size_t write(const void *ptr, size_t size);
100  virtual si64 tell();
102  virtual void flush();
104  virtual void close();
105 
106  private:
107  FILE *fh;
108  };
109 
110  //*************************************************************************/
123  class mem_outfile : public outfile_base
124  {
125  public:
128  mem_outfile();
131  ~mem_outfile();
132 
142  void open(size_t initial_size = 65536);
143 
153  virtual size_t write(const void *ptr, size_t size);
154 
161  virtual si64 tell() { return cur_ptr - buf; }
162 
168  virtual void close();
169 
178  const ui8* get_data() { return buf; }
179 
188  const ui8* get_data() const { return buf; }
189 
190  private:
191  bool is_open;
192  size_t buf_size;
195  };
196 
199  {
200  public:
201  enum seek : int {
202  OJPH_SEEK_SET = SEEK_SET,
203  OJPH_SEEK_CUR = SEEK_CUR,
204  OJPH_SEEK_END = SEEK_END
205  };
206 
207  virtual ~infile_base() {}
208 
209  //read reads size bytes, returns the number of bytes read
210  virtual size_t read(void *ptr, size_t size) = 0;
211  //seek returns 0 on success
212  virtual int seek(si64 offset, enum infile_base::seek origin) = 0;
213  virtual si64 tell() = 0;
214  virtual bool eof() = 0;
215  virtual void close() {}
216  };
217 
219  class j2c_infile : public infile_base
220  {
221  public:
223  j2c_infile() { fh = 0; }
225  ~j2c_infile() { if (fh) fclose(fh); }
226 
228  void open(const char *filename);
229 
230  //read reads size bytes, returns the number of bytes read
232  virtual size_t read(void *ptr, size_t size);
233  //seek returns 0 on success
235  virtual int seek(si64 offset, enum infile_base::seek origin);
237  virtual si64 tell();
239  virtual bool eof() { return feof(fh) != 0; }
241  virtual void close();
242 
243  private:
244  FILE *fh;
245 
246  };
247 
249  class mem_infile : public infile_base
250  {
251  public:
253  mem_infile() { close(); }
256 
258  void open(const ui8* data, size_t size);
259 
260  //read reads size bytes, returns the number of bytes read
262  virtual size_t read(void *ptr, size_t size);
263  //seek returns 0 on success
265  virtual int seek(si64 offset, enum infile_base::seek origin);
267  virtual si64 tell() { return cur_ptr - data; }
269  virtual bool eof() { return cur_ptr >= data + size; }
271  virtual void close() { data = cur_ptr = NULL; size = 0; }
272 
273  private:
274  const ui8 *data, *cur_ptr;
275  size_t size;
276 
277  };
278 
279 
280 }
281 
282 #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:215
virtual ~infile_base()
Definition: ojph_file.h:207
virtual si64 tell()=0
virtual size_t read(void *ptr, size_t size)=0
OJPH_EXPORT ~j2c_infile()
Definition: ojph_file.h:225
OJPH_EXPORT void open(const char *filename)
Definition: ojph_file.cpp:181
OJPH_EXPORT j2c_infile()
Definition: ojph_file.h:223
virtual OJPH_EXPORT si64 tell()
Definition: ojph_file.cpp:204
virtual OJPH_EXPORT size_t read(void *ptr, size_t size)
Definition: ojph_file.cpp:190
virtual OJPH_EXPORT void close()
Definition: ojph_file.cpp:211
virtual OJPH_EXPORT bool eof()
Definition: ojph_file.h:239
OJPH_EXPORT ~j2c_outfile()
Definition: ojph_file.h:93
virtual OJPH_EXPORT void close()
Definition: ojph_file.cpp:90
OJPH_EXPORT j2c_outfile()
Definition: ojph_file.h:91
virtual OJPH_EXPORT void flush()
Definition: ojph_file.cpp:83
virtual OJPH_EXPORT si64 tell()
Definition: ojph_file.cpp:76
virtual OJPH_EXPORT size_t write(const void *ptr, size_t size)
Definition: ojph_file.cpp:69
OJPH_EXPORT void open(const char *filename)
Definition: ojph_file.cpp:60
OJPH_EXPORT mem_infile()
Definition: ojph_file.h:253
const ui8 * cur_ptr
Definition: ojph_file.h:274
virtual OJPH_EXPORT size_t read(void *ptr, size_t size)
Definition: ojph_file.cpp:236
OJPH_EXPORT void open(const ui8 *data, size_t size)
Definition: ojph_file.cpp:228
virtual OJPH_EXPORT void close()
Definition: ojph_file.h:271
OJPH_EXPORT ~mem_infile()
Definition: ojph_file.h:255
const ui8 * data
Definition: ojph_file.h:274
virtual OJPH_EXPORT bool eof()
Definition: ojph_file.h:269
virtual OJPH_EXPORT si64 tell()
Definition: ojph_file.h:267
mem_outfile stores encoded j2k codestreams in memory
Definition: ojph_file.h:124
OJPH_EXPORT mem_outfile()
Definition: ojph_file.cpp:102
OJPH_EXPORT void open(size_t initial_size=65536)
Definition: ojph_file.cpp:116
OJPH_EXPORT ~mem_outfile()
Definition: ojph_file.cpp:110
virtual OJPH_EXPORT void close()
Definition: ojph_file.cpp:132
OJPH_EXPORT const ui8 * get_data()
Definition: ojph_file.h:178
OJPH_EXPORT const ui8 * get_data() const
Definition: ojph_file.h:188
virtual OJPH_EXPORT si64 tell()
Definition: ojph_file.h:161
virtual OJPH_EXPORT size_t write(const void *ptr, size_t size)
Definition: ojph_file.cpp:143
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:202