update j2k_lib with new opj type
[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  * 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 /** @name Exported functions (see also openjpeg.h) */
47 /*@{*/
48 /* ----------------------------------------------------------------------- */
49 /**
50 Number of bytes left before the end of the stream
51 @param cio CIO handle
52 @return Returns the number of bytes before the end of the stream
53 */
54 OPJ_SIZE_T cio_numbytesleft(opj_cio_t *cio);
55 /**
56 Get pointer to the current position in the stream
57 @param cio CIO handle
58 @return Returns a pointer to the current position
59 */
60 unsigned char *cio_getbp(opj_cio_t *cio);
61 /**
62 */
63 opj_bool cio_byteout(opj_cio_t *cio, unsigned char v);
64 /**
65 */
66 unsigned char cio_bytein(opj_cio_t *cio);
67 /**
68 Write some bytes
69 @param cio CIO handle
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
73 */
74 unsigned int cio_write(opj_cio_t *cio, unsigned long long int v, int n);
75 /**
76 Read some bytes
77 @param cio CIO handle
78 @param n Number of bytes to read
79 @return Returns the value of the n bytes read
80 */
81 unsigned int cio_read(opj_cio_t *cio, int n);
82 /**
83 Skip some bytes
84 @param cio CIO handle
85 @param n Number of bytes to skip
86 */
87 void cio_skip(opj_cio_t *cio, int n);
88 /* ----------------------------------------------------------------------- */
89 /*@}*/
90
91 /*@}*/
92
93
94
95 /* ----------------------------------------------------------------------- */
96
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
104 #else
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
111 #endif
112
113
114
115 typedef enum
116 {
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
122 }
123 opj_stream_flag ;
124
125 /**
126 Byte input-output stream.
127 */
128 typedef struct opj_stream_private
129 {
130         /**
131          * User data, be it files, ... The actual data depends on the type of the stream.
132          */
133         void *                                  m_user_data;
134
135         /**
136          * User data length
137          */
138         OPJ_UINT64                              m_user_data_length;
139
140         /**
141          * Pointer to actual read function (NULL at the initialization of the cio.
142          */
143         opj_stream_read_fn              m_read_fn;
144
145         /**
146          * Pointer to actual write function (NULL at the initialization of the cio.
147          */
148         opj_stream_write_fn             m_write_fn;
149
150         /**
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.
153          */
154         opj_stream_skip_fn              m_skip_fn;
155
156         /**
157          * Pointer to actual seek function (if available).
158          */
159         opj_stream_seek_fn              m_seek_fn;
160
161
162
163
164         /**
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.
167          */
168         OPJ_BYTE *                                      m_stored_data;
169
170         /**
171          * Pointer to the current read data.
172          */
173         OPJ_BYTE *                                      m_current_data;
174
175         OPJ_OFF_T (* m_opj_skip)(struct opj_stream_private * ,OPJ_OFF_T , struct opj_event_mgr *);
176
177         opj_bool (* m_opj_seek) (struct opj_stream_private * , OPJ_OFF_T , struct opj_event_mgr *);
178
179         /**
180          * number of bytes containing in the buffer.
181          */
182         OPJ_SIZE_T                      m_bytes_in_buffer;
183
184         /**
185          * The number of bytes read/written from the beginning of the stream
186          */
187         OPJ_OFF_T                       m_byte_offset;
188
189         /**
190          * The size of the buffer.
191          */
192         OPJ_SIZE_T                      m_buffer_size;
193
194         /**
195          * Flags to tell the status of the stream.
196          */
197         opj_stream_flag m_status;
198
199 }
200 opj_stream_private_t;
201
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  * @param p_nb_bytes    the number of bytes to write
208 */
209 void opj_write_bytes_BE (OPJ_BYTE * p_buffer, OPJ_UINT32 p_value, OPJ_UINT32 p_nb_bytes);
210
211 /**
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.
217  */
218 void opj_read_bytes_BE(const OPJ_BYTE * p_buffer, OPJ_UINT32 * p_value, OPJ_UINT32 p_nb_bytes);
219
220 /**
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
226 */
227 void opj_write_bytes_LE (OPJ_BYTE * p_buffer, OPJ_UINT32 p_value, OPJ_UINT32 p_nb_bytes);
228
229 /**
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.
235  */
236 void opj_read_bytes_LE(const OPJ_BYTE * p_buffer, OPJ_UINT32 * p_value, OPJ_UINT32 p_nb_bytes);
237
238
239 /**
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
243  */
244 void opj_write_double_LE(OPJ_BYTE * p_buffer, OPJ_FLOAT64 p_value);
245
246 /***
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
250  */
251 void opj_write_double_BE(OPJ_BYTE * p_buffer, OPJ_FLOAT64 p_value);
252
253 /**
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.
257  */
258 void opj_read_double_LE(const OPJ_BYTE * p_buffer, OPJ_FLOAT64 * p_value);
259
260 /**
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.
264  */
265 void opj_read_double_BE(const OPJ_BYTE * p_buffer, OPJ_FLOAT64 * p_value);
266
267 /**
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.
271  */
272 void opj_read_float_LE(const OPJ_BYTE * p_buffer, OPJ_FLOAT32 * p_value);
273
274 /**
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.
278  */
279 void opj_read_float_BE(const OPJ_BYTE * p_buffer, OPJ_FLOAT32 * p_value);
280
281 /**
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
285  */
286 void opj_write_float_LE(OPJ_BYTE * p_buffer, OPJ_FLOAT32 p_value);
287
288 /***
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
292  */
293 void opj_write_float_BE(OPJ_BYTE * p_buffer, OPJ_FLOAT32 p_value);
294
295 /**
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.
302  */
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);
304
305 /**
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.
312  */
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);
314
315 /**
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.
320  */
321 opj_bool opj_stream_flush (opj_stream_private_t * p_stream, struct opj_event_mgr * p_event_mgr);
322
323 /**
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.
329  */
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);
331
332 /**
333  * Tells the byte offset on the stream (similar to ftell).
334  *
335  * @param               p_stream        the stream to get the information from.
336  *
337  * @return              the current position o fthe stream.
338  */
339 OPJ_OFF_T opj_stream_tell (const opj_stream_private_t * p_stream);
340
341
342 /**
343  * Get the number of bytes left before the end of the stream (similar to cio_numbytesleft).
344  *
345  * @param               p_stream        the stream to get the information from.
346  *
347  * @return              Number of bytes left before the end of the stream.
348  */
349 OPJ_OFF_T opj_stream_get_number_byte_left (const opj_stream_private_t * p_stream);
350
351 /**
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.
357  */
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);
359
360 /**
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.
366  */
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);
368
369 /**
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.
375  */
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);
377
378 /**
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.
384  */
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);
386
387 /**
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.
393  */
394 opj_bool opj_stream_seek (opj_stream_private_t * p_stream, OPJ_OFF_T p_size, struct opj_event_mgr * p_event_mgr);
395
396 /**
397  * Tells if the given stream is seekable.
398  */
399 opj_bool opj_stream_has_seek (const opj_stream_private_t * p_stream);
400
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);
405
406
407
408 #endif /* __CIO_H */
409