示例#1
0
jas_image_t *converttosrgb(jas_image_t *inimage)
{
	jas_image_t *outimage;
	jas_cmpixmap_t inpixmap;
	jas_cmpixmap_t outpixmap;
	jas_cmcmptfmt_t incmptfmts[16];
	jas_cmcmptfmt_t outcmptfmts[16];

	outprof = jas_cmprof_createfromclrspc(JAS_CLRSPC_SRGB);
	assert(outprof);
	xform = jas_cmxform_create(jas_image_cmprof(inimage), outprof, 0, JAS_CMXFORM_FWD, JAS_CMXFORM_INTENT_PER, 0);
	assert(xform);

	inpixmap.numcmpts = jas_image_numcmpts(oldimage);
	outpixmap.numcmpts = 3;
	for (i = 0; i < inpixmap.numcmpts; ++i) {
		inpixmap.cmptfmts[i] = &incmptfmts[i];
	}
	for (i = 0; i < outpixmap.numcmpts; ++i)
		outpixmap.cmptfmts[i] = &outcmptfmts[i];
	if (jas_cmxform_apply(xform, &inpixmap, &outpixmap))
		abort();

	jas_xform_destroy(xform);
	jas_cmprof_destroy(outprof);
	return 0;
}
示例#2
0
void JP2Instance::WriteICCProfile( const ICCProfile& icc )
{
   CheckOpenStream( !m_path.IsEmpty(), "WriteICCProfile" );

   if ( m_jp2CMProfile != nullptr )
      jas_cmprof_destroy( m_jp2CMProfile ), m_jp2CMProfile = nullptr;

   if ( icc.IsProfile() )
   {
      jas_iccprof_t* jp2ICCProfile = nullptr;

      try
      {
         ByteArray iccData = icc.ProfileData();
         iccData.EnsureUnique();

         jp2ICCProfile = jas_iccprof_createfrombuf( iccData.Begin(), int( icc.ProfileSize() ) );
         if ( jp2ICCProfile == nullptr )
            JP2KERROR( "Unable to generate embedded ICC profile in JPEG2000 image" );

         m_jp2CMProfile = jas_cmprof_createfromiccprof( jp2ICCProfile );
         if ( m_jp2CMProfile == nullptr )
            JP2KERROR( "Unable to embed ICC profile in JPEG2000 image" );

         jas_iccprof_destroy( jp2ICCProfile ), jp2ICCProfile = nullptr;

         Console().WriteLn( "<end><cbr>ICC profile embedded: \'" + icc.Description() + "\', " + String( icc.ProfileSize() ) + " bytes." );
      }
      catch ( ... )
      {
         if ( jp2ICCProfile != nullptr )
            jas_iccprof_destroy( jp2ICCProfile );
         Close();
         throw;
      }
   }
}
示例#3
0
void JPCInstance::Close()
{
   if ( m_jp2Stream != nullptr )
      jas_stream_close( m_jp2Stream ), m_jp2Stream = nullptr;

   if ( m_jp2Image != nullptr )
   {
      jas_image_setcmprof( m_jp2Image, nullptr ); // don't destroy embedded profile now
      jas_image_destroy( m_jp2Image ), m_jp2Image = nullptr;
   }

   if ( m_jp2CMProfile != nullptr )
      jas_cmprof_destroy( m_jp2CMProfile ), m_jp2CMProfile = nullptr;

   CleanupJasPer();

   m_path.Clear();

   m_options.Reset();

   m_jp2Options = JPCFormat::DefaultOptions();

   m_queriedOptions = false;
}
示例#4
0
int main(int argc, char **argv)
{
	jas_image_t *image;
	cmdopts_t *cmdopts;
	jas_stream_t *in;
	jas_stream_t *out;
	clock_t startclk;
	clock_t endclk;
	long dectime;
	long enctime;
	int_fast16_t numcmpts;
	int i;

	/* Determine the base name of this command. */
	if ((cmdname = strrchr(argv[0], '/'))) {
		++cmdname;
	} else {
		cmdname = argv[0];
	}

	if (jas_init()) {
		errprint(0, "error: cannot initialize jasper library\n");
		abort();
	}

	/* set our error callback */
	jas_set_error_cb(errprint);

	/* Parse the command line options. */
	if (!(cmdopts = cmdopts_parse(argc, argv))) {
		jas_eprintf("error: cannot parse command line\n");
		exit(EXIT_FAILURE);
	}

	if (cmdopts->version) {
		jas_eprintf("%s\n", JAS_VERSION);
		jas_eprintf("libjasper %s\n", jas_getversion());
		exit(EXIT_SUCCESS);
	}

	jas_setdbglevel(cmdopts->debug);

	if (cmdopts->verbose) {
		cmdinfo();
	}

	/* Open the input image file. */
	if (cmdopts->infile) {
		/* The input image is to be read from a file. */
		if (!(in = jas_stream_fopen(cmdopts->infile, "rb"))) {
			jas_eprintf("error: cannot open input image file %s\n",
			  cmdopts->infile);
			exit(EXIT_FAILURE);
		}
	} else {
		/* The input image is to be read from standard input. */
		if (!(in = jas_stream_fdopen(0, "rb"))) {
			jas_eprintf("error: cannot open standard input\n");
			exit(EXIT_FAILURE);
		}
	}

	/* Open the output image file. */
	if (cmdopts->outfile) {
		/* The output image is to be written to a file. */
		if (!(out = jas_stream_fopen(cmdopts->outfile, "w+b"))) {
			jas_eprintf("error: cannot open output image file %s\n",
			  cmdopts->outfile);
			exit(EXIT_FAILURE);
		}
	} else {
		/* The output image is to be written to standard output. */
		if (!(out = jas_stream_fdopen(1, "w+b"))) {
			jas_eprintf("error: cannot open standard output\n");
			exit(EXIT_FAILURE);
		}
	}

	if (cmdopts->infmt < 0) {
		if ((cmdopts->infmt = jas_image_getfmt(in)) < 0) {
			jas_eprintf("error: input image has unknown format\n");
			exit(EXIT_FAILURE);
		}
	}

	/* Get the input image data. */
	startclk = clock();
	if (!(image = jas_image_decode(in, cmdopts->infmt, cmdopts->inopts))) {
		jas_eprintf("error: cannot load image data\n");
		exit(EXIT_FAILURE);
	}
	endclk = clock();
	dectime = endclk - startclk;

	/* If requested, throw away all of the components except one.
	  Why might this be desirable?  It is a hack, really.
	  None of the image formats other than the JPEG-2000 ones support
	  images with two, four, five, or more components.  This hack
	  allows such images to be decoded with the non-JPEG-2000 decoders,
	  one component at a time. */
	numcmpts = jas_image_numcmpts(image);
	if (cmdopts->cmptno >= 0 && cmdopts->cmptno < numcmpts) {
		for (i = numcmpts - 1; i >= 0; --i) {
			if (i != cmdopts->cmptno) {
				jas_image_delcmpt(image, i);
			}
		}
	}

	if (cmdopts->srgb) {
		jas_image_t *newimage;
		jas_cmprof_t *outprof;
		jas_eprintf("forcing conversion to sRGB\n");
		if (!(outprof = jas_cmprof_createfromclrspc(JAS_CLRSPC_SRGB))) {
			jas_eprintf("cannot create sRGB profile\n");
			exit(EXIT_FAILURE);
		}
		if (!(newimage = jas_image_chclrspc(image, outprof, JAS_CMXFORM_INTENT_PER))) {
			jas_eprintf("cannot convert to sRGB\n");
			exit(EXIT_FAILURE);
		}
		jas_image_destroy(image);
		jas_cmprof_destroy(outprof);
		image = newimage;
	}

	/* Generate the output image data. */
	startclk = clock();
	if (jas_image_encode(image, out, cmdopts->outfmt, cmdopts->outopts)) {
		jas_eprintf("error: cannot encode image\n");
		exit(EXIT_FAILURE);
	}
	jas_stream_flush(out);
	endclk = clock();
	enctime = endclk - startclk;

	if (cmdopts->verbose) {
		jas_eprintf("decoding time = %f\n", dectime / (double)
		  CLOCKS_PER_SEC);
		jas_eprintf("encoding time = %f\n", enctime / (double)
		  CLOCKS_PER_SEC);
	}

	/* If this fails, we don't care. */
	(void) jas_stream_close(in);

	/* Close the output image stream. */
	if (jas_stream_close(out)) {
		jas_eprintf("error: cannot close output image file\n");
		exit(EXIT_FAILURE);
	}

	cmdopts_destroy(cmdopts);
	jas_image_destroy(image);
	jas_image_clearfmts();

	/* Success at last! :-) */
	return EXIT_SUCCESS;
}
示例#5
0
bool  Jpeg2KDecoder::readData(Mat& img) {
    bool result = false;
    int color = img.channels() > 1;
    uchar* data = img.data;
    int step = img.step;
    jas_stream_t* stream = (jas_stream_t*)m_stream;
    jas_image_t* image = (jas_image_t*)m_image;

    if (stream && image) {
        bool convert;
        int colorspace;
        if (color) {
            convert = (jas_image_clrspc(image) != JAS_CLRSPC_SRGB);
            colorspace = JAS_CLRSPC_SRGB;
        } else {
            convert = (jas_clrspc_fam(jas_image_clrspc(image)) != JAS_CLRSPC_FAM_GRAY);
            colorspace = JAS_CLRSPC_SGRAY; // TODO GENGRAY or SGRAY?
        }

        // convert to the desired colorspace
        if (convert) {
            jas_cmprof_t* clrprof = jas_cmprof_createfromclrspc(colorspace);
            if (clrprof) {
                jas_image_t* _img = jas_image_chclrspc(image, clrprof, JAS_CMXFORM_INTENT_RELCLR);
                if (_img) {
                    jas_image_destroy(image);
                    m_image = image = _img;
                    result = true;
                } else {
                    fprintf(stderr, "JPEG 2000 LOADER ERROR: cannot convert colorspace\n");
                }
                jas_cmprof_destroy(clrprof);
            } else {
                fprintf(stderr, "JPEG 2000 LOADER ERROR: unable to create colorspace\n");
            }
        } else {
            result = true;
        }

        if (result) {
            int ncmpts;
            int cmptlut[3];
            if (color) {
                cmptlut[0] = jas_image_getcmptbytype(image, JAS_IMAGE_CT_RGB_B);
                cmptlut[1] = jas_image_getcmptbytype(image, JAS_IMAGE_CT_RGB_G);
                cmptlut[2] = jas_image_getcmptbytype(image, JAS_IMAGE_CT_RGB_R);
                if (cmptlut[0] < 0 || cmptlut[1] < 0 || cmptlut[0] < 0) {
                    result = false;
                }
                ncmpts = 3;
            } else {
                cmptlut[0] = jas_image_getcmptbytype(image, JAS_IMAGE_CT_GRAY_Y);
                if (cmptlut[0] < 0) {
                    result = false;
                }
                ncmpts = 1;
            }

            if (result) {
                for (int i = 0; i < ncmpts; i++) {
                    int maxval = 1 << jas_image_cmptprec(image, cmptlut[i]);
                    int offset =  jas_image_cmptsgnd(image, cmptlut[i]) ? maxval / 2 : 0;

                    int yend = jas_image_cmptbry(image, cmptlut[i]);
                    int ystep = jas_image_cmptvstep(image, cmptlut[i]);
                    int xend = jas_image_cmptbrx(image, cmptlut[i]);
                    int xstep = jas_image_cmpthstep(image, cmptlut[i]);

                    jas_matrix_t* buffer = jas_matrix_create(yend / ystep, xend / xstep);
                    if (buffer) {
                        if (!jas_image_readcmpt(image, cmptlut[i], 0, 0, xend / xstep, yend / ystep, buffer)) {
                            if (img.depth() == CV_8U) {
                                result = readComponent8u(data + i, buffer, step, cmptlut[i], maxval, offset, ncmpts);
                            } else {
                                result = readComponent16u(((unsigned short*)data) + i, buffer, step / 2, cmptlut[i], maxval, offset, ncmpts);
                            }
                            if (!result) {
                                i = ncmpts;
                                result = false;
                            }
                        }
                        jas_matrix_destroy(buffer);
                    }
                }
            }
        } else {
            fprintf(stderr, "JPEG2000 LOADER ERROR: colorspace conversion failed\n");
        }
    }

    close();

    return result;
}
示例#6
0
int JPEG2000Dataset::DecodeImage()
{
    if (bAlreadyDecoded)
        return psImage != NULL;
        
    bAlreadyDecoded = TRUE;    
    if ( !( psImage = jas_image_decode(psStream, iFormat, 0) ) )
    {
        CPLDebug( "JPEG2000", "Unable to decode image. Format: %s, %d",
                  jas_image_fmttostr( iFormat ), iFormat );
        return FALSE;
    }
    
    /* Case of a JP2 image : check that the properties given by */
    /* the JP2 boxes match the ones of the code stream */
    if (nBands != 0)
    {
        if (nBands != jas_image_numcmpts( psImage ))
        {
            CPLError(CE_Failure, CPLE_AppDefined,
                     "The number of components indicated in the IHDR box (%d) mismatch "
                     "the value specified in the code stream (%d)",
                     nBands, jas_image_numcmpts( psImage ));
            jas_image_destroy( psImage );
            psImage = NULL;
            return FALSE;
        }
        
        if (nRasterXSize != jas_image_cmptwidth( psImage, 0 ) ||
            nRasterYSize != jas_image_cmptheight( psImage, 0 ) )
        {
            CPLError(CE_Failure, CPLE_AppDefined,
                     "The dimensions indicated in the IHDR box (%d x %d) mismatch "
                     "the value specified in the code stream (%d x %d)",
                     nRasterXSize, nRasterYSize,
                     jas_image_cmptwidth( psImage, 0 ),
                     jas_image_cmptheight( psImage, 0 ));
            jas_image_destroy( psImage );
            psImage = NULL;
            return FALSE;
        }
        
        int iBand;
        for ( iBand = 0; iBand < nBands; iBand++ )
        {
            JPEG2000RasterBand* poBand = (JPEG2000RasterBand*) GetRasterBand(iBand+1);
            if (poBand->iDepth != jas_image_cmptprec( psImage, iBand ) ||
                poBand->bSignedness != jas_image_cmptsgnd( psImage, iBand ))
            {
                CPLError(CE_Failure, CPLE_AppDefined,
                         "The bit depth of band %d indicated in the IHDR box (%d) mismatch "
                         "the value specified in the code stream (%d)",
                         iBand + 1, poBand->iDepth, jas_image_cmptprec( psImage, iBand ));
                jas_image_destroy( psImage );
                psImage = NULL;
                return FALSE;
            }
        }
    }
    
    /* Ask for YCbCr -> RGB translation */
    if ( jas_clrspc_fam( jas_image_clrspc( psImage ) ) == 
              JAS_CLRSPC_FAM_YCBCR )
    {
        jas_image_t *psRGBImage;
        jas_cmprof_t *psRGBProf;
        CPLDebug( "JPEG2000", "forcing conversion to sRGB");
        if (!(psRGBProf = jas_cmprof_createfromclrspc(JAS_CLRSPC_SRGB))) {
            CPLDebug( "JPEG2000", "cannot create sRGB profile");
            return TRUE;
        }
        if (!(psRGBImage = jas_image_chclrspc(psImage, psRGBProf, JAS_CMXFORM_INTENT_PER))) {
            CPLDebug( "JPEG2000", "cannot convert to sRGB");
            jas_cmprof_destroy(psRGBProf);
            return TRUE;
        }
        jas_image_destroy(psImage);
        jas_cmprof_destroy(psRGBProf);
        psImage = psRGBImage;
    }
    
    return TRUE;
}
示例#7
0
jas_cmprof_t *jas_cmprof_createfromiccprof(jas_iccprof_t *iccprof)
{
	jas_cmprof_t *prof;
	jas_icchdr_t icchdr;
	jas_cmpxformseq_t *fwdpxformseq;
	jas_cmpxformseq_t *revpxformseq;

	prof = 0;
	fwdpxformseq = 0;
	revpxformseq = 0;

	if (!(prof = jas_cmprof_create()))
		goto error;
	jas_iccprof_gethdr(iccprof, &icchdr);
	if (!(prof->iccprof = jas_iccprof_copy(iccprof)))
		goto error;
	prof->clrspc = icctoclrspc(icchdr.colorspc, 0);
	prof->refclrspc = icctoclrspc(icchdr.refcolorspc, 1);
	prof->numchans = jas_clrspc_numchans(prof->clrspc);
	prof->numrefchans = jas_clrspc_numchans(prof->refclrspc);

	if (prof->numchans == 1) {
		if (mono(prof->iccprof, 0, &fwdpxformseq))
			goto error;
		if (mono(prof->iccprof, 1, &revpxformseq))
			goto error;
	} else if (prof->numchans == 3) {
		if (triclr(prof->iccprof, 0, &fwdpxformseq))
			goto error;
		if (triclr(prof->iccprof, 1, &revpxformseq))
			goto error;
	}
	prof->pxformseqs[SEQFWD(0)] = fwdpxformseq;
	prof->pxformseqs[SEQREV(0)] = revpxformseq;

#if 0
	if (prof->numchans > 1) {
		lut(prof->iccprof, 0, PER, &pxformseq);
		pxformseqs_set(prof, SEQFWD(PER), pxformseq);
		lut(prof->iccprof, 1, PER, &pxformseq);
		pxformseqs_set(prof, SEQREV(PER), pxformseq);
		lut(prof->iccprof, 0, CLR, &pxformseq);
		pxformseqs_set(prof, SEQREV(CLR), pxformseq);
		lut(prof->iccprof, 1, CLR, &pxformseq);
		pxformseqs_set(prof, SEQREV(CLR), pxformseq);
		lut(prof->iccprof, 0, SAT, &pxformseq);
		pxformseqs_set(prof, SEQREV(SAT), pxformseq);
		lut(prof->iccprof, 1, SAT, &pxformseq);
		pxformseqs_set(prof, SEQREV(SAT), pxformseq);
	}
#endif

	return prof;

error:
	if (fwdpxformseq) {
		jas_cmpxformseq_destroy(fwdpxformseq);
	}
	if (revpxformseq) {
		jas_cmpxformseq_destroy(revpxformseq);
	}
	if (prof) {
		jas_cmprof_destroy(prof);
	}

	return 0;
}
示例#8
0
bool  GrFmtJpeg2000Reader::ReadData( uchar* data, int step, int color )
{
    bool result = false;

    color = color > 0 || ( m_iscolor && color < 0 );

    if( m_stream && m_image )
    {
        bool convert;
        int colorspace;
        if( color )
        {
            convert = (jas_image_clrspc( m_image ) != JAS_CLRSPC_SRGB);
            colorspace = JAS_CLRSPC_SRGB;
        }
        else
        {
            convert = (jas_clrspc_fam( jas_image_clrspc( m_image ) ) != JAS_CLRSPC_FAM_GRAY);
            colorspace = JAS_CLRSPC_SGRAY; // TODO GENGRAY or SGRAY?
        }

        // convert to the desired colorspace
        if( convert )
        {
            jas_cmprof_t *clrprof = jas_cmprof_createfromclrspc( colorspace );
            if( clrprof )
            {
                jas_image_t *img = jas_image_chclrspc( m_image, clrprof, JAS_CMXFORM_INTENT_RELCLR );
                if( img )
                {
                    jas_image_destroy( m_image );
                    m_image = img;
                    result = true;
                }
                else
                    fprintf(stderr, "JPEG 2000 LOADER ERROR: cannot convert colorspace\n");
                jas_cmprof_destroy( clrprof );
            }
            else
                fprintf(stderr, "JPEG 2000 LOADER ERROR: unable to create colorspace\n");
        }
        else
            result = true;

        if( result )
        {
            int ncmpts;
            int cmptlut[3];
            if( color )
            {
                cmptlut[0] = jas_image_getcmptbytype( m_image, JAS_IMAGE_CT_RGB_B );
                cmptlut[1] = jas_image_getcmptbytype( m_image, JAS_IMAGE_CT_RGB_G );
                cmptlut[2] = jas_image_getcmptbytype( m_image, JAS_IMAGE_CT_RGB_R );
                if( cmptlut[0] < 0 || cmptlut[1] < 0 || cmptlut[0] < 0 )
                    result = false;
                ncmpts = 3;
            }
            else
            {
                cmptlut[0] = jas_image_getcmptbytype( m_image, JAS_IMAGE_CT_GRAY_Y );
                if( cmptlut[0] < 0 )
                    result = false;
                ncmpts = 1;
            }

            if( result )
            {
                for( int i = 0; i < ncmpts; i++ )
                {
                    int maxval = 1 << jas_image_cmptprec( m_image, cmptlut[i] );
                    int offset =  jas_image_cmptsgnd( m_image, cmptlut[i] ) ? maxval / 2 : 0;

                    int yend = jas_image_cmptbry( m_image, cmptlut[i] );
                    int ystep = jas_image_cmptvstep( m_image, cmptlut[i] );
                    int xend = jas_image_cmptbrx( m_image, cmptlut[i] );
                    int xstep = jas_image_cmpthstep( m_image, cmptlut[i] );

                    jas_matrix_t *buffer = jas_matrix_create( yend / ystep, xend / xstep );
                    if( buffer )
                    {
                        if( !jas_image_readcmpt( m_image, cmptlut[i], 0, 0, xend / xstep, yend / ystep, buffer ))
                        {
                            if( m_bit_depth == 8 || !m_native_depth )
                                result = ReadComponent8u( data + i, buffer, step, cmptlut[i], maxval, offset, ncmpts );
                            else
                                result = ReadComponent16u( ((unsigned short *)data) + i, buffer, step / 2, cmptlut[i], maxval, offset, ncmpts );
                            if( !result )
                            {
                                i = ncmpts;
                                result = false;
                            }
                        }
                        jas_matrix_destroy( buffer );
                    }
                }
            }
        }
        else
	    fprintf(stderr, "JPEG2000 LOADER ERROR: colorspace conversion failed\n" );
    }

    Close();

    return result;
}
示例#9
0
static gboolean
query_jp2 (const gchar   *path,
           gint          *width,
           gint          *height,
           gint          *depth,
           jas_image_t  **jas_image)
{
  gboolean ret;
  jas_stream_t *in;
  int image_fmt;
  jas_image_t *image;
  jas_cmprof_t *output_profile;
  jas_image_t *cimage;
  int numcmpts;
  int i;
  gboolean b;

  in = NULL;
  cimage = image = NULL;
  output_profile = NULL;
  ret = FALSE;

  do
    {
      in = jas_stream_fopen (path, "rb");
      if (!in)
	{
	  g_warning ("Unable to open image file '%s'", path);
	  break;
	}

      image_fmt = jas_image_getfmt (in);
      if (image_fmt < 0)
	{
	  g_warning (_("Unknown JPEG-2000 image format in '%s'"), path);
          break;
	}

      image = jas_image_decode (in, image_fmt, NULL);
      if (!image)
	{
	  g_warning (_("Unable to open JPEG-2000 image in '%s'"), path);
	  break;
	}

      output_profile = jas_cmprof_createfromclrspc (JAS_CLRSPC_SRGB);
      if (!output_profile)
        {
	  g_warning (_("Unable to create output color profile for '%s'"), path);
	  break;
        }

      cimage = jas_image_chclrspc (image, output_profile,
                                   JAS_CMXFORM_INTENT_PER);
      if (!cimage)
        {
	  g_warning (_("Unable to convert image to sRGB color space "
                       "when processing '%s'"), path);
	  break;
        }

      numcmpts = jas_image_numcmpts (cimage);
      if (numcmpts != 3)
	{
	  g_warning (_("Unsupported non-RGB JPEG-2000 file with "
                       "%d components in '%s'"), numcmpts, path);
	  break;
	}

      *width = jas_image_cmptwidth (cimage, 0);
      *height = jas_image_cmptheight (cimage, 0);
      *depth = jas_image_cmptprec (cimage, 0);

      if ((*depth != 8) && (*depth != 16))
	{
	  g_warning (_("Unsupported JPEG-2000 file with depth %d in '%s'"),
                     *depth, path);
	  break;
	}

      b = FALSE;

      for (i = 1; i < 3; i++)
        {
          if ((jas_image_cmptprec (cimage, i) != *depth) ||
              (jas_image_cmptwidth (cimage, i) != *width) ||
              (jas_image_cmptheight (cimage, i) != *height))
            {
              g_warning (_("Components of input image '%s' don't match"),
                         path);
              b = TRUE;
              break;
            }
        }

      if (b)
        break;

      ret = TRUE;
    }
  while (FALSE); /* structured goto */

  if (jas_image)
    *jas_image = cimage;
  else if (cimage)
    jas_image_destroy (cimage);

  if (image)
    jas_image_destroy (image);

  if (output_profile)
    jas_cmprof_destroy (output_profile);

  if (in)
    jas_stream_close (in);

  return ret;
}
示例#10
0
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 {