removed unused parameters warnings with the solution proposed by myself and Bob Fries...
[openjpeg.git] / libopenjpeg / openjpeg.c
1 /*
2  * Copyright (c) 2005, Herve Drolon, FreeImage Team
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24  * POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #ifdef _WIN32
28 #include <windows.h>
29 #endif /* _WIN32 */
30
31 #include "opj_config.h"
32 #include "opj_includes.h"
33
34 /* ---------------------------------------------------------------------- */
35 #ifdef _WIN32
36 #ifndef OPJ_STATIC
37 BOOL APIENTRY
38 DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
39
40         OPJ_ARG_NOT_USED(lpReserved);
41         OPJ_ARG_NOT_USED(hModule);
42
43         switch (ul_reason_for_call) {
44                 case DLL_PROCESS_ATTACH :
45                         break;
46                 case DLL_PROCESS_DETACH :
47                         break;
48                 case DLL_THREAD_ATTACH :
49                 case DLL_THREAD_DETACH :
50                         break;
51     }
52
53     return TRUE;
54 }
55 #endif /* OPJ_STATIC */
56 #endif /* _WIN32 */
57
58 /* ---------------------------------------------------------------------- */
59
60
61 const char* OPJ_CALLCONV opj_version(void) {
62     return PACKAGE_VERSION;
63 }
64
65 opj_dinfo_t* OPJ_CALLCONV opj_create_decompress(OPJ_CODEC_FORMAT format) {
66         opj_dinfo_t *dinfo = (opj_dinfo_t*)opj_calloc(1, sizeof(opj_dinfo_t));
67         if(!dinfo) return NULL;
68         dinfo->is_decompressor = OPJ_TRUE;
69         switch(format) {
70                 case CODEC_J2K:
71                 case CODEC_JPT:
72                         /* get a J2K decoder handle */
73                         dinfo->j2k_handle = (void*)j2k_create_decompress((opj_common_ptr)dinfo);
74                         if(!dinfo->j2k_handle) {
75                                 opj_free(dinfo);
76                                 return NULL;
77                         }
78                         break;
79                 case CODEC_JP2:
80                         /* get a JP2 decoder handle */
81                         dinfo->jp2_handle = (void*)jp2_create_decompress((opj_common_ptr)dinfo);
82                         if(!dinfo->jp2_handle) {
83                                 opj_free(dinfo);
84                                 return NULL;
85                         }
86                         break;
87                 case CODEC_UNKNOWN:
88                 default:
89                         opj_free(dinfo);
90                         return NULL;
91         }
92
93         dinfo->codec_format = format;
94
95         return dinfo;
96 }
97
98 void OPJ_CALLCONV opj_destroy_decompress(opj_dinfo_t *dinfo) {
99         if(dinfo) {
100                 /* destroy the codec */
101                 switch(dinfo->codec_format) {
102                         case CODEC_J2K:
103                         case CODEC_JPT:
104                                 j2k_destroy_decompress((opj_j2k_t*)dinfo->j2k_handle);
105                                 break;
106                         case CODEC_JP2:
107                                 jp2_destroy_decompress((opj_jp2_t*)dinfo->jp2_handle);
108                                 break;
109                         case CODEC_UNKNOWN:
110                         default:
111                                 break;
112                 }
113                 /* destroy the decompressor */
114                 opj_free(dinfo);
115         }
116 }
117
118 void OPJ_CALLCONV opj_set_default_decoder_parameters(opj_dparameters_t *parameters) {
119         if(parameters) {
120                 memset(parameters, 0, sizeof(opj_dparameters_t));
121                 /* default decoding parameters */
122                 parameters->cp_layer = 0;
123                 parameters->cp_reduce = 0;
124                 parameters->cp_limit_decoding = NO_LIMITATION;
125
126                 parameters->decod_format = -1;
127                 parameters->cod_format = -1;
128 /* UniPG>> */
129 #ifdef USE_JPWL
130                 parameters->jpwl_correct = OPJ_FALSE;
131                 parameters->jpwl_exp_comps = JPWL_EXPECTED_COMPONENTS;
132                 parameters->jpwl_max_tiles = JPWL_MAXIMUM_TILES;
133 #endif /* USE_JPWL */
134 /* <<UniPG */
135         }
136 }
137
138 void OPJ_CALLCONV opj_setup_decoder(opj_dinfo_t *dinfo, opj_dparameters_t *parameters) {
139         if(dinfo && parameters) {
140                 switch(dinfo->codec_format) {
141                         case CODEC_J2K:
142                         case CODEC_JPT:
143                                 j2k_setup_decoder((opj_j2k_t*)dinfo->j2k_handle, parameters);
144                                 break;
145                         case CODEC_JP2:
146                                 jp2_setup_decoder((opj_jp2_t*)dinfo->jp2_handle, parameters);
147                                 break;
148                         case CODEC_UNKNOWN:
149                         default:
150                                 break;
151                 }
152         }
153 }
154
155 opj_image_t* OPJ_CALLCONV opj_decode(opj_dinfo_t *dinfo, opj_cio_t *cio) {
156         return opj_decode_with_info(dinfo, cio, NULL);
157 }
158
159 opj_image_t* OPJ_CALLCONV opj_decode_with_info(opj_dinfo_t *dinfo, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
160         if(dinfo && cio) {
161                 switch(dinfo->codec_format) {
162                         case CODEC_J2K:
163                                 return j2k_decode((opj_j2k_t*)dinfo->j2k_handle, cio, cstr_info);
164                         case CODEC_JPT:
165                                 return j2k_decode_jpt_stream((opj_j2k_t*)dinfo->j2k_handle, cio, cstr_info);
166                         case CODEC_JP2:
167                                 return opj_jp2_decode((opj_jp2_t*)dinfo->jp2_handle, cio, cstr_info);
168                         case CODEC_UNKNOWN:
169                         default:
170                                 break;
171                 }
172         }
173         return NULL;
174 }
175
176 opj_cinfo_t* OPJ_CALLCONV opj_create_compress(OPJ_CODEC_FORMAT format) {
177         opj_cinfo_t *cinfo = (opj_cinfo_t*)opj_calloc(1, sizeof(opj_cinfo_t));
178         if(!cinfo) return NULL;
179         cinfo->is_decompressor = OPJ_FALSE;
180         switch(format) {
181                 case CODEC_J2K:
182                         /* get a J2K coder handle */
183                         cinfo->j2k_handle = (void*)j2k_create_compress((opj_common_ptr)cinfo);
184                         if(!cinfo->j2k_handle) {
185                                 opj_free(cinfo);
186                                 return NULL;
187                         }
188                         break;
189                 case CODEC_JP2:
190                         /* get a JP2 coder handle */
191                         cinfo->jp2_handle = (void*)jp2_create_compress((opj_common_ptr)cinfo);
192                         if(!cinfo->jp2_handle) {
193                                 opj_free(cinfo);
194                                 return NULL;
195                         }
196                         break;
197                 case CODEC_JPT:
198                 case CODEC_UNKNOWN:
199                 default:
200                         opj_free(cinfo);
201                         return NULL;
202         }
203
204         cinfo->codec_format = format;
205
206         return cinfo;
207 }
208
209 void OPJ_CALLCONV opj_destroy_compress(opj_cinfo_t *cinfo) {
210         if(cinfo) {
211                 /* destroy the codec */
212                 switch(cinfo->codec_format) {
213                         case CODEC_J2K:
214                                 j2k_destroy_compress((opj_j2k_t*)cinfo->j2k_handle);
215                                 break;
216                         case CODEC_JP2:
217                                 jp2_destroy_compress((opj_jp2_t*)cinfo->jp2_handle);
218                                 break;
219                         case CODEC_JPT:
220                         case CODEC_UNKNOWN:
221                         default:
222                                 break;
223                 }
224                 /* destroy the decompressor */
225                 opj_free(cinfo);
226         }
227 }
228
229 void OPJ_CALLCONV opj_set_default_encoder_parameters(opj_cparameters_t *parameters) {
230         if(parameters) {
231                 memset(parameters, 0, sizeof(opj_cparameters_t));
232                 /* default coding parameters */
233                 parameters->cp_cinema = OFF; 
234                 parameters->max_comp_size = 0;
235                 parameters->numresolution = 6;
236                 parameters->cp_rsiz = STD_RSIZ;
237                 parameters->cblockw_init = 64;
238                 parameters->cblockh_init = 64;
239                 parameters->prog_order = LRCP;
240                 parameters->roi_compno = -1;            /* no ROI */
241                 parameters->subsampling_dx = 1;
242                 parameters->subsampling_dy = 1;
243                 parameters->tp_on = 0;
244                 parameters->decod_format = -1;
245                 parameters->cod_format = -1;
246                 parameters->tcp_rates[0] = 0;   
247                 parameters->tcp_numlayers = 0;
248     parameters->cp_disto_alloc = 0;
249                 parameters->cp_fixed_alloc = 0;
250                 parameters->cp_fixed_quality = 0;
251
252 /* UniPG>> */
253 #ifdef USE_JPWL
254                 parameters->jpwl_epc_on = OPJ_FALSE;
255                 parameters->jpwl_hprot_MH = -1; /* -1 means unassigned */
256                 {
257                         int i;
258                         for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
259                                 parameters->jpwl_hprot_TPH_tileno[i] = -1; /* unassigned */
260                                 parameters->jpwl_hprot_TPH[i] = 0; /* absent */
261                         }
262                 };
263                 {
264                         int i;
265                         for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
266                                 parameters->jpwl_pprot_tileno[i] = -1; /* unassigned */
267                                 parameters->jpwl_pprot_packno[i] = -1; /* unassigned */
268                                 parameters->jpwl_pprot[i] = 0; /* absent */
269                         }
270                 };
271                 parameters->jpwl_sens_size = 0; /* 0 means no ESD */
272                 parameters->jpwl_sens_addr = 0; /* 0 means auto */
273                 parameters->jpwl_sens_range = 0; /* 0 means packet */
274                 parameters->jpwl_sens_MH = -1; /* -1 means unassigned */
275                 {
276                         int i;
277                         for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
278                                 parameters->jpwl_sens_TPH_tileno[i] = -1; /* unassigned */
279                                 parameters->jpwl_sens_TPH[i] = -1; /* absent */
280                         }
281                 };
282 #endif /* USE_JPWL */
283 /* <<UniPG */
284         }
285 }
286
287 void OPJ_CALLCONV opj_setup_encoder(opj_cinfo_t *cinfo, opj_cparameters_t *parameters, opj_image_t *image) {
288         if(cinfo && parameters && image) {
289                 switch(cinfo->codec_format) {
290                         case CODEC_J2K:
291                                 j2k_setup_encoder((opj_j2k_t*)cinfo->j2k_handle, parameters, image);
292                                 break;
293                         case CODEC_JP2:
294                                 jp2_setup_encoder((opj_jp2_t*)cinfo->jp2_handle, parameters, image);
295                                 break;
296                         case CODEC_JPT:
297                         case CODEC_UNKNOWN:
298                         default:
299                                 break;
300                 }
301         }
302 }
303
304 opj_bool OPJ_CALLCONV opj_encode(opj_cinfo_t *cinfo, opj_cio_t *cio, opj_image_t *image, char *index) {
305         if (index != NULL)
306                 opj_event_msg((opj_common_ptr)cinfo, EVT_WARNING, "Set index to NULL when calling the opj_encode function.\n"
307                 "To extract the index, use the opj_encode_with_info() function.\n"
308                 "No index will be generated during this encoding\n");
309         return opj_encode_with_info(cinfo, cio, image, NULL);
310 }
311
312 opj_bool OPJ_CALLCONV opj_encode_with_info(opj_cinfo_t *cinfo, opj_cio_t *cio, opj_image_t *image, opj_codestream_info_t *cstr_info) {
313         if(cinfo && cio && image) {
314                 switch(cinfo->codec_format) {
315                         case CODEC_J2K:
316                                 return j2k_encode((opj_j2k_t*)cinfo->j2k_handle, cio, image, cstr_info);
317                         case CODEC_JP2:
318                                 return opj_jp2_encode((opj_jp2_t*)cinfo->jp2_handle, cio, image, cstr_info);
319                         case CODEC_JPT:
320                         case CODEC_UNKNOWN:
321                         default:
322                                 break;
323                 }
324         }
325         return OPJ_FALSE;
326 }
327
328 void OPJ_CALLCONV opj_destroy_cstr_info(opj_codestream_info_t *cstr_info) {
329         if (cstr_info) {
330                 int tileno;
331                 for (tileno = 0; tileno < cstr_info->tw * cstr_info->th; tileno++) {
332                         opj_tile_info_t *tile_info = &cstr_info->tile[tileno];
333                         opj_free(tile_info->thresh);
334                         opj_free(tile_info->packet);
335                         opj_free(tile_info->tp);
336                 }
337                 opj_free(cstr_info->tile);
338                 opj_free(cstr_info->marker);
339                 opj_free(cstr_info->numdecompos);
340         }
341 }