source: liacs/MIR2010/SourceCode/cximage/ximajas.cpp@ 188

Last change on this file since 188 was 95, checked in by Rick van der Zwet, 15 years ago

Bad boy, improper move of directory

File size: 9.0 KB
Line 
1/*
2 * File: ximajas.cpp
3 * Purpose: Platform Independent JasPer Image Class Loader and Writer
4 * 12/Apr/2003 Davide Pizzolato - www.xdp.it
5 * CxImage version 6.0.0 02/Feb/2008
6 */
7
8#include "ximajas.h"
9
10#if CXIMAGE_SUPPORT_JASPER
11
12////////////////////////////////////////////////////////////////////////////////
13#if CXIMAGE_SUPPORT_DECODE
14////////////////////////////////////////////////////////////////////////////////
15bool CxImageJAS::Decode(CxFile *hFile, DWORD imagetype)
16{
17 if (hFile == NULL) return false;
18
19 jas_image_t *image=0;
20 jas_stream_t *in=0;
21 jas_matrix_t **bufs=0;
22 long i,error=0;
23 int fmt;
24 //jas_setdbglevel(0);
25
26 cx_try
27 {
28 if (jas_init())
29 cx_throw("cannot initialize jasper");
30
31 in = jas_stream_fdopen(0, "rb");
32 if (!in)
33 cx_throw("error: cannot open standard input");
34
35 CxFileJas src(hFile,in);
36
37 fmt = jas_image_getfmt(in);
38 if (fmt<0)
39 cx_throw("error: unknowm format");
40
41 image = jas_image_decode(in, fmt, 0);
42 if (!image){
43 fmt = -1;
44 cx_throw("error: cannot load image data");
45 }
46
47 char szfmt[4];
48 *szfmt = '\0';
49 strncpy(szfmt,jas_image_fmttostr(fmt),3);
50 szfmt[3] = '\0';
51
52 fmt = -1;
53#if CXIMAGE_SUPPORT_JP2
54 if (strcmp(szfmt,"jp2")==0) fmt = CXIMAGE_FORMAT_JP2;
55#endif
56#if CXIMAGE_SUPPORT_JPC
57 if (strcmp(szfmt,"jpc")==0) fmt = CXIMAGE_FORMAT_JPC;
58#endif
59#if CXIMAGE_SUPPORT_RAS
60 if (strcmp(szfmt,"ras")==0) fmt = CXIMAGE_FORMAT_RAS;
61#endif
62#if CXIMAGE_SUPPORT_PNM
63 if (strcmp(szfmt,"pnm")==0) fmt = CXIMAGE_FORMAT_PNM;
64#endif
65#if CXIMAGE_SUPPORT_PGX
66 if (strcmp(szfmt,"pgx")==0) fmt = CXIMAGE_FORMAT_PGX;
67#endif
68
69 //if (fmt<0)
70 // cx_throw("error: unknowm format");
71
72 long x,y,w,h,depth,cmptno;
73
74 w = jas_image_cmptwidth(image,0);
75 h = jas_image_cmptheight(image,0);
76 depth = jas_image_cmptprec(image,0);
77
78 if (info.nEscape == -1){
79 head.biWidth = w;
80 head.biHeight= h;
81 info.dwType = fmt<0 ? 0 : fmt;
82 cx_throw("output dimensions returned");
83 }
84
85 if (image->numcmpts_ > 64 || image->numcmpts_ < 0)
86 cx_throw("error: too many components");
87
88 // <LD> 01/Jan/2005: Always force conversion to sRGB. Seems to be required for many types of JPEG2000 file.
89 // if (depth!=1 && depth!=4 && depth!=8)
90 if (image->numcmpts_>=3 && depth <=8)
91 {
92 jas_image_t *newimage;
93 jas_cmprof_t *outprof;
94 //jas_eprintf("forcing conversion to sRGB\n");
95 outprof = jas_cmprof_createfromclrspc(JAS_CLRSPC_SRGB);
96 if (!outprof) {
97 cx_throw("cannot create sRGB profile");
98 }
99 newimage = jas_image_chclrspc(image, outprof, JAS_CMXFORM_INTENT_PER);
100 if (!newimage) {
101 jas_cmprof_destroy(outprof); // <LD> 01/Jan/2005: Destroy color profile on error.
102 cx_throw("cannot convert to sRGB");
103 }
104 jas_image_destroy(image);
105 jas_cmprof_destroy(outprof);
106 image = newimage;
107 }
108
109 bufs = (jas_matrix_t **)calloc(image->numcmpts_, sizeof(jas_matrix_t**));
110 for (i = 0; i < image->numcmpts_; ++i) {
111 bufs[i] = jas_matrix_create(1, w);
112 if (!bufs[i]) {
113 cx_throw("error: cannot allocate memory");
114 }
115 }
116
117 int nshift = (depth>8) ? (depth-8) : 0;
118
119 if (image->numcmpts_==3 &&
120 image->cmpts_[0]->width_ == image->cmpts_[1]->width_ &&
121 image->cmpts_[1]->width_ == image->cmpts_[2]->width_ &&
122 image->cmpts_[0]->height_ == image->cmpts_[1]->height_ &&
123 image->cmpts_[1]->height_ == image->cmpts_[2]->height_ &&
124 image->cmpts_[0]->prec_ == image->cmpts_[1]->prec_ &&
125 image->cmpts_[1]->prec_ == image->cmpts_[2]->prec_ )
126 {
127
128 if(!Create(w,h,24,fmt))
129 cx_throw("");
130
131 RGBQUAD c;
132 for (y=0; y<h; y++) {
133 for (cmptno = 0; cmptno < image->numcmpts_; ++cmptno) {
134 jas_image_readcmpt(image, cmptno, 0, y, w, 1, bufs[cmptno]);
135 }
136
137 for (x=0; x<w; x++){
138 c.rgbRed = (BYTE)((jas_matrix_getv(bufs[0], x)>>nshift));
139 c.rgbGreen = (BYTE)((jas_matrix_getv(bufs[1], x)>>nshift));
140 c.rgbBlue = (BYTE)((jas_matrix_getv(bufs[2], x)>>nshift));
141 SetPixelColor(x,h-1-y,c);
142 }
143 }
144 } else {
145 info.nNumFrames = image->numcmpts_;
146 if ((info.nFrame<0)||(info.nFrame>=info.nNumFrames)){
147 cx_throw("wrong frame!");
148 }
149 for (cmptno=0; cmptno<=info.nFrame; cmptno++) {
150 w = jas_image_cmptwidth(image,cmptno);
151 h = jas_image_cmptheight(image,cmptno);
152 depth = jas_image_cmptprec(image,cmptno);
153 if (depth>8) depth=8;
154 if(!Create(w,h,depth,imagetype))
155 cx_throw("");
156 SetGrayPalette();
157 for (y=0; y<h; y++) {
158 jas_image_readcmpt(image, cmptno, 0, y, w, 1, bufs[0]);
159 for (x=0; x<w; x++){
160 SetPixelIndex(x,h-1-y,(BYTE)((jas_matrix_getv(bufs[0], x)>>nshift)));
161 }
162 }
163 }
164 }
165
166
167 } cx_catch {
168 if (strcmp(message,"")) strncpy(info.szLastError,message,255);
169 if (info.nEscape == -1 && fmt>0){
170 error = 0;
171 } else {
172 error = 1;
173 }
174 }
175
176 if (bufs) {
177 for (i = 0; i < image->numcmpts_; ++i){ if (bufs[i]) jas_matrix_destroy(bufs[i]);}
178 free(bufs);
179 }
180 jas_cleanup();
181 if (image) jas_image_destroy(image);
182 if (in) jas_stream_close(in);
183 return (error==0);
184}
185////////////////////////////////////////////////////////////////////////////////
186#endif //CXIMAGE_SUPPORT_DECODE
187////////////////////////////////////////////////////////////////////////////////
188#if CXIMAGE_SUPPORT_ENCODE
189////////////////////////////////////////////////////////////////////////////////
190bool CxImageJAS::Encode(CxFile * hFile, DWORD imagetype)
191{
192 if (EncodeSafeCheck(hFile)) return false;
193
194 if (head.biClrUsed!=0 && !IsGrayScale()){
195 strcpy(info.szLastError,"JasPer can save only RGB or GrayScale images");
196 return false;
197 }
198
199 jas_image_t *image=0;
200 jas_stream_t *out=0;
201 jas_matrix_t *cmpts[3];
202 long x,y,yflip,error=0;
203 uint_fast16_t cmptno, numcmpts=0;
204 jas_image_cmptparm_t cmptparms[3], *cmptparm;
205
206 cx_try {
207
208 if (jas_init())
209 cx_throw("cannot initialize jasper");
210
211 out = jas_stream_fdopen(0, "wb");
212 if (!out)
213 cx_throw("error: cannot open standard output");
214
215 CxFileJas src(hFile,out);
216
217 numcmpts = head.biClrUsed==0 ? 3 : 1;
218
219 for (cmptno = 0, cmptparm = cmptparms; cmptno < numcmpts; ++cmptno, ++cmptparm) {
220 cmptparm->tlx = 0;
221 cmptparm->tly = 0;
222 cmptparm->hstep = 1;
223 cmptparm->vstep = 1;
224 cmptparm->width = head.biWidth;
225 cmptparm->height = head.biHeight;
226 cmptparm->prec = 8;
227 cmptparm->sgnd = false;
228 }
229
230 /* Create image object. */
231 image = jas_image_create(numcmpts, cmptparms, JAS_CLRSPC_UNKNOWN);
232 if (!image)
233 cx_throw("error : jas_image_create");
234
235 if (numcmpts == 3) {
236 jas_image_setclrspc(image, JAS_CLRSPC_SRGB);
237 jas_image_setcmpttype(image, 0,
238 JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_R));
239 jas_image_setcmpttype(image, 1,
240 JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_G));
241 jas_image_setcmpttype(image, 2,
242 JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_B));
243 } else {
244 jas_image_setclrspc(image, JAS_CLRSPC_SGRAY);
245 jas_image_setcmpttype(image, 0,
246 JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_GRAY_Y));
247 }
248
249
250 for (x = 0; x < numcmpts; ++x) { cmpts[x] = 0; }
251 /* Create temporary matrices to hold component data. */
252 for (x = 0; x < numcmpts; ++x) {
253 cmpts[x] = jas_matrix_create(1, head.biWidth);
254 if (!cmpts[x]) {
255 cx_throw("error : can't allocate memory");
256 }
257 }
258
259 RGBQUAD c;
260 for (y = 0; y < head.biHeight; ++y) {
261 for (x = 0; x < head.biWidth; ++x) {
262 if (head.biClrUsed==0){
263 c = GetPixelColor(x,y);
264 jas_matrix_setv(cmpts[0], x, c.rgbRed);
265 jas_matrix_setv(cmpts[1], x, c.rgbGreen);
266 jas_matrix_setv(cmpts[2], x, c.rgbBlue);
267 } else {
268 jas_matrix_setv(cmpts[0], x, GetPixelIndex(x,y));
269 }
270 }
271 yflip = head.biHeight - 1 - y;
272 for (cmptno = 0; cmptno < numcmpts; ++cmptno) {
273 if (jas_image_writecmpt(image, cmptno, 0, yflip, head.biWidth, 1, cmpts[cmptno])) {
274 cx_throw("error : jas_image_writecmpt");
275 }
276 }
277 }
278
279 char szfmt[4];
280 *szfmt = '\0';
281#if CXIMAGE_SUPPORT_JP2
282 if (imagetype == CXIMAGE_FORMAT_JP2) strcpy(szfmt,"jp2");
283#endif
284#if CXIMAGE_SUPPORT_JPC
285 if (imagetype == CXIMAGE_FORMAT_JPC) strcpy(szfmt,"jpc");
286#endif
287#if CXIMAGE_SUPPORT_RAS
288 if (imagetype == CXIMAGE_FORMAT_RAS) strcpy(szfmt,"ras");
289#endif
290#if CXIMAGE_SUPPORT_PNM
291 if (imagetype == CXIMAGE_FORMAT_PNM) strcpy(szfmt,"pnm");
292#endif
293#if CXIMAGE_SUPPORT_PGX
294 if (imagetype == CXIMAGE_FORMAT_PGX){
295 strcpy(szfmt,"pgx");
296 if (head.biClrUsed==0) cx_throw("PGX can save only GrayScale images");
297 }
298#endif
299 int outfmt = jas_image_strtofmt(szfmt);
300
301 char szoutopts[32];
302 sprintf(szoutopts,"rate=%.3f", info.fQuality/100.0f);
303
304 if (jas_image_encode(image, out, outfmt, szoutopts)) {
305 cx_throw("error: cannot encode image");
306 }
307 jas_stream_flush(out);
308
309 } cx_catch {
310 if (strcmp(message,"")) strncpy(info.szLastError,message,255);
311 error = 1;
312 }
313
314 for (x = 0; x < numcmpts; ++x) { if (cmpts[x]) { jas_matrix_destroy(cmpts[x]); } }
315 jas_cleanup();
316 if (image) jas_image_destroy(image);
317 if (out) jas_stream_close(out);
318
319 return (error==0);
320}
321////////////////////////////////////////////////////////////////////////////////
322#endif // CXIMAGE_SUPPORT_ENCODE
323////////////////////////////////////////////////////////////////////////////////
324#endif // CXIMAGE_SUPPORT_JASPER
325
Note: See TracBrowser for help on using the repository browser.