2 * Copyright (c) 2002-2007, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
3 * Copyright (c) 2002-2007, Professor Benoit Macq
4 * Copyright (c) 2001-2003, David Janssens
5 * Copyright (c) 2002-2003, Yannick Verschueren
6 * Copyright (c) 2003-2007, Francois-Olivier Devaux and Antonin Descampe
7 * Copyright (c) 2005, Herve Drolon, FreeImage Team
8 * Copyright (c) 2008;2011-2012, Centre National d'Etudes Spatiales (CNES), France
9 * Copyright (c) 2012, CS Systemes d'Information, France
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
38 @brief Implementation of a byte input-output process (CIO)
40 The functions in CIO.C have for goal to realize a byte input / output process.
43 /** @defgroup CIO CIO - byte input-output stream */
46 #include "opj_config.h"
48 /* ----------------------------------------------------------------------- */
50 #if defined(OPJ_BIG_ENDIAN)
51 #define opj_write_bytes opj_write_bytes_BE
52 #define opj_read_bytes opj_read_bytes_BE
53 #define opj_write_double opj_write_double_BE
54 #define opj_read_double opj_read_double_BE
55 #define opj_write_float opj_write_float_BE
56 #define opj_read_float opj_read_float_BE
58 #define opj_write_bytes opj_write_bytes_LE
59 #define opj_read_bytes opj_read_bytes_LE
60 #define opj_write_double opj_write_double_LE
61 #define opj_read_double opj_read_double_LE
62 #define opj_write_float opj_write_float_LE
63 #define opj_read_float opj_read_float_LE
70 opj_signed_sentinel = -1, /* do not use in code */
71 opj_stream_e_output = 0x1,
72 opj_stream_e_input = 0x2,
73 opj_stream_e_end = 0x4,
74 opj_stream_e_error = 0x8
79 Byte input-output stream.
81 typedef struct opj_stream_private
84 * User data, be it files, ... The actual data depends on the type of the stream.
91 OPJ_UINT64 m_user_data_length;
94 * Pointer to actual read function (NULL at the initialization of the cio.
96 opj_stream_read_fn m_read_fn;
99 * Pointer to actual write function (NULL at the initialization of the cio.
101 opj_stream_write_fn m_write_fn;
104 * Pointer to actual skip function (NULL at the initialization of the cio.
105 * There is no seek function to prevent from back and forth slow procedures.
107 opj_stream_skip_fn m_skip_fn;
110 * Pointer to actual seek function (if available).
112 opj_stream_seek_fn m_seek_fn;
115 * Actual data stored into the stream if readed from. Data is read by chunk of fixed size.
116 * you should never access this data directly.
118 OPJ_BYTE * m_stored_data;
121 * Pointer to the current read data.
123 OPJ_BYTE * m_current_data;
128 OPJ_OFF_T (* m_opj_skip)(struct opj_stream_private * ,OPJ_OFF_T , struct opj_event_mgr *);
133 OPJ_BOOL (* m_opj_seek) (struct opj_stream_private * , OPJ_OFF_T , struct opj_event_mgr *);
136 * number of bytes containing in the buffer.
138 OPJ_SIZE_T m_bytes_in_buffer;
141 * The number of bytes read/written from the beginning of the stream
143 OPJ_OFF_T m_byte_offset;
146 * The size of the buffer.
148 OPJ_SIZE_T m_buffer_size;
151 * Flags to tell the status of the stream.
153 opj_stream_flag m_status;
156 opj_stream_private_t;
158 /** @name Exported functions (see also openjpeg.h) */
160 /* ----------------------------------------------------------------------- */
162 * Write some bytes to the given data buffer, this function is used in Big Endian cpus.
163 * @param p_buffer pointer the data buffer to write data to.
164 * @param p_value the value to write
165 * @param p_nb_bytes the number of bytes to write
167 void opj_write_bytes_BE (OPJ_BYTE * p_buffer, OPJ_UINT32 p_value, OPJ_UINT32 p_nb_bytes);
170 * Reads some bytes from the given data buffer, this function is used in Big Endian cpus.
171 * @param p_buffer pointer the data buffer to read data from.
172 * @param p_value pointer to the value that will store the data.
173 * @param p_nb_bytes the nb bytes to read.
174 * @return the number of bytes read or -1 if an error occured.
176 void opj_read_bytes_BE(const OPJ_BYTE * p_buffer, OPJ_UINT32 * p_value, OPJ_UINT32 p_nb_bytes);
179 * Write some bytes to the given data buffer, this function is used in Little Endian cpus.
180 * @param p_buffer pointer the data buffer to write data to.
181 * @param p_value the value to write
182 * @param p_nb_bytes the number of bytes to write
183 * @return the number of bytes written or -1 if an error occured
185 void opj_write_bytes_LE (OPJ_BYTE * p_buffer, OPJ_UINT32 p_value, OPJ_UINT32 p_nb_bytes);
188 * Reads some bytes from the given data buffer, this function is used in Little Endian cpus.
189 * @param p_buffer pointer the data buffer to read data from.
190 * @param p_value pointer to the value that will store the data.
191 * @param p_nb_bytes the nb bytes to read.
192 * @return the number of bytes read or -1 if an error occured.
194 void opj_read_bytes_LE(const OPJ_BYTE * p_buffer, OPJ_UINT32 * p_value, OPJ_UINT32 p_nb_bytes);
198 * Write some bytes to the given data buffer, this function is used in Little Endian cpus.
199 * @param p_buffer pointer the data buffer to write data to.
200 * @param p_value the value to write
202 void opj_write_double_LE(OPJ_BYTE * p_buffer, OPJ_FLOAT64 p_value);
205 * Write some bytes to the given data buffer, this function is used in Big Endian cpus.
206 * @param p_buffer pointer the data buffer to write data to.
207 * @param p_value the value to write
209 void opj_write_double_BE(OPJ_BYTE * p_buffer, OPJ_FLOAT64 p_value);
212 * Reads some bytes from the given data buffer, this function is used in Little Endian cpus.
213 * @param p_buffer pointer the data buffer to read data from.
214 * @param p_value pointer to the value that will store the data.
216 void opj_read_double_LE(const OPJ_BYTE * p_buffer, OPJ_FLOAT64 * p_value);
219 * Reads some bytes from the given data buffer, this function is used in Big Endian cpus.
220 * @param p_buffer pointer the data buffer to read data from.
221 * @param p_value pointer to the value that will store the data.
223 void opj_read_double_BE(const OPJ_BYTE * p_buffer, OPJ_FLOAT64 * p_value);
226 * Reads some bytes from the given data buffer, this function is used in Little Endian cpus.
227 * @param p_buffer pointer the data buffer to read data from.
228 * @param p_value pointer to the value that will store the data.
230 void opj_read_float_LE(const OPJ_BYTE * p_buffer, OPJ_FLOAT32 * p_value);
233 * Reads some bytes from the given data buffer, this function is used in Big Endian cpus.
234 * @param p_buffer pointer the data buffer to read data from.
235 * @param p_value pointer to the value that will store the data.
237 void opj_read_float_BE(const OPJ_BYTE * p_buffer, OPJ_FLOAT32 * p_value);
240 * Write some bytes to the given data buffer, this function is used in Little Endian cpus.
241 * @param p_buffer pointer the data buffer to write data to.
242 * @param p_value the value to write
244 void opj_write_float_LE(OPJ_BYTE * p_buffer, OPJ_FLOAT32 p_value);
247 * Write some bytes to the given data buffer, this function is used in Big Endian cpus.
248 * @param p_buffer pointer the data buffer to write data to.
249 * @param p_value the value to write
251 void opj_write_float_BE(OPJ_BYTE * p_buffer, OPJ_FLOAT32 p_value);
254 * Reads some bytes from the stream.
255 * @param p_stream the stream to read data from.
256 * @param p_buffer pointer to the data buffer that will receive the data.
257 * @param p_size number of bytes to read.
258 * @param p_event_mgr the user event manager to be notified of special events.
259 * @return the number of bytes read, or -1 if an error occured or if the stream is at the end.
261 OPJ_SIZE_T opj_stream_read_data (opj_stream_private_t * p_stream,OPJ_BYTE * p_buffer, OPJ_SIZE_T p_size, struct opj_event_mgr * p_event_mgr);
264 * Writes some bytes to the stream.
265 * @param p_stream the stream to write data to.
266 * @param p_buffer pointer to the data buffer holds the data to be writtent.
267 * @param p_size number of bytes to write.
268 * @param p_event_mgr the user event manager to be notified of special events.
269 * @return the number of bytes writtent, or -1 if an error occured.
271 OPJ_SIZE_T opj_stream_write_data (opj_stream_private_t * p_stream,const OPJ_BYTE * p_buffer, OPJ_SIZE_T p_size, struct opj_event_mgr * p_event_mgr);
274 * Writes the content of the stream buffer to the stream.
275 * @param p_stream the stream to write data to.
276 * @param p_event_mgr the user event manager to be notified of special events.
277 * @return true if the data could be flushed, false else.
279 OPJ_BOOL opj_stream_flush (opj_stream_private_t * p_stream, struct opj_event_mgr * p_event_mgr);
282 * Skips a number of bytes from the stream.
283 * @param p_stream the stream to skip data from.
284 * @param p_size the number of bytes to skip.
285 * @param p_event_mgr the user event manager to be notified of special events.
286 * @return the number of bytes skipped, or -1 if an error occured.
288 OPJ_OFF_T opj_stream_skip (opj_stream_private_t * p_stream,OPJ_OFF_T p_size, struct opj_event_mgr * p_event_mgr);
291 * Tells the byte offset on the stream (similar to ftell).
293 * @param p_stream the stream to get the information from.
295 * @return the current position o fthe stream.
297 OPJ_OFF_T opj_stream_tell (const opj_stream_private_t * p_stream);
301 * Get the number of bytes left before the end of the stream (similar to cio_numbytesleft).
303 * @param p_stream the stream to get the information from.
305 * @return Number of bytes left before the end of the stream.
307 OPJ_OFF_T opj_stream_get_number_byte_left (const opj_stream_private_t * p_stream);
310 * Skips a number of bytes from the stream.
311 * @param p_stream the stream to skip data from.
312 * @param p_size the number of bytes to skip.
313 * @param p_event_mgr the user event manager to be notified of special events.
314 * @return the number of bytes skipped, or -1 if an error occured.
316 OPJ_OFF_T opj_stream_write_skip (opj_stream_private_t * p_stream, OPJ_OFF_T p_size, struct opj_event_mgr * p_event_mgr);
319 * Skips a number of bytes from the stream.
320 * @param p_stream the stream to skip data from.
321 * @param p_size the number of bytes to skip.
322 * @param p_event_mgr the user event manager to be notified of special events.
323 * @return the number of bytes skipped, or -1 if an error occured.
325 OPJ_OFF_T opj_stream_read_skip (opj_stream_private_t * p_stream, OPJ_OFF_T p_size, struct opj_event_mgr * p_event_mgr);
328 * Skips a number of bytes from the stream.
329 * @param p_stream the stream to skip data from.
330 * @param p_size the number of bytes to skip.
331 * @param p_event_mgr the user event manager to be notified of special events.
332 * @return OPJ_TRUE if success, or OPJ_FALSE if an error occured.
334 OPJ_BOOL opj_stream_read_seek (opj_stream_private_t * p_stream, OPJ_OFF_T p_size, struct opj_event_mgr * p_event_mgr);
337 * Skips a number of bytes from the stream.
338 * @param p_stream the stream to skip data from.
339 * @param p_size the number of bytes to skip.
340 * @param p_event_mgr the user event manager to be notified of special events.
341 * @return the number of bytes skipped, or -1 if an error occured.
343 OPJ_BOOL opj_stream_write_seek (opj_stream_private_t * p_stream, OPJ_OFF_T p_size, struct opj_event_mgr * p_event_mgr);
346 * Seeks a number of bytes from the stream.
347 * @param p_stream the stream to skip data from.
348 * @param p_size the number of bytes to skip.
349 * @param p_event_mgr the user event manager to be notified of special events.
350 * @return true if the stream is seekable.
352 OPJ_BOOL opj_stream_seek (opj_stream_private_t * p_stream, OPJ_OFF_T p_size, struct opj_event_mgr * p_event_mgr);
355 * Tells if the given stream is seekable.
357 OPJ_BOOL opj_stream_has_seek (const opj_stream_private_t * p_stream);
362 OPJ_SIZE_T opj_stream_default_read (void * p_buffer, OPJ_SIZE_T p_nb_bytes, void * p_user_data);
367 OPJ_SIZE_T opj_stream_default_write (void * p_buffer, OPJ_SIZE_T p_nb_bytes, void * p_user_data);
372 OPJ_OFF_T opj_stream_default_skip (OPJ_OFF_T p_nb_bytes, void * p_user_data);
377 OPJ_BOOL opj_stream_default_seek (OPJ_OFF_T p_nb_bytes, void * p_user_data);
379 /* ----------------------------------------------------------------------- */