[trunk] move opj_bool to OPJ_BOOL to follow the other type
[openjpeg.git] / src / lib / openjp2 / cio.h
1 /*
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  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
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.
18  *
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.
30  */
31
32 #ifndef __CIO_H
33 #define __CIO_H
34 /**
35 @file cio.h
36 @brief Implementation of a byte input-output process (CIO)
37
38 The functions in CIO.C have for goal to realize a byte input / output process.
39 */
40
41 /** @defgroup CIO CIO - byte input-output stream */
42 /*@{*/
43
44 #include "opj_config.h"
45
46 /* ----------------------------------------------------------------------- */
47
48 #if defined(OPJ_BIG_ENDIAN)
49         #define opj_write_bytes         opj_write_bytes_BE
50         #define opj_read_bytes          opj_read_bytes_BE
51         #define opj_write_double        opj_write_double_BE
52         #define opj_read_double         opj_read_double_BE
53         #define opj_write_float         opj_write_float_BE
54         #define opj_read_float          opj_read_float_BE
55 #else
56         #define opj_write_bytes         opj_write_bytes_LE
57         #define opj_read_bytes          opj_read_bytes_LE
58         #define opj_write_double        opj_write_double_LE
59         #define opj_read_double         opj_read_double_LE
60         #define opj_write_float         opj_write_float_LE
61         #define opj_read_float          opj_read_float_LE
62 #endif
63
64
65
66 typedef enum
67 {
68         opj_signed_sentinel             = -1, /* do not use in code */
69         opj_stream_e_output             = 0x1,
70         opj_stream_e_input              = 0x2,
71         opj_stream_e_end                = 0x4,
72         opj_stream_e_error              = 0x8
73 }
74 opj_stream_flag ;
75
76 /**
77 Byte input-output stream.
78 */
79 typedef struct opj_stream_private
80 {
81         /**
82          * User data, be it files, ... The actual data depends on the type of the stream.
83          */
84         void *                                  m_user_data;
85
86         /**
87          * User data length
88          */
89         OPJ_UINT64                              m_user_data_length;
90
91         /**
92          * Pointer to actual read function (NULL at the initialization of the cio.
93          */
94         opj_stream_read_fn              m_read_fn;
95
96         /**
97          * Pointer to actual write function (NULL at the initialization of the cio.
98          */
99         opj_stream_write_fn             m_write_fn;
100
101         /**
102          * Pointer to actual skip function (NULL at the initialization of the cio.
103          * There is no seek function to prevent from back and forth slow procedures.
104          */
105         opj_stream_skip_fn              m_skip_fn;
106
107         /**
108          * Pointer to actual seek function (if available).
109          */
110         opj_stream_seek_fn              m_seek_fn;
111
112         /**
113          * Actual data stored into the stream if readed from. Data is read by chunk of fixed size.
114          * you should never access this data directly.
115          */
116         OPJ_BYTE *                                      m_stored_data;
117
118         /**
119          * Pointer to the current read data.
120          */
121         OPJ_BYTE *                                      m_current_data;
122
123     /**
124     * FIXME DOC.
125     */
126         OPJ_OFF_T (* m_opj_skip)(struct opj_stream_private * ,OPJ_OFF_T , struct opj_event_mgr *);
127
128     /**
129     * FIXME DOC.
130     */
131         OPJ_BOOL (* m_opj_seek) (struct opj_stream_private * , OPJ_OFF_T , struct opj_event_mgr *);
132
133         /**
134          * number of bytes containing in the buffer.
135          */
136         OPJ_SIZE_T                      m_bytes_in_buffer;
137
138         /**
139          * The number of bytes read/written from the beginning of the stream
140          */
141         OPJ_OFF_T                       m_byte_offset;
142
143         /**
144          * The size of the buffer.
145          */
146         OPJ_SIZE_T                      m_buffer_size;
147
148         /**
149          * Flags to tell the status of the stream.
150          */
151         opj_stream_flag m_status;
152
153 }
154 opj_stream_private_t;
155
156 /** @name Exported functions (see also openjpeg.h) */
157 /*@{*/
158 /* ----------------------------------------------------------------------- */
159 /**
160  * Write some bytes to the given data buffer, this function is used in Big Endian cpus.
161  * @param p_buffer              pointer the data buffer to write data to.
162  * @param p_value               the value to write
163  * @param p_nb_bytes    the number of bytes to write
164 */
165 void opj_write_bytes_BE (OPJ_BYTE * p_buffer, OPJ_UINT32 p_value, OPJ_UINT32 p_nb_bytes);
166
167 /**
168  * Reads some bytes from the given data buffer, this function is used in Big Endian cpus.
169  * @param p_buffer              pointer the data buffer to read data from.
170  * @param p_value               pointer to the value that will store the data.
171  * @param p_nb_bytes    the nb bytes to read.
172  * @return                              the number of bytes read or -1 if an error occured.
173  */
174 void opj_read_bytes_BE(const OPJ_BYTE * p_buffer, OPJ_UINT32 * p_value, OPJ_UINT32 p_nb_bytes);
175
176 /**
177  * Write some bytes to the given data buffer, this function is used in Little Endian cpus.
178  * @param p_buffer              pointer the data buffer to write data to.
179  * @param p_value               the value to write
180  * @param p_nb_bytes    the number of bytes to write
181  * @return                              the number of bytes written or -1 if an error occured
182 */
183 void opj_write_bytes_LE (OPJ_BYTE * p_buffer, OPJ_UINT32 p_value, OPJ_UINT32 p_nb_bytes);
184
185 /**
186  * Reads some bytes from the given data buffer, this function is used in Little Endian cpus.
187  * @param p_buffer              pointer the data buffer to read data from.
188  * @param p_value               pointer to the value that will store the data.
189  * @param p_nb_bytes    the nb bytes to read.
190  * @return                              the number of bytes read or -1 if an error occured.
191  */
192 void opj_read_bytes_LE(const OPJ_BYTE * p_buffer, OPJ_UINT32 * p_value, OPJ_UINT32 p_nb_bytes);
193
194
195 /**
196  * Write some bytes to the given data buffer, this function is used in Little Endian cpus.
197  * @param p_buffer              pointer the data buffer to write data to.
198  * @param p_value               the value to write
199  */
200 void opj_write_double_LE(OPJ_BYTE * p_buffer, OPJ_FLOAT64 p_value);
201
202 /***
203  * Write some bytes to the given data buffer, this function is used in Big Endian cpus.
204  * @param p_buffer              pointer the data buffer to write data to.
205  * @param p_value               the value to write
206  */
207 void opj_write_double_BE(OPJ_BYTE * p_buffer, OPJ_FLOAT64 p_value);
208
209 /**
210  * Reads some bytes from the given data buffer, this function is used in Little Endian cpus.
211  * @param p_buffer              pointer the data buffer to read data from.
212  * @param p_value               pointer to the value that will store the data.
213  */
214 void opj_read_double_LE(const OPJ_BYTE * p_buffer, OPJ_FLOAT64 * p_value);
215
216 /**
217  * Reads some bytes from the given data buffer, this function is used in Big Endian cpus.
218  * @param p_buffer              pointer the data buffer to read data from.
219  * @param p_value               pointer to the value that will store the data.
220  */
221 void opj_read_double_BE(const OPJ_BYTE * p_buffer, OPJ_FLOAT64 * p_value);
222
223 /**
224  * Reads some bytes from the given data buffer, this function is used in Little Endian cpus.
225  * @param p_buffer              pointer the data buffer to read data from.
226  * @param p_value               pointer to the value that will store the data.
227  */
228 void opj_read_float_LE(const OPJ_BYTE * p_buffer, OPJ_FLOAT32 * p_value);
229
230 /**
231  * Reads some bytes from the given data buffer, this function is used in Big Endian cpus.
232  * @param p_buffer              pointer the data buffer to read data from.
233  * @param p_value               pointer to the value that will store the data.
234  */
235 void opj_read_float_BE(const OPJ_BYTE * p_buffer, OPJ_FLOAT32 * p_value);
236
237 /**
238  * Write some bytes to the given data buffer, this function is used in Little Endian cpus.
239  * @param p_buffer              pointer the data buffer to write data to.
240  * @param p_value               the value to write
241  */
242 void opj_write_float_LE(OPJ_BYTE * p_buffer, OPJ_FLOAT32 p_value);
243
244 /***
245  * Write some bytes to the given data buffer, this function is used in Big Endian cpus.
246  * @param p_buffer              pointer the data buffer to write data to.
247  * @param p_value               the value to write
248  */
249 void opj_write_float_BE(OPJ_BYTE * p_buffer, OPJ_FLOAT32 p_value);
250
251 /**
252  * Reads some bytes from the stream.
253  * @param               p_stream        the stream to read data from.
254  * @param               p_buffer        pointer to the data buffer that will receive the data.
255  * @param               p_size          number of bytes to read.
256  * @param               p_event_mgr     the user event manager to be notified of special events.
257  * @return              the number of bytes read, or -1 if an error occured or if the stream is at the end.
258  */
259 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);
260
261 /**
262  * Writes some bytes to the stream.
263  * @param               p_stream        the stream to write data to.
264  * @param               p_buffer        pointer to the data buffer holds the data to be writtent.
265  * @param               p_size          number of bytes to write.
266  * @param               p_event_mgr     the user event manager to be notified of special events.
267  * @return              the number of bytes writtent, or -1 if an error occured.
268  */
269 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);
270
271 /**
272  * Writes the content of the stream buffer to the stream.
273  * @param               p_stream        the stream to write data to.
274  * @param               p_event_mgr     the user event manager to be notified of special events.
275  * @return              true if the data could be flushed, false else.
276  */
277 OPJ_BOOL opj_stream_flush (opj_stream_private_t * p_stream, struct opj_event_mgr * p_event_mgr);
278
279 /**
280  * Skips a number of bytes from the stream.
281  * @param               p_stream        the stream to skip data from.
282  * @param               p_size          the number of bytes to skip.
283  * @param               p_event_mgr     the user event manager to be notified of special events.
284  * @return              the number of bytes skipped, or -1 if an error occured.
285  */
286 OPJ_OFF_T opj_stream_skip (opj_stream_private_t * p_stream,OPJ_OFF_T p_size, struct opj_event_mgr * p_event_mgr);
287
288 /**
289  * Tells the byte offset on the stream (similar to ftell).
290  *
291  * @param               p_stream        the stream to get the information from.
292  *
293  * @return              the current position o fthe stream.
294  */
295 OPJ_OFF_T opj_stream_tell (const opj_stream_private_t * p_stream);
296
297
298 /**
299  * Get the number of bytes left before the end of the stream (similar to cio_numbytesleft).
300  *
301  * @param               p_stream        the stream to get the information from.
302  *
303  * @return              Number of bytes left before the end of the stream.
304  */
305 OPJ_OFF_T opj_stream_get_number_byte_left (const opj_stream_private_t * p_stream);
306
307 /**
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.
313  */
314 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);
315
316 /**
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.
322  */
323 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);
324
325 /**
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              OPJ_TRUE if success, or OPJ_FALSE if an error occured.
331  */
332 OPJ_BOOL opj_stream_read_seek (opj_stream_private_t * p_stream, OPJ_OFF_T p_size, struct opj_event_mgr * p_event_mgr);
333
334 /**
335  * Skips 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              the number of bytes skipped, or -1 if an error occured.
340  */
341 OPJ_BOOL opj_stream_write_seek (opj_stream_private_t * p_stream, OPJ_OFF_T p_size, struct opj_event_mgr * p_event_mgr);
342
343 /**
344  * Seeks a number of bytes from the stream.
345  * @param               p_stream        the stream to skip data from.
346  * @param               p_size          the number of bytes to skip.
347  * @param               p_event_mgr     the user event manager to be notified of special events.
348  * @return              true if the stream is seekable.
349  */
350 OPJ_BOOL opj_stream_seek (opj_stream_private_t * p_stream, OPJ_OFF_T p_size, struct opj_event_mgr * p_event_mgr);
351
352 /**
353  * Tells if the given stream is seekable.
354  */
355 OPJ_BOOL opj_stream_has_seek (const opj_stream_private_t * p_stream);
356
357 /**
358  * FIXME DOC.
359  */
360 OPJ_SIZE_T opj_stream_default_read (void * p_buffer, OPJ_SIZE_T p_nb_bytes, void * p_user_data);
361
362 /**
363  * FIXME DOC.
364  */
365 OPJ_SIZE_T opj_stream_default_write (void * p_buffer, OPJ_SIZE_T p_nb_bytes, void * p_user_data);
366
367 /**
368  * FIXME DOC.
369  */
370 OPJ_OFF_T opj_stream_default_skip (OPJ_OFF_T p_nb_bytes, void * p_user_data);
371
372 /**
373  * FIXME DOC.
374  */
375 OPJ_BOOL opj_stream_default_seek (OPJ_OFF_T p_nb_bytes, void * p_user_data);
376
377 /* ----------------------------------------------------------------------- */
378 /*@}*/
379
380 /*@}*/
381
382
383 #endif /* __CIO_H */
384