public Segment(JBIG2StreamDecoder streamDecoder)
 {
     this.decoder      = streamDecoder;
     huffmanDecoder    = decoder.GetHuffmanDecoder();
     arithmeticDecoder = decoder.GetArithmeticDecoder();
     mmrDecoder        = decoder.GetMMRDecoder();
 }
Esempio n. 2
0
        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);

            var newarray = new byte[dataStream.Length];

            Array.Copy(dataStream, newarray, dataStream.Length);
            int stride = (width * 1 + 7) / 8;

            var bitmap = new WriteableBitmap(width, height, 96, 96, System.Windows.Media.PixelFormats.BlackWhite, null);

            bitmap.WritePixels(new System.Windows.Int32Rect(0, 0, width, height), newarray, stride, 0);

            MemoryStream stream3 = new MemoryStream();

            if (format == ImageFormat.TIFF)
            {
                var encoder = new TiffBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(bitmap));
                encoder.Save(stream3);
            }
            else if (format == ImageFormat.JPEG)
            {
                var encoder = new JpegBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(bitmap));
                encoder.Save(stream3);
            }
            else if (format == ImageFormat.PNG)
            {
                var encoder = new PngBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(bitmap));
                encoder.Save(stream3);
            }

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

            return(stream3.ToArray());
        }