[trunk]Replaced deprecated opj_stream_set_user_data function from API
[openjpeg.git] / src / lib / openjp2 / openjpeg.c
1 /*
2  * The copyright in this software is being made available under the 2-clauses 
3  * BSD License, included below. This software may be subject to other third 
4  * party and contributor rights, including patent rights, and no such rights
5  * are granted under this license.
6  *
7  * Copyright (c) 2005, Herve Drolon, FreeImage Team
8  * Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR 
9  * Copyright (c) 2012, CS Systemes d'Information, France
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #ifdef _WIN32
35 #include <windows.h>
36 #endif /* _WIN32 */
37
38 #include "opj_includes.h"
39
40
41 /* ---------------------------------------------------------------------- */
42 /* Functions to set the message handlers */
43
44 OPJ_BOOL OPJ_CALLCONV opj_set_info_handler(     opj_codec_t * p_codec, 
45                                                                                         opj_msg_callback p_callback,
46                                                                                         void * p_user_data)
47 {
48         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
49         if(! l_codec){
50                 return OPJ_FALSE;
51         }
52         
53         l_codec->m_event_mgr.info_handler = p_callback;
54         l_codec->m_event_mgr.m_info_data = p_user_data;
55         
56         return OPJ_TRUE;
57 }
58
59 OPJ_BOOL OPJ_CALLCONV opj_set_warning_handler(  opj_codec_t * p_codec, 
60                                                                                                 opj_msg_callback p_callback,
61                                                                                                 void * p_user_data)
62 {
63         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
64         if (! l_codec) {
65                 return OPJ_FALSE;
66         }
67         
68         l_codec->m_event_mgr.warning_handler = p_callback;
69         l_codec->m_event_mgr.m_warning_data = p_user_data;
70         
71         return OPJ_TRUE;
72 }
73
74 OPJ_BOOL OPJ_CALLCONV opj_set_error_handler(opj_codec_t * p_codec, 
75                                                                                         opj_msg_callback p_callback,
76                                                                                         void * p_user_data)
77 {
78         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
79         if (! l_codec) {
80                 return OPJ_FALSE;
81         }
82         
83         l_codec->m_event_mgr.error_handler = p_callback;
84         l_codec->m_event_mgr.m_error_data = p_user_data;
85         
86         return OPJ_TRUE;
87 }
88
89 /* ---------------------------------------------------------------------- */
90
91 static OPJ_SIZE_T opj_read_from_file (void * p_buffer, OPJ_SIZE_T p_nb_bytes, FILE * p_file)
92 {
93         OPJ_SIZE_T l_nb_read = fread(p_buffer,1,p_nb_bytes,p_file);
94         return l_nb_read ? l_nb_read : (OPJ_SIZE_T)-1;
95 }
96
97 static OPJ_UINT64 opj_get_data_length_from_file (FILE * p_file)
98 {
99         OPJ_OFF_T file_length = 0;
100
101         OPJ_FSEEK(p_file, 0, SEEK_END);
102         file_length = (OPJ_OFF_T)OPJ_FTELL(p_file);
103         OPJ_FSEEK(p_file, 0, SEEK_SET);
104
105         return (OPJ_UINT64)file_length;
106 }
107
108 static OPJ_SIZE_T opj_write_from_file (void * p_buffer, OPJ_SIZE_T p_nb_bytes, FILE * p_file)
109 {
110         return fwrite(p_buffer,1,p_nb_bytes,p_file);
111 }
112
113 static OPJ_OFF_T opj_skip_from_file (OPJ_OFF_T p_nb_bytes, FILE * p_user_data)
114 {
115         if (OPJ_FSEEK(p_user_data,p_nb_bytes,SEEK_CUR)) {
116                 return -1;
117         }
118
119         return p_nb_bytes;
120 }
121
122 static OPJ_BOOL opj_seek_from_file (OPJ_OFF_T p_nb_bytes, FILE * p_user_data)
123 {
124         if (OPJ_FSEEK(p_user_data,p_nb_bytes,SEEK_SET)) {
125                 return OPJ_FALSE;
126         }
127
128         return OPJ_TRUE;
129 }
130
131 /* ---------------------------------------------------------------------- */
132 #ifdef _WIN32
133 #ifndef OPJ_STATIC
134 BOOL APIENTRY
135 DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
136
137         OPJ_ARG_NOT_USED(lpReserved);
138         OPJ_ARG_NOT_USED(hModule);
139
140         switch (ul_reason_for_call) {
141                 case DLL_PROCESS_ATTACH :
142                         break;
143                 case DLL_PROCESS_DETACH :
144                         break;
145                 case DLL_THREAD_ATTACH :
146                 case DLL_THREAD_DETACH :
147                         break;
148     }
149
150     return TRUE;
151 }
152 #endif /* OPJ_STATIC */
153 #endif /* _WIN32 */
154
155 /* ---------------------------------------------------------------------- */
156
157 const char* OPJ_CALLCONV opj_version(void) {
158     return OPJ_PACKAGE_VERSION;
159 }
160
161 /* ---------------------------------------------------------------------- */
162 /* DECOMPRESSION FUNCTIONS*/
163
164 opj_codec_t* OPJ_CALLCONV opj_create_decompress(OPJ_CODEC_FORMAT p_format)
165 {
166         opj_codec_private_t *l_codec = 00;
167
168         l_codec = (opj_codec_private_t*) opj_calloc(1, sizeof(opj_codec_private_t));
169         if (!l_codec){
170                 return 00;
171         }
172         memset(l_codec, 0, sizeof(opj_codec_private_t));
173
174         l_codec->is_decompressor = 1;
175
176         switch (p_format) {
177                 case OPJ_CODEC_J2K:
178                         l_codec->opj_dump_codec = (void (*) (void*, OPJ_INT32, FILE*)) j2k_dump;
179
180                         l_codec->opj_get_codec_info = (opj_codestream_info_v2_t* (*) (void*) ) j2k_get_cstr_info;
181
182                         l_codec->opj_get_codec_index = (opj_codestream_index_t* (*) (void*) ) j2k_get_cstr_index;
183
184                         l_codec->m_codec_data.m_decompression.opj_decode =
185                                         (OPJ_BOOL (*) ( void *,
186                                                                         struct opj_stream_private *,
187                                                                         opj_image_t*, struct opj_event_mgr * )) opj_j2k_decode;
188
189                         l_codec->m_codec_data.m_decompression.opj_end_decompress =
190                                         (OPJ_BOOL (*) ( void *,
191                                                                         struct opj_stream_private *,
192                                                                         struct opj_event_mgr *)) opj_j2k_end_decompress;
193
194                         l_codec->m_codec_data.m_decompression.opj_read_header =
195                                         (OPJ_BOOL (*) ( struct opj_stream_private *,
196                                                                         void *,
197                                                                         opj_image_t **,
198                                                                         struct opj_event_mgr * )) opj_j2k_read_header;
199
200                         l_codec->m_codec_data.m_decompression.opj_destroy =
201                                         (void (*) (void *))opj_j2k_destroy;
202
203                         l_codec->m_codec_data.m_decompression.opj_setup_decoder =
204                                         (void (*) (void * , opj_dparameters_t * )) opj_j2k_setup_decoder;
205
206                         l_codec->m_codec_data.m_decompression.opj_read_tile_header =
207                                         (OPJ_BOOL (*) ( void *,
208                                                                         OPJ_UINT32*,
209                                                                         OPJ_UINT32*,
210                                                                         OPJ_INT32*, OPJ_INT32*,
211                                                                         OPJ_INT32*, OPJ_INT32*,
212                                                                         OPJ_UINT32*,
213                                                                         OPJ_BOOL*,
214                                                                         struct opj_stream_private *,
215                                                                         struct opj_event_mgr * )) opj_j2k_read_tile_header;
216
217                         l_codec->m_codec_data.m_decompression.opj_decode_tile_data =
218                                         (OPJ_BOOL (*) ( void *, 
219                                     OPJ_UINT32, 
220                                     OPJ_BYTE*, 
221                                     OPJ_UINT32, 
222                                     struct opj_stream_private *,
223                                     struct opj_event_mgr *)) opj_j2k_decode_tile;
224
225                         l_codec->m_codec_data.m_decompression.opj_set_decode_area =
226                                         (OPJ_BOOL (*) ( void *, 
227                                     opj_image_t*, 
228                                     OPJ_INT32, OPJ_INT32, OPJ_INT32, OPJ_INT32, 
229                                     struct opj_event_mgr *)) opj_j2k_set_decode_area;
230
231                         l_codec->m_codec_data.m_decompression.opj_get_decoded_tile = 
232                     (OPJ_BOOL (*) ( void *p_codec,
233                                                                     opj_stream_private_t *p_cio,
234                                                                     opj_image_t *p_image,
235                                                                     struct opj_event_mgr * p_manager,
236                                                                     OPJ_UINT32 tile_index)) opj_j2k_get_tile;
237
238                         l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor = 
239                     (OPJ_BOOL (*) ( void * p_codec,
240                                                                         OPJ_UINT32 res_factor,
241                                                                         struct opj_event_mgr * p_manager)) opj_j2k_set_decoded_resolution_factor;
242
243                         l_codec->m_codec = opj_j2k_create_decompress();
244
245                         if (! l_codec->m_codec) {
246                                 opj_free(l_codec);
247                                 return NULL;
248                         }
249
250                         break;
251
252                 case OPJ_CODEC_JP2:
253                         /* get a JP2 decoder handle */
254                         l_codec->opj_dump_codec = (void (*) (void*, OPJ_INT32, FILE*)) jp2_dump;
255
256                         l_codec->opj_get_codec_info = (opj_codestream_info_v2_t* (*) (void*) ) jp2_get_cstr_info;
257
258                         l_codec->opj_get_codec_index = (opj_codestream_index_t* (*) (void*) ) jp2_get_cstr_index;
259
260                         l_codec->m_codec_data.m_decompression.opj_decode =
261                                         (OPJ_BOOL (*) ( void *,
262                                                                         struct opj_stream_private *,
263                                                                         opj_image_t*,
264                                                                         struct opj_event_mgr * )) opj_jp2_decode;
265
266                         l_codec->m_codec_data.m_decompression.opj_end_decompress =  
267                     (OPJ_BOOL (*) ( void *,
268                                     struct opj_stream_private *,
269                                     struct opj_event_mgr *)) opj_jp2_end_decompress;
270
271                         l_codec->m_codec_data.m_decompression.opj_read_header =  
272                     (OPJ_BOOL (*) ( struct opj_stream_private *,
273                                                         void *,
274                                                         opj_image_t **,
275                                                         struct opj_event_mgr * )) opj_jp2_read_header;
276
277                         l_codec->m_codec_data.m_decompression.opj_read_tile_header = 
278                     (OPJ_BOOL (*) ( void *,
279                                                         OPJ_UINT32*,
280                                                         OPJ_UINT32*,
281                                                         OPJ_INT32*,
282                                                         OPJ_INT32*,
283                                                         OPJ_INT32 * ,
284                                                         OPJ_INT32 * ,
285                                                         OPJ_UINT32 * ,
286                                                         OPJ_BOOL *,
287                                                         struct opj_stream_private *,
288                                                         struct opj_event_mgr * )) opj_jp2_read_tile_header;
289
290                         l_codec->m_codec_data.m_decompression.opj_decode_tile_data = 
291                     (OPJ_BOOL (*) ( void *,
292                                     OPJ_UINT32,OPJ_BYTE*,OPJ_UINT32,
293                                     struct opj_stream_private *,
294                                     struct opj_event_mgr * )) opj_jp2_decode_tile;
295
296                         l_codec->m_codec_data.m_decompression.opj_destroy = (void (*) (void *))opj_jp2_destroy;
297
298                         l_codec->m_codec_data.m_decompression.opj_setup_decoder = 
299                     (void (*) (void * ,opj_dparameters_t * )) opj_jp2_setup_decoder;
300
301                         l_codec->m_codec_data.m_decompression.opj_set_decode_area = 
302                     (OPJ_BOOL (*) ( void *,
303                                     opj_image_t*, 
304                                     OPJ_INT32,OPJ_INT32,OPJ_INT32,OPJ_INT32,
305                                     struct opj_event_mgr * )) opj_jp2_set_decode_area;
306
307                         l_codec->m_codec_data.m_decompression.opj_get_decoded_tile = 
308                     (OPJ_BOOL (*) ( void *p_codec,
309                                                                         opj_stream_private_t *p_cio,
310                                                                         opj_image_t *p_image,
311                                                                         struct opj_event_mgr * p_manager,
312                                                                         OPJ_UINT32 tile_index)) opj_jp2_get_tile;
313
314                         l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor = 
315                     (OPJ_BOOL (*) ( void * p_codec,
316                                                                 OPJ_UINT32 res_factor,
317                                                                 opj_event_mgr_t * p_manager)) opj_jp2_set_decoded_resolution_factor;
318
319                         l_codec->m_codec = opj_jp2_create(OPJ_TRUE);
320
321                         if (! l_codec->m_codec) {
322                                 opj_free(l_codec);
323                                 return 00;
324                         }
325
326                         break;
327                 case OPJ_CODEC_UNKNOWN:
328                 case OPJ_CODEC_JPT:
329                 default:
330                         opj_free(l_codec);
331                         return 00;
332         }
333
334         opj_set_default_event_handler(&(l_codec->m_event_mgr));
335         return (opj_codec_t*) l_codec;
336 }
337
338 void OPJ_CALLCONV opj_set_default_decoder_parameters(opj_dparameters_t *parameters) {
339         if(parameters) {
340                 memset(parameters, 0, sizeof(opj_dparameters_t));
341                 /* default decoding parameters */
342                 parameters->cp_layer = 0;
343                 parameters->cp_reduce = 0;
344
345                 parameters->decod_format = -1;
346                 parameters->cod_format = -1;
347                 parameters->flags = 0;          
348 /* UniPG>> */
349 #ifdef USE_JPWL
350                 parameters->jpwl_correct = OPJ_FALSE;
351                 parameters->jpwl_exp_comps = JPWL_EXPECTED_COMPONENTS;
352                 parameters->jpwl_max_tiles = JPWL_MAXIMUM_TILES;
353 #endif /* USE_JPWL */
354 /* <<UniPG */
355         }
356 }
357
358 OPJ_BOOL OPJ_CALLCONV opj_setup_decoder(opj_codec_t *p_codec,
359                                         opj_dparameters_t *parameters 
360                                                                                 )
361 {
362         if (p_codec && parameters) { 
363                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
364
365                 if (! l_codec->is_decompressor) {
366                         opj_event_msg(&(l_codec->m_event_mgr), EVT_ERROR, 
367                 "Codec provided to the opj_setup_decoder function is not a decompressor handler.\n");
368                         return OPJ_FALSE;
369                 }
370
371                 l_codec->m_codec_data.m_decompression.opj_setup_decoder(l_codec->m_codec,
372                                                                                                                                 parameters);
373                 return OPJ_TRUE;
374         }
375         return OPJ_FALSE;
376 }
377
378 OPJ_BOOL OPJ_CALLCONV opj_read_header ( opj_stream_t *p_stream,
379                                                                                 opj_codec_t *p_codec,
380                                                                                 opj_image_t **p_image )
381 {
382         if (p_codec && p_stream) {
383                 opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
384                 opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
385
386                 if(! l_codec->is_decompressor) {
387                         opj_event_msg(&(l_codec->m_event_mgr), EVT_ERROR, 
388                 "Codec provided to the opj_read_header function is not a decompressor handler.\n");
389                         return OPJ_FALSE;
390                 }
391
392                 return l_codec->m_codec_data.m_decompression.opj_read_header(   l_stream,
393                                                                                                                                                 l_codec->m_codec,
394                                                                                                                                                 p_image,
395                                                                                                                                                 &(l_codec->m_event_mgr) );
396         }
397
398         return OPJ_FALSE;
399 }
400
401 OPJ_BOOL OPJ_CALLCONV opj_decode(   opj_codec_t *p_codec,
402                                     opj_stream_t *p_stream,
403                                     opj_image_t* p_image)
404 {
405         if (p_codec && p_stream) {
406                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
407                 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
408
409                 if (! l_codec->is_decompressor) {
410                         return OPJ_FALSE;
411                 }
412
413                 return l_codec->m_codec_data.m_decompression.opj_decode(l_codec->m_codec,
414                                                                                                                                 l_stream,
415                                                                                                                                 p_image,
416                                                                                                                                 &(l_codec->m_event_mgr) );
417         }
418
419         return OPJ_FALSE;
420 }
421
422 OPJ_BOOL OPJ_CALLCONV opj_set_decode_area(      opj_codec_t *p_codec,
423                                                                                         opj_image_t* p_image,
424                                                                                         OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
425                                                                                         OPJ_INT32 p_end_x, OPJ_INT32 p_end_y
426                                                                                         )
427 {
428         if (p_codec) {
429                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
430                 
431                 if (! l_codec->is_decompressor) {
432                         return OPJ_FALSE;
433                 }
434
435                 return  l_codec->m_codec_data.m_decompression.opj_set_decode_area(      l_codec->m_codec,
436                                                                                                                                                         p_image,
437                                                                                                                                                         p_start_x, p_start_y,
438                                                                                                                                                         p_end_x, p_end_y,
439                                                                                                                                                         &(l_codec->m_event_mgr) );
440         }
441         return OPJ_FALSE;
442 }
443
444 OPJ_BOOL OPJ_CALLCONV opj_read_tile_header(     opj_codec_t *p_codec,
445                                                                                         opj_stream_t * p_stream,
446                                                                                         OPJ_UINT32 * p_tile_index,
447                                                                                         OPJ_UINT32 * p_data_size,
448                                                                                         OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
449                                                                                         OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
450                                                                                         OPJ_UINT32 * p_nb_comps,
451                                                                                         OPJ_BOOL * p_should_go_on)
452 {
453         if (p_codec && p_stream && p_data_size && p_tile_index) {
454                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
455                 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
456
457                 if (! l_codec->is_decompressor) {
458                         return OPJ_FALSE;
459                 }
460
461                 return l_codec->m_codec_data.m_decompression.opj_read_tile_header(      l_codec->m_codec,
462                                                                                                                                                         p_tile_index,
463                                                                                                                                                         p_data_size,
464                                                                                                                                                         p_tile_x0, p_tile_y0,
465                                                                                                                                                         p_tile_x1, p_tile_y1,
466                                                                                                                                                         p_nb_comps,
467                                                                                                                                                         p_should_go_on,
468                                                                                                                                                         l_stream,
469                                                                                                                                                         &(l_codec->m_event_mgr));
470         }
471         return OPJ_FALSE;
472 }
473
474 OPJ_BOOL OPJ_CALLCONV opj_decode_tile_data(     opj_codec_t *p_codec,
475                                                                                         OPJ_UINT32 p_tile_index,
476                                                                                         OPJ_BYTE * p_data,
477                                                                                         OPJ_UINT32 p_data_size,
478                                                                                         opj_stream_t *p_stream
479                                                                                         )
480 {
481         if (p_codec && p_data && p_stream) {
482                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
483                 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
484
485                 if (! l_codec->is_decompressor) {
486                         return OPJ_FALSE;
487                 }
488
489                 return l_codec->m_codec_data.m_decompression.opj_decode_tile_data(      l_codec->m_codec,
490                                                                                                                                                         p_tile_index,
491                                                                                                                                                         p_data,
492                                                                                                                                                         p_data_size,
493                                                                                                                                                         l_stream,
494                                                                                                                                                         &(l_codec->m_event_mgr) );
495         }
496         return OPJ_FALSE;
497 }
498
499 OPJ_BOOL OPJ_CALLCONV opj_get_decoded_tile(     opj_codec_t *p_codec,
500                                                                                         opj_stream_t *p_stream,
501                                                                                         opj_image_t *p_image,
502                                                                                         OPJ_UINT32 tile_index)
503 {
504         if (p_codec && p_stream) {
505                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
506                 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
507
508                 if (! l_codec->is_decompressor) {
509                         return OPJ_FALSE;
510                 }
511                 
512                 return l_codec->m_codec_data.m_decompression.opj_get_decoded_tile(      l_codec->m_codec,
513                                                                                                                                                         l_stream,
514                                                                                                                                                         p_image,
515                                                                                                                                                         &(l_codec->m_event_mgr),
516                                                                                                                                                         tile_index);
517         }
518
519         return OPJ_FALSE;
520 }
521
522 OPJ_BOOL OPJ_CALLCONV opj_set_decoded_resolution_factor(opj_codec_t *p_codec, 
523                                                                                                                 OPJ_UINT32 res_factor )
524 {
525         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
526
527         if ( !l_codec ){
528                 fprintf(stderr, "[ERROR] Input parameters of the setup_decoder function are incorrect.\n");
529                 return OPJ_FALSE;
530         }
531
532         l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor(l_codec->m_codec, 
533                                                                                                                                                         res_factor,
534                                                                                                                                                         &(l_codec->m_event_mgr) );
535         return OPJ_TRUE;
536 }
537
538 /* ---------------------------------------------------------------------- */
539 /* COMPRESSION FUNCTIONS*/
540
541 opj_codec_t* OPJ_CALLCONV opj_create_compress(OPJ_CODEC_FORMAT p_format)
542 {
543         opj_codec_private_t *l_codec = 00;
544
545         l_codec = (opj_codec_private_t*)opj_calloc(1, sizeof(opj_codec_private_t));
546         if (!l_codec) {
547                 return 00;
548         }
549         memset(l_codec, 0, sizeof(opj_codec_private_t));
550         
551         l_codec->is_decompressor = 0;
552
553         switch(p_format) {
554                 case OPJ_CODEC_J2K:
555                         l_codec->m_codec_data.m_compression.opj_encode = (OPJ_BOOL (*) (void *,
556                                                                                                                                                         struct opj_stream_private *,
557                                                                                                                                                         struct opj_event_mgr * )) opj_j2k_encode;
558
559                         l_codec->m_codec_data.m_compression.opj_end_compress = (OPJ_BOOL (*) (  void *,
560                                                                                                                                                                         struct opj_stream_private *,
561                                                                                                                                                                         struct opj_event_mgr *)) opj_j2k_end_compress;
562
563                         l_codec->m_codec_data.m_compression.opj_start_compress = (OPJ_BOOL (*) (void *,
564                                                                                                                                                                         struct opj_stream_private *,
565                                                                                                                                                                         struct opj_image * ,
566                                                                                                                                                                         struct opj_event_mgr *)) opj_j2k_start_compress;
567
568                         l_codec->m_codec_data.m_compression.opj_write_tile = (OPJ_BOOL (*) (void *,
569                                                                                                                                                                 OPJ_UINT32,
570                                                                                                                                                                 OPJ_BYTE*,
571                                                                                                                                                                 OPJ_UINT32,
572                                                                                                                                                                 struct opj_stream_private *,
573                                                                                                                                                                 struct opj_event_mgr *) ) opj_j2k_write_tile;
574
575                         l_codec->m_codec_data.m_compression.opj_destroy = (void (*) (void *)) opj_j2k_destroy;
576
577                         l_codec->m_codec_data.m_compression.opj_setup_encoder = (void (*) (     void *,
578                                                                                                                                                                 opj_cparameters_t *,
579                                                                                                                                                                 struct opj_image *,
580                                                                                                                                                                 struct opj_event_mgr * )) opj_j2k_setup_encoder;
581
582                         l_codec->m_codec = opj_j2k_create_compress();
583                         if (! l_codec->m_codec) {
584                                 opj_free(l_codec);
585                                 return 00;
586                         }
587
588                         break;
589
590                 case OPJ_CODEC_JP2:
591                         /* get a JP2 decoder handle */
592                         l_codec->m_codec_data.m_compression.opj_encode = (OPJ_BOOL (*) (void *,
593                                                                                                                                                         struct opj_stream_private *,
594                                                                                                                                                         struct opj_event_mgr * )) opj_jp2_encode;
595
596                         l_codec->m_codec_data.m_compression.opj_end_compress = (OPJ_BOOL (*) (  void *,
597                                                                                                                                                                         struct opj_stream_private *,
598                                                                                                                                                                         struct opj_event_mgr *)) opj_jp2_end_compress;
599
600                         l_codec->m_codec_data.m_compression.opj_start_compress = (OPJ_BOOL (*) (void *,
601                                                                                                                                                                         struct opj_stream_private *,
602                                                                                                                                                                         struct opj_image * ,
603                                                                                                                                                                         struct opj_event_mgr *))  opj_jp2_start_compress;
604
605                         l_codec->m_codec_data.m_compression.opj_write_tile = (OPJ_BOOL (*) (void *,
606                                                                                                                                                                 OPJ_UINT32,
607                                                                                                                                                                 OPJ_BYTE*,
608                                                                                                                                                                 OPJ_UINT32,
609                                                                                                                                                                 struct opj_stream_private *,
610                                                                                                                                                                 struct opj_event_mgr *)) opj_jp2_write_tile;
611
612                         l_codec->m_codec_data.m_compression.opj_destroy = (void (*) (void *)) opj_jp2_destroy;
613
614                         l_codec->m_codec_data.m_compression.opj_setup_encoder = (void (*) (     void *,
615                                                                                                                                                                 opj_cparameters_t *,
616                                                                                                                                                                 struct opj_image *,
617                                                                                                                                                                 struct opj_event_mgr * )) opj_jp2_setup_encoder;
618
619                         l_codec->m_codec = opj_jp2_create(OPJ_FALSE);
620                         if (! l_codec->m_codec) {
621                                 opj_free(l_codec);
622                                 return 00;
623                         }
624
625                         break;
626
627                 case OPJ_CODEC_UNKNOWN:
628                 case OPJ_CODEC_JPT:
629                 default:
630                         opj_free(l_codec);
631                         return 00;
632         }
633
634         opj_set_default_event_handler(&(l_codec->m_event_mgr));
635         return (opj_codec_t*) l_codec;
636 }
637
638 void OPJ_CALLCONV opj_set_default_encoder_parameters(opj_cparameters_t *parameters) {
639         if(parameters) {
640                 memset(parameters, 0, sizeof(opj_cparameters_t));
641                 /* default coding parameters */
642         parameters->cp_cinema = OPJ_OFF; /* DEPRECATED */
643         parameters->rsiz = OPJ_PROFILE_NONE;
644                 parameters->max_comp_size = 0;
645                 parameters->numresolution = 6;
646         parameters->cp_rsiz = OPJ_STD_RSIZ; /* DEPRECATED */
647                 parameters->cblockw_init = 64;
648                 parameters->cblockh_init = 64;
649                 parameters->prog_order = OPJ_LRCP;
650                 parameters->roi_compno = -1;            /* no ROI */
651                 parameters->subsampling_dx = 1;
652                 parameters->subsampling_dy = 1;
653                 parameters->tp_on = 0;
654                 parameters->decod_format = -1;
655                 parameters->cod_format = -1;
656                 parameters->tcp_rates[0] = 0;   
657                 parameters->tcp_numlayers = 0;
658                 parameters->cp_disto_alloc = 0;
659                 parameters->cp_fixed_alloc = 0;
660                 parameters->cp_fixed_quality = 0;
661                 parameters->jpip_on = OPJ_FALSE;
662 /* UniPG>> */
663 #ifdef USE_JPWL
664                 parameters->jpwl_epc_on = OPJ_FALSE;
665                 parameters->jpwl_hprot_MH = -1; /* -1 means unassigned */
666                 {
667                         int i;
668                         for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
669                                 parameters->jpwl_hprot_TPH_tileno[i] = -1; /* unassigned */
670                                 parameters->jpwl_hprot_TPH[i] = 0; /* absent */
671                         }
672                 };
673                 {
674                         int i;
675                         for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
676                                 parameters->jpwl_pprot_tileno[i] = -1; /* unassigned */
677                                 parameters->jpwl_pprot_packno[i] = -1; /* unassigned */
678                                 parameters->jpwl_pprot[i] = 0; /* absent */
679                         }
680                 };
681                 parameters->jpwl_sens_size = 0; /* 0 means no ESD */
682                 parameters->jpwl_sens_addr = 0; /* 0 means auto */
683                 parameters->jpwl_sens_range = 0; /* 0 means packet */
684                 parameters->jpwl_sens_MH = -1; /* -1 means unassigned */
685                 {
686                         int i;
687                         for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
688                                 parameters->jpwl_sens_TPH_tileno[i] = -1; /* unassigned */
689                                 parameters->jpwl_sens_TPH[i] = -1; /* absent */
690                         }
691                 };
692 #endif /* USE_JPWL */
693 /* <<UniPG */
694         }
695 }
696
697 OPJ_BOOL OPJ_CALLCONV opj_setup_encoder(opj_codec_t *p_codec, 
698                                                                                 opj_cparameters_t *parameters, 
699                                                                                 opj_image_t *p_image)
700 {
701         if (p_codec && parameters && p_image) {
702                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
703
704                 if (! l_codec->is_decompressor) {
705                         l_codec->m_codec_data.m_compression.opj_setup_encoder(  l_codec->m_codec,
706                                                                                                                                         parameters,
707                                                                                                                                         p_image,
708                                                                                                                                         &(l_codec->m_event_mgr) );
709                         return OPJ_TRUE;
710                 }
711         }
712
713         return OPJ_FALSE;
714 }
715
716 OPJ_BOOL OPJ_CALLCONV opj_start_compress (      opj_codec_t *p_codec,
717                                                                                         opj_image_t * p_image,
718                                                                                         opj_stream_t *p_stream)
719 {
720         if (p_codec && p_stream) {
721                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
722                 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
723
724                 if (! l_codec->is_decompressor) {
725                         return l_codec->m_codec_data.m_compression.opj_start_compress(  l_codec->m_codec,
726                                                                                                                                                         l_stream,
727                                                                                                                                                         p_image,
728                                                                                                                                                         &(l_codec->m_event_mgr));
729                 }
730         }
731
732         return OPJ_FALSE;
733 }
734
735 OPJ_BOOL OPJ_CALLCONV opj_encode(opj_codec_t *p_info, opj_stream_t *p_stream)
736 {
737         if (p_info && p_stream) {
738                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_info;
739                 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
740
741                 if (! l_codec->is_decompressor) {
742                         return l_codec->m_codec_data.m_compression.opj_encode(  l_codec->m_codec,
743                                                                                                                         l_stream,
744                                                                                                                         &(l_codec->m_event_mgr));
745                 }
746         }
747
748         return OPJ_FALSE;
749
750 }
751
752 OPJ_BOOL OPJ_CALLCONV opj_end_compress (opj_codec_t *p_codec,
753                                                                                 opj_stream_t *p_stream)
754 {
755         if (p_codec && p_stream) {
756                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
757                 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
758
759                 if (! l_codec->is_decompressor) {
760                         return l_codec->m_codec_data.m_compression.opj_end_compress(l_codec->m_codec,
761                                                                                                                                                 l_stream,
762                                                                                                                                                 &(l_codec->m_event_mgr));
763                 }
764         }
765         return OPJ_FALSE;
766
767 }
768
769 OPJ_BOOL OPJ_CALLCONV opj_end_decompress (      opj_codec_t *p_codec,
770                                                                                         opj_stream_t *p_stream)
771 {
772         if (p_codec && p_stream) {
773                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
774                 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
775
776                 if (! l_codec->is_decompressor) {
777                         return OPJ_FALSE;
778                 }
779                 
780                 return l_codec->m_codec_data.m_decompression.opj_end_decompress(l_codec->m_codec,
781                                                                                                                                                 l_stream,
782                                                                                                                                                 &(l_codec->m_event_mgr) );
783         }
784
785         return OPJ_FALSE;
786 }
787
788 OPJ_BOOL OPJ_CALLCONV opj_set_MCT(opj_cparameters_t *parameters,
789                                   OPJ_FLOAT32 * pEncodingMatrix,
790                                   OPJ_INT32 * p_dc_shift,OPJ_UINT32 pNbComp)
791 {
792         OPJ_UINT32 l_matrix_size = pNbComp * pNbComp * (OPJ_UINT32)sizeof(OPJ_FLOAT32);
793         OPJ_UINT32 l_dc_shift_size = pNbComp * (OPJ_UINT32)sizeof(OPJ_INT32);
794         OPJ_UINT32 l_mct_total_size = l_matrix_size + l_dc_shift_size;
795
796         /* add MCT capability */
797     if (OPJ_IS_PART2(parameters->rsiz)) {
798         parameters->rsiz |= OPJ_EXTENSION_MCT;
799     } else {
800         parameters->rsiz = ((OPJ_PROFILE_PART2) | (OPJ_EXTENSION_MCT));
801     }
802         parameters->irreversible = 1;
803
804         /* use array based MCT */
805         parameters->tcp_mct = 2;
806         parameters->mct_data = opj_malloc(l_mct_total_size);
807         if (! parameters->mct_data) {
808                 return OPJ_FALSE;
809         }
810
811         memcpy(parameters->mct_data,pEncodingMatrix,l_matrix_size);
812         memcpy(((OPJ_BYTE *) parameters->mct_data) +  l_matrix_size,p_dc_shift,l_dc_shift_size);
813
814         return OPJ_TRUE;
815 }
816
817 OPJ_BOOL OPJ_CALLCONV opj_write_tile (  opj_codec_t *p_codec,
818                                                                                 OPJ_UINT32 p_tile_index,
819                                                                                 OPJ_BYTE * p_data,
820                                                                                 OPJ_UINT32 p_data_size,
821                                                                                 opj_stream_t *p_stream )
822 {
823         if (p_codec && p_stream && p_data) {
824                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
825                 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
826
827                 if (l_codec->is_decompressor) {
828                         return OPJ_FALSE;
829                 }
830
831                 return l_codec->m_codec_data.m_compression.opj_write_tile(      l_codec->m_codec,
832                                                                                                                                         p_tile_index,
833                                                                                                                                         p_data,
834                                                                                                                                         p_data_size,
835                                                                                                                                         l_stream,
836                                                                                                                                         &(l_codec->m_event_mgr) );
837         }
838
839         return OPJ_FALSE;
840 }
841
842 /* ---------------------------------------------------------------------- */
843
844 void OPJ_CALLCONV opj_destroy_codec(opj_codec_t *p_codec)
845 {
846         if (p_codec) {
847                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
848
849                 if (l_codec->is_decompressor) {
850                         l_codec->m_codec_data.m_decompression.opj_destroy(l_codec->m_codec);
851                 }
852                 else {
853                         l_codec->m_codec_data.m_compression.opj_destroy(l_codec->m_codec);
854                 }
855
856                 l_codec->m_codec = 00;
857                 opj_free(l_codec);
858         }
859 }
860
861 /* ---------------------------------------------------------------------- */
862
863 void OPJ_CALLCONV opj_dump_codec(       opj_codec_t *p_codec,
864                                                                         OPJ_INT32 info_flag,
865                                                                         FILE* output_stream)
866 {
867         if (p_codec) {
868                 opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
869
870                 l_codec->opj_dump_codec(l_codec->m_codec, info_flag, output_stream);
871                 return;
872         }
873
874         fprintf(stderr, "[ERROR] Input parameter of the dump_codec function are incorrect.\n");
875         return;
876 }
877
878 opj_codestream_info_v2_t* OPJ_CALLCONV opj_get_cstr_info(opj_codec_t *p_codec)
879 {
880         if (p_codec) {
881                 opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
882
883                 return l_codec->opj_get_codec_info(l_codec->m_codec);
884         }
885
886         return NULL;
887 }
888
889 void OPJ_CALLCONV opj_destroy_cstr_info(opj_codestream_info_v2_t **cstr_info) {
890         if (cstr_info) {
891
892                 if ((*cstr_info)->m_default_tile_info.tccp_info){
893                         opj_free((*cstr_info)->m_default_tile_info.tccp_info);
894                 }
895
896                 if ((*cstr_info)->tile_info){
897                         /* FIXME not used for the moment*/
898                 }
899
900                 opj_free((*cstr_info));
901                 (*cstr_info) = NULL;
902         }
903 }
904
905 opj_codestream_index_t * OPJ_CALLCONV opj_get_cstr_index(opj_codec_t *p_codec)
906 {
907         if (p_codec) {
908                 opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
909
910                 return l_codec->opj_get_codec_index(l_codec->m_codec);
911         }
912
913         return NULL;
914 }
915
916 void OPJ_CALLCONV opj_destroy_cstr_index(opj_codestream_index_t **p_cstr_index)
917 {
918         if (*p_cstr_index){
919                 j2k_destroy_cstr_index(*p_cstr_index);
920                 (*p_cstr_index) = NULL;
921         }
922 }
923
924 opj_stream_t* OPJ_CALLCONV opj_stream_create_default_file_stream (const char *fname, OPJ_BOOL p_is_read_stream)
925 {
926     return opj_stream_create_file_stream(fname, OPJ_J2K_STREAM_CHUNK_SIZE, p_is_read_stream);
927 }
928
929 opj_stream_t* OPJ_CALLCONV opj_stream_create_file_stream (
930         const char *fname, 
931                 OPJ_SIZE_T p_size, 
932         OPJ_BOOL p_is_read_stream)
933 {
934     opj_stream_t* l_stream = 00;
935     FILE *p_file;
936     const char *mode;
937
938     if (! fname) {
939         return NULL;
940     }
941     
942     if(p_is_read_stream) mode = "rb"; else mode = "wb";
943
944     p_file = fopen(fname, mode);
945
946     if (! p_file) {
947             return NULL;
948     }
949
950     l_stream = opj_stream_create(p_size,p_is_read_stream);
951     if (! l_stream) {
952         fclose(p_file);
953         return NULL;
954     }
955
956     opj_stream_set_user_data(l_stream, p_file, (opj_stream_free_user_data_fn) fclose);
957     opj_stream_set_user_data_length(l_stream, opj_get_data_length_from_file(p_file));
958     opj_stream_set_read_function(l_stream, (opj_stream_read_fn) opj_read_from_file);
959     opj_stream_set_write_function(l_stream, (opj_stream_write_fn) opj_write_from_file);
960     opj_stream_set_skip_function(l_stream, (opj_stream_skip_fn) opj_skip_from_file);
961     opj_stream_set_seek_function(l_stream, (opj_stream_seek_fn) opj_seek_from_file);
962
963     return l_stream;
964 }