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