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