Merge pull request #866 from tSed/master
[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,
92                                      FILE * p_file)
93 {
94     OPJ_SIZE_T l_nb_read = fread(p_buffer, 1, p_nb_bytes, p_file);
95     return l_nb_read ? l_nb_read : (OPJ_SIZE_T) - 1;
96 }
97
98 static OPJ_UINT64 opj_get_data_length_from_file(FILE * p_file)
99 {
100     OPJ_OFF_T file_length = 0;
101
102     OPJ_FSEEK(p_file, 0, SEEK_END);
103     file_length = (OPJ_OFF_T)OPJ_FTELL(p_file);
104     OPJ_FSEEK(p_file, 0, SEEK_SET);
105
106     return (OPJ_UINT64)file_length;
107 }
108
109 static OPJ_SIZE_T opj_write_from_file(void * p_buffer, OPJ_SIZE_T p_nb_bytes,
110                                       FILE * p_file)
111 {
112     return fwrite(p_buffer, 1, p_nb_bytes, p_file);
113 }
114
115 static OPJ_OFF_T opj_skip_from_file(OPJ_OFF_T p_nb_bytes, FILE * p_user_data)
116 {
117     if (OPJ_FSEEK(p_user_data, p_nb_bytes, SEEK_CUR)) {
118         return -1;
119     }
120
121     return p_nb_bytes;
122 }
123
124 static OPJ_BOOL opj_seek_from_file(OPJ_OFF_T p_nb_bytes, FILE * p_user_data)
125 {
126     if (OPJ_FSEEK(p_user_data, p_nb_bytes, SEEK_SET)) {
127         return OPJ_FALSE;
128     }
129
130     return OPJ_TRUE;
131 }
132
133 /* ---------------------------------------------------------------------- */
134 #ifdef _WIN32
135 #ifndef OPJ_STATIC
136 BOOL APIENTRY
137 DllMain(HINSTANCE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
138 {
139
140     OPJ_ARG_NOT_USED(lpReserved);
141     OPJ_ARG_NOT_USED(hModule);
142
143     switch (ul_reason_for_call) {
144     case DLL_PROCESS_ATTACH :
145         break;
146     case DLL_PROCESS_DETACH :
147         break;
148     case DLL_THREAD_ATTACH :
149     case DLL_THREAD_DETACH :
150         break;
151     }
152
153     return TRUE;
154 }
155 #endif /* OPJ_STATIC */
156 #endif /* _WIN32 */
157
158 /* ---------------------------------------------------------------------- */
159
160 const char* OPJ_CALLCONV opj_version(void)
161 {
162     return OPJ_PACKAGE_VERSION;
163 }
164
165 /* ---------------------------------------------------------------------- */
166 /* DECOMPRESSION FUNCTIONS*/
167
168 opj_codec_t* OPJ_CALLCONV opj_create_decompress(OPJ_CODEC_FORMAT p_format)
169 {
170     opj_codec_private_t *l_codec = 00;
171
172     l_codec = (opj_codec_private_t*) opj_calloc(1, sizeof(opj_codec_private_t));
173     if (!l_codec) {
174         return 00;
175     }
176
177     l_codec->is_decompressor = 1;
178
179     switch (p_format) {
180     case OPJ_CODEC_J2K:
181         l_codec->opj_dump_codec = (void (*)(void*, OPJ_INT32, FILE*)) j2k_dump;
182
183         l_codec->opj_get_codec_info = (opj_codestream_info_v2_t* (*)(
184                                            void*)) j2k_get_cstr_info;
185
186         l_codec->opj_get_codec_index = (opj_codestream_index_t* (*)(
187                                             void*)) j2k_get_cstr_index;
188
189         l_codec->m_codec_data.m_decompression.opj_decode =
190             (OPJ_BOOL(*)(void *,
191                          struct opj_stream_private *,
192                          opj_image_t*, struct opj_event_mgr *)) opj_j2k_decode;
193
194         l_codec->m_codec_data.m_decompression.opj_end_decompress =
195             (OPJ_BOOL(*)(void *,
196                          struct opj_stream_private *,
197                          struct opj_event_mgr *)) opj_j2k_end_decompress;
198
199         l_codec->m_codec_data.m_decompression.opj_read_header =
200             (OPJ_BOOL(*)(struct opj_stream_private *,
201                          void *,
202                          opj_image_t **,
203                          struct opj_event_mgr *)) opj_j2k_read_header;
204
205         l_codec->m_codec_data.m_decompression.opj_destroy =
206             (void (*)(void *))opj_j2k_destroy;
207
208         l_codec->m_codec_data.m_decompression.opj_setup_decoder =
209             (void (*)(void *, opj_dparameters_t *)) opj_j2k_setup_decoder;
210
211         l_codec->m_codec_data.m_decompression.opj_read_tile_header =
212             (OPJ_BOOL(*)(void *,
213                          OPJ_UINT32*,
214                          OPJ_UINT32*,
215                          OPJ_INT32*, OPJ_INT32*,
216                          OPJ_INT32*, OPJ_INT32*,
217                          OPJ_UINT32*,
218                          OPJ_BOOL*,
219                          struct opj_stream_private *,
220                          struct opj_event_mgr *)) opj_j2k_read_tile_header;
221
222         l_codec->m_codec_data.m_decompression.opj_decode_tile_data =
223             (OPJ_BOOL(*)(void *,
224                          OPJ_UINT32,
225                          OPJ_BYTE*,
226                          OPJ_UINT32,
227                          struct opj_stream_private *,
228                          struct opj_event_mgr *)) opj_j2k_decode_tile;
229
230         l_codec->m_codec_data.m_decompression.opj_set_decode_area =
231             (OPJ_BOOL(*)(void *,
232                          opj_image_t*,
233                          OPJ_INT32, OPJ_INT32, OPJ_INT32, OPJ_INT32,
234                          struct opj_event_mgr *)) opj_j2k_set_decode_area;
235
236         l_codec->m_codec_data.m_decompression.opj_get_decoded_tile =
237             (OPJ_BOOL(*)(void *p_codec,
238                          opj_stream_private_t *p_cio,
239                          opj_image_t *p_image,
240                          struct opj_event_mgr * p_manager,
241                          OPJ_UINT32 tile_index)) opj_j2k_get_tile;
242
243         l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor =
244             (OPJ_BOOL(*)(void * p_codec,
245                          OPJ_UINT32 res_factor,
246                          struct opj_event_mgr * p_manager)) opj_j2k_set_decoded_resolution_factor;
247
248         l_codec->m_codec_data.m_decompression.opj_set_decoded_components =
249             (OPJ_BOOL(*)(void * p_codec,
250                          OPJ_UINT32 numcomps,
251                          const OPJ_UINT32 * comps_indices,
252                          struct opj_event_mgr * p_manager)) opj_j2k_set_decoded_components;
253
254         l_codec->opj_set_threads =
255             (OPJ_BOOL(*)(void * p_codec, OPJ_UINT32 num_threads)) opj_j2k_set_threads;
256
257         l_codec->m_codec = opj_j2k_create_decompress();
258
259         if (! l_codec->m_codec) {
260             opj_free(l_codec);
261             return NULL;
262         }
263
264         break;
265
266     case OPJ_CODEC_JP2:
267         /* get a JP2 decoder handle */
268         l_codec->opj_dump_codec = (void (*)(void*, OPJ_INT32, FILE*)) jp2_dump;
269
270         l_codec->opj_get_codec_info = (opj_codestream_info_v2_t* (*)(
271                                            void*)) jp2_get_cstr_info;
272
273         l_codec->opj_get_codec_index = (opj_codestream_index_t* (*)(
274                                             void*)) jp2_get_cstr_index;
275
276         l_codec->m_codec_data.m_decompression.opj_decode =
277             (OPJ_BOOL(*)(void *,
278                          struct opj_stream_private *,
279                          opj_image_t*,
280                          struct opj_event_mgr *)) opj_jp2_decode;
281
282         l_codec->m_codec_data.m_decompression.opj_end_decompress =
283             (OPJ_BOOL(*)(void *,
284                          struct opj_stream_private *,
285                          struct opj_event_mgr *)) opj_jp2_end_decompress;
286
287         l_codec->m_codec_data.m_decompression.opj_read_header =
288             (OPJ_BOOL(*)(struct opj_stream_private *,
289                          void *,
290                          opj_image_t **,
291                          struct opj_event_mgr *)) opj_jp2_read_header;
292
293         l_codec->m_codec_data.m_decompression.opj_read_tile_header =
294             (OPJ_BOOL(*)(void *,
295                          OPJ_UINT32*,
296                          OPJ_UINT32*,
297                          OPJ_INT32*,
298                          OPJ_INT32*,
299                          OPJ_INT32 *,
300                          OPJ_INT32 *,
301                          OPJ_UINT32 *,
302                          OPJ_BOOL *,
303                          struct opj_stream_private *,
304                          struct opj_event_mgr *)) opj_jp2_read_tile_header;
305
306         l_codec->m_codec_data.m_decompression.opj_decode_tile_data =
307             (OPJ_BOOL(*)(void *,
308                          OPJ_UINT32, OPJ_BYTE*, OPJ_UINT32,
309                          struct opj_stream_private *,
310                          struct opj_event_mgr *)) opj_jp2_decode_tile;
311
312         l_codec->m_codec_data.m_decompression.opj_destroy = (void (*)(
313                     void *))opj_jp2_destroy;
314
315         l_codec->m_codec_data.m_decompression.opj_setup_decoder =
316             (void (*)(void *, opj_dparameters_t *)) opj_jp2_setup_decoder;
317
318         l_codec->m_codec_data.m_decompression.opj_set_decode_area =
319             (OPJ_BOOL(*)(void *,
320                          opj_image_t*,
321                          OPJ_INT32, OPJ_INT32, OPJ_INT32, OPJ_INT32,
322                          struct opj_event_mgr *)) opj_jp2_set_decode_area;
323
324         l_codec->m_codec_data.m_decompression.opj_get_decoded_tile =
325             (OPJ_BOOL(*)(void *p_codec,
326                          opj_stream_private_t *p_cio,
327                          opj_image_t *p_image,
328                          struct opj_event_mgr * p_manager,
329                          OPJ_UINT32 tile_index)) opj_jp2_get_tile;
330
331         l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor =
332             (OPJ_BOOL(*)(void * p_codec,
333                          OPJ_UINT32 res_factor,
334                          opj_event_mgr_t * p_manager)) opj_jp2_set_decoded_resolution_factor;
335
336         l_codec->m_codec_data.m_decompression.opj_set_decoded_components =
337             (OPJ_BOOL(*)(void * p_codec,
338                          OPJ_UINT32 numcomps,
339                          const OPJ_UINT32 * comps_indices,
340                          struct opj_event_mgr * p_manager)) opj_jp2_set_decoded_components;
341
342         l_codec->opj_set_threads =
343             (OPJ_BOOL(*)(void * p_codec, OPJ_UINT32 num_threads)) opj_jp2_set_threads;
344
345         l_codec->m_codec = opj_jp2_create(OPJ_TRUE);
346
347         if (! l_codec->m_codec) {
348             opj_free(l_codec);
349             return 00;
350         }
351
352         break;
353     case OPJ_CODEC_UNKNOWN:
354     case OPJ_CODEC_JPT:
355     default:
356         opj_free(l_codec);
357         return 00;
358     }
359
360     opj_set_default_event_handler(&(l_codec->m_event_mgr));
361     return (opj_codec_t*) l_codec;
362 }
363
364 void OPJ_CALLCONV opj_set_default_decoder_parameters(opj_dparameters_t
365         *parameters)
366 {
367     if (parameters) {
368         memset(parameters, 0, sizeof(opj_dparameters_t));
369         /* default decoding parameters */
370         parameters->cp_layer = 0;
371         parameters->cp_reduce = 0;
372
373         parameters->decod_format = -1;
374         parameters->cod_format = -1;
375         parameters->flags = 0;
376         /* UniPG>> */
377 #ifdef USE_JPWL
378         parameters->jpwl_correct = OPJ_FALSE;
379         parameters->jpwl_exp_comps = JPWL_EXPECTED_COMPONENTS;
380         parameters->jpwl_max_tiles = JPWL_MAXIMUM_TILES;
381 #endif /* USE_JPWL */
382         /* <<UniPG */
383     }
384 }
385
386
387 OPJ_BOOL OPJ_CALLCONV opj_codec_set_threads(opj_codec_t *p_codec,
388         int num_threads)
389 {
390     if (p_codec && (num_threads >= 0)) {
391         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
392
393         return l_codec->opj_set_threads(l_codec->m_codec, (OPJ_UINT32)num_threads);
394     }
395     return OPJ_FALSE;
396 }
397
398 OPJ_BOOL OPJ_CALLCONV opj_setup_decoder(opj_codec_t *p_codec,
399                                         opj_dparameters_t *parameters
400                                        )
401 {
402     if (p_codec && parameters) {
403         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
404
405         if (! l_codec->is_decompressor) {
406             opj_event_msg(&(l_codec->m_event_mgr), EVT_ERROR,
407                           "Codec provided to the opj_setup_decoder function is not a decompressor handler.\n");
408             return OPJ_FALSE;
409         }
410
411         l_codec->m_codec_data.m_decompression.opj_setup_decoder(l_codec->m_codec,
412                 parameters);
413         return OPJ_TRUE;
414     }
415     return OPJ_FALSE;
416 }
417
418 OPJ_BOOL OPJ_CALLCONV opj_read_header(opj_stream_t *p_stream,
419                                       opj_codec_t *p_codec,
420                                       opj_image_t **p_image)
421 {
422     if (p_codec && p_stream) {
423         opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
424         opj_stream_private_t* l_stream = (opj_stream_private_t*) p_stream;
425
426         if (! l_codec->is_decompressor) {
427             opj_event_msg(&(l_codec->m_event_mgr), EVT_ERROR,
428                           "Codec provided to the opj_read_header function is not a decompressor handler.\n");
429             return OPJ_FALSE;
430         }
431
432         return l_codec->m_codec_data.m_decompression.opj_read_header(l_stream,
433                 l_codec->m_codec,
434                 p_image,
435                 &(l_codec->m_event_mgr));
436     }
437
438     return OPJ_FALSE;
439 }
440
441
442 OPJ_BOOL OPJ_CALLCONV opj_set_decoded_components(opj_codec_t *p_codec,
443         OPJ_UINT32 numcomps,
444         const OPJ_UINT32* comps_indices,
445         OPJ_BOOL apply_color_transforms)
446 {
447     if (p_codec) {
448         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
449
450         if (! l_codec->is_decompressor) {
451             opj_event_msg(&(l_codec->m_event_mgr), EVT_ERROR,
452                           "Codec provided to the opj_set_decoded_components function is not a decompressor handler.\n");
453             return OPJ_FALSE;
454         }
455
456         if (apply_color_transforms) {
457             opj_event_msg(&(l_codec->m_event_mgr), EVT_ERROR,
458                           "apply_color_transforms = OPJ_TRUE is not supported.\n");
459             return OPJ_FALSE;
460         }
461
462         return  l_codec->m_codec_data.m_decompression.opj_set_decoded_components(
463                     l_codec->m_codec,
464                     numcomps,
465                     comps_indices,
466                     &(l_codec->m_event_mgr));
467     }
468     return OPJ_FALSE;
469 }
470
471 OPJ_BOOL OPJ_CALLCONV opj_decode(opj_codec_t *p_codec,
472                                  opj_stream_t *p_stream,
473                                  opj_image_t* p_image)
474 {
475     if (p_codec && p_stream) {
476         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
477         opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
478
479         if (! l_codec->is_decompressor) {
480             return OPJ_FALSE;
481         }
482
483         return l_codec->m_codec_data.m_decompression.opj_decode(l_codec->m_codec,
484                 l_stream,
485                 p_image,
486                 &(l_codec->m_event_mgr));
487     }
488
489     return OPJ_FALSE;
490 }
491
492 OPJ_BOOL OPJ_CALLCONV opj_set_decode_area(opj_codec_t *p_codec,
493         opj_image_t* p_image,
494         OPJ_INT32 p_start_x, OPJ_INT32 p_start_y,
495         OPJ_INT32 p_end_x, OPJ_INT32 p_end_y
496                                          )
497 {
498     if (p_codec) {
499         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
500
501         if (! l_codec->is_decompressor) {
502             return OPJ_FALSE;
503         }
504
505         return  l_codec->m_codec_data.m_decompression.opj_set_decode_area(
506                     l_codec->m_codec,
507                     p_image,
508                     p_start_x, p_start_y,
509                     p_end_x, p_end_y,
510                     &(l_codec->m_event_mgr));
511     }
512     return OPJ_FALSE;
513 }
514
515 OPJ_BOOL OPJ_CALLCONV opj_read_tile_header(opj_codec_t *p_codec,
516         opj_stream_t * p_stream,
517         OPJ_UINT32 * p_tile_index,
518         OPJ_UINT32 * p_data_size,
519         OPJ_INT32 * p_tile_x0, OPJ_INT32 * p_tile_y0,
520         OPJ_INT32 * p_tile_x1, OPJ_INT32 * p_tile_y1,
521         OPJ_UINT32 * p_nb_comps,
522         OPJ_BOOL * p_should_go_on)
523 {
524     if (p_codec && p_stream && p_data_size && p_tile_index) {
525         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
526         opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
527
528         if (! l_codec->is_decompressor) {
529             return OPJ_FALSE;
530         }
531
532         return l_codec->m_codec_data.m_decompression.opj_read_tile_header(
533                    l_codec->m_codec,
534                    p_tile_index,
535                    p_data_size,
536                    p_tile_x0, p_tile_y0,
537                    p_tile_x1, p_tile_y1,
538                    p_nb_comps,
539                    p_should_go_on,
540                    l_stream,
541                    &(l_codec->m_event_mgr));
542     }
543     return OPJ_FALSE;
544 }
545
546 OPJ_BOOL OPJ_CALLCONV opj_decode_tile_data(opj_codec_t *p_codec,
547         OPJ_UINT32 p_tile_index,
548         OPJ_BYTE * p_data,
549         OPJ_UINT32 p_data_size,
550         opj_stream_t *p_stream
551                                           )
552 {
553     if (p_codec && p_data && p_stream) {
554         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
555         opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
556
557         if (! l_codec->is_decompressor) {
558             return OPJ_FALSE;
559         }
560
561         return l_codec->m_codec_data.m_decompression.opj_decode_tile_data(
562                    l_codec->m_codec,
563                    p_tile_index,
564                    p_data,
565                    p_data_size,
566                    l_stream,
567                    &(l_codec->m_event_mgr));
568     }
569     return OPJ_FALSE;
570 }
571
572 OPJ_BOOL OPJ_CALLCONV opj_get_decoded_tile(opj_codec_t *p_codec,
573         opj_stream_t *p_stream,
574         opj_image_t *p_image,
575         OPJ_UINT32 tile_index)
576 {
577     if (p_codec && p_stream) {
578         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
579         opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
580
581         if (! l_codec->is_decompressor) {
582             return OPJ_FALSE;
583         }
584
585         return l_codec->m_codec_data.m_decompression.opj_get_decoded_tile(
586                    l_codec->m_codec,
587                    l_stream,
588                    p_image,
589                    &(l_codec->m_event_mgr),
590                    tile_index);
591     }
592
593     return OPJ_FALSE;
594 }
595
596 OPJ_BOOL OPJ_CALLCONV opj_set_decoded_resolution_factor(opj_codec_t *p_codec,
597         OPJ_UINT32 res_factor)
598 {
599     opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
600
601     if (!l_codec) {
602         return OPJ_FALSE;
603     }
604
605     return l_codec->m_codec_data.m_decompression.opj_set_decoded_resolution_factor(
606                l_codec->m_codec,
607                res_factor,
608                &(l_codec->m_event_mgr));
609 }
610
611 /* ---------------------------------------------------------------------- */
612 /* COMPRESSION FUNCTIONS*/
613
614 opj_codec_t* OPJ_CALLCONV opj_create_compress(OPJ_CODEC_FORMAT p_format)
615 {
616     opj_codec_private_t *l_codec = 00;
617
618     l_codec = (opj_codec_private_t*)opj_calloc(1, sizeof(opj_codec_private_t));
619     if (!l_codec) {
620         return 00;
621     }
622
623     l_codec->is_decompressor = 0;
624
625     switch (p_format) {
626     case OPJ_CODEC_J2K:
627         l_codec->m_codec_data.m_compression.opj_encode = (OPJ_BOOL(*)(void *,
628                 struct opj_stream_private *,
629                 struct opj_event_mgr *)) opj_j2k_encode;
630
631         l_codec->m_codec_data.m_compression.opj_end_compress = (OPJ_BOOL(*)(void *,
632                 struct opj_stream_private *,
633                 struct opj_event_mgr *)) opj_j2k_end_compress;
634
635         l_codec->m_codec_data.m_compression.opj_start_compress = (OPJ_BOOL(*)(void *,
636                 struct opj_stream_private *,
637                 struct opj_image *,
638                 struct opj_event_mgr *)) opj_j2k_start_compress;
639
640         l_codec->m_codec_data.m_compression.opj_write_tile = (OPJ_BOOL(*)(void *,
641                 OPJ_UINT32,
642                 OPJ_BYTE*,
643                 OPJ_UINT32,
644                 struct opj_stream_private *,
645                 struct opj_event_mgr *)) opj_j2k_write_tile;
646
647         l_codec->m_codec_data.m_compression.opj_destroy = (void (*)(
648                     void *)) opj_j2k_destroy;
649
650         l_codec->m_codec_data.m_compression.opj_setup_encoder = (OPJ_BOOL(*)(void *,
651                 opj_cparameters_t *,
652                 struct opj_image *,
653                 struct opj_event_mgr *)) opj_j2k_setup_encoder;
654
655         l_codec->m_codec_data.m_compression.opj_encoder_set_extra_options = (OPJ_BOOL(
656                     *)(void *,
657                        const char* const*,
658                        struct opj_event_mgr *)) opj_j2k_encoder_set_extra_options;
659
660         l_codec->opj_set_threads =
661             (OPJ_BOOL(*)(void * p_codec, OPJ_UINT32 num_threads)) opj_j2k_set_threads;
662
663         l_codec->m_codec = opj_j2k_create_compress();
664         if (! l_codec->m_codec) {
665             opj_free(l_codec);
666             return 00;
667         }
668
669         break;
670
671     case OPJ_CODEC_JP2:
672         /* get a JP2 decoder handle */
673         l_codec->m_codec_data.m_compression.opj_encode = (OPJ_BOOL(*)(void *,
674                 struct opj_stream_private *,
675                 struct opj_event_mgr *)) opj_jp2_encode;
676
677         l_codec->m_codec_data.m_compression.opj_end_compress = (OPJ_BOOL(*)(void *,
678                 struct opj_stream_private *,
679                 struct opj_event_mgr *)) opj_jp2_end_compress;
680
681         l_codec->m_codec_data.m_compression.opj_start_compress = (OPJ_BOOL(*)(void *,
682                 struct opj_stream_private *,
683                 struct opj_image *,
684                 struct opj_event_mgr *))  opj_jp2_start_compress;
685
686         l_codec->m_codec_data.m_compression.opj_write_tile = (OPJ_BOOL(*)(void *,
687                 OPJ_UINT32,
688                 OPJ_BYTE*,
689                 OPJ_UINT32,
690                 struct opj_stream_private *,
691                 struct opj_event_mgr *)) opj_jp2_write_tile;
692
693         l_codec->m_codec_data.m_compression.opj_destroy = (void (*)(
694                     void *)) opj_jp2_destroy;
695
696         l_codec->m_codec_data.m_compression.opj_setup_encoder = (OPJ_BOOL(*)(void *,
697                 opj_cparameters_t *,
698                 struct opj_image *,
699                 struct opj_event_mgr *)) opj_jp2_setup_encoder;
700
701         l_codec->m_codec_data.m_compression.opj_encoder_set_extra_options = (OPJ_BOOL(
702                     *)(void *,
703                        const char* const*,
704                        struct opj_event_mgr *)) opj_jp2_encoder_set_extra_options;
705
706         l_codec->opj_set_threads =
707             (OPJ_BOOL(*)(void * p_codec, OPJ_UINT32 num_threads)) opj_jp2_set_threads;
708
709         l_codec->m_codec = opj_jp2_create(OPJ_FALSE);
710         if (! l_codec->m_codec) {
711             opj_free(l_codec);
712             return 00;
713         }
714
715         break;
716
717     case OPJ_CODEC_UNKNOWN:
718     case OPJ_CODEC_JPT:
719     default:
720         opj_free(l_codec);
721         return 00;
722     }
723
724     opj_set_default_event_handler(&(l_codec->m_event_mgr));
725     return (opj_codec_t*) l_codec;
726 }
727
728 void OPJ_CALLCONV opj_set_default_encoder_parameters(opj_cparameters_t
729         *parameters)
730 {
731     if (parameters) {
732         memset(parameters, 0, sizeof(opj_cparameters_t));
733         /* default coding parameters */
734         parameters->cp_cinema = OPJ_OFF; /* DEPRECATED */
735         parameters->rsiz = OPJ_PROFILE_NONE;
736         parameters->max_comp_size = 0;
737         parameters->numresolution = OPJ_COMP_PARAM_DEFAULT_NUMRESOLUTION;
738         parameters->cp_rsiz = OPJ_STD_RSIZ; /* DEPRECATED */
739         parameters->cblockw_init = OPJ_COMP_PARAM_DEFAULT_CBLOCKW;
740         parameters->cblockh_init = OPJ_COMP_PARAM_DEFAULT_CBLOCKH;
741         parameters->prog_order = OPJ_COMP_PARAM_DEFAULT_PROG_ORDER;
742         parameters->roi_compno = -1;        /* no ROI */
743         parameters->subsampling_dx = 1;
744         parameters->subsampling_dy = 1;
745         parameters->tp_on = 0;
746         parameters->decod_format = -1;
747         parameters->cod_format = -1;
748         parameters->tcp_rates[0] = 0;
749         parameters->tcp_numlayers = 0;
750         parameters->cp_disto_alloc = 0;
751         parameters->cp_fixed_alloc = 0;
752         parameters->cp_fixed_quality = 0;
753         parameters->jpip_on = OPJ_FALSE;
754         /* UniPG>> */
755 #ifdef USE_JPWL
756         parameters->jpwl_epc_on = OPJ_FALSE;
757         parameters->jpwl_hprot_MH = -1; /* -1 means unassigned */
758         {
759             int i;
760             for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
761                 parameters->jpwl_hprot_TPH_tileno[i] = -1; /* unassigned */
762                 parameters->jpwl_hprot_TPH[i] = 0; /* absent */
763             }
764         };
765         {
766             int i;
767             for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
768                 parameters->jpwl_pprot_tileno[i] = -1; /* unassigned */
769                 parameters->jpwl_pprot_packno[i] = -1; /* unassigned */
770                 parameters->jpwl_pprot[i] = 0; /* absent */
771             }
772         };
773         parameters->jpwl_sens_size = 0; /* 0 means no ESD */
774         parameters->jpwl_sens_addr = 0; /* 0 means auto */
775         parameters->jpwl_sens_range = 0; /* 0 means packet */
776         parameters->jpwl_sens_MH = -1; /* -1 means unassigned */
777         {
778             int i;
779             for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
780                 parameters->jpwl_sens_TPH_tileno[i] = -1; /* unassigned */
781                 parameters->jpwl_sens_TPH[i] = -1; /* absent */
782             }
783         };
784 #endif /* USE_JPWL */
785         /* <<UniPG */
786     }
787 }
788
789 OPJ_BOOL OPJ_CALLCONV opj_setup_encoder(opj_codec_t *p_codec,
790                                         opj_cparameters_t *parameters,
791                                         opj_image_t *p_image)
792 {
793     if (p_codec && parameters && p_image) {
794         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
795
796         if (! l_codec->is_decompressor) {
797             return l_codec->m_codec_data.m_compression.opj_setup_encoder(l_codec->m_codec,
798                     parameters,
799                     p_image,
800                     &(l_codec->m_event_mgr));
801         }
802     }
803
804     return OPJ_FALSE;
805 }
806
807 /* ----------------------------------------------------------------------- */
808
809 OPJ_BOOL OPJ_CALLCONV opj_encoder_set_extra_options(opj_codec_t *p_codec,
810         const char* const* options)
811 {
812     if (p_codec) {
813         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
814
815         if (! l_codec->is_decompressor) {
816             return l_codec->m_codec_data.m_compression.opj_encoder_set_extra_options(
817                        l_codec->m_codec,
818                        options,
819                        &(l_codec->m_event_mgr));
820         }
821     }
822
823     return OPJ_FALSE;
824 }
825
826 /* ----------------------------------------------------------------------- */
827
828 OPJ_BOOL OPJ_CALLCONV opj_start_compress(opj_codec_t *p_codec,
829         opj_image_t * p_image,
830         opj_stream_t *p_stream)
831 {
832     if (p_codec && p_stream) {
833         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
834         opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
835
836         if (! l_codec->is_decompressor) {
837             return l_codec->m_codec_data.m_compression.opj_start_compress(l_codec->m_codec,
838                     l_stream,
839                     p_image,
840                     &(l_codec->m_event_mgr));
841         }
842     }
843
844     return OPJ_FALSE;
845 }
846
847 OPJ_BOOL OPJ_CALLCONV opj_encode(opj_codec_t *p_info, opj_stream_t *p_stream)
848 {
849     if (p_info && p_stream) {
850         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_info;
851         opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
852
853         if (! l_codec->is_decompressor) {
854             return l_codec->m_codec_data.m_compression.opj_encode(l_codec->m_codec,
855                     l_stream,
856                     &(l_codec->m_event_mgr));
857         }
858     }
859
860     return OPJ_FALSE;
861
862 }
863
864 OPJ_BOOL OPJ_CALLCONV opj_end_compress(opj_codec_t *p_codec,
865                                        opj_stream_t *p_stream)
866 {
867     if (p_codec && p_stream) {
868         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
869         opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
870
871         if (! l_codec->is_decompressor) {
872             return l_codec->m_codec_data.m_compression.opj_end_compress(l_codec->m_codec,
873                     l_stream,
874                     &(l_codec->m_event_mgr));
875         }
876     }
877     return OPJ_FALSE;
878
879 }
880
881 OPJ_BOOL OPJ_CALLCONV opj_end_decompress(opj_codec_t *p_codec,
882         opj_stream_t *p_stream)
883 {
884     if (p_codec && p_stream) {
885         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
886         opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
887
888         if (! l_codec->is_decompressor) {
889             return OPJ_FALSE;
890         }
891
892         return l_codec->m_codec_data.m_decompression.opj_end_decompress(
893                    l_codec->m_codec,
894                    l_stream,
895                    &(l_codec->m_event_mgr));
896     }
897
898     return OPJ_FALSE;
899 }
900
901 OPJ_BOOL OPJ_CALLCONV opj_set_MCT(opj_cparameters_t *parameters,
902                                   OPJ_FLOAT32 * pEncodingMatrix,
903                                   OPJ_INT32 * p_dc_shift, OPJ_UINT32 pNbComp)
904 {
905     OPJ_UINT32 l_matrix_size = pNbComp * pNbComp * (OPJ_UINT32)sizeof(OPJ_FLOAT32);
906     OPJ_UINT32 l_dc_shift_size = pNbComp * (OPJ_UINT32)sizeof(OPJ_INT32);
907     OPJ_UINT32 l_mct_total_size = l_matrix_size + l_dc_shift_size;
908
909     /* add MCT capability */
910     if (OPJ_IS_PART2(parameters->rsiz)) {
911         parameters->rsiz |= OPJ_EXTENSION_MCT;
912     } else {
913         parameters->rsiz = ((OPJ_PROFILE_PART2) | (OPJ_EXTENSION_MCT));
914     }
915     parameters->irreversible = 1;
916
917     /* use array based MCT */
918     parameters->tcp_mct = 2;
919     parameters->mct_data = opj_malloc(l_mct_total_size);
920     if (! parameters->mct_data) {
921         return OPJ_FALSE;
922     }
923
924     memcpy(parameters->mct_data, pEncodingMatrix, l_matrix_size);
925     memcpy(((OPJ_BYTE *) parameters->mct_data) +  l_matrix_size, p_dc_shift,
926            l_dc_shift_size);
927
928     return OPJ_TRUE;
929 }
930
931 OPJ_BOOL OPJ_CALLCONV opj_write_tile(opj_codec_t *p_codec,
932                                      OPJ_UINT32 p_tile_index,
933                                      OPJ_BYTE * p_data,
934                                      OPJ_UINT32 p_data_size,
935                                      opj_stream_t *p_stream)
936 {
937     if (p_codec && p_stream && p_data) {
938         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
939         opj_stream_private_t * l_stream = (opj_stream_private_t *) p_stream;
940
941         if (l_codec->is_decompressor) {
942             return OPJ_FALSE;
943         }
944
945         return l_codec->m_codec_data.m_compression.opj_write_tile(l_codec->m_codec,
946                 p_tile_index,
947                 p_data,
948                 p_data_size,
949                 l_stream,
950                 &(l_codec->m_event_mgr));
951     }
952
953     return OPJ_FALSE;
954 }
955
956 /* ---------------------------------------------------------------------- */
957
958 void OPJ_CALLCONV opj_destroy_codec(opj_codec_t *p_codec)
959 {
960     if (p_codec) {
961         opj_codec_private_t * l_codec = (opj_codec_private_t *) p_codec;
962
963         if (l_codec->is_decompressor) {
964             l_codec->m_codec_data.m_decompression.opj_destroy(l_codec->m_codec);
965         } else {
966             l_codec->m_codec_data.m_compression.opj_destroy(l_codec->m_codec);
967         }
968
969         l_codec->m_codec = 00;
970         opj_free(l_codec);
971     }
972 }
973
974 /* ---------------------------------------------------------------------- */
975
976 void OPJ_CALLCONV opj_dump_codec(opj_codec_t *p_codec,
977                                  OPJ_INT32 info_flag,
978                                  FILE* output_stream)
979 {
980     if (p_codec) {
981         opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
982
983         l_codec->opj_dump_codec(l_codec->m_codec, info_flag, output_stream);
984         return;
985     }
986
987     /* TODO return error */
988     /* fprintf(stderr, "[ERROR] Input parameter of the dump_codec function are incorrect.\n"); */
989     return;
990 }
991
992 opj_codestream_info_v2_t* OPJ_CALLCONV opj_get_cstr_info(opj_codec_t *p_codec)
993 {
994     if (p_codec) {
995         opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
996
997         return l_codec->opj_get_codec_info(l_codec->m_codec);
998     }
999
1000     return NULL;
1001 }
1002
1003 void OPJ_CALLCONV opj_destroy_cstr_info(opj_codestream_info_v2_t **cstr_info)
1004 {
1005     if (cstr_info) {
1006
1007         if ((*cstr_info)->m_default_tile_info.tccp_info) {
1008             opj_free((*cstr_info)->m_default_tile_info.tccp_info);
1009         }
1010
1011         if ((*cstr_info)->tile_info) {
1012             /* FIXME not used for the moment*/
1013         }
1014
1015         opj_free((*cstr_info));
1016         (*cstr_info) = NULL;
1017     }
1018 }
1019
1020 opj_codestream_index_t * OPJ_CALLCONV opj_get_cstr_index(opj_codec_t *p_codec)
1021 {
1022     if (p_codec) {
1023         opj_codec_private_t* l_codec = (opj_codec_private_t*) p_codec;
1024
1025         return l_codec->opj_get_codec_index(l_codec->m_codec);
1026     }
1027
1028     return NULL;
1029 }
1030
1031 void OPJ_CALLCONV opj_destroy_cstr_index(opj_codestream_index_t **p_cstr_index)
1032 {
1033     if (*p_cstr_index) {
1034         j2k_destroy_cstr_index(*p_cstr_index);
1035         (*p_cstr_index) = NULL;
1036     }
1037 }
1038
1039 opj_stream_t* OPJ_CALLCONV opj_stream_create_default_file_stream(
1040     const char *fname, OPJ_BOOL p_is_read_stream)
1041 {
1042     return opj_stream_create_file_stream(fname, OPJ_J2K_STREAM_CHUNK_SIZE,
1043                                          p_is_read_stream);
1044 }
1045
1046 opj_stream_t* OPJ_CALLCONV opj_stream_create_file_stream(
1047     const char *fname,
1048     OPJ_SIZE_T p_size,
1049     OPJ_BOOL p_is_read_stream)
1050 {
1051     opj_stream_t* l_stream = 00;
1052     FILE *p_file;
1053     const char *mode;
1054
1055     if (! fname) {
1056         return NULL;
1057     }
1058
1059     if (p_is_read_stream) {
1060         mode = "rb";
1061     } else {
1062         mode = "wb";
1063     }
1064
1065     p_file = fopen(fname, mode);
1066
1067     if (! p_file) {
1068         return NULL;
1069     }
1070
1071     l_stream = opj_stream_create(p_size, p_is_read_stream);
1072     if (! l_stream) {
1073         fclose(p_file);
1074         return NULL;
1075     }
1076
1077     opj_stream_set_user_data(l_stream, p_file,
1078                              (opj_stream_free_user_data_fn) fclose);
1079     opj_stream_set_user_data_length(l_stream,
1080                                     opj_get_data_length_from_file(p_file));
1081     opj_stream_set_read_function(l_stream, (opj_stream_read_fn) opj_read_from_file);
1082     opj_stream_set_write_function(l_stream,
1083                                   (opj_stream_write_fn) opj_write_from_file);
1084     opj_stream_set_skip_function(l_stream, (opj_stream_skip_fn) opj_skip_from_file);
1085     opj_stream_set_seek_function(l_stream, (opj_stream_seek_fn) opj_seek_from_file);
1086
1087     return l_stream;
1088 }
1089
1090
1091 void* OPJ_CALLCONV opj_image_data_alloc(OPJ_SIZE_T size)
1092 {
1093     void* ret = opj_aligned_malloc(size);
1094     /* printf("opj_image_data_alloc %p\n", ret); */
1095     return ret;
1096 }
1097
1098 void OPJ_CALLCONV opj_image_data_free(void* ptr)
1099 {
1100     /* printf("opj_image_data_free %p\n", ptr); */
1101     opj_aligned_free(ptr);
1102 }