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.
89 * Pointer to function to free m_user_data (NULL at initialization)
90 * when destroying the stream. If pointer is NULL the function is not
91 * called and the m_user_data is not freed (even if non-NULL).
93 opj_stream_free_user_data_fn m_free_user_data_fn;
98 OPJ_UINT64 m_user_data_length;
101 * Pointer to actual read function (NULL at the initialization of the cio.
103 opj_stream_read_fn m_read_fn;
106 * Pointer to actual write function (NULL at the initialization of the cio.
108 opj_stream_write_fn m_write_fn;
111 * Pointer to actual skip function (NULL at the initialization of the cio.
112 * There is no seek function to prevent from back and forth slow procedures.
114 opj_stream_skip_fn m_skip_fn;
117 * Pointer to actual seek function (if available).
119 opj_stream_seek_fn m_seek_fn;
122 * Actual data stored into the stream if readed from. Data is read by chunk of fixed size.
123 * you should never access this data directly.
125 OPJ_BYTE * m_stored_data;
128 * Pointer to the current read data.
130 OPJ_BYTE * m_current_data;
135 OPJ_OFF_T (* m_opj_skip)(struct opj_stream_private * ,OPJ_OFF_T , struct opj_event_mgr *);
140 OPJ_BOOL (* m_opj_seek) (struct opj_stream_private * , OPJ_OFF_T , struct opj_event_mgr *);
143 * number of bytes containing in the buffer.
145 OPJ_SIZE_T m_bytes_in_buffer;
148 * The number of bytes read/written from the beginning of the stream
150 OPJ_OFF_T m_byte_offset;
153 * The size of the buffer.
155 OPJ_SIZE_T m_buffer_size;
158 * Flags to tell the status of the stream.
160 opj_stream_flag m_status;
163 opj_stream_private_t;
165 /** @name Exported functions (see also openjpeg.h) */
167 /* ----------------------------------------------------------------------- */
169 * Write some bytes to the given data buffer, this function is used in Big Endian cpus.
170 * @param p_buffer pointer the data buffer to write data to.
171 * @param p_value the value to write
172 * @param p_nb_bytes the number of bytes to write
174 void opj_write_bytes_BE (OPJ_BYTE * p_buffer, OPJ_UINT32 p_value, OPJ_UINT32 p_nb_bytes);
177 * Reads some bytes from the given data buffer, this function is used in Big Endian cpus.
178 * @param p_buffer pointer the data buffer to read data from.
179 * @param p_value pointer to the value that will store the data.
180 * @param p_nb_bytes the nb bytes to read.
181 * @return the number of bytes read or -1 if an error occured.
183 void opj_read_bytes_BE(const OPJ_BYTE * p_buffer, OPJ_UINT32 * p_value, OPJ_UINT32 p_nb_bytes);
186 * Write some bytes to the given data buffer, this function is used in Little Endian cpus.
187 * @param p_buffer pointer the data buffer to write data to.
188 * @param p_value the value to write
189 * @param p_nb_bytes the number of bytes to write
190 * @return the number of bytes written or -1 if an error occured
192 void opj_write_bytes_LE (OPJ_BYTE * p_buffer, OPJ_UINT32 p_value, OPJ_UINT32 p_nb_bytes);
195 * Reads some bytes from the given data buffer, this function is used in Little Endian cpus.
196 * @param p_buffer pointer the data buffer to read data from.
197 * @param p_value pointer to the value that will store the data.
198 * @param p_nb_bytes the nb bytes to read.
199 * @return the number of bytes read or -1 if an error occured.
201 void opj_read_bytes_LE(const OPJ_BYTE * p_buffer, OPJ_UINT32 * p_value, OPJ_UINT32 p_nb_bytes);
205 * Write some bytes to the given data buffer, this function is used in Little 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_LE(OPJ_BYTE * p_buffer, OPJ_FLOAT64 p_value);
212 * Write some bytes to the given data buffer, this function is used in Big Endian cpus.
213 * @param p_buffer pointer the data buffer to write data to.
214 * @param p_value the value to write
216 void opj_write_double_BE(OPJ_BYTE * p_buffer, OPJ_FLOAT64 p_value);
219 * Reads some bytes from the given data buffer, this function is used in Little 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_LE(const OPJ_BYTE * p_buffer, OPJ_FLOAT64 * p_value);
226 * Reads some bytes from the given data buffer, this function is used in Big 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_double_BE(const OPJ_BYTE * p_buffer, OPJ_FLOAT64 * p_value);
233 * Reads some bytes from the given data buffer, this function is used in Little 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_LE(const OPJ_BYTE * p_buffer, OPJ_FLOAT32 * p_value);
240 * Reads some bytes from the given data buffer, this function is used in Big Endian cpus.
241 * @param p_buffer pointer the data buffer to read data from.
242 * @param p_value pointer to the value that will store the data.
244 void opj_read_float_BE(const OPJ_BYTE * p_buffer, OPJ_FLOAT32 * p_value);
247 * Write some bytes to the given data buffer, this function is used in Little 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_LE(OPJ_BYTE * p_buffer, OPJ_FLOAT32 p_value);
254 * Write some bytes to the given data buffer, this function is used in Big Endian cpus.
255 * @param p_buffer pointer the data buffer to write data to.
256 * @param p_value the value to write
258 void opj_write_float_BE(OPJ_BYTE * p_buffer, OPJ_FLOAT32 p_value);
261 * Reads some bytes from the stream.
262 * @param p_stream the stream to read data from.
263 * @param p_buffer pointer to the data buffer that will receive the data.
264 * @param p_size number of bytes to read.
265 * @param p_event_mgr the user event manager to be notified of special events.
266 * @return the number of bytes read, or -1 if an error occured or if the stream is at the end.
268 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);
271 * Writes some bytes to the stream.
272 * @param p_stream the stream to write data to.
273 * @param p_buffer pointer to the data buffer holds the data to be writtent.
274 * @param p_size number of bytes to write.
275 * @param p_event_mgr the user event manager to be notified of special events.
276 * @return the number of bytes writtent, or -1 if an error occured.
278 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);
281 * Writes the content of the stream buffer to the stream.
282 * @param p_stream the stream to write data to.
283 * @param p_event_mgr the user event manager to be notified of special events.
284 * @return true if the data could be flushed, false else.
286 OPJ_BOOL opj_stream_flush (opj_stream_private_t * p_stream, struct opj_event_mgr * p_event_mgr);
289 * Skips a number of bytes from the stream.
290 * @param p_stream the stream to skip data from.
291 * @param p_size the number of bytes to skip.
292 * @param p_event_mgr the user event manager to be notified of special events.
293 * @return the number of bytes skipped, or -1 if an error occured.
295 OPJ_OFF_T opj_stream_skip (opj_stream_private_t * p_stream,OPJ_OFF_T p_size, struct opj_event_mgr * p_event_mgr);
298 * Tells the byte offset on the stream (similar to ftell).
300 * @param p_stream the stream to get the information from.
302 * @return the current position o fthe stream.
304 OPJ_OFF_T opj_stream_tell (const opj_stream_private_t * p_stream);
308 * Get the number of bytes left before the end of the stream (similar to cio_numbytesleft).
310 * @param p_stream the stream to get the information from.
312 * @return Number of bytes left before the end of the stream.
314 OPJ_OFF_T opj_stream_get_number_byte_left (const opj_stream_private_t * p_stream);
317 * Skips a number of bytes from the stream.
318 * @param p_stream the stream to skip data from.
319 * @param p_size the number of bytes to skip.
320 * @param p_event_mgr the user event manager to be notified of special events.
321 * @return the number of bytes skipped, or -1 if an error occured.
323 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);
326 * Skips a number of bytes from the stream.
327 * @param p_stream the stream to skip data from.
328 * @param p_size the number of bytes to skip.
329 * @param p_event_mgr the user event manager to be notified of special events.
330 * @return the number of bytes skipped, or -1 if an error occured.
332 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);
335 * Skips a number of bytes from the stream.
336 * @param p_stream the stream to skip data from.
337 * @param p_size the number of bytes to skip.
338 * @param p_event_mgr the user event manager to be notified of special events.
339 * @return OPJ_TRUE if success, or OPJ_FALSE if an error occured.
341 OPJ_BOOL opj_stream_read_seek (opj_stream_private_t * p_stream, OPJ_OFF_T p_size, struct opj_event_mgr * p_event_mgr);
344 * Skips a number of bytes from the stream.
345 * @param p_stream the stream to skip data from.
346 * @param p_size the number of bytes to skip.
347 * @param p_event_mgr the user event manager to be notified of special events.
348 * @return the number of bytes skipped, or -1 if an error occured.
350 OPJ_BOOL opj_stream_write_seek (opj_stream_private_t * p_stream, OPJ_OFF_T p_size, struct opj_event_mgr * p_event_mgr);
353 * Seeks a number of bytes from the stream.
354 * @param p_stream the stream to skip data from.
355 * @param p_size the number of bytes to skip.
356 * @param p_event_mgr the user event manager to be notified of special events.
357 * @return true if the stream is seekable.
359 OPJ_BOOL opj_stream_seek (opj_stream_private_t * p_stream, OPJ_OFF_T p_size, struct opj_event_mgr * p_event_mgr);
362 * Tells if the given stream is seekable.
364 OPJ_BOOL opj_stream_has_seek (const opj_stream_private_t * p_stream);
369 OPJ_SIZE_T opj_stream_default_read (void * p_buffer, OPJ_SIZE_T p_nb_bytes, void * p_user_data);
374 OPJ_SIZE_T opj_stream_default_write (void * p_buffer, OPJ_SIZE_T p_nb_bytes, void * p_user_data);
379 OPJ_OFF_T opj_stream_default_skip (OPJ_OFF_T p_nb_bytes, void * p_user_data);
384 OPJ_BOOL opj_stream_default_seek (OPJ_OFF_T p_nb_bytes, void * p_user_data);
386 /* ----------------------------------------------------------------------- */