bool CxImageWBMP::Decode(CxFile *hFile) { if (hFile == NULL) return false; WBMPHEADER wbmpHead; try { if (hFile->Read(&wbmpHead,sizeof(wbmpHead),1)==0) throw "Not a WBMP"; if (wbmpHead.Type != 0) throw "Unsupported WBMP type"; if (wbmpHead.ImageHeight==0 || wbmpHead.ImageWidth==0) throw "Corrupted WBMP"; Create(wbmpHead.ImageWidth, wbmpHead.ImageHeight, 1, CXIMAGE_FORMAT_WBMP); if (!IsValid()) throw "WBMP Create failed"; SetGrayPalette(); int linewidth=(wbmpHead.ImageWidth+7)/8; CImageIterator iter(this); iter.Upset(); for (int y=0; y < wbmpHead.ImageHeight; y++){ hFile->Read(iter.GetRow(),linewidth,1); iter.PrevRow(); } } catch (char *message) { strncpy(info.szLastError,message,255); return FALSE; } return true; }
CDemoDib::CDemoDib() { m_pBits = NULL; m_hBmp = NULL; memset(m_BmpInfoPalette,0, sizeof(BITMAPINFO)+255*sizeof(RGBQUAD)); SetGrayPalette(); }
bool CxImageJPG::Decode(CxFile * hFile) { bool is_exif = false; #if CXIMAGEJPG_SUPPORT_EXIF is_exif = DecodeExif(hFile); #endif CImageIterator iter(this); /* This struct contains the JPEG decompression parameters and pointers to * working space (which is allocated as needed by the JPEG library). */ struct jpeg_decompress_struct cinfo; /* We use our private extension JPEG error handler. <CSC> */ struct ima_error_mgr jerr; jerr.buffer=info.szLastError; /* More stuff */ JSAMPARRAY buffer; /* Output row buffer */ int row_stride; /* physical row width in output buffer */ /* In this example we want to open the input file before doing anything else, * so that the setjmp() error recovery below can assume the file is open. * VERY IMPORTANT: use "b" option to fopen() if you are on a machine that * requires it in order to read binary files. */ /* Step 1: allocate and initialize JPEG decompression object */ /* We set up the normal JPEG error routines, then override error_exit. */ cinfo.err = jpeg_std_error(&jerr.pub); jerr.pub.error_exit = ima_jpeg_error_exit; /* Establish the setjmp return context for my_error_exit to use. */ if (setjmp(jerr.setjmp_buffer)) { /* If we get here, the JPEG code has signaled an error. * We need to clean up the JPEG object, close the input file, and return. */ jpeg_destroy_decompress(&cinfo); return 0; } /* Now we can initialize the JPEG decompression object. */ jpeg_create_decompress(&cinfo); /* Step 2: specify data source (eg, a file) */ //jpeg_stdio_src(&cinfo, infile); CxFileJpg src(hFile); cinfo.src = &src; /* Step 3: read file parameters with jpeg_read_header() */ (void) jpeg_read_header(&cinfo, TRUE); /* Step 4 <chupeev> handle decoder options*/ if ((GetCodecOption() & DECODE_GRAYSCALE) != 0) cinfo.out_color_space = JCS_GRAYSCALE; if ((GetCodecOption() & DECODE_QUANTIZE) != 0) { cinfo.quantize_colors = TRUE; cinfo.desired_number_of_colors = info.nQuality; } if ((GetCodecOption() & DECODE_DITHER) != 0) cinfo.dither_mode = m_nDither; if ((GetCodecOption() & DECODE_ONEPASS) != 0) cinfo.two_pass_quantize = FALSE; if ((GetCodecOption() & DECODE_NOSMOOTH) != 0) cinfo.do_fancy_upsampling = FALSE; //<DP>: Load true color images as RGB (no quantize) /* Step 4: set parameters for decompression */ /* if (cinfo.jpeg_color_space!=JCS_GRAYSCALE) { * cinfo.quantize_colors = TRUE; * cinfo.desired_number_of_colors = 128; *} */ //</DP> // Set the scale <ignacio> cinfo.scale_denom = info.nScale; // Borrowed the idea from GIF implementation <ignacio> if (info.nEscape == -1) { // Return output dimensions only jpeg_calc_output_dimensions(&cinfo); head.biWidth = cinfo.output_width; head.biHeight = cinfo.output_height; jpeg_destroy_decompress(&cinfo); return true; } /* Step 5: Start decompressor */ jpeg_start_decompress(&cinfo); /* We may need to do some setup of our own at this point before reading * the data. After jpeg_start_decompress() we have the correct scaled * output image dimensions available, as well as the output colormap * if we asked for color quantization. */ //Create the image using output dimensions <ignacio> //Create(cinfo.image_width, cinfo.image_height, 8*cinfo.output_components, CXIMAGE_FORMAT_JPG); Create(cinfo.output_width, cinfo.output_height, 8*cinfo.output_components, CXIMAGE_FORMAT_JPG); if (!pDib) longjmp(jerr.setjmp_buffer, 1); //<DP> check if the image has been created if (is_exif){ #if CXIMAGEJPG_SUPPORT_EXIF if ((m_exifinfo.Xresolution != 0.0) && (m_exifinfo.ResolutionUnit != 0)) SetXDPI((long)(m_exifinfo.Xresolution/m_exifinfo.ResolutionUnit)); if ((m_exifinfo.Yresolution != 0.0) && (m_exifinfo.ResolutionUnit != 0)) SetYDPI((long)(m_exifinfo.Yresolution/m_exifinfo.ResolutionUnit)); #endif } else { if (cinfo.density_unit==2){ SetXDPI((long)floor(cinfo.X_density * 254.0 / 10000.0 + 0.5)); SetYDPI((long)floor(cinfo.Y_density * 254.0 / 10000.0 + 0.5)); } else { SetXDPI(cinfo.X_density); SetYDPI(cinfo.Y_density); } } if (cinfo.out_color_space==JCS_GRAYSCALE){ SetGrayPalette(); head.biClrUsed =256; } else { if (cinfo.quantize_colors==TRUE){ SetPalette(cinfo.actual_number_of_colors, cinfo.colormap[0], cinfo.colormap[1], cinfo.colormap[2]); head.biClrUsed=cinfo.actual_number_of_colors; } else { head.biClrUsed=0; } } /* JSAMPLEs per row in output buffer */ row_stride = cinfo.output_width * cinfo.output_components; /* Make a one-row-high sample array that will go away when done with image */ buffer = (*cinfo.mem->alloc_sarray) ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1); /* Step 6: while (scan lines remain to be read) */ /* jpeg_read_scanlines(...); */ /* Here we use the library's state variable cinfo.output_scanline as the * loop counter, so that we don't have to keep track ourselves. */ iter.Upset(); while (cinfo.output_scanline < cinfo.output_height) { if (info.nEscape) longjmp(jerr.setjmp_buffer, 1); // <vho> - cancel decoding (void) jpeg_read_scanlines(&cinfo, buffer, 1); // info.nProgress = (long)(100*cinfo.output_scanline/cinfo.output_height); //<DP> Step 6a: CMYK->RGB */ if ((cinfo.num_components==4)&&(cinfo.quantize_colors==FALSE)){ BYTE k,*dst,*src; dst=iter.GetRow(); src=buffer[0]; for(long x3=0,x4=0; x3<(long)info.dwEffWidth && x4<row_stride; x3+=3, x4+=4){ k=src[x4+3]; dst[x3] =(BYTE)((k * src[x4+2])/255); dst[x3+1]=(BYTE)((k * src[x4+1])/255); dst[x3+2]=(BYTE)((k * src[x4+0])/255); } } else { /* Assume put_scanline_someplace wants a pointer and sample count. */ iter.SetRow(buffer[0], row_stride); } iter.PrevRow(); } /* Step 7: Finish decompression */ (void) jpeg_finish_decompress(&cinfo); /* We can ignore the return value since suspension is not possible * with the stdio data source. */ //<DP> Step 7A: Swap red and blue components // not necessary if swapped red and blue definition in jmorecfg.h;ln322 <W. Morrison> if ((cinfo.num_components==3)&&(cinfo.quantize_colors==FALSE)){ BYTE* r0=GetBits(); for(long y=0;y<head.biHeight;y++){ if (info.nEscape) longjmp(jerr.setjmp_buffer, 1); // <vho> - cancel decoding RGBtoBGR(r0,3*head.biWidth); r0+=info.dwEffWidth; } } /* Step 8: Release JPEG decompression object */ /* This is an important step since it will release a good deal of memory. */ jpeg_destroy_decompress(&cinfo); /* At this point you may want to check to see whether any corrupt-data * warnings occurred (test whether jerr.pub.num_warnings is nonzero). */ /* And we're done! */ return true; }
bool CxImageJ2K::Decode(CxFile *hFile) { if (hFile == NULL) return false; try { BYTE* src; long len; j2k_image_t *img=NULL; j2k_cp_t *cp=NULL; long i,x,y,w,h,max; len=hFile->Size(); src=(BYTE*)malloc(len); hFile->Read(src, len, 1); if (!j2k_decode(src, len, &img, &cp)) { free(src); throw "failed to decode J2K image!"; } free(src); if (img->numcomps==3 && img->comps[0].dx==img->comps[1].dx && img->comps[1].dx==img->comps[2].dx && img->comps[0].dy==img->comps[1].dy && img->comps[1].dy==img->comps[2].dy && img->comps[0].prec==img->comps[1].prec && img->comps[1].prec==img->comps[2].prec) { w=CEILDIV(img->x1-img->x0, img->comps[0].dx); h=CEILDIV(img->y1-img->y0, img->comps[0].dy); max=(1<<img->comps[0].prec)-1; Create(w,h,24,CXIMAGE_FORMAT_J2K); RGBQUAD c; for (i=0,y=0; y<h; y++) { for (x=0; x<w; x++,i++){ c.rgbRed = img->comps[0].data[i]; c.rgbGreen = img->comps[1].data[i]; c.rgbBlue = img->comps[2].data[i]; SetPixelColor(x,h-1-y,c); } } } else { int compno; info.nNumFrames = img->numcomps; if ((info.nFrame<0)||(info.nFrame>=info.nNumFrames)){ j2k_destroy(&img,&cp); throw "wrong frame!"; } for (compno=0; compno<=info.nFrame; compno++) { w=CEILDIV(img->x1-img->x0, img->comps[compno].dx); h=CEILDIV(img->y1-img->y0, img->comps[compno].dy); max=(1<<img->comps[compno].prec)-1; Create(w,h,8,CXIMAGE_FORMAT_J2K); SetGrayPalette(); for (i=0,y=0; y<h; y++) { for (x=0; x<w; x++,i++){ SetPixelIndex(x,h-1-y,img->comps[compno].data[i]); } } } } j2k_destroy(&img,&cp); } catch (char *message) { strncpy(info.szLastError,message,255); return FALSE; } return true; }
bool CxImageJAS::Decode(CxFile *hFile, uint32_t imagetype) { if (hFile == NULL) return false; jas_image_t *image=0; jas_stream_t *in=0; jas_matrix_t **bufs=0; int32_t i,error=0; int32_t fmt; //jas_setdbglevel(0); cx_try { if (jas_init()) cx_throw("cannot initialize jasper"); in = jas_stream_fdopen(0, "rb"); if (!in) cx_throw("error: cannot open standard input"); CxFileJas src(hFile,in); fmt = jas_image_getfmt(in); if (fmt<0) cx_throw("error: unknowm format"); image = jas_image_decode(in, fmt, 0); if (!image){ fmt = -1; cx_throw("error: cannot load image data"); } char szfmt[4]; *szfmt = '\0'; strncpy(szfmt,jas_image_fmttostr(fmt),3); szfmt[3] = '\0'; fmt = -1; #if CXIMAGE_SUPPORT_JP2 if (strcmp(szfmt,"jp2")==0) fmt = CXIMAGE_FORMAT_JP2; #endif #if CXIMAGE_SUPPORT_JPC if (strcmp(szfmt,"jpc")==0) fmt = CXIMAGE_FORMAT_JPC; #endif #if CXIMAGE_SUPPORT_RAS if (strcmp(szfmt,"ras")==0) fmt = CXIMAGE_FORMAT_RAS; #endif #if CXIMAGE_SUPPORT_PNM if (strcmp(szfmt,"pnm")==0) fmt = CXIMAGE_FORMAT_PNM; #endif #if CXIMAGE_SUPPORT_PGX if (strcmp(szfmt,"pgx")==0) fmt = CXIMAGE_FORMAT_PGX; #endif //if (fmt<0) // cx_throw("error: unknowm format"); int32_t x,y,w,h,depth,cmptno; w = jas_image_cmptwidth(image,0); h = jas_image_cmptheight(image,0); depth = jas_image_cmptprec(image,0); if (info.nEscape == -1){ head.biWidth = w; head.biHeight= h; info.dwType = fmt<0 ? 0 : fmt; cx_throw("output dimensions returned"); } if (image->numcmpts_ > 64 || image->numcmpts_ < 0) cx_throw("error: too many components"); // <LD> 01/Jan/2005: Always force conversion to sRGB. Seems to be required for many types of JPEG2000 file. // if (depth!=1 && depth!=4 && depth!=8) if (image->numcmpts_>=3 && depth <=8) { jas_image_t *newimage; jas_cmprof_t *outprof; //jas_eprintf("forcing conversion to sRGB\n"); outprof = jas_cmprof_createfromclrspc(JAS_CLRSPC_SRGB); if (!outprof) { cx_throw("cannot create sRGB profile"); } newimage = jas_image_chclrspc(image, outprof, JAS_CMXFORM_INTENT_PER); if (!newimage) { jas_cmprof_destroy(outprof); // <LD> 01/Jan/2005: Destroy color profile on error. cx_throw("cannot convert to sRGB"); } jas_image_destroy(image); jas_cmprof_destroy(outprof); image = newimage; } bufs = (jas_matrix_t **)calloc(image->numcmpts_, sizeof(jas_matrix_t**)); for (i = 0; i < image->numcmpts_; ++i) { bufs[i] = jas_matrix_create(1, w); if (!bufs[i]) { cx_throw("error: cannot allocate memory"); } } int32_t nshift = (depth>8) ? (depth-8) : 0; if (image->numcmpts_==3 && image->cmpts_[0]->width_ == image->cmpts_[1]->width_ && image->cmpts_[1]->width_ == image->cmpts_[2]->width_ && image->cmpts_[0]->height_ == image->cmpts_[1]->height_ && image->cmpts_[1]->height_ == image->cmpts_[2]->height_ && image->cmpts_[0]->prec_ == image->cmpts_[1]->prec_ && image->cmpts_[1]->prec_ == image->cmpts_[2]->prec_ ) { if(!Create(w,h,24,fmt)) cx_throw(""); RGBQUAD c; for (y=0; y<h; y++) { for (cmptno = 0; cmptno < image->numcmpts_; ++cmptno) { jas_image_readcmpt(image, cmptno, 0, y, w, 1, bufs[cmptno]); } for (x=0; x<w; x++){ c.rgbRed = (uint8_t)((jas_matrix_getv(bufs[0], x)>>nshift)); c.rgbGreen = (uint8_t)((jas_matrix_getv(bufs[1], x)>>nshift)); c.rgbBlue = (uint8_t)((jas_matrix_getv(bufs[2], x)>>nshift)); SetPixelColor(x,h-1-y,c); } } } else { info.nNumFrames = image->numcmpts_; if ((info.nFrame<0)||(info.nFrame>=info.nNumFrames)){ cx_throw("wrong frame!"); } for (cmptno=0; cmptno<=info.nFrame; cmptno++) { w = jas_image_cmptwidth(image,cmptno); h = jas_image_cmptheight(image,cmptno); depth = jas_image_cmptprec(image,cmptno); if (depth>8) depth=8; if(!Create(w,h,depth,imagetype)) cx_throw(""); SetGrayPalette(); for (y=0; y<h; y++) { jas_image_readcmpt(image, cmptno, 0, y, w, 1, bufs[0]); for (x=0; x<w; x++){ SetPixelIndex(x,h-1-y,(uint8_t)((jas_matrix_getv(bufs[0], x)>>nshift))); } } } } } cx_catch {