[trunk] WIP: add a read MCO marker function (JPEG2000 part 2)
[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         /** FIXME DOC */
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         /** FIXME DOC */
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_INT32 p_start_x, OPJ_INT32 p_end_x,
78                                                                                 OPJ_INT32 p_start_y, OPJ_INT32 p_end_y,
79                                                                                 struct opj_event_mgr * p_manager);
80 }opj_decompression_t;
81
82 /**
83  * Compression handler. FIXME DOC
84  */
85 typedef struct opj_compression
86 {
87         opj_bool (* opj_start_compress) (void *p_codec,struct opj_stream_private *cio,struct opj_image * p_image,       struct opj_event_mgr * p_manager);
88         opj_bool (* opj_encode) (void * p_codec, struct opj_stream_private *p_cio, struct opj_event_mgr * p_manager);
89         opj_bool (* opj_write_tile) (void * p_codec,OPJ_UINT32 p_tile_index,OPJ_BYTE * p_data,OPJ_UINT32 p_data_size,struct opj_stream_private * p_cio,struct opj_event_mgr * p_manager);
90         opj_bool (* opj_end_compress) (void * p_codec, struct opj_stream_private *p_cio, struct opj_event_mgr * p_manager);
91         void (* opj_destroy) (void * p_codec);
92         void (*opj_setup_encoder) (void * p_codec,opj_cparameters_t * p_param,struct opj_image * p_image, struct opj_event_mgr * p_manager);
93
94 }opj_compression_t;
95
96 /**
97  * Main codec handler used for compression or decompression.
98  */
99 typedef struct opj_codec_private
100 {
101         /** FIXME DOC */
102         union {
103                 opj_decompression_t m_decompression;
104                 opj_compression_t m_compression;
105     } m_codec_data;
106     /** FIXME DOC*/
107         void * m_codec;
108         /** Event handler */
109         opj_event_mgr_t* m_event_mgr;
110         /** Flag to indicate if the codec is used to decode or encode*/
111         opj_bool is_decompressor;
112         void (*opj_dump_codec) (void * p_codec, OPJ_INT32 info_flag, FILE* output_stream);
113         opj_codestream_info_v2_t* (*opj_get_codec_info)(void* p_codec);
114         opj_codestream_index_t* (*opj_get_codec_index)(void* p_codec);
115 }
116 opj_codec_private_t;
117
118
119
120 /* ---------------------------------------------------------------------- */
121
122
123 OPJ_UINT32 opj_read_from_file (void * p_buffer, OPJ_UINT32 p_nb_bytes, FILE * p_file)
124 {
125         OPJ_UINT32 l_nb_read = fread(p_buffer,1,p_nb_bytes,p_file);
126         return l_nb_read ? l_nb_read : -1;
127 }
128
129 OPJ_UINT32 opj_get_data_length_from_file (FILE * p_file)
130 {
131         OPJ_UINT32 file_length = 0;
132
133         fseek(p_file, 0, SEEK_END);
134         file_length = ftell(p_file);
135         fseek(p_file, 0, SEEK_SET);
136
137         return file_length;
138 }
139
140 OPJ_UINT32 opj_write_from_file (void * p_buffer, OPJ_UINT32 p_nb_bytes, FILE * p_file)
141 {
142         return fwrite(p_buffer,1,p_nb_bytes,p_file);
143 }
144
145 OPJ_SIZE_T opj_skip_from_file (OPJ_SIZE_T p_nb_bytes, FILE * p_user_data)
146 {
147         if (fseek(p_user_data,p_nb_bytes,SEEK_CUR)) {
148                 return -1;
149         }
150
151         return p_nb_bytes;
152 }
153
154 opj_bool opj_seek_from_file (OPJ_SIZE_T p_nb_bytes, FILE * p_user_data)
155 {
156         if (fseek(p_user_data,p_nb_bytes,SEEK_SET)) {
157                 return EXIT_FAILURE;
158         }
159
160         return EXIT_SUCCESS;
161 }
162
163 /* ---------------------------------------------------------------------- */
164 #ifdef _WIN32
165 #ifndef OPJ_STATIC
166 BOOL APIENTRY
167 DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
168
169         OPJ_ARG_NOT_USED(lpReserved);
170         OPJ_ARG_NOT_USED(hModule);
171
172         switch (ul_reason_for_call) {
173                 case DLL_PROCESS_ATTACH :
174                         break;
175                 case DLL_PROCESS_DETACH :
176                         break;
177                 case DLL_THREAD_ATTACH :
178                 case DLL_THREAD_DETACH :
179                         break;
180     }
181
182     return TRUE;
183 }
184 #endif /* OPJ_STATIC */
185 #endif /* _WIN32 */
186
187 /* ---------------------------------------------------------------------- */
188
189
190 const char* OPJ_CALLCONV opj_version(void) {
191     return PACKAGE_VERSION;
192 }
193
194
195
196 opj_dinfo_t* OPJ_CALLCONV opj_create_decompress(OPJ_CODEC_FORMAT format) {
197         opj_dinfo_t *dinfo = (opj_dinfo_t*)opj_calloc(1, sizeof(opj_dinfo_t));
198         if(!dinfo) return NULL;
199         dinfo->is_decompressor = OPJ_TRUE;
200         switch(format) {
201                 case CODEC_J2K:
202                 case CODEC_JPT:
203                         /* get a J2K decoder handle */
204                         dinfo->j2k_handle = (void*)j2k_create_decompress((opj_common_ptr)dinfo);
205                         if(!dinfo->j2k_handle) {
206                                 opj_free(dinfo);
207                                 return NULL;
208                         }
209                         break;
210                 case CODEC_JP2:
211                         /* get a JP2 decoder handle */
212                         dinfo->jp2_handle = (void*)jp2_create_decompress((opj_common_ptr)dinfo);
213                         if(!dinfo->jp2_handle) {
214                                 opj_free(dinfo);
215                                 return NULL;
216                         }
217                         break;
218                 case CODEC_UNKNOWN:
219                 default:
220                         opj_free(dinfo);
221                         return NULL;
222         }
223
224         dinfo->codec_format = format;
225
226         return dinfo;
227 }
228
229 opj_codec_t* OPJ_CALLCONV opj_create_decompress_v2(OPJ_CODEC_FORMAT p_format)
230 {
231         opj_codec_private_t *l_info = 00;
232
233         l_info = (opj_codec_private_t*) opj_calloc(1, sizeof(opj_codec_private_t));
234         if (!l_info){
235                 return 00;
236         }
237         memset(l_info, 0, sizeof(opj_codec_private_t));
238
239         l_info->is_decompressor = 1;
240
241         switch (p_format) {
242                 case CODEC_J2K:
243                         l_info->opj_dump_codec = (void (*) (void*, OPJ_INT32, FILE*)) j2k_dump;
244
245                         l_info->opj_get_codec_info = (opj_codestream_info_v2_t* (*) (void*) ) j2k_get_cstr_info;
246
247                         l_info->opj_get_codec_index = (opj_codestream_index_t* (*) (void*) ) j2k_get_cstr_index;
248
249                         l_info->m_codec_data.m_decompression.opj_decode =
250                                         (opj_bool (*) ( void *,
251                                                                         struct opj_stream_private *,
252                                                                         opj_image_t*, struct opj_event_mgr * )) j2k_decode_v2;
253
254                         l_info->m_codec_data.m_decompression.opj_end_decompress =
255                                         (opj_bool (*) ( void *,
256                                                                         struct opj_stream_private *,
257                                                                         struct opj_event_mgr *)) j2k_end_decompress;
258
259                         l_info->m_codec_data.m_decompression.opj_read_header =
260                                         (opj_bool (*) ( struct opj_stream_private *,
261                                                                         void *,
262                                                                         opj_image_t *,
263                                                                         struct opj_event_mgr * )) j2k_read_header;
264
265                         l_info->m_codec_data.m_decompression.opj_destroy =
266                                         (void (*) (void *))j2k_destroy;
267
268                         l_info->m_codec_data.m_decompression.opj_setup_decoder =
269                                         (void (*) (void * , opj_dparameters_t * )) j2k_setup_decoder_v2;
270
271                         l_info->m_codec_data.m_decompression.opj_read_tile_header =
272                                         (opj_bool (*) ( void *,
273                                                                         OPJ_UINT32*,
274                                                                         OPJ_UINT32*,
275                                                                         OPJ_INT32*, OPJ_INT32*,
276                                                                         OPJ_INT32*, OPJ_INT32*,
277                                                                         OPJ_UINT32*,
278                                                                         opj_bool*,
279                                                                         struct opj_stream_private *,
280                                                                         struct opj_event_mgr * )) j2k_read_tile_header;
281
282                         l_info->m_codec_data.m_decompression.opj_decode_tile_data =
283                                         (opj_bool (*) (void *, OPJ_UINT32, OPJ_BYTE*, OPJ_UINT32, struct opj_stream_private *, struct opj_event_mgr *)) j2k_decode_tile;
284
285                         l_info->m_codec_data.m_decompression.opj_set_decode_area =
286                                         (opj_bool (*) (void *, OPJ_INT32, OPJ_INT32, OPJ_INT32, OPJ_INT32, struct opj_event_mgr *)) j2k_set_decode_area;
287
288                         l_info->m_codec = j2k_create_decompress_v2();
289
290                         if (! l_info->m_codec) {
291                                 opj_free(l_info);
292                                 return NULL;
293                         }
294
295                         break;
296
297                 case CODEC_JP2:
298                         /* get a JP2 decoder handle */
299                         l_info->opj_dump_codec = (void (*) (void*, OPJ_INT32, FILE*)) jp2_dump;
300
301                         l_info->opj_get_codec_info = (opj_codestream_info_v2_t* (*) (void*) ) jp2_get_cstr_info;
302
303                         l_info->opj_get_codec_index = (opj_codestream_index_t* (*) (void*) ) jp2_get_cstr_index;
304
305                         l_info->m_codec_data.m_decompression.opj_decode =
306                                         (opj_bool (*) ( void *,
307                                                                         struct opj_stream_private *,
308                                                                         opj_image_t*,
309                                                                         struct opj_event_mgr * )) opj_jp2_decode_v2;
310
311                         l_info->m_codec_data.m_decompression.opj_end_decompress =  (opj_bool (*) (void *,struct opj_stream_private *,struct opj_event_mgr *)) jp2_end_decompress;
312
313                         l_info->m_codec_data.m_decompression.opj_read_header =  (opj_bool (*) (
314                                         struct opj_stream_private *,
315                                         void *,
316                                         opj_image_t *,
317                                         struct opj_event_mgr * )) jp2_read_header;
318
319                         l_info->m_codec_data.m_decompression.opj_read_tile_header = ( opj_bool (*) (
320                                         void *,
321                                         OPJ_UINT32*,
322                                         OPJ_UINT32*,
323                                         OPJ_INT32*,
324                                         OPJ_INT32*,
325                                         OPJ_INT32 * ,
326                                         OPJ_INT32 * ,
327                                         OPJ_UINT32 * ,
328                                         opj_bool *,
329                                         struct opj_stream_private *,
330                                         struct opj_event_mgr * )) jp2_read_tile_header;
331
332                         l_info->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 * )) opj_jp2_decode_tile;
333
334                         l_info->m_codec_data.m_decompression.opj_destroy = (void (*) (void *))jp2_destroy;
335
336                         l_info->m_codec_data.m_decompression.opj_setup_decoder = (void (*) (void * ,opj_dparameters_t * )) jp2_setup_decoder_v2;
337
338                         l_info->m_codec_data.m_decompression.opj_set_decode_area = (opj_bool (*) (void *,OPJ_INT32,OPJ_INT32,OPJ_INT32,OPJ_INT32, struct opj_event_mgr * )) jp2_set_decode_area;
339
340                         l_info->m_codec = jp2_create(OPJ_TRUE);
341
342                         if (! l_info->m_codec) {
343                                 opj_free(l_info);
344                                 return 00;
345                         }
346
347                         break;
348                 case CODEC_UNKNOWN:
349                 case CODEC_JPT:
350                 default:
351                         opj_free(l_info);
352                         return 00;
353         }
354
355         // FIXME set_default_event_handler(&(l_info->m_event_mgr));
356         return (opj_codec_t*) l_info;
357 }
358
359
360 void OPJ_CALLCONV opj_destroy_decompress(opj_dinfo_t *dinfo) {
361         if(dinfo) {
362                 /* destroy the codec */
363                 switch(dinfo->codec_format) {
364                         case CODEC_J2K:
365                         case CODEC_JPT:
366                                 j2k_destroy_decompress((opj_j2k_t*)dinfo->j2k_handle);
367                                 break;
368                         case CODEC_JP2:
369                                 jp2_destroy_decompress((opj_jp2_t*)dinfo->jp2_handle);
370                                 break;
371                         case CODEC_UNKNOWN:
372                         default:
373                                 break;
374                 }
375                 /* destroy the decompressor */
376                 opj_free(dinfo);
377         }
378 }
379
380 void OPJ_CALLCONV opj_set_default_decoder_parameters(opj_dparameters_t *parameters) {
381         if(parameters) {
382                 memset(parameters, 0, sizeof(opj_dparameters_t));
383                 /* default decoding parameters */
384                 parameters->cp_layer = 0;
385                 parameters->cp_reduce = 0;
386                 parameters->cp_limit_decoding = NO_LIMITATION;
387
388                 parameters->decod_format = -1;
389                 parameters->cod_format = -1;
390 /* UniPG>> */
391 #ifdef USE_JPWL
392                 parameters->jpwl_correct = OPJ_FALSE;
393                 parameters->jpwl_exp_comps = JPWL_EXPECTED_COMPONENTS;
394                 parameters->jpwl_max_tiles = JPWL_MAXIMUM_TILES;
395 #endif /* USE_JPWL */
396 /* <<UniPG */
397         }
398 }
399
400 void OPJ_CALLCONV opj_setup_decoder(opj_dinfo_t *dinfo, opj_dparameters_t *parameters) {
401         if(dinfo && parameters) {
402                 switch(dinfo->codec_format) {
403                         case CODEC_J2K:
404                         case CODEC_JPT:
405                                 j2k_setup_decoder((opj_j2k_t*)dinfo->j2k_handle, parameters);
406                                 break;
407                         case CODEC_JP2:
408                                 jp2_setup_decoder((opj_jp2_t*)dinfo->jp2_handle, parameters);
409                                 break;
410                         case CODEC_UNKNOWN:
411                         default:
412                                 break;
413                 }
414         }
415 }
416
417 opj_bool OPJ_CALLCONV opj_setup_decoder_v2(opj_codec_t *p_info, opj_dparameters_t *parameters, opj_event_mgr_t* event_mgr)
418 {
419         opj_codec_private_t * l_info = (opj_codec_private_t *) p_info;
420
421         if ( !p_info || !parameters || !event_mgr ){
422                 fprintf(stderr, "[ERROR] Input parameters of the setup_decoder function are incorrect.\n");
423                 return OPJ_FALSE;
424         }
425
426         if ( !event_mgr->error_handler || !event_mgr->warning_handler || !event_mgr->error_handler){
427                 fprintf(stderr, "[ERROR] Event handler provided to the setup_decoder function is not valid.\n");
428                 return OPJ_FALSE;
429         }
430
431         if (! l_info->is_decompressor) {
432                 opj_event_msg_v2(event_mgr, EVT_ERROR, "Codec provided to the setup_decoder function is not a decompressor handler.\n");
433                 return OPJ_FALSE;
434         }
435
436         l_info->m_codec_data.m_decompression.opj_setup_decoder(l_info->m_codec, parameters);
437
438         l_info->m_event_mgr = event_mgr;
439
440         return OPJ_TRUE;
441 }
442
443 opj_image_t* OPJ_CALLCONV opj_decode(opj_dinfo_t *dinfo, opj_cio_t *cio) {
444         return opj_decode_with_info(dinfo, cio, NULL);
445 }
446
447 opj_image_t* OPJ_CALLCONV opj_decode_with_info(opj_dinfo_t *dinfo, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
448         if(dinfo && cio) {
449                 switch(dinfo->codec_format) {
450                         case CODEC_J2K:
451                                 return j2k_decode((opj_j2k_t*)dinfo->j2k_handle, cio, cstr_info);
452                         case CODEC_JPT:
453                                 return j2k_decode_jpt_stream((opj_j2k_t*)dinfo->j2k_handle, cio, cstr_info);
454                         case CODEC_JP2:
455                                 return opj_jp2_decode((opj_jp2_t*)dinfo->jp2_handle, cio, cstr_info);
456                         case CODEC_UNKNOWN:
457                         default:
458                                 break;
459                 }
460         }
461         return NULL;
462 }
463
464 opj_cinfo_t* OPJ_CALLCONV opj_create_compress(OPJ_CODEC_FORMAT format) {
465         opj_cinfo_t *cinfo = (opj_cinfo_t*)opj_calloc(1, sizeof(opj_cinfo_t));
466         if(!cinfo) return NULL;
467         cinfo->is_decompressor = OPJ_FALSE;
468         switch(format) {
469                 case CODEC_J2K:
470                         /* get a J2K coder handle */
471                         cinfo->j2k_handle = (void*)j2k_create_compress((opj_common_ptr)cinfo);
472                         if(!cinfo->j2k_handle) {
473                                 opj_free(cinfo);
474                                 return NULL;
475                         }
476                         break;
477                 case CODEC_JP2:
478                         /* get a JP2 coder handle */
479                         cinfo->jp2_handle = (void*)jp2_create_compress((opj_common_ptr)cinfo);
480                         if(!cinfo->jp2_handle) {
481                                 opj_free(cinfo);
482                                 return NULL;
483                         }
484                         break;
485                 case CODEC_JPT:
486                 case CODEC_UNKNOWN:
487                 default:
488                         opj_free(cinfo);
489                         return NULL;
490         }
491
492         cinfo->codec_format = format;
493
494         return cinfo;
495 }
496
497 void OPJ_CALLCONV opj_destroy_compress(opj_cinfo_t *cinfo) {
498         if(cinfo) {
499                 /* destroy the codec */
500                 switch(cinfo->codec_format) {
501                         case CODEC_J2K:
502                                 j2k_destroy_compress((opj_j2k_t*)cinfo->j2k_handle);
503                                 break;
504                         case CODEC_JP2:
505                                 jp2_destroy_compress((opj_jp2_t*)cinfo->jp2_handle);
506                                 break;
507                         case CODEC_JPT:
508                         case CODEC_UNKNOWN:
509                         default:
510                                 break;
511                 }
512                 /* destroy the decompressor */
513                 opj_free(cinfo);
514         }
515 }
516
517 void OPJ_CALLCONV opj_set_default_encoder_parameters(opj_cparameters_t *parameters) {
518         if(parameters) {
519                 memset(parameters, 0, sizeof(opj_cparameters_t));
520                 /* default coding parameters */
521                 parameters->cp_cinema = OFF; 
522                 parameters->max_comp_size = 0;
523                 parameters->numresolution = 6;
524                 parameters->cp_rsiz = STD_RSIZ;
525                 parameters->cblockw_init = 64;
526                 parameters->cblockh_init = 64;
527                 parameters->prog_order = LRCP;
528                 parameters->roi_compno = -1;            /* no ROI */
529                 parameters->subsampling_dx = 1;
530                 parameters->subsampling_dy = 1;
531                 parameters->tp_on = 0;
532                 parameters->decod_format = -1;
533                 parameters->cod_format = -1;
534                 parameters->tcp_rates[0] = 0;   
535                 parameters->tcp_numlayers = 0;
536                 parameters->cp_disto_alloc = 0;
537                 parameters->cp_fixed_alloc = 0;
538                 parameters->cp_fixed_quality = 0;
539                 parameters->jpip_on = OPJ_FALSE;
540 /* UniPG>> */
541 #ifdef USE_JPWL
542                 parameters->jpwl_epc_on = OPJ_FALSE;
543                 parameters->jpwl_hprot_MH = -1; /* -1 means unassigned */
544                 {
545                         int i;
546                         for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
547                                 parameters->jpwl_hprot_TPH_tileno[i] = -1; /* unassigned */
548                                 parameters->jpwl_hprot_TPH[i] = 0; /* absent */
549                         }
550                 };
551                 {
552                         int i;
553                         for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
554                                 parameters->jpwl_pprot_tileno[i] = -1; /* unassigned */
555                                 parameters->jpwl_pprot_packno[i] = -1; /* unassigned */
556                                 parameters->jpwl_pprot[i] = 0; /* absent */
557                         }
558                 };
559                 parameters->jpwl_sens_size = 0; /* 0 means no ESD */
560                 parameters->jpwl_sens_addr = 0; /* 0 means auto */
561                 parameters->jpwl_sens_range = 0; /* 0 means packet */
562                 parameters->jpwl_sens_MH = -1; /* -1 means unassigned */
563                 {
564                         int i;
565                         for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
566                                 parameters->jpwl_sens_TPH_tileno[i] = -1; /* unassigned */
567                                 parameters->jpwl_sens_TPH[i] = -1; /* absent */
568                         }
569                 };
570 #endif /* USE_JPWL */
571 /* <<UniPG */
572         }
573 }
574
575 /**
576  * Helper function.
577  * Sets the stream to be a file stream. The FILE must have been open previously.
578  * @param               p_stream        the stream to modify
579  * @param               p_file          handler to an already open file.
580 */
581 opj_stream_t* OPJ_CALLCONV opj_stream_create_default_file_stream (FILE * p_file, opj_bool p_is_read_stream)
582 {
583         return opj_stream_create_file_stream(p_file,J2K_STREAM_CHUNK_SIZE,p_is_read_stream);
584 }
585
586 opj_stream_t* OPJ_CALLCONV opj_stream_create_file_stream (FILE * p_file, OPJ_UINT32 p_size, opj_bool p_is_read_stream)
587 {
588         opj_stream_t* l_stream = 00;
589
590         if (! p_file) {
591                 return NULL;
592         }
593
594         l_stream = opj_stream_create(p_size,p_is_read_stream);
595         if (! l_stream) {
596                 return NULL;
597         }
598
599         opj_stream_set_user_data(l_stream, p_file);
600         opj_stream_set_user_data_length(l_stream, opj_get_data_length_from_file(p_file));
601         opj_stream_set_read_function(l_stream, (opj_stream_read_fn) opj_read_from_file);
602         opj_stream_set_write_function(l_stream, (opj_stream_write_fn) opj_write_from_file);
603         opj_stream_set_skip_function(l_stream, (opj_stream_skip_fn) opj_skip_from_file);
604         opj_stream_set_seek_function(l_stream, (opj_stream_seek_fn) opj_seek_from_file);
605
606         return l_stream;
607 }
608
609 void OPJ_CALLCONV opj_setup_encoder(opj_cinfo_t *cinfo, opj_cparameters_t *parameters, opj_image_t *image) {
610         if(cinfo && parameters && image) {
611                 switch(cinfo->codec_format) {
612                         case CODEC_J2K:
613                                 j2k_setup_encoder((opj_j2k_t*)cinfo->j2k_handle, parameters, image);
614                                 break;
615                         case CODEC_JP2:
616                                 jp2_setup_encoder((opj_jp2_t*)cinfo->jp2_handle, parameters, image);
617                                 break;
618                         case CODEC_JPT:
619                         case CODEC_UNKNOWN:
620                         default:
621                                 break;
622                 }
623         }
624 }
625
626 opj_bool OPJ_CALLCONV opj_encode(opj_cinfo_t *cinfo, opj_cio_t *cio, opj_image_t *image, char *index) {
627         if (index != NULL)
628                 opj_event_msg((opj_common_ptr)cinfo, EVT_WARNING, "Set index to NULL when calling the opj_encode function.\n"
629                 "To extract the index, use the opj_encode_with_info() function.\n"
630                 "No index will be generated during this encoding\n");
631         return opj_encode_with_info(cinfo, cio, image, NULL);
632 }
633
634 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) {
635         if(cinfo && cio && image) {
636                 switch(cinfo->codec_format) {
637                         case CODEC_J2K:
638                                 return j2k_encode((opj_j2k_t*)cinfo->j2k_handle, cio, image, cstr_info);
639                         case CODEC_JP2:
640                                 return opj_jp2_encode((opj_jp2_t*)cinfo->jp2_handle, cio, image, cstr_info);        
641                         case CODEC_JPT:
642                         case CODEC_UNKNOWN:
643                         default:
644                                 break;
645                 }
646         }
647         return OPJ_FALSE;
648 }
649
650 void OPJ_CALLCONV opj_destroy_cstr_info(opj_codestream_info_t *cstr_info) {
651         if (cstr_info) {
652                 int tileno;
653                 for (tileno = 0; tileno < cstr_info->tw * cstr_info->th; tileno++) {
654                         opj_tile_info_t *tile_info = &cstr_info->tile[tileno];
655                         opj_free(tile_info->thresh);
656                         opj_free(tile_info->packet);
657                         opj_free(tile_info->tp);
658                         opj_free(tile_info->marker);
659                 }
660                 opj_free(cstr_info->tile);
661                 opj_free(cstr_info->marker);
662                 opj_free(cstr_info->numdecompos);
663         }
664 }
665
666 void OPJ_CALLCONV opj_destroy_cstr_info_v2(opj_codestream_info_v2_t *cstr_info) {
667         if (cstr_info) {
668                 int tileno, compno;
669
670                 if (cstr_info->tile_info){
671                         for (tileno = 0; tileno < cstr_info->tw * cstr_info->th; tileno++) {
672                                 for (compno = 0; compno < cstr_info->nbcomps; compno++){
673                                         opj_free(cstr_info->tile_info[tileno].tccp_info);
674                                 }
675                         }
676                         opj_free(cstr_info->tile_info);
677                 }
678
679                 if (cstr_info->m_default_tile_info.tccp_info){
680                         opj_free(cstr_info->m_default_tile_info.tccp_info);
681                 }
682
683                 opj_free(cstr_info);
684         }
685 }
686
687
688
689 #ifdef OLD_WAY_MS
690 opj_bool OPJ_CALLCONV opj_read_header (
691                                                                    opj_codec_t *p_codec,
692                                                                    opj_image_t ** p_image,
693                                                                    OPJ_INT32 * p_tile_x0,
694                                                                    OPJ_INT32 * p_tile_y0,
695                                                                    OPJ_UINT32 * p_tile_width,
696                                                                    OPJ_UINT32 * p_tile_height,
697                                                                    OPJ_UINT32 * p_nb_tiles_x,
698                                                                    OPJ_UINT32 * p_nb_tiles_y,
699                                                                    opj_stream_t *p_cio)
700 {
701         if (p_codec && p_cio) {
702                 opj_codec_private_t * l_info = (opj_codec_private_t *) p_codec;
703                 opj_stream_private_t * l_cio = (opj_stream_private_t *) p_cio;
704
705                 if(! l_info->is_decompressor) {
706                         return OPJ_FALSE;
707                 }
708
709                 return l_info->m_codec_data.m_decompression.opj_read_header(
710                         l_info->m_codec,
711                         p_image,
712                         p_tile_x0,
713                         p_tile_y0,
714                         p_tile_width,
715                         p_tile_height,
716                         p_nb_tiles_x,
717                         p_nb_tiles_y,
718                         l_cio,
719                         l_info->m_event_mgr); //&(l_info->m_event_mgr));
720         }
721         return OPJ_FALSE;
722 }
723 #endif
724
725 opj_bool OPJ_CALLCONV opj_read_header ( opj_stream_t *p_cio,
726                                                                                 opj_codec_t *p_codec,
727                                                                                 opj_image_t *p_image )
728 {
729         if (p_codec && p_cio) {
730                 opj_codec_private_t* l_info = (opj_codec_private_t*) p_codec;
731                 opj_stream_private_t* l_cio = (opj_stream_private_t*) p_cio;
732
733                 if(! l_info->is_decompressor) {
734                         opj_event_msg_v2(l_info->m_event_mgr, EVT_ERROR, "Codec provided to the read_header function is not a decompressor handler.\n");
735                         return OPJ_FALSE;
736                 }
737
738                 return l_info->m_codec_data.m_decompression.opj_read_header(
739                                         l_cio,
740                                         l_info->m_codec,
741                                         p_image,
742                                         l_info->m_event_mgr);
743         }
744
745         fprintf(stderr, "[ERROR] Input parameters of the read_header function are incorrect.\n");
746         return OPJ_FALSE;
747 }
748
749
750 void OPJ_CALLCONV opj_destroy_codec(opj_codec_t *p_info)
751 {
752         if (p_info) {
753                 opj_codec_private_t * l_info = (opj_codec_private_t *) p_info;
754
755                 if (l_info->is_decompressor) {
756                         l_info->m_codec_data.m_decompression.opj_destroy(l_info->m_codec);
757                 }
758                 else {
759                         l_info->m_codec_data.m_compression.opj_destroy(l_info->m_codec);
760                 }
761
762                 l_info->m_codec = 00;
763                 opj_free(l_info);
764         }
765 }
766
767 /**
768  * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
769  *
770  * @param       p_codec                 the jpeg2000 codec.
771  * @param       p_start_x               the left position of the rectangle to decode (in image coordinates).
772  * @param       p_end_x                 the right position of the rectangle to decode (in image coordinates).
773  * @param       p_start_y               the up position of the rectangle to decode (in image coordinates).
774  * @param       p_end_y                 the bottom position of the rectangle to decode (in image coordinates).
775  *
776  * @return      true                    if the area could be set.
777  */
778 opj_bool OPJ_CALLCONV opj_set_decode_area(      opj_codec_t *p_codec,
779                                                                                         OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
780                                                                                         OPJ_INT32 p_end_x, OPJ_INT32 p_end_y
781                                                                                         )
782 {
783         if (p_codec) {
784                 opj_codec_private_t * l_info = (opj_codec_private_t *) p_codec;
785                 if (! l_info->is_decompressor) {
786                         return OPJ_FALSE;
787                 }
788
789                 return  l_info->m_codec_data.m_decompression.opj_set_decode_area(
790                                 l_info->m_codec,
791                                 p_start_x,
792                                 p_start_y,
793                                 p_end_x,
794                                 p_end_y,
795                                 l_info->m_event_mgr);
796
797         }
798         return OPJ_FALSE;
799 }
800
801 /**
802  * Reads a tile header. This function is compulsory and allows one to know the size of the tile thta will be decoded.
803  * The user may need to refer to the image got by opj_read_header to understand the size being taken by the tile.
804  *
805  * @param       p_codec                 the jpeg2000 codec.
806  * @param       p_tile_index    pointer to a value that will hold the index of the tile being decoded, in case of success.
807  * @param       p_data_size             pointer to a value that will hold the maximum size of the decoded data, in case of success. In case
808  *                                                      of truncated codestreams, the actual number of bytes decoded may be lower. The computation of the size is the same
809  *                                                      as depicted in opj_write_tile.
810  * @param       p_tile_x0               pointer to a value that will hold the x0 pos of the tile (in the image).
811  * @param       p_tile_y0               pointer to a value that will hold the y0 pos of the tile (in the image).
812  * @param       p_tile_x1               pointer to a value that will hold the x1 pos of the tile (in the image).
813  * @param       p_tile_y1               pointer to a value that will hold the y1 pos of the tile (in the image).
814  * @param       p_nb_comps              pointer to a value that will hold the number of components in the tile.
815  * @param       p_should_go_on  pointer to a boolean that will hold the fact that the decoding should go on. In case the
816  *                                                      codestream is over at the time of the call, the value will be set to false. The user should then stop
817  *                                                      the decoding.
818  * @param       p_stream                the stream to decode.
819  * @return      true                    if the tile header could be decoded. In case the decoding should end, the returned value is still true.
820  *                                                      returning false may be the result of a shortage of memory or an internal error.
821  */
822 opj_bool OPJ_CALLCONV opj_read_tile_header(
823                                         opj_codec_t *p_codec,
824                                         opj_stream_t * p_stream,
825                                         OPJ_UINT32 * p_tile_index,
826                                         OPJ_UINT32 * p_data_size,
827                                         OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
828                                         OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
829                                         OPJ_UINT32 * p_nb_comps,
830                                         opj_bool * p_should_go_on)
831 {
832         if (p_codec && p_stream && p_data_size && p_tile_index) {
833                 opj_codec_private_t * l_info = (opj_codec_private_t *) p_codec;
834                 opj_stream_private_t * l_cio = (opj_stream_private_t *) p_stream;
835
836                 if (! l_info->is_decompressor) {
837                         return OPJ_FALSE;
838                 }
839
840                 return l_info->m_codec_data.m_decompression.opj_read_tile_header(
841                         l_info->m_codec,
842                         p_tile_index,
843                         p_data_size,
844                         p_tile_x0, p_tile_y0,
845                         p_tile_x1, p_tile_y1,
846                         p_nb_comps,
847                         p_should_go_on,
848                         l_cio,
849                         l_info->m_event_mgr);
850         }
851         return OPJ_FALSE;
852 }
853
854 /**
855  * Reads a tile data. This function is compulsory and allows one to decode tile data. opj_read_tile_header should be called before.
856  * The user may need to refer to the image got by opj_read_header to understand the size being taken by the tile.
857  *
858  * @param       p_codec                 the jpeg2000 codec.
859  * @param       p_tile_index    the index of the tile being decoded, this should be the value set by opj_read_tile_header.
860  * @param       p_data                  pointer to a memory block that will hold the decoded data.
861  * @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.
862  * @param       p_stream                the stream to decode.
863  *
864  * @return      true                    if the data could be decoded.
865  */
866 opj_bool OPJ_CALLCONV opj_decode_tile_data(
867                                         opj_codec_t *p_codec,
868                                         OPJ_UINT32 p_tile_index,
869                                         OPJ_BYTE * p_data,
870                                         OPJ_UINT32 p_data_size,
871                                         opj_stream_t *p_stream
872                                         )
873 {
874         if (p_codec && p_data && p_stream) {
875                 opj_codec_private_t * l_info = (opj_codec_private_t *) p_codec;
876                 opj_stream_private_t * l_cio = (opj_stream_private_t *) p_stream;
877
878                 if (! l_info->is_decompressor) {
879                         return OPJ_FALSE;
880                 }
881
882                 return l_info->m_codec_data.m_decompression.opj_decode_tile_data(       l_info->m_codec,
883                                                                                                                                                         p_tile_index,
884                                                                                                                                                         p_data,
885                                                                                                                                                         p_data_size,
886                                                                                                                                                         l_cio,
887                                                                                                                                                         l_info->m_event_mgr);
888         }
889         return OPJ_FALSE;
890 }
891
892 /*
893  *
894  *
895  */
896 void OPJ_CALLCONV opj_dump_codec(       opj_codec_t *p_codec,
897                                                                         OPJ_INT32 info_flag,
898                                                                         FILE* output_stream)
899 {
900         if (p_codec) {
901                 opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
902
903                 l_codec->opj_dump_codec(l_codec->m_codec, info_flag, output_stream);
904                 return;
905         }
906
907         fprintf(stderr, "[ERROR] Input parameter of the dump_codec function are incorrect.\n");
908         return;
909 }
910
911 /*
912  *
913  *
914  */
915 opj_codestream_info_v2_t* OPJ_CALLCONV opj_get_cstr_info(opj_codec_t *p_codec)
916 {
917         if (p_codec) {
918                 opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
919
920                 return l_codec->opj_get_codec_info(l_codec->m_codec);
921         }
922
923         return NULL;
924 }
925
926 /*
927  *
928  *
929  */
930 opj_codestream_index_t * OPJ_CALLCONV opj_get_cstr_index(opj_codec_t *p_codec)
931 {
932         if (p_codec) {
933                 opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
934
935                 return l_codec->opj_get_codec_index(l_codec->m_codec);
936         }
937
938         return NULL;
939 }
940
941 opj_bool OPJ_CALLCONV opj_decode_v2(opj_codec_t *p_info,
942                                                                         opj_stream_t *cio,
943                                                                         opj_image_t* p_image)
944 {
945         if (p_info && cio) {
946                 opj_codec_private_t * l_info = (opj_codec_private_t *) p_info;
947                 opj_stream_private_t * l_cio = (opj_stream_private_t *) cio;
948
949                 if (! l_info->is_decompressor) {
950                         return OPJ_FALSE;
951                 }
952
953                 return l_info->m_codec_data.m_decompression.opj_decode( l_info->m_codec,
954                                                                                                                                 l_cio,
955                                                                                                                                 p_image,
956                                                                                                                                 l_info->m_event_mgr);
957         }
958
959         return OPJ_FALSE;
960 }
961
962 opj_bool OPJ_CALLCONV opj_end_decompress (opj_codec_t *p_codec,opj_stream_t *p_cio)
963 {
964         if (p_codec && p_cio) {
965                 opj_codec_private_t * l_info = (opj_codec_private_t *) p_codec;
966                 opj_stream_private_t * l_cio = (opj_stream_private_t *) p_cio;
967
968                 if (! l_info->is_decompressor) {
969                         return OPJ_FALSE;
970                 }
971                 return l_info->m_codec_data.m_decompression.opj_end_decompress( l_info->m_codec,
972                                                                                                                                                 l_cio,
973                                                                                                                                                 l_info->m_event_mgr);
974         }
975
976         return OPJ_FALSE;
977 }