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