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.
131 OPJ_UINT32 m_user_data_length;
134 * Pointer to actual read function (NULL at the initialization of the cio.
136 opj_stream_read_fn m_read_fn;
139 * Pointer to actual write function (NULL at the initialization of the cio.
141 opj_stream_write_fn m_write_fn;
144 * Pointer to actual skip function (NULL at the initialization of the cio.
145 * There is no seek function to prevent from back and forth slow procedures.
147 opj_stream_skip_fn m_skip_fn;
150 * Pointer to actual seek function (if available).
152 opj_stream_seek_fn m_seek_fn;
158 * Actual data stored into the stream if readed from. Data is read by chunk of fixed size.
159 * you should never access this data directly.
161 OPJ_BYTE * m_stored_data;
164 * Pointer to the current read data.
166 OPJ_BYTE * m_current_data;
168 OPJ_SIZE_T (* m_opj_skip)(struct opj_stream_private * ,OPJ_SIZE_T , struct opj_event_mgr *);
170 opj_bool (* m_opj_seek) (struct opj_stream_private * , OPJ_SIZE_T , struct opj_event_mgr *);
173 * number of bytes containing in the buffer.
175 OPJ_UINT32 m_bytes_in_buffer;
178 * The number of bytes read/written.
180 OPJ_SIZE_T m_byte_offset;
183 * The size of the buffer.
185 OPJ_UINT32 m_buffer_size;
188 * Flags to tell the status of the stream.
193 opj_stream_private_t;
197 * Write some bytes to the given data buffer, this function is used in Big Endian cpus.
198 * @param p_buffer pointer the data buffer to write data to.
199 * @param p_value the value to write
200 * @param p_nb_bytes the number of bytes to write
202 void opj_write_bytes_BE (OPJ_BYTE * p_buffer, OPJ_UINT32 p_value, OPJ_UINT32 p_nb_bytes);
205 * Reads some bytes from the given data buffer, this function is used in Big Endian cpus.
206 * @param p_buffer pointer the data buffer to read data from.
207 * @param p_value pointer to the value that will store the data.
208 * @param p_nb_bytes the nb bytes to read.
209 * @return the number of bytes read or -1 if an error occured.
211 void opj_read_bytes_BE(const OPJ_BYTE * p_buffer, OPJ_UINT32 * p_value, OPJ_UINT32 p_nb_bytes);
214 * Write some bytes to the given data buffer, this function is used in Little Endian cpus.
215 * @param p_buffer pointer the data buffer to write data to.
216 * @param p_value the value to write
217 * @param p_nb_bytes the number of bytes to write
218 * @return the number of bytes written or -1 if an error occured
220 void opj_write_bytes_LE (OPJ_BYTE * p_buffer, OPJ_UINT32 p_value, OPJ_UINT32 p_nb_bytes);
223 * Reads some bytes from the given data buffer, this function is used in Little Endian cpus.
224 * @param p_buffer pointer the data buffer to read data from.
225 * @param p_value pointer to the value that will store the data.
226 * @param p_nb_bytes the nb bytes to read.
227 * @return the number of bytes read or -1 if an error occured.
229 void opj_read_bytes_LE(const OPJ_BYTE * p_buffer, OPJ_UINT32 * p_value, OPJ_UINT32 p_nb_bytes);
233 * Write some bytes to the given data buffer, this function is used in Little Endian cpus.
234 * @param p_buffer pointer the data buffer to write data to.
235 * @param p_value the value to write
237 void opj_write_double_LE(OPJ_BYTE * p_buffer, OPJ_FLOAT64 p_value);
240 * Write some bytes to the given data buffer, this function is used in Big 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_double_BE(OPJ_BYTE * p_buffer, OPJ_FLOAT64 p_value);
247 * Reads some bytes from the given data buffer, this function is used in Little Endian cpus.
248 * @param p_buffer pointer the data buffer to read data from.
249 * @param p_value pointer to the value that will store the data.
251 void opj_read_double_LE(const OPJ_BYTE * p_buffer, OPJ_FLOAT64 * p_value);
254 * Reads some bytes from the given data buffer, this function is used in Big Endian cpus.
255 * @param p_buffer pointer the data buffer to read data from.
256 * @param p_value pointer to the value that will store the data.
258 void opj_read_double_BE(const OPJ_BYTE * p_buffer, OPJ_FLOAT64 * p_value);
261 * Reads some bytes from the given data buffer, this function is used in Little Endian cpus.
262 * @param p_buffer pointer the data buffer to read data from.
263 * @param p_value pointer to the value that will store the data.
265 void opj_read_float_LE(const OPJ_BYTE * p_buffer, OPJ_FLOAT32 * p_value);
268 * Reads some bytes from the given data buffer, this function is used in Big Endian cpus.
269 * @param p_buffer pointer the data buffer to read data from.
270 * @param p_value pointer to the value that will store the data.
272 void opj_read_float_BE(const OPJ_BYTE * p_buffer, OPJ_FLOAT32 * p_value);
275 * Write some bytes to the given data buffer, this function is used in Little Endian cpus.
276 * @param p_buffer pointer the data buffer to write data to.
277 * @param p_value the value to write
279 void opj_write_float_LE(OPJ_BYTE * p_buffer, OPJ_FLOAT32 p_value);
282 * Write some bytes to the given data buffer, this function is used in Big Endian cpus.
283 * @param p_buffer pointer the data buffer to write data to.
284 * @param p_value the value to write
286 void opj_write_float_BE(OPJ_BYTE * p_buffer, OPJ_FLOAT32 p_value);
289 * Reads some bytes from the stream.
290 * @param p_stream the stream to read data from.
291 * @param p_buffer pointer to the data buffer that will receive the data.
292 * @param p_size number of bytes to read.
293 * @param p_event_mgr the user event manager to be notified of special events.
294 * @return the number of bytes read, or -1 if an error occured or if the stream is at the end.
296 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);
299 * Writes some bytes to the stream.
300 * @param p_stream the stream to write data to.
301 * @param p_buffer pointer to the data buffer holds the data to be writtent.
302 * @param p_size number of bytes to write.
303 * @param p_event_mgr the user event manager to be notified of special events.
304 * @return the number of bytes writtent, or -1 if an error occured.
306 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);
309 * Writes the content of the stream buffer to the stream.
310 * @param p_stream the stream to write data to.
311 * @param p_event_mgr the user event manager to be notified of special events.
312 * @return true if the data could be flushed, false else.
314 opj_bool opj_stream_flush (opj_stream_private_t * p_stream, 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 OPJ_SIZE_T opj_stream_skip (opj_stream_private_t * p_stream,OPJ_SIZE_T p_size, struct opj_event_mgr * p_event_mgr);
326 * Tells the byte offset on the stream (similar to ftell).
328 * @param p_stream the stream to get the information from.
330 * @return the current position o fthe stream.
332 OPJ_SIZE_T opj_stream_tell (const opj_stream_private_t * p_stream);
336 * Get the number of bytes left before the end of the stream (similar to cio_numbytesleft).
338 * @param p_stream the stream to get the information from.
340 * @return Number of bytes left before the end of the stream.
342 OPJ_SIZE_T opj_stream_get_number_byte_left (const opj_stream_private_t * p_stream);
345 * Skips a number of bytes from the stream.
346 * @param p_stream the stream to skip data from.
347 * @param p_size the number of bytes to skip.
348 * @param p_event_mgr the user event manager to be notified of special events.
349 * @return the number of bytes skipped, or -1 if an error occured.
351 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);
354 * Skips a number of bytes from the stream.
355 * @param p_stream the stream to skip data from.
356 * @param p_size the number of bytes to skip.
357 * @param p_event_mgr the user event manager to be notified of special events.
358 * @return the number of bytes skipped, or -1 if an error occured.
360 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);
363 * Skips a number of bytes from the stream.
364 * @param p_stream the stream to skip data from.
365 * @param p_size the number of bytes to skip.
366 * @param p_event_mgr the user event manager to be notified of special events.
367 * @return the number of bytes skipped, or -1 if an error occured.
369 opj_bool opj_stream_read_seek (opj_stream_private_t * p_stream, OPJ_SIZE_T p_size, struct opj_event_mgr * p_event_mgr);
372 * Skips a number of bytes from the stream.
373 * @param p_stream the stream to skip data from.
374 * @param p_size the number of bytes to skip.
375 * @param p_event_mgr the user event manager to be notified of special events.
376 * @return the number of bytes skipped, or -1 if an error occured.
378 opj_bool opj_stream_write_seek (opj_stream_private_t * p_stream, OPJ_SIZE_T p_size, struct opj_event_mgr * p_event_mgr);
381 * Seeks a number of bytes from the stream.
382 * @param p_stream the stream to skip data from.
383 * @param p_size the number of bytes to skip.
384 * @param p_event_mgr the user event manager to be notified of special events.
385 * @return true if the stream is seekable.
387 opj_bool opj_stream_seek (opj_stream_private_t * p_stream, OPJ_SIZE_T p_size, struct opj_event_mgr * p_event_mgr);
390 * Tells if the given stream is seekable.
392 opj_bool opj_stream_has_seek (const opj_stream_private_t * p_stream);
394 OPJ_UINT32 opj_stream_default_read (void * p_buffer, OPJ_UINT32 p_nb_bytes, void * p_user_data);
395 OPJ_UINT32 opj_stream_default_write (void * p_buffer, OPJ_UINT32 p_nb_bytes, void * p_user_data);
396 OPJ_SIZE_T opj_stream_default_skip (OPJ_SIZE_T p_nb_bytes, void * p_user_data);
397 opj_bool opj_stream_default_seek (OPJ_SIZE_T p_nb_bytes, void * p_user_data);