aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/openjpeg-libsl/libsl/libsl.cpp
diff options
context:
space:
mode:
authordan miller2007-10-18 08:29:49 +0000
committerdan miller2007-10-18 08:29:49 +0000
commitcc07c64bdef46f4d25c2203e874f91ee94583f56 (patch)
tree004c68e8d8813ff1addfd9749e3dbdc6ff4432a9 /libraries/openjpeg-libsl/libsl/libsl.cpp
parenthmm... trying again to get everything committed (diff)
downloadopensim-SC-cc07c64bdef46f4d25c2203e874f91ee94583f56.zip
opensim-SC-cc07c64bdef46f4d25c2203e874f91ee94583f56.tar.gz
opensim-SC-cc07c64bdef46f4d25c2203e874f91ee94583f56.tar.bz2
opensim-SC-cc07c64bdef46f4d25c2203e874f91ee94583f56.tar.xz
one more time
Diffstat (limited to 'libraries/openjpeg-libsl/libsl/libsl.cpp')
-rw-r--r--libraries/openjpeg-libsl/libsl/libsl.cpp259
1 files changed, 259 insertions, 0 deletions
diff --git a/libraries/openjpeg-libsl/libsl/libsl.cpp b/libraries/openjpeg-libsl/libsl/libsl.cpp
new file mode 100644
index 0000000..f3cae74
--- /dev/null
+++ b/libraries/openjpeg-libsl/libsl/libsl.cpp
@@ -0,0 +1,259 @@
1// This is the main DLL file.
2
3#include "libsl.h"
4extern "C" {
5#include "../libopenjpeg/openjpeg.h"
6}
7
8#define NULL 0
9
10
11struct image_wrapper
12{
13 opj_image* image;
14
15 image_wrapper(int numcmpts, opj_image_cmptparm_t* cmptparms, OPJ_COLOR_SPACE clrspc)
16 {
17 image = opj_image_create(numcmpts, cmptparms, clrspc);
18
19 if (image == NULL)
20 throw "opj_image_create failed";
21 }
22
23 image_wrapper(opj_dinfo* dinfo, opj_cio* cio)
24 {
25 image = opj_decode(dinfo,cio);
26
27 if (image == NULL)
28 throw "opj_decode failed";
29 }
30
31 ~image_wrapper()
32 {
33 opj_image_destroy(image);
34 }
35};
36
37struct cinfo_wrapper
38{
39 opj_cinfo* cinfo;
40
41 cinfo_wrapper(CODEC_FORMAT format)
42 {
43 cinfo = opj_create_compress(format);
44
45 if (cinfo == NULL)
46 throw "opj_create_compress failed";
47 }
48
49 ~cinfo_wrapper()
50 {
51 opj_destroy_compress(cinfo);
52 }
53};
54
55struct dinfo_wrapper
56{
57 opj_dinfo* dinfo;
58
59 dinfo_wrapper(CODEC_FORMAT format)
60 {
61 dinfo = opj_create_decompress(format);
62
63 if (dinfo == NULL)
64 throw "opj_create_decompress failed";
65 }
66
67 ~dinfo_wrapper()
68 {
69 opj_destroy_decompress(dinfo);
70 }
71};
72
73struct cio_wrapper
74{
75 opj_cio* cio;
76
77 cio_wrapper(opj_cinfo* cinfo, unsigned char* buffer, int length)
78 {
79 cio = opj_cio_open((opj_common_ptr)cinfo,buffer,length);
80
81 if (cio == NULL)
82 throw "opj_cio_open failed";
83 }
84
85 cio_wrapper(opj_dinfo* dinfo, unsigned char* buffer, int length)
86 {
87 cio = opj_cio_open((opj_common_ptr)dinfo,buffer,length);
88
89 if (cio == NULL)
90 throw "opj_cio_open failed";
91 }
92
93 ~cio_wrapper()
94 {
95 opj_cio_close(cio);
96 }
97};
98
99bool LibslAllocEncoded(LibslImage* image)
100{
101 try
102 {
103 image->encoded = new unsigned char[image->length];
104 image->decoded = 0;
105 }
106
107 catch (...)
108 {
109 return false;
110 }
111
112 return true;
113}
114
115bool LibslAllocDecoded(LibslImage* image)
116{
117 try
118 {
119 image->decoded = new unsigned char[image->width * image->height * image->components];
120 image->encoded = 0;
121 }
122
123 catch (...)
124 {
125 return false;
126 }
127
128 return true;
129}
130
131void LibslFree(LibslImage* image)
132{
133 if (image->encoded != 0) delete image->encoded;
134 if (image->decoded != 0) delete image->decoded;
135}
136
137
138bool LibslEncode(LibslImage* image, bool lossless)
139{
140 try
141 {
142 opj_cparameters cparameters;
143 opj_set_default_encoder_parameters(&cparameters);
144 cparameters.cp_disto_alloc = 1;
145
146 if (lossless)
147 {
148 cparameters.tcp_numlayers = 1;
149 cparameters.tcp_rates[0] = 0;
150 }
151 else
152 {
153 cparameters.tcp_numlayers = 6;
154 cparameters.tcp_rates[0] = 1280;
155 cparameters.tcp_rates[1] = 640;
156 cparameters.tcp_rates[2] = 320;
157 cparameters.tcp_rates[3] = 160;
158 cparameters.tcp_rates[4] = 80;
159 cparameters.tcp_rates[5] = 40;
160 }
161
162 cparameters.cp_comment = "LL_RGBHM";
163
164 if (image->components > 5)
165 return false;
166
167 opj_image_comptparm comptparm[5];
168
169 for (int i = 0; i < image->components; i++)
170 {
171 comptparm[i].bpp = 8;
172 comptparm[i].prec = 8;
173 comptparm[i].sgnd = 0;
174 comptparm[i].dx = 1;
175 comptparm[i].dy = 1;
176 comptparm[i].x0 = 0;
177 comptparm[i].y0 = 0;
178 comptparm[i].w = image->width;
179 comptparm[i].h = image->height;
180 }
181
182 image_wrapper cimage(image->components,comptparm,CLRSPC_SRGB);
183 cimage.image->x0 = 0;
184 cimage.image->y0 = 0;
185 cimage.image->x1 = image->width;
186 cimage.image->y1 = image->height;
187
188 int dataIndex = 0, compIndex = 0, x, y, c;
189
190 for (y = 0; y < image->height; y++)
191 {
192 for (x = 0; x < image->width; x++)
193 {
194 for (c = 0; c < image->components; c++)
195 cimage.image->comps[c].data[compIndex] = image->decoded[dataIndex++];
196
197 compIndex++;
198 }
199 }
200
201 cinfo_wrapper cinfo(CODEC_J2K);
202 opj_setup_encoder(cinfo.cinfo,&cparameters,cimage.image);
203 cio_wrapper cio(cinfo.cinfo,NULL,0);
204
205 if (!opj_encode(cinfo.cinfo,cio.cio,cimage.image,cparameters.index))
206 return false;
207
208 image->length = cio_tell(cio.cio);
209 image->encoded = new unsigned char[image->length];
210
211 for (int i = 0; i < image->length; i++)
212 image->encoded[i] = cio.cio->buffer[i];
213
214 return true;
215 }
216
217 catch (...)
218 {
219 return false;
220 }
221}
222
223bool LibslDecode(LibslImage* image)
224{
225 opj_dparameters dparameters;
226
227 try
228 {
229 opj_set_default_decoder_parameters(&dparameters);
230 dinfo_wrapper dinfo(CODEC_J2K);
231 opj_setup_decoder(dinfo.dinfo, &dparameters);
232 cio_wrapper cio(dinfo.dinfo,image->encoded,image->length);
233 image_wrapper cimage(dinfo.dinfo, cio.cio); // decode happens here
234
235 int dataIndex = 0, compIndex = 0, x, y, c;
236 image->width = cimage.image->x1 - cimage.image->x0;
237 image->height = cimage.image->y1 - cimage.image->y0;
238 image->components = cimage.image->numcomps;
239 image->decoded = new unsigned char[image->width*image->height*image->components];
240
241 for (y = 0; y < image->height; y++)
242 {
243 for (x = 0; x < image->width; x++)
244 {
245 for (c = 0; c < image->components; c++)
246 image->decoded[dataIndex++] = cimage.image->comps[c].data[compIndex];
247
248 compIndex++;
249 }
250 }
251
252 return true;
253 }
254
255 catch (...)
256 {
257 return false;
258 }
259}