[trunk] WIP: add a set decoded resolution factor function and update j2k_to_image...
[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_UINT32 opj_read_from_file (void * p_buffer, OPJ_UINT32 p_nb_bytes, FILE * p_file)
136 {
137         OPJ_UINT32 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_UINT32 opj_get_data_length_from_file (FILE * p_file)
142 {
143         OPJ_UINT32 file_length = 0;
144
145         fseek(p_file, 0, SEEK_END);
146         file_length = ftell(p_file);
147         fseek(p_file, 0, SEEK_SET);
148
149         return file_length;
150 }
151
152 OPJ_UINT32 opj_write_from_file (void * p_buffer, OPJ_UINT32 p_nb_bytes, FILE * p_file)
153 {
154         return fwrite(p_buffer,1,p_nb_bytes,p_file);
155 }
156
157 OPJ_SIZE_T opj_skip_from_file (OPJ_SIZE_T p_nb_bytes, FILE * p_user_data)
158 {
159         if (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_SIZE_T p_nb_bytes, FILE * p_user_data)
167 {
168         if (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 /* UniPG>> */
422 #ifdef USE_JPWL
423                 parameters->jpwl_correct = OPJ_FALSE;
424                 parameters->jpwl_exp_comps = JPWL_EXPECTED_COMPONENTS;
425                 parameters->jpwl_max_tiles = JPWL_MAXIMUM_TILES;
426 #endif /* USE_JPWL */
427 /* <<UniPG */
428         }
429 }
430
431 /* DEPRECATED */
432 void OPJ_CALLCONV opj_setup_decoder(opj_dinfo_t *dinfo, opj_dparameters_t *parameters) {
433         if(dinfo && parameters) {
434                 switch(dinfo->codec_format) {
435                         case CODEC_J2K:
436                         case CODEC_JPT:
437                                 j2k_setup_decoder((opj_j2k_t*)dinfo->j2k_handle, parameters);
438                                 break;
439                         case CODEC_JP2:
440                                 jp2_setup_decoder((opj_jp2_t*)dinfo->jp2_handle, parameters);
441                                 break;
442                         case CODEC_UNKNOWN:
443                         default:
444                                 break;
445                 }
446         }
447 }
448
449 opj_bool OPJ_CALLCONV opj_setup_decoder_v2(opj_codec_t *p_info, opj_dparameters_t *parameters, opj_event_mgr_t* event_mgr)
450 {
451         opj_codec_private_t * l_info = (opj_codec_private_t *) p_info;
452
453         if ( !p_info || !parameters || !event_mgr ){
454                 fprintf(stderr, "[ERROR] Input parameters of the setup_decoder function are incorrect.\n");
455                 return OPJ_FALSE;
456         }
457
458         if ( !event_mgr->error_handler || !event_mgr->warning_handler || !event_mgr->error_handler){
459                 fprintf(stderr, "[ERROR] Event handler provided to the setup_decoder function is not valid.\n");
460                 return OPJ_FALSE;
461         }
462
463         if (! l_info->is_decompressor) {
464                 opj_event_msg_v2(event_mgr, EVT_ERROR, "Codec provided to the setup_decoder function is not a decompressor handler.\n");
465                 return OPJ_FALSE;
466         }
467
468         l_info->m_codec_data.m_decompression.opj_setup_decoder(l_info->m_codec, parameters);
469
470         l_info->m_event_mgr = event_mgr;
471
472         return OPJ_TRUE;
473 }
474
475 /* DEPRECATED */
476 opj_image_t* OPJ_CALLCONV opj_decode(opj_dinfo_t *dinfo, opj_cio_t *cio) {
477         return opj_decode_with_info(dinfo, cio, NULL);
478 }
479
480 /* DEPRECATED */
481 opj_image_t* OPJ_CALLCONV opj_decode_with_info(opj_dinfo_t *dinfo, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
482         if(dinfo && cio) {
483                 switch(dinfo->codec_format) {
484                         case CODEC_J2K:
485                                 return j2k_decode((opj_j2k_t*)dinfo->j2k_handle, cio, cstr_info);
486                         case CODEC_JPT:
487                                 return j2k_decode_jpt_stream((opj_j2k_t*)dinfo->j2k_handle, cio, cstr_info);
488                         case CODEC_JP2:
489                                 return opj_jp2_decode((opj_jp2_t*)dinfo->jp2_handle, cio, cstr_info);
490                         case CODEC_UNKNOWN:
491                         default:
492                                 break;
493                 }
494         }
495         return NULL;
496 }
497
498 opj_cinfo_t* OPJ_CALLCONV opj_create_compress(OPJ_CODEC_FORMAT format) {
499         opj_cinfo_t *cinfo = (opj_cinfo_t*)opj_calloc(1, sizeof(opj_cinfo_t));
500         if(!cinfo) return NULL;
501         cinfo->is_decompressor = OPJ_FALSE;
502         switch(format) {
503                 case CODEC_J2K:
504                         /* get a J2K coder handle */
505                         cinfo->j2k_handle = (void*)j2k_create_compress((opj_common_ptr)cinfo);
506                         if(!cinfo->j2k_handle) {
507                                 opj_free(cinfo);
508                                 return NULL;
509                         }
510                         break;
511                 case CODEC_JP2:
512                         /* get a JP2 coder handle */
513                         cinfo->jp2_handle = (void*)jp2_create_compress((opj_common_ptr)cinfo);
514                         if(!cinfo->jp2_handle) {
515                                 opj_free(cinfo);
516                                 return NULL;
517                         }
518                         break;
519                 case CODEC_JPT:
520                 case CODEC_UNKNOWN:
521                 default:
522                         opj_free(cinfo);
523                         return NULL;
524         }
525
526         cinfo->codec_format = format;
527
528         return cinfo;
529 }
530
531 void OPJ_CALLCONV opj_destroy_compress(opj_cinfo_t *cinfo) {
532         if(cinfo) {
533                 /* destroy the codec */
534                 switch(cinfo->codec_format) {
535                         case CODEC_J2K:
536                                 j2k_destroy_compress((opj_j2k_t*)cinfo->j2k_handle);
537                                 break;
538                         case CODEC_JP2:
539                                 jp2_destroy_compress((opj_jp2_t*)cinfo->jp2_handle);
540                                 break;
541                         case CODEC_JPT:
542                         case CODEC_UNKNOWN:
543                         default:
544                                 break;
545                 }
546                 /* destroy the decompressor */
547                 opj_free(cinfo);
548         }
549 }
550
551 void OPJ_CALLCONV opj_set_default_encoder_parameters(opj_cparameters_t *parameters) {
552         if(parameters) {
553                 memset(parameters, 0, sizeof(opj_cparameters_t));
554                 /* default coding parameters */
555                 parameters->cp_cinema = OFF; 
556                 parameters->max_comp_size = 0;
557                 parameters->numresolution = 6;
558                 parameters->cp_rsiz = STD_RSIZ;
559                 parameters->cblockw_init = 64;
560                 parameters->cblockh_init = 64;
561                 parameters->prog_order = LRCP;
562                 parameters->roi_compno = -1;            /* no ROI */
563                 parameters->subsampling_dx = 1;
564                 parameters->subsampling_dy = 1;
565                 parameters->tp_on = 0;
566                 parameters->decod_format = -1;
567                 parameters->cod_format = -1;
568                 parameters->tcp_rates[0] = 0;   
569                 parameters->tcp_numlayers = 0;
570                 parameters->cp_disto_alloc = 0;
571                 parameters->cp_fixed_alloc = 0;
572                 parameters->cp_fixed_quality = 0;
573                 parameters->jpip_on = OPJ_FALSE;
574 /* UniPG>> */
575 #ifdef USE_JPWL
576                 parameters->jpwl_epc_on = OPJ_FALSE;
577                 parameters->jpwl_hprot_MH = -1; /* -1 means unassigned */
578                 {
579                         int i;
580                         for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
581                                 parameters->jpwl_hprot_TPH_tileno[i] = -1; /* unassigned */
582                                 parameters->jpwl_hprot_TPH[i] = 0; /* absent */
583                         }
584                 };
585                 {
586                         int i;
587                         for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
588                                 parameters->jpwl_pprot_tileno[i] = -1; /* unassigned */
589                                 parameters->jpwl_pprot_packno[i] = -1; /* unassigned */
590                                 parameters->jpwl_pprot[i] = 0; /* absent */
591                         }
592                 };
593                 parameters->jpwl_sens_size = 0; /* 0 means no ESD */
594                 parameters->jpwl_sens_addr = 0; /* 0 means auto */
595                 parameters->jpwl_sens_range = 0; /* 0 means packet */
596                 parameters->jpwl_sens_MH = -1; /* -1 means unassigned */
597                 {
598                         int i;
599                         for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
600                                 parameters->jpwl_sens_TPH_tileno[i] = -1; /* unassigned */
601                                 parameters->jpwl_sens_TPH[i] = -1; /* absent */
602                         }
603                 };
604 #endif /* USE_JPWL */
605 /* <<UniPG */
606         }
607 }
608
609 /**
610  * Helper function.
611  * Sets the stream to be a file stream. The FILE must have been open previously.
612  * @param               p_stream        the stream to modify
613  * @param               p_file          handler to an already open file.
614 */
615 opj_stream_t* OPJ_CALLCONV opj_stream_create_default_file_stream (FILE * p_file, opj_bool p_is_read_stream)
616 {
617         return opj_stream_create_file_stream(p_file,J2K_STREAM_CHUNK_SIZE,p_is_read_stream);
618 }
619
620 opj_stream_t* OPJ_CALLCONV opj_stream_create_file_stream (FILE * p_file, OPJ_UINT32 p_size, opj_bool p_is_read_stream)
621 {
622         opj_stream_t* l_stream = 00;
623
624         if (! p_file) {
625                 return NULL;
626         }
627
628         l_stream = opj_stream_create(p_size,p_is_read_stream);
629         if (! l_stream) {
630                 return NULL;
631         }
632
633         opj_stream_set_user_data(l_stream, p_file);
634         opj_stream_set_user_data_length(l_stream, opj_get_data_length_from_file(p_file));
635         opj_stream_set_read_function(l_stream, (opj_stream_read_fn) opj_read_from_file);
636         opj_stream_set_write_function(l_stream, (opj_stream_write_fn) opj_write_from_file);
637         opj_stream_set_skip_function(l_stream, (opj_stream_skip_fn) opj_skip_from_file);
638         opj_stream_set_seek_function(l_stream, (opj_stream_seek_fn) opj_seek_from_file);
639
640         return l_stream;
641 }
642
643 void OPJ_CALLCONV opj_setup_encoder(opj_cinfo_t *cinfo, opj_cparameters_t *parameters, opj_image_t *image) {
644         if(cinfo && parameters && image) {
645                 switch(cinfo->codec_format) {
646                         case CODEC_J2K:
647                                 j2k_setup_encoder((opj_j2k_t*)cinfo->j2k_handle, parameters, image);
648                                 break;
649                         case CODEC_JP2:
650                                 jp2_setup_encoder((opj_jp2_t*)cinfo->jp2_handle, parameters, image);
651                                 break;
652                         case CODEC_JPT:
653                         case CODEC_UNKNOWN:
654                         default:
655                                 break;
656                 }
657         }
658 }
659
660 opj_bool OPJ_CALLCONV opj_encode(opj_cinfo_t *cinfo, opj_cio_t *cio, opj_image_t *image, char *index) {
661         if (index != NULL)
662                 opj_event_msg((opj_common_ptr)cinfo, EVT_WARNING, "Set index to NULL when calling the opj_encode function.\n"
663                 "To extract the index, use the opj_encode_with_info() function.\n"
664                 "No index will be generated during this encoding\n");
665         return opj_encode_with_info(cinfo, cio, image, NULL);
666 }
667
668 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) {
669         if(cinfo && cio && image) {
670                 switch(cinfo->codec_format) {
671                         case CODEC_J2K:
672                                 return j2k_encode((opj_j2k_t*)cinfo->j2k_handle, cio, image, cstr_info);
673                         case CODEC_JP2:
674                                 return opj_jp2_encode((opj_jp2_t*)cinfo->jp2_handle, cio, image, cstr_info);        
675                         case CODEC_JPT:
676                         case CODEC_UNKNOWN:
677                         default:
678                                 break;
679                 }
680         }
681         return OPJ_FALSE;
682 }
683
684 void OPJ_CALLCONV opj_destroy_cstr_info(opj_codestream_info_t *cstr_info) {
685         if (cstr_info) {
686                 int tileno;
687                 for (tileno = 0; tileno < cstr_info->tw * cstr_info->th; tileno++) {
688                         opj_tile_info_t *tile_info = &cstr_info->tile[tileno];
689                         opj_free(tile_info->thresh);
690                         opj_free(tile_info->packet);
691                         opj_free(tile_info->tp);
692                         opj_free(tile_info->marker);
693                 }
694                 opj_free(cstr_info->tile);
695                 opj_free(cstr_info->marker);
696                 opj_free(cstr_info->numdecompos);
697         }
698 }
699
700
701 opj_bool OPJ_CALLCONV opj_read_header ( opj_stream_t *p_cio,
702                                                                                 opj_codec_t *p_codec,
703                                                                                 opj_image_t **p_image )
704 {
705         if (p_codec && p_cio) {
706                 opj_codec_private_t* l_info = (opj_codec_private_t*) p_codec;
707                 opj_stream_private_t* l_cio = (opj_stream_private_t*) p_cio;
708
709                 if(! l_info->is_decompressor) {
710                         opj_event_msg_v2(l_info->m_event_mgr, EVT_ERROR, "Codec provided to the read_header function is not a decompressor handler.\n");
711                         return OPJ_FALSE;
712                 }
713
714                 return l_info->m_codec_data.m_decompression.opj_read_header(
715                                         l_cio,
716                                         l_info->m_codec,
717                                         p_image,
718                                         l_info->m_event_mgr);
719         }
720
721         fprintf(stderr, "[ERROR] Input parameters of the read_header function are incorrect.\n");
722         return OPJ_FALSE;
723 }
724
725
726 void OPJ_CALLCONV opj_destroy_codec(opj_codec_t *p_info)
727 {
728         if (p_info) {
729                 opj_codec_private_t * l_info = (opj_codec_private_t *) p_info;
730
731                 if (l_info->is_decompressor) {
732                         l_info->m_codec_data.m_decompression.opj_destroy(l_info->m_codec);
733                 }
734                 else {
735                         l_info->m_codec_data.m_compression.opj_destroy(l_info->m_codec);
736                 }
737
738                 l_info->m_codec = 00;
739                 opj_free(l_info);
740         }
741 }
742
743 /**
744  * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
745  *
746  * @param       p_codec                 the jpeg2000 codec.
747  * @param       p_start_x               the left position of the rectangle to decode (in image coordinates).
748  * @param       p_end_x                 the right position of the rectangle to decode (in image coordinates).
749  * @param       p_start_y               the up position of the rectangle to decode (in image coordinates).
750  * @param       p_end_y                 the bottom position of the rectangle to decode (in image coordinates).
751  *
752  * @return      true                    if the area could be set.
753  */
754 opj_bool OPJ_CALLCONV opj_set_decode_area(      opj_codec_t *p_codec,
755                                                                                         opj_image_t* p_image,
756                                                                                         OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
757                                                                                         OPJ_INT32 p_end_x, OPJ_INT32 p_end_y
758                                                                                         )
759 {
760         if (p_codec) {
761                 opj_codec_private_t * l_info = (opj_codec_private_t *) p_codec;
762                 if (! l_info->is_decompressor) {
763                         return OPJ_FALSE;
764                 }
765
766                 return  l_info->m_codec_data.m_decompression.opj_set_decode_area(       l_info->m_codec,
767                                                                                                                                                         p_image,
768                                                                                                                                                         p_start_x, p_start_y,
769                                                                                                                                                         p_end_x, p_end_y,
770                                                                                                                                                         l_info->m_event_mgr);
771
772         }
773         return OPJ_FALSE;
774 }
775
776 /**
777  * Reads a tile header. This function is compulsory and allows one to know the size of the tile thta will be decoded.
778  * The user may need to refer to the image got by opj_read_header to understand the size being taken by the tile.
779  *
780  * @param       p_codec                 the jpeg2000 codec.
781  * @param       p_tile_index    pointer to a value that will hold the index of the tile being decoded, in case of success.
782  * @param       p_data_size             pointer to a value that will hold the maximum size of the decoded data, in case of success. In case
783  *                                                      of truncated codestreams, the actual number of bytes decoded may be lower. The computation of the size is the same
784  *                                                      as depicted in opj_write_tile.
785  * @param       p_tile_x0               pointer to a value that will hold the x0 pos of the tile (in the image).
786  * @param       p_tile_y0               pointer to a value that will hold the y0 pos of the tile (in the image).
787  * @param       p_tile_x1               pointer to a value that will hold the x1 pos of the tile (in the image).
788  * @param       p_tile_y1               pointer to a value that will hold the y1 pos of the tile (in the image).
789  * @param       p_nb_comps              pointer to a value that will hold the number of components in the tile.
790  * @param       p_should_go_on  pointer to a boolean that will hold the fact that the decoding should go on. In case the
791  *                                                      codestream is over at the time of the call, the value will be set to false. The user should then stop
792  *                                                      the decoding.
793  * @param       p_stream                the stream to decode.
794  * @return      true                    if the tile header could be decoded. In case the decoding should end, the returned value is still true.
795  *                                                      returning false may be the result of a shortage of memory or an internal error.
796  */
797 opj_bool OPJ_CALLCONV opj_read_tile_header(
798                                         opj_codec_t *p_codec,
799                                         opj_stream_t * p_stream,
800                                         OPJ_UINT32 * p_tile_index,
801                                         OPJ_UINT32 * p_data_size,
802                                         OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
803                                         OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
804                                         OPJ_UINT32 * p_nb_comps,
805                                         opj_bool * p_should_go_on)
806 {
807         if (p_codec && p_stream && p_data_size && p_tile_index) {
808                 opj_codec_private_t * l_info = (opj_codec_private_t *) p_codec;
809                 opj_stream_private_t * l_cio = (opj_stream_private_t *) p_stream;
810
811                 if (! l_info->is_decompressor) {
812                         return OPJ_FALSE;
813                 }
814
815                 return l_info->m_codec_data.m_decompression.opj_read_tile_header(
816                         l_info->m_codec,
817                         p_tile_index,
818                         p_data_size,
819                         p_tile_x0, p_tile_y0,
820                         p_tile_x1, p_tile_y1,
821                         p_nb_comps,
822                         p_should_go_on,
823                         l_cio,
824                         l_info->m_event_mgr);
825         }
826         return OPJ_FALSE;
827 }
828
829 /**
830  * Reads a tile data. This function is compulsory and allows one to decode tile data. opj_read_tile_header should be called before.
831  * The user may need to refer to the image got by opj_read_header to understand the size being taken by the tile.
832  *
833  * @param       p_codec                 the jpeg2000 codec.
834  * @param       p_tile_index    the index of the tile being decoded, this should be the value set by opj_read_tile_header.
835  * @param       p_data                  pointer to a memory block that will hold the decoded data.
836  * @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.
837  * @param       p_stream                the stream to decode.
838  *
839  * @return      true                    if the data could be decoded.
840  */
841 opj_bool OPJ_CALLCONV opj_decode_tile_data(
842                                         opj_codec_t *p_codec,
843                                         OPJ_UINT32 p_tile_index,
844                                         OPJ_BYTE * p_data,
845                                         OPJ_UINT32 p_data_size,
846                                         opj_stream_t *p_stream
847                                         )
848 {
849         if (p_codec && p_data && p_stream) {
850                 opj_codec_private_t * l_info = (opj_codec_private_t *) p_codec;
851                 opj_stream_private_t * l_cio = (opj_stream_private_t *) p_stream;
852
853                 if (! l_info->is_decompressor) {
854                         return OPJ_FALSE;
855                 }
856
857                 return l_info->m_codec_data.m_decompression.opj_decode_tile_data(       l_info->m_codec,
858                                                                                                                                                         p_tile_index,
859                                                                                                                                                         p_data,
860                                                                                                                                                         p_data_size,
861                                                                                                                                                         l_cio,
862                                                                                                                                                         l_info->m_event_mgr);
863         }
864         return OPJ_FALSE;
865 }
866
867 /*
868  *
869  *
870  */
871 void OPJ_CALLCONV opj_dump_codec(       opj_codec_t *p_codec,
872                                                                         OPJ_INT32 info_flag,
873                                                                         FILE* output_stream)
874 {
875         if (p_codec) {
876                 opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
877
878                 l_codec->opj_dump_codec(l_codec->m_codec, info_flag, output_stream);
879                 return;
880         }
881
882         fprintf(stderr, "[ERROR] Input parameter of the dump_codec function are incorrect.\n");
883         return;
884 }
885
886 /*
887  *
888  *
889  */
890 opj_codestream_info_v2_t* OPJ_CALLCONV opj_get_cstr_info(opj_codec_t *p_codec)
891 {
892         if (p_codec) {
893                 opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
894
895                 return l_codec->opj_get_codec_info(l_codec->m_codec);
896         }
897
898         return NULL;
899 }
900
901 /*
902  *
903  *
904  */
905 void OPJ_CALLCONV opj_destroy_cstr_info_v2(opj_codestream_info_v2_t **cstr_info) {
906         if (cstr_info) {
907
908                 if ((*cstr_info)->m_default_tile_info.tccp_info){
909                         opj_free((*cstr_info)->m_default_tile_info.tccp_info);
910                 }
911
912                 if ((*cstr_info)->tile_info){
913                         /* FIXME not used for the moment*/
914                 }
915
916                 opj_free((*cstr_info));
917                 (*cstr_info) = NULL;
918         }
919 }
920
921 /*
922  *
923  *
924  */
925 opj_codestream_index_t * OPJ_CALLCONV opj_get_cstr_index(opj_codec_t *p_codec)
926 {
927         if (p_codec) {
928                 opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
929
930                 return l_codec->opj_get_codec_index(l_codec->m_codec);
931         }
932
933         return NULL;
934 }
935
936 /*
937  *
938  *
939  */
940 void OPJ_CALLCONV opj_destroy_cstr_index(opj_codestream_index_t **p_cstr_index)
941 {
942         if (*p_cstr_index){
943
944                 j2k_destroy_cstr_index(*p_cstr_index);
945                 (*p_cstr_index) = NULL;
946         }
947 }
948
949
950 opj_bool OPJ_CALLCONV opj_decode_v2(opj_codec_t *p_info,
951                                                                         opj_stream_t *cio,
952                                                                         opj_image_t* p_image)
953 {
954         if (p_info && cio) {
955                 opj_codec_private_t * l_info = (opj_codec_private_t *) p_info;
956                 opj_stream_private_t * l_cio = (opj_stream_private_t *) cio;
957
958                 if (! l_info->is_decompressor) {
959                         return OPJ_FALSE;
960                 }
961
962                 return l_info->m_codec_data.m_decompression.opj_decode( l_info->m_codec,
963                                                                                                                                 l_cio,
964                                                                                                                                 p_image,
965                                                                                                                                 l_info->m_event_mgr);
966         }
967
968         return OPJ_FALSE;
969 }
970
971 /*
972  *
973  *
974  */
975 opj_bool OPJ_CALLCONV opj_end_decompress (opj_codec_t *p_codec,opj_stream_t *p_cio)
976 {
977         if (p_codec && p_cio) {
978                 opj_codec_private_t * l_info = (opj_codec_private_t *) p_codec;
979                 opj_stream_private_t * l_cio = (opj_stream_private_t *) p_cio;
980
981                 if (! l_info->is_decompressor) {
982                         return OPJ_FALSE;
983                 }
984                 return l_info->m_codec_data.m_decompression.opj_end_decompress( l_info->m_codec,
985                                                                                                                                                 l_cio,
986                                                                                                                                                 l_info->m_event_mgr);
987         }
988
989         return OPJ_FALSE;
990 }
991
992 /*
993  *
994  *
995  */
996 opj_bool OPJ_CALLCONV opj_get_decoded_tile(     opj_codec_t *p_codec,
997                                                                                         opj_stream_t *p_cio,
998                                                                                         opj_image_t *p_image,
999                                                                                         OPJ_UINT32 tile_index)
1000 {
1001         if (p_codec && p_cio) {
1002                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
1003                 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_cio;
1004
1005         if (! l_codec->is_decompressor) {
1006                 return OPJ_FALSE;
1007                 }
1008                 return l_codec->m_codec_data.m_decompression.opj_get_decoded_tile(      l_codec->m_codec,
1009                                                                                                                                                         l_stream,
1010                                                                                                                                                         p_image,
1011                                                                                                                                                         l_codec->m_event_mgr,
1012                                                                                                                                                         tile_index);
1013         }
1014
1015         return OPJ_FALSE;
1016 }
1017
1018 /*
1019  *
1020  *
1021  */
1022 opj_bool OPJ_CALLCONV opj_set_decoded_resolution_factor(opj_codec_t *p_codec, OPJ_UINT32 res_factor)
1023 {
1024         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
1025
1026         if ( !l_codec ){
1027                 fprintf(stderr, "[ERROR] Input parameters of the setup_decoder function are incorrect.\n");
1028                 return OPJ_FALSE;
1029         }
1030
1031
1032         l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor(l_codec->m_codec, res_factor, l_codec->m_event_mgr);
1033
1034         return OPJ_TRUE;
1035 }