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