示例#1
0
 public Segment(JBIG2StreamDecoder streamDecoder)
 {
     this.decoder      = streamDecoder;
     huffmanDecoder    = decoder.GetHuffmanDecoder();
     arithmeticDecoder = decoder.GetArithmeticDecoder();
     mmrDecoder        = decoder.GetMMRDecoder();
 }
        public byte[] DecodeJBIG2(byte[] data, ImageFormat format = ImageFormat.TIFF, int NewWidth = 0, int NewHeight = 0)
        {
            reader = new Big2StreamReader(data);
            ResetDecoder();
            bool validFile = CheckHeader();

            if (JBIG2StreamDecoder.debug)
            {
                Console.WriteLine("validFile = " + validFile);
            }
            if (!validFile)
            {
                /**
                 * Assume this is a stream from a PDF so there is no file header,
                 * end of page segments, or end of file segments. Organisation must
                 * be sequential, and the number of pages is assumed to be 1.
                 */
                noOfPagesKnown           = true;
                randomAccessOrganisation = false;
                noOfPages = 1;
                /** check to see if there is any global data to be read */
                if (globalData != null)
                {
                    /** set the reader to read from the global data */
                    reader = new Big2StreamReader(globalData);

                    huffmanDecoder    = new HuffmanDecoder(reader);
                    mmrDecoder        = new MMRDecoder(reader);
                    arithmeticDecoder = new ArithmeticDecoder(reader);

                    /** read in the global data segments */
                    ReadSegments();

                    /** set the reader back to the main data */
                    reader = new Big2StreamReader(data);
                }
                else
                {
                    /**
                     * There's no global data, so move the file pointer back to the
                     * start of the stream
                     */
                    reader.MovePointer(-8);
                }
            }
            else
            {
                /**
                 * We have the file header, so assume it is a valid stand-alone
                 * file.
                 */

                if (JBIG2StreamDecoder.debug)
                {
                    Console.WriteLine("==== File Header ====");
                }

                SetFileHeaderFlags();

                if (JBIG2StreamDecoder.debug)
                {
                    Console.WriteLine("randomAccessOrganisation = " + randomAccessOrganisation);
                    Console.WriteLine("noOfPagesKnown = " + noOfPagesKnown);
                }

                if (noOfPagesKnown)
                {
                    noOfPages = GetNoOfPages();

                    if (JBIG2StreamDecoder.debug)
                    {
                        Console.WriteLine("noOfPages = " + noOfPages);
                    }
                }
            }

            huffmanDecoder    = new HuffmanDecoder(reader);
            mmrDecoder        = new MMRDecoder(reader);
            arithmeticDecoder = new ArithmeticDecoder(reader);

            /** read in the main segment data */
            ReadSegments();

            //Create Image
            var rawimage   = FindPageSegement(1).GetPageBitmap();
            int width      = (int)rawimage.GetWidth();
            int height     = (int)rawimage.GetHeight();
            var dataStream = rawimage.GetData(true);
            int stride     = (width * 1 + 7) / 8;

            // This section is new for .net standard 2.0
            var bmp = BitsToImage(dataStream, width, height, stride, format);

            if (NewWidth != 0 && NewHeight != 0)
            {
                var newbitmap = ResizeHelpers.ScaleImage(bmp, NewWidth, NewHeight);
                return(newbitmap);
            }

            return(bmp);
        }