[trunk] add test_tile_encoder test and function related
[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,
100                                                                                 struct opj_stream_private *cio,
101                                                                                 struct opj_image * p_image,
102                                                                                 struct opj_event_mgr * p_manager);
103
104         opj_bool (* opj_encode) (       void * p_codec,
105                                                                 struct opj_stream_private *p_cio,
106                                                                 struct opj_event_mgr * p_manager);
107
108         opj_bool (* opj_write_tile) (   void * p_codec,
109                                                                         OPJ_UINT32 p_tile_index,
110                                                                         OPJ_BYTE * p_data,
111                                                                         OPJ_UINT32 p_data_size,
112                                                                         struct opj_stream_private * p_cio,
113                                                                         struct opj_event_mgr * p_manager);
114
115         opj_bool (* opj_end_compress) ( void * p_codec,
116                                                                         struct opj_stream_private *p_cio,
117                                                                         struct opj_event_mgr * p_manager);
118
119         void (* opj_destroy) (void * p_codec);
120
121         void (*opj_setup_encoder) (     void * p_codec,
122                                                                 opj_cparameters_t * p_param,
123                                                                 struct opj_image * p_image,
124                                                                 struct opj_event_mgr * p_manager);
125
126 }opj_compression_t;
127
128 /**
129  * Main codec handler used for compression or decompression.
130  */
131 typedef struct opj_codec_private
132 {
133         /** FIXME DOC */
134         union {
135                 opj_decompression_t m_decompression;
136                 opj_compression_t m_compression;
137     } m_codec_data;
138     /** FIXME DOC*/
139         void * m_codec;
140         /** Event handler */
141         opj_event_mgr_t* m_event_mgr;
142         /** Flag to indicate if the codec is used to decode or encode*/
143         opj_bool is_decompressor;
144         void (*opj_dump_codec) (void * p_codec, OPJ_INT32 info_flag, FILE* output_stream);
145         opj_codestream_info_v2_t* (*opj_get_codec_info)(void* p_codec);
146         opj_codestream_index_t* (*opj_get_codec_index)(void* p_codec);
147 }
148 opj_codec_private_t;
149
150
151
152 /* ---------------------------------------------------------------------- */
153
154
155 OPJ_SIZE_T opj_read_from_file (void * p_buffer, OPJ_SIZE_T p_nb_bytes, FILE * p_file)
156 {
157         OPJ_SIZE_T l_nb_read = fread(p_buffer,1,p_nb_bytes,p_file);
158         return l_nb_read ? l_nb_read : -1;
159 }
160
161 OPJ_UINT64 opj_get_data_length_from_file (FILE * p_file)
162 {
163         OPJ_OFF_T file_length = 0;
164
165         OPJ_FSEEK(p_file, 0, SEEK_END);
166         file_length = (OPJ_UINT64)OPJ_FTELL(p_file);
167         OPJ_FSEEK(p_file, 0, SEEK_SET);
168
169         return file_length;
170 }
171
172 OPJ_SIZE_T opj_write_from_file (void * p_buffer, OPJ_SIZE_T p_nb_bytes, FILE * p_file)
173 {
174         return fwrite(p_buffer,1,p_nb_bytes,p_file);
175 }
176
177 OPJ_OFF_T opj_skip_from_file (OPJ_OFF_T p_nb_bytes, FILE * p_user_data)
178 {
179         if (OPJ_FSEEK(p_user_data,p_nb_bytes,SEEK_CUR)) {
180                 return -1;
181         }
182
183         return p_nb_bytes;
184 }
185
186 opj_bool opj_seek_from_file (OPJ_OFF_T p_nb_bytes, FILE * p_user_data)
187 {
188         if (OPJ_FSEEK(p_user_data,p_nb_bytes,SEEK_SET)) {
189                 return EXIT_FAILURE;
190         }
191
192         return EXIT_SUCCESS;
193 }
194
195 /* ---------------------------------------------------------------------- */
196 #ifdef _WIN32
197 #ifndef OPJ_STATIC
198 BOOL APIENTRY
199 DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
200
201         OPJ_ARG_NOT_USED(lpReserved);
202         OPJ_ARG_NOT_USED(hModule);
203
204         switch (ul_reason_for_call) {
205                 case DLL_PROCESS_ATTACH :
206                         break;
207                 case DLL_PROCESS_DETACH :
208                         break;
209                 case DLL_THREAD_ATTACH :
210                 case DLL_THREAD_DETACH :
211                         break;
212     }
213
214     return TRUE;
215 }
216 #endif /* OPJ_STATIC */
217 #endif /* _WIN32 */
218
219 /* ---------------------------------------------------------------------- */
220
221
222 const char* OPJ_CALLCONV opj_version(void) {
223     return OPJ_PACKAGE_VERSION;
224 }
225
226
227 /* DEPRECATED */
228 opj_dinfo_t* OPJ_CALLCONV opj_create_decompress(OPJ_CODEC_FORMAT format) {
229         opj_dinfo_t *dinfo = (opj_dinfo_t*)opj_calloc(1, sizeof(opj_dinfo_t));
230         if(!dinfo) return NULL;
231         dinfo->is_decompressor = OPJ_TRUE;
232         switch(format) {
233                 case CODEC_J2K:
234                 case CODEC_JPT:
235                         /* get a J2K decoder handle */
236                         dinfo->j2k_handle = (void*)j2k_create_decompress((opj_common_ptr)dinfo);
237                         if(!dinfo->j2k_handle) {
238                                 opj_free(dinfo);
239                                 return NULL;
240                         }
241                         break;
242                 case CODEC_JP2:
243                         /* get a JP2 decoder handle */
244                         dinfo->jp2_handle = (void*)jp2_create_decompress((opj_common_ptr)dinfo);
245                         if(!dinfo->jp2_handle) {
246                                 opj_free(dinfo);
247                                 return NULL;
248                         }
249                         break;
250                 case CODEC_UNKNOWN:
251                 default:
252                         opj_free(dinfo);
253                         return NULL;
254         }
255
256         dinfo->codec_format = format;
257
258         return dinfo;
259 }
260
261 opj_codec_t* OPJ_CALLCONV opj_create_decompress_v2(OPJ_CODEC_FORMAT p_format)
262 {
263         opj_codec_private_t *l_info = 00;
264
265         l_info = (opj_codec_private_t*) opj_calloc(1, sizeof(opj_codec_private_t));
266         if (!l_info){
267                 return 00;
268         }
269         memset(l_info, 0, sizeof(opj_codec_private_t));
270
271         l_info->is_decompressor = 1;
272
273         switch (p_format) {
274                 case CODEC_J2K:
275                         l_info->opj_dump_codec = (void (*) (void*, OPJ_INT32, FILE*)) j2k_dump;
276
277                         l_info->opj_get_codec_info = (opj_codestream_info_v2_t* (*) (void*) ) j2k_get_cstr_info;
278
279                         l_info->opj_get_codec_index = (opj_codestream_index_t* (*) (void*) ) j2k_get_cstr_index;
280
281                         l_info->m_codec_data.m_decompression.opj_decode =
282                                         (opj_bool (*) ( void *,
283                                                                         struct opj_stream_private *,
284                                                                         opj_image_t*, struct opj_event_mgr * )) j2k_decode_v2;
285
286                         l_info->m_codec_data.m_decompression.opj_end_decompress =
287                                         (opj_bool (*) ( void *,
288                                                                         struct opj_stream_private *,
289                                                                         struct opj_event_mgr *)) j2k_end_decompress;
290
291                         l_info->m_codec_data.m_decompression.opj_read_header =
292                                         (opj_bool (*) ( struct opj_stream_private *,
293                                                                         void *,
294                                                                         opj_image_t **,
295                                                                         struct opj_event_mgr * )) j2k_read_header;
296
297                         l_info->m_codec_data.m_decompression.opj_destroy =
298                                         (void (*) (void *))j2k_destroy;
299
300                         l_info->m_codec_data.m_decompression.opj_setup_decoder =
301                                         (void (*) (void * , opj_dparameters_t * )) j2k_setup_decoder_v2;
302
303                         l_info->m_codec_data.m_decompression.opj_read_tile_header =
304                                         (opj_bool (*) ( void *,
305                                                                         OPJ_UINT32*,
306                                                                         OPJ_UINT32*,
307                                                                         OPJ_INT32*, OPJ_INT32*,
308                                                                         OPJ_INT32*, OPJ_INT32*,
309                                                                         OPJ_UINT32*,
310                                                                         opj_bool*,
311                                                                         struct opj_stream_private *,
312                                                                         struct opj_event_mgr * )) j2k_read_tile_header;
313
314                         l_info->m_codec_data.m_decompression.opj_decode_tile_data =
315                                         (opj_bool (*) (void *, OPJ_UINT32, OPJ_BYTE*, OPJ_UINT32, struct opj_stream_private *, struct opj_event_mgr *)) j2k_decode_tile;
316
317                         l_info->m_codec_data.m_decompression.opj_set_decode_area =
318                                         (opj_bool (*) (void *, opj_image_t*, OPJ_INT32, OPJ_INT32, OPJ_INT32, OPJ_INT32, struct opj_event_mgr *)) j2k_set_decode_area;
319
320                         l_info->m_codec_data.m_decompression.opj_get_decoded_tile = (opj_bool (*) (     void *p_codec,
321                                                                                                                                                                                 opj_stream_private_t *p_cio,
322                                                                                                                                                                                 opj_image_t *p_image,
323                                                                                                                                                                                 struct opj_event_mgr * p_manager,
324                                                                                                                                                                                 OPJ_UINT32 tile_index)) j2k_get_tile;
325
326                         l_info->m_codec_data.m_decompression.opj_set_decoded_resolution_factor = (opj_bool (*) (void * p_codec,
327                                                                                                                                                                                                         OPJ_UINT32 res_factor,
328                                                                                                                                                                                                         struct opj_event_mgr * p_manager)) j2k_set_decoded_resolution_factor;
329
330                         l_info->m_codec = j2k_create_decompress_v2();
331
332                         if (! l_info->m_codec) {
333                                 opj_free(l_info);
334                                 return NULL;
335                         }
336
337                         break;
338
339                 case CODEC_JP2:
340                         /* get a JP2 decoder handle */
341                         l_info->opj_dump_codec = (void (*) (void*, OPJ_INT32, FILE*)) jp2_dump;
342
343                         l_info->opj_get_codec_info = (opj_codestream_info_v2_t* (*) (void*) ) jp2_get_cstr_info;
344
345                         l_info->opj_get_codec_index = (opj_codestream_index_t* (*) (void*) ) jp2_get_cstr_index;
346
347                         l_info->m_codec_data.m_decompression.opj_decode =
348                                         (opj_bool (*) ( void *,
349                                                                         struct opj_stream_private *,
350                                                                         opj_image_t*,
351                                                                         struct opj_event_mgr * )) jp2_decode_v2;
352
353                         l_info->m_codec_data.m_decompression.opj_end_decompress =  (opj_bool (*) (void *,struct opj_stream_private *,struct opj_event_mgr *)) jp2_end_decompress;
354
355                         l_info->m_codec_data.m_decompression.opj_read_header =  (opj_bool (*) (
356                                         struct opj_stream_private *,
357                                         void *,
358                                         opj_image_t **,
359                                         struct opj_event_mgr * )) jp2_read_header;
360
361                         l_info->m_codec_data.m_decompression.opj_read_tile_header = ( opj_bool (*) (
362                                         void *,
363                                         OPJ_UINT32*,
364                                         OPJ_UINT32*,
365                                         OPJ_INT32*,
366                                         OPJ_INT32*,
367                                         OPJ_INT32 * ,
368                                         OPJ_INT32 * ,
369                                         OPJ_UINT32 * ,
370                                         opj_bool *,
371                                         struct opj_stream_private *,
372                                         struct opj_event_mgr * )) jp2_read_tile_header;
373
374                         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;
375
376                         l_info->m_codec_data.m_decompression.opj_destroy = (void (*) (void *))jp2_destroy;
377
378                         l_info->m_codec_data.m_decompression.opj_setup_decoder = (void (*) (void * ,opj_dparameters_t * )) jp2_setup_decoder_v2;
379
380                         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;
381
382                         l_info->m_codec_data.m_decompression.opj_get_decoded_tile = (opj_bool (*) (     void *p_codec,
383                                                                                                                                                                                 opj_stream_private_t *p_cio,
384                                                                                                                                                                                 opj_image_t *p_image,
385                                                                                                                                                                                 struct opj_event_mgr * p_manager,
386                                                                                                                                                                                 OPJ_UINT32 tile_index)) jp2_get_tile;
387
388                         l_info->m_codec_data.m_decompression.opj_set_decoded_resolution_factor = (opj_bool (*) (void * p_codec,
389                                                                                                                                                                                                         OPJ_UINT32 res_factor,
390                                                                                                                                                                                                         opj_event_mgr_t * p_manager)) jp2_set_decoded_resolution_factor;
391
392                         l_info->m_codec = jp2_create(OPJ_TRUE);
393
394                         if (! l_info->m_codec) {
395                                 opj_free(l_info);
396                                 return 00;
397                         }
398
399                         break;
400                 case CODEC_UNKNOWN:
401                 case CODEC_JPT:
402                 default:
403                         opj_free(l_info);
404                         return 00;
405         }
406
407         return (opj_codec_t*) l_info;
408 }
409
410 /* DEPRECATED */
411 void OPJ_CALLCONV opj_destroy_decompress(opj_dinfo_t *dinfo) {
412         if(dinfo) {
413                 /* destroy the codec */
414                 switch(dinfo->codec_format) {
415                         case CODEC_J2K:
416                         case CODEC_JPT:
417                                 j2k_destroy_decompress((opj_j2k_t*)dinfo->j2k_handle);
418                                 break;
419                         case CODEC_JP2:
420                                 jp2_destroy_decompress((opj_jp2_t*)dinfo->jp2_handle);
421                                 break;
422                         case CODEC_UNKNOWN:
423                         default:
424                                 break;
425                 }
426                 /* destroy the decompressor */
427                 opj_free(dinfo);
428         }
429 }
430
431 void OPJ_CALLCONV opj_set_default_decoder_parameters(opj_dparameters_t *parameters) {
432         if(parameters) {
433                 memset(parameters, 0, sizeof(opj_dparameters_t));
434                 /* default decoding parameters */
435                 parameters->cp_layer = 0;
436                 parameters->cp_reduce = 0;
437                 parameters->cp_limit_decoding = NO_LIMITATION;
438
439                 parameters->decod_format = -1;
440                 parameters->cod_format = -1;
441                 parameters->flags = 0;          
442 /* UniPG>> */
443 #ifdef USE_JPWL
444                 parameters->jpwl_correct = OPJ_FALSE;
445                 parameters->jpwl_exp_comps = JPWL_EXPECTED_COMPONENTS;
446                 parameters->jpwl_max_tiles = JPWL_MAXIMUM_TILES;
447 #endif /* USE_JPWL */
448 /* <<UniPG */
449         }
450 }
451
452 /* DEPRECATED */
453 void OPJ_CALLCONV opj_setup_decoder(opj_dinfo_t *dinfo, opj_dparameters_t *parameters) {
454         if(dinfo && parameters) {
455                 switch(dinfo->codec_format) {
456                         case CODEC_J2K:
457                         case CODEC_JPT:
458                                 j2k_setup_decoder((opj_j2k_t*)dinfo->j2k_handle, parameters);
459                                 break;
460                         case CODEC_JP2:
461                                 jp2_setup_decoder((opj_jp2_t*)dinfo->jp2_handle, parameters);
462                                 break;
463                         case CODEC_UNKNOWN:
464                         default:
465                                 break;
466                 }
467         }
468 }
469
470 opj_bool OPJ_CALLCONV opj_setup_decoder_v2(opj_codec_t *p_info, opj_dparameters_t *parameters, opj_event_mgr_t* event_mgr)
471 {
472         opj_codec_private_t * l_info = (opj_codec_private_t *) p_info;
473
474         if ( !p_info || !parameters || !event_mgr ){
475                 fprintf(stderr, "[ERROR] Input parameters of the setup_decoder function are incorrect.\n");
476                 return OPJ_FALSE;
477         }
478
479         if ( !event_mgr->error_handler || !event_mgr->warning_handler || !event_mgr->error_handler){
480                 fprintf(stderr, "[ERROR] Event handler provided to the setup_decoder function is not valid.\n");
481                 return OPJ_FALSE;
482         }
483
484         if (! l_info->is_decompressor) {
485                 opj_event_msg_v2(event_mgr, EVT_ERROR, "Codec provided to the setup_decoder function is not a decompressor handler.\n");
486                 return OPJ_FALSE;
487         }
488
489         l_info->m_codec_data.m_decompression.opj_setup_decoder(l_info->m_codec, parameters);
490
491         l_info->m_event_mgr = event_mgr;
492
493         return OPJ_TRUE;
494 }
495
496 /* DEPRECATED */
497 opj_image_t* OPJ_CALLCONV opj_decode(opj_dinfo_t *dinfo, opj_cio_t *cio) {
498         return opj_decode_with_info(dinfo, cio, NULL);
499 }
500
501 /* DEPRECATED */
502 opj_image_t* OPJ_CALLCONV opj_decode_with_info(opj_dinfo_t *dinfo, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
503         if(dinfo && cio) {
504                 switch(dinfo->codec_format) {
505                         case CODEC_J2K:
506                                 return j2k_decode((opj_j2k_t*)dinfo->j2k_handle, cio, cstr_info);
507                         case CODEC_JPT:
508                                 return j2k_decode_jpt_stream((opj_j2k_t*)dinfo->j2k_handle, cio, cstr_info);
509                         case CODEC_JP2:
510                                 return opj_jp2_decode((opj_jp2_t*)dinfo->jp2_handle, cio, cstr_info);
511                         case CODEC_UNKNOWN:
512                         default:
513                                 break;
514                 }
515         }
516         return NULL;
517 }
518
519 opj_cinfo_t* OPJ_CALLCONV opj_create_compress(OPJ_CODEC_FORMAT format) {
520         opj_cinfo_t *cinfo = (opj_cinfo_t*)opj_calloc(1, sizeof(opj_cinfo_t));
521         if(!cinfo) return NULL;
522         cinfo->is_decompressor = OPJ_FALSE;
523         switch(format) {
524                 case CODEC_J2K:
525                         /* get a J2K coder handle */
526                         cinfo->j2k_handle = (void*)j2k_create_compress((opj_common_ptr)cinfo);
527                         if(!cinfo->j2k_handle) {
528                                 opj_free(cinfo);
529                                 return NULL;
530                         }
531                         break;
532                 case CODEC_JP2:
533                         /* get a JP2 coder handle */
534                         cinfo->jp2_handle = (void*)jp2_create_compress((opj_common_ptr)cinfo);
535                         if(!cinfo->jp2_handle) {
536                                 opj_free(cinfo);
537                                 return NULL;
538                         }
539                         break;
540                 case CODEC_JPT:
541                 case CODEC_UNKNOWN:
542                 default:
543                         opj_free(cinfo);
544                         return NULL;
545         }
546
547         cinfo->codec_format = format;
548
549         return cinfo;
550 }
551
552
553 opj_codec_t* OPJ_CALLCONV opj_create_compress_v2(OPJ_CODEC_FORMAT p_format)
554 {
555         opj_codec_private_t *l_info = 00;
556
557         l_info = (opj_codec_private_t*)opj_calloc(1, sizeof(opj_codec_private_t));
558         if (!l_info) {
559                 return 00;
560         }
561
562         memset(l_info, 0, sizeof(opj_codec_private_t));
563         l_info->is_decompressor = 0;
564
565         switch(p_format) {
566                 case CODEC_J2K:
567                         l_info->m_codec_data.m_compression.opj_encode = (opj_bool (*) ( void *,
568                                                                                                                                                         struct opj_stream_private *,
569                                                                                                                                                         struct opj_event_mgr * )) j2k_encode_v2;
570
571                         l_info->m_codec_data.m_compression.opj_end_compress = (opj_bool (*) (   void *,
572                                                                                                                                                                         struct opj_stream_private *,
573                                                                                                                                                                         struct opj_event_mgr *)) j2k_end_compress;
574
575                         l_info->m_codec_data.m_compression.opj_start_compress = (opj_bool (*) ( void *,
576                                                                                                                                                                         struct opj_stream_private *,
577                                                                                                                                                                         struct opj_image * ,
578                                                                                                                                                                         struct opj_event_mgr *)) j2k_start_compress;
579
580                         l_info->m_codec_data.m_compression.opj_write_tile = (opj_bool (*) (     void *,
581                                                                                                                                                                 OPJ_UINT32,
582                                                                                                                                                                 OPJ_BYTE*,
583                                                                                                                                                                 OPJ_UINT32,
584                                                                                                                                                                 struct opj_stream_private *,
585                                                                                                                                                                 struct opj_event_mgr *) ) j2k_write_tile;
586
587                         l_info->m_codec_data.m_compression.opj_destroy = (void (*) (void *)) j2k_destroy;
588
589                         l_info->m_codec_data.m_compression.opj_setup_encoder = (void (*) (      void *,
590                                                                                                                                                                 opj_cparameters_t *,
591                                                                                                                                                                 struct opj_image *,
592                                                                                                                                                                 struct opj_event_mgr * )) j2k_setup_encoder_v2;
593
594                         l_info->m_codec = j2k_create_compress_v2();
595                         if (! l_info->m_codec) {
596                                 opj_free(l_info);
597                                 return 00;
598                         }
599
600                         break;
601
602                 case CODEC_JP2:
603                         /* get a JP2 decoder handle */
604                         l_info->m_codec_data.m_compression.opj_encode = (opj_bool (*) ( void *,
605                                                                                                                                                         struct opj_stream_private *,
606                                                                                                                                                         struct opj_event_mgr * )) opj_jp2_encode;
607
608                         l_info->m_codec_data.m_compression.opj_end_compress = (opj_bool (*) (   void *,
609                                                                                                                                                                         struct opj_stream_private *,
610                                                                                                                                                                         struct opj_event_mgr *)) jp2_end_compress;
611
612                         l_info->m_codec_data.m_compression.opj_start_compress = (opj_bool (*) ( void *,
613                                                                                                                                                                         struct opj_stream_private *,
614                                                                                                                                                                         struct opj_image * ,
615                                                                                                                                                                         struct opj_event_mgr *))  jp2_start_compress;
616
617                         l_info->m_codec_data.m_compression.opj_write_tile = (opj_bool (*) (     void *,
618                                                                                                                                                                 OPJ_UINT32,
619                                                                                                                                                                 OPJ_BYTE*,
620                                                                                                                                                                 OPJ_UINT32,
621                                                                                                                                                                 struct opj_stream_private *,
622                                                                                                                                                                 struct opj_event_mgr *)) jp2_write_tile;
623
624                         l_info->m_codec_data.m_compression.opj_destroy = (void (*) (void *)) jp2_destroy;
625
626                         l_info->m_codec_data.m_compression.opj_setup_encoder = (void (*) (      void *,
627                                                                                                                                                                 opj_cparameters_t *,
628                                                                                                                                                                 struct opj_image *,
629                                                                                                                                                                 struct opj_event_mgr * )) jp2_setup_encoder;
630
631                         l_info->m_codec = jp2_create(OPJ_FALSE);
632                         if (! l_info->m_codec) {
633                                 opj_free(l_info);
634                                 return 00;
635                         }
636
637                         break;
638
639                 case CODEC_UNKNOWN:
640                 case CODEC_JPT:
641                 default:
642                         opj_free(l_info);
643                         return 00;
644         }
645
646         /*set_default_event_handler(&(l_info->m_event_mgr));*/
647         return (opj_codec_t*) l_info;
648 }
649
650
651 void OPJ_CALLCONV opj_destroy_compress(opj_cinfo_t *cinfo) {
652         if(cinfo) {
653                 /* destroy the codec */
654                 switch(cinfo->codec_format) {
655                         case CODEC_J2K:
656                                 j2k_destroy_compress((opj_j2k_t*)cinfo->j2k_handle);
657                                 break;
658                         case CODEC_JP2:
659                                 jp2_destroy_compress((opj_jp2_t*)cinfo->jp2_handle);
660                                 break;
661                         case CODEC_JPT:
662                         case CODEC_UNKNOWN:
663                         default:
664                                 break;
665                 }
666                 /* destroy the decompressor */
667                 opj_free(cinfo);
668         }
669 }
670
671 void OPJ_CALLCONV opj_set_default_encoder_parameters(opj_cparameters_t *parameters) {
672         if(parameters) {
673                 memset(parameters, 0, sizeof(opj_cparameters_t));
674                 /* default coding parameters */
675                 parameters->cp_cinema = OFF; 
676                 parameters->max_comp_size = 0;
677                 parameters->numresolution = 6;
678                 parameters->cp_rsiz = STD_RSIZ;
679                 parameters->cblockw_init = 64;
680                 parameters->cblockh_init = 64;
681                 parameters->prog_order = LRCP;
682                 parameters->roi_compno = -1;            /* no ROI */
683                 parameters->subsampling_dx = 1;
684                 parameters->subsampling_dy = 1;
685                 parameters->tp_on = 0;
686                 parameters->decod_format = -1;
687                 parameters->cod_format = -1;
688                 parameters->tcp_rates[0] = 0;   
689                 parameters->tcp_numlayers = 0;
690                 parameters->cp_disto_alloc = 0;
691                 parameters->cp_fixed_alloc = 0;
692                 parameters->cp_fixed_quality = 0;
693                 parameters->jpip_on = OPJ_FALSE;
694 /* UniPG>> */
695 #ifdef USE_JPWL
696                 parameters->jpwl_epc_on = OPJ_FALSE;
697                 parameters->jpwl_hprot_MH = -1; /* -1 means unassigned */
698                 {
699                         int i;
700                         for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
701                                 parameters->jpwl_hprot_TPH_tileno[i] = -1; /* unassigned */
702                                 parameters->jpwl_hprot_TPH[i] = 0; /* absent */
703                         }
704                 };
705                 {
706                         int i;
707                         for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
708                                 parameters->jpwl_pprot_tileno[i] = -1; /* unassigned */
709                                 parameters->jpwl_pprot_packno[i] = -1; /* unassigned */
710                                 parameters->jpwl_pprot[i] = 0; /* absent */
711                         }
712                 };
713                 parameters->jpwl_sens_size = 0; /* 0 means no ESD */
714                 parameters->jpwl_sens_addr = 0; /* 0 means auto */
715                 parameters->jpwl_sens_range = 0; /* 0 means packet */
716                 parameters->jpwl_sens_MH = -1; /* -1 means unassigned */
717                 {
718                         int i;
719                         for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
720                                 parameters->jpwl_sens_TPH_tileno[i] = -1; /* unassigned */
721                                 parameters->jpwl_sens_TPH[i] = -1; /* absent */
722                         }
723                 };
724 #endif /* USE_JPWL */
725 /* <<UniPG */
726         }
727 }
728
729 /**
730  * Helper function.
731  * Sets the stream to be a file stream. The FILE must have been open previously.
732  * @param               p_stream        the stream to modify
733  * @param               p_file          handler to an already open file.
734 */
735 opj_stream_t* OPJ_CALLCONV opj_stream_create_default_file_stream (FILE * p_file, opj_bool p_is_read_stream)
736 {
737         return opj_stream_create_file_stream(p_file,J2K_STREAM_CHUNK_SIZE,p_is_read_stream);
738 }
739
740 opj_stream_t* OPJ_CALLCONV opj_stream_create_file_stream (FILE * p_file, OPJ_SIZE_T p_size, opj_bool p_is_read_stream)
741 {
742         opj_stream_t* l_stream = 00;
743
744         if (! p_file) {
745                 return NULL;
746         }
747
748         l_stream = opj_stream_create(p_size,p_is_read_stream);
749         if (! l_stream) {
750                 return NULL;
751         }
752
753         opj_stream_set_user_data(l_stream, p_file);
754         opj_stream_set_user_data_length(l_stream, opj_get_data_length_from_file(p_file));
755         opj_stream_set_read_function(l_stream, (opj_stream_read_fn) opj_read_from_file);
756         opj_stream_set_write_function(l_stream, (opj_stream_write_fn) opj_write_from_file);
757         opj_stream_set_skip_function(l_stream, (opj_stream_skip_fn) opj_skip_from_file);
758         opj_stream_set_seek_function(l_stream, (opj_stream_seek_fn) opj_seek_from_file);
759
760         return l_stream;
761 }
762
763 void OPJ_CALLCONV opj_setup_encoder(opj_cinfo_t *cinfo, opj_cparameters_t *parameters, opj_image_t *image) {
764         if(cinfo && parameters && image) {
765                 switch(cinfo->codec_format) {
766                         case CODEC_J2K:
767                                 j2k_setup_encoder((opj_j2k_t*)cinfo->j2k_handle, parameters, image);
768                                 break;
769                         case CODEC_JP2:
770                                 jp2_setup_encoder((opj_jp2_t*)cinfo->jp2_handle, parameters, image);
771                                 break;
772                         case CODEC_JPT:
773                         case CODEC_UNKNOWN:
774                         default:
775                                 break;
776                 }
777         }
778 }
779
780 opj_bool OPJ_CALLCONV opj_setup_encoder_v2(opj_codec_t *p_info, opj_cparameters_t *parameters, opj_image_t *image)
781 {
782         if (p_info && parameters && image) {
783                 opj_codec_private_t * l_codec = ((opj_codec_private_t *) p_info);
784
785                 if (! l_codec->is_decompressor) {
786                         l_codec->m_codec_data.m_compression.opj_setup_encoder(l_codec->m_codec,parameters,image,l_codec->m_event_mgr);
787                         return OPJ_TRUE;
788                 }
789         }
790
791         return OPJ_FALSE;
792 }
793
794 opj_bool OPJ_CALLCONV opj_start_compress (      opj_codec_t *p_codec,
795                                                                                         opj_image_t * p_image,
796                                                                                         opj_stream_t *p_cio)
797 {
798         if (p_codec && p_cio) {
799                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
800                 opj_stream_private_t * l_cio = (opj_stream_private_t *) p_cio;
801
802                 if (! l_codec->is_decompressor) {
803                         return l_codec->m_codec_data.m_compression.opj_start_compress(l_codec->m_codec,l_cio,p_image,l_codec->m_event_mgr);
804                 }
805         }
806
807         return OPJ_FALSE;
808 }
809
810 opj_bool OPJ_CALLCONV opj_encode_v2(opj_codec_t *p_info, opj_stream_t *cio)
811 {
812         if (p_info && cio) {
813                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_info;
814                 opj_stream_private_t * l_cio = (opj_stream_private_t *) cio;
815
816                 if (! l_codec->is_decompressor) {
817                         l_codec->m_codec_data.m_compression.opj_encode(l_codec->m_codec,l_cio,l_codec->m_event_mgr);
818                         return OPJ_TRUE;
819                 }
820         }
821
822         return OPJ_FALSE;
823
824 }
825
826 opj_bool OPJ_CALLCONV opj_end_compress (opj_codec_t *p_codec,opj_stream_t *p_cio)
827 {
828         if (p_codec && p_cio) {
829                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
830                 opj_stream_private_t * l_cio = (opj_stream_private_t *) p_cio;
831
832                 if (! l_codec->is_decompressor) {
833                         return l_codec->m_codec_data.m_compression.opj_end_compress(l_codec->m_codec,l_cio,l_codec->m_event_mgr);
834                 }
835         }
836         return OPJ_FALSE;
837
838 }
839
840 opj_bool OPJ_CALLCONV opj_encode(opj_cinfo_t *cinfo, opj_cio_t *cio, opj_image_t *image, char *index) {
841         if (index != NULL)
842                 opj_event_msg((opj_common_ptr)cinfo, EVT_WARNING, "Set index to NULL when calling the opj_encode function.\n"
843                 "To extract the index, use the opj_encode_with_info() function.\n"
844                 "No index will be generated during this encoding\n");
845         return opj_encode_with_info(cinfo, cio, image, NULL);
846 }
847
848 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) {
849         if(cinfo && cio && image) {
850                 switch(cinfo->codec_format) {
851                         case CODEC_J2K:
852                                 return j2k_encode((opj_j2k_t*)cinfo->j2k_handle, cio, image, cstr_info);
853                         case CODEC_JP2:
854                                 return opj_jp2_encode((opj_jp2_t*)cinfo->jp2_handle, cio, image, cstr_info);        
855                         case CODEC_JPT:
856                         case CODEC_UNKNOWN:
857                         default:
858                                 break;
859                 }
860         }
861         return OPJ_FALSE;
862 }
863
864 void OPJ_CALLCONV opj_destroy_cstr_info(opj_codestream_info_t *cstr_info) {
865         if (cstr_info) {
866                 int tileno;
867                 for (tileno = 0; tileno < cstr_info->tw * cstr_info->th; tileno++) {
868                         opj_tile_info_t *tile_info = &cstr_info->tile[tileno];
869                         opj_free(tile_info->thresh);
870                         opj_free(tile_info->packet);
871                         opj_free(tile_info->tp);
872                         opj_free(tile_info->marker);
873                 }
874                 opj_free(cstr_info->tile);
875                 opj_free(cstr_info->marker);
876                 opj_free(cstr_info->numdecompos);
877         }
878 }
879
880
881 opj_bool OPJ_CALLCONV opj_read_header ( opj_stream_t *p_cio,
882                                                                                 opj_codec_t *p_codec,
883                                                                                 opj_image_t **p_image )
884 {
885         if (p_codec && p_cio) {
886                 opj_codec_private_t* l_info = (opj_codec_private_t*) p_codec;
887                 opj_stream_private_t* l_cio = (opj_stream_private_t*) p_cio;
888
889                 if(! l_info->is_decompressor) {
890                         opj_event_msg_v2(l_info->m_event_mgr, EVT_ERROR, "Codec provided to the read_header function is not a decompressor handler.\n");
891                         return OPJ_FALSE;
892                 }
893
894                 return l_info->m_codec_data.m_decompression.opj_read_header(
895                                         l_cio,
896                                         l_info->m_codec,
897                                         p_image,
898                                         l_info->m_event_mgr);
899         }
900
901         fprintf(stderr, "[ERROR] Input parameters of the read_header function are incorrect.\n");
902         return OPJ_FALSE;
903 }
904
905
906 void OPJ_CALLCONV opj_destroy_codec(opj_codec_t *p_info)
907 {
908         if (p_info) {
909                 opj_codec_private_t * l_info = (opj_codec_private_t *) p_info;
910
911                 if (l_info->is_decompressor) {
912                         l_info->m_codec_data.m_decompression.opj_destroy(l_info->m_codec);
913                 }
914                 else {
915                         l_info->m_codec_data.m_compression.opj_destroy(l_info->m_codec);
916                 }
917
918                 l_info->m_codec = 00;
919                 opj_free(l_info);
920         }
921 }
922
923 /**
924  * Sets the given area to be decoded. This function should be called right after opj_read_header and before any tile header reading.
925  *
926  * @param       p_codec                 the jpeg2000 codec.
927  * @param       p_start_x               the left position of the rectangle to decode (in image coordinates).
928  * @param       p_end_x                 the right position of the rectangle to decode (in image coordinates).
929  * @param       p_start_y               the up position of the rectangle to decode (in image coordinates).
930  * @param       p_end_y                 the bottom position of the rectangle to decode (in image coordinates).
931  *
932  * @return      true                    if the area could be set.
933  */
934 opj_bool OPJ_CALLCONV opj_set_decode_area(      opj_codec_t *p_codec,
935                                                                                         opj_image_t* p_image,
936                                                                                         OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
937                                                                                         OPJ_INT32 p_end_x, OPJ_INT32 p_end_y
938                                                                                         )
939 {
940         if (p_codec) {
941                 opj_codec_private_t * l_info = (opj_codec_private_t *) p_codec;
942                 if (! l_info->is_decompressor) {
943                         return OPJ_FALSE;
944                 }
945
946                 return  l_info->m_codec_data.m_decompression.opj_set_decode_area(       l_info->m_codec,
947                                                                                                                                                         p_image,
948                                                                                                                                                         p_start_x, p_start_y,
949                                                                                                                                                         p_end_x, p_end_y,
950                                                                                                                                                         l_info->m_event_mgr);
951
952         }
953         return OPJ_FALSE;
954 }
955
956 /**
957  * Reads a tile header. This function is compulsory and allows one to know the size of the tile thta will be decoded.
958  * The user may need to refer to the image got by opj_read_header to understand the size being taken by the tile.
959  *
960  * @param       p_codec                 the jpeg2000 codec.
961  * @param       p_tile_index    pointer to a value that will hold the index of the tile being decoded, in case of success.
962  * @param       p_data_size             pointer to a value that will hold the maximum size of the decoded data, in case of success. In case
963  *                                                      of truncated codestreams, the actual number of bytes decoded may be lower. The computation of the size is the same
964  *                                                      as depicted in opj_write_tile.
965  * @param       p_tile_x0               pointer to a value that will hold the x0 pos of the tile (in the image).
966  * @param       p_tile_y0               pointer to a value that will hold the y0 pos of the tile (in the image).
967  * @param       p_tile_x1               pointer to a value that will hold the x1 pos of the tile (in the image).
968  * @param       p_tile_y1               pointer to a value that will hold the y1 pos of the tile (in the image).
969  * @param       p_nb_comps              pointer to a value that will hold the number of components in the tile.
970  * @param       p_should_go_on  pointer to a boolean that will hold the fact that the decoding should go on. In case the
971  *                                                      codestream is over at the time of the call, the value will be set to false. The user should then stop
972  *                                                      the decoding.
973  * @param       p_stream                the stream to decode.
974  * @return      true                    if the tile header could be decoded. In case the decoding should end, the returned value is still true.
975  *                                                      returning false may be the result of a shortage of memory or an internal error.
976  */
977 opj_bool OPJ_CALLCONV opj_read_tile_header(
978                                         opj_codec_t *p_codec,
979                                         opj_stream_t * p_stream,
980                                         OPJ_UINT32 * p_tile_index,
981                                         OPJ_UINT32 * p_data_size,
982                                         OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
983                                         OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
984                                         OPJ_UINT32 * p_nb_comps,
985                                         opj_bool * p_should_go_on)
986 {
987         if (p_codec && p_stream && p_data_size && p_tile_index) {
988                 opj_codec_private_t * l_info = (opj_codec_private_t *) p_codec;
989                 opj_stream_private_t * l_cio = (opj_stream_private_t *) p_stream;
990
991                 if (! l_info->is_decompressor) {
992                         return OPJ_FALSE;
993                 }
994
995                 return l_info->m_codec_data.m_decompression.opj_read_tile_header(
996                         l_info->m_codec,
997                         p_tile_index,
998                         p_data_size,
999                         p_tile_x0, p_tile_y0,
1000                         p_tile_x1, p_tile_y1,
1001                         p_nb_comps,
1002                         p_should_go_on,
1003                         l_cio,
1004                         l_info->m_event_mgr);
1005         }
1006         return OPJ_FALSE;
1007 }
1008
1009 /**
1010  * Reads a tile data. This function is compulsory and allows one to decode tile data. opj_read_tile_header should be called before.
1011  * The user may need to refer to the image got by opj_read_header to understand the size being taken by the tile.
1012  *
1013  * @param       p_codec                 the jpeg2000 codec.
1014  * @param       p_tile_index    the index of the tile being decoded, this should be the value set by opj_read_tile_header.
1015  * @param       p_data                  pointer to a memory block that will hold the decoded data.
1016  * @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.
1017  * @param       p_stream                the stream to decode.
1018  *
1019  * @return      true                    if the data could be decoded.
1020  */
1021 opj_bool OPJ_CALLCONV opj_decode_tile_data(
1022                                         opj_codec_t *p_codec,
1023                                         OPJ_UINT32 p_tile_index,
1024                                         OPJ_BYTE * p_data,
1025                                         OPJ_UINT32 p_data_size,
1026                                         opj_stream_t *p_stream
1027                                         )
1028 {
1029         if (p_codec && p_data && p_stream) {
1030                 opj_codec_private_t * l_info = (opj_codec_private_t *) p_codec;
1031                 opj_stream_private_t * l_cio = (opj_stream_private_t *) p_stream;
1032
1033                 if (! l_info->is_decompressor) {
1034                         return OPJ_FALSE;
1035                 }
1036
1037                 return l_info->m_codec_data.m_decompression.opj_decode_tile_data(       l_info->m_codec,
1038                                                                                                                                                         p_tile_index,
1039                                                                                                                                                         p_data,
1040                                                                                                                                                         p_data_size,
1041                                                                                                                                                         l_cio,
1042                                                                                                                                                         l_info->m_event_mgr);
1043         }
1044         return OPJ_FALSE;
1045 }
1046
1047 /*
1048  *
1049  *
1050  */
1051 void OPJ_CALLCONV opj_dump_codec(       opj_codec_t *p_codec,
1052                                                                         OPJ_INT32 info_flag,
1053                                                                         FILE* output_stream)
1054 {
1055         if (p_codec) {
1056                 opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
1057
1058                 l_codec->opj_dump_codec(l_codec->m_codec, info_flag, output_stream);
1059                 return;
1060         }
1061
1062         fprintf(stderr, "[ERROR] Input parameter of the dump_codec function are incorrect.\n");
1063         return;
1064 }
1065
1066 /*
1067  *
1068  *
1069  */
1070 opj_codestream_info_v2_t* OPJ_CALLCONV opj_get_cstr_info(opj_codec_t *p_codec)
1071 {
1072         if (p_codec) {
1073                 opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
1074
1075                 return l_codec->opj_get_codec_info(l_codec->m_codec);
1076         }
1077
1078         return NULL;
1079 }
1080
1081 /*
1082  *
1083  *
1084  */
1085 void OPJ_CALLCONV opj_destroy_cstr_info_v2(opj_codestream_info_v2_t **cstr_info) {
1086         if (cstr_info) {
1087
1088                 if ((*cstr_info)->m_default_tile_info.tccp_info){
1089                         opj_free((*cstr_info)->m_default_tile_info.tccp_info);
1090                 }
1091
1092                 if ((*cstr_info)->tile_info){
1093                         /* FIXME not used for the moment*/
1094                 }
1095
1096                 opj_free((*cstr_info));
1097                 (*cstr_info) = NULL;
1098         }
1099 }
1100
1101 /*
1102  *
1103  *
1104  */
1105 opj_codestream_index_t * OPJ_CALLCONV opj_get_cstr_index(opj_codec_t *p_codec)
1106 {
1107         if (p_codec) {
1108                 opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
1109
1110                 return l_codec->opj_get_codec_index(l_codec->m_codec);
1111         }
1112
1113         return NULL;
1114 }
1115
1116 /*
1117  *
1118  *
1119  */
1120 void OPJ_CALLCONV opj_destroy_cstr_index(opj_codestream_index_t **p_cstr_index)
1121 {
1122         if (*p_cstr_index){
1123
1124                 j2k_destroy_cstr_index(*p_cstr_index);
1125                 (*p_cstr_index) = NULL;
1126         }
1127 }
1128
1129
1130 opj_bool OPJ_CALLCONV opj_decode_v2(opj_codec_t *p_info,
1131                                                                         opj_stream_t *cio,
1132                                                                         opj_image_t* p_image)
1133 {
1134         if (p_info && cio) {
1135                 opj_codec_private_t * l_info = (opj_codec_private_t *) p_info;
1136                 opj_stream_private_t * l_cio = (opj_stream_private_t *) cio;
1137
1138                 if (! l_info->is_decompressor) {
1139                         return OPJ_FALSE;
1140                 }
1141
1142                 return l_info->m_codec_data.m_decompression.opj_decode( l_info->m_codec,
1143                                                                                                                                 l_cio,
1144                                                                                                                                 p_image,
1145                                                                                                                                 l_info->m_event_mgr);
1146         }
1147
1148         return OPJ_FALSE;
1149 }
1150
1151 /*
1152  *
1153  *
1154  */
1155 opj_bool OPJ_CALLCONV opj_end_decompress (opj_codec_t *p_codec,opj_stream_t *p_cio)
1156 {
1157         if (p_codec && p_cio) {
1158                 opj_codec_private_t * l_info = (opj_codec_private_t *) p_codec;
1159                 opj_stream_private_t * l_cio = (opj_stream_private_t *) p_cio;
1160
1161                 if (! l_info->is_decompressor) {
1162                         return OPJ_FALSE;
1163                 }
1164                 return l_info->m_codec_data.m_decompression.opj_end_decompress( l_info->m_codec,
1165                                                                                                                                                 l_cio,
1166                                                                                                                                                 l_info->m_event_mgr);
1167         }
1168
1169         return OPJ_FALSE;
1170 }
1171
1172 /*
1173  *
1174  *
1175  */
1176 opj_bool OPJ_CALLCONV opj_get_decoded_tile(     opj_codec_t *p_codec,
1177                                                                                         opj_stream_t *p_cio,
1178                                                                                         opj_image_t *p_image,
1179                                                                                         OPJ_UINT32 tile_index)
1180 {
1181         if (p_codec && p_cio) {
1182                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
1183                 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_cio;
1184
1185         if (! l_codec->is_decompressor) {
1186                 return OPJ_FALSE;
1187                 }
1188                 return l_codec->m_codec_data.m_decompression.opj_get_decoded_tile(      l_codec->m_codec,
1189                                                                                                                                                         l_stream,
1190                                                                                                                                                         p_image,
1191                                                                                                                                                         l_codec->m_event_mgr,
1192                                                                                                                                                         tile_index);
1193         }
1194
1195         return OPJ_FALSE;
1196 }
1197
1198 /*
1199  *
1200  *
1201  */
1202 opj_bool OPJ_CALLCONV opj_set_decoded_resolution_factor(opj_codec_t *p_codec, OPJ_UINT32 res_factor)
1203 {
1204         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
1205
1206         if ( !l_codec ){
1207                 fprintf(stderr, "[ERROR] Input parameters of the setup_decoder function are incorrect.\n");
1208                 return OPJ_FALSE;
1209         }
1210
1211
1212         l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor(l_codec->m_codec, res_factor, l_codec->m_event_mgr);
1213
1214         return OPJ_TRUE;
1215 }
1216
1217
1218 opj_bool OPJ_CALLCONV opj_set_MCT(opj_cparameters_t *parameters,OPJ_FLOAT32 * pEncodingMatrix,OPJ_INT32 * p_dc_shift,OPJ_UINT32 pNbComp)
1219 {
1220         OPJ_UINT32 l_matrix_size = pNbComp * pNbComp * sizeof(OPJ_FLOAT32);
1221         OPJ_UINT32 l_dc_shift_size = pNbComp * sizeof(OPJ_INT32);
1222         OPJ_UINT32 l_mct_total_size = l_matrix_size + l_dc_shift_size;
1223
1224         /* add MCT capability */
1225         int rsiz = (int)parameters->cp_rsiz | (int)MCT;
1226         parameters->cp_rsiz = (OPJ_RSIZ_CAPABILITIES)rsiz;
1227         parameters->irreversible = 1;
1228
1229         /* use array based MCT */
1230         parameters->tcp_mct = 2;
1231         parameters->mct_data = opj_malloc(l_mct_total_size);
1232         if (! parameters->mct_data) {
1233                 return OPJ_FALSE;
1234         }
1235
1236         memcpy(parameters->mct_data,pEncodingMatrix,l_matrix_size);
1237         memcpy(((OPJ_BYTE *) parameters->mct_data) +  l_matrix_size,p_dc_shift,l_dc_shift_size);
1238
1239         return OPJ_TRUE;
1240 }
1241
1242 /**
1243  * Writes a tile with the given data.
1244  *
1245  * @param       p_compressor            the jpeg2000 codec.
1246  * @param       p_tile_index            the index of the tile to write. At the moment, the tiles must be written from 0 to n-1 in sequence.
1247  * @param       p_data                          pointer to the data to write. Data is arranged in sequence, data_comp0, then data_comp1, then ... NO INTERLEAVING should be set.
1248  * @param       p_data_size                     this value os used to make sure the data being written is correct. The size must be equal to the sum for each component of tile_width * tile_height * component_size. component_size can be 1,2 or 4 bytes,
1249  *                                                              depending on the precision of the given component.
1250  * @param       p_stream                        the stream to write data to.
1251  */
1252 opj_bool OPJ_CALLCONV opj_write_tile (  opj_codec_t *p_codec,
1253                                                                                 OPJ_UINT32 p_tile_index,
1254                                                                                 OPJ_BYTE * p_data,
1255                                                                                 OPJ_UINT32 p_data_size,
1256                                                                                 opj_stream_t *p_stream )
1257 {
1258         if (p_codec && p_stream && p_data) {
1259                 opj_codec_private_t * l_info = (opj_codec_private_t *) p_codec;
1260                 opj_stream_private_t * l_cio = (opj_stream_private_t *) p_stream;
1261
1262                 if (l_info->is_decompressor) {
1263                         return OPJ_FALSE;
1264                 }
1265
1266                 return l_info->m_codec_data.m_compression.opj_write_tile(l_info->m_codec,p_tile_index,p_data,p_data_size,l_cio,l_info->m_event_mgr);
1267         }
1268
1269         return OPJ_FALSE;
1270 }
1271
1272