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
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
36 @brief Implementation of a byte input-output process (CIO)
38 The functions in CIO.C have for goal to realize a byte input / output process.
41 /** @defgroup CIO CIO - byte input-output stream */
44 #include "opj_config.h"
46 /** @name Exported functions (see also openjpeg.h) */
48 /* ----------------------------------------------------------------------- */
50 Number of bytes left before the end of the stream
52 @return Returns the number of bytes before the end of the stream
54 int cio_numbytesleft(opj_cio_t *cio);
56 Get pointer to the current position in the stream
58 @return Returns a pointer to the current position
60 unsigned char *cio_getbp(opj_cio_t *cio);
64 @param v Value to write
65 @param n Number of bytes to write
66 @return Returns the number of bytes written or 0 if an error occured
68 unsigned int cio_write(opj_cio_t *cio, unsigned long long int v, int n);
72 @param n Number of bytes to read
73 @return Returns the value of the n bytes read
75 unsigned int cio_read(opj_cio_t *cio, int n);
79 @param n Number of bytes to skip
81 void cio_skip(opj_cio_t *cio, int n);
82 /* ----------------------------------------------------------------------- */
89 /* ----------------------------------------------------------------------- */
91 #if defined(OPJ_BIG_ENDIAN)
92 #define opj_write_bytes opj_write_bytes_BE
93 #define opj_read_bytes opj_read_bytes_BE
94 #define opj_write_double opj_write_double_BE
95 #define opj_read_double opj_read_double_BE
96 #define opj_write_float opj_write_float_BE
97 #define opj_read_float opj_read_float_BE
99 #define opj_write_bytes opj_write_bytes_LE
100 #define opj_read_bytes opj_read_bytes_LE
101 #define opj_write_double opj_write_double_LE
102 #define opj_read_double opj_read_double_LE
103 #define opj_write_float opj_write_float_LE
104 #define opj_read_float opj_read_float_LE
111 opj_stream_e_output = 0x1,
112 opj_stream_e_input = 0x2,
113 opj_stream_e_end = 0x4,
114 opj_stream_e_error = 0x8
119 Byte input-output stream.
121 typedef struct opj_stream_private
124 * User data, be it files, ... The actual data depends on the type of the stream.
129 * Pointer to actual read function (NULL at the initialization of the cio.
131 opj_stream_read_fn m_read_fn;
134 * Pointer to actual write function (NULL at the initialization of the cio.
136 opj_stream_write_fn m_write_fn;
139 * Pointer to actual skip function (NULL at the initialization of the cio.
140 * There is no seek function to prevent from back and forth slow procedures.
142 opj_stream_skip_fn m_skip_fn;
145 * Pointer to actual seek function (if available).
147 opj_stream_seek_fn m_seek_fn;
153 * Actual data stored into the stream if readed from. Data is read by chunk of fixed size.
154 * you should never access this data directly.
156 OPJ_BYTE * m_stored_data;
159 * Pointer to the current read data.
161 OPJ_BYTE * m_current_data;
163 OPJ_SIZE_T (* m_opj_skip)(struct opj_stream_private * ,OPJ_SIZE_T , struct opj_event_mgr *);
165 opj_bool (* m_opj_seek) (struct opj_stream_private * , OPJ_SIZE_T , struct opj_event_mgr *);
168 * number of bytes containing in the buffer.
170 OPJ_UINT32 m_bytes_in_buffer;
173 * The number of bytes read/written.
175 OPJ_SIZE_T m_byte_offset;
178 * The size of the buffer.
180 OPJ_UINT32 m_buffer_size;
183 * Flags to tell the status of the stream.
188 opj_stream_private_t;
192 * Write some bytes to the given data buffer, this function is used in Big Endian cpus.
193 * @param p_buffer pointer the data buffer to write data to.
194 * @param p_value the value to write
195 * @param p_nb_bytes the number of bytes to write
197 void opj_write_bytes_BE (OPJ_BYTE * p_buffer, OPJ_UINT32 p_value, OPJ_UINT32 p_nb_bytes);
200 * Reads some bytes from the given data buffer, this function is used in Big Endian cpus.
201 * @param p_buffer pointer the data buffer to read data from.
202 * @param p_value pointer to the value that will store the data.
203 * @param p_nb_bytes the nb bytes to read.
204 * @return the number of bytes read or -1 if an error occured.
206 void opj_read_bytes_BE(const OPJ_BYTE * p_buffer, OPJ_UINT32 * p_value, OPJ_UINT32 p_nb_bytes);
209 * Write some bytes to the given data buffer, this function is used in Little Endian cpus.
210 * @param p_buffer pointer the data buffer to write data to.
211 * @param p_value the value to write
212 * @param p_nb_bytes the number of bytes to write
213 * @return the number of bytes written or -1 if an error occured
215 void opj_write_bytes_LE (OPJ_BYTE * p_buffer, OPJ_UINT32 p_value, OPJ_UINT32 p_nb_bytes);
218 * Reads some bytes from the given data buffer, this function is used in Little 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.
221 * @param p_nb_bytes the nb bytes to read.
222 * @return the number of bytes read or -1 if an error occured.
224 void opj_read_bytes_LE(const OPJ_BYTE * p_buffer, OPJ_UINT32 * p_value, OPJ_UINT32 p_nb_bytes);
228 * Write some bytes to the given data buffer, this function is used in Little Endian cpus.
229 * @param p_buffer pointer the data buffer to write data to.
230 * @param p_value the value to write
232 void opj_write_double_LE(OPJ_BYTE * p_buffer, OPJ_FLOAT64 p_value);
235 * Write some bytes to the given data buffer, this function is used in Big Endian cpus.
236 * @param p_buffer pointer the data buffer to write data to.
237 * @param p_value the value to write
239 void opj_write_double_BE(OPJ_BYTE * p_buffer, OPJ_FLOAT64 p_value);
242 * Reads some bytes from the given data buffer, this function is used in Little Endian cpus.
243 * @param p_buffer pointer the data buffer to read data from.
244 * @param p_value pointer to the value that will store the data.
246 void opj_read_double_LE(const OPJ_BYTE * p_buffer, OPJ_FLOAT64 * p_value);
249 * Reads some bytes from the given data buffer, this function is used in Big Endian cpus.
250 * @param p_buffer pointer the data buffer to read data from.
251 * @param p_value pointer to the value that will store the data.
253 void opj_read_double_BE(const OPJ_BYTE * p_buffer, OPJ_FLOAT64 * p_value);
256 * Reads some bytes from the given data buffer, this function is used in Little Endian cpus.
257 * @param p_buffer pointer the data buffer to read data from.
258 * @param p_value pointer to the value that will store the data.
260 void opj_read_float_LE(const OPJ_BYTE * p_buffer, OPJ_FLOAT32 * p_value);
263 * Reads some bytes from the given data buffer, this function is used in Big Endian cpus.
264 * @param p_buffer pointer the data buffer to read data from.
265 * @param p_value pointer to the value that will store the data.
267 void opj_read_float_BE(const OPJ_BYTE * p_buffer, OPJ_FLOAT32 * p_value);
270 * Write some bytes to the given data buffer, this function is used in Little Endian cpus.
271 * @param p_buffer pointer the data buffer to write data to.
272 * @param p_value the value to write
274 void opj_write_float_LE(OPJ_BYTE * p_buffer, OPJ_FLOAT32 p_value);
277 * Write some bytes to the given data buffer, this function is used in Big Endian cpus.
278 * @param p_buffer pointer the data buffer to write data to.
279 * @param p_value the value to write
281 void opj_write_float_BE(OPJ_BYTE * p_buffer, OPJ_FLOAT32 p_value);
284 * Reads some bytes from the stream.
285 * @param p_stream the stream to read data from.
286 * @param p_buffer pointer to the data buffer that will receive the data.
287 * @param p_size number of bytes to read.
288 * @param p_event_mgr the user event manager to be notified of special events.
289 * @return the number of bytes read, or -1 if an error occured or if the stream is at the end.
291 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);
294 * Writes some bytes to the stream.
295 * @param p_stream the stream to write data to.
296 * @param p_buffer pointer to the data buffer holds the data to be writtent.
297 * @param p_size number of bytes to write.
298 * @param p_event_mgr the user event manager to be notified of special events.
299 * @return the number of bytes writtent, or -1 if an error occured.
301 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);
304 * Writes the content of the stream buffer to the stream.
305 * @param p_stream the stream to write data to.
306 * @param p_event_mgr the user event manager to be notified of special events.
307 * @return true if the data could be flushed, false else.
309 opj_bool opj_stream_flush (opj_stream_private_t * p_stream, struct opj_event_mgr * p_event_mgr);
312 * Skips a number of bytes from the stream.
313 * @param p_stream the stream to skip data from.
314 * @param p_size the number of bytes to skip.
315 * @param p_event_mgr the user event manager to be notified of special events.
316 * @return the number of bytes skipped, or -1 if an error occured.
318 OPJ_SIZE_T opj_stream_skip (opj_stream_private_t * p_stream,OPJ_SIZE_T p_size, struct opj_event_mgr * p_event_mgr);
321 * Tells the byte offset on the stream (similar to ftell).
323 * @param p_stream the stream to get the information from.
325 * @return the current position o fthe stream.
327 OPJ_SIZE_T opj_stream_tell (const opj_stream_private_t * p_stream);
330 * Skips a number of bytes from the stream.
331 * @param p_stream the stream to skip data from.
332 * @param p_size the number of bytes to skip.
333 * @param p_event_mgr the user event manager to be notified of special events.
334 * @return the number of bytes skipped, or -1 if an error occured.
336 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);
339 * Skips a number of bytes from the stream.
340 * @param p_stream the stream to skip data from.
341 * @param p_size the number of bytes to skip.
342 * @param p_event_mgr the user event manager to be notified of special events.
343 * @return the number of bytes skipped, or -1 if an error occured.
345 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);
348 * Skips a number of bytes from the stream.
349 * @param p_stream the stream to skip data from.
350 * @param p_size the number of bytes to skip.
351 * @param p_event_mgr the user event manager to be notified of special events.
352 * @return the number of bytes skipped, or -1 if an error occured.
354 opj_bool opj_stream_read_seek (opj_stream_private_t * p_stream, OPJ_SIZE_T p_size, struct opj_event_mgr * p_event_mgr);
357 * Skips a number of bytes from the stream.
358 * @param p_stream the stream to skip data from.
359 * @param p_size the number of bytes to skip.
360 * @param p_event_mgr the user event manager to be notified of special events.
361 * @return the number of bytes skipped, or -1 if an error occured.
363 opj_bool opj_stream_write_seek (opj_stream_private_t * p_stream, OPJ_SIZE_T p_size, struct opj_event_mgr * p_event_mgr);
366 * Seeks a number of bytes from the stream.
367 * @param p_stream the stream to skip data from.
368 * @param p_size the number of bytes to skip.
369 * @param p_event_mgr the user event manager to be notified of special events.
370 * @return true if the stream is seekable.
372 opj_bool opj_stream_seek (opj_stream_private_t * p_stream, OPJ_SIZE_T p_size, struct opj_event_mgr * p_event_mgr);
375 * Tells if the given stream is seekable.
377 opj_bool opj_stream_has_seek (const opj_stream_private_t * p_stream);
379 OPJ_UINT32 opj_stream_default_read (void * p_buffer, OPJ_UINT32 p_nb_bytes, void * p_user_data);
380 OPJ_UINT32 opj_stream_default_write (void * p_buffer, OPJ_UINT32 p_nb_bytes, void * p_user_data);
381 OPJ_SIZE_T opj_stream_default_skip (OPJ_SIZE_T p_nb_bytes, void * p_user_data);
382 opj_bool opj_stream_default_seek (OPJ_SIZE_T p_nb_bytes, void * p_user_data);