public ArithmeticDecoder(Big2StreamReader reader)
        {
            this.reader = reader;

            genericRegionStats    = new ArithmeticDecoderStats(1 << 1);
            refinementRegionStats = new ArithmeticDecoderStats(1 << 1);

            iadhStats  = new ArithmeticDecoderStats(1 << 9);
            iadwStats  = new ArithmeticDecoderStats(1 << 9);
            iaexStats  = new ArithmeticDecoderStats(1 << 9);
            iaaiStats  = new ArithmeticDecoderStats(1 << 9);
            iadtStats  = new ArithmeticDecoderStats(1 << 9);
            iaitStats  = new ArithmeticDecoderStats(1 << 9);
            iafsStats  = new ArithmeticDecoderStats(1 << 9);
            iadsStats  = new ArithmeticDecoderStats(1 << 9);
            iardxStats = new ArithmeticDecoderStats(1 << 9);
            iardyStats = new ArithmeticDecoderStats(1 << 9);
            iardwStats = new ArithmeticDecoderStats(1 << 9);
            iardhStats = new ArithmeticDecoderStats(1 << 9);
            iariStats  = new ArithmeticDecoderStats(1 << 9);
            iaidStats  = new ArithmeticDecoderStats(1 << 1);
        }
        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);
        }
 public HuffmanDecoder(Big2StreamReader reader)
 {
     this.reader = reader;
 }
 public MMRDecoder(Big2StreamReader reader)
 {
     this.reader = reader;
 }