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