STYLE: Remove completely reference to OPJ_EXPORT
[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 #include "opj_includes.h"
28
29 const char * opj_version() {
30     return OPENJPEG_VERSION;
31 }
32
33 opj_dinfo_t* opj_create_decompress(OPJ_CODEC_FORMAT format) {
34         opj_dinfo_t *dinfo = (opj_dinfo_t*)opj_malloc(sizeof(opj_dinfo_t));
35         if(!dinfo) return NULL;
36         dinfo->is_decompressor = true;
37         switch(format) {
38                 case CODEC_J2K:
39                 case CODEC_JPT:
40                         /* get a J2K decoder handle */
41                         dinfo->j2k_handle = (void*)j2k_create_decompress((opj_common_ptr)dinfo);
42                         if(!dinfo->j2k_handle) {
43                                 opj_free(dinfo);
44                                 return NULL;
45                         }
46                         break;
47                 case CODEC_JP2:
48                         /* get a JP2 decoder handle */
49                         dinfo->jp2_handle = (void*)jp2_create_decompress((opj_common_ptr)dinfo);
50                         if(!dinfo->jp2_handle) {
51                                 opj_free(dinfo);
52                                 return NULL;
53                         }
54                         break;
55                 case CODEC_UNKNOWN:
56                 default:
57                         opj_free(dinfo);
58                         return NULL;
59         }
60
61         dinfo->codec_format = format;
62
63         return dinfo;
64 }
65
66 void opj_destroy_decompress(opj_dinfo_t *dinfo) {
67         if(dinfo) {
68                 /* destroy the codec */
69                 switch(dinfo->codec_format) {
70                         case CODEC_J2K:
71                         case CODEC_JPT:
72                                 j2k_destroy_decompress((opj_j2k_t*)dinfo->j2k_handle);
73                                 break;
74                         case CODEC_JP2:
75                                 jp2_destroy_decompress((opj_jp2_t*)dinfo->jp2_handle);
76                                 break;
77                         case CODEC_UNKNOWN:
78                         default:
79                                 break;
80                 }
81                 /* destroy the decompressor */
82                 opj_free(dinfo);
83         }
84 }
85
86 void opj_set_default_decoder_parameters(opj_dparameters_t *parameters) {
87         if(parameters) {
88                 memset(parameters, 0, sizeof(opj_dparameters_t));
89                 /* default decoding parameters */
90                 parameters->cp_layer = 0;
91                 parameters->cp_reduce = 0;
92
93                 parameters->decod_format = -1;
94                 parameters->cod_format = -1;
95         }
96 }
97
98 void opj_setup_decoder(opj_dinfo_t *dinfo, opj_dparameters_t *parameters) {
99         if(dinfo && parameters) {
100                 switch(dinfo->codec_format) {
101                         case CODEC_J2K:
102                         case CODEC_JPT:
103                                 j2k_setup_decoder((opj_j2k_t*)dinfo->j2k_handle, parameters);
104                                 break;
105                         case CODEC_JP2:
106                                 jp2_setup_decoder((opj_jp2_t*)dinfo->jp2_handle, parameters);
107                                 break;
108                         case CODEC_UNKNOWN:
109                         default:
110                                 break;
111                 }
112         }
113 }
114
115 opj_image_t* opj_decode(opj_dinfo_t *dinfo, opj_cio_t *cio) {
116         if(dinfo && cio) {
117                 switch(dinfo->codec_format) {
118                         case CODEC_J2K:
119                                 return j2k_decode((opj_j2k_t*)dinfo->j2k_handle, cio);
120                         case CODEC_JPT:
121                                 return j2k_decode_jpt_stream((opj_j2k_t*)dinfo->j2k_handle, cio);
122                         case CODEC_JP2:
123                                 return jp2_decode((opj_jp2_t*)dinfo->jp2_handle, cio);
124                 }
125         }
126
127         return NULL;
128 }
129
130 opj_cinfo_t* opj_create_compress(OPJ_CODEC_FORMAT format) {
131         opj_cinfo_t *cinfo = (opj_cinfo_t*)opj_malloc(sizeof(opj_cinfo_t));
132         if(!cinfo) return NULL;
133         cinfo->is_decompressor = false;
134         switch(format) {
135                 case CODEC_J2K:
136                         /* get a J2K coder handle */
137                         cinfo->j2k_handle = (void*)j2k_create_compress((opj_common_ptr)cinfo);
138                         if(!cinfo->j2k_handle) {
139                                 opj_free(cinfo);
140                                 return NULL;
141                         }
142                         break;
143                 case CODEC_JP2:
144                         /* get a JP2 coder handle */
145                         cinfo->jp2_handle = (void*)jp2_create_compress((opj_common_ptr)cinfo);
146                         if(!cinfo->jp2_handle) {
147                                 opj_free(cinfo);
148                                 return NULL;
149                         }
150                         break;
151                 case CODEC_JPT:
152                 case CODEC_UNKNOWN:
153                 default:
154                         opj_free(cinfo);
155                         return NULL;
156         }
157
158         cinfo->codec_format = format;
159
160         return cinfo;
161 }
162
163 void opj_destroy_compress(opj_cinfo_t *cinfo) {
164         if(cinfo) {
165                 /* destroy the codec */
166                 switch(cinfo->codec_format) {
167                         case CODEC_J2K:
168                                 j2k_destroy_decompress((opj_j2k_t*)cinfo->j2k_handle);
169                                 break;
170                         case CODEC_JP2:
171                                 jp2_destroy_decompress((opj_jp2_t*)cinfo->jp2_handle);
172                                 break;
173                         case CODEC_JPT:
174                         case CODEC_UNKNOWN:
175                         default:
176                                 break;
177                 }
178                 /* destroy the decompressor */
179                 opj_free(cinfo);
180         }
181 }
182
183 void opj_set_default_encoder_parameters(opj_cparameters_t *parameters) {
184         if(parameters) {
185                 memset(parameters, 0, sizeof(opj_cparameters_t));
186                 /* default coding parameters */
187                 parameters->numresolution = 6;
188                 parameters->cblockw_init = 64;
189                 parameters->cblockh_init = 64;
190                 parameters->prog_order = LRCP;
191                 parameters->roi_compno = -1;            /* no ROI */
192                 parameters->subsampling_dx = 1;
193                 parameters->subsampling_dy = 1;
194
195                 parameters->decod_format = -1;
196                 parameters->cod_format = -1;
197         }
198 }
199
200 void opj_setup_encoder(opj_cinfo_t *cinfo, opj_cparameters_t *parameters, opj_image_t *image) {
201         if(cinfo && parameters && image) {
202                 switch(cinfo->codec_format) {
203                         case CODEC_J2K:
204                                 j2k_setup_encoder((opj_j2k_t*)cinfo->j2k_handle, parameters, image);
205                                 break;
206                         case CODEC_JP2:
207                                 jp2_setup_encoder((opj_jp2_t*)cinfo->jp2_handle, parameters, image);
208                                 break;
209                         case CODEC_JPT:
210                         case CODEC_UNKNOWN:
211                         default:
212                                 break;
213                 }
214         }
215 }
216
217 bool opj_encode(opj_cinfo_t *cinfo, opj_cio_t *cio, opj_image_t *image, char *index) {
218         if(cinfo && cio && image) {
219                 switch(cinfo->codec_format) {
220                         case CODEC_J2K:
221                                 return j2k_encode((opj_j2k_t*)cinfo->j2k_handle, cio, image, index);
222                         case CODEC_JP2:
223                                 return jp2_encode((opj_jp2_t*)cinfo->jp2_handle, cio, image, index);
224                         case CODEC_JPT:
225                         case CODEC_UNKNOWN:
226                         default:
227                                 break;
228                 }
229         }
230
231         return false;
232 }
233
234