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