fixed minor bugs which were triggering warnings at compilation (different signedness...
[openjpeg.git] / libopenjpeg / 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  * Copyright (c) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #ifndef __CIO_H
34 #define __CIO_H
35 /**
36 @file cio.h
37 @brief Implementation of a byte input-output process (CIO)
38
39 The functions in CIO.C have for goal to realize a byte input / output process.
40 */
41
42 /** @defgroup CIO CIO - byte input-output stream */
43 /*@{*/
44
45 #include "openjpeg.h"
46 #include "opj_configure.h"
47 struct opj_event_mgr;
48 /** @name Exported functions (see also openjpeg.h) */
49 /*@{*/
50 /* ----------------------------------------------------------------------- */
51
52 #if defined(OPJ_BIG_ENDIAN) 
53         #if !defined(OPJ_LITTLE_ENDIAN)
54                         #define opj_write_bytes         opj_write_bytes_BE
55                         #define opj_read_bytes          opj_read_bytes_BE
56                         #define opj_write_double        opj_write_double_BE
57                         #define opj_read_double         opj_read_double_BE
58                         #define opj_write_float         opj_write_float_BE
59                         #define opj_read_float          opj_read_float_BE
60         #else
61                         #error "Either BIG_ENDIAN or LITTLE_ENDIAN must be #defined, but not both."
62         #endif
63 #else
64         #if defined(OPJ_LITTLE_ENDIAN)
65                         #define opj_write_bytes         opj_write_bytes_LE
66                         #define opj_read_bytes          opj_read_bytes_LE
67                         #define opj_write_double        opj_write_double_LE
68                         #define opj_read_double         opj_read_double_LE
69                         #define opj_write_float         opj_write_float_LE
70                         #define opj_read_float          opj_read_float_LE
71         #else
72                 #error "Either BIG_ENDIAN or LITTLE_ENDIAN must be #defined, but not none."
73         #endif
74 #endif
75
76
77
78 typedef enum  
79 {
80         opj_stream_e_output             = 0x1,
81         opj_stream_e_input              = 0x2,
82         opj_stream_e_end                = 0x4,
83         opj_stream_e_error              = 0x8
84 }
85 opj_stream_flag ;
86
87 /**
88 Byte input-output stream.
89 */
90 typedef struct opj_stream_private
91 {
92         /**
93          * User data, be it files, ... The actual data depends on the type of the stream.
94          */
95         void *                                  m_user_data;
96         
97         /**
98          * Pointer to actual read function (NULL at the initialization of the cio.
99          */
100         opj_stream_read_fn              m_read_fn;
101         
102         /**
103          * Pointer to actual write function (NULL at the initialization of the cio.
104          */
105         opj_stream_write_fn             m_write_fn;
106         
107         /**
108          * Pointer to actual skip function (NULL at the initialization of the cio.
109          * There is no seek function to prevent from back and forth slow procedures.
110          */
111         opj_stream_skip_fn              m_skip_fn;
112         
113         /**
114          * Pointer to actual seek function (if available).
115          */
116         opj_stream_seek_fn              m_seek_fn;
117
118
119
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         OPJ_SIZE_T (* m_opj_skip)(struct opj_stream_private * ,OPJ_SIZE_T , struct opj_event_mgr *);
133         
134         bool (* m_opj_seek) (struct opj_stream_private * , OPJ_SIZE_T , struct opj_event_mgr *);
135         
136         /**
137          * number of bytes containing in the buffer.
138          */
139         OPJ_UINT32                      m_bytes_in_buffer;
140         
141         /**
142          * The number of bytes read/written.
143          */
144         OPJ_SIZE_T                      m_byte_offset;
145         
146         /**
147          * The size of the buffer.
148          */
149         OPJ_UINT32                      m_buffer_size;
150
151         /**
152          * Flags to tell the status of the stream.
153          */
154         OPJ_UINT32                      m_status;
155         
156
157 opj_stream_private_t;
158
159
160 /**
161  * Write some bytes to the given data buffer, this function is used in Big Endian cpus.
162  * @param p_buffer              pointer the data buffer to write data to.
163  * @param p_value               the value to write
164  * @param p_nb_bytes    the number of bytes to write
165 */
166 void opj_write_bytes_BE (OPJ_BYTE * p_buffer, OPJ_UINT32 p_value, OPJ_UINT32 p_nb_bytes);
167
168 /**
169  * Reads some bytes from the given data buffer, this function is used in Big Endian cpus.
170  * @param p_buffer              pointer the data buffer to read data from.
171  * @param p_value               pointer to the value that will store the data.
172  * @param p_nb_bytes    the nb bytes to read.
173  * @return                              the number of bytes read or -1 if an error occured.
174  */
175 void opj_read_bytes_BE(const OPJ_BYTE * p_buffer, OPJ_UINT32 * p_value, OPJ_UINT32 p_nb_bytes);
176
177 /**
178  * Write some bytes to the given data buffer, this function is used in Little Endian cpus.
179  * @param p_buffer              pointer the data buffer to write data to.
180  * @param p_value               the value to write
181  * @param p_nb_bytes    the number of bytes to write
182  * @return                              the number of bytes written or -1 if an error occured
183 */
184 void opj_write_bytes_LE (OPJ_BYTE * p_buffer, OPJ_UINT32 p_value, OPJ_UINT32 p_nb_bytes);
185
186 /**
187  * Reads some bytes from the given data buffer, this function is used in Little Endian cpus.
188  * @param p_buffer              pointer the data buffer to read data from.
189  * @param p_value               pointer to the value that will store the data.
190  * @param p_nb_bytes    the nb bytes to read.
191  * @return                              the number of bytes read or -1 if an error occured.
192  */
193 void opj_read_bytes_LE(const OPJ_BYTE * p_buffer, OPJ_UINT32 * p_value, OPJ_UINT32 p_nb_bytes);
194
195
196 /**
197  * Write some bytes to the given data buffer, this function is used in Little Endian cpus.
198  * @param p_buffer              pointer the data buffer to write data to.
199  * @param p_value               the value to write
200  */
201 void opj_write_double_LE(OPJ_BYTE * p_buffer, OPJ_FLOAT64 p_value);
202
203 /***
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  */
208 void opj_write_double_BE(OPJ_BYTE * p_buffer, OPJ_FLOAT64 p_value);
209
210 /**
211  * Reads some bytes from the given data buffer, this function is used in Little Endian cpus.
212  * @param p_buffer              pointer the data buffer to read data from.
213  * @param p_value               pointer to the value that will store the data.
214  */
215 void opj_read_double_LE(const OPJ_BYTE * p_buffer, OPJ_FLOAT64 * p_value);
216
217 /**
218  * Reads some bytes from the given data buffer, this function is used in Big 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  */
222 void opj_read_double_BE(const OPJ_BYTE * p_buffer, OPJ_FLOAT64 * p_value);
223
224 /**
225  * Reads some bytes from the given data buffer, this function is used in Little Endian cpus.
226  * @param p_buffer              pointer the data buffer to read data from.
227  * @param p_value               pointer to the value that will store the data.
228  */
229 void opj_read_float_LE(const OPJ_BYTE * p_buffer, OPJ_FLOAT32 * p_value);
230
231 /**
232  * Reads some bytes from the given data buffer, this function is used in Big Endian cpus.
233  * @param p_buffer              pointer the data buffer to read data from.
234  * @param p_value               pointer to the value that will store the data.
235  */
236 void opj_read_float_BE(const OPJ_BYTE * p_buffer, OPJ_FLOAT32 * p_value);
237
238 /**
239  * Write some bytes to the given data buffer, this function is used in Little Endian cpus.
240  * @param p_buffer              pointer the data buffer to write data to.
241  * @param p_value               the value to write
242  */
243 void opj_write_float_LE(OPJ_BYTE * p_buffer, OPJ_FLOAT32 p_value);
244
245 /***
246  * Write some bytes to the given data buffer, this function is used in Big Endian cpus.
247  * @param p_buffer              pointer the data buffer to write data to.
248  * @param p_value               the value to write
249  */
250 void opj_write_float_BE(OPJ_BYTE * p_buffer, OPJ_FLOAT32 p_value);
251
252 /**
253  * Reads some bytes from the stream.
254  * @param               p_stream        the stream to read data from.
255  * @param               p_buffer        pointer to the data buffer that will receive the data.
256  * @param               p_size          number of bytes to read.
257  * @param               p_event_mgr     the user event manager to be notified of special events.
258  * @return              the number of bytes read, or -1 if an error occured or if the stream is at the end.
259  */
260 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);
261
262 /**
263  * Writes some bytes to the stream.
264  * @param               p_stream        the stream to write data to.
265  * @param               p_buffer        pointer to the data buffer holds the data to be writtent.
266  * @param               p_size          number of bytes to write.
267  * @param               p_event_mgr     the user event manager to be notified of special events.
268  * @return              the number of bytes writtent, or -1 if an error occured.
269  */
270 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);
271
272 /**
273  * Writes the content of the stream buffer to the stream.
274  * @param               p_stream        the stream to write data to.
275  * @param               p_event_mgr     the user event manager to be notified of special events.
276  * @return              true if the data could be flushed, false else.
277  */
278 bool opj_stream_flush (opj_stream_private_t * p_stream, struct opj_event_mgr * p_event_mgr);
279
280 /**
281  * Skips a number of bytes from the stream.
282  * @param               p_stream        the stream to skip data from.
283  * @param               p_size          the number of bytes to skip.
284  * @param               p_event_mgr     the user event manager to be notified of special events.
285  * @return              the number of bytes skipped, or -1 if an error occured.
286  */
287 OPJ_SIZE_T opj_stream_skip (opj_stream_private_t * p_stream,OPJ_SIZE_T p_size, struct opj_event_mgr * p_event_mgr);
288
289 /**
290  * Tells the byte offset on the stream (similar to ftell).
291  * 
292  * @param               p_stream        the stream to get the information from.
293  * 
294  * @return              the current position o fthe stream.
295  */
296 OPJ_SIZE_T opj_stream_tell (const opj_stream_private_t * p_stream);
297
298 /**
299  * Skips a number of bytes from the stream.
300  * @param               p_stream        the stream to skip data from.
301  * @param               p_size          the number of bytes to skip.
302  * @param               p_event_mgr     the user event manager to be notified of special events.
303  * @return              the number of bytes skipped, or -1 if an error occured.
304  */
305 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);
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_SIZE_T opj_stream_read_skip (opj_stream_private_t * p_stream, OPJ_SIZE_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 bool opj_stream_read_seek (opj_stream_private_t * p_stream, OPJ_SIZE_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              the number of bytes skipped, or -1 if an error occured.
331  */
332 bool opj_stream_write_seek (opj_stream_private_t * p_stream, OPJ_SIZE_T p_size, struct opj_event_mgr * p_event_mgr);
333
334 /**
335  * Seeks 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              true if the stream is seekable.
340  */
341 bool opj_stream_seek (opj_stream_private_t * p_stream, OPJ_SIZE_T p_size, struct opj_event_mgr * p_event_mgr);
342
343 /**
344  * Tells if the given stream is seekable.
345  */
346 bool opj_stream_has_seek (const opj_stream_private_t * p_stream);
347
348 OPJ_UINT32 opj_stream_default_read (void * p_buffer, OPJ_UINT32 p_nb_bytes, void * p_user_data);
349 OPJ_UINT32 opj_stream_default_write (void * p_buffer, OPJ_UINT32 p_nb_bytes, void * p_user_data);
350 OPJ_SIZE_T opj_stream_default_skip (OPJ_SIZE_T p_nb_bytes, void * p_user_data);
351 bool opj_stream_default_seek (OPJ_SIZE_T p_nb_bytes, void * p_user_data);
352
353 /* ----------------------------------------------------------------------- */
354 /*@}*/
355
356 /*@}*/
357
358 #endif /* __CIO_H */
359