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