ENH: Do the proper thing for static/shared
[openjpeg.git] / 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() {
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
120                 parameters->decod_format = -1;
121                 parameters->cod_format = -1;
122         }
123 }
124
125 void OPJ_CALLCONV opj_setup_decoder(opj_dinfo_t *dinfo, opj_dparameters_t *parameters) {
126         if(dinfo && parameters) {
127                 switch(dinfo->codec_format) {
128                         case CODEC_J2K:
129                         case CODEC_JPT:
130                                 j2k_setup_decoder((opj_j2k_t*)dinfo->j2k_handle, parameters);
131                                 break;
132                         case CODEC_JP2:
133                                 jp2_setup_decoder((opj_jp2_t*)dinfo->jp2_handle, parameters);
134                                 break;
135                         case CODEC_UNKNOWN:
136                         default:
137                                 break;
138                 }
139         }
140 }
141
142 opj_image_t* OPJ_CALLCONV opj_decode(opj_dinfo_t *dinfo, opj_cio_t *cio) {
143         if(dinfo && cio) {
144                 switch(dinfo->codec_format) {
145                         case CODEC_J2K:
146                                 return j2k_decode((opj_j2k_t*)dinfo->j2k_handle, cio);
147                         case CODEC_JPT:
148                                 return j2k_decode_jpt_stream((opj_j2k_t*)dinfo->j2k_handle, cio);
149                         case CODEC_JP2:
150                                 return jp2_decode((opj_jp2_t*)dinfo->jp2_handle, cio);
151                         case CODEC_UNKNOWN:
152                         default:
153                                 break;
154                 }
155         }
156
157         return NULL;
158 }
159
160 opj_cinfo_t* OPJ_CALLCONV opj_create_compress(OPJ_CODEC_FORMAT format) {
161         opj_cinfo_t *cinfo = (opj_cinfo_t*)opj_malloc(sizeof(opj_cinfo_t));
162         if(!cinfo) return NULL;
163         cinfo->is_decompressor = false;
164         switch(format) {
165                 case CODEC_J2K:
166                         /* get a J2K coder handle */
167                         cinfo->j2k_handle = (void*)j2k_create_compress((opj_common_ptr)cinfo);
168                         if(!cinfo->j2k_handle) {
169                                 opj_free(cinfo);
170                                 return NULL;
171                         }
172                         break;
173                 case CODEC_JP2:
174                         /* get a JP2 coder handle */
175                         cinfo->jp2_handle = (void*)jp2_create_compress((opj_common_ptr)cinfo);
176                         if(!cinfo->jp2_handle) {
177                                 opj_free(cinfo);
178                                 return NULL;
179                         }
180                         break;
181                 case CODEC_JPT:
182                 case CODEC_UNKNOWN:
183                 default:
184                         opj_free(cinfo);
185                         return NULL;
186         }
187
188         cinfo->codec_format = format;
189
190         return cinfo;
191 }
192
193 void OPJ_CALLCONV opj_destroy_compress(opj_cinfo_t *cinfo) {
194         if(cinfo) {
195                 /* destroy the codec */
196                 switch(cinfo->codec_format) {
197                         case CODEC_J2K:
198                                 j2k_destroy_decompress((opj_j2k_t*)cinfo->j2k_handle);
199                                 break;
200                         case CODEC_JP2:
201                                 jp2_destroy_decompress((opj_jp2_t*)cinfo->jp2_handle);
202                                 break;
203                         case CODEC_JPT:
204                         case CODEC_UNKNOWN:
205                         default:
206                                 break;
207                 }
208                 /* destroy the decompressor */
209                 opj_free(cinfo);
210         }
211 }
212
213 void OPJ_CALLCONV opj_set_default_encoder_parameters(opj_cparameters_t *parameters) {
214         if(parameters) {
215                 memset(parameters, 0, sizeof(opj_cparameters_t));
216                 /* default coding parameters */
217                 parameters->numresolution = 6;
218                 parameters->cblockw_init = 64;
219                 parameters->cblockh_init = 64;
220                 parameters->prog_order = LRCP;
221                 parameters->roi_compno = -1;            /* no ROI */
222                 parameters->subsampling_dx = 1;
223                 parameters->subsampling_dy = 1;
224
225                 parameters->decod_format = -1;
226                 parameters->cod_format = -1;
227         }
228 }
229
230 void OPJ_CALLCONV opj_setup_encoder(opj_cinfo_t *cinfo, opj_cparameters_t *parameters, opj_image_t *image) {
231         if(cinfo && parameters && image) {
232                 switch(cinfo->codec_format) {
233                         case CODEC_J2K:
234                                 j2k_setup_encoder((opj_j2k_t*)cinfo->j2k_handle, parameters, image);
235                                 break;
236                         case CODEC_JP2:
237                                 jp2_setup_encoder((opj_jp2_t*)cinfo->jp2_handle, parameters, image);
238                                 break;
239                         case CODEC_JPT:
240                         case CODEC_UNKNOWN:
241                         default:
242                                 break;
243                 }
244         }
245 }
246
247 bool OPJ_CALLCONV opj_encode(opj_cinfo_t *cinfo, opj_cio_t *cio, opj_image_t *image, char *index) {
248         if(cinfo && cio && image) {
249                 switch(cinfo->codec_format) {
250                         case CODEC_J2K:
251                                 return j2k_encode((opj_j2k_t*)cinfo->j2k_handle, cio, image, index);
252                         case CODEC_JP2:
253                                 return jp2_encode((opj_jp2_t*)cinfo->jp2_handle, cio, image, index);
254                         case CODEC_JPT:
255                         case CODEC_UNKNOWN:
256                         default:
257                                 break;
258                 }
259         }
260
261         return false;
262 }
263
264