Merge to XFA: Move libopenjpeg to third_party/
[pdfium.git] / third_party / libopenjpeg20 / 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(HINSTANCE 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
173         l_codec->is_decompressor = 1;
174
175         switch (p_format) {
176                 case OPJ_CODEC_J2K:
177                         l_codec->opj_dump_codec = (void (*) (void*, OPJ_INT32, FILE*)) j2k_dump;
178
179                         l_codec->opj_get_codec_info = (opj_codestream_info_v2_t* (*) (void*) ) j2k_get_cstr_info;
180
181                         l_codec->opj_get_codec_index = (opj_codestream_index_t* (*) (void*) ) j2k_get_cstr_index;
182
183                         l_codec->m_codec_data.m_decompression.opj_decode =
184                                         (OPJ_BOOL (*) ( void *,
185                                                                         struct opj_stream_private *,
186                                                                         opj_image_t*, struct opj_event_mgr * )) opj_j2k_decode;
187
188                         l_codec->m_codec_data.m_decompression.opj_end_decompress =
189                                         (OPJ_BOOL (*) ( void *,
190                                                                         struct opj_stream_private *,
191                                                                         struct opj_event_mgr *)) opj_j2k_end_decompress;
192
193                         l_codec->m_codec_data.m_decompression.opj_read_header =
194                                         (OPJ_BOOL (*) ( struct opj_stream_private *,
195                                                                         void *,
196                                                                         opj_image_t **,
197                                                                         struct opj_event_mgr * )) opj_j2k_read_header;
198
199                         l_codec->m_codec_data.m_decompression.opj_destroy =
200                                         (void (*) (void *))opj_j2k_destroy;
201
202                         l_codec->m_codec_data.m_decompression.opj_setup_decoder =
203                                         (void (*) (void * , opj_dparameters_t * )) opj_j2k_setup_decoder;
204
205                         l_codec->m_codec_data.m_decompression.opj_read_tile_header =
206                                         (OPJ_BOOL (*) ( void *,
207                                                                         OPJ_UINT32*,
208                                                                         OPJ_UINT32*,
209                                                                         OPJ_INT32*, OPJ_INT32*,
210                                                                         OPJ_INT32*, OPJ_INT32*,
211                                                                         OPJ_UINT32*,
212                                                                         OPJ_BOOL*,
213                                                                         struct opj_stream_private *,
214                                                                         struct opj_event_mgr * )) opj_j2k_read_tile_header;
215
216                         l_codec->m_codec_data.m_decompression.opj_decode_tile_data =
217                                         (OPJ_BOOL (*) ( void *, 
218                                     OPJ_UINT32, 
219                                     OPJ_BYTE*, 
220                                     OPJ_UINT32, 
221                                     struct opj_stream_private *,
222                                     struct opj_event_mgr *)) opj_j2k_decode_tile;
223
224                         l_codec->m_codec_data.m_decompression.opj_set_decode_area =
225                                         (OPJ_BOOL (*) ( void *, 
226                                     opj_image_t*, 
227                                     OPJ_INT32, OPJ_INT32, OPJ_INT32, OPJ_INT32, 
228                                     struct opj_event_mgr *)) opj_j2k_set_decode_area;
229
230                         l_codec->m_codec_data.m_decompression.opj_get_decoded_tile = 
231                     (OPJ_BOOL (*) ( void *p_codec,
232                                                                     opj_stream_private_t *p_cio,
233                                                                     opj_image_t *p_image,
234                                                                     struct opj_event_mgr * p_manager,
235                                                                     OPJ_UINT32 tile_index)) opj_j2k_get_tile;
236
237                         l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor = 
238                     (OPJ_BOOL (*) ( void * p_codec,
239                                                                         OPJ_UINT32 res_factor,
240                                                                         struct opj_event_mgr * p_manager)) opj_j2k_set_decoded_resolution_factor;
241
242                         l_codec->m_codec = opj_j2k_create_decompress();
243
244                         if (! l_codec->m_codec) {
245                                 opj_free(l_codec);
246                                 return NULL;
247                         }
248
249                         break;
250
251                 case OPJ_CODEC_JP2:
252                         /* get a JP2 decoder handle */
253                         l_codec->opj_dump_codec = (void (*) (void*, OPJ_INT32, FILE*)) jp2_dump;
254
255                         l_codec->opj_get_codec_info = (opj_codestream_info_v2_t* (*) (void*) ) jp2_get_cstr_info;
256
257                         l_codec->opj_get_codec_index = (opj_codestream_index_t* (*) (void*) ) jp2_get_cstr_index;
258
259                         l_codec->m_codec_data.m_decompression.opj_decode =
260                                         (OPJ_BOOL (*) ( void *,
261                                                                         struct opj_stream_private *,
262                                                                         opj_image_t*,
263                                                                         struct opj_event_mgr * )) opj_jp2_decode;
264
265                         l_codec->m_codec_data.m_decompression.opj_end_decompress =  
266                     (OPJ_BOOL (*) ( void *,
267                                     struct opj_stream_private *,
268                                     struct opj_event_mgr *)) opj_jp2_end_decompress;
269
270                         l_codec->m_codec_data.m_decompression.opj_read_header =  
271                     (OPJ_BOOL (*) ( struct opj_stream_private *,
272                                                         void *,
273                                                         opj_image_t **,
274                                                         struct opj_event_mgr * )) opj_jp2_read_header;
275
276                         l_codec->m_codec_data.m_decompression.opj_read_tile_header = 
277                     (OPJ_BOOL (*) ( void *,
278                                                         OPJ_UINT32*,
279                                                         OPJ_UINT32*,
280                                                         OPJ_INT32*,
281                                                         OPJ_INT32*,
282                                                         OPJ_INT32 * ,
283                                                         OPJ_INT32 * ,
284                                                         OPJ_UINT32 * ,
285                                                         OPJ_BOOL *,
286                                                         struct opj_stream_private *,
287                                                         struct opj_event_mgr * )) opj_jp2_read_tile_header;
288
289                         l_codec->m_codec_data.m_decompression.opj_decode_tile_data = 
290                     (OPJ_BOOL (*) ( void *,
291                                     OPJ_UINT32,OPJ_BYTE*,OPJ_UINT32,
292                                     struct opj_stream_private *,
293                                     struct opj_event_mgr * )) opj_jp2_decode_tile;
294
295                         l_codec->m_codec_data.m_decompression.opj_destroy = (void (*) (void *))opj_jp2_destroy;
296
297                         l_codec->m_codec_data.m_decompression.opj_setup_decoder = 
298                     (void (*) (void * ,opj_dparameters_t * )) opj_jp2_setup_decoder;
299
300                         l_codec->m_codec_data.m_decompression.opj_set_decode_area = 
301                     (OPJ_BOOL (*) ( void *,
302                                     opj_image_t*, 
303                                     OPJ_INT32,OPJ_INT32,OPJ_INT32,OPJ_INT32,
304                                     struct opj_event_mgr * )) opj_jp2_set_decode_area;
305
306                         l_codec->m_codec_data.m_decompression.opj_get_decoded_tile = 
307                     (OPJ_BOOL (*) ( void *p_codec,
308                                                                         opj_stream_private_t *p_cio,
309                                                                         opj_image_t *p_image,
310                                                                         struct opj_event_mgr * p_manager,
311                                                                         OPJ_UINT32 tile_index)) opj_jp2_get_tile;
312
313                         l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor = 
314                     (OPJ_BOOL (*) ( void * p_codec,
315                                                                 OPJ_UINT32 res_factor,
316                                                                 opj_event_mgr_t * p_manager)) opj_jp2_set_decoded_resolution_factor;
317
318                         l_codec->m_codec = opj_jp2_create(OPJ_TRUE);
319
320                         if (! l_codec->m_codec) {
321                                 opj_free(l_codec);
322                                 return 00;
323                         }
324
325                         break;
326                 case OPJ_CODEC_UNKNOWN:
327                 case OPJ_CODEC_JPT:
328                 default:
329                         opj_free(l_codec);
330                         return 00;
331         }
332
333         opj_set_default_event_handler(&(l_codec->m_event_mgr));
334         return (opj_codec_t*) l_codec;
335 }
336
337 void OPJ_CALLCONV opj_set_default_decoder_parameters(opj_dparameters_t *parameters) {
338         if(parameters) {
339                 memset(parameters, 0, sizeof(opj_dparameters_t));
340                 /* default decoding parameters */
341                 parameters->cp_layer = 0;
342                 parameters->cp_reduce = 0;
343
344                 parameters->decod_format = -1;
345                 parameters->cod_format = -1;
346                 parameters->flags = 0;          
347 /* UniPG>> */
348 #ifdef USE_JPWL
349                 parameters->jpwl_correct = OPJ_FALSE;
350                 parameters->jpwl_exp_comps = JPWL_EXPECTED_COMPONENTS;
351                 parameters->jpwl_max_tiles = JPWL_MAXIMUM_TILES;
352 #endif /* USE_JPWL */
353 /* <<UniPG */
354         }
355 }
356
357 OPJ_BOOL OPJ_CALLCONV opj_setup_decoder(opj_codec_t *p_codec,
358                                         opj_dparameters_t *parameters 
359                                                                                 )
360 {
361         if (p_codec && parameters) { 
362                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
363
364                 if (! l_codec->is_decompressor) {
365                         opj_event_msg(&(l_codec->m_event_mgr), EVT_ERROR, 
366                 "Codec provided to the opj_setup_decoder function is not a decompressor handler.\n");
367                         return OPJ_FALSE;
368                 }
369
370                 l_codec->m_codec_data.m_decompression.opj_setup_decoder(l_codec->m_codec,
371                                                                                                                                 parameters);
372                 return OPJ_TRUE;
373         }
374         return OPJ_FALSE;
375 }
376
377 OPJ_BOOL OPJ_CALLCONV opj_read_header ( opj_stream_t *p_stream,
378                                                                                 opj_codec_t *p_codec,
379                                                                                 opj_image_t **p_image )
380 {
381         if (p_codec && p_stream) {
382                 opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
383                 opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
384
385                 if(! l_codec->is_decompressor) {
386                         opj_event_msg(&(l_codec->m_event_mgr), EVT_ERROR, 
387                 "Codec provided to the opj_read_header function is not a decompressor handler.\n");
388                         return OPJ_FALSE;
389                 }
390
391                 return l_codec->m_codec_data.m_decompression.opj_read_header(   l_stream,
392                                                                                                                                                 l_codec->m_codec,
393                                                                                                                                                 p_image,
394                                                                                                                                                 &(l_codec->m_event_mgr) );
395         }
396
397         return OPJ_FALSE;
398 }
399
400 OPJ_BOOL OPJ_CALLCONV opj_decode(   opj_codec_t *p_codec,
401                                     opj_stream_t *p_stream,
402                                     opj_image_t* p_image)
403 {
404         if (p_codec && p_stream) {
405                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
406                 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
407
408                 if (! l_codec->is_decompressor) {
409                         return OPJ_FALSE;
410                 }
411
412                 return l_codec->m_codec_data.m_decompression.opj_decode(l_codec->m_codec,
413                                                                                                                                 l_stream,
414                                                                                                                                 p_image,
415                                                                                                                                 &(l_codec->m_event_mgr) );
416         }
417
418         return OPJ_FALSE;
419 }
420
421 OPJ_BOOL OPJ_CALLCONV opj_set_decode_area(      opj_codec_t *p_codec,
422                                                                                         opj_image_t* p_image,
423                                                                                         OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
424                                                                                         OPJ_INT32 p_end_x, OPJ_INT32 p_end_y
425                                                                                         )
426 {
427         if (p_codec) {
428                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
429                 
430                 if (! l_codec->is_decompressor) {
431                         return OPJ_FALSE;
432                 }
433
434                 return  l_codec->m_codec_data.m_decompression.opj_set_decode_area(      l_codec->m_codec,
435                                                                                                                                                         p_image,
436                                                                                                                                                         p_start_x, p_start_y,
437                                                                                                                                                         p_end_x, p_end_y,
438                                                                                                                                                         &(l_codec->m_event_mgr) );
439         }
440         return OPJ_FALSE;
441 }
442
443 OPJ_BOOL OPJ_CALLCONV opj_read_tile_header(     opj_codec_t *p_codec,
444                                                                                         opj_stream_t * p_stream,
445                                                                                         OPJ_UINT32 * p_tile_index,
446                                                                                         OPJ_UINT32 * p_data_size,
447                                                                                         OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
448                                                                                         OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
449                                                                                         OPJ_UINT32 * p_nb_comps,
450                                                                                         OPJ_BOOL * p_should_go_on)
451 {
452         if (p_codec && p_stream && p_data_size && p_tile_index) {
453                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
454                 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
455
456                 if (! l_codec->is_decompressor) {
457                         return OPJ_FALSE;
458                 }
459
460                 return l_codec->m_codec_data.m_decompression.opj_read_tile_header(      l_codec->m_codec,
461                                                                                                                                                         p_tile_index,
462                                                                                                                                                         p_data_size,
463                                                                                                                                                         p_tile_x0, p_tile_y0,
464                                                                                                                                                         p_tile_x1, p_tile_y1,
465                                                                                                                                                         p_nb_comps,
466                                                                                                                                                         p_should_go_on,
467                                                                                                                                                         l_stream,
468                                                                                                                                                         &(l_codec->m_event_mgr));
469         }
470         return OPJ_FALSE;
471 }
472
473 OPJ_BOOL OPJ_CALLCONV opj_decode_tile_data(     opj_codec_t *p_codec,
474                                                                                         OPJ_UINT32 p_tile_index,
475                                                                                         OPJ_BYTE * p_data,
476                                                                                         OPJ_UINT32 p_data_size,
477                                                                                         opj_stream_t *p_stream
478                                                                                         )
479 {
480         if (p_codec && p_data && p_stream) {
481                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
482                 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
483
484                 if (! l_codec->is_decompressor) {
485                         return OPJ_FALSE;
486                 }
487
488                 return l_codec->m_codec_data.m_decompression.opj_decode_tile_data(      l_codec->m_codec,
489                                                                                                                                                         p_tile_index,
490                                                                                                                                                         p_data,
491                                                                                                                                                         p_data_size,
492                                                                                                                                                         l_stream,
493                                                                                                                                                         &(l_codec->m_event_mgr) );
494         }
495         return OPJ_FALSE;
496 }
497
498 OPJ_BOOL OPJ_CALLCONV opj_get_decoded_tile(     opj_codec_t *p_codec,
499                                                                                         opj_stream_t *p_stream,
500                                                                                         opj_image_t *p_image,
501                                                                                         OPJ_UINT32 tile_index)
502 {
503         if (p_codec && p_stream) {
504                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
505                 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
506
507                 if (! l_codec->is_decompressor) {
508                         return OPJ_FALSE;
509                 }
510                 
511                 return l_codec->m_codec_data.m_decompression.opj_get_decoded_tile(      l_codec->m_codec,
512                                                                                                                                                         l_stream,
513                                                                                                                                                         p_image,
514                                                                                                                                                         &(l_codec->m_event_mgr),
515                                                                                                                                                         tile_index);
516         }
517
518         return OPJ_FALSE;
519 }
520
521 OPJ_BOOL OPJ_CALLCONV opj_set_decoded_resolution_factor(opj_codec_t *p_codec, 
522                                                                                                                 OPJ_UINT32 res_factor )
523 {
524         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
525
526         if ( !l_codec ){
527                 fprintf(stderr, "[ERROR] Input parameters of the setup_decoder function are incorrect.\n");
528                 return OPJ_FALSE;
529         }
530
531         l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor(l_codec->m_codec, 
532                                                                                                                                                         res_factor,
533                                                                                                                                                         &(l_codec->m_event_mgr) );
534         return OPJ_TRUE;
535 }
536
537 /* ---------------------------------------------------------------------- */
538 /* COMPRESSION FUNCTIONS*/
539
540 opj_codec_t* OPJ_CALLCONV opj_create_compress(OPJ_CODEC_FORMAT p_format)
541 {
542         opj_codec_private_t *l_codec = 00;
543
544         l_codec = (opj_codec_private_t*)opj_calloc(1, sizeof(opj_codec_private_t));
545         if (!l_codec) {
546                 return 00;
547         }
548         
549         l_codec->is_decompressor = 0;
550
551         switch(p_format) {
552                 case OPJ_CODEC_J2K:
553                         l_codec->m_codec_data.m_compression.opj_encode = (OPJ_BOOL (*) (void *,
554                                                                                                                                                         struct opj_stream_private *,
555                                                                                                                                                         struct opj_event_mgr * )) opj_j2k_encode;
556
557                         l_codec->m_codec_data.m_compression.opj_end_compress = (OPJ_BOOL (*) (  void *,
558                                                                                                                                                                         struct opj_stream_private *,
559                                                                                                                                                                         struct opj_event_mgr *)) opj_j2k_end_compress;
560
561                         l_codec->m_codec_data.m_compression.opj_start_compress = (OPJ_BOOL (*) (void *,
562                                                                                                                                                                         struct opj_stream_private *,
563                                                                                                                                                                         struct opj_image * ,
564                                                                                                                                                                         struct opj_event_mgr *)) opj_j2k_start_compress;
565
566                         l_codec->m_codec_data.m_compression.opj_write_tile = (OPJ_BOOL (*) (void *,
567                                                                                                                                                                 OPJ_UINT32,
568                                                                                                                                                                 OPJ_BYTE*,
569                                                                                                                                                                 OPJ_UINT32,
570                                                                                                                                                                 struct opj_stream_private *,
571                                                                                                                                                                 struct opj_event_mgr *) ) opj_j2k_write_tile;
572
573                         l_codec->m_codec_data.m_compression.opj_destroy = (void (*) (void *)) opj_j2k_destroy;
574
575                         l_codec->m_codec_data.m_compression.opj_setup_encoder = (OPJ_BOOL (*) ( void *,
576                                                                                                                                                                 opj_cparameters_t *,
577                                                                                                                                                                 struct opj_image *,
578                                                                                                                                                                 struct opj_event_mgr * )) opj_j2k_setup_encoder;
579
580                         l_codec->m_codec = opj_j2k_create_compress();
581                         if (! l_codec->m_codec) {
582                                 opj_free(l_codec);
583                                 return 00;
584                         }
585
586                         break;
587
588                 case OPJ_CODEC_JP2:
589                         /* get a JP2 decoder handle */
590                         l_codec->m_codec_data.m_compression.opj_encode = (OPJ_BOOL (*) (void *,
591                                                                                                                                                         struct opj_stream_private *,
592                                                                                                                                                         struct opj_event_mgr * )) opj_jp2_encode;
593
594                         l_codec->m_codec_data.m_compression.opj_end_compress = (OPJ_BOOL (*) (  void *,
595                                                                                                                                                                         struct opj_stream_private *,
596                                                                                                                                                                         struct opj_event_mgr *)) opj_jp2_end_compress;
597
598                         l_codec->m_codec_data.m_compression.opj_start_compress = (OPJ_BOOL (*) (void *,
599                                                                                                                                                                         struct opj_stream_private *,
600                                                                                                                                                                         struct opj_image * ,
601                                                                                                                                                                         struct opj_event_mgr *))  opj_jp2_start_compress;
602
603                         l_codec->m_codec_data.m_compression.opj_write_tile = (OPJ_BOOL (*) (void *,
604                                                                                                                                                                 OPJ_UINT32,
605                                                                                                                                                                 OPJ_BYTE*,
606                                                                                                                                                                 OPJ_UINT32,
607                                                                                                                                                                 struct opj_stream_private *,
608                                                                                                                                                                 struct opj_event_mgr *)) opj_jp2_write_tile;
609
610                         l_codec->m_codec_data.m_compression.opj_destroy = (void (*) (void *)) opj_jp2_destroy;
611
612                         l_codec->m_codec_data.m_compression.opj_setup_encoder = (OPJ_BOOL (*) ( void *,
613                                                                                                                                                                 opj_cparameters_t *,
614                                                                                                                                                                 struct opj_image *,
615                                                                                                                                                                 struct opj_event_mgr * )) opj_jp2_setup_encoder;
616
617                         l_codec->m_codec = opj_jp2_create(OPJ_FALSE);
618                         if (! l_codec->m_codec) {
619                                 opj_free(l_codec);
620                                 return 00;
621                         }
622
623                         break;
624
625                 case OPJ_CODEC_UNKNOWN:
626                 case OPJ_CODEC_JPT:
627                 default:
628                         opj_free(l_codec);
629                         return 00;
630         }
631
632         opj_set_default_event_handler(&(l_codec->m_event_mgr));
633         return (opj_codec_t*) l_codec;
634 }
635
636 void OPJ_CALLCONV opj_set_default_encoder_parameters(opj_cparameters_t *parameters) {
637         if(parameters) {
638                 memset(parameters, 0, sizeof(opj_cparameters_t));
639                 /* default coding parameters */
640         parameters->cp_cinema = OPJ_OFF; /* DEPRECATED */
641         parameters->rsiz = OPJ_PROFILE_NONE;
642                 parameters->max_comp_size = 0;
643                 parameters->numresolution = 6;
644         parameters->cp_rsiz = OPJ_STD_RSIZ; /* DEPRECATED */
645                 parameters->cblockw_init = 64;
646                 parameters->cblockh_init = 64;
647                 parameters->prog_order = OPJ_LRCP;
648                 parameters->roi_compno = -1;            /* no ROI */
649                 parameters->subsampling_dx = 1;
650                 parameters->subsampling_dy = 1;
651                 parameters->tp_on = 0;
652                 parameters->decod_format = -1;
653                 parameters->cod_format = -1;
654                 parameters->tcp_rates[0] = 0;   
655                 parameters->tcp_numlayers = 0;
656                 parameters->cp_disto_alloc = 0;
657                 parameters->cp_fixed_alloc = 0;
658                 parameters->cp_fixed_quality = 0;
659                 parameters->jpip_on = OPJ_FALSE;
660 /* UniPG>> */
661 #ifdef USE_JPWL
662                 parameters->jpwl_epc_on = OPJ_FALSE;
663                 parameters->jpwl_hprot_MH = -1; /* -1 means unassigned */
664                 {
665                         int i;
666                         for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
667                                 parameters->jpwl_hprot_TPH_tileno[i] = -1; /* unassigned */
668                                 parameters->jpwl_hprot_TPH[i] = 0; /* absent */
669                         }
670                 };
671                 {
672                         int i;
673                         for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
674                                 parameters->jpwl_pprot_tileno[i] = -1; /* unassigned */
675                                 parameters->jpwl_pprot_packno[i] = -1; /* unassigned */
676                                 parameters->jpwl_pprot[i] = 0; /* absent */
677                         }
678                 };
679                 parameters->jpwl_sens_size = 0; /* 0 means no ESD */
680                 parameters->jpwl_sens_addr = 0; /* 0 means auto */
681                 parameters->jpwl_sens_range = 0; /* 0 means packet */
682                 parameters->jpwl_sens_MH = -1; /* -1 means unassigned */
683                 {
684                         int i;
685                         for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
686                                 parameters->jpwl_sens_TPH_tileno[i] = -1; /* unassigned */
687                                 parameters->jpwl_sens_TPH[i] = -1; /* absent */
688                         }
689                 };
690 #endif /* USE_JPWL */
691 /* <<UniPG */
692         }
693 }
694
695 OPJ_BOOL OPJ_CALLCONV opj_setup_encoder(opj_codec_t *p_codec, 
696                                                                                 opj_cparameters_t *parameters, 
697                                                                                 opj_image_t *p_image)
698 {
699         if (p_codec && parameters && p_image) {
700                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
701
702                 if (! l_codec->is_decompressor) {
703                         l_codec->m_codec_data.m_compression.opj_setup_encoder(  l_codec->m_codec,
704                                                                                                                                         parameters,
705                                                                                                                                         p_image,
706                                                                                                                                         &(l_codec->m_event_mgr) );
707                         return OPJ_TRUE;
708                 }
709         }
710
711         return OPJ_FALSE;
712 }
713
714 OPJ_BOOL OPJ_CALLCONV opj_start_compress (      opj_codec_t *p_codec,
715                                                                                         opj_image_t * p_image,
716                                                                                         opj_stream_t *p_stream)
717 {
718         if (p_codec && p_stream) {
719                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
720                 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
721
722                 if (! l_codec->is_decompressor) {
723                         return l_codec->m_codec_data.m_compression.opj_start_compress(  l_codec->m_codec,
724                                                                                                                                                         l_stream,
725                                                                                                                                                         p_image,
726                                                                                                                                                         &(l_codec->m_event_mgr));
727                 }
728         }
729
730         return OPJ_FALSE;
731 }
732
733 OPJ_BOOL OPJ_CALLCONV opj_encode(opj_codec_t *p_info, opj_stream_t *p_stream)
734 {
735         if (p_info && p_stream) {
736                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_info;
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_encode(  l_codec->m_codec,
741                                                                                                                         l_stream,
742                                                                                                                         &(l_codec->m_event_mgr));
743                 }
744         }
745
746         return OPJ_FALSE;
747
748 }
749
750 OPJ_BOOL OPJ_CALLCONV opj_end_compress (opj_codec_t *p_codec,
751                                                                                 opj_stream_t *p_stream)
752 {
753         if (p_codec && p_stream) {
754                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
755                 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
756
757                 if (! l_codec->is_decompressor) {
758                         return l_codec->m_codec_data.m_compression.opj_end_compress(l_codec->m_codec,
759                                                                                                                                                 l_stream,
760                                                                                                                                                 &(l_codec->m_event_mgr));
761                 }
762         }
763         return OPJ_FALSE;
764
765 }
766
767 OPJ_BOOL OPJ_CALLCONV opj_end_decompress (      opj_codec_t *p_codec,
768                                                                                         opj_stream_t *p_stream)
769 {
770         if (p_codec && p_stream) {
771                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
772                 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
773
774                 if (! l_codec->is_decompressor) {
775                         return OPJ_FALSE;
776                 }
777                 
778                 return l_codec->m_codec_data.m_decompression.opj_end_decompress(l_codec->m_codec,
779                                                                                                                                                 l_stream,
780                                                                                                                                                 &(l_codec->m_event_mgr) );
781         }
782
783         return OPJ_FALSE;
784 }
785
786 OPJ_BOOL OPJ_CALLCONV opj_set_MCT(opj_cparameters_t *parameters,
787                                   OPJ_FLOAT32 * pEncodingMatrix,
788                                   OPJ_INT32 * p_dc_shift,OPJ_UINT32 pNbComp)
789 {
790         OPJ_UINT32 l_matrix_size = pNbComp * pNbComp * (OPJ_UINT32)sizeof(OPJ_FLOAT32);
791         OPJ_UINT32 l_dc_shift_size = pNbComp * (OPJ_UINT32)sizeof(OPJ_INT32);
792         OPJ_UINT32 l_mct_total_size = l_matrix_size + l_dc_shift_size;
793
794         /* add MCT capability */
795     if (OPJ_IS_PART2(parameters->rsiz)) {
796         parameters->rsiz |= OPJ_EXTENSION_MCT;
797     } else {
798         parameters->rsiz = ((OPJ_PROFILE_PART2) | (OPJ_EXTENSION_MCT));
799     }
800         parameters->irreversible = 1;
801
802         /* use array based MCT */
803         parameters->tcp_mct = 2;
804         parameters->mct_data = opj_malloc(l_mct_total_size);
805         if (! parameters->mct_data) {
806                 return OPJ_FALSE;
807         }
808
809         memcpy(parameters->mct_data,pEncodingMatrix,l_matrix_size);
810         memcpy(((OPJ_BYTE *) parameters->mct_data) +  l_matrix_size,p_dc_shift,l_dc_shift_size);
811
812         return OPJ_TRUE;
813 }
814
815 OPJ_BOOL OPJ_CALLCONV opj_write_tile (  opj_codec_t *p_codec,
816                                                                                 OPJ_UINT32 p_tile_index,
817                                                                                 OPJ_BYTE * p_data,
818                                                                                 OPJ_UINT32 p_data_size,
819                                                                                 opj_stream_t *p_stream )
820 {
821         if (p_codec && p_stream && p_data) {
822                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
823                 opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
824
825                 if (l_codec->is_decompressor) {
826                         return OPJ_FALSE;
827                 }
828
829                 return l_codec->m_codec_data.m_compression.opj_write_tile(      l_codec->m_codec,
830                                                                                                                                         p_tile_index,
831                                                                                                                                         p_data,
832                                                                                                                                         p_data_size,
833                                                                                                                                         l_stream,
834                                                                                                                                         &(l_codec->m_event_mgr) );
835         }
836
837         return OPJ_FALSE;
838 }
839
840 /* ---------------------------------------------------------------------- */
841
842 void OPJ_CALLCONV opj_destroy_codec(opj_codec_t *p_codec)
843 {
844         if (p_codec) {
845                 opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
846
847                 if (l_codec->is_decompressor) {
848                         l_codec->m_codec_data.m_decompression.opj_destroy(l_codec->m_codec);
849                 }
850                 else {
851                         l_codec->m_codec_data.m_compression.opj_destroy(l_codec->m_codec);
852                 }
853
854                 l_codec->m_codec = 00;
855                 opj_free(l_codec);
856         }
857 }
858
859 /* ---------------------------------------------------------------------- */
860
861 void OPJ_CALLCONV opj_dump_codec(       opj_codec_t *p_codec,
862                                                                         OPJ_INT32 info_flag,
863                                                                         FILE* output_stream)
864 {
865         if (p_codec) {
866                 opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
867
868                 l_codec->opj_dump_codec(l_codec->m_codec, info_flag, output_stream);
869                 return;
870         }
871
872         fprintf(stderr, "[ERROR] Input parameter of the dump_codec function are incorrect.\n");
873         return;
874 }
875
876 opj_codestream_info_v2_t* OPJ_CALLCONV opj_get_cstr_info(opj_codec_t *p_codec)
877 {
878         if (p_codec) {
879                 opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
880
881                 return l_codec->opj_get_codec_info(l_codec->m_codec);
882         }
883
884         return NULL;
885 }
886
887 void OPJ_CALLCONV opj_destroy_cstr_info(opj_codestream_info_v2_t **cstr_info) {
888         if (cstr_info) {
889
890                 if ((*cstr_info)->m_default_tile_info.tccp_info){
891                         opj_free((*cstr_info)->m_default_tile_info.tccp_info);
892                 }
893
894                 if ((*cstr_info)->tile_info){
895                         /* FIXME not used for the moment*/
896                 }
897
898                 opj_free((*cstr_info));
899                 (*cstr_info) = NULL;
900         }
901 }
902
903 opj_codestream_index_t * OPJ_CALLCONV opj_get_cstr_index(opj_codec_t *p_codec)
904 {
905         if (p_codec) {
906                 opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
907
908                 return l_codec->opj_get_codec_index(l_codec->m_codec);
909         }
910
911         return NULL;
912 }
913
914 void OPJ_CALLCONV opj_destroy_cstr_index(opj_codestream_index_t **p_cstr_index)
915 {
916         if (*p_cstr_index){
917                 j2k_destroy_cstr_index(*p_cstr_index);
918                 (*p_cstr_index) = NULL;
919         }
920 }
921
922 opj_stream_t* OPJ_CALLCONV opj_stream_create_default_file_stream (const char *fname, OPJ_BOOL p_is_read_stream)
923 {
924     return opj_stream_create_file_stream(fname, OPJ_J2K_STREAM_CHUNK_SIZE, p_is_read_stream);
925 }
926
927 opj_stream_t* OPJ_CALLCONV opj_stream_create_file_stream (
928         const char *fname, 
929                 OPJ_SIZE_T p_size, 
930         OPJ_BOOL p_is_read_stream)
931 {
932     opj_stream_t* l_stream = 00;
933     FILE *p_file;
934     const char *mode;
935
936     if (! fname) {
937         return NULL;
938     }
939     
940     if(p_is_read_stream) mode = "rb"; else mode = "wb";
941
942     p_file = fopen(fname, mode);
943
944     if (! p_file) {
945             return NULL;
946     }
947
948     l_stream = opj_stream_create(p_size,p_is_read_stream);
949     if (! l_stream) {
950         fclose(p_file);
951         return NULL;
952     }
953
954     opj_stream_set_user_data(l_stream, p_file, (opj_stream_free_user_data_fn) fclose);
955     opj_stream_set_user_data_length(l_stream, opj_get_data_length_from_file(p_file));
956     opj_stream_set_read_function(l_stream, (opj_stream_read_fn) opj_read_from_file);
957     opj_stream_set_write_function(l_stream, (opj_stream_write_fn) opj_write_from_file);
958     opj_stream_set_skip_function(l_stream, (opj_stream_skip_fn) opj_skip_from_file);
959     opj_stream_set_seek_function(l_stream, (opj_stream_seek_fn) opj_seek_from_file);
960
961     return l_stream;
962 }