[2.0] Backport all changes since r2798 (included) from trunk
[openjpeg.git] / src / lib / openjp3d / openjp3d.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  * Copyright (c) 2006, M�nica D�ez Garc�a, Image Processing Laboratory, University of Valladolid, Spain
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #ifdef _WIN32
34 #include <windows.h>
35 #endif /* _WIN32 */
36
37 #include "opj_includes.h"
38 #include "openjp3d.h"
39 #define JP3D_VERSION "1.3.0"
40 /* ---------------------------------------------------------------------- */
41 #ifdef _WIN32
42 #ifndef OPJ_STATIC
43 BOOL APIENTRY
44 DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved) {
45         switch (ul_reason_for_call) {
46                 case DLL_PROCESS_ATTACH :
47                         break;
48                 case DLL_PROCESS_DETACH :
49                         break;
50                 case DLL_THREAD_ATTACH :
51                 case DLL_THREAD_DETACH :
52                         break;
53     }
54
55     return TRUE;
56 }
57 #endif /* OPJ_STATIC */
58 #endif /* _WIN32 */
59
60 /* ---------------------------------------------------------------------- */
61
62 const char* OPJ_CALLCONV opj_version() {
63     return JP3D_VERSION;
64 }
65 opj_dinfo_t* OPJ_CALLCONV opj_create_decompress(OPJ_CODEC_FORMAT format) {
66         opj_dinfo_t *dinfo = (opj_dinfo_t*)opj_malloc(sizeof(opj_dinfo_t));
67         if(!dinfo) return NULL;
68         dinfo->is_decompressor = true;
69         switch(format) {
70                 case CODEC_J3D:
71                 case CODEC_J2K:
72                         /* get a J3D decoder handle */
73                         dinfo->j3d_handle = (void*)j3d_create_decompress((opj_common_ptr)dinfo);
74                         if(!dinfo->j3d_handle) {
75                                 opj_free(dinfo);
76                                 return NULL;
77                         }
78                         break;
79                 default:
80                         opj_free(dinfo);
81                         return NULL;
82         }
83
84         dinfo->codec_format = format;
85
86         return dinfo;
87 }
88
89 void OPJ_CALLCONV opj_destroy_decompress(opj_dinfo_t *dinfo) {
90         if(dinfo) {
91                 /* destroy the codec */
92                 if(dinfo->codec_format != CODEC_UNKNOWN) {
93                         j3d_destroy_decompress((opj_j3d_t*)dinfo->j3d_handle);
94                 }
95                 /* destroy the decompressor */
96                 opj_free(dinfo);
97         }
98 }
99
100 void OPJ_CALLCONV opj_set_default_decoder_parameters(opj_dparameters_t *parameters) {
101         if(parameters) {
102                 memset(parameters, 0, sizeof(opj_dparameters_t));
103                 /* default decoding parameters */
104                 parameters->cp_layer = 0;
105                 parameters->cp_reduce[0] = 0;
106                 parameters->cp_reduce[1] = 0;
107                 parameters->cp_reduce[2] = 0;
108                 parameters->bigendian = 0;
109
110                 parameters->decod_format = -1;
111                 parameters->cod_format = -1;
112         }
113 }
114
115 void OPJ_CALLCONV opj_setup_decoder(opj_dinfo_t *dinfo, opj_dparameters_t *parameters) {
116         if(dinfo && parameters) {
117                 if (dinfo->codec_format != CODEC_UNKNOWN) {
118                         j3d_setup_decoder((opj_j3d_t*)dinfo->j3d_handle, parameters);
119                 }
120         }
121 }
122
123 opj_volume_t* OPJ_CALLCONV opj_decode(opj_dinfo_t *dinfo, opj_cio_t *cio) {
124         if(dinfo && cio) {
125                 if (dinfo->codec_format != CODEC_UNKNOWN) {
126                         return j3d_decode((opj_j3d_t*)dinfo->j3d_handle, cio);
127                 }
128         }
129
130         return NULL;
131 }
132
133 opj_cinfo_t* OPJ_CALLCONV opj_create_compress(OPJ_CODEC_FORMAT format) {
134         opj_cinfo_t *cinfo = (opj_cinfo_t*)opj_malloc(sizeof(opj_cinfo_t));
135         if(!cinfo) return NULL;
136         cinfo->is_decompressor = false;
137         switch(format) {
138                 case CODEC_J3D:
139                 case CODEC_J2K:
140                         /* get a J3D coder handle */
141                         cinfo->j3d_handle = (void*)j3d_create_compress((opj_common_ptr)cinfo);
142                         if(!cinfo->j3d_handle) {
143                                 opj_free(cinfo);
144                                 return NULL;
145                         }
146                         break;
147                 default:
148                         opj_free(cinfo);
149                         return NULL;
150         }
151
152         cinfo->codec_format = format;
153
154         return cinfo;
155 }
156
157 void OPJ_CALLCONV opj_destroy_compress(opj_cinfo_t *cinfo) {
158         if(cinfo) {
159                 /* destroy the codec */
160                 if (cinfo->codec_format != CODEC_UNKNOWN) {
161                                 j3d_destroy_compress((opj_j3d_t*)cinfo->j3d_handle);
162                 }
163                 /* destroy the decompressor */
164                 opj_free(cinfo);
165         }
166 }
167
168 void OPJ_CALLCONV opj_set_default_encoder_parameters(opj_cparameters_t *parameters) {
169         if(parameters) {
170                 memset(parameters, 0, sizeof(opj_cparameters_t));
171                 /* default coding parameters */
172                 parameters->numresolution[0] = 3;
173                 parameters->numresolution[1] = 3;
174                 parameters->numresolution[2] = 1;
175                 parameters->cblock_init[0] = 64;
176                 parameters->cblock_init[1] = 64;
177                 parameters->cblock_init[2] = 64;
178                 parameters->prog_order = LRCP;
179                 parameters->roi_compno = -1;            /* no ROI */
180                 parameters->atk_wt[0] = 1;                              /* 5-3 WT */
181                 parameters->atk_wt[1] = 1;                              /* 5-3 WT */
182                 parameters->atk_wt[2] = 1;                              /* 5-3 WT */
183                 parameters->irreversible = 0;
184                 parameters->subsampling_dx = 1;
185                 parameters->subsampling_dy = 1;
186                 parameters->subsampling_dz = 1;
187
188                 parameters->decod_format = -1;
189                 parameters->cod_format = -1;
190                 parameters->encoding_format = ENCOD_2EB;
191                 parameters->transform_format = TRF_2D_DWT;
192         }
193 }
194
195 void OPJ_CALLCONV opj_setup_encoder(opj_cinfo_t *cinfo, opj_cparameters_t *parameters, opj_volume_t *volume) {
196         if(cinfo && parameters && volume) {
197                 if (cinfo->codec_format != CODEC_UNKNOWN) {
198                         j3d_setup_encoder((opj_j3d_t*)cinfo->j3d_handle, parameters, volume);
199                 }
200         }
201 }
202
203 bool OPJ_CALLCONV opj_encode(opj_cinfo_t *cinfo, opj_cio_t *cio, opj_volume_t *volume, char *index) {
204         if(cinfo && cio && volume) {
205                 if (cinfo->codec_format != CODEC_UNKNOWN) {
206                         return j3d_encode((opj_j3d_t*)cinfo->j3d_handle, cio, volume, index);
207                 }
208         }
209
210         return false;
211 }
212
213