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
10 #if CXIMAGE_SUPPORT_JASPER
12 ////////////////////////////////////////////////////////////////////////////////
13 #if CXIMAGE_SUPPORT_DECODE
14 ////////////////////////////////////////////////////////////////////////////////
15 bool CxImageJAS::Decode(CxFile *hFile, DWORD imagetype)
17 if (hFile == NULL) return false;
21 jas_matrix_t **bufs=0;
29 cx_throw("cannot initialize jasper");
31 in = jas_stream_fdopen(0, "rb");
33 cx_throw("error: cannot open standard input");
35 CxFileJas src(hFile,in);
37 fmt = jas_image_getfmt(in);
39 cx_throw("error: unknowm format");
41 image = jas_image_decode(in, fmt, 0);
44 cx_throw("error: cannot load image data");
49 strncpy(szfmt,jas_image_fmttostr(fmt),3);
53 #if CXIMAGE_SUPPORT_JP2
54 if (strcmp(szfmt,"jp2")==0) fmt = CXIMAGE_FORMAT_JP2;
56 #if CXIMAGE_SUPPORT_JPC
57 if (strcmp(szfmt,"jpc")==0) fmt = CXIMAGE_FORMAT_JPC;
59 #if CXIMAGE_SUPPORT_RAS
60 if (strcmp(szfmt,"ras")==0) fmt = CXIMAGE_FORMAT_RAS;
62 #if CXIMAGE_SUPPORT_PNM
63 if (strcmp(szfmt,"pnm")==0) fmt = CXIMAGE_FORMAT_PNM;
65 #if CXIMAGE_SUPPORT_PGX
66 if (strcmp(szfmt,"pgx")==0) fmt = CXIMAGE_FORMAT_PGX;
70 // cx_throw("error: unknowm format");
72 long x,y,w,h,depth,cmptno;
74 w = jas_image_cmptwidth(image,0);
75 h = jas_image_cmptheight(image,0);
76 depth = jas_image_cmptprec(image,0);
78 if (info.nEscape == -1){
81 info.dwType = fmt<0 ? 0 : fmt;
82 cx_throw("output dimensions returned");
85 if (image->numcmpts_ > 64 || image->numcmpts_ < 0)
86 cx_throw("error: too many components");
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)
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);
97 cx_throw("cannot create sRGB profile");
99 newimage = jas_image_chclrspc(image, outprof, JAS_CMXFORM_INTENT_PER);
101 jas_cmprof_destroy(outprof); // <LD> 01/Jan/2005: Destroy color profile on error.
102 cx_throw("cannot convert to sRGB");
104 jas_image_destroy(image);
105 jas_cmprof_destroy(outprof);
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);
113 cx_throw("error: cannot allocate memory");
117 int nshift = (depth>8) ? (depth-8) : 0;
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_ )
128 if(!Create(w,h,24,fmt))
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]);
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);
145 info.nNumFrames = image->numcmpts_;
146 if ((info.nFrame<0)||(info.nFrame>=info.nNumFrames)){
147 cx_throw("wrong frame!");
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))
157 for (y=0; y<h; y++) {
158 jas_image_readcmpt(image, cmptno, 0, y, w, 1, bufs[0]);
160 SetPixelIndex(x,h-1-y,(BYTE)((jas_matrix_getv(bufs[0], x)>>nshift)));
168 if (strcmp(message,"")) strncpy(info.szLastError,message,255);
169 if (info.nEscape == -1 && fmt>0){
177 for (i = 0; i < image->numcmpts_; ++i){ if (bufs[i]) jas_matrix_destroy(bufs[i]);}
181 if (image) jas_image_destroy(image);
182 if (in) jas_stream_close(in);
185 ////////////////////////////////////////////////////////////////////////////////
186 #endif //CXIMAGE_SUPPORT_DECODE
187 ////////////////////////////////////////////////////////////////////////////////
188 #if CXIMAGE_SUPPORT_ENCODE
189 ////////////////////////////////////////////////////////////////////////////////
190 bool CxImageJAS::Encode(CxFile * hFile, DWORD imagetype)
192 if (EncodeSafeCheck(hFile)) return false;
194 if (head.biClrUsed!=0 && !IsGrayScale()){
195 strcpy(info.szLastError,"JasPer can save only RGB or GrayScale images");
199 jas_image_t *image=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;
209 cx_throw("cannot initialize jasper");
211 out = jas_stream_fdopen(0, "wb");
213 cx_throw("error: cannot open standard output");
215 CxFileJas src(hFile,out);
217 numcmpts = head.biClrUsed==0 ? 3 : 1;
219 for (cmptno = 0, cmptparm = cmptparms; cmptno < numcmpts; ++cmptno, ++cmptparm) {
224 cmptparm->width = head.biWidth;
225 cmptparm->height = head.biHeight;
227 cmptparm->sgnd = false;
230 /* Create image object. */
231 image = jas_image_create(numcmpts, cmptparms, JAS_CLRSPC_UNKNOWN);
233 cx_throw("error : jas_image_create");
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));
244 jas_image_setclrspc(image, JAS_CLRSPC_SGRAY);
245 jas_image_setcmpttype(image, 0,
246 JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_GRAY_Y));
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);
255 cx_throw("error : can't allocate memory");
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);
268 jas_matrix_setv(cmpts[0], x, GetPixelIndex(x,y));
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");
281 #if CXIMAGE_SUPPORT_JP2
282 if (imagetype == CXIMAGE_FORMAT_JP2) strcpy(szfmt,"jp2");
284 #if CXIMAGE_SUPPORT_JPC
285 if (imagetype == CXIMAGE_FORMAT_JPC) strcpy(szfmt,"jpc");
287 #if CXIMAGE_SUPPORT_RAS
288 if (imagetype == CXIMAGE_FORMAT_RAS) strcpy(szfmt,"ras");
290 #if CXIMAGE_SUPPORT_PNM
291 if (imagetype == CXIMAGE_FORMAT_PNM) strcpy(szfmt,"pnm");
293 #if CXIMAGE_SUPPORT_PGX
294 if (imagetype == CXIMAGE_FORMAT_PGX){
296 if (head.biClrUsed==0) cx_throw("PGX can save only GrayScale images");
299 int outfmt = jas_image_strtofmt(szfmt);
302 sprintf(szoutopts,"rate=%.3f", info.fQuality/100.0f);
304 if (jas_image_encode(image, out, outfmt, szoutopts)) {
305 cx_throw("error: cannot encode image");
307 jas_stream_flush(out);
310 if (strcmp(message,"")) strncpy(info.szLastError,message,255);
314 for (x = 0; x < numcmpts; ++x) { if (cmpts[x]) { jas_matrix_destroy(cmpts[x]); } }
316 if (image) jas_image_destroy(image);
317 if (out) jas_stream_close(out);
321 ////////////////////////////////////////////////////////////////////////////////
322 #endif // CXIMAGE_SUPPORT_ENCODE
323 ////////////////////////////////////////////////////////////////////////////////
324 #endif // CXIMAGE_SUPPORT_JASPER