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