示例#1
0
        public static byte[] EncodeJPEG(Image jpgImage)
        {
            Tiler                 imgtiler;
            ForwCompTransf        fctransf;
            ImgDataConverter      converter;
            EncoderSpecs          encSpec;
            ForwardWT             dwt;
            Quantizer             quant;
            ROIScaler             rois;
            EntropyCoder          ecoder;
            PostCompRateAllocator ralloc;
            HeaderEncoder         headenc;
            CodestreamWriter      bwriter;

            float rate = Single.MaxValue;

            ImgReaderGDI imgsrc = new ImgReaderGDI(jpgImage);

            imgtiler = new Tiler(imgsrc, 0, 0, 0, 0, jpgImage.Width, jpgImage.Height);
            int ntiles = imgtiler.getNumTiles();

            encSpec = new EncoderSpecs(ntiles, 3, imgsrc, pl);

            fctransf  = new ForwCompTransf(imgtiler, encSpec);
            converter = new ImgDataConverter(fctransf);
            dwt       = ForwardWT.createInstance(converter, pl, encSpec);
            quant     = Quantizer.createInstance(dwt, encSpec);
            rois      = ROIScaler.createInstance(quant, pl, encSpec);
            ecoder    = EntropyCoder.createInstance(rois, pl, encSpec.cblks,
                                                    encSpec.pss, encSpec.bms,
                                                    encSpec.mqrs, encSpec.rts,
                                                    encSpec.css, encSpec.sss,
                                                    encSpec.lcs, encSpec.tts);

            using (MemoryStream stream = new MemoryStream())
            {
                bwriter = new FileCodestreamWriter(stream, Int32.MaxValue);
                ralloc  = PostCompRateAllocator.createInstance(ecoder, pl, rate, bwriter, encSpec);

                headenc = new HeaderEncoder(imgsrc, new bool[3], dwt, imgtiler, encSpec, rois, ralloc, pl);
                ralloc.HeaderEncoder = headenc;
                headenc.encodeMainHeader();
                ralloc.initialize();
                headenc.reset();
                headenc.encodeMainHeader();
                bwriter.commitBitstreamHeader(headenc);

                ralloc.runAndWrite();
                bwriter.close();

                return(stream.ToArray());
            }
        }