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