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, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
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.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
37 @brief Implementation of a byte input-output process (CIO)
39 The functions in CIO.C have for goal to realize a byte input / output process.
42 /** @defgroup CIO CIO - byte input-output stream */
46 #include "opj_configure.h"
48 /** @name Exported functions (see also openjpeg.h) */
50 /* ----------------------------------------------------------------------- */
52 #if defined(OPJ_BIG_ENDIAN)
53 #if !defined(OPJ_LITTLE_ENDIAN)
54 #define opj_write_bytes opj_write_bytes_BE
55 #define opj_read_bytes opj_read_bytes_BE
56 #define opj_write_double opj_write_double_BE
57 #define opj_read_double opj_read_double_BE
58 #define opj_write_float opj_write_float_BE
59 #define opj_read_float opj_read_float_BE
61 #error "Either BIG_ENDIAN or LITTLE_ENDIAN must be #defined, but not both."
64 #if defined(OPJ_LITTLE_ENDIAN)
65 #define opj_write_bytes opj_write_bytes_LE
66 #define opj_read_bytes opj_read_bytes_LE
67 #define opj_write_double opj_write_double_LE
68 #define opj_read_double opj_read_double_LE
69 #define opj_write_float opj_write_float_LE
70 #define opj_read_float opj_read_float_LE
72 #error "Either BIG_ENDIAN or LITTLE_ENDIAN must be #defined, but not none."
80 opj_stream_e_output = 0x1,
81 opj_stream_e_input = 0x2,
82 opj_stream_e_end = 0x4,
83 opj_stream_e_error = 0x8
88 Byte input-output stream.
90 typedef struct opj_stream_private
93 * User data, be it files, ... The actual data depends on the type of the stream.
98 * Pointer to actual read function (NULL at the initialization of the cio.
100 opj_stream_read_fn m_read_fn;
103 * Pointer to actual write function (NULL at the initialization of the cio.
105 opj_stream_write_fn m_write_fn;
108 * Pointer to actual skip function (NULL at the initialization of the cio.
109 * There is no seek function to prevent from back and forth slow procedures.
111 opj_stream_skip_fn m_skip_fn;
114 * Pointer to actual seek function (if available).
116 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;
132 OPJ_SIZE_T (* m_opj_skip)(struct opj_stream_private * ,OPJ_SIZE_T , struct opj_event_mgr *);
134 bool (* m_opj_seek) (struct opj_stream_private * , OPJ_SIZE_T , struct opj_event_mgr *);
137 * number of bytes containing in the buffer.
139 OPJ_UINT32 m_bytes_in_buffer;
142 * The number of bytes read/written.
144 OPJ_SIZE_T m_byte_offset;
147 * The size of the buffer.
149 OPJ_UINT32 m_buffer_size;
152 * Flags to tell the status of the stream.
157 opj_stream_private_t;
161 * Write some bytes to the given data buffer, this function is used in Big Endian cpus.
162 * @param p_buffer pointer the data buffer to write data to.
163 * @param p_value the value to write
164 * @param p_nb_bytes the number of bytes to write
166 void opj_write_bytes_BE (OPJ_BYTE * p_buffer, OPJ_UINT32 p_value, OPJ_UINT32 p_nb_bytes);
169 * Reads some bytes from the given data buffer, this function is used in Big Endian cpus.
170 * @param p_buffer pointer the data buffer to read data from.
171 * @param p_value pointer to the value that will store the data.
172 * @param p_nb_bytes the nb bytes to read.
173 * @return the number of bytes read or -1 if an error occured.
175 void opj_read_bytes_BE(const OPJ_BYTE * p_buffer, OPJ_UINT32 * p_value, OPJ_UINT32 p_nb_bytes);
178 * Write some bytes to the given data buffer, this function is used in Little Endian cpus.
179 * @param p_buffer pointer the data buffer to write data to.
180 * @param p_value the value to write
181 * @param p_nb_bytes the number of bytes to write
182 * @return the number of bytes written or -1 if an error occured
184 void opj_write_bytes_LE (OPJ_BYTE * p_buffer, OPJ_UINT32 p_value, OPJ_UINT32 p_nb_bytes);
187 * Reads some bytes from the given data buffer, this function is used in Little Endian cpus.
188 * @param p_buffer pointer the data buffer to read data from.
189 * @param p_value pointer to the value that will store the data.
190 * @param p_nb_bytes the nb bytes to read.
191 * @return the number of bytes read or -1 if an error occured.
193 void opj_read_bytes_LE(const OPJ_BYTE * p_buffer, OPJ_UINT32 * p_value, OPJ_UINT32 p_nb_bytes);
197 * Write some bytes to the given data buffer, this function is used in Little Endian cpus.
198 * @param p_buffer pointer the data buffer to write data to.
199 * @param p_value the value to write
201 void opj_write_double_LE(OPJ_BYTE * p_buffer, OPJ_FLOAT64 p_value);
204 * Write some bytes to the given data buffer, this function is used in Big Endian cpus.
205 * @param p_buffer pointer the data buffer to write data to.
206 * @param p_value the value to write
208 void opj_write_double_BE(OPJ_BYTE * p_buffer, OPJ_FLOAT64 p_value);
211 * Reads some bytes from the given data buffer, this function is used in Little Endian cpus.
212 * @param p_buffer pointer the data buffer to read data from.
213 * @param p_value pointer to the value that will store the data.
215 void opj_read_double_LE(const OPJ_BYTE * p_buffer, OPJ_FLOAT64 * p_value);
218 * Reads some bytes from the given data buffer, this function is used in Big Endian cpus.
219 * @param p_buffer pointer the data buffer to read data from.
220 * @param p_value pointer to the value that will store the data.
222 void opj_read_double_BE(const OPJ_BYTE * p_buffer, OPJ_FLOAT64 * p_value);
225 * Reads some bytes from the given data buffer, this function is used in Little Endian cpus.
226 * @param p_buffer pointer the data buffer to read data from.
227 * @param p_value pointer to the value that will store the data.
229 void opj_read_float_LE(const OPJ_BYTE * p_buffer, OPJ_FLOAT32 * p_value);
232 * Reads some bytes from the given data buffer, this function is used in Big Endian cpus.
233 * @param p_buffer pointer the data buffer to read data from.
234 * @param p_value pointer to the value that will store the data.
236 void opj_read_float_BE(const OPJ_BYTE * p_buffer, OPJ_FLOAT32 * p_value);
239 * Write some bytes to the given data buffer, this function is used in Little Endian cpus.
240 * @param p_buffer pointer the data buffer to write data to.
241 * @param p_value the value to write
243 void opj_write_float_LE(OPJ_BYTE * p_buffer, OPJ_FLOAT32 p_value);
246 * Write some bytes to the given data buffer, this function is used in Big Endian cpus.
247 * @param p_buffer pointer the data buffer to write data to.
248 * @param p_value the value to write
250 void opj_write_float_BE(OPJ_BYTE * p_buffer, OPJ_FLOAT32 p_value);
253 * Reads some bytes from the stream.
254 * @param p_stream the stream to read data from.
255 * @param p_buffer pointer to the data buffer that will receive the data.
256 * @param p_size number of bytes to read.
257 * @param p_event_mgr the user event manager to be notified of special events.
258 * @return the number of bytes read, or -1 if an error occured or if the stream is at the end.
260 OPJ_UINT32 opj_stream_read_data (opj_stream_private_t * p_stream,OPJ_BYTE * p_buffer, OPJ_UINT32 p_size, struct opj_event_mgr * p_event_mgr);
263 * Writes some bytes to the stream.
264 * @param p_stream the stream to write data to.
265 * @param p_buffer pointer to the data buffer holds the data to be writtent.
266 * @param p_size number of bytes to write.
267 * @param p_event_mgr the user event manager to be notified of special events.
268 * @return the number of bytes writtent, or -1 if an error occured.
270 OPJ_UINT32 opj_stream_write_data (opj_stream_private_t * p_stream,const OPJ_BYTE * p_buffer, OPJ_UINT32 p_size, struct opj_event_mgr * p_event_mgr);
273 * Writes the content of the stream buffer to the stream.
274 * @param p_stream the stream to write data to.
275 * @param p_event_mgr the user event manager to be notified of special events.
276 * @return true if the data could be flushed, false else.
278 bool opj_stream_flush (opj_stream_private_t * p_stream, struct opj_event_mgr * p_event_mgr);
281 * Skips a number of bytes from the stream.
282 * @param p_stream the stream to skip data from.
283 * @param p_size the number of bytes to skip.
284 * @param p_event_mgr the user event manager to be notified of special events.
285 * @return the number of bytes skipped, or -1 if an error occured.
287 OPJ_SIZE_T opj_stream_skip (opj_stream_private_t * p_stream,OPJ_SIZE_T p_size, struct opj_event_mgr * p_event_mgr);
290 * Tells the byte offset on the stream (similar to ftell).
292 * @param p_stream the stream to get the information from.
294 * @return the current position o fthe stream.
296 OPJ_SIZE_T opj_stream_tell (const opj_stream_private_t * p_stream);
299 * Skips a number of bytes from the stream.
300 * @param p_stream the stream to skip data from.
301 * @param p_size the number of bytes to skip.
302 * @param p_event_mgr the user event manager to be notified of special events.
303 * @return the number of bytes skipped, or -1 if an error occured.
305 OPJ_SIZE_T opj_stream_write_skip (opj_stream_private_t * p_stream, OPJ_SIZE_T p_size, struct opj_event_mgr * p_event_mgr);
308 * Skips a number of bytes from the stream.
309 * @param p_stream the stream to skip data from.
310 * @param p_size the number of bytes to skip.
311 * @param p_event_mgr the user event manager to be notified of special events.
312 * @return the number of bytes skipped, or -1 if an error occured.
314 OPJ_SIZE_T opj_stream_read_skip (opj_stream_private_t * p_stream, OPJ_SIZE_T p_size, struct opj_event_mgr * p_event_mgr);
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 bool opj_stream_read_seek (opj_stream_private_t * p_stream, OPJ_SIZE_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 bool opj_stream_write_seek (opj_stream_private_t * p_stream, OPJ_SIZE_T p_size, struct opj_event_mgr * p_event_mgr);
335 * Seeks 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 true if the stream is seekable.
341 bool opj_stream_seek (opj_stream_private_t * p_stream, OPJ_SIZE_T p_size, struct opj_event_mgr * p_event_mgr);
344 * Tells if the given stream is seekable.
346 bool opj_stream_has_seek (const opj_stream_private_t * p_stream);
348 OPJ_UINT32 opj_stream_default_read (void * p_buffer, OPJ_UINT32 p_nb_bytes, void * p_user_data);
349 OPJ_UINT32 opj_stream_default_write (void * p_buffer, OPJ_UINT32 p_nb_bytes, void * p_user_data);
350 OPJ_SIZE_T opj_stream_default_skip (OPJ_SIZE_T p_nb_bytes, void * p_user_data);
351 bool opj_stream_default_seek (OPJ_SIZE_T p_nb_bytes, void * p_user_data);
353 /* ----------------------------------------------------------------------- */