Merge branch 'master' into coc-qcc
[openjpeg.git] / src / lib / openjp2 / cio.h
1 /*
2  * The copyright in this software is being made available under the 2-clauses 
3  * BSD License, included below. This software may be subject to other third 
4  * party and contributor rights, including patent rights, and no such rights
5  * are granted under this license.
6  *
7  * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
8  * Copyright (c) 2002-2014, Professor Benoit Macq
9  * Copyright (c) 2001-2003, David Janssens
10  * Copyright (c) 2002-2003, Yannick Verschueren
11  * Copyright (c) 2003-2007, Francois-Olivier Devaux 
12  * Copyright (c) 2003-2014, Antonin Descampe
13  * Copyright (c) 2005, Herve Drolon, FreeImage Team
14  * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR 
15  * Copyright (c) 2012, CS Systemes d'Information, France
16  * All rights reserved.
17  *
18  * Redistribution and use in source and binary forms, with or without
19  * modification, are permitted provided that the following conditions
20  * are met:
21  * 1. Redistributions of source code must retain the above copyright
22  *    notice, this list of conditions and the following disclaimer.
23  * 2. Redistributions in binary form must reproduce the above copyright
24  *    notice, this list of conditions and the following disclaimer in the
25  *    documentation and/or other materials provided with the distribution.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
28  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
31  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39
40 #ifndef __CIO_H
41 #define __CIO_H
42 /**
43 @file cio.h
44 @brief Implementation of a byte input-output process (CIO)
45
46 The functions in CIO.C have for goal to realize a byte input / output process.
47 */
48
49 /** @defgroup CIO CIO - byte input-output stream */
50 /*@{*/
51
52 #include "opj_config_private.h"
53
54 /* ----------------------------------------------------------------------- */
55
56 #if defined(OPJ_BIG_ENDIAN)
57         #define opj_write_bytes         opj_write_bytes_BE
58         #define opj_read_bytes          opj_read_bytes_BE
59         #define opj_write_double        opj_write_double_BE
60         #define opj_read_double         opj_read_double_BE
61         #define opj_write_float         opj_write_float_BE
62         #define opj_read_float          opj_read_float_BE
63 #else
64         #define opj_write_bytes         opj_write_bytes_LE
65         #define opj_read_bytes          opj_read_bytes_LE
66         #define opj_write_double        opj_write_double_LE
67         #define opj_read_double         opj_read_double_LE
68         #define opj_write_float         opj_write_float_LE
69         #define opj_read_float          opj_read_float_LE
70 #endif
71
72
73 #define OPJ_STREAM_STATUS_OUTPUT  0x1U
74 #define OPJ_STREAM_STATUS_INPUT   0x2U
75 #define OPJ_STREAM_STATUS_END     0x4U
76 #define OPJ_STREAM_STATUS_ERROR   0x8U
77
78 /**
79 Byte input-output stream.
80 */
81 typedef struct opj_stream_private
82 {
83         /**
84          * User data, be it files, ... The actual data depends on the type of the stream.
85          */
86         void *                                  m_user_data;
87
88         /**
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).
92          */
93         opj_stream_free_user_data_fn            m_free_user_data_fn;
94
95         /**
96          * User data length
97          */
98         OPJ_UINT64                              m_user_data_length;
99
100         /**
101          * Pointer to actual read function (NULL at the initialization of the cio.
102          */
103         opj_stream_read_fn              m_read_fn;
104
105         /**
106          * Pointer to actual write function (NULL at the initialization of the cio.
107          */
108         opj_stream_write_fn             m_write_fn;
109
110         /**
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.
113          */
114         opj_stream_skip_fn              m_skip_fn;
115
116         /**
117          * Pointer to actual seek function (if available).
118          */
119         opj_stream_seek_fn              m_seek_fn;
120
121         /**
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.
124          */
125         OPJ_BYTE *                                      m_stored_data;
126
127         /**
128          * Pointer to the current read data.
129          */
130         OPJ_BYTE *                                      m_current_data;
131
132     /**
133     * FIXME DOC.
134     */
135         OPJ_OFF_T (* m_opj_skip)(struct opj_stream_private * ,OPJ_OFF_T , struct opj_event_mgr *);
136
137     /**
138     * FIXME DOC.
139     */
140         OPJ_BOOL (* m_opj_seek) (struct opj_stream_private * , OPJ_OFF_T , struct opj_event_mgr *);
141
142         /**
143          * number of bytes containing in the buffer.
144          */
145         OPJ_SIZE_T                      m_bytes_in_buffer;
146
147         /**
148          * The number of bytes read/written from the beginning of the stream
149          */
150         OPJ_OFF_T                       m_byte_offset;
151
152         /**
153          * The size of the buffer.
154          */
155         OPJ_SIZE_T                      m_buffer_size;
156
157         /**
158          * Flags to tell the status of the stream.
159          * Used with OPJ_STREAM_STATUS_* defines.
160          */
161         OPJ_UINT32 m_status;
162
163 }
164 opj_stream_private_t;
165
166 /** @name Exported functions (see also openjpeg.h) */
167 /*@{*/
168 /* ----------------------------------------------------------------------- */
169 /**
170  * Write some bytes to the given data buffer, this function is used in Big Endian cpus.
171  * @param p_buffer              pointer the data buffer to write data to.
172  * @param p_value               the value to write
173  * @param p_nb_bytes    the number of bytes to write
174 */
175 void opj_write_bytes_BE (OPJ_BYTE * p_buffer, OPJ_UINT32 p_value, OPJ_UINT32 p_nb_bytes);
176
177 /**
178  * Reads some bytes from the given data buffer, this function is used in Big Endian cpus.
179  * @param p_buffer              pointer the data buffer to read data from.
180  * @param p_value               pointer to the value that will store the data.
181  * @param p_nb_bytes    the nb bytes to read.
182  * @return                              the number of bytes read or -1 if an error occurred.
183  */
184 void opj_read_bytes_BE(const OPJ_BYTE * p_buffer, OPJ_UINT32 * p_value, OPJ_UINT32 p_nb_bytes);
185
186 /**
187  * Write some bytes to the given data buffer, this function is used in Little Endian cpus.
188  * @param p_buffer              pointer the data buffer to write data to.
189  * @param p_value               the value to write
190  * @param p_nb_bytes    the number of bytes to write
191  * @return                              the number of bytes written or -1 if an error occurred
192 */
193 void opj_write_bytes_LE (OPJ_BYTE * p_buffer, OPJ_UINT32 p_value, OPJ_UINT32 p_nb_bytes);
194
195 /**
196  * Reads some bytes from the given data buffer, this function is used in Little Endian cpus.
197  * @param p_buffer              pointer the data buffer to read data from.
198  * @param p_value               pointer to the value that will store the data.
199  * @param p_nb_bytes    the nb bytes to read.
200  * @return                              the number of bytes read or -1 if an error occurred.
201  */
202 void opj_read_bytes_LE(const OPJ_BYTE * p_buffer, OPJ_UINT32 * p_value, OPJ_UINT32 p_nb_bytes);
203
204
205 /**
206  * Write some bytes to the given data buffer, this function is used in Little Endian cpus.
207  * @param p_buffer              pointer the data buffer to write data to.
208  * @param p_value               the value to write
209  */
210 void opj_write_double_LE(OPJ_BYTE * p_buffer, OPJ_FLOAT64 p_value);
211
212 /***
213  * Write some bytes to the given data buffer, this function is used in Big Endian cpus.
214  * @param p_buffer              pointer the data buffer to write data to.
215  * @param p_value               the value to write
216  */
217 void opj_write_double_BE(OPJ_BYTE * p_buffer, OPJ_FLOAT64 p_value);
218
219 /**
220  * Reads some bytes from the given data buffer, this function is used in Little Endian cpus.
221  * @param p_buffer              pointer the data buffer to read data from.
222  * @param p_value               pointer to the value that will store the data.
223  */
224 void opj_read_double_LE(const OPJ_BYTE * p_buffer, OPJ_FLOAT64 * p_value);
225
226 /**
227  * Reads some bytes from the given data buffer, this function is used in Big Endian cpus.
228  * @param p_buffer              pointer the data buffer to read data from.
229  * @param p_value               pointer to the value that will store the data.
230  */
231 void opj_read_double_BE(const OPJ_BYTE * p_buffer, OPJ_FLOAT64 * p_value);
232
233 /**
234  * Reads some bytes from the given data buffer, this function is used in Little Endian cpus.
235  * @param p_buffer              pointer the data buffer to read data from.
236  * @param p_value               pointer to the value that will store the data.
237  */
238 void opj_read_float_LE(const OPJ_BYTE * p_buffer, OPJ_FLOAT32 * p_value);
239
240 /**
241  * Reads some bytes from the given data buffer, this function is used in Big Endian cpus.
242  * @param p_buffer              pointer the data buffer to read data from.
243  * @param p_value               pointer to the value that will store the data.
244  */
245 void opj_read_float_BE(const OPJ_BYTE * p_buffer, OPJ_FLOAT32 * p_value);
246
247 /**
248  * Write some bytes to the given data buffer, this function is used in Little Endian cpus.
249  * @param p_buffer              pointer the data buffer to write data to.
250  * @param p_value               the value to write
251  */
252 void opj_write_float_LE(OPJ_BYTE * p_buffer, OPJ_FLOAT32 p_value);
253
254 /***
255  * Write some bytes to the given data buffer, this function is used in Big Endian cpus.
256  * @param p_buffer              pointer the data buffer to write data to.
257  * @param p_value               the value to write
258  */
259 void opj_write_float_BE(OPJ_BYTE * p_buffer, OPJ_FLOAT32 p_value);
260
261 /**
262  * Reads some bytes from the stream.
263  * @param               p_stream        the stream to read data from.
264  * @param               p_buffer        pointer to the data buffer that will receive the data.
265  * @param               p_size          number of bytes to read.
266  * @param               p_event_mgr     the user event manager to be notified of special events.
267  * @return              the number of bytes read, or -1 if an error occurred or if the stream is at the end.
268  */
269 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);
270
271 /**
272  * Writes some bytes to the stream.
273  * @param               p_stream        the stream to write data to.
274  * @param               p_buffer        pointer to the data buffer holds the data to be writtent.
275  * @param               p_size          number of bytes to write.
276  * @param               p_event_mgr     the user event manager to be notified of special events.
277  * @return              the number of bytes writtent, or -1 if an error occurred.
278  */
279 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);
280
281 /**
282  * Writes the content of the stream buffer to the stream.
283  * @param               p_stream        the stream to write data to.
284  * @param               p_event_mgr     the user event manager to be notified of special events.
285  * @return              true if the data could be flushed, false else.
286  */
287 OPJ_BOOL opj_stream_flush (opj_stream_private_t * p_stream, struct opj_event_mgr * p_event_mgr);
288
289 /**
290  * Skips a number of bytes from the stream.
291  * @param               p_stream        the stream to skip data from.
292  * @param               p_size          the number of bytes to skip.
293  * @param               p_event_mgr     the user event manager to be notified of special events.
294  * @return              the number of bytes skipped, or -1 if an error occurred.
295  */
296 OPJ_OFF_T opj_stream_skip (opj_stream_private_t * p_stream,OPJ_OFF_T p_size, struct opj_event_mgr * p_event_mgr);
297
298 /**
299  * Tells the byte offset on the stream (similar to ftell).
300  *
301  * @param               p_stream        the stream to get the information from.
302  *
303  * @return              the current position o fthe stream.
304  */
305 OPJ_OFF_T opj_stream_tell (const opj_stream_private_t * p_stream);
306
307
308 /**
309  * Get the number of bytes left before the end of the stream (similar to cio_numbytesleft).
310  *
311  * @param               p_stream        the stream to get the information from.
312  *
313  * @return              Number of bytes left before the end of the stream.
314  */
315 OPJ_OFF_T opj_stream_get_number_byte_left (const opj_stream_private_t * p_stream);
316
317 /**
318  * Skips a number of bytes from the stream.
319  * @param               p_stream        the stream to skip data from.
320  * @param               p_size          the number of bytes to skip.
321  * @param               p_event_mgr     the user event manager to be notified of special events.
322  * @return              the number of bytes skipped, or -1 if an error occurred.
323  */
324 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);
325
326 /**
327  * Skips a number of bytes from the stream.
328  * @param               p_stream        the stream to skip data from.
329  * @param               p_size          the number of bytes to skip.
330  * @param               p_event_mgr     the user event manager to be notified of special events.
331  * @return              the number of bytes skipped, or -1 if an error occurred.
332  */
333 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);
334
335 /**
336  * Skips a number of bytes from the stream.
337  * @param               p_stream        the stream to skip data from.
338  * @param               p_size          the number of bytes to skip.
339  * @param               p_event_mgr     the user event manager to be notified of special events.
340  * @return              OPJ_TRUE if success, or OPJ_FALSE if an error occurred.
341  */
342 OPJ_BOOL opj_stream_read_seek (opj_stream_private_t * p_stream, OPJ_OFF_T p_size, struct opj_event_mgr * p_event_mgr);
343
344 /**
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 occurred.
350  */
351 OPJ_BOOL opj_stream_write_seek (opj_stream_private_t * p_stream, OPJ_OFF_T p_size, struct opj_event_mgr * p_event_mgr);
352
353 /**
354  * Seeks 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              true if the stream is seekable.
359  */
360 OPJ_BOOL opj_stream_seek (opj_stream_private_t * p_stream, OPJ_OFF_T p_size, struct opj_event_mgr * p_event_mgr);
361
362 /**
363  * Tells if the given stream is seekable.
364  */
365 OPJ_BOOL opj_stream_has_seek (const opj_stream_private_t * p_stream);
366
367 /**
368  * FIXME DOC.
369  */
370 OPJ_SIZE_T opj_stream_default_read (void * p_buffer, OPJ_SIZE_T p_nb_bytes, void * p_user_data);
371
372 /**
373  * FIXME DOC.
374  */
375 OPJ_SIZE_T opj_stream_default_write (void * p_buffer, OPJ_SIZE_T p_nb_bytes, void * p_user_data);
376
377 /**
378  * FIXME DOC.
379  */
380 OPJ_OFF_T opj_stream_default_skip (OPJ_OFF_T p_nb_bytes, void * p_user_data);
381
382 /**
383  * FIXME DOC.
384  */
385 OPJ_BOOL opj_stream_default_seek (OPJ_OFF_T p_nb_bytes, void * p_user_data);
386
387 /* ----------------------------------------------------------------------- */
388 /*@}*/
389
390 /*@}*/
391
392
393 #endif /* __CIO_H */
394