defined new type "opj_bool", and new constants OPJ_FALSE and OPJ_TRUE, to avoid havin...
[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         switch (ul_reason_for_call) {
40                 case DLL_PROCESS_ATTACH :
41                         break;
42                 case DLL_PROCESS_DETACH :
43                         break;
44                 case DLL_THREAD_ATTACH :
45                 case DLL_THREAD_DETACH :
46                         break;
47     }
48
49     return TRUE;
50 }
51 #endif /* OPJ_STATIC */
52 #endif /* _WIN32 */
53
54 /* ---------------------------------------------------------------------- */
55
56
57 const char* OPJ_CALLCONV opj_version(void) {
58     return PACKAGE_VERSION;
59 }
60
61 opj_dinfo_t* OPJ_CALLCONV opj_create_decompress(OPJ_CODEC_FORMAT format) {
62         opj_dinfo_t *dinfo = (opj_dinfo_t*)opj_calloc(1, sizeof(opj_dinfo_t));
63         if(!dinfo) return NULL;
64         dinfo->is_decompressor = OPJ_TRUE;
65         switch(format) {
66                 case CODEC_J2K:
67                 case CODEC_JPT:
68                         /* get a J2K decoder handle */
69                         dinfo->j2k_handle = (void*)j2k_create_decompress((opj_common_ptr)dinfo);
70                         if(!dinfo->j2k_handle) {
71                                 opj_free(dinfo);
72                                 return NULL;
73                         }
74                         break;
75                 case CODEC_JP2:
76                         /* get a JP2 decoder handle */
77                         dinfo->jp2_handle = (void*)jp2_create_decompress((opj_common_ptr)dinfo);
78                         if(!dinfo->jp2_handle) {
79                                 opj_free(dinfo);
80                                 return NULL;
81                         }
82                         break;
83                 case CODEC_UNKNOWN:
84                 default:
85                         opj_free(dinfo);
86                         return NULL;
87         }
88
89         dinfo->codec_format = format;
90
91         return dinfo;
92 }
93
94 void OPJ_CALLCONV opj_destroy_decompress(opj_dinfo_t *dinfo) {
95         if(dinfo) {
96                 /* destroy the codec */
97                 switch(dinfo->codec_format) {
98                         case CODEC_J2K:
99                         case CODEC_JPT:
100                                 j2k_destroy_decompress((opj_j2k_t*)dinfo->j2k_handle);
101                                 break;
102                         case CODEC_JP2:
103                                 jp2_destroy_decompress((opj_jp2_t*)dinfo->jp2_handle);
104                                 break;
105                         case CODEC_UNKNOWN:
106                         default:
107                                 break;
108                 }
109                 /* destroy the decompressor */
110                 opj_free(dinfo);
111         }
112 }
113
114 void OPJ_CALLCONV opj_set_default_decoder_parameters(opj_dparameters_t *parameters) {
115         if(parameters) {
116                 memset(parameters, 0, sizeof(opj_dparameters_t));
117                 /* default decoding parameters */
118                 parameters->cp_layer = 0;
119                 parameters->cp_reduce = 0;
120                 parameters->cp_limit_decoding = NO_LIMITATION;
121
122                 parameters->decod_format = -1;
123                 parameters->cod_format = -1;
124 /* UniPG>> */
125 #ifdef USE_JPWL
126                 parameters->jpwl_correct = OPJ_FALSE;
127                 parameters->jpwl_exp_comps = JPWL_EXPECTED_COMPONENTS;
128                 parameters->jpwl_max_tiles = JPWL_MAXIMUM_TILES;
129 #endif /* USE_JPWL */
130 /* <<UniPG */
131         }
132 }
133
134 void OPJ_CALLCONV opj_setup_decoder(opj_dinfo_t *dinfo, opj_dparameters_t *parameters) {
135         if(dinfo && parameters) {
136                 switch(dinfo->codec_format) {
137                         case CODEC_J2K:
138                         case CODEC_JPT:
139                                 j2k_setup_decoder((opj_j2k_t*)dinfo->j2k_handle, parameters);
140                                 break;
141                         case CODEC_JP2:
142                                 jp2_setup_decoder((opj_jp2_t*)dinfo->jp2_handle, parameters);
143                                 break;
144                         case CODEC_UNKNOWN:
145                         default:
146                                 break;
147                 }
148         }
149 }
150
151 opj_image_t* OPJ_CALLCONV opj_decode(opj_dinfo_t *dinfo, opj_cio_t *cio) {
152         return opj_decode_with_info(dinfo, cio, NULL);
153 }
154
155 opj_image_t* OPJ_CALLCONV opj_decode_with_info(opj_dinfo_t *dinfo, opj_cio_t *cio, opj_codestream_info_t *cstr_info) {
156         if(dinfo && cio) {
157                 switch(dinfo->codec_format) {
158                         case CODEC_J2K:
159                                 return j2k_decode((opj_j2k_t*)dinfo->j2k_handle, cio, cstr_info);
160                         case CODEC_JPT:
161                                 return j2k_decode_jpt_stream((opj_j2k_t*)dinfo->j2k_handle, cio, cstr_info);
162                         case CODEC_JP2:
163                                 return opj_jp2_decode((opj_jp2_t*)dinfo->jp2_handle, cio, cstr_info);
164                         case CODEC_UNKNOWN:
165                         default:
166                                 break;
167                 }
168         }
169         return NULL;
170 }
171
172 opj_cinfo_t* OPJ_CALLCONV opj_create_compress(OPJ_CODEC_FORMAT format) {
173         opj_cinfo_t *cinfo = (opj_cinfo_t*)opj_calloc(1, sizeof(opj_cinfo_t));
174         if(!cinfo) return NULL;
175         cinfo->is_decompressor = OPJ_FALSE;
176         switch(format) {
177                 case CODEC_J2K:
178                         /* get a J2K coder handle */
179                         cinfo->j2k_handle = (void*)j2k_create_compress((opj_common_ptr)cinfo);
180                         if(!cinfo->j2k_handle) {
181                                 opj_free(cinfo);
182                                 return NULL;
183                         }
184                         break;
185                 case CODEC_JP2:
186                         /* get a JP2 coder handle */
187                         cinfo->jp2_handle = (void*)jp2_create_compress((opj_common_ptr)cinfo);
188                         if(!cinfo->jp2_handle) {
189                                 opj_free(cinfo);
190                                 return NULL;
191                         }
192                         break;
193                 case CODEC_JPT:
194                 case CODEC_UNKNOWN:
195                 default:
196                         opj_free(cinfo);
197                         return NULL;
198         }
199
200         cinfo->codec_format = format;
201
202         return cinfo;
203 }
204
205 void OPJ_CALLCONV opj_destroy_compress(opj_cinfo_t *cinfo) {
206         if(cinfo) {
207                 /* destroy the codec */
208                 switch(cinfo->codec_format) {
209                         case CODEC_J2K:
210                                 j2k_destroy_compress((opj_j2k_t*)cinfo->j2k_handle);
211                                 break;
212                         case CODEC_JP2:
213                                 jp2_destroy_compress((opj_jp2_t*)cinfo->jp2_handle);
214                                 break;
215                         case CODEC_JPT:
216                         case CODEC_UNKNOWN:
217                         default:
218                                 break;
219                 }
220                 /* destroy the decompressor */
221                 opj_free(cinfo);
222         }
223 }
224
225 void OPJ_CALLCONV opj_set_default_encoder_parameters(opj_cparameters_t *parameters) {
226         if(parameters) {
227                 memset(parameters, 0, sizeof(opj_cparameters_t));
228                 /* default coding parameters */
229                 parameters->cp_cinema = OFF; 
230                 parameters->max_comp_size = 0;
231                 parameters->numresolution = 6;
232                 parameters->cp_rsiz = STD_RSIZ;
233                 parameters->cblockw_init = 64;
234                 parameters->cblockh_init = 64;
235                 parameters->prog_order = LRCP;
236                 parameters->roi_compno = -1;            /* no ROI */
237                 parameters->subsampling_dx = 1;
238                 parameters->subsampling_dy = 1;
239                 parameters->tp_on = 0;
240                 parameters->decod_format = -1;
241                 parameters->cod_format = -1;
242                 parameters->tcp_rates[0] = 0;   
243                 parameters->tcp_numlayers = 0;
244     parameters->cp_disto_alloc = 0;
245                 parameters->cp_fixed_alloc = 0;
246                 parameters->cp_fixed_quality = 0;
247
248 /* UniPG>> */
249 #ifdef USE_JPWL
250                 parameters->jpwl_epc_on = OPJ_FALSE;
251                 parameters->jpwl_hprot_MH = -1; /* -1 means unassigned */
252                 {
253                         int i;
254                         for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
255                                 parameters->jpwl_hprot_TPH_tileno[i] = -1; /* unassigned */
256                                 parameters->jpwl_hprot_TPH[i] = 0; /* absent */
257                         }
258                 };
259                 {
260                         int i;
261                         for (i = 0; i < JPWL_MAX_NO_PACKSPECS; i++) {
262                                 parameters->jpwl_pprot_tileno[i] = -1; /* unassigned */
263                                 parameters->jpwl_pprot_packno[i] = -1; /* unassigned */
264                                 parameters->jpwl_pprot[i] = 0; /* absent */
265                         }
266                 };
267                 parameters->jpwl_sens_size = 0; /* 0 means no ESD */
268                 parameters->jpwl_sens_addr = 0; /* 0 means auto */
269                 parameters->jpwl_sens_range = 0; /* 0 means packet */
270                 parameters->jpwl_sens_MH = -1; /* -1 means unassigned */
271                 {
272                         int i;
273                         for (i = 0; i < JPWL_MAX_NO_TILESPECS; i++) {
274                                 parameters->jpwl_sens_TPH_tileno[i] = -1; /* unassigned */
275                                 parameters->jpwl_sens_TPH[i] = -1; /* absent */
276                         }
277                 };
278 #endif /* USE_JPWL */
279 /* <<UniPG */
280         }
281 }
282
283 void OPJ_CALLCONV opj_setup_encoder(opj_cinfo_t *cinfo, opj_cparameters_t *parameters, opj_image_t *image) {
284         if(cinfo && parameters && image) {
285                 switch(cinfo->codec_format) {
286                         case CODEC_J2K:
287                                 j2k_setup_encoder((opj_j2k_t*)cinfo->j2k_handle, parameters, image);
288                                 break;
289                         case CODEC_JP2:
290                                 jp2_setup_encoder((opj_jp2_t*)cinfo->jp2_handle, parameters, image);
291                                 break;
292                         case CODEC_JPT:
293                         case CODEC_UNKNOWN:
294                         default:
295                                 break;
296                 }
297         }
298 }
299
300 opj_bool OPJ_CALLCONV opj_encode(opj_cinfo_t *cinfo, opj_cio_t *cio, opj_image_t *image, char *index) {
301         if (index != NULL)
302                 opj_event_msg((opj_common_ptr)cinfo, EVT_WARNING, "Set index to NULL when calling the opj_encode function.\n"
303                 "To extract the index, use the opj_encode_with_info() function.\n"
304                 "No index will be generated during this encoding\n");
305         return opj_encode_with_info(cinfo, cio, image, NULL);
306 }
307
308 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) {
309         if(cinfo && cio && image) {
310                 switch(cinfo->codec_format) {
311                         case CODEC_J2K:
312                                 return j2k_encode((opj_j2k_t*)cinfo->j2k_handle, cio, image, cstr_info);
313                         case CODEC_JP2:
314                                 return opj_jp2_encode((opj_jp2_t*)cinfo->jp2_handle, cio, image, cstr_info);
315                         case CODEC_JPT:
316                         case CODEC_UNKNOWN:
317                         default:
318                                 break;
319                 }
320         }
321         return OPJ_FALSE;
322 }
323
324 void OPJ_CALLCONV opj_destroy_cstr_info(opj_codestream_info_t *cstr_info) {
325         if (cstr_info) {
326                 int tileno;
327                 for (tileno = 0; tileno < cstr_info->tw * cstr_info->th; tileno++) {
328                         opj_tile_info_t *tile_info = &cstr_info->tile[tileno];
329                         opj_free(tile_info->thresh);
330                         opj_free(tile_info->packet);
331                         opj_free(tile_info->tp);
332                 }
333                 opj_free(cstr_info->tile);
334                 opj_free(cstr_info->marker);
335                 opj_free(cstr_info->numdecompos);
336         }
337 }