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 OPJ_SIZE_T 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);
63 opj_bool cio_byteout(opj_cio_t *cio, unsigned char v);
66 unsigned char cio_bytein(opj_cio_t *cio);
70 @param v Value to write
71 @param n Number of bytes to write
72 @return Returns the number of bytes written or 0 if an error occured
74 unsigned int cio_write(opj_cio_t *cio, unsigned long long int v, int n);
78 @param n Number of bytes to read
79 @return Returns the value of the n bytes read
81 unsigned int cio_read(opj_cio_t *cio, int n);
85 @param n Number of bytes to skip
87 void cio_skip(opj_cio_t *cio, int n);
88 /* ----------------------------------------------------------------------- */
95 /* ----------------------------------------------------------------------- */
97 #if defined(OPJ_BIG_ENDIAN)
98 #define opj_write_bytes opj_write_bytes_BE
99 #define opj_read_bytes opj_read_bytes_BE
100 #define opj_write_double opj_write_double_BE
101 #define opj_read_double opj_read_double_BE
102 #define opj_write_float opj_write_float_BE
103 #define opj_read_float opj_read_float_BE
105 #define opj_write_bytes opj_write_bytes_LE
106 #define opj_read_bytes opj_read_bytes_LE
107 #define opj_write_double opj_write_double_LE
108 #define opj_read_double opj_read_double_LE
109 #define opj_write_float opj_write_float_LE
110 #define opj_read_float opj_read_float_LE
117 opj_signed_sentinel = -1, /* do not use in code */
118 opj_stream_e_output = 0x1,
119 opj_stream_e_input = 0x2,
120 opj_stream_e_end = 0x4,
121 opj_stream_e_error = 0x8
126 Byte input-output stream.
128 typedef struct opj_stream_private
131 * User data, be it files, ... The actual data depends on the type of the stream.
138 OPJ_UINT64 m_user_data_length;
141 * Pointer to actual read function (NULL at the initialization of the cio.
143 opj_stream_read_fn m_read_fn;
146 * Pointer to actual write function (NULL at the initialization of the cio.
148 opj_stream_write_fn m_write_fn;
151 * Pointer to actual skip function (NULL at the initialization of the cio.
152 * There is no seek function to prevent from back and forth slow procedures.
154 opj_stream_skip_fn m_skip_fn;
157 * Pointer to actual seek function (if available).
159 opj_stream_seek_fn m_seek_fn;
165 * Actual data stored into the stream if readed from. Data is read by chunk of fixed size.
166 * you should never access this data directly.
168 OPJ_BYTE * m_stored_data;
171 * Pointer to the current read data.
173 OPJ_BYTE * m_current_data;
175 OPJ_OFF_T (* m_opj_skip)(struct opj_stream_private * ,OPJ_OFF_T , struct opj_event_mgr *);
177 opj_bool (* m_opj_seek) (struct opj_stream_private * , OPJ_OFF_T , struct opj_event_mgr *);
180 * number of bytes containing in the buffer.
182 OPJ_SIZE_T m_bytes_in_buffer;
185 * The number of bytes read/written from the beginning of the stream
187 OPJ_OFF_T m_byte_offset;
190 * The size of the buffer.
192 OPJ_SIZE_T m_buffer_size;
195 * Flags to tell the status of the stream.
197 opj_stream_flag m_status;
200 opj_stream_private_t;
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
207 * @param p_nb_bytes the number of bytes to write
209 void opj_write_bytes_BE (OPJ_BYTE * p_buffer, OPJ_UINT32 p_value, OPJ_UINT32 p_nb_bytes);
212 * Reads some bytes from the given data buffer, this function is used in Big 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.
215 * @param p_nb_bytes the nb bytes to read.
216 * @return the number of bytes read or -1 if an error occured.
218 void opj_read_bytes_BE(const OPJ_BYTE * p_buffer, OPJ_UINT32 * p_value, OPJ_UINT32 p_nb_bytes);
221 * Write some bytes to the given data buffer, this function is used in Little Endian cpus.
222 * @param p_buffer pointer the data buffer to write data to.
223 * @param p_value the value to write
224 * @param p_nb_bytes the number of bytes to write
225 * @return the number of bytes written or -1 if an error occured
227 void opj_write_bytes_LE (OPJ_BYTE * p_buffer, OPJ_UINT32 p_value, OPJ_UINT32 p_nb_bytes);
230 * Reads some bytes from the given data buffer, this function is used in Little Endian cpus.
231 * @param p_buffer pointer the data buffer to read data from.
232 * @param p_value pointer to the value that will store the data.
233 * @param p_nb_bytes the nb bytes to read.
234 * @return the number of bytes read or -1 if an error occured.
236 void opj_read_bytes_LE(const OPJ_BYTE * p_buffer, OPJ_UINT32 * p_value, OPJ_UINT32 p_nb_bytes);
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_double_LE(OPJ_BYTE * p_buffer, OPJ_FLOAT64 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_double_BE(OPJ_BYTE * p_buffer, OPJ_FLOAT64 p_value);
254 * Reads some bytes from the given data buffer, this function is used in Little 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_LE(const OPJ_BYTE * p_buffer, OPJ_FLOAT64 * p_value);
261 * Reads some bytes from the given data buffer, this function is used in Big 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_double_BE(const OPJ_BYTE * p_buffer, OPJ_FLOAT64 * p_value);
268 * Reads some bytes from the given data buffer, this function is used in Little 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_LE(const OPJ_BYTE * p_buffer, OPJ_FLOAT32 * p_value);
275 * Reads some bytes from the given data buffer, this function is used in Big Endian cpus.
276 * @param p_buffer pointer the data buffer to read data from.
277 * @param p_value pointer to the value that will store the data.
279 void opj_read_float_BE(const OPJ_BYTE * p_buffer, OPJ_FLOAT32 * p_value);
282 * Write some bytes to the given data buffer, this function is used in Little 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_LE(OPJ_BYTE * p_buffer, OPJ_FLOAT32 p_value);
289 * Write some bytes to the given data buffer, this function is used in Big Endian cpus.
290 * @param p_buffer pointer the data buffer to write data to.
291 * @param p_value the value to write
293 void opj_write_float_BE(OPJ_BYTE * p_buffer, OPJ_FLOAT32 p_value);
296 * Reads some bytes from the stream.
297 * @param p_stream the stream to read data from.
298 * @param p_buffer pointer to the data buffer that will receive the data.
299 * @param p_size number of bytes to read.
300 * @param p_event_mgr the user event manager to be notified of special events.
301 * @return the number of bytes read, or -1 if an error occured or if the stream is at the end.
303 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);
306 * Writes some bytes to the stream.
307 * @param p_stream the stream to write data to.
308 * @param p_buffer pointer to the data buffer holds the data to be writtent.
309 * @param p_size number of bytes to write.
310 * @param p_event_mgr the user event manager to be notified of special events.
311 * @return the number of bytes writtent, or -1 if an error occured.
313 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);
316 * Writes the content of the stream buffer to the stream.
317 * @param p_stream the stream to write data to.
318 * @param p_event_mgr the user event manager to be notified of special events.
319 * @return true if the data could be flushed, false else.
321 opj_bool opj_stream_flush (opj_stream_private_t * p_stream, struct opj_event_mgr * p_event_mgr);
324 * Skips a number of bytes from the stream.
325 * @param p_stream the stream to skip data from.
326 * @param p_size the number of bytes to skip.
327 * @param p_event_mgr the user event manager to be notified of special events.
328 * @return the number of bytes skipped, or -1 if an error occured.
330 OPJ_OFF_T opj_stream_skip (opj_stream_private_t * p_stream,OPJ_OFF_T p_size, struct opj_event_mgr * p_event_mgr);
333 * Tells the byte offset on the stream (similar to ftell).
335 * @param p_stream the stream to get the information from.
337 * @return the current position o fthe stream.
339 OPJ_OFF_T opj_stream_tell (const opj_stream_private_t * p_stream);
343 * Get the number of bytes left before the end of the stream (similar to cio_numbytesleft).
345 * @param p_stream the stream to get the information from.
347 * @return Number of bytes left before the end of the stream.
349 OPJ_OFF_T opj_stream_get_number_byte_left (const opj_stream_private_t * p_stream);
352 * Skips a number of bytes from the stream.
353 * @param p_stream the stream to skip data from.
354 * @param p_size the number of bytes to skip.
355 * @param p_event_mgr the user event manager to be notified of special events.
356 * @return the number of bytes skipped, or -1 if an error occured.
358 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);
361 * Skips a number of bytes from the stream.
362 * @param p_stream the stream to skip data from.
363 * @param p_size the number of bytes to skip.
364 * @param p_event_mgr the user event manager to be notified of special events.
365 * @return the number of bytes skipped, or -1 if an error occured.
367 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);
370 * Skips a number of bytes from the stream.
371 * @param p_stream the stream to skip data from.
372 * @param p_size the number of bytes to skip.
373 * @param p_event_mgr the user event manager to be notified of special events.
374 * @return OPJ_TRUE if success, or OPJ_FALSE if an error occured.
376 opj_bool opj_stream_read_seek (opj_stream_private_t * p_stream, OPJ_OFF_T p_size, struct opj_event_mgr * p_event_mgr);
379 * Skips a number of bytes from the stream.
380 * @param p_stream the stream to skip data from.
381 * @param p_size the number of bytes to skip.
382 * @param p_event_mgr the user event manager to be notified of special events.
383 * @return the number of bytes skipped, or -1 if an error occured.
385 opj_bool opj_stream_write_seek (opj_stream_private_t * p_stream, OPJ_OFF_T p_size, struct opj_event_mgr * p_event_mgr);
388 * Seeks a number of bytes from the stream.
389 * @param p_stream the stream to skip data from.
390 * @param p_size the number of bytes to skip.
391 * @param p_event_mgr the user event manager to be notified of special events.
392 * @return true if the stream is seekable.
394 opj_bool opj_stream_seek (opj_stream_private_t * p_stream, OPJ_OFF_T p_size, struct opj_event_mgr * p_event_mgr);
397 * Tells if the given stream is seekable.
399 opj_bool opj_stream_has_seek (const opj_stream_private_t * p_stream);
401 OPJ_SIZE_T opj_stream_default_read (void * p_buffer, OPJ_SIZE_T p_nb_bytes, void * p_user_data);
402 OPJ_SIZE_T opj_stream_default_write (void * p_buffer, OPJ_SIZE_T p_nb_bytes, void * p_user_data);
403 OPJ_OFF_T opj_stream_default_skip (OPJ_OFF_T p_nb_bytes, void * p_user_data);
404 opj_bool opj_stream_default_seek (OPJ_OFF_T p_nb_bytes, void * p_user_data);