Esempio n. 1
0
        public override void ReadSegment()
        {
            /** read text region Segment flags */
            ReadPatternDictionaryFlags();

            width  = decoder.Readbyte();
            height = decoder.Readbyte();

            if (JBIG2StreamDecoder.debug)
            {
                Console.WriteLine("pattern dictionary size = " + width + " , " + height);
            }

            short[] buf = new short[4];
            decoder.Readbyte(buf);
            grayMax = BinaryOperation.GetInt32(buf);

            if (JBIG2StreamDecoder.debug)
            {
                Console.WriteLine("grey max = " + grayMax);
            }

            bool useMMR   = patternDictionaryFlags.GetFlagValue(PatternDictionaryFlags.HD_MMR) == 1;
            int  template = patternDictionaryFlags.GetFlagValue(PatternDictionaryFlags.HD_TEMPLATE);

            if (!useMMR)
            {
                arithmeticDecoder.ResetGenericStats(template, null);
                arithmeticDecoder.Start();
            }

            short[] genericBAdaptiveTemplateX = new short[4], genericBAdaptiveTemplateY = new short[4];

            genericBAdaptiveTemplateX[0] = (short)-width;
            genericBAdaptiveTemplateY[0] = 0;
            genericBAdaptiveTemplateX[1] = -3;
            genericBAdaptiveTemplateY[1] = -1;
            genericBAdaptiveTemplateX[2] = 2;
            genericBAdaptiveTemplateY[2] = -2;
            genericBAdaptiveTemplateX[3] = -2;
            genericBAdaptiveTemplateY[3] = -2;

            size = grayMax + 1;

            JBIG2Bitmap bitmap = new JBIG2Bitmap(size * width, height, arithmeticDecoder, huffmanDecoder, mmrDecoder);

            bitmap.Clear(0);
            bitmap.ReadBitmap(useMMR, template, false, false, null, genericBAdaptiveTemplateX, genericBAdaptiveTemplateY, segmentHeader.GetSegmentDataLength() - 7);

            JBIG2Bitmap[] bitmaps = new JBIG2Bitmap[size];

            int x = 0;

            for (int i = 0; i < size; i++)
            {
                bitmaps[i] = bitmap.GetSlice(x, 0, width, height);
                x         += width;
            }

            this.bitmaps = bitmaps;
        }
        public override void ReadSegment()
        {
            base.ReadSegment();

            /** read text region Segment flags */
            ReadHalftoneRegionFlags();

            short[] buf = new short[4];
            decoder.Readbyte(buf);
            int gridWidth = BinaryOperation.GetInt32(buf);

            buf = new short[4];
            decoder.Readbyte(buf);
            int gridHeight = BinaryOperation.GetInt32(buf);

            buf = new short[4];
            decoder.Readbyte(buf);
            int gridX = BinaryOperation.GetInt32(buf);

            buf = new short[4];
            decoder.Readbyte(buf);
            int gridY = BinaryOperation.GetInt32(buf);

            if (JBIG2StreamDecoder.debug)
            {
                Console.WriteLine("grid pos and size = " + gridX + ',' + gridY + ' ' + gridWidth + ',' + gridHeight);
            }

            buf = new short[2];
            decoder.Readbyte(buf);
            int stepX = BinaryOperation.GetInt16(buf);

            buf = new short[2];
            decoder.Readbyte(buf);
            int stepY = BinaryOperation.GetInt16(buf);

            if (JBIG2StreamDecoder.debug)
            {
                Console.WriteLine("step size = " + stepX + ',' + stepY);
            }

            int[] referedToSegments = segmentHeader.GetReferredToSegments();
            if (referedToSegments.Length != 1)
            {
                Console.WriteLine("Error in halftone Segment. refSegs should == 1");
            }

            Segment segment = decoder.FindSegment(referedToSegments[0]);

            if (segment.GetSegmentHeader().GetSegmentType() != Segment.PATTERN_DICTIONARY)
            {
                if (JBIG2StreamDecoder.debug)
                {
                    Console.WriteLine("Error in halftone Segment. bad symbol dictionary reference");
                }
            }

            PatternDictionarySegment patternDictionarySegment = (PatternDictionarySegment)segment;

            int bitsPerValue = 0, i = 1;

            while (i < patternDictionarySegment.GetSize())
            {
                bitsPerValue++;
                i <<= 1;
            }

            JBIG2Bitmap bitmap        = patternDictionarySegment.GetBitmaps()[0];
            long        patternWidth  = bitmap.GetWidth();
            long        patternHeight = bitmap.GetHeight();

            if (JBIG2StreamDecoder.debug)
            {
                Console.WriteLine("pattern size = " + patternWidth + ',' + patternHeight);
            }

            bool useMMR   = halftoneRegionFlags.GetFlagValue(HalftoneRegionFlags.H_MMR) != 0;
            int  template = halftoneRegionFlags.GetFlagValue(HalftoneRegionFlags.H_TEMPLATE);

            if (!useMMR)
            {
                arithmeticDecoder.ResetGenericStats(template, null);
                arithmeticDecoder.Start();
            }

            int halftoneDefaultPixel = halftoneRegionFlags.GetFlagValue(HalftoneRegionFlags.H_DEF_PIXEL);

            bitmap = new JBIG2Bitmap(regionBitmapWidth, regionBitmapHeight, arithmeticDecoder, huffmanDecoder, mmrDecoder);
            bitmap.Clear(halftoneDefaultPixel);

            bool enableSkip = halftoneRegionFlags.GetFlagValue(HalftoneRegionFlags.H_ENABLE_SKIP) != 0;

            JBIG2Bitmap skipBitmap = null;

            if (enableSkip)
            {
                skipBitmap = new JBIG2Bitmap(gridWidth, gridHeight, arithmeticDecoder, huffmanDecoder, mmrDecoder);
                skipBitmap.Clear(0);
                for (int y = 0; y < gridHeight; y++)
                {
                    for (int x = 0; x < gridWidth; x++)
                    {
                        int xx = gridX + y * stepY + x * stepX;
                        int yy = gridY + y * stepX - x * stepY;

                        if (((xx + patternWidth) >> 8) <= 0 || (xx >> 8) >= regionBitmapWidth || ((yy + patternHeight) >> 8) <= 0 || (yy >> 8) >= regionBitmapHeight)
                        {
                            skipBitmap.SetPixel(y, x, 1);
                        }
                    }
                }
            }

            int[] grayScaleImage = new int[gridWidth * gridHeight];

            short[] genericBAdaptiveTemplateX = new short[4], genericBAdaptiveTemplateY = new short[4];

            genericBAdaptiveTemplateX[0] = (short)(template <= 1 ? 3 : 2);
            genericBAdaptiveTemplateY[0] = -1;
            genericBAdaptiveTemplateX[1] = -3;
            genericBAdaptiveTemplateY[1] = -1;
            genericBAdaptiveTemplateX[2] = 2;
            genericBAdaptiveTemplateY[2] = -2;
            genericBAdaptiveTemplateX[3] = -2;
            genericBAdaptiveTemplateY[3] = -2;

            JBIG2Bitmap grayBitmap;

            for (int j = bitsPerValue - 1; j >= 0; --j)
            {
                grayBitmap = new JBIG2Bitmap(gridWidth, gridHeight, arithmeticDecoder, huffmanDecoder, mmrDecoder);

                grayBitmap.ReadBitmap(useMMR, template, false, enableSkip, skipBitmap, genericBAdaptiveTemplateX, genericBAdaptiveTemplateY, -1);

                i = 0;
                for (int row = 0; row < gridHeight; row++)
                {
                    for (int col = 0; col < gridWidth; col++)
                    {
                        int bit = grayBitmap.GetPixel(col, row) ^ grayScaleImage[i] & 1;
                        grayScaleImage[i] = (grayScaleImage[i] << 1) | bit;
                        i++;
                    }
                }
            }

            int combinationOperator = halftoneRegionFlags.GetFlagValue(HalftoneRegionFlags.H_COMB_OP);

            i = 0;
            for (int col = 0; col < gridHeight; col++)
            {
                int xx = gridX + col * stepY;
                int yy = gridY + col * stepX;
                for (int row = 0; row < gridWidth; row++)
                {
                    if (!(enableSkip && skipBitmap.GetPixel(col, row) == 1))
                    {
                        JBIG2Bitmap patternBitmap = patternDictionarySegment.GetBitmaps()[grayScaleImage[i]];
                        bitmap.Combine(patternBitmap, xx >> 8, yy >> 8, combinationOperator);
                    }

                    xx += stepX;
                    yy -= stepY;

                    i++;
                }
            }

            if (inlineImage)
            {
                PageInformationSegment pageSegment = decoder.FindPageSegement(segmentHeader.GetPageAssociation());
                JBIG2Bitmap            pageBitmap  = pageSegment.GetPageBitmap();

                int externalCombinationOperator = regionFlags.GetFlagValue(RegionFlags.EXTERNAL_COMBINATION_OPERATOR);
                pageBitmap.Combine(bitmap, regionBitmapXLocation, regionBitmapYLocation, externalCombinationOperator);
            }
            else
            {
                bitmap.SetBitmapNumber(GetSegmentHeader().GetSegmentNumber());
                decoder.AppendBitmap(bitmap);
            }
        }
Esempio n. 3
0
        public override void ReadSegment()
        {
            if (JBIG2StreamDecoder.debug)
            {
                Console.WriteLine("==== Reading Page Information Dictionary ====");
            }

            short[] buff = new short[4];
            decoder.Readbyte(buff);
            pageBitmapWidth = BinaryOperation.GetInt32(buff);

            buff = new short[4];
            decoder.Readbyte(buff);
            pageBitmapHeight = BinaryOperation.GetInt32(buff);

            if (JBIG2StreamDecoder.debug)
            {
                Console.WriteLine("Bitmap size = " + pageBitmapWidth + 'x' + pageBitmapHeight);
            }

            buff = new short[4];
            decoder.Readbyte(buff);
            xResolution = BinaryOperation.GetInt32(buff);

            buff = new short[4];
            decoder.Readbyte(buff);
            yResolution = BinaryOperation.GetInt32(buff);

            if (JBIG2StreamDecoder.debug)
            {
                Console.WriteLine("Resolution = " + xResolution + 'x' + yResolution);
            }

            /** extract page information flags */
            short pageInformationFlagsField = decoder.Readbyte();

            pageInformationFlags.SetFlags(pageInformationFlagsField);

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

            buff = new short[2];
            decoder.Readbyte(buff);
            pageStriping = BinaryOperation.GetInt16(buff);

            if (JBIG2StreamDecoder.debug)
            {
                Console.WriteLine("Page Striping = " + pageStriping);
            }

            int defPix = pageInformationFlags.GetFlagValue(PageInformationFlags.DEFAULT_PIXEL_VALUE);

            int height;

            if (pageBitmapHeight == -1)
            {
                height = pageStriping & 0x7fff;
            }
            else
            {
                height = pageBitmapHeight;
            }

            pageBitmap = new JBIG2Bitmap(pageBitmapWidth, height, arithmeticDecoder, huffmanDecoder, mmrDecoder);
            pageBitmap.Clear(defPix);
        }
Esempio n. 4
0
        public override void ReadSegment()
        {
            if (JBIG2StreamDecoder.debug)
            {
                Console.WriteLine("==== Reading Immediate Generic Region ====");
            }

            base.ReadSegment();

            /** read text region Segment flags */
            ReadGenericRegionFlags();

            bool useMMR   = genericRegionFlags.GetFlagValue(GenericRegionFlags.MMR) != 0;
            int  template = genericRegionFlags.GetFlagValue(GenericRegionFlags.GB_TEMPLATE);

            short[] genericBAdaptiveTemplateX = new short[4];
            short[] genericBAdaptiveTemplateY = new short[4];

            if (!useMMR)
            {
                if (template == 0)
                {
                    genericBAdaptiveTemplateX[0] = ReadATValue();
                    genericBAdaptiveTemplateY[0] = ReadATValue();
                    genericBAdaptiveTemplateX[1] = ReadATValue();
                    genericBAdaptiveTemplateY[1] = ReadATValue();
                    genericBAdaptiveTemplateX[2] = ReadATValue();
                    genericBAdaptiveTemplateY[2] = ReadATValue();
                    genericBAdaptiveTemplateX[3] = ReadATValue();
                    genericBAdaptiveTemplateY[3] = ReadATValue();
                }
                else
                {
                    genericBAdaptiveTemplateX[0] = ReadATValue();
                    genericBAdaptiveTemplateY[0] = ReadATValue();
                }

                arithmeticDecoder.ResetGenericStats(template, null);
                arithmeticDecoder.Start();
            }

            bool typicalPredictionGenericDecodingOn = genericRegionFlags.GetFlagValue(GenericRegionFlags.TPGDON) != 0;
            int  length = segmentHeader.GetSegmentDataLength();

            if (length == -1)
            {
                /**
                 * length of data is unknown, so it needs to be determined through examination of the data.
                 * See 7.2.7 - Segment data length of the JBIG2 specification.
                 */

                unknownLength = true;

                short match1;
                short match2;

                if (useMMR)
                {
                    // look for 0x00 0x00 (0, 0)

                    match1 = 0;
                    match2 = 0;
                }
                else
                {
                    // look for 0xFF 0xAC (255, 172)

                    match1 = 255;
                    match2 = 172;
                }

                int bytesRead = 0;
                while (true)
                {
                    short bite1 = decoder.Readbyte();
                    bytesRead++;

                    if (bite1 == match1)
                    {
                        short bite2 = decoder.Readbyte();
                        bytesRead++;

                        if (bite2 == match2)
                        {
                            length = bytesRead - 2;
                            break;
                        }
                    }
                }

                decoder.MovePointer(-bytesRead);
            }

            JBIG2Bitmap bitmap = new JBIG2Bitmap(regionBitmapWidth, regionBitmapHeight, arithmeticDecoder, huffmanDecoder, mmrDecoder);

            bitmap.Clear(0);
            bitmap.ReadBitmap(useMMR, template, typicalPredictionGenericDecodingOn, false, null, genericBAdaptiveTemplateX, genericBAdaptiveTemplateY, useMMR ? 0 : length - 18);



            if (inlineImage)
            {
                PageInformationSegment pageSegment = decoder.FindPageSegement(segmentHeader.GetPageAssociation());
                JBIG2Bitmap            pageBitmap  = pageSegment.GetPageBitmap();

                int extCombOp = regionFlags.GetFlagValue(RegionFlags.EXTERNAL_COMBINATION_OPERATOR);

                if (pageSegment.GetPageBitmapHeight() == -1 && regionBitmapYLocation + regionBitmapHeight > pageBitmap.GetHeight())
                {
                    pageBitmap.Expand(regionBitmapYLocation + regionBitmapHeight,
                                      pageSegment.GetPageInformationFlags().GetFlagValue(PageInformationFlags.DEFAULT_PIXEL_VALUE));
                }

                pageBitmap.Combine(bitmap, regionBitmapXLocation, regionBitmapYLocation, extCombOp);
            }
            else
            {
                bitmap.SetBitmapNumber(GetSegmentHeader().GetSegmentNumber());
                decoder.AppendBitmap(bitmap);
            }


            if (unknownLength)
            {
                decoder.MovePointer(4);
            }
        }