OpenJPH
Open-source implementation of JPEG2000 Part-15
ojph_file.cpp
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.cpp
34// Author: Aous Naman
35// Date: 28 August 2019
36//***************************************************************************/
37
38
43#include <cassert>
44#include <cstddef>
45
46#include "ojph_file.h"
47#include "ojph_message.h"
48
49namespace ojph {
50
52 //
53 //
54 //
55 //
56 //
58
60 void j2c_outfile::open(const char *filename)
61 {
62 assert(fh == 0);
63 fh = fopen(filename, "wb");
64 if (fh == NULL)
65 OJPH_ERROR(0x00060001, "failed to open %s for writing", filename);
66 }
67
69 size_t j2c_outfile::write(const void *ptr, size_t size)
70 {
71 assert(fh);
72 return fwrite(ptr, 1, size, fh);
73 }
74
77 {
78 assert(fh);
79 return ojph_ftell(fh);
80 }
81
84 {
85 assert(fh);
86 fflush(fh);
87 }
88
91 {
92 assert(fh);
93 fclose(fh);
94 fh = NULL;
95 }
96
97 //*************************************************************************/
98 // mem_outfile
99 //*************************************************************************/
100
103 {
104 is_open = false;
105 buf_size = 0;
106 buf = cur_ptr = NULL;
107 }
108
111 {
112 close();
113 }
114
116 void mem_outfile::open(size_t initial_size /* = 65536 */)
117 {
118 assert(this->is_open == false);
119 assert(this->buf_size == 0);
120 assert(this->buf == NULL);
121 assert(this->cur_ptr == NULL);
122
123 // do initial buffer allocation
124 this->is_open = true;
125 this->buf_size = initial_size;
126 if (initial_size)
127 this->buf = (ui8*)malloc(this->buf_size);
128 this->cur_ptr = this->buf;
129 }
130
133 if (buf)
134 free(buf);
135 is_open = false;
136 buf_size = 0;
137 buf = cur_ptr = NULL;
138 }
139
143 size_t mem_outfile::write(const void *ptr, size_t size)
144 {
145 assert(this->is_open);
146 assert(this->buf_size);
147 assert(this->buf);
148 assert(this->cur_ptr);
149
150 // expand buffer if needed to make sure it has room for this write
151 si64 used_size = tell(); //current used size
152 size_t new_used_size = (size_t)used_size + size; //needed size
153 if (new_used_size > this->buf_size) //only expand when there is need
154 {
155 size_t new_buf_size = this->buf_size;
156 while (new_used_size > new_buf_size)
157 new_buf_size += new_buf_size >> 1; //expand by ~1.5x
158
159 this->buf = (ui8*)realloc(this->buf, new_buf_size);
160 this->buf_size = new_buf_size;
161 this->cur_ptr = this->buf + used_size;
162 }
163
164 // copy bytes into buffer and adjust cur_ptr
165 memcpy(this->cur_ptr, ptr, size);
166 cur_ptr += size;
167
168 return size;
169 }
170
171
173 //
174 //
175 //
176 //
177 //
179
181 void j2c_infile::open(const char *filename)
182 {
183 assert(fh == NULL);
184 fh = fopen(filename, "rb");
185 if (fh == NULL)
186 OJPH_ERROR(0x00060002, "failed to open %s for reading", filename);
187 }
188
190 size_t j2c_infile::read(void *ptr, size_t size)
191 {
192 assert(fh);
193 return fread(ptr, 1, size, fh);
194 }
195
197 int j2c_infile::seek(si64 offset, enum infile_base::seek origin)
198 {
199 assert(fh);
200 return ojph_fseek(fh, offset, origin);
201 }
202
205 {
206 assert(fh);
207 return ojph_ftell(fh);
208 }
209
212 {
213 assert(fh);
214 fclose(fh);
215 fh = NULL;
216 }
217
218
220 //
221 //
222 //
223 //
224 //
226
228 void mem_infile::open(const ui8* data, size_t size)
229 {
230 assert(this->data == NULL);
231 cur_ptr = this->data = data;
232 this->size = size;
233 }
234
236 size_t mem_infile::read(void *ptr, size_t size)
237 {
238 std::ptrdiff_t bytes_left = (data + this->size) - cur_ptr;
239 if (bytes_left > 0)
240 {
241 size_t bytes_to_read = ojph_min(size, (size_t)bytes_left);
242 memcpy(ptr, cur_ptr, bytes_to_read);
243 cur_ptr += bytes_to_read;
244 return bytes_to_read;
245 }
246 else
247 return 0;
248 }
249
251 int mem_infile::seek(si64 offset, enum infile_base::seek origin)
252 {
253 int result = -1;
254 if (origin == OJPH_SEEK_SET)
255 {
256 if (offset >= 0 && (size_t)offset <= size)
257 {
258 cur_ptr = data + offset;
259 result = 0;
260 }
261 }
262 else if (origin == OJPH_SEEK_CUR)
263 {
264 std::ptrdiff_t bytes_off = cur_ptr - data; bytes_off += offset;
265 if (bytes_off >= 0 && (size_t)bytes_off <= size)
266 {
267 cur_ptr = data + bytes_off;
268 result = 0;
269 }
270 }
271 else if (origin == OJPH_SEEK_END)
272 {
273 if (offset <= 0 && (std::ptrdiff_t)size + offset >= 0)
274 {
275 cur_ptr = data + size + offset;
276 result = 0;
277 }
278 }
279 else
280 assert(0);
281
282 return result;
283 }
284
285
286}
void close() override
Definition: ojph_file.cpp:211
void open(const char *filename)
Definition: ojph_file.cpp:181
size_t read(void *ptr, size_t size) override
Definition: ojph_file.cpp:190
si64 tell() override
Definition: ojph_file.cpp:204
void flush() override
Definition: ojph_file.cpp:83
void close() override
Definition: ojph_file.cpp:90
si64 tell() override
Definition: ojph_file.cpp:76
void open(const char *filename)
Definition: ojph_file.cpp:60
size_t write(const void *ptr, size_t size) override
Definition: ojph_file.cpp:69
const ui8 * cur_ptr
Definition: ojph_file.h:242
size_t read(void *ptr, size_t size) override
Definition: ojph_file.cpp:236
void open(const ui8 *data, size_t size)
Definition: ojph_file.cpp:228
const ui8 * data
Definition: ojph_file.h:242
si64 tell() override
Definition: ojph_file.h:149
size_t write(const void *ptr, size_t size) override
Definition: ojph_file.cpp:143
void open(size_t initial_size=65536)
Definition: ojph_file.cpp:116
~mem_outfile() override
Definition: ojph_file.cpp:110
void close() override
Definition: ojph_file.cpp:132
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_min(a, b)
Definition: ojph_defs.h:76
#define OJPH_ERROR(t,...)
Definition: ojph_message.h:131