public void readSegment()
        {
            if (JBIG2StreamDecoder.debug)
            {
                Console.WriteLine("==== Reading Generic Refinement Region ====");
            }

            base.readSegment();

            /** read text region segment flags */
            readGenericRegionFlags();

            short[] genericRegionAdaptiveTemplateX = new short[2];
            short[] genericRegionAdaptiveTemplateY = new short[2];

            int template = refinementRegionFlags.getFlagValue(RefinementRegionFlags.GR_TEMPLATE);

            if (template == 0)
            {
                genericRegionAdaptiveTemplateX[0] = readATValue();
                genericRegionAdaptiveTemplateY[0] = readATValue();
                genericRegionAdaptiveTemplateX[1] = readATValue();
                genericRegionAdaptiveTemplateY[1] = readATValue();
            }

            if (noOfReferedToSegments == 0 || inlineImage)
            {
                PageInformationSegment pageSegment = decoder.findPageSegement(segmentHeader.getPageAssociation());
                JBIG2Bitmap            pageBitmap  = pageSegment.getPageBitmap();

                if (pageSegment.getPageBitmapHeight() == -1 && regionBitmapYLocation + regionBitmapHeight > pageBitmap.getHeight())
                {
                    pageBitmap.expand(regionBitmapYLocation + regionBitmapHeight, pageSegment.getPageInformationFlags().getFlagValue(PageInformationFlags.DEFAULT_PIXEL_VALUE));
                }
            }

            if (noOfReferedToSegments > 1)
            {
                if (JBIG2StreamDecoder.debug)
                {
                    Console.WriteLine("Bad reference in JBIG2 generic refinement Segment");
                }

                return;
            }

            JBIG2Bitmap referedToBitmap;

            if (noOfReferedToSegments == 1)
            {
                referedToBitmap = decoder.findBitmap(referedToSegments[0]);
            }
            else
            {
                PageInformationSegment pageSegment = decoder.findPageSegement(segmentHeader.getPageAssociation());
                JBIG2Bitmap            pageBitmap  = pageSegment.getPageBitmap();

                referedToBitmap = pageBitmap.getSlice(regionBitmapXLocation, regionBitmapYLocation, regionBitmapWidth, regionBitmapHeight);
            }

            arithmeticDecoder.resetRefinementStats(template, null);
            arithmeticDecoder.start();

            bool typicalPredictionGenericRefinementOn = refinementRegionFlags.getFlagValue(RefinementRegionFlags.TPGDON) != 0;

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

            bitmap.readGenericRefinementRegion(template, typicalPredictionGenericRefinementOn, referedToBitmap, 0, 0, genericRegionAdaptiveTemplateX, genericRegionAdaptiveTemplateY);

            if (inlineImage)
            {
                PageInformationSegment pageSegment = decoder.findPageSegement(segmentHeader.getPageAssociation());
                JBIG2Bitmap            pageBitmap  = pageSegment.getPageBitmap();

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

                pageBitmap.combine(bitmap, regionBitmapXLocation, regionBitmapYLocation, extCombOp);
            }
            else
            {
                bitmap.setBitmapNumber(getSegmentHeader().getSegmentNumber());
                decoder.appendBitmap(bitmap);
            }
        }
Exemple #2
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);
            }
        }