]> Creatis software - gdcm.git/blob - src/gdcmopenjpeg/libopenjpeg/openjpeg.c
COMP: Why do people use microsoft extension...
[gdcm.git] / src / gdcmopenjpeg / 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     default:
56       opj_free(dinfo);
57       return NULL;
58   }
59
60   dinfo->codec_format = format;
61
62   return dinfo;
63 }
64
65 void opj_destroy_decompress(opj_dinfo_t *dinfo) {
66   if(dinfo) {
67     /* destroy the codec */
68     switch(dinfo->codec_format) {
69       case CODEC_J2K:
70       case CODEC_JPT:
71         j2k_destroy_decompress((opj_j2k_t*)dinfo->j2k_handle);
72         break;
73       case CODEC_JP2:
74         jp2_destroy_decompress((opj_jp2_t*)dinfo->jp2_handle);
75         break;
76     }
77     /* destroy the decompressor */
78     opj_free(dinfo);
79   }
80 }
81
82 void opj_set_default_decoder_parameters(opj_dparameters_t *parameters) {
83   if(parameters) {
84     memset(parameters, 0, sizeof(opj_dparameters_t));
85     /* default decoding parameters */
86     parameters->cp_layer = 0;
87     parameters->cp_reduce = 0;
88
89     parameters->decod_format = -1;
90     parameters->cod_format = -1;
91   }
92 }
93
94 void opj_setup_decoder(opj_dinfo_t *dinfo, opj_dparameters_t *parameters) {
95   if(dinfo && parameters) {
96     switch(dinfo->codec_format) {
97       case CODEC_J2K:
98       case CODEC_JPT:
99         j2k_setup_decoder((opj_j2k_t*)dinfo->j2k_handle, parameters);
100         break;
101       case CODEC_JP2:
102         jp2_setup_decoder((opj_jp2_t*)dinfo->jp2_handle, parameters);
103         break;
104     }
105   }
106 }
107
108 opj_image_t* opj_decode(opj_dinfo_t *dinfo, opj_cio_t *cio) {
109   if(dinfo && cio) {
110     switch(dinfo->codec_format) {
111       case CODEC_J2K:
112         return j2k_decode((opj_j2k_t*)dinfo->j2k_handle, cio);
113       case CODEC_JPT:
114         return j2k_decode_jpt_stream((opj_j2k_t*)dinfo->j2k_handle, cio);
115       case CODEC_JP2:
116         return jp2_decode((opj_jp2_t*)dinfo->jp2_handle, cio);
117     }
118   }
119
120   return NULL;
121 }
122
123 opj_cinfo_t* opj_create_compress(OPJ_CODEC_FORMAT format) {
124   opj_cinfo_t *cinfo = (opj_cinfo_t*)opj_malloc(sizeof(opj_cinfo_t));
125   if(!cinfo) return NULL;
126   cinfo->is_decompressor = false;
127   switch(format) {
128     case CODEC_J2K:
129       /* get a J2K coder handle */
130       cinfo->j2k_handle = (void*)j2k_create_compress((opj_common_ptr)cinfo);
131       if(!cinfo->j2k_handle) {
132         opj_free(cinfo);
133         return NULL;
134       }
135       break;
136     case CODEC_JP2:
137       /* get a JP2 coder handle */
138       cinfo->jp2_handle = (void*)jp2_create_compress((opj_common_ptr)cinfo);
139       if(!cinfo->jp2_handle) {
140         opj_free(cinfo);
141         return NULL;
142       }
143       break;
144     default:
145       opj_free(cinfo);
146       return NULL;
147   }
148
149   cinfo->codec_format = format;
150
151   return cinfo;
152 }
153
154 void opj_destroy_compress(opj_cinfo_t *cinfo) {
155   if(cinfo) {
156     /* destroy the codec */
157     switch(cinfo->codec_format) {
158       case CODEC_J2K:
159         j2k_destroy_decompress((opj_j2k_t*)cinfo->j2k_handle);
160         break;
161       case CODEC_JP2:
162         jp2_destroy_decompress((opj_jp2_t*)cinfo->jp2_handle);
163         break;
164     }
165     /* destroy the decompressor */
166     opj_free(cinfo);
167   }
168 }
169
170 void opj_set_default_encoder_parameters(opj_cparameters_t *parameters) {
171   if(parameters) {
172     memset(parameters, 0, sizeof(opj_cparameters_t));
173     /* default coding parameters */
174     parameters->numresolution = 6;
175     parameters->cblockw_init = 64;
176     parameters->cblockh_init = 64;
177     parameters->prog_order = LRCP;
178     parameters->roi_compno = -1;    /* no ROI */
179     parameters->subsampling_dx = 1;
180     parameters->subsampling_dy = 1;
181
182     parameters->decod_format = -1;
183     parameters->cod_format = -1;
184   }
185 }
186
187 void opj_setup_encoder(opj_cinfo_t *cinfo, opj_cparameters_t *parameters, opj_image_t *image) {
188   if(cinfo && parameters && image) {
189     switch(cinfo->codec_format) {
190       case CODEC_J2K:
191         j2k_setup_encoder((opj_j2k_t*)cinfo->j2k_handle, parameters, image);
192         break;
193       case CODEC_JP2:
194         jp2_setup_encoder((opj_jp2_t*)cinfo->jp2_handle, parameters, image);
195         break;
196     }
197   }
198 }
199
200 bool opj_encode(opj_cinfo_t *cinfo, opj_cio_t *cio, opj_image_t *image, char *index) {
201   if(cinfo && cio && image) {
202     switch(cinfo->codec_format) {
203       case CODEC_J2K:
204         return j2k_encode((opj_j2k_t*)cinfo->j2k_handle, cio, image, index);
205       case CODEC_JP2:
206         return jp2_encode((opj_jp2_t*)cinfo->jp2_handle, cio, image, index);
207     }
208   }
209
210   return false;
211 }
212
213