rename jp2_setup_decoder_v2 to opj_jp2_setup_decoder
[openjpeg.git] / libopenjpeg / openjpeg.c
1 /*
2  * Copyright (c) 2005, Herve Drolon, FreeImage Team
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24  * POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #ifdef _WIN32
28 #include <windows.h>
29 #endif /* _WIN32 */
30
31 #include "opj_config.h"
32 #include "opj_includes.h"
33
34
35 /**
36  * Decompression handler.
37  */
38 typedef struct opj_decompression
39 {
40         /** Main header reading function handler*/
41         opj_bool (*opj_read_header) (   struct opj_stream_private * cio,
42                                                                         void * p_codec,
43                                                                         opj_image_t **p_image,
44                                                                         struct opj_event_mgr * p_manager);
45         /** Decoding function */
46         opj_bool (*opj_decode) (        void * p_codec,
47                                                                 struct opj_stream_private *p_cio,
48                                                                 opj_image_t *p_image,
49                                                                 struct opj_event_mgr * p_manager);
50         /** FIXME DOC */
51         opj_bool (*opj_read_tile_header)(       void * p_codec,
52                                                                                 OPJ_UINT32 * p_tile_index,
53                                                                                 OPJ_UINT32* p_data_size,
54                                                                                 OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
55                                                                                 OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
56                                                                                 OPJ_UINT32 * p_nb_comps,
57                                                                                 opj_bool * p_should_go_on,
58                                                                                 struct opj_stream_private *p_cio,
59                                                                                 struct opj_event_mgr * p_manager);
60         /** FIXME DOC */
61         opj_bool (*opj_decode_tile_data)(       void * p_codec,
62                                                                                 OPJ_UINT32 p_tile_index,
63                                                                                 OPJ_BYTE * p_data,
64                                                                                 OPJ_UINT32 p_data_size,
65                                                                                 struct opj_stream_private *p_cio,
66                                                                                 struct opj_event_mgr * p_manager);
67         /** Reading function used after codestream if necessary */
68         opj_bool (* opj_end_decompress) (       void *p_codec,
69                                                                                 struct opj_stream_private *cio,
70                                                                                 struct opj_event_mgr * p_manager);
71         /** Codec destroy function handler*/
72         void (*opj_destroy) (void * p_codec);
73         /** Setup decoder function handler */
74         void (*opj_setup_decoder) (void * p_codec, opj_dparameters_t * p_param);
75         /** Set decode area function handler */
76         opj_bool (*opj_set_decode_area) (       void * p_codec,
77                                                                                 opj_image_t* p_image,
78                                                                                 OPJ_INT32 p_start_x, OPJ_INT32 p_end_x,
79                                                                                 OPJ_INT32 p_start_y, OPJ_INT32 p_end_y,
80                                                                                 struct opj_event_mgr * p_manager);
81
82         /** Get tile function */
83         opj_bool (*opj_get_decoded_tile) (      void *p_codec,
84                                                                                 opj_stream_private_t *p_cio,
85                                                                                 opj_image_t *p_image,
86                                                                                 struct opj_event_mgr * p_manager,
87                                                                                 OPJ_UINT32 tile_index);
88
89         /** Set the decoded resolution factor */
90         opj_bool (*opj_set_decoded_resolution_factor) (void * p_codec, OPJ_UINT32 res_factor, struct opj_event_mgr * p_manager);
91
92 }opj_decompression_t;
93
94 /**
95  * Compression handler. FIXME DOC
96  */
97 typedef struct opj_compression
98 {
99         opj_bool (* opj_start_compress) (       void *p_codec,
100                                                                                 struct opj_stream_private *cio,
101                                                                                 struct opj_image * p_image,
102                                                                                 struct opj_event_mgr * p_manager);
103
104         opj_bool (* opj_encode) (       void * p_codec,
105                                                                 struct opj_stream_private *p_cio,
106                                                                 struct opj_event_mgr * p_manager);
107
108         opj_bool (* opj_write_tile) (   void * p_codec,
109                                                                         OPJ_UINT32 p_tile_index,
110                                                                         OPJ_BYTE * p_data,
111                                                                         OPJ_UINT32 p_data_size,
112                                                                         struct opj_stream_private * p_cio,
113                                                                         struct opj_event_mgr * p_manager);
114
115         opj_bool (* opj_end_compress) ( void * p_codec,
116                                                                         struct opj_stream_private *p_cio,
117                                                                         struct opj_event_mgr * p_manager);
118
119         void (* opj_destroy) (void * p_codec);
120
121         void (*opj_setup_encoder) (     void * p_codec,
122                                                                 opj_cparameters_t * p_param,
123                                                                 struct opj_image * p_image,
124                                                                 struct opj_event_mgr * p_manager);
125
126 }opj_compression_t;
127
128 /**
129  * Main codec handler used for compression or decompression.
130  */
131 typedef struct opj_codec_private
132 {
133         /** FIXME DOC */
134         union 
135     {
136         opj_decompression_t m_decompression;
137         opj_compression_t m_compression;
138     } m_codec_data;
139     /** FIXME DOC*/
140         void * m_codec;
141         /** Event handler */
142         opj_event_mgr_t m_event_mgr;
143         /** Flag to indicate if the codec is used to decode or encode*/
144         opj_bool is_decompressor;
145         void (*opj_dump_codec) (void * p_codec, OPJ_INT32 info_flag, FILE* output_stream);
146         opj_codestream_info_v2_t* (*opj_get_codec_info)(void* p_codec);
147         opj_codestream_index_t* (*opj_get_codec_index)(void* p_codec);
148 }
149 opj_codec_private_t;
150
151 /* ---------------------------------------------------------------------- */
152 /* Functions to set info_handler */
153
154 opj_bool OPJ_CALLCONV opj_set_info_handler(     opj_codec_t * p_codec, 
155                                                                                         opj_msg_callback p_callback,
156                                                                                         void * p_user_data)
157 {
158         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
159         if(! l_codec){
160                 return OPJ_FALSE;
161         }
162         
163         l_codec->m_event_mgr.info_handler = p_callback;
164         l_codec->m_event_mgr.m_info_data = p_user_data;
165         
166         return OPJ_TRUE;
167 }
168
169 opj_bool OPJ_CALLCONV opj_set_warning_handler(  opj_codec_t * p_codec, 
170                                                                                                 opj_msg_callback p_callback,
171                                                                                                 void * p_user_data)
172 {
173         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
174         if (! l_codec) {
175                 return OPJ_FALSE;
176         }
177         
178         l_codec->m_event_mgr.warning_handler = p_callback;
179         l_codec->m_event_mgr.m_warning_data = p_user_data;
180         
181         return OPJ_TRUE;
182 }
183
184 opj_bool OPJ_CALLCONV opj_set_error_handler(opj_codec_t * p_codec, 
185                                                                                         opj_msg_callback p_callback,
186                                                                                         void * p_user_data)
187 {
188         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
189         if (! l_codec) {
190                 return OPJ_FALSE;
191         }
192         
193         l_codec->m_event_mgr.error_handler = p_callback;
194         l_codec->m_event_mgr.m_error_data = p_user_data;
195         
196         return OPJ_TRUE;
197 }
198
199 /* ---------------------------------------------------------------------- */
200
201
202 OPJ_SIZE_T opj_read_from_file (void * p_buffer, OPJ_SIZE_T p_nb_bytes, FILE * p_file)
203 {
204         OPJ_SIZE_T l_nb_read = fread(p_buffer,1,p_nb_bytes,p_file);
205         return l_nb_read ? l_nb_read : -1;
206 }
207
208 OPJ_UINT64 opj_get_data_length_from_file (FILE * p_file)
209 {
210         OPJ_OFF_T file_length = 0;
211
212         OPJ_FSEEK(p_file, 0, SEEK_END);
213         file_length = (OPJ_UINT64)OPJ_FTELL(p_file);
214         OPJ_FSEEK(p_file, 0, SEEK_SET);
215
216         return file_length;
217 }
218
219 OPJ_SIZE_T opj_write_from_file (void * p_buffer, OPJ_SIZE_T p_nb_bytes, FILE * p_file)
220 {
221         return fwrite(p_buffer,1,p_nb_bytes,p_file);
222 }
223
224 OPJ_OFF_T opj_skip_from_file (OPJ_OFF_T p_nb_bytes, FILE * p_user_data)
225 {
226         if (OPJ_FSEEK(p_user_data,p_nb_bytes,SEEK_CUR)) {
227                 return -1;
228         }
229
230         return p_nb_bytes;
231 }
232
233 opj_bool opj_seek_from_file (OPJ_OFF_T p_nb_bytes, FILE * p_user_data)
234 {
235         if (OPJ_FSEEK(p_user_data,p_nb_bytes,SEEK_SET)) {
236                 return OPJ_FALSE;
237         }
238
239         return OPJ_TRUE;
240 }
241
242 /* ---------------------------------------------------------------------- */
243 #ifdef _WIN32
244 #ifndef OPJ_STATIC
245 BOOL APIENTRY
246 DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
247
248         OPJ_ARG_NOT_USED(lpReserved);
249         OPJ_ARG_NOT_USED(hModule);
250
251         switch (ul_reason_for_call) {
252                 case DLL_PROCESS_ATTACH :
253                         break;
254                 case DLL_PROCESS_DETACH :
255                         break;
256                 case DLL_THREAD_ATTACH :
257                 case DLL_THREAD_DETACH :
258                         break;
259     }
260
261     return TRUE;
262 }
263 #endif /* OPJ_STATIC */
264 #endif /* _WIN32 */
265
266 /* ---------------------------------------------------------------------- */
267
268
269 const char* OPJ_CALLCONV opj_version(void) {
270     return PACKAGE_VERSION;
271 }
272
273 /* ---------------------------------------------------------------------- */
274 /* DECOMPRESSION FUNCTIONS*/
275
276 opj_codec_t* OPJ_CALLCONV opj_create_decompress(OPJ_CODEC_FORMAT p_format)
277 {
278         opj_codec_private_t *l_codec = 00;
279
280         l_codec = (opj_codec_private_t*) opj_calloc(1, sizeof(opj_codec_private_t));
281         if (!l_codec){
282                 return 00;
283         }
284         memset(l_codec, 0, sizeof(opj_codec_private_t));
285
286         l_codec->is_decompressor = 1;
287
288         switch (p_format) {
289                 case CODEC_J2K:
290                         l_codec->opj_dump_codec = (void (*) (void*, OPJ_INT32, FILE*)) j2k_dump;
291
292                         l_codec->opj_get_codec_info = (opj_codestream_info_v2_t* (*) (void*) ) j2k_get_cstr_info;
293
294                         l_codec->opj_get_codec_index = (opj_codestream_index_t* (*) (void*) ) j2k_get_cstr_index;
295
296                         l_codec->m_codec_data.m_decompression.opj_decode =
297                                         (opj_bool (*) ( void *,
298                                                                         struct opj_stream_private *,
299                                                                         opj_image_t*, struct opj_event_mgr * )) j2k_decode_v2;
300
301                         l_codec->m_codec_data.m_decompression.opj_end_decompress =
302                                         (opj_bool (*) ( void *,
303                                                                         struct opj_stream_private *,
304                                                                         struct opj_event_mgr *)) j2k_end_decompress;
305
306                         l_codec->m_codec_data.m_decompression.opj_read_header =
307                                         (opj_bool (*) ( struct opj_stream_private *,
308                                                                         void *,
309                                                                         opj_image_t **,
310                                                                         struct opj_event_mgr * )) j2k_read_header;
311
312                         l_codec->m_codec_data.m_decompression.opj_destroy =
313                                         (void (*) (void *))j2k_destroy;
314
315                         l_codec->m_codec_data.m_decompression.opj_setup_decoder =
316                                         (void (*) (void * , opj_dparameters_t * )) j2k_setup_decoder_v2;
317
318                         l_codec->m_codec_data.m_decompression.opj_read_tile_header =
319                                         (opj_bool (*) ( void *,
320                                                                         OPJ_UINT32*,
321                                                                         OPJ_UINT32*,
322                                                                         OPJ_INT32*, OPJ_INT32*,
323                                                                         OPJ_INT32*, OPJ_INT32*,
324                                                                         OPJ_UINT32*,
325                                                                         opj_bool*,
326                                                                         struct opj_stream_private *,
327                                                                         struct opj_event_mgr * )) j2k_read_tile_header;
328
329                         l_codec->m_codec_data.m_decompression.opj_decode_tile_data =
330                                         (opj_bool (*) (void *, OPJ_UINT32, OPJ_BYTE*, OPJ_UINT32, struct opj_stream_private *, struct opj_event_mgr *)) j2k_decode_tile;
331
332                         l_codec->m_codec_data.m_decompression.opj_set_decode_area =
333                                         (opj_bool (*) (void *, opj_image_t*, OPJ_INT32, OPJ_INT32, OPJ_INT32, OPJ_INT32, struct opj_event_mgr *)) j2k_set_decode_area;
334
335                         l_codec->m_codec_data.m_decompression.opj_get_decoded_tile = (opj_bool (*) (void *p_codec,
336                                                                                                                                                                                 opj_stream_private_t *p_cio,
337                                                                                                                                                                                 opj_image_t *p_image,
338                                                                                                                                                                                 struct opj_event_mgr * p_manager,
339                                                                                                                                                                                 OPJ_UINT32 tile_index)) j2k_get_tile;
340
341                         l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor = (opj_bool (*) (void * p_codec,
342                                                                                                                                                                                                         OPJ_UINT32 res_factor,
343                                                                                                                                                                                                         struct opj_event_mgr * p_manager)) j2k_set_decoded_resolution_factor;
344
345                         l_codec->m_codec = j2k_create_decompress_v2();
346
347                         if (! l_codec->m_codec) {
348                                 opj_free(l_codec);
349                                 return NULL;
350                         }
351
352                         break;
353
354                 case CODEC_JP2:
355                         /* get a JP2 decoder handle */
356                         l_codec->opj_dump_codec = (void (*) (void*, OPJ_INT32, FILE*)) jp2_dump;
357
358                         l_codec->opj_get_codec_info = (opj_codestream_info_v2_t* (*) (void*) ) jp2_get_cstr_info;
359
360                         l_codec->opj_get_codec_index = (opj_codestream_index_t* (*) (void*) ) jp2_get_cstr_index;
361
362                         l_codec->m_codec_data.m_decompression.opj_decode =
363                                         (opj_bool (*) ( void *,
364                                                                         struct opj_stream_private *,
365                                                                         opj_image_t*,
366                                                                         struct opj_event_mgr * )) jp2_decode_v2;
367
368                         l_codec->m_codec_data.m_decompression.opj_end_decompress =  (opj_bool (*) (void *,struct opj_stream_private *,struct opj_event_mgr *)) jp2_end_decompress;
369
370                         l_codec->m_codec_data.m_decompression.opj_read_header =  (opj_bool (*) (
371                                         struct opj_stream_private *,
372                                         void *,
373                                         opj_image_t **,
374                                         struct opj_event_mgr * )) jp2_read_header;
375
376                         l_codec->m_codec_data.m_decompression.opj_read_tile_header = ( opj_bool (*) (
377                                         void *,
378                                         OPJ_UINT32*,
379                                         OPJ_UINT32*,
380                                         OPJ_INT32*,
381                                         OPJ_INT32*,
382                                         OPJ_INT32 * ,
383                                         OPJ_INT32 * ,
384                                         OPJ_UINT32 * ,
385                                         opj_bool *,
386                                         struct opj_stream_private *,
387                                         struct opj_event_mgr * )) jp2_read_tile_header;
388
389                         l_codec->m_codec_data.m_decompression.opj_decode_tile_data = (opj_bool (*) (void *,OPJ_UINT32,OPJ_BYTE*,OPJ_UINT32,struct opj_stream_private *, struct opj_event_mgr * )) jp2_decode_tile;
390
391                         l_codec->m_codec_data.m_decompression.opj_destroy = (void (*) (void *))jp2_destroy;
392
393                         l_codec->m_codec_data.m_decompression.opj_setup_decoder = (void (*) (void * ,opj_dparameters_t * )) opj_jp2_setup_decoder;
394
395                         l_codec->m_codec_data.m_decompression.opj_set_decode_area = (opj_bool (*) (void *,opj_image_t*, OPJ_INT32,OPJ_INT32,OPJ_INT32,OPJ_INT32, struct opj_event_mgr * )) jp2_set_decode_area;
396
397                         l_codec->m_codec_data.m_decompression.opj_get_decoded_tile = (opj_bool (*) (void *p_codec,
398                                                                                                                                                                                 opj_stream_private_t *p_cio,
399                                                                                                                                                                                 opj_image_t *p_image,
400                                                                                                                                                                                 struct opj_event_mgr * p_manager,
401                                                                                                                                                                                 OPJ_UINT32 tile_index)) jp2_get_tile;
402
403                         l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor = (opj_bool (*) (void * p_codec,
404                                                                                                                                                                                                         OPJ_UINT32 res_factor,
405                                                                                                                                                                                                         opj_event_mgr_t * p_manager)) jp2_set_decoded_resolution_factor;
406
407                         l_codec->m_codec = jp2_create(OPJ_TRUE);
408
409                         if (! l_codec->m_codec) {
410                                 opj_free(l_codec);
411                                 return 00;
412                         }
413
414                         break;
415                 case CODEC_UNKNOWN:
416                 case CODEC_JPT:
417                 default:
418                         opj_free(l_codec);
419                         return 00;
420         }
421
422         opj_set_default_event_handler(&(l_codec->m_event_mgr));
423         return (opj_codec_t*) l_codec;
424 }
425
426 void OPJ_CALLCONV opj_set_default_decoder_parameters(opj_dparameters_t *parameters) {
427         if(parameters) {
428                 memset(parameters, 0, sizeof(opj_dparameters_t));
429                 /* default decoding parameters */
430                 parameters->cp_layer = 0;
431                 parameters->cp_reduce = 0;
432                 parameters->cp_limit_decoding = NO_LIMITATION;
433
434                 parameters->decod_format = -1;
435                 parameters->cod_format = -1;
436                 parameters->flags = 0;          
437 /* UniPG>> */
438 #ifdef USE_JPWL
439                 parameters->jpwl_correct = OPJ_FALSE;
440                 parameters->jpwl_exp_comps = JPWL_EXPECTED_COMPONENTS;
441                 parameters->jpwl_max_tiles = JPWL_MAXIMUM_TILES;
442 #endif /* USE_JPWL */
443 /* <<UniPG */
444         }
445 }
446
447 opj_bool OPJ_CALLCONV opj_setup_decoder(opj_codec_t *p_codec,
448                                         opj_dparameters_t *parameters 
449                                                                                 )
450 {
451         if (p_codec && parameters) { 
452                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
453
454                 if (! l_codec->is_decompressor) {
455                         opj_event_msg_v2(&(l_codec->m_event_mgr), EVT_ERROR, "Codec provided to the opj_setup_decoder function is not a decompressor handler.\n");
456                         return OPJ_FALSE;
457                 }
458
459                 l_codec->m_codec_data.m_decompression.opj_setup_decoder(l_codec->m_codec,
460                                                                                                                                 parameters);
461                 return OPJ_TRUE;
462         }
463         return OPJ_FALSE;
464 }
465
466 opj_bool OPJ_CALLCONV opj_read_header ( opj_stream_t *p_stream,
467                                                                                 opj_codec_t *p_codec,
468                                                                                 opj_image_t **p_image )
469 {
470         if (p_codec && p_stream) {
471                 opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
472                 opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
473
474                 if(! l_codec->is_decompressor) {
475                         opj_event_msg_v2(&(l_codec->m_event_mgr), EVT_ERROR, "Codec provided to the opj_read_header function is not a decompressor handler.\n");
476                         return OPJ_FALSE;
477                 }
478
479                 return l_codec->m_codec_data.m_decompression.opj_read_header(   l_stream,
480                                                                                                                                                 l_codec->m_codec,
481                                                                                                                                                 p_image,
482                                                                                                                                                 &(l_codec->m_event_mgr) );
483         }
484
485         return OPJ_FALSE;
486 }
487
488 /*
489  *
490  *
491  */
492 opj_bool OPJ_CALLCONV opj_decode(   opj_codec_t *p_codec,
493                                     opj_stream_t *p_stream,
494                                     opj_image_t* p_image)
495 {
496         if (p_codec && p_stream) {
497                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
498                 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
499
500                 if (! l_codec->is_decompressor) {
501                         return OPJ_FALSE;
502                 }
503
504                 return l_codec->m_codec_data.m_decompression.opj_decode(l_codec->m_codec,
505                                                                                                                                 l_stream,
506                                                                                                                                 p_image,
507                                                                                                                                 &(l_codec->m_event_mgr) );
508         }
509
510         return OPJ_FALSE;
511 }
512
513
514 /**
515  * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
516  *
517  * @param       p_codec                 the jpeg2000 codec.
518  * @param       p_start_x               the left position of the rectangle to decode (in image coordinates).
519  * @param       p_end_x                 the right position of the rectangle to decode (in image coordinates).
520  * @param       p_start_y               the up position of the rectangle to decode (in image coordinates).
521  * @param       p_end_y                 the bottom position of the rectangle to decode (in image coordinates).
522  *
523  * @return      true                    if the area could be set.
524  */
525 opj_bool OPJ_CALLCONV opj_set_decode_area(      opj_codec_t *p_codec,
526                                                                                         opj_image_t* p_image,
527                                                                                         OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
528                                                                                         OPJ_INT32 p_end_x, OPJ_INT32 p_end_y
529                                                                                         )
530 {
531         if (p_codec) {
532                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
533                 
534                 if (! l_codec->is_decompressor) {
535                         return OPJ_FALSE;
536                 }
537
538                 return  l_codec->m_codec_data.m_decompression.opj_set_decode_area(      l_codec->m_codec,
539                                                                                                                                                         p_image,
540                                                                                                                                                         p_start_x, p_start_y,
541                                                                                                                                                         p_end_x, p_end_y,
542                                                                                                                                                         &(l_codec->m_event_mgr) );
543         }
544         return OPJ_FALSE;
545 }
546
547 /**
548  * Reads a tile header. This function is compulsory and allows one to know the size of the tile thta will be decoded.
549  * The user may need to refer to the image got by opj_read_header to understand the size being taken by the tile.
550  *
551  * @param       p_codec                 the jpeg2000 codec.
552  * @param       p_tile_index    pointer to a value that will hold the index of the tile being decoded, in case of success.
553  * @param       p_data_size             pointer to a value that will hold the maximum size of the decoded data, in case of success. In case
554  *                                                      of truncated codestreams, the actual number of bytes decoded may be lower. The computation of the size is the same
555  *                                                      as depicted in opj_write_tile.
556  * @param       p_tile_x0               pointer to a value that will hold the x0 pos of the tile (in the image).
557  * @param       p_tile_y0               pointer to a value that will hold the y0 pos of the tile (in the image).
558  * @param       p_tile_x1               pointer to a value that will hold the x1 pos of the tile (in the image).
559  * @param       p_tile_y1               pointer to a value that will hold the y1 pos of the tile (in the image).
560  * @param       p_nb_comps              pointer to a value that will hold the number of components in the tile.
561  * @param       p_should_go_on  pointer to a boolean that will hold the fact that the decoding should go on. In case the
562  *                                                      codestream is over at the time of the call, the value will be set to false. The user should then stop
563  *                                                      the decoding.
564  * @param       p_stream                the stream to decode.
565  * @return      true                    if the tile header could be decoded. In case the decoding should end, the returned value is still true.
566  *                                                      returning false may be the result of a shortage of memory or an internal error.
567  */
568 opj_bool OPJ_CALLCONV opj_read_tile_header(     opj_codec_t *p_codec,
569                                                                                         opj_stream_t * p_stream,
570                                                                                         OPJ_UINT32 * p_tile_index,
571                                                                                         OPJ_UINT32 * p_data_size,
572                                                                                         OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
573                                                                                         OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
574                                                                                         OPJ_UINT32 * p_nb_comps,
575                                                                                         opj_bool * p_should_go_on)
576 {
577         if (p_codec && p_stream && p_data_size && p_tile_index) {
578                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
579                 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
580
581                 if (! l_codec->is_decompressor) {
582                         return OPJ_FALSE;
583                 }
584
585                 return l_codec->m_codec_data.m_decompression.opj_read_tile_header(      l_codec->m_codec,
586                                                                                                                                                         p_tile_index,
587                                                                                                                                                         p_data_size,
588                                                                                                                                                         p_tile_x0, p_tile_y0,
589                                                                                                                                                         p_tile_x1, p_tile_y1,
590                                                                                                                                                         p_nb_comps,
591                                                                                                                                                         p_should_go_on,
592                                                                                                                                                         l_stream,
593                                                                                                                                                         &(l_codec->m_event_mgr));
594         }
595         return OPJ_FALSE;
596 }
597
598 /**
599  * Reads a tile data. This function is compulsory and allows one to decode tile data. opj_read_tile_header should be called before.
600  * The user may need to refer to the image got by opj_read_header to understand the size being taken by the tile.
601  *
602  * @param       p_codec                 the jpeg2000 codec.
603  * @param       p_tile_index    the index of the tile being decoded, this should be the value set by opj_read_tile_header.
604  * @param       p_data                  pointer to a memory block that will hold the decoded data.
605  * @param       p_data_size             size of p_data. p_data_size should be bigger or equal to the value set by opj_read_tile_header.
606  * @param       p_stream                the stream to decode.
607  *
608  * @return      true                    if the data could be decoded.
609  */
610 opj_bool OPJ_CALLCONV opj_decode_tile_data(     opj_codec_t *p_codec,
611                                                                                         OPJ_UINT32 p_tile_index,
612                                                                                         OPJ_BYTE * p_data,
613                                                                                         OPJ_UINT32 p_data_size,
614                                                                                         opj_stream_t *p_stream
615                                                                                         )
616 {
617         if (p_codec && p_data && p_stream) {
618                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
619                 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
620
621                 if (! l_codec->is_decompressor) {
622                         return OPJ_FALSE;
623                 }
624
625                 return l_codec->m_codec_data.m_decompression.opj_decode_tile_data(      l_codec->m_codec,
626                                                                                                                                                         p_tile_index,
627                                                                                                                                                         p_data,
628                                                                                                                                                         p_data_size,
629                                                                                                                                                         l_stream,
630                                                                                                                                                         &(l_codec->m_event_mgr) );
631         }
632         return OPJ_FALSE;
633 }
634
635 /*
636  *
637  *
638  */
639 opj_bool OPJ_CALLCONV opj_get_decoded_tile(     opj_codec_t *p_codec,
640                                                                                         opj_stream_t *p_stream,
641                                                                                         opj_image_t *p_image,
642                                                                                         OPJ_UINT32 tile_index)
643 {
644         if (p_codec && p_stream) {
645                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
646                 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
647
648                 if (! l_codec->is_decompressor) {
649                         return OPJ_FALSE;
650                 }
651                 
652                 return l_codec->m_codec_data.m_decompression.opj_get_decoded_tile(      l_codec->m_codec,
653                                                                                                                                                         l_stream,
654                                                                                                                                                         p_image,
655                                                                                                                                                         &(l_codec->m_event_mgr),
656                                                                                                                                                         tile_index);
657         }
658
659         return OPJ_FALSE;
660 }
661
662 /*
663  *
664  *
665  */
666 opj_bool OPJ_CALLCONV opj_set_decoded_resolution_factor(opj_codec_t *p_codec, 
667                                                                                                                 OPJ_UINT32 res_factor )
668 {
669         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
670
671         if ( !l_codec ){
672                 fprintf(stderr, "[ERROR] Input parameters of the setup_decoder function are incorrect.\n");
673                 return OPJ_FALSE;
674         }
675
676         l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor(l_codec->m_codec, 
677                                                                                                                                                         res_factor,
678                                                                                                                                                         &(l_codec->m_event_mgr) );
679         return OPJ_TRUE;
680 }
681
682 /* ---------------------------------------------------------------------- */
683 /* COMPRESSION FUNCTIONS*/
684
685 opj_codec_t* OPJ_CALLCONV opj_create_compress(OPJ_CODEC_FORMAT p_format)
686 {
687         opj_codec_private_t *l_codec = 00;
688
689         l_codec = (opj_codec_private_t*)opj_calloc(1, sizeof(opj_codec_private_t));
690         if (!l_codec) {
691                 return 00;
692         }
693         memset(l_codec, 0, sizeof(opj_codec_private_t));
694         
695         l_codec->is_decompressor = 0;
696
697         switch(p_format) {
698                 case CODEC_J2K:
699                         l_codec->m_codec_data.m_compression.opj_encode = (opj_bool (*) (void *,
700                                                                                                                                                         struct opj_stream_private *,
701                                                                                                                                                         struct opj_event_mgr * )) j2k_encode_v2;
702
703                         l_codec->m_codec_data.m_compression.opj_end_compress = (opj_bool (*) (  void *,
704                                                                                                                                                                         struct opj_stream_private *,
705                                                                                                                                                                         struct opj_event_mgr *)) j2k_end_compress;
706
707                         l_codec->m_codec_data.m_compression.opj_start_compress = (opj_bool (*) (void *,
708                                                                                                                                                                         struct opj_stream_private *,
709                                                                                                                                                                         struct opj_image * ,
710                                                                                                                                                                         struct opj_event_mgr *)) j2k_start_compress;
711
712                         l_codec->m_codec_data.m_compression.opj_write_tile = (opj_bool (*) (void *,
713                                                                                                                                                                 OPJ_UINT32,
714                                                                                                                                                                 OPJ_BYTE*,
715                                                                                                                                                                 OPJ_UINT32,
716                                                                                                                                                                 struct opj_stream_private *,
717                                                                                                                                                                 struct opj_event_mgr *) ) j2k_write_tile;
718
719                         l_codec->m_codec_data.m_compression.opj_destroy = (void (*) (void *)) j2k_destroy;
720
721                         l_codec->m_codec_data.m_compression.opj_setup_encoder = (void (*) (     void *,
722                                                                                                                                                                 opj_cparameters_t *,
723                                                                                                                                                                 struct opj_image *,
724                                                                                                                                                                 struct opj_event_mgr * )) j2k_setup_encoder_v2;
725
726                         l_codec->m_codec = j2k_create_compress_v2();
727                         if (! l_codec->m_codec) {
728                                 opj_free(l_codec);
729                                 return 00;
730                         }
731
732                         break;
733
734                 case CODEC_JP2:
735                         /* get a JP2 decoder handle */
736                         l_codec->m_codec_data.m_compression.opj_encode = (opj_bool (*) (void *,
737                                                                                                                                                         struct opj_stream_private *,
738                                                                                                                                                         struct opj_event_mgr * )) opj_jp2_encode_v2;
739
740                         l_codec->m_codec_data.m_compression.opj_end_compress = (opj_bool (*) (  void *,
741                                                                                                                                                                         struct opj_stream_private *,
742                                                                                                                                                                         struct opj_event_mgr *)) jp2_end_compress;
743
744                         l_codec->m_codec_data.m_compression.opj_start_compress = (opj_bool (*) (void *,
745                                                                                                                                                                         struct opj_stream_private *,
746                                                                                                                                                                         struct opj_image * ,
747                                                                                                                                                                         struct opj_event_mgr *))  jp2_start_compress;
748
749                         l_codec->m_codec_data.m_compression.opj_write_tile = (opj_bool (*) (void *,
750                                                                                                                                                                 OPJ_UINT32,
751                                                                                                                                                                 OPJ_BYTE*,
752                                                                                                                                                                 OPJ_UINT32,
753                                                                                                                                                                 struct opj_stream_private *,
754                                                                                                                                                                 struct opj_event_mgr *)) jp2_write_tile;
755
756                         l_codec->m_codec_data.m_compression.opj_destroy = (void (*) (void *)) jp2_destroy;
757
758                         l_codec->m_codec_data.m_compression.opj_setup_encoder = (void (*) (     void *,
759                                                                                                                                                                 opj_cparameters_t *,
760                                                                                                                                                                 struct opj_image *,
761                                                                                                                                                                 struct opj_event_mgr * )) jp2_setup_encoder;
762
763                         l_codec->m_codec = jp2_create(OPJ_FALSE);
764                         if (! l_codec->m_codec) {
765                                 opj_free(l_codec);
766                                 return 00;
767                         }
768
769                         break;
770
771                 case CODEC_UNKNOWN:
772                 case CODEC_JPT:
773                 default:
774                         opj_free(l_codec);
775                         return 00;
776         }
777
778         opj_set_default_event_handler(&(l_codec->m_event_mgr));
779         return (opj_codec_t*) l_codec;
780 }
781
782 void OPJ_CALLCONV opj_set_default_encoder_parameters(opj_cparameters_t *parameters) {
783         if(parameters) {
784                 memset(parameters, 0, sizeof(opj_cparameters_t));
785                 /* default coding parameters */
786                 parameters->cp_cinema = OFF; 
787                 parameters->max_comp_size = 0;
788                 parameters->numresolution = 6;
789                 parameters->cp_rsiz = STD_RSIZ;
790                 parameters->cblockw_init = 64;
791                 parameters->cblockh_init = 64;
792                 parameters->prog_order = LRCP;
793                 parameters->roi_compno = -1;            /* no ROI */
794                 parameters->subsampling_dx = 1;
795                 parameters->subsampling_dy = 1;
796                 parameters->tp_on = 0;
797                 parameters->decod_format = -1;
798                 parameters->cod_format = -1;
799                 parameters->tcp_rates[0] = 0;   
800                 parameters->tcp_numlayers = 0;
801                 parameters->cp_disto_alloc = 0;
802                 parameters->cp_fixed_alloc = 0;
803                 parameters->cp_fixed_quality = 0;
804                 parameters->jpip_on = OPJ_FALSE;
805 /* UniPG>> */
806 #ifdef USE_JPWL
807                 parameters->jpwl_epc_on = OPJ_FALSE;
808                 parameters->jpwl_hprot_MH = -1; /* -1 means unassigned */
809                 {
810                         int i;
811                         for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
812                                 parameters->jpwl_hprot_TPH_tileno[i] = -1; /* unassigned */
813                                 parameters->jpwl_hprot_TPH[i] = 0; /* absent */
814                         }
815                 };
816                 {
817                         int i;
818                         for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
819                                 parameters->jpwl_pprot_tileno[i] = -1; /* unassigned */
820                                 parameters->jpwl_pprot_packno[i] = -1; /* unassigned */
821                                 parameters->jpwl_pprot[i] = 0; /* absent */
822                         }
823                 };
824                 parameters->jpwl_sens_size = 0; /* 0 means no ESD */
825                 parameters->jpwl_sens_addr = 0; /* 0 means auto */
826                 parameters->jpwl_sens_range = 0; /* 0 means packet */
827                 parameters->jpwl_sens_MH = -1; /* -1 means unassigned */
828                 {
829                         int i;
830                         for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
831                                 parameters->jpwl_sens_TPH_tileno[i] = -1; /* unassigned */
832                                 parameters->jpwl_sens_TPH[i] = -1; /* absent */
833                         }
834                 };
835 #endif /* USE_JPWL */
836 /* <<UniPG */
837         }
838 }
839
840 opj_bool OPJ_CALLCONV opj_setup_encoder(opj_codec_t *p_codec, 
841                                                                                 opj_cparameters_t *parameters, 
842                                                                                 opj_image_t *p_image)
843 {
844         if (p_codec && parameters && p_image) {
845                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
846
847                 if (! l_codec->is_decompressor) {
848                         l_codec->m_codec_data.m_compression.opj_setup_encoder(  l_codec->m_codec,
849                                                                                                                                         parameters,
850                                                                                                                                         p_image,
851                                                                                                                                         &(l_codec->m_event_mgr) );
852                         return OPJ_TRUE;
853                 }
854         }
855
856         return OPJ_FALSE;
857 }
858
859 opj_bool OPJ_CALLCONV opj_start_compress (      opj_codec_t *p_codec,
860                                                                                         opj_image_t * p_image,
861                                                                                         opj_stream_t *p_stream)
862 {
863         if (p_codec && p_stream) {
864                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
865                 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
866
867                 if (! l_codec->is_decompressor) {
868                         return l_codec->m_codec_data.m_compression.opj_start_compress(  l_codec->m_codec,
869                                                                                                                                                         l_stream,
870                                                                                                                                                         p_image,
871                                                                                                                                                         &(l_codec->m_event_mgr));
872                 }
873         }
874
875         return OPJ_FALSE;
876 }
877
878 opj_bool OPJ_CALLCONV opj_encode(opj_codec_t *p_info, opj_stream_t *p_stream)
879 {
880         if (p_info && p_stream) {
881                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_info;
882                 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
883
884                 if (! l_codec->is_decompressor) {
885                         l_codec->m_codec_data.m_compression.opj_encode( l_codec->m_codec,
886                                                                                                                         l_stream,
887                                                                                                                         &(l_codec->m_event_mgr));
888                         return OPJ_TRUE;
889                 }
890         }
891
892         return OPJ_FALSE;
893
894 }
895
896 opj_bool OPJ_CALLCONV opj_end_compress (opj_codec_t *p_codec,
897                                                                                 opj_stream_t *p_stream)
898 {
899         if (p_codec && p_stream) {
900                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
901                 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
902
903                 if (! l_codec->is_decompressor) {
904                         return l_codec->m_codec_data.m_compression.opj_end_compress(l_codec->m_codec,
905                                                                                                                                                 l_stream,
906                                                                                                                                                 &(l_codec->m_event_mgr));
907                 }
908         }
909         return OPJ_FALSE;
910
911 }
912
913 /*
914  *
915  *
916  */
917 opj_bool OPJ_CALLCONV opj_end_decompress (      opj_codec_t *p_codec,
918                                                                                         opj_stream_t *p_stream)
919 {
920         if (p_codec && p_stream) {
921                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
922                 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
923
924                 if (! l_codec->is_decompressor) {
925                         return OPJ_FALSE;
926                 }
927                 
928                 return l_codec->m_codec_data.m_decompression.opj_end_decompress(l_codec->m_codec,
929                                                                                                                                                 l_stream,
930                                                                                                                                                 &(l_codec->m_event_mgr) );
931         }
932
933         return OPJ_FALSE;
934 }
935
936
937 opj_bool OPJ_CALLCONV opj_set_MCT(opj_cparameters_t *parameters,OPJ_FLOAT32 * pEncodingMatrix,OPJ_INT32 * p_dc_shift,OPJ_UINT32 pNbComp)
938 {
939         OPJ_UINT32 l_matrix_size = pNbComp * pNbComp * sizeof(OPJ_FLOAT32);
940         OPJ_UINT32 l_dc_shift_size = pNbComp * sizeof(OPJ_INT32);
941         OPJ_UINT32 l_mct_total_size = l_matrix_size + l_dc_shift_size;
942
943         /* add MCT capability */
944         int rsiz = (int)parameters->cp_rsiz | (int)MCT;
945         parameters->cp_rsiz = (OPJ_RSIZ_CAPABILITIES)rsiz;
946         parameters->irreversible = 1;
947
948         /* use array based MCT */
949         parameters->tcp_mct = 2;
950         parameters->mct_data = opj_malloc(l_mct_total_size);
951         if (! parameters->mct_data) {
952                 return OPJ_FALSE;
953         }
954
955         memcpy(parameters->mct_data,pEncodingMatrix,l_matrix_size);
956         memcpy(((OPJ_BYTE *) parameters->mct_data) +  l_matrix_size,p_dc_shift,l_dc_shift_size);
957
958         return OPJ_TRUE;
959 }
960
961 /**
962  * Writes a tile with the given data.
963  *
964  * @param       p_compressor            the jpeg2000 codec.
965  * @param       p_tile_index            the index of the tile to write. At the moment, the tiles must be written from 0 to n-1 in sequence.
966  * @param       p_data                          pointer to the data to write. Data is arranged in sequence, data_comp0, then data_comp1, then ... NO INTERLEAVING should be set.
967  * @param       p_data_size                     this value os used to make sure the data being written is correct. The size must be equal to the sum for each component of tile_width * tile_height * component_size. component_size can be 1,2 or 4 bytes,
968  *                                                              depending on the precision of the given component.
969  * @param       p_stream                        the stream to write data to.
970  */
971 opj_bool OPJ_CALLCONV opj_write_tile (  opj_codec_t *p_codec,
972                                                                                 OPJ_UINT32 p_tile_index,
973                                                                                 OPJ_BYTE * p_data,
974                                                                                 OPJ_UINT32 p_data_size,
975                                                                                 opj_stream_t *p_stream )
976 {
977         if (p_codec && p_stream && p_data) {
978                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
979                 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
980
981                 if (l_codec->is_decompressor) {
982                         return OPJ_FALSE;
983                 }
984
985                 return l_codec->m_codec_data.m_compression.opj_write_tile(      l_codec->m_codec,
986                                                                                                                                         p_tile_index,
987                                                                                                                                         p_data,
988                                                                                                                                         p_data_size,
989                                                                                                                                         l_stream,
990                                                                                                                                         &(l_codec->m_event_mgr) );
991         }
992
993         return OPJ_FALSE;
994 }
995
996 /* ---------------------------------------------------------------------- */
997
998 void OPJ_CALLCONV opj_destroy_codec(opj_codec_t *p_codec)
999 {
1000         if (p_codec) {
1001                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
1002
1003                 if (l_codec->is_decompressor) {
1004                         l_codec->m_codec_data.m_decompression.opj_destroy(l_codec->m_codec);
1005                 }
1006                 else {
1007                         l_codec->m_codec_data.m_compression.opj_destroy(l_codec->m_codec);
1008                 }
1009
1010                 l_codec->m_codec = 00;
1011                 opj_free(l_codec);
1012         }
1013 }
1014
1015 /* ---------------------------------------------------------------------- */
1016
1017 /*
1018  *
1019  *
1020  */
1021 void OPJ_CALLCONV opj_dump_codec(       opj_codec_t *p_codec,
1022                                                                         OPJ_INT32 info_flag,
1023                                                                         FILE* output_stream)
1024 {
1025         if (p_codec) {
1026                 opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
1027
1028                 l_codec->opj_dump_codec(l_codec->m_codec, info_flag, output_stream);
1029                 return;
1030         }
1031
1032         fprintf(stderr, "[ERROR] Input parameter of the dump_codec function are incorrect.\n");
1033         return;
1034 }
1035
1036 /*
1037  *
1038  *
1039  */
1040 opj_codestream_info_v2_t* OPJ_CALLCONV opj_get_cstr_info(opj_codec_t *p_codec)
1041 {
1042         if (p_codec) {
1043                 opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
1044
1045                 return l_codec->opj_get_codec_info(l_codec->m_codec);
1046         }
1047
1048         return NULL;
1049 }
1050
1051 /*
1052  *
1053  *
1054  */
1055 void OPJ_CALLCONV opj_destroy_cstr_info(opj_codestream_info_v2_t **cstr_info) {
1056         if (cstr_info) {
1057
1058                 if ((*cstr_info)->m_default_tile_info.tccp_info){
1059                         opj_free((*cstr_info)->m_default_tile_info.tccp_info);
1060                 }
1061
1062                 if ((*cstr_info)->tile_info){
1063                         /* FIXME not used for the moment*/
1064                 }
1065
1066                 opj_free((*cstr_info));
1067                 (*cstr_info) = NULL;
1068         }
1069 }
1070
1071 /*
1072  *
1073  *
1074  */
1075 opj_codestream_index_t * OPJ_CALLCONV opj_get_cstr_index(opj_codec_t *p_codec)
1076 {
1077         if (p_codec) {
1078                 opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
1079
1080                 return l_codec->opj_get_codec_index(l_codec->m_codec);
1081         }
1082
1083         return NULL;
1084 }
1085
1086 /*
1087  *
1088  *
1089  */
1090 void OPJ_CALLCONV opj_destroy_cstr_index(opj_codestream_index_t **p_cstr_index)
1091 {
1092         if (*p_cstr_index){
1093                 j2k_destroy_cstr_index(*p_cstr_index);
1094                 (*p_cstr_index) = NULL;
1095         }
1096 }
1097
1098 /* ---------------------------------------------------------------------- */
1099 /**
1100  * Helper function.
1101  * Sets the stream to be a file stream. The FILE must have been open previously.
1102  * @param               p_stream        the stream to modify
1103  * @param               p_file          handler to an already open file.
1104 */
1105 opj_stream_t* OPJ_CALLCONV opj_stream_create_default_file_stream (FILE * p_file, opj_bool p_is_read_stream)
1106 {
1107         return opj_stream_create_file_stream(p_file,J2K_STREAM_CHUNK_SIZE,p_is_read_stream);
1108 }
1109
1110 opj_stream_t* OPJ_CALLCONV opj_stream_create_file_stream (      FILE * p_file, 
1111                                                                                                                         OPJ_SIZE_T p_size, 
1112                                                                                                                         opj_bool p_is_read_stream)
1113 {
1114         opj_stream_t* l_stream = 00;
1115
1116         if (! p_file) {
1117                 return NULL;
1118         }
1119
1120         l_stream = opj_stream_create(p_size,p_is_read_stream);
1121         if (! l_stream) {
1122                 return NULL;
1123         }
1124
1125         opj_stream_set_user_data(l_stream, p_file);
1126         opj_stream_set_user_data_length(l_stream, opj_get_data_length_from_file(p_file));
1127         opj_stream_set_read_function(l_stream, (opj_stream_read_fn) opj_read_from_file);
1128         opj_stream_set_write_function(l_stream, (opj_stream_write_fn) opj_write_from_file);
1129         opj_stream_set_skip_function(l_stream, (opj_stream_skip_fn) opj_skip_from_file);
1130         opj_stream_set_seek_function(l_stream, (opj_stream_seek_fn) opj_seek_from_file);
1131
1132         return l_stream;
1133 }