rename jp2_write_colr_v2 to opj_jp2_write_colr
[openjpeg.git] / libopenjpeg / cio.c
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 #include "opj_includes.h"
33
34 /* ----------------------------------------------------------------------- */
35
36 opj_cio_t* OPJ_CALLCONV opj_cio_open(opj_common_ptr cinfo, unsigned char *buffer, int length) {
37         opj_cp_t *cp = NULL;
38         opj_cio_t *cio = (opj_cio_t*)opj_malloc(sizeof(opj_cio_t));
39         if(!cio) return NULL;
40         cio->cinfo = cinfo;
41         if(buffer && length) {
42                 /* wrap a user buffer containing the encoded image */
43                 cio->openmode = OPJ_STREAM_READ;
44                 cio->buffer = buffer;
45                 cio->length = length;
46         }
47         else if(!buffer && !length && cinfo) {
48                 /* allocate a buffer for the encoded image */
49                 cio->openmode = OPJ_STREAM_WRITE;
50                 switch(cinfo->codec_format) {
51                         case CODEC_J2K:
52                                 cp = ((opj_j2k_t*)cinfo->j2k_handle)->cp;
53                                 break;
54                         case CODEC_JP2:
55                                 cp = ((opj_jp2_t*)cinfo->jp2_handle)->j2k->cp;
56                                 break;
57                         default:
58                                 opj_free(cio);
59                                 return NULL;
60                 }
61                 cio->length = (int) (0.1625 * cp->img_size + 2000); /* 0.1625 = 1.3/8 and 2000 bytes as a minimum for headers */
62                 assert(cio->length >= 0);
63                 cio->buffer = (unsigned char *)opj_malloc((OPJ_SIZE_T)cio->length);
64                 if(!cio->buffer) {
65                         opj_event_msg(cio->cinfo, EVT_ERROR, "Error allocating memory for compressed bitstream\n");
66                         opj_free(cio);
67                         return NULL;
68                 }
69         }
70         else {
71                 opj_free(cio);
72                 return NULL;
73         }
74
75         /* Initialize byte IO */
76         cio->start = cio->buffer;
77         cio->end = cio->buffer + cio->length;
78         cio->bp = cio->buffer;
79
80         return cio;
81 }
82
83 void OPJ_CALLCONV opj_cio_close(opj_cio_t *cio) {
84         if(cio) {
85                 if(cio->openmode == OPJ_STREAM_WRITE) {
86                         /* destroy the allocated buffer */
87                         opj_free(cio->buffer);
88                 }
89                 /* destroy the cio */
90                 opj_free(cio);
91         }
92 }
93
94
95 /* ----------------------------------------------------------------------- */
96
97 /*
98  * Get position in byte stream.
99  */
100 OPJ_OFF_T OPJ_CALLCONV cio_tell(opj_cio_t *cio) {
101         return cio->bp - cio->start;
102 }
103
104 /*
105  * Set position in byte stream.
106  *
107  * pos : position, in number of bytes, from the beginning of the stream
108  */
109 void OPJ_CALLCONV cio_seek(opj_cio_t *cio, int pos) {
110         cio->bp = cio->start + pos;
111 }
112
113 /*
114  * Number of bytes left before the end of the stream.
115  */
116 OPJ_SIZE_T cio_numbytesleft(opj_cio_t *cio) {
117         const ptrdiff_t diff = cio->end - cio->bp;
118   assert( diff >= 0 );
119   return (OPJ_SIZE_T)diff;
120 }
121
122 /*
123  * Get pointer to the current position in the stream.
124  */
125 unsigned char *cio_getbp(opj_cio_t *cio) {
126         return cio->bp;
127 }
128
129 /*
130  * Write a byte.
131  */
132 opj_bool cio_byteout(opj_cio_t *cio, unsigned char v) {
133         if (cio->bp >= cio->end) {
134                 opj_event_msg(cio->cinfo, EVT_ERROR, "write error\n");
135                 return OPJ_FALSE;
136         }
137         *cio->bp++ = v;
138         return OPJ_TRUE;
139 }
140
141 /*
142  * Read a byte.
143  */
144 unsigned char cio_bytein(opj_cio_t *cio) {
145         if (cio->bp >= cio->end) {
146                 opj_event_msg(cio->cinfo, EVT_ERROR, "read error: passed the end of the codestream (start = %d, current = %d, end = %d\n", cio->start, cio->bp, cio->end);
147                 return 0;
148         }
149         return *cio->bp++;
150 }
151
152 /*
153  * Write some bytes.
154  *
155  * v : value to write
156  * n : number of bytes to write
157  */
158 unsigned int cio_write(opj_cio_t *cio, unsigned long long int v, int n) {
159         int i;
160         for (i = n - 1; i >= 0; i--) {
161                 if( !cio_byteout(cio, (unsigned char) ((v >> (i << 3)) & 0xff)) )
162                         return 0;
163         }
164   assert( n >= 0 );
165         return (unsigned int)n;
166 }
167
168 /*
169  * Read some bytes.
170  *
171  * n : number of bytes to read
172  *
173  * return : value of the n bytes read
174  */
175 unsigned int cio_read(opj_cio_t *cio, int n) {
176         int i;
177         unsigned int v = 0;
178         for (i = n - 1; i >= 0; i--) {
179     const unsigned int c = cio_bytein(cio);
180                 v += c << (i << 3);
181         }
182         return v;
183 }
184
185 /* 
186  * Skip some bytes.
187  *
188  * n : number of bytes to skip
189  */
190 void cio_skip(opj_cio_t *cio, int n) {
191         cio->bp += n;
192 }
193
194
195 /* ----------------------------------------------------------------------- */
196
197
198 /**
199  * Write some bytes to the given data buffer, this function is used in Big Endian cpus.
200  * @param p_buffer              pointer the data buffer to write data to.
201  * @param p_value               the value to write
202  * @param p_nb_bytes    the number of bytes to write
203 */
204 void opj_write_bytes_BE (OPJ_BYTE * p_buffer, OPJ_UINT32 p_value, OPJ_UINT32 p_nb_bytes)
205 {
206         const OPJ_BYTE * l_data_ptr = ((const OPJ_BYTE *) &p_value) + p_nb_bytes;
207
208         assert(p_nb_bytes > 0 && p_nb_bytes <=  sizeof(OPJ_UINT32));
209
210         memcpy(p_buffer,l_data_ptr,p_nb_bytes);
211 }
212
213 /**
214  * Write some bytes to the given data buffer, this function is used in Little Endian cpus.
215  * @param p_buffer              pointer the data buffer to write data to.
216  * @param p_value               the value to write
217  * @param p_nb_bytes    the number of bytes to write
218  * @return                              the number of bytes written or -1 if an error occured
219 */
220 void opj_write_bytes_LE (OPJ_BYTE * p_buffer, OPJ_UINT32 p_value, OPJ_UINT32 p_nb_bytes)
221 {
222         const OPJ_BYTE * l_data_ptr = ((const OPJ_BYTE *) &p_value) + p_nb_bytes - 1;
223         OPJ_UINT32 i;
224
225         assert(p_nb_bytes > 0 && p_nb_bytes <= sizeof(OPJ_UINT32));
226
227         for     (i=0;i<p_nb_bytes;++i) {
228                 *(p_buffer++) = *(l_data_ptr--);
229         }
230 }
231
232 /**
233  * Reads some bytes from the given data buffer, this function is used in Big Endian cpus.
234  * @param p_buffer              pointer the data buffer to read data from.
235  * @param p_value               pointer to the value that will store the data.
236  * @param p_nb_bytes    the nb bytes to read.
237  * @return                              the number of bytes read or -1 if an error occured.
238  */
239 void opj_read_bytes_BE(const OPJ_BYTE * p_buffer, OPJ_UINT32 * p_value, OPJ_UINT32 p_nb_bytes)
240 {
241         OPJ_BYTE * l_data_ptr = ((OPJ_BYTE *) p_value);
242
243         assert(p_nb_bytes > 0 && p_nb_bytes <= sizeof(OPJ_UINT32));
244
245         *p_value = 0;
246         memcpy(l_data_ptr+4-p_nb_bytes,p_buffer,p_nb_bytes);
247 }
248
249 /**
250  * Reads some bytes from the given data buffer, this function is used in Little Endian cpus.
251  * @param p_buffer              pointer the data buffer to read data from.
252  * @param p_value               pointer to the value that will store the data.
253  * @param p_nb_bytes    the nb bytes to read.
254  * @return                              the number of bytes read or -1 if an error occured.
255  */
256 void opj_read_bytes_LE(const OPJ_BYTE * p_buffer, OPJ_UINT32 * p_value, OPJ_UINT32 p_nb_bytes)
257 {
258         OPJ_BYTE * l_data_ptr = ((OPJ_BYTE *) p_value) + p_nb_bytes-1;
259         OPJ_UINT32 i;
260
261         assert(p_nb_bytes > 0 && p_nb_bytes <= sizeof(OPJ_UINT32));
262
263         *p_value = 0;
264         for (i=0;i<p_nb_bytes;++i) {
265                 *(l_data_ptr--) = *(p_buffer++);
266         }
267 }
268
269 /**
270  * Write some bytes to the given data buffer, this function is used in Big Endian cpus.
271  * @param p_buffer              pointer the data buffer to write data to.
272  * @param p_value               the value to write
273  * @return                              the number of bytes written or -1 if an error occured
274  */
275 void opj_write_double_BE(OPJ_BYTE * p_buffer, OPJ_FLOAT64 p_value)
276 {
277         const OPJ_BYTE * l_data_ptr = ((const OPJ_BYTE *) &p_value);
278         memcpy(p_buffer,l_data_ptr,sizeof(OPJ_FLOAT64));
279 }
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_double_LE(OPJ_BYTE * p_buffer, OPJ_FLOAT64 p_value)
287 {
288         const OPJ_BYTE * l_data_ptr = ((const OPJ_BYTE *) &p_value) + sizeof(OPJ_FLOAT64) - 1;
289         OPJ_UINT32 i;
290         for     (i=0;i<sizeof(OPJ_FLOAT64);++i) {
291                 *(p_buffer++) = *(l_data_ptr--);
292         }
293 }
294
295 /**
296  * Reads some bytes from the given data buffer, this function is used in Big Endian cpus.
297  * @param p_buffer              pointer the data buffer to read data from.
298  * @param p_value               pointer to the value that will store the data.
299  */
300 void opj_read_double_BE(const OPJ_BYTE * p_buffer, OPJ_FLOAT64 * p_value)
301 {
302         OPJ_BYTE * l_data_ptr = ((OPJ_BYTE *) p_value);
303         memcpy(l_data_ptr,p_buffer,sizeof(OPJ_FLOAT64));
304 }
305
306
307 /**
308  * Reads some bytes from the given data buffer, this function is used in Little Endian cpus.
309  * @param p_buffer              pointer the data buffer to read data from.
310  * @param p_value               pointer to the value that will store the data.
311  */
312 void opj_read_double_LE(const OPJ_BYTE * p_buffer, OPJ_FLOAT64 * p_value)
313 {
314         OPJ_BYTE * l_data_ptr = ((OPJ_BYTE *) p_value) + sizeof(OPJ_FLOAT64)-1;
315         OPJ_UINT32 i;
316         for (i=0;i<sizeof(OPJ_FLOAT64);++i) {
317                 *(l_data_ptr--) = *(p_buffer++);
318         }
319 }
320
321 /**
322  * Write some bytes to the given data buffer, this function is used in Big Endian cpus.
323  * @param p_buffer              pointer the data buffer to write data to.
324  * @param p_value               the value to write
325  * @return                              the number of bytes written or -1 if an error occured
326  */
327 void opj_write_float_BE(OPJ_BYTE * p_buffer, OPJ_FLOAT32 p_value)
328 {
329         const OPJ_BYTE * l_data_ptr = ((const OPJ_BYTE *) &p_value);
330         memcpy(p_buffer,l_data_ptr,sizeof(OPJ_FLOAT32));
331 }
332
333 /**
334  * Write some bytes to the given data buffer, this function is used in Little Endian cpus.
335  * @param p_buffer              pointer the data buffer to write data to.
336  * @param p_value               the value to write
337  */
338 void opj_write_float_LE(OPJ_BYTE * p_buffer, OPJ_FLOAT32 p_value)
339 {
340         const OPJ_BYTE * l_data_ptr = ((const OPJ_BYTE *) &p_value) + sizeof(OPJ_FLOAT32) - 1;
341         OPJ_UINT32 i;
342         for     (i=0;i<sizeof(OPJ_FLOAT32);++i) {
343                 *(p_buffer++) = *(l_data_ptr--);
344         }
345 }
346
347 /**
348  * Reads some bytes from the given data buffer, this function is used in Big Endian cpus.
349  * @param p_buffer              pointer the data buffer to read data from.
350  * @param p_value               pointer to the value that will store the data.
351  */
352 void opj_read_float_BE(const OPJ_BYTE * p_buffer, OPJ_FLOAT32 * p_value)
353 {
354         OPJ_BYTE * l_data_ptr = ((OPJ_BYTE *) p_value);
355         memcpy(l_data_ptr,p_buffer,sizeof(OPJ_FLOAT32));
356 }
357
358
359 /**
360  * Reads some bytes from the given data buffer, this function is used in Little Endian cpus.
361  * @param p_buffer              pointer the data buffer to read data from.
362  * @param p_value               pointer to the value that will store the data.
363  */
364 void opj_read_float_LE(const OPJ_BYTE * p_buffer, OPJ_FLOAT32 * p_value)
365 {
366         OPJ_BYTE * l_data_ptr = ((OPJ_BYTE *) p_value) + sizeof(OPJ_FLOAT32)-1;
367         OPJ_UINT32 i;
368         for     (i=0;i<sizeof(OPJ_FLOAT32);++i) {
369                 *(l_data_ptr--) = *(p_buffer++);
370         }
371 }
372
373
374 /**
375  * Creates an abstract stream. This function does nothing except allocating memory and initializing the abstract stream.
376  * @return a stream object.
377 */
378 opj_stream_t* OPJ_CALLCONV opj_stream_create(OPJ_SIZE_T p_buffer_size,opj_bool l_is_input)
379 {
380         opj_stream_private_t * l_stream = 00;
381         l_stream = (opj_stream_private_t*) opj_malloc(sizeof(opj_stream_private_t));
382         if (! l_stream) {
383                 return 00;
384         }
385
386         memset(l_stream,0,sizeof(opj_stream_private_t));
387         l_stream->m_buffer_size = p_buffer_size;
388         l_stream->m_stored_data = (OPJ_BYTE *) opj_malloc(p_buffer_size);
389         if (! l_stream->m_stored_data) {
390                 opj_free(l_stream);
391                 return 00;
392         }
393
394         l_stream->m_current_data = l_stream->m_stored_data;
395
396         if (l_is_input) {
397                 l_stream->m_status |= opj_stream_e_input;
398                 l_stream->m_opj_skip = opj_stream_read_skip;
399                 l_stream->m_opj_seek = opj_stream_read_seek;
400         }
401         else {
402                 l_stream->m_status |= opj_stream_e_output;
403                 l_stream->m_opj_skip = opj_stream_write_skip;
404                 l_stream->m_opj_seek = opj_stream_write_seek;
405         }
406
407         l_stream->m_read_fn = opj_stream_default_read;
408         l_stream->m_write_fn = opj_stream_default_write;
409         l_stream->m_skip_fn = opj_stream_default_skip;
410         l_stream->m_seek_fn = opj_stream_default_seek;
411
412         return (opj_stream_t *) l_stream;
413 }
414
415 /**
416  * Creates an abstract stream. This function does nothing except allocating memory and initializing the abstract stream.
417  * @return a stream object.
418 */
419 opj_stream_t* OPJ_CALLCONV opj_stream_default_create(opj_bool l_is_input)
420 {
421         return opj_stream_create(J2K_STREAM_CHUNK_SIZE,l_is_input);
422 }
423
424 /**
425  * Destroys a stream created by opj_create_stream. This function does NOT close the abstract stream. If needed the user must
426  * close its own implementation of the stream.
427  */
428 OPJ_API void OPJ_CALLCONV opj_stream_destroy(opj_stream_t* p_stream)
429 {
430         opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
431         
432         if (l_stream) {
433                 opj_free(l_stream->m_stored_data);
434                 l_stream->m_stored_data = 00;
435                 opj_free(l_stream);
436         }
437 }
438
439 /**
440  * Sets the given function to be used as a read function.
441  * @param               p_stream        the stream to modify
442  * @param               p_function      the function to use a read function.
443 */
444 OPJ_API void OPJ_CALLCONV opj_stream_set_read_function(opj_stream_t* p_stream, opj_stream_read_fn p_function)
445 {
446         opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
447
448         if ((!l_stream) || (! (l_stream->m_status & opj_stream_e_input))) {
449                 return;
450         }
451
452         l_stream->m_read_fn = p_function;
453 }
454
455 OPJ_API void OPJ_CALLCONV opj_stream_set_seek_function(opj_stream_t* p_stream, opj_stream_seek_fn p_function)
456 {
457         opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
458         
459         if (!l_stream) {
460                 return;
461         }
462         l_stream->m_seek_fn = p_function;
463 }
464
465 /**
466  * Sets the given function to be used as a write function.
467  * @param               p_stream        the stream to modify
468  * @param               p_function      the function to use a write function.
469 */
470 OPJ_API void OPJ_CALLCONV opj_stream_set_write_function(opj_stream_t* p_stream, opj_stream_write_fn p_function)
471 {
472         opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
473         
474         if ((!l_stream )|| (! (l_stream->m_status & opj_stream_e_output))) {
475                 return;
476         }
477
478         l_stream->m_write_fn = p_function;
479 }
480
481 /**
482  * Sets the given function to be used as a skip function.
483  * @param               p_stream        the stream to modify
484  * @param               p_function      the function to use a skip function.
485 */
486 OPJ_API void OPJ_CALLCONV opj_stream_set_skip_function(opj_stream_t* p_stream, opj_stream_skip_fn p_function)
487 {
488         opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
489         
490         if (! l_stream) {
491                 return;
492         }
493
494         l_stream->m_skip_fn = p_function;
495 }
496
497 /**
498  * Sets the given data to be used as a user data for the stream.
499  * @param               p_stream        the stream to modify
500  * @param               p_data          the data to set.
501 */
502 OPJ_API void OPJ_CALLCONV opj_stream_set_user_data(opj_stream_t* p_stream, void * p_data)
503 {
504         opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
505         l_stream->m_user_data = p_data;
506 }
507
508 /**
509  * Sets the given data to be used as a user data for the stream.
510  * @param               p_stream        the stream to modify
511  * @param               p_data          the data to set.
512 */
513 OPJ_API void OPJ_CALLCONV opj_stream_set_user_data_length(opj_stream_t* p_stream, OPJ_UINT64 data_length)
514 {
515         opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
516         l_stream->m_user_data_length = data_length;
517 }
518
519 /**
520  * Reads some bytes from the stream.
521  * @param               p_stream        the stream to read data from.
522  * @param               p_buffer        pointer to the data buffer that will receive the data.
523  * @param               p_size          number of bytes to read.
524  * @param               p_event_mgr     the user event manager to be notified of special events.
525  * @return              the number of bytes read, or -1 if an error occured or if the stream is at the end.
526  */
527 OPJ_SIZE_T opj_stream_read_data (opj_stream_private_t * p_stream,OPJ_BYTE * p_buffer, OPJ_SIZE_T p_size, opj_event_mgr_t * p_event_mgr)
528 {
529         OPJ_SIZE_T l_read_nb_bytes = 0;
530         if (p_stream->m_bytes_in_buffer >= p_size) {
531                 memcpy(p_buffer,p_stream->m_current_data,p_size);
532                 p_stream->m_current_data += p_size;
533                 p_stream->m_bytes_in_buffer -= p_size;
534                 l_read_nb_bytes += p_size;
535                 p_stream->m_byte_offset += (OPJ_OFF_T)p_size;
536                 return l_read_nb_bytes;
537         }
538
539         /* we are now in the case when the remaining data if not sufficient */
540         if (p_stream->m_status & opj_stream_e_end) {
541                 l_read_nb_bytes += p_stream->m_bytes_in_buffer;
542                 memcpy(p_buffer,p_stream->m_current_data,p_stream->m_bytes_in_buffer);
543                 p_stream->m_current_data += p_stream->m_bytes_in_buffer;
544                 p_stream->m_byte_offset += (OPJ_OFF_T)p_stream->m_bytes_in_buffer;
545                 p_stream->m_bytes_in_buffer = 0;
546                 return l_read_nb_bytes ? l_read_nb_bytes : (OPJ_SIZE_T)-1;
547         }
548
549         /* the flag is not set, we copy data and then do an actual read on the stream */
550         if (p_stream->m_bytes_in_buffer) {
551                 l_read_nb_bytes += p_stream->m_bytes_in_buffer;
552                 memcpy(p_buffer,p_stream->m_current_data,p_stream->m_bytes_in_buffer);
553                 p_stream->m_current_data = p_stream->m_stored_data;
554                 p_buffer += p_stream->m_bytes_in_buffer;
555                 p_size -= p_stream->m_bytes_in_buffer;
556                 p_stream->m_byte_offset += (OPJ_OFF_T)p_stream->m_bytes_in_buffer;
557                 p_stream->m_bytes_in_buffer = 0;
558         }
559         else {
560     /* case where we are already at the end of the buffer
561        so reset the m_current_data to point to the start of the
562        stored buffer to get ready to read from disk*/
563                 p_stream->m_current_data = p_stream->m_stored_data;
564         }
565
566
567         while(1){
568                 /* we should read less than a chunk -> read a chunk */
569                 if (p_size < p_stream->m_buffer_size) {
570                         /* we should do an actual read on the media */
571                         p_stream->m_bytes_in_buffer = p_stream->m_read_fn(p_stream->m_stored_data,p_stream->m_buffer_size,p_stream->m_user_data);
572
573                         if (p_stream->m_bytes_in_buffer == (OPJ_SIZE_T)-1) {
574                                 /* end of stream */
575                                 opj_event_msg_v2(p_event_mgr, EVT_INFO, "Stream reached its end !\n");
576
577                                 p_stream->m_bytes_in_buffer = 0;
578                                 p_stream->m_status |= opj_stream_e_end;
579                                 /* end of stream */
580                                 return l_read_nb_bytes ? l_read_nb_bytes : (OPJ_SIZE_T)-1;
581                         }
582                         else if (p_stream->m_bytes_in_buffer < p_size) {
583                                 /* not enough data */
584                                 l_read_nb_bytes += p_stream->m_bytes_in_buffer;
585                                 memcpy(p_buffer,p_stream->m_current_data,p_stream->m_bytes_in_buffer);
586                                 p_stream->m_current_data = p_stream->m_stored_data;
587                                 p_buffer += p_stream->m_bytes_in_buffer;
588                                 p_size -= p_stream->m_bytes_in_buffer;
589                                 p_stream->m_byte_offset += (OPJ_OFF_T)p_stream->m_bytes_in_buffer;
590                                 p_stream->m_bytes_in_buffer = 0;
591                         }
592                         else {
593                                 l_read_nb_bytes += p_size;
594                                 memcpy(p_buffer,p_stream->m_current_data,p_size);
595                                 p_stream->m_current_data += p_size;
596                                 p_stream->m_bytes_in_buffer -= p_size;
597                                 p_stream->m_byte_offset += (OPJ_OFF_T)p_size;
598                                 return l_read_nb_bytes;
599                         }
600                 }
601                 else {
602                         /* direct read on the dest buffer */
603                         p_stream->m_bytes_in_buffer = p_stream->m_read_fn(p_buffer,p_size,p_stream->m_user_data);
604
605                         if (p_stream->m_bytes_in_buffer == (OPJ_SIZE_T)-1) {
606                                 /*  end of stream */
607                                 opj_event_msg_v2(p_event_mgr, EVT_INFO, "Stream reached its end !\n");
608
609                                 p_stream->m_bytes_in_buffer = 0;
610                                 p_stream->m_status |= opj_stream_e_end;
611                                 /* end of stream */
612                                 return l_read_nb_bytes ? l_read_nb_bytes : (OPJ_SIZE_T)-1;
613                         }
614                         else if (p_stream->m_bytes_in_buffer < p_size) {
615                                 /* not enough data */
616                                 l_read_nb_bytes += p_stream->m_bytes_in_buffer;
617                                 p_stream->m_current_data = p_stream->m_stored_data;
618                                 p_buffer += p_stream->m_bytes_in_buffer;
619                                 p_size -= p_stream->m_bytes_in_buffer;
620                                 p_stream->m_byte_offset += (OPJ_OFF_T)p_stream->m_bytes_in_buffer;
621                                 p_stream->m_bytes_in_buffer = 0;
622                         }
623                         else {
624                                 /* we have read the exact size */
625                                 l_read_nb_bytes += p_stream->m_bytes_in_buffer;
626                                 p_stream->m_byte_offset += (OPJ_OFF_T)p_stream->m_bytes_in_buffer;
627                                 p_stream->m_current_data = p_stream->m_stored_data;
628                                 p_stream->m_bytes_in_buffer = 0;
629                                 return l_read_nb_bytes;
630                         }
631                 }
632         }
633 }
634
635 /**
636  * Writes some bytes from the stream.
637  * @param               p_stream        the stream to write data to.
638  * @param               p_buffer        pointer to the data buffer holds the data to be writtent.
639  * @param               p_size          number of bytes to write.
640  * @param               p_event_mgr     the user event manager to be notified of special events.
641  * @return              the number of bytes writtent, or -1 if an error occured.
642  */
643 OPJ_SIZE_T opj_stream_write_data (opj_stream_private_t * p_stream,
644                                                                   const OPJ_BYTE * p_buffer,
645                                                                   OPJ_SIZE_T p_size, 
646                                                                   opj_event_mgr_t * p_event_mgr)
647 {
648         OPJ_SIZE_T l_remaining_bytes = 0;
649         OPJ_SIZE_T l_write_nb_bytes = 0;
650
651         if (p_stream->m_status & opj_stream_e_error) {
652                 return (OPJ_SIZE_T)-1;
653         }
654
655         while(1) {
656                 l_remaining_bytes = p_stream->m_buffer_size - p_stream->m_bytes_in_buffer;
657                 
658                 /* we have more memory than required */
659                 if (l_remaining_bytes >= p_size) {
660                         memcpy(p_stream->m_current_data, p_buffer, p_size);
661                         
662                         p_stream->m_current_data += p_size;
663                         p_stream->m_bytes_in_buffer += p_size;
664                         l_write_nb_bytes += p_size;
665                         p_stream->m_byte_offset += (OPJ_OFF_T)p_size;
666                         
667                         return l_write_nb_bytes;
668                 }
669
670                 /* we copy data and then do an actual read on the stream */
671                 if (l_remaining_bytes) {
672                         l_write_nb_bytes += l_remaining_bytes;
673                         
674                         memcpy(p_stream->m_current_data,p_buffer,l_remaining_bytes);
675                         
676                         p_stream->m_current_data = p_stream->m_stored_data;
677                         
678                         p_buffer += l_remaining_bytes;
679                         p_size -= l_remaining_bytes;
680                         p_stream->m_bytes_in_buffer += l_remaining_bytes;
681                         p_stream->m_byte_offset += (OPJ_OFF_T)l_remaining_bytes;
682                 }
683
684                 if (! opj_stream_flush(p_stream, p_event_mgr)) {
685                         return (OPJ_SIZE_T)-1;
686                 }
687         }
688
689 }
690
691 /**
692  * Writes the content of the stream buffer to the stream.
693  * @param               p_stream        the stream to write data to.
694  * @param               p_event_mgr     the user event manager to be notified of special events.
695  * @return              the number of bytes written, or -1 if an error occured.
696  */
697 opj_bool opj_stream_flush (opj_stream_private_t * p_stream, opj_event_mgr_t * p_event_mgr)
698 {
699         /* the number of bytes written on the media. */
700         OPJ_SIZE_T l_current_write_nb_bytes = 0;
701
702         p_stream->m_current_data = p_stream->m_stored_data;
703
704         while (p_stream->m_bytes_in_buffer) {
705                 /* we should do an actual write on the media */
706                 l_current_write_nb_bytes = p_stream->m_write_fn(p_stream->m_current_data,
707                                                                                                                 p_stream->m_bytes_in_buffer,
708                                                                                                                 p_stream->m_user_data);
709                 
710                 if (l_current_write_nb_bytes == (OPJ_SIZE_T)-1) {
711                         p_stream->m_status |= opj_stream_e_error;
712                         opj_event_msg_v2(p_event_mgr, EVT_INFO, "Error on writting stream!\n");
713
714                         return OPJ_FALSE;
715                 }
716
717                 p_stream->m_current_data += l_current_write_nb_bytes;
718                 p_stream->m_bytes_in_buffer -= l_current_write_nb_bytes;
719         }
720
721         p_stream->m_current_data = p_stream->m_stored_data;
722         
723         return OPJ_TRUE;
724 }
725
726 /**
727  * Skips a number of bytes from the stream.
728  * @param               p_stream        the stream to skip data from.
729  * @param               p_size          the number of bytes to skip.
730  * @param               p_event_mgr     the user event manager to be notified of special events.
731  * @return              the number of bytes skipped, or -1 if an error occured.
732  */
733 OPJ_OFF_T opj_stream_read_skip (opj_stream_private_t * p_stream, OPJ_OFF_T p_size, opj_event_mgr_t * p_event_mgr)
734 {
735         OPJ_OFF_T l_skip_nb_bytes = 0;
736         OPJ_OFF_T l_current_skip_nb_bytes = 0;
737         
738         assert( p_size >= 0 );
739         
740         if (p_stream->m_bytes_in_buffer >= (OPJ_SIZE_T)p_size) {
741                 p_stream->m_current_data += p_size;
742                 /* it is safe to cast p_size to OPJ_SIZE_T since it is <= m_bytes_in_buffer
743                 which is of type OPJ_SIZE_T */
744                 p_stream->m_bytes_in_buffer -= (OPJ_SIZE_T)p_size;
745                 l_skip_nb_bytes += p_size;
746                 p_stream->m_byte_offset += l_skip_nb_bytes;
747                 return l_skip_nb_bytes;
748         }
749
750         /* we are now in the case when the remaining data if not sufficient */
751         if (p_stream->m_status & opj_stream_e_end) {
752                 l_skip_nb_bytes += (OPJ_OFF_T)p_stream->m_bytes_in_buffer;
753                 p_stream->m_current_data += p_stream->m_bytes_in_buffer;
754                 p_stream->m_bytes_in_buffer = 0;
755                 p_stream->m_byte_offset += l_skip_nb_bytes;
756                 return l_skip_nb_bytes ? l_skip_nb_bytes : (OPJ_OFF_T) -1;
757         }
758
759         /* the flag is not set, we copy data and then do an actual skip on the stream */
760         if (p_stream->m_bytes_in_buffer) {
761                 l_skip_nb_bytes += (OPJ_OFF_T)p_stream->m_bytes_in_buffer;
762                 p_stream->m_current_data = p_stream->m_stored_data;
763                 p_size -= (OPJ_OFF_T)p_stream->m_bytes_in_buffer;
764                 p_stream->m_bytes_in_buffer = 0;
765         }
766
767         while (p_size > 0) {
768                 /* we should do an actual skip on the media */
769                 l_current_skip_nb_bytes = p_stream->m_skip_fn(p_size, p_stream->m_user_data);
770                 if (l_current_skip_nb_bytes == (OPJ_OFF_T) -1) {
771                         opj_event_msg_v2(p_event_mgr, EVT_INFO, "Stream reached its end !\n");
772
773                         p_stream->m_status |= opj_stream_e_end;
774                         p_stream->m_byte_offset += l_skip_nb_bytes;
775                         /* end if stream */
776                         return l_skip_nb_bytes ? l_skip_nb_bytes : (OPJ_OFF_T) -1;
777                 }
778                 p_size -= l_current_skip_nb_bytes;
779                 l_skip_nb_bytes += l_current_skip_nb_bytes;
780         }
781
782         p_stream->m_byte_offset += l_skip_nb_bytes;
783         
784         return l_skip_nb_bytes;
785 }
786
787 /**
788  * Skips a number of bytes from the stream.
789  * @param               p_stream        the stream to skip data from.
790  * @param               p_size          the number of bytes to skip.
791  * @param               p_event_mgr     the user event manager to be notified of special events.
792  * @return              the number of bytes skipped, or -1 if an error occured.
793  */
794 OPJ_OFF_T opj_stream_write_skip (opj_stream_private_t * p_stream, OPJ_OFF_T p_size, opj_event_mgr_t * p_event_mgr)
795 {
796         opj_bool l_is_written = 0;
797         OPJ_OFF_T l_current_skip_nb_bytes = 0;
798         OPJ_OFF_T l_skip_nb_bytes = 0;
799
800         if (p_stream->m_status & opj_stream_e_error) {
801                 return (OPJ_OFF_T) -1;
802         }
803
804         /* we should flush data */
805         l_is_written = opj_stream_flush (p_stream, p_event_mgr);
806         if (! l_is_written) {
807                 p_stream->m_status |= opj_stream_e_error;
808                 p_stream->m_bytes_in_buffer = 0;
809                 p_stream->m_current_data = p_stream->m_current_data;
810                 return (OPJ_OFF_T) -1;
811         }
812         /* then skip */
813
814         while (p_size > 0) {
815                 /* we should do an actual skip on the media */
816                 l_current_skip_nb_bytes = p_stream->m_skip_fn(p_size, p_stream->m_user_data);
817                 
818                 if (l_current_skip_nb_bytes == (OPJ_OFF_T)-1) {
819                         opj_event_msg_v2(p_event_mgr, EVT_INFO, "Stream error!\n");
820
821                         p_stream->m_status |= opj_stream_e_error;
822                         p_stream->m_byte_offset += l_skip_nb_bytes;
823                         /* end if stream */
824                         return l_skip_nb_bytes ? l_skip_nb_bytes : (OPJ_OFF_T)-1;
825                 }
826                 p_size -= l_current_skip_nb_bytes;
827                 l_skip_nb_bytes += l_current_skip_nb_bytes;
828         }
829
830         p_stream->m_byte_offset += l_skip_nb_bytes;
831         
832         return l_skip_nb_bytes;
833 }
834
835 /**
836  * Tells the byte offset on the stream (similar to ftell).
837  *
838  * @param               p_stream        the stream to get the information from.
839  *
840  * @return              the current position of the stream.
841  */
842 OPJ_OFF_T opj_stream_tell (const opj_stream_private_t * p_stream)
843 {
844         return p_stream->m_byte_offset;
845 }
846
847
848 /**
849  * Get the number of bytes left before the end of the stream (similar to cio_numbytesleft).
850  *
851  * @param               p_stream        the stream to get the information from.
852  *
853  * @return              Number of bytes left before the end of the stream.
854  */
855 OPJ_OFF_T opj_stream_get_number_byte_left (const opj_stream_private_t * p_stream)
856 {
857   assert( p_stream->m_byte_offset >= 0 );
858   assert( p_stream->m_user_data_length >= (OPJ_UINT64)p_stream->m_byte_offset);
859   return p_stream->m_user_data_length ?
860                                 (OPJ_OFF_T)(p_stream->m_user_data_length) - p_stream->m_byte_offset :
861                                 0;
862 }
863
864 /**
865  * Skips a number of bytes from the stream.
866  * @param               p_stream        the stream to skip data from.
867  * @param               p_size          the number of bytes to skip.
868  * @param               p_event_mgr     the user event manager to be notified of special events.
869  * @return              the number of bytes skipped, or -1 if an error occured.
870  */
871 OPJ_OFF_T opj_stream_skip (opj_stream_private_t * p_stream, OPJ_OFF_T p_size, opj_event_mgr_t * p_event_mgr)
872 {
873         assert(p_size >= 0);
874         return p_stream->m_opj_skip(p_stream,p_size,p_event_mgr);
875 }
876
877
878 /**
879  * Skips a number of bytes from the stream.
880  * @param               p_stream        the stream to skip data from.
881  * @param               p_size          the number of bytes to skip.
882  * @param               p_event_mgr     the user event manager to be notified of special events.
883  * @return              the number of bytes skipped, or -1 if an error occured.
884  */
885 opj_bool opj_stream_read_seek (opj_stream_private_t * p_stream, OPJ_OFF_T p_size, opj_event_mgr_t * p_event_mgr)
886 {
887         OPJ_ARG_NOT_USED(p_event_mgr);
888         p_stream->m_current_data = p_stream->m_stored_data;
889         p_stream->m_bytes_in_buffer = 0;
890
891         if( !(p_stream->m_seek_fn(p_size,p_stream->m_user_data)) ) {
892                 p_stream->m_status |= opj_stream_e_end;
893                 return OPJ_FALSE;
894         }
895         else {
896                 /* reset stream status */
897                 p_stream->m_status &= (~opj_stream_e_end);
898                 p_stream->m_byte_offset = p_size;
899
900         }
901
902         return OPJ_TRUE;
903 }
904
905 /**
906  * Skips a number of bytes from the stream.
907  * @param               p_stream        the stream to skip data from.
908  * @param               p_size          the number of bytes to skip.
909  * @param               p_event_mgr     the user event manager to be notified of special events.
910  * @return              the number of bytes skipped, or -1 if an error occured.
911  */
912 opj_bool opj_stream_write_seek (opj_stream_private_t * p_stream, OPJ_OFF_T p_size, opj_event_mgr_t * p_event_mgr)
913 {
914         if (! opj_stream_flush(p_stream,p_event_mgr)) {
915                 p_stream->m_status |= opj_stream_e_error;
916                 return OPJ_FALSE;
917         }
918
919         p_stream->m_current_data = p_stream->m_stored_data;
920         p_stream->m_bytes_in_buffer = 0;
921
922         if (! p_stream->m_seek_fn(p_size,p_stream->m_user_data)) {
923                 p_stream->m_status |= opj_stream_e_error;
924                 return OPJ_FALSE;
925         }
926         else {
927                 p_stream->m_byte_offset = p_size;
928         }
929
930         return OPJ_TRUE;
931 }
932
933
934 /**
935  * Seeks a number of bytes from the stream.
936  * @param               p_stream        the stream to skip data from.
937  * @param               p_size          the number of bytes to skip.
938  * @param               p_event_mgr     the user event manager to be notified of special events.
939  * @return              true if the stream is seekable.
940  */
941 opj_bool opj_stream_seek (opj_stream_private_t * p_stream, OPJ_OFF_T p_size, struct opj_event_mgr * p_event_mgr)
942 {
943         assert(p_size >= 0);
944         return p_stream->m_opj_seek(p_stream,p_size,p_event_mgr);
945 }
946
947 /**
948  * Tells if the given stream is seekable.
949  */
950 opj_bool opj_stream_has_seek (const opj_stream_private_t * p_stream)
951 {
952         return p_stream->m_seek_fn != opj_stream_default_seek;
953 }
954
955 OPJ_SIZE_T opj_stream_default_read (void * p_buffer, OPJ_SIZE_T p_nb_bytes, void * p_user_data)
956 {
957         OPJ_ARG_NOT_USED(p_buffer);
958         OPJ_ARG_NOT_USED(p_nb_bytes);
959         OPJ_ARG_NOT_USED(p_user_data);
960         return (OPJ_SIZE_T) -1;
961 }
962
963 OPJ_SIZE_T opj_stream_default_write (void * p_buffer, OPJ_SIZE_T p_nb_bytes, void * p_user_data)
964 {
965         OPJ_ARG_NOT_USED(p_buffer);
966         OPJ_ARG_NOT_USED(p_nb_bytes);
967         OPJ_ARG_NOT_USED(p_user_data);
968         return (OPJ_SIZE_T) -1;
969 }
970
971 OPJ_OFF_T opj_stream_default_skip (OPJ_OFF_T p_nb_bytes, void * p_user_data)
972 {
973         OPJ_ARG_NOT_USED(p_nb_bytes);
974         OPJ_ARG_NOT_USED(p_user_data);
975         return (OPJ_OFF_T) -1;
976 }
977
978 opj_bool opj_stream_default_seek (OPJ_OFF_T p_nb_bytes, void * p_user_data)
979 {
980         OPJ_ARG_NOT_USED(p_nb_bytes);
981         OPJ_ARG_NOT_USED(p_user_data);
982         return OPJ_FALSE;
983 }