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