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