[trunk] style
[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                 opj_decompression_t m_decompression;
136                 opj_compression_t m_compression;
137     } m_codec_data;
138     /** FIXME DOC*/
139         void * m_codec;
140         /** Event handler */
141         opj_event_mgr_t m_event_mgr;
142         /** Flag to indicate if the codec is used to decode or encode*/
143         opj_bool is_decompressor;
144         void (*opj_dump_codec) (void * p_codec, OPJ_INT32 info_flag, FILE* output_stream);
145         opj_codestream_info_v2_t* (*opj_get_codec_info)(void* p_codec);
146         opj_codestream_index_t* (*opj_get_codec_index)(void* p_codec);
147 }
148 opj_codec_private_t;
149
150 /* ---------------------------------------------------------------------- */
151 /**
152  * Default callback function.
153  * Do nothing.
154  */
155 void opj_default_callback (const char *msg, void *client_data)
156 {
157 }
158
159 void set_default_event_handler(opj_event_mgr_t * p_manager)
160 {
161         p_manager->m_error_data = 00;
162         p_manager->m_warning_data = 00;
163         p_manager->m_info_data = 00;
164         p_manager->error_handler = opj_default_callback;
165         p_manager->info_handler = opj_default_callback;
166         p_manager->warning_handler = opj_default_callback;
167 }
168
169 opj_bool OPJ_CALLCONV opj_set_info_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.info_handler = p_callback;
179         l_codec->m_event_mgr.m_info_data = p_user_data;
180         
181         return OPJ_TRUE;
182 }
183
184 opj_bool OPJ_CALLCONV opj_set_warning_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.warning_handler = p_callback;
194         l_codec->m_event_mgr.m_warning_data = p_user_data;
195         
196         return OPJ_TRUE;
197 }
198
199 opj_bool OPJ_CALLCONV opj_set_error_handler(opj_codec_t * p_codec, 
200                                                                                         opj_msg_callback p_callback,
201                                                                                         void * p_user_data)
202 {
203         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
204         if (! l_codec) {
205                 return OPJ_FALSE;
206         }
207         
208         l_codec->m_event_mgr.error_handler = p_callback;
209         l_codec->m_event_mgr.m_error_data = p_user_data;
210         
211         return OPJ_TRUE;
212 }
213
214 /* ---------------------------------------------------------------------- */
215
216
217 OPJ_SIZE_T opj_read_from_file (void * p_buffer, OPJ_SIZE_T p_nb_bytes, FILE * p_file)
218 {
219         OPJ_SIZE_T l_nb_read = fread(p_buffer,1,p_nb_bytes,p_file);
220         return l_nb_read ? l_nb_read : -1;
221 }
222
223 OPJ_UINT64 opj_get_data_length_from_file (FILE * p_file)
224 {
225         OPJ_OFF_T file_length = 0;
226
227         OPJ_FSEEK(p_file, 0, SEEK_END);
228         file_length = (OPJ_UINT64)OPJ_FTELL(p_file);
229         OPJ_FSEEK(p_file, 0, SEEK_SET);
230
231         return file_length;
232 }
233
234 OPJ_SIZE_T opj_write_from_file (void * p_buffer, OPJ_SIZE_T p_nb_bytes, FILE * p_file)
235 {
236         return fwrite(p_buffer,1,p_nb_bytes,p_file);
237 }
238
239 OPJ_OFF_T opj_skip_from_file (OPJ_OFF_T p_nb_bytes, FILE * p_user_data)
240 {
241         if (OPJ_FSEEK(p_user_data,p_nb_bytes,SEEK_CUR)) {
242                 return -1;
243         }
244
245         return p_nb_bytes;
246 }
247
248 opj_bool opj_seek_from_file (OPJ_OFF_T p_nb_bytes, FILE * p_user_data)
249 {
250         if (OPJ_FSEEK(p_user_data,p_nb_bytes,SEEK_SET)) {
251                 return EXIT_FAILURE;
252         }
253
254         return EXIT_SUCCESS;
255 }
256
257 /* ---------------------------------------------------------------------- */
258 #ifdef _WIN32
259 #ifndef OPJ_STATIC
260 BOOL APIENTRY
261 DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
262
263         OPJ_ARG_NOT_USED(lpReserved);
264         OPJ_ARG_NOT_USED(hModule);
265
266         switch (ul_reason_for_call) {
267                 case DLL_PROCESS_ATTACH :
268                         break;
269                 case DLL_PROCESS_DETACH :
270                         break;
271                 case DLL_THREAD_ATTACH :
272                 case DLL_THREAD_DETACH :
273                         break;
274     }
275
276     return TRUE;
277 }
278 #endif /* OPJ_STATIC */
279 #endif /* _WIN32 */
280
281 /* ---------------------------------------------------------------------- */
282
283
284 const char* OPJ_CALLCONV opj_version(void) {
285     return PACKAGE_VERSION;
286 }
287
288
289 /* DEPRECATED */
290 opj_dinfo_t* OPJ_CALLCONV opj_create_decompress(OPJ_CODEC_FORMAT format) {
291         opj_dinfo_t *dinfo = (opj_dinfo_t*)opj_calloc(1, sizeof(opj_dinfo_t));
292         if(!dinfo) return NULL;
293         dinfo->is_decompressor = OPJ_TRUE;
294         switch(format) {
295                 case CODEC_J2K:
296                 case CODEC_JPT:
297                         /* get a J2K decoder handle */
298                         dinfo->j2k_handle = (void*)j2k_create_decompress((opj_common_ptr)dinfo);
299                         if(!dinfo->j2k_handle) {
300                                 opj_free(dinfo);
301                                 return NULL;
302                         }
303                         break;
304                 case CODEC_JP2:
305                         /* get a JP2 decoder handle */
306                         dinfo->jp2_handle = (void*)jp2_create_decompress((opj_common_ptr)dinfo);
307                         if(!dinfo->jp2_handle) {
308                                 opj_free(dinfo);
309                                 return NULL;
310                         }
311                         break;
312                 case CODEC_UNKNOWN:
313                 default:
314                         opj_free(dinfo);
315                         return NULL;
316         }
317
318         dinfo->codec_format = format;
319
320         return dinfo;
321 }
322
323 opj_codec_t* OPJ_CALLCONV opj_create_decompress_v2(OPJ_CODEC_FORMAT p_format)
324 {
325         opj_codec_private_t *l_codec = 00;
326
327         l_codec = (opj_codec_private_t*) opj_calloc(1, sizeof(opj_codec_private_t));
328         if (!l_codec){
329                 return 00;
330         }
331         memset(l_codec, 0, sizeof(opj_codec_private_t));
332
333         l_codec->is_decompressor = 1;
334
335         switch (p_format) {
336                 case CODEC_J2K:
337                         l_codec->opj_dump_codec = (void (*) (void*, OPJ_INT32, FILE*)) j2k_dump;
338
339                         l_codec->opj_get_codec_info = (opj_codestream_info_v2_t* (*) (void*) ) j2k_get_cstr_info;
340
341                         l_codec->opj_get_codec_index = (opj_codestream_index_t* (*) (void*) ) j2k_get_cstr_index;
342
343                         l_codec->m_codec_data.m_decompression.opj_decode =
344                                         (opj_bool (*) ( void *,
345                                                                         struct opj_stream_private *,
346                                                                         opj_image_t*, struct opj_event_mgr * )) j2k_decode_v2;
347
348                         l_codec->m_codec_data.m_decompression.opj_end_decompress =
349                                         (opj_bool (*) ( void *,
350                                                                         struct opj_stream_private *,
351                                                                         struct opj_event_mgr *)) j2k_end_decompress;
352
353                         l_codec->m_codec_data.m_decompression.opj_read_header =
354                                         (opj_bool (*) ( struct opj_stream_private *,
355                                                                         void *,
356                                                                         opj_image_t **,
357                                                                         struct opj_event_mgr * )) j2k_read_header;
358
359                         l_codec->m_codec_data.m_decompression.opj_destroy =
360                                         (void (*) (void *))j2k_destroy;
361
362                         l_codec->m_codec_data.m_decompression.opj_setup_decoder =
363                                         (void (*) (void * , opj_dparameters_t * )) j2k_setup_decoder_v2;
364
365                         l_codec->m_codec_data.m_decompression.opj_read_tile_header =
366                                         (opj_bool (*) ( void *,
367                                                                         OPJ_UINT32*,
368                                                                         OPJ_UINT32*,
369                                                                         OPJ_INT32*, OPJ_INT32*,
370                                                                         OPJ_INT32*, OPJ_INT32*,
371                                                                         OPJ_UINT32*,
372                                                                         opj_bool*,
373                                                                         struct opj_stream_private *,
374                                                                         struct opj_event_mgr * )) j2k_read_tile_header;
375
376                         l_codec->m_codec_data.m_decompression.opj_decode_tile_data =
377                                         (opj_bool (*) (void *, OPJ_UINT32, OPJ_BYTE*, OPJ_UINT32, struct opj_stream_private *, struct opj_event_mgr *)) j2k_decode_tile;
378
379                         l_codec->m_codec_data.m_decompression.opj_set_decode_area =
380                                         (opj_bool (*) (void *, opj_image_t*, OPJ_INT32, OPJ_INT32, OPJ_INT32, OPJ_INT32, struct opj_event_mgr *)) j2k_set_decode_area;
381
382                         l_codec->m_codec_data.m_decompression.opj_get_decoded_tile = (opj_bool (*) (void *p_codec,
383                                                                                                                                                                                 opj_stream_private_t *p_cio,
384                                                                                                                                                                                 opj_image_t *p_image,
385                                                                                                                                                                                 struct opj_event_mgr * p_manager,
386                                                                                                                                                                                 OPJ_UINT32 tile_index)) j2k_get_tile;
387
388                         l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor = (opj_bool (*) (void * p_codec,
389                                                                                                                                                                                                         OPJ_UINT32 res_factor,
390                                                                                                                                                                                                         struct opj_event_mgr * p_manager)) j2k_set_decoded_resolution_factor;
391
392                         l_codec->m_codec = j2k_create_decompress_v2();
393
394                         if (! l_codec->m_codec) {
395                                 opj_free(l_codec);
396                                 return NULL;
397                         }
398
399                         break;
400
401                 case CODEC_JP2:
402                         /* get a JP2 decoder handle */
403                         l_codec->opj_dump_codec = (void (*) (void*, OPJ_INT32, FILE*)) jp2_dump;
404
405                         l_codec->opj_get_codec_info = (opj_codestream_info_v2_t* (*) (void*) ) jp2_get_cstr_info;
406
407                         l_codec->opj_get_codec_index = (opj_codestream_index_t* (*) (void*) ) jp2_get_cstr_index;
408
409                         l_codec->m_codec_data.m_decompression.opj_decode =
410                                         (opj_bool (*) ( void *,
411                                                                         struct opj_stream_private *,
412                                                                         opj_image_t*,
413                                                                         struct opj_event_mgr * )) jp2_decode_v2;
414
415                         l_codec->m_codec_data.m_decompression.opj_end_decompress =  (opj_bool (*) (void *,struct opj_stream_private *,struct opj_event_mgr *)) jp2_end_decompress;
416
417                         l_codec->m_codec_data.m_decompression.opj_read_header =  (opj_bool (*) (
418                                         struct opj_stream_private *,
419                                         void *,
420                                         opj_image_t **,
421                                         struct opj_event_mgr * )) jp2_read_header;
422
423                         l_codec->m_codec_data.m_decompression.opj_read_tile_header = ( opj_bool (*) (
424                                         void *,
425                                         OPJ_UINT32*,
426                                         OPJ_UINT32*,
427                                         OPJ_INT32*,
428                                         OPJ_INT32*,
429                                         OPJ_INT32 * ,
430                                         OPJ_INT32 * ,
431                                         OPJ_UINT32 * ,
432                                         opj_bool *,
433                                         struct opj_stream_private *,
434                                         struct opj_event_mgr * )) jp2_read_tile_header;
435
436                         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;
437
438                         l_codec->m_codec_data.m_decompression.opj_destroy = (void (*) (void *))jp2_destroy;
439
440                         l_codec->m_codec_data.m_decompression.opj_setup_decoder = (void (*) (void * ,opj_dparameters_t * )) jp2_setup_decoder_v2;
441
442                         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;
443
444                         l_codec->m_codec_data.m_decompression.opj_get_decoded_tile = (opj_bool (*) (void *p_codec,
445                                                                                                                                                                                 opj_stream_private_t *p_cio,
446                                                                                                                                                                                 opj_image_t *p_image,
447                                                                                                                                                                                 struct opj_event_mgr * p_manager,
448                                                                                                                                                                                 OPJ_UINT32 tile_index)) jp2_get_tile;
449
450                         l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor = (opj_bool (*) (void * p_codec,
451                                                                                                                                                                                                         OPJ_UINT32 res_factor,
452                                                                                                                                                                                                         opj_event_mgr_t * p_manager)) jp2_set_decoded_resolution_factor;
453
454                         l_codec->m_codec = jp2_create(OPJ_TRUE);
455
456                         if (! l_codec->m_codec) {
457                                 opj_free(l_codec);
458                                 return 00;
459                         }
460
461                         break;
462                 case CODEC_UNKNOWN:
463                 case CODEC_JPT:
464                 default:
465                         opj_free(l_codec);
466                         return 00;
467         }
468
469         set_default_event_handler(&(l_codec->m_event_mgr));
470         return (opj_codec_t*) l_codec;
471 }
472
473 /* DEPRECATED */
474 void OPJ_CALLCONV opj_destroy_decompress(opj_dinfo_t *dinfo) {
475         if(dinfo) {
476                 /* destroy the codec */
477                 switch(dinfo->codec_format) {
478                         case CODEC_J2K:
479                         case CODEC_JPT:
480                                 j2k_destroy_decompress((opj_j2k_t*)dinfo->j2k_handle);
481                                 break;
482                         case CODEC_JP2:
483                                 jp2_destroy_decompress((opj_jp2_t*)dinfo->jp2_handle);
484                                 break;
485                         case CODEC_UNKNOWN:
486                         default:
487                                 break;
488                 }
489                 /* destroy the decompressor */
490                 opj_free(dinfo);
491         }
492 }
493
494 void OPJ_CALLCONV opj_set_default_decoder_parameters(opj_dparameters_t *parameters) {
495         if(parameters) {
496                 memset(parameters, 0, sizeof(opj_dparameters_t));
497                 /* default decoding parameters */
498                 parameters->cp_layer = 0;
499                 parameters->cp_reduce = 0;
500                 parameters->cp_limit_decoding = NO_LIMITATION;
501
502                 parameters->decod_format = -1;
503                 parameters->cod_format = -1;
504                 parameters->flags = 0;          
505 /* UniPG>> */
506 #ifdef USE_JPWL
507                 parameters->jpwl_correct = OPJ_FALSE;
508                 parameters->jpwl_exp_comps = JPWL_EXPECTED_COMPONENTS;
509                 parameters->jpwl_max_tiles = JPWL_MAXIMUM_TILES;
510 #endif /* USE_JPWL */
511 /* <<UniPG */
512         }
513 }
514
515 /* DEPRECATED */
516 void OPJ_CALLCONV opj_setup_decoder(opj_dinfo_t *dinfo, opj_dparameters_t *parameters) {
517         if(dinfo && parameters) {
518                 switch(dinfo->codec_format) {
519                         case CODEC_J2K:
520                         case CODEC_JPT:
521                                 j2k_setup_decoder((opj_j2k_t*)dinfo->j2k_handle, parameters);
522                                 break;
523                         case CODEC_JP2:
524                                 jp2_setup_decoder((opj_jp2_t*)dinfo->jp2_handle, parameters);
525                                 break;
526                         case CODEC_UNKNOWN:
527                         default:
528                                 break;
529                 }
530         }
531 }
532
533 opj_bool OPJ_CALLCONV opj_setup_decoder_v2(     opj_codec_t *p_codec, 
534                                                                                         opj_dparameters_t *parameters 
535                                                                                         )
536 {
537         if (p_codec && parameters) { 
538                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
539
540                 if (! l_codec->is_decompressor) {
541                         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");
542                         return OPJ_FALSE;
543                 }
544
545                 l_codec->m_codec_data.m_decompression.opj_setup_decoder(l_codec->m_codec,
546                                                                                                                                 parameters);
547                 return OPJ_TRUE;
548         }
549         return OPJ_FALSE;
550 }
551
552 /* DEPRECATED */
553 opj_image_t* OPJ_CALLCONV opj_decode(opj_dinfo_t *dinfo, opj_cio_t *cio) {
554         return opj_decode_with_info(dinfo, cio, NULL);
555 }
556
557 /* DEPRECATED */
558 opj_image_t* OPJ_CALLCONV opj_decode_with_info(opj_dinfo_t *dinfo, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
559         if(dinfo && cio) {
560                 switch(dinfo->codec_format) {
561                         case CODEC_J2K:
562                                 return j2k_decode((opj_j2k_t*)dinfo->j2k_handle, cio, cstr_info);
563                         case CODEC_JPT:
564                                 return j2k_decode_jpt_stream((opj_j2k_t*)dinfo->j2k_handle, cio, cstr_info);
565                         case CODEC_JP2:
566                                 return opj_jp2_decode((opj_jp2_t*)dinfo->jp2_handle, cio, cstr_info);
567                         case CODEC_UNKNOWN:
568                         default:
569                                 break;
570                 }
571         }
572         return NULL;
573 }
574
575 /* DEPRECATED */
576 opj_cinfo_t* OPJ_CALLCONV opj_create_compress(OPJ_CODEC_FORMAT format) {
577         opj_cinfo_t *cinfo = (opj_cinfo_t*)opj_calloc(1, sizeof(opj_cinfo_t));
578         if(!cinfo) return NULL;
579         cinfo->is_decompressor = OPJ_FALSE;
580         switch(format) {
581                 case CODEC_J2K:
582                         /* get a J2K coder handle */
583                         cinfo->j2k_handle = (void*)j2k_create_compress((opj_common_ptr)cinfo);
584                         if(!cinfo->j2k_handle) {
585                                 opj_free(cinfo);
586                                 return NULL;
587                         }
588                         break;
589                 case CODEC_JP2:
590                         /* get a JP2 coder handle */
591                         cinfo->jp2_handle = (void*)jp2_create_compress((opj_common_ptr)cinfo);
592                         if(!cinfo->jp2_handle) {
593                                 opj_free(cinfo);
594                                 return NULL;
595                         }
596                         break;
597                 case CODEC_JPT:
598                 case CODEC_UNKNOWN:
599                 default:
600                         opj_free(cinfo);
601                         return NULL;
602         }
603
604         cinfo->codec_format = format;
605
606         return cinfo;
607 }
608
609
610 opj_codec_t* OPJ_CALLCONV opj_create_compress_v2(OPJ_CODEC_FORMAT p_format)
611 {
612         opj_codec_private_t *l_codec = 00;
613
614         l_codec = (opj_codec_private_t*)opj_calloc(1, sizeof(opj_codec_private_t));
615         if (!l_codec) {
616                 return 00;
617         }
618         memset(l_codec, 0, sizeof(opj_codec_private_t));
619         
620         l_codec->is_decompressor = 0;
621
622         switch(p_format) {
623                 case CODEC_J2K:
624                         l_codec->m_codec_data.m_compression.opj_encode = (opj_bool (*) (void *,
625                                                                                                                                                         struct opj_stream_private *,
626                                                                                                                                                         struct opj_event_mgr * )) j2k_encode_v2;
627
628                         l_codec->m_codec_data.m_compression.opj_end_compress = (opj_bool (*) (  void *,
629                                                                                                                                                                         struct opj_stream_private *,
630                                                                                                                                                                         struct opj_event_mgr *)) j2k_end_compress;
631
632                         l_codec->m_codec_data.m_compression.opj_start_compress = (opj_bool (*) (void *,
633                                                                                                                                                                         struct opj_stream_private *,
634                                                                                                                                                                         struct opj_image * ,
635                                                                                                                                                                         struct opj_event_mgr *)) j2k_start_compress;
636
637                         l_codec->m_codec_data.m_compression.opj_write_tile = (opj_bool (*) (void *,
638                                                                                                                                                                 OPJ_UINT32,
639                                                                                                                                                                 OPJ_BYTE*,
640                                                                                                                                                                 OPJ_UINT32,
641                                                                                                                                                                 struct opj_stream_private *,
642                                                                                                                                                                 struct opj_event_mgr *) ) j2k_write_tile;
643
644                         l_codec->m_codec_data.m_compression.opj_destroy = (void (*) (void *)) j2k_destroy;
645
646                         l_codec->m_codec_data.m_compression.opj_setup_encoder = (void (*) (     void *,
647                                                                                                                                                                 opj_cparameters_t *,
648                                                                                                                                                                 struct opj_image *,
649                                                                                                                                                                 struct opj_event_mgr * )) j2k_setup_encoder_v2;
650
651                         l_codec->m_codec = j2k_create_compress_v2();
652                         if (! l_codec->m_codec) {
653                                 opj_free(l_codec);
654                                 return 00;
655                         }
656
657                         break;
658
659                 case CODEC_JP2:
660                         /* get a JP2 decoder handle */
661                         l_codec->m_codec_data.m_compression.opj_encode = (opj_bool (*) (void *,
662                                                                                                                                                         struct opj_stream_private *,
663                                                                                                                                                         struct opj_event_mgr * )) opj_jp2_encode;
664
665                         l_codec->m_codec_data.m_compression.opj_end_compress = (opj_bool (*) (  void *,
666                                                                                                                                                                         struct opj_stream_private *,
667                                                                                                                                                                         struct opj_event_mgr *)) jp2_end_compress;
668
669                         l_codec->m_codec_data.m_compression.opj_start_compress = (opj_bool (*) (void *,
670                                                                                                                                                                         struct opj_stream_private *,
671                                                                                                                                                                         struct opj_image * ,
672                                                                                                                                                                         struct opj_event_mgr *))  jp2_start_compress;
673
674                         l_codec->m_codec_data.m_compression.opj_write_tile = (opj_bool (*) (void *,
675                                                                                                                                                                 OPJ_UINT32,
676                                                                                                                                                                 OPJ_BYTE*,
677                                                                                                                                                                 OPJ_UINT32,
678                                                                                                                                                                 struct opj_stream_private *,
679                                                                                                                                                                 struct opj_event_mgr *)) jp2_write_tile;
680
681                         l_codec->m_codec_data.m_compression.opj_destroy = (void (*) (void *)) jp2_destroy;
682
683                         l_codec->m_codec_data.m_compression.opj_setup_encoder = (void (*) (     void *,
684                                                                                                                                                                 opj_cparameters_t *,
685                                                                                                                                                                 struct opj_image *,
686                                                                                                                                                                 struct opj_event_mgr * )) jp2_setup_encoder;
687
688                         l_codec->m_codec = jp2_create(OPJ_FALSE);
689                         if (! l_codec->m_codec) {
690                                 opj_free(l_codec);
691                                 return 00;
692                         }
693
694                         break;
695
696                 case CODEC_UNKNOWN:
697                 case CODEC_JPT:
698                 default:
699                         opj_free(l_codec);
700                         return 00;
701         }
702
703         set_default_event_handler(&(l_codec->m_event_mgr));
704         return (opj_codec_t*) l_codec;
705 }
706
707 /* DEPRECATED */
708 void OPJ_CALLCONV opj_destroy_compress(opj_cinfo_t *cinfo) {
709         if(cinfo) {
710                 /* destroy the codec */
711                 switch(cinfo->codec_format) {
712                         case CODEC_J2K:
713                                 j2k_destroy_compress((opj_j2k_t*)cinfo->j2k_handle);
714                                 break;
715                         case CODEC_JP2:
716                                 jp2_destroy_compress((opj_jp2_t*)cinfo->jp2_handle);
717                                 break;
718                         case CODEC_JPT:
719                         case CODEC_UNKNOWN:
720                         default:
721                                 break;
722                 }
723                 /* destroy the decompressor */
724                 opj_free(cinfo);
725         }
726 }
727
728 void OPJ_CALLCONV opj_set_default_encoder_parameters(opj_cparameters_t *parameters) {
729         if(parameters) {
730                 memset(parameters, 0, sizeof(opj_cparameters_t));
731                 /* default coding parameters */
732                 parameters->cp_cinema = OFF; 
733                 parameters->max_comp_size = 0;
734                 parameters->numresolution = 6;
735                 parameters->cp_rsiz = STD_RSIZ;
736                 parameters->cblockw_init = 64;
737                 parameters->cblockh_init = 64;
738                 parameters->prog_order = LRCP;
739                 parameters->roi_compno = -1;            /* no ROI */
740                 parameters->subsampling_dx = 1;
741                 parameters->subsampling_dy = 1;
742                 parameters->tp_on = 0;
743                 parameters->decod_format = -1;
744                 parameters->cod_format = -1;
745                 parameters->tcp_rates[0] = 0;   
746                 parameters->tcp_numlayers = 0;
747                 parameters->cp_disto_alloc = 0;
748                 parameters->cp_fixed_alloc = 0;
749                 parameters->cp_fixed_quality = 0;
750                 parameters->jpip_on = OPJ_FALSE;
751 /* UniPG>> */
752 #ifdef USE_JPWL
753                 parameters->jpwl_epc_on = OPJ_FALSE;
754                 parameters->jpwl_hprot_MH = -1; /* -1 means unassigned */
755                 {
756                         int i;
757                         for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
758                                 parameters->jpwl_hprot_TPH_tileno[i] = -1; /* unassigned */
759                                 parameters->jpwl_hprot_TPH[i] = 0; /* absent */
760                         }
761                 };
762                 {
763                         int i;
764                         for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
765                                 parameters->jpwl_pprot_tileno[i] = -1; /* unassigned */
766                                 parameters->jpwl_pprot_packno[i] = -1; /* unassigned */
767                                 parameters->jpwl_pprot[i] = 0; /* absent */
768                         }
769                 };
770                 parameters->jpwl_sens_size = 0; /* 0 means no ESD */
771                 parameters->jpwl_sens_addr = 0; /* 0 means auto */
772                 parameters->jpwl_sens_range = 0; /* 0 means packet */
773                 parameters->jpwl_sens_MH = -1; /* -1 means unassigned */
774                 {
775                         int i;
776                         for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
777                                 parameters->jpwl_sens_TPH_tileno[i] = -1; /* unassigned */
778                                 parameters->jpwl_sens_TPH[i] = -1; /* absent */
779                         }
780                 };
781 #endif /* USE_JPWL */
782 /* <<UniPG */
783         }
784 }
785
786 /**
787  * Helper function.
788  * Sets the stream to be a file stream. The FILE must have been open previously.
789  * @param               p_stream        the stream to modify
790  * @param               p_file          handler to an already open file.
791 */
792 opj_stream_t* OPJ_CALLCONV opj_stream_create_default_file_stream (FILE * p_file, opj_bool p_is_read_stream)
793 {
794         return opj_stream_create_file_stream(p_file,J2K_STREAM_CHUNK_SIZE,p_is_read_stream);
795 }
796
797 opj_stream_t* OPJ_CALLCONV opj_stream_create_file_stream (      FILE * p_file, 
798                                                                                                                         OPJ_SIZE_T p_size, 
799                                                                                                                         opj_bool p_is_read_stream)
800 {
801         opj_stream_t* l_stream = 00;
802
803         if (! p_file) {
804                 return NULL;
805         }
806
807         l_stream = opj_stream_create(p_size,p_is_read_stream);
808         if (! l_stream) {
809                 return NULL;
810         }
811
812         opj_stream_set_user_data(l_stream, p_file);
813         opj_stream_set_user_data_length(l_stream, opj_get_data_length_from_file(p_file));
814         opj_stream_set_read_function(l_stream, (opj_stream_read_fn) opj_read_from_file);
815         opj_stream_set_write_function(l_stream, (opj_stream_write_fn) opj_write_from_file);
816         opj_stream_set_skip_function(l_stream, (opj_stream_skip_fn) opj_skip_from_file);
817         opj_stream_set_seek_function(l_stream, (opj_stream_seek_fn) opj_seek_from_file);
818
819         return l_stream;
820 }
821 /* DEPRECATED */
822 void OPJ_CALLCONV opj_setup_encoder(opj_cinfo_t *cinfo, opj_cparameters_t *parameters, opj_image_t *image) {
823         if(cinfo && parameters && image) {
824                 switch(cinfo->codec_format) {
825                         case CODEC_J2K:
826                                 j2k_setup_encoder((opj_j2k_t*)cinfo->j2k_handle, parameters, image);
827                                 break;
828                         case CODEC_JP2:
829                                 jp2_setup_encoder((opj_jp2_t*)cinfo->jp2_handle, parameters, image);
830                                 break;
831                         case CODEC_JPT:
832                         case CODEC_UNKNOWN:
833                         default:
834                                 break;
835                 }
836         }
837 }
838
839 opj_bool OPJ_CALLCONV opj_setup_encoder_v2(     opj_codec_t *p_codec, 
840                                                                                         opj_cparameters_t *parameters, 
841                                                                                         opj_image_t *p_image)
842 {
843         if (p_codec && parameters && p_image) {
844                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
845
846                 if (! l_codec->is_decompressor) {
847                         l_codec->m_codec_data.m_compression.opj_setup_encoder(  l_codec->m_codec,
848                                                                                                                                         parameters,
849                                                                                                                                         p_image,
850                                                                                                                                         &(l_codec->m_event_mgr) );
851                         return OPJ_TRUE;
852                 }
853         }
854
855         return OPJ_FALSE;
856 }
857
858 opj_bool OPJ_CALLCONV opj_start_compress (      opj_codec_t *p_codec,
859                                                                                         opj_image_t * p_image,
860                                                                                         opj_stream_t *p_stream)
861 {
862         if (p_codec && p_stream) {
863                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
864                 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
865
866                 if (! l_codec->is_decompressor) {
867                         return l_codec->m_codec_data.m_compression.opj_start_compress(  l_codec->m_codec,
868                                                                                                                                                         l_stream,
869                                                                                                                                                         p_image,
870                                                                                                                                                         &(l_codec->m_event_mgr));
871                 }
872         }
873
874         return OPJ_FALSE;
875 }
876
877 opj_bool OPJ_CALLCONV opj_encode_v2(opj_codec_t *p_info, opj_stream_t *p_stream)
878 {
879         if (p_info && p_stream) {
880                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_info;
881                 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
882
883                 if (! l_codec->is_decompressor) {
884                         l_codec->m_codec_data.m_compression.opj_encode( l_codec->m_codec,
885                                                                                                                         l_stream,
886                                                                                                                         &(l_codec->m_event_mgr));
887                         return OPJ_TRUE;
888                 }
889         }
890
891         return OPJ_FALSE;
892
893 }
894
895 opj_bool OPJ_CALLCONV opj_end_compress (opj_codec_t *p_codec,
896                                                                                 opj_stream_t *p_stream)
897 {
898         if (p_codec && p_stream) {
899                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
900                 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
901
902                 if (! l_codec->is_decompressor) {
903                         return l_codec->m_codec_data.m_compression.opj_end_compress(l_codec->m_codec,
904                                                                                                                                                 l_stream,
905                                                                                                                                                 &(l_codec->m_event_mgr));
906                 }
907         }
908         return OPJ_FALSE;
909
910 }
911
912 /* DEPRECATED */
913 opj_bool OPJ_CALLCONV opj_encode(opj_cinfo_t *cinfo, opj_cio_t *cio, opj_image_t *image, char *index) {
914         if (index != NULL)
915                 opj_event_msg((opj_common_ptr)cinfo, EVT_WARNING, "Set index to NULL when calling the opj_encode function.\n"
916                 "To extract the index, use the opj_encode_with_info() function.\n"
917                 "No index will be generated during this encoding\n");
918         return opj_encode_with_info(cinfo, cio, image, NULL);
919 }
920
921 /* DEPRECATED */
922 opj_bool OPJ_CALLCONV opj_encode_with_info(opj_cinfo_t *cinfo, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
923         if(cinfo && cio && image) {
924                 switch(cinfo->codec_format) {
925                         case CODEC_J2K:
926                                 return j2k_encode((opj_j2k_t*)cinfo->j2k_handle, cio, image, cstr_info);
927                         case CODEC_JP2:
928                                 return opj_jp2_encode((opj_jp2_t*)cinfo->jp2_handle, cio, image, cstr_info);        
929                         case CODEC_JPT:
930                         case CODEC_UNKNOWN:
931                         default:
932                                 break;
933                 }
934         }
935         return OPJ_FALSE;
936 }
937
938 /* DEPRECATED */
939 void OPJ_CALLCONV opj_destroy_cstr_info(opj_codestream_info_t *cstr_info) {
940         if (cstr_info) {
941                 int tileno;
942                 for (tileno = 0; tileno < cstr_info->tw * cstr_info->th; tileno++) {
943                         opj_tile_info_t *tile_info = &cstr_info->tile[tileno];
944                         opj_free(tile_info->thresh);
945                         opj_free(tile_info->packet);
946                         opj_free(tile_info->tp);
947                         opj_free(tile_info->marker);
948                 }
949                 opj_free(cstr_info->tile);
950                 opj_free(cstr_info->marker);
951                 opj_free(cstr_info->numdecompos);
952         }
953 }
954
955
956 opj_bool OPJ_CALLCONV opj_read_header ( opj_stream_t *p_stream,
957                                                                                 opj_codec_t *p_codec,
958                                                                                 opj_image_t **p_image )
959 {
960         if (p_codec && p_stream) {
961                 opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
962                 opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
963
964                 if(! l_codec->is_decompressor) {
965                         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");
966                         return OPJ_FALSE;
967                 }
968
969                 return l_codec->m_codec_data.m_decompression.opj_read_header(   l_stream,
970                                                                                                                                                 l_codec->m_codec,
971                                                                                                                                                 p_image,
972                                                                                                                                                 &(l_codec->m_event_mgr) );
973         }
974
975         return OPJ_FALSE;
976 }
977
978
979 void OPJ_CALLCONV opj_destroy_codec(opj_codec_t *p_codec)
980 {
981         if (p_codec) {
982                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
983
984                 if (l_codec->is_decompressor) {
985                         l_codec->m_codec_data.m_decompression.opj_destroy(l_codec->m_codec);
986                 }
987                 else {
988                         l_codec->m_codec_data.m_compression.opj_destroy(l_codec->m_codec);
989                 }
990
991                 l_codec->m_codec = 00;
992                 opj_free(l_codec);
993         }
994 }
995
996 /**
997  * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
998  *
999  * @param       p_codec                 the jpeg2000 codec.
1000  * @param       p_start_x               the left position of the rectangle to decode (in image coordinates).
1001  * @param       p_end_x                 the right position of the rectangle to decode (in image coordinates).
1002  * @param       p_start_y               the up position of the rectangle to decode (in image coordinates).
1003  * @param       p_end_y                 the bottom position of the rectangle to decode (in image coordinates).
1004  *
1005  * @return      true                    if the area could be set.
1006  */
1007 opj_bool OPJ_CALLCONV opj_set_decode_area(      opj_codec_t *p_codec,
1008                                                                                         opj_image_t* p_image,
1009                                                                                         OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
1010                                                                                         OPJ_INT32 p_end_x, OPJ_INT32 p_end_y
1011                                                                                         )
1012 {
1013         if (p_codec) {
1014                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
1015                 
1016                 if (! l_codec->is_decompressor) {
1017                         return OPJ_FALSE;
1018                 }
1019
1020                 return  l_codec->m_codec_data.m_decompression.opj_set_decode_area(      l_codec->m_codec,
1021                                                                                                                                                         p_image,
1022                                                                                                                                                         p_start_x, p_start_y,
1023                                                                                                                                                         p_end_x, p_end_y,
1024                                                                                                                                                         &(l_codec->m_event_mgr) );
1025         }
1026         return OPJ_FALSE;
1027 }
1028
1029 /**
1030  * Reads a tile header. This function is compulsory and allows one to know the size of the tile thta will be decoded.
1031  * The user may need to refer to the image got by opj_read_header to understand the size being taken by the tile.
1032  *
1033  * @param       p_codec                 the jpeg2000 codec.
1034  * @param       p_tile_index    pointer to a value that will hold the index of the tile being decoded, in case of success.
1035  * @param       p_data_size             pointer to a value that will hold the maximum size of the decoded data, in case of success. In case
1036  *                                                      of truncated codestreams, the actual number of bytes decoded may be lower. The computation of the size is the same
1037  *                                                      as depicted in opj_write_tile.
1038  * @param       p_tile_x0               pointer to a value that will hold the x0 pos of the tile (in the image).
1039  * @param       p_tile_y0               pointer to a value that will hold the y0 pos of the tile (in the image).
1040  * @param       p_tile_x1               pointer to a value that will hold the x1 pos of the tile (in the image).
1041  * @param       p_tile_y1               pointer to a value that will hold the y1 pos of the tile (in the image).
1042  * @param       p_nb_comps              pointer to a value that will hold the number of components in the tile.
1043  * @param       p_should_go_on  pointer to a boolean that will hold the fact that the decoding should go on. In case the
1044  *                                                      codestream is over at the time of the call, the value will be set to false. The user should then stop
1045  *                                                      the decoding.
1046  * @param       p_stream                the stream to decode.
1047  * @return      true                    if the tile header could be decoded. In case the decoding should end, the returned value is still true.
1048  *                                                      returning false may be the result of a shortage of memory or an internal error.
1049  */
1050 opj_bool OPJ_CALLCONV opj_read_tile_header(     opj_codec_t *p_codec,
1051                                                                                         opj_stream_t * p_stream,
1052                                                                                         OPJ_UINT32 * p_tile_index,
1053                                                                                         OPJ_UINT32 * p_data_size,
1054                                                                                         OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
1055                                                                                         OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
1056                                                                                         OPJ_UINT32 * p_nb_comps,
1057                                                                                         opj_bool * p_should_go_on)
1058 {
1059         if (p_codec && p_stream && p_data_size && p_tile_index) {
1060                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
1061                 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
1062
1063                 if (! l_codec->is_decompressor) {
1064                         return OPJ_FALSE;
1065                 }
1066
1067                 return l_codec->m_codec_data.m_decompression.opj_read_tile_header(      l_codec->m_codec,
1068                                                                                                                                                         p_tile_index,
1069                                                                                                                                                         p_data_size,
1070                                                                                                                                                         p_tile_x0, p_tile_y0,
1071                                                                                                                                                         p_tile_x1, p_tile_y1,
1072                                                                                                                                                         p_nb_comps,
1073                                                                                                                                                         p_should_go_on,
1074                                                                                                                                                         l_stream,
1075                                                                                                                                                         &(l_codec->m_event_mgr));
1076         }
1077         return OPJ_FALSE;
1078 }
1079
1080 /**
1081  * Reads a tile data. This function is compulsory and allows one to decode tile data. opj_read_tile_header should be called before.
1082  * The user may need to refer to the image got by opj_read_header to understand the size being taken by the tile.
1083  *
1084  * @param       p_codec                 the jpeg2000 codec.
1085  * @param       p_tile_index    the index of the tile being decoded, this should be the value set by opj_read_tile_header.
1086  * @param       p_data                  pointer to a memory block that will hold the decoded data.
1087  * @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.
1088  * @param       p_stream                the stream to decode.
1089  *
1090  * @return      true                    if the data could be decoded.
1091  */
1092 opj_bool OPJ_CALLCONV opj_decode_tile_data(     opj_codec_t *p_codec,
1093                                                                                         OPJ_UINT32 p_tile_index,
1094                                                                                         OPJ_BYTE * p_data,
1095                                                                                         OPJ_UINT32 p_data_size,
1096                                                                                         opj_stream_t *p_stream
1097                                                                                         )
1098 {
1099         if (p_codec && p_data && p_stream) {
1100                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
1101                 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
1102
1103                 if (! l_codec->is_decompressor) {
1104                         return OPJ_FALSE;
1105                 }
1106
1107                 return l_codec->m_codec_data.m_decompression.opj_decode_tile_data(      l_codec->m_codec,
1108                                                                                                                                                         p_tile_index,
1109                                                                                                                                                         p_data,
1110                                                                                                                                                         p_data_size,
1111                                                                                                                                                         l_stream,
1112                                                                                                                                                         &(l_codec->m_event_mgr) );
1113         }
1114         return OPJ_FALSE;
1115 }
1116
1117 /*
1118  *
1119  *
1120  */
1121 void OPJ_CALLCONV opj_dump_codec(       opj_codec_t *p_codec,
1122                                                                         OPJ_INT32 info_flag,
1123                                                                         FILE* output_stream)
1124 {
1125         if (p_codec) {
1126                 opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
1127
1128                 l_codec->opj_dump_codec(l_codec->m_codec, info_flag, output_stream);
1129                 return;
1130         }
1131
1132         fprintf(stderr, "[ERROR] Input parameter of the dump_codec function are incorrect.\n");
1133         return;
1134 }
1135
1136 /*
1137  *
1138  *
1139  */
1140 opj_codestream_info_v2_t* OPJ_CALLCONV opj_get_cstr_info(opj_codec_t *p_codec)
1141 {
1142         if (p_codec) {
1143                 opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
1144
1145                 return l_codec->opj_get_codec_info(l_codec->m_codec);
1146         }
1147
1148         return NULL;
1149 }
1150
1151 /*
1152  *
1153  *
1154  */
1155 void OPJ_CALLCONV opj_destroy_cstr_info_v2(opj_codestream_info_v2_t **cstr_info) {
1156         if (cstr_info) {
1157
1158                 if ((*cstr_info)->m_default_tile_info.tccp_info){
1159                         opj_free((*cstr_info)->m_default_tile_info.tccp_info);
1160                 }
1161
1162                 if ((*cstr_info)->tile_info){
1163                         /* FIXME not used for the moment*/
1164                 }
1165
1166                 opj_free((*cstr_info));
1167                 (*cstr_info) = NULL;
1168         }
1169 }
1170
1171 /*
1172  *
1173  *
1174  */
1175 opj_codestream_index_t * OPJ_CALLCONV opj_get_cstr_index(opj_codec_t *p_codec)
1176 {
1177         if (p_codec) {
1178                 opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
1179
1180                 return l_codec->opj_get_codec_index(l_codec->m_codec);
1181         }
1182
1183         return NULL;
1184 }
1185
1186 /*
1187  *
1188  *
1189  */
1190 void OPJ_CALLCONV opj_destroy_cstr_index(opj_codestream_index_t **p_cstr_index)
1191 {
1192         if (*p_cstr_index){
1193                 j2k_destroy_cstr_index(*p_cstr_index);
1194                 (*p_cstr_index) = NULL;
1195         }
1196 }
1197
1198 /*
1199  *
1200  *
1201  */
1202 opj_bool OPJ_CALLCONV opj_decode_v2(opj_codec_t *p_codec,
1203                                                                         opj_stream_t *p_stream,
1204                                                                         opj_image_t* p_image)
1205 {
1206         if (p_codec && p_stream) {
1207                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
1208                 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
1209
1210                 if (! l_codec->is_decompressor) {
1211                         return OPJ_FALSE;
1212                 }
1213
1214                 return l_codec->m_codec_data.m_decompression.opj_decode(l_codec->m_codec,
1215                                                                                                                                 l_stream,
1216                                                                                                                                 p_image,
1217                                                                                                                                 &(l_codec->m_event_mgr) );
1218         }
1219
1220         return OPJ_FALSE;
1221 }
1222
1223 /*
1224  *
1225  *
1226  */
1227 opj_bool OPJ_CALLCONV opj_end_decompress (      opj_codec_t *p_codec,
1228                                                                                         opj_stream_t *p_stream)
1229 {
1230         if (p_codec && p_stream) {
1231                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
1232                 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
1233
1234                 if (! l_codec->is_decompressor) {
1235                         return OPJ_FALSE;
1236                 }
1237                 
1238                 return l_codec->m_codec_data.m_decompression.opj_end_decompress(l_codec->m_codec,
1239                                                                                                                                                 l_stream,
1240                                                                                                                                                 &(l_codec->m_event_mgr) );
1241         }
1242
1243         return OPJ_FALSE;
1244 }
1245
1246 /*
1247  *
1248  *
1249  */
1250 opj_bool OPJ_CALLCONV opj_get_decoded_tile(     opj_codec_t *p_codec,
1251                                                                                         opj_stream_t *p_stream,
1252                                                                                         opj_image_t *p_image,
1253                                                                                         OPJ_UINT32 tile_index)
1254 {
1255         if (p_codec && p_stream) {
1256                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
1257                 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
1258
1259                 if (! l_codec->is_decompressor) {
1260                         return OPJ_FALSE;
1261                 }
1262                 
1263                 return l_codec->m_codec_data.m_decompression.opj_get_decoded_tile(      l_codec->m_codec,
1264                                                                                                                                                         l_stream,
1265                                                                                                                                                         p_image,
1266                                                                                                                                                         &(l_codec->m_event_mgr),
1267                                                                                                                                                         tile_index);
1268         }
1269
1270         return OPJ_FALSE;
1271 }
1272
1273 /*
1274  *
1275  *
1276  */
1277 opj_bool OPJ_CALLCONV opj_set_decoded_resolution_factor(opj_codec_t *p_codec, 
1278                                                                                                                 OPJ_UINT32 res_factor )
1279 {
1280         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
1281
1282         if ( !l_codec ){
1283                 fprintf(stderr, "[ERROR] Input parameters of the setup_decoder function are incorrect.\n");
1284                 return OPJ_FALSE;
1285         }
1286
1287         l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor(l_codec->m_codec, 
1288                                                                                                                                                         res_factor,
1289                                                                                                                                                         &(l_codec->m_event_mgr) );
1290         return OPJ_TRUE;
1291 }
1292
1293
1294 opj_bool OPJ_CALLCONV opj_set_MCT(opj_cparameters_t *parameters,OPJ_FLOAT32 * pEncodingMatrix,OPJ_INT32 * p_dc_shift,OPJ_UINT32 pNbComp)
1295 {
1296         OPJ_UINT32 l_matrix_size = pNbComp * pNbComp * sizeof(OPJ_FLOAT32);
1297         OPJ_UINT32 l_dc_shift_size = pNbComp * sizeof(OPJ_INT32);
1298         OPJ_UINT32 l_mct_total_size = l_matrix_size + l_dc_shift_size;
1299
1300         /* add MCT capability */
1301         int rsiz = (int)parameters->cp_rsiz | (int)MCT;
1302         parameters->cp_rsiz = (OPJ_RSIZ_CAPABILITIES)rsiz;
1303         parameters->irreversible = 1;
1304
1305         /* use array based MCT */
1306         parameters->tcp_mct = 2;
1307         parameters->mct_data = opj_malloc(l_mct_total_size);
1308         if (! parameters->mct_data) {
1309                 return OPJ_FALSE;
1310         }
1311
1312         memcpy(parameters->mct_data,pEncodingMatrix,l_matrix_size);
1313         memcpy(((OPJ_BYTE *) parameters->mct_data) +  l_matrix_size,p_dc_shift,l_dc_shift_size);
1314
1315         return OPJ_TRUE;
1316 }
1317
1318 /**
1319  * Writes a tile with the given data.
1320  *
1321  * @param       p_compressor            the jpeg2000 codec.
1322  * @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.
1323  * @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.
1324  * @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,
1325  *                                                              depending on the precision of the given component.
1326  * @param       p_stream                        the stream to write data to.
1327  */
1328 opj_bool OPJ_CALLCONV opj_write_tile (  opj_codec_t *p_codec,
1329                                                                                 OPJ_UINT32 p_tile_index,
1330                                                                                 OPJ_BYTE * p_data,
1331                                                                                 OPJ_UINT32 p_data_size,
1332                                                                                 opj_stream_t *p_stream )
1333 {
1334         if (p_codec && p_stream && p_data) {
1335                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
1336                 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
1337
1338                 if (l_codec->is_decompressor) {
1339                         return OPJ_FALSE;
1340                 }
1341
1342                 return l_codec->m_codec_data.m_compression.opj_write_tile(      l_codec->m_codec,
1343                                                                                                                                         p_tile_index,
1344                                                                                                                                         p_data,
1345                                                                                                                                         p_data_size,
1346                                                                                                                                         l_stream,
1347                                                                                                                                         &(l_codec->m_event_mgr) );
1348         }
1349
1350         return OPJ_FALSE;
1351 }