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

            base.readSegment();

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

            short[] buff = new short[4];
            decoder.readbyte(buff);
            long noOfSymbolInstances = BinaryOperation.getInt32(buff);

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

            int noOfReferredToSegments = segmentHeader.getReferredToSegmentCount();

            int[] referredToSegments = segmentHeader.getReferredToSegments();

            //List codeTables = new ArrayList();
            List <Segment> segmentsReferenced = new List <Segment>();
            long           noOfSymbols        = 0;

            if (JBIG2StreamDecoder.debug)
            {
                Console.WriteLine("noOfReferredToSegments = " + noOfReferredToSegments);
            }
            int i = 0;

            for (i = 0; i < noOfReferredToSegments; i++)
            {
                Segment seg  = decoder.findSegment(referredToSegments[i]);
                int     type = seg.getSegmentHeader().getSegmentType();

                if (type == Segment.SYMBOL_DICTIONARY)
                {
                    segmentsReferenced.Add(seg);
                    noOfSymbols += ((SymbolDictionarySegment)seg).getNoOfExportedSymbols();
                }
                else if (type == Segment.TABLES)
                {
                    //codeTables.add(seg);
                }
            }

            long symbolCodeLength = 0;
            int  count            = 1;

            while (count < noOfSymbols)
            {
                symbolCodeLength++;
                count <<= 1;
            }

            int currentSymbol = 0;

            JBIG2Bitmap[] symbols = new JBIG2Bitmap[noOfSymbols];
            foreach (Segment it in segmentsReferenced)
            {
                if (it.getSegmentHeader().getSegmentType() == Segment.SYMBOL_DICTIONARY)
                {
                    JBIG2Bitmap[] bitmaps = ((SymbolDictionarySegment)it).getBitmaps();
                    for (int j = 0; j < bitmaps.Length; j++)
                    {
                        symbols[currentSymbol] = bitmaps[j];
                        currentSymbol++;
                    }
                }
            }

            long[,] huffmanFSTable = null;
            long[,] huffmanDSTable = null;
            long[,] huffmanDTTable = null;

            long[,] huffmanRDWTable = null;
            long[,] huffmanRDHTable = null;

            long[,] huffmanRDXTable   = null;
            long[,] huffmanRDYTable   = null;
            long[,] huffmanRSizeTable = null;

            bool sbHuffman = textRegionFlags.getFlagValue(TextRegionFlags.SB_HUFF) != 0;

            i = 0;
            if (sbHuffman)
            {
                int sbHuffFS = textRegionHuffmanFlags.getFlagValue(TextRegionHuffmanFlags.SB_HUFF_FS);
                if (sbHuffFS == 0)
                {
                    huffmanFSTable = HuffmanDecoder.huffmanTableF;
                }
                else if (sbHuffFS == 1)
                {
                    huffmanFSTable = HuffmanDecoder.huffmanTableG;
                }
                else
                {
                }

                int sbHuffDS = textRegionHuffmanFlags.getFlagValue(TextRegionHuffmanFlags.SB_HUFF_DS);
                if (sbHuffDS == 0)
                {
                    huffmanDSTable = HuffmanDecoder.huffmanTableH;
                }
                else if (sbHuffDS == 1)
                {
                    huffmanDSTable = HuffmanDecoder.huffmanTableI;
                }
                else if (sbHuffDS == 2)
                {
                    huffmanDSTable = HuffmanDecoder.huffmanTableJ;
                }
                else
                {
                }

                int sbHuffDT = textRegionHuffmanFlags.getFlagValue(TextRegionHuffmanFlags.SB_HUFF_DT);
                if (sbHuffDT == 0)
                {
                    huffmanDTTable = HuffmanDecoder.huffmanTableK;
                }
                else if (sbHuffDT == 1)
                {
                    huffmanDTTable = HuffmanDecoder.huffmanTableL;
                }
                else if (sbHuffDT == 2)
                {
                    huffmanDTTable = HuffmanDecoder.huffmanTableM;
                }
                else
                {
                }

                int sbHuffRDW = textRegionHuffmanFlags.getFlagValue(TextRegionHuffmanFlags.SB_HUFF_RDW);
                if (sbHuffRDW == 0)
                {
                    huffmanRDWTable = HuffmanDecoder.huffmanTableN;
                }
                else if (sbHuffRDW == 1)
                {
                    huffmanRDWTable = HuffmanDecoder.huffmanTableO;
                }
                else
                {
                }

                int sbHuffRDH = textRegionHuffmanFlags.getFlagValue(TextRegionHuffmanFlags.SB_HUFF_RDH);
                if (sbHuffRDH == 0)
                {
                    huffmanRDHTable = HuffmanDecoder.huffmanTableN;
                }
                else if (sbHuffRDH == 1)
                {
                    huffmanRDHTable = HuffmanDecoder.huffmanTableO;
                }
                else
                {
                }

                int sbHuffRDX = textRegionHuffmanFlags.getFlagValue(TextRegionHuffmanFlags.SB_HUFF_RDX);
                if (sbHuffRDX == 0)
                {
                    huffmanRDXTable = HuffmanDecoder.huffmanTableN;
                }
                else if (sbHuffRDX == 1)
                {
                    huffmanRDXTable = HuffmanDecoder.huffmanTableO;
                }
                else
                {
                }

                int sbHuffRDY = textRegionHuffmanFlags.getFlagValue(TextRegionHuffmanFlags.SB_HUFF_RDY);
                if (sbHuffRDY == 0)
                {
                    huffmanRDYTable = HuffmanDecoder.huffmanTableN;
                }
                else if (sbHuffRDY == 1)
                {
                    huffmanRDYTable = HuffmanDecoder.huffmanTableO;
                }
                else
                {
                }

                int sbHuffRSize = textRegionHuffmanFlags.getFlagValue(TextRegionHuffmanFlags.SB_HUFF_RSIZE);
                if (sbHuffRSize == 0)
                {
                    huffmanRSizeTable = HuffmanDecoder.huffmanTableA;
                }
                else
                {
                }
            }

            long[][] runLengthTable  = new long[36][];
            long[][] symbolCodeTable = new long[noOfSymbols + 1][];
            if (sbHuffman)
            {
                decoder.consumeRemainingBits();

                for (i = 0; i < 32; i++)
                {
                    runLengthTable[i] = new long[] { i, decoder.readBits(4), 0, 0 };
                }

                runLengthTable[32] = new long[] { 0x103, decoder.readBits(4), 2, 0 };

                runLengthTable[33] = new long[] { 0x203, decoder.readBits(4), 3, 0 };

                runLengthTable[34] = new long[] { 0x20b, decoder.readBits(4), 7, 0 };

                runLengthTable[35] = new long[] { 0, 0, HuffmanDecoder.jbig2HuffmanEOT };

                runLengthTable = HuffmanDecoder.buildTable(runLengthTable, 35);

                for (i = 0; i < noOfSymbols; i++)
                {
                    symbolCodeTable[i] = new long[] { i, 0, 0, 0 };
                }

                i = 0;
                while (i < noOfSymbols)
                {
                    long j = huffmanDecoder.decodeInt(runLengthTable).intResult();
                    if (j > 0x200)
                    {
                        for (j -= 0x200; j != 0 && i < noOfSymbols; j--)
                        {
                            symbolCodeTable[i++][1] = 0;
                        }
                    }
                    else if (j > 0x100)
                    {
                        for (j -= 0x100; j != 0 && i < noOfSymbols; j--)
                        {
                            symbolCodeTable[i][1] = symbolCodeTable[i - 1][1];
                            i++;
                        }
                    }
                    else
                    {
                        symbolCodeTable[i++][1] = j;
                    }
                }

                symbolCodeTable[noOfSymbols][1] = 0;
                symbolCodeTable[noOfSymbols][2] = HuffmanDecoder.jbig2HuffmanEOT;
                symbolCodeTable = HuffmanDecoder.buildTable(symbolCodeTable, (int)noOfSymbols);

                decoder.consumeRemainingBits();
            }
            else
            {
                symbolCodeTable = null;
                arithmeticDecoder.resetIntStats((int)symbolCodeLength);
                arithmeticDecoder.start();
            }

            bool symbolRefine        = textRegionFlags.getFlagValue(TextRegionFlags.SB_REFINE) != 0;
            long logStrips           = textRegionFlags.getFlagValue(TextRegionFlags.LOG_SB_STRIPES);
            int  defaultPixel        = textRegionFlags.getFlagValue(TextRegionFlags.SB_DEF_PIXEL);
            int  combinationOperator = textRegionFlags.getFlagValue(TextRegionFlags.SB_COMB_OP);
            bool transposed          = textRegionFlags.getFlagValue(TextRegionFlags.TRANSPOSED) != 0;
            int  referenceCorner     = textRegionFlags.getFlagValue(TextRegionFlags.REF_CORNER);
            int  sOffset             = textRegionFlags.getFlagValue(TextRegionFlags.SB_DS_OFFSET);
            int  template            = textRegionFlags.getFlagValue(TextRegionFlags.SB_R_TEMPLATE);

            if (symbolRefine)
            {
                arithmeticDecoder.resetRefinementStats(template, null);
            }

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

            bitmap.readTextRegion2(sbHuffman, symbolRefine, noOfSymbolInstances, logStrips, noOfSymbols, symbolCodeTable, symbolCodeLength, symbols, defaultPixel, combinationOperator, transposed, referenceCorner, sOffset, huffmanFSTable, huffmanDSTable, huffmanDTTable, huffmanRDWTable, huffmanRDHTable, huffmanRDXTable, huffmanRDYTable, huffmanRSizeTable, template, symbolRegionAdaptiveTemplateX, symbolRegionAdaptiveTemplateY, decoder);

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

                if (JBIG2StreamDecoder.debug)
                {
                    Console.WriteLine(pageBitmap + " " + bitmap);
                }

                int externalCombinationOperator = regionFlags.getFlagValue(RegionFlags.EXTERNAL_COMBINATION_OPERATOR);
                pageBitmap.combine(bitmap, regionBitmapXLocation, regionBitmapYLocation, externalCombinationOperator);
            }
            else
            {
                bitmap.setBitmapNumber(getSegmentHeader().getSegmentNumber());
                decoder.appendBitmap(bitmap);
            }

            decoder.consumeRemainingBits();
        }
        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);
            }
        }
Beispiel #3
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);
            }
        }
        private void readSegments()
        {
            bool finished = false;

            while (!reader.isFinished() && !finished)
            {
                SegmentHeader segmentHeader = new SegmentHeader();
                readSegmentHeader(segmentHeader);

                // read the Segment data
                Segment segment = null;

                int   segmentType            = segmentHeader.getSegmentType();
                int[] referredToSegments     = segmentHeader.getReferredToSegments();
                int   noOfReferredToSegments = segmentHeader.getReferredToSegmentCount();

                switch (segmentType)
                {
                case Segment.SYMBOL_DICTIONARY:

                    segment = new SymbolDictionarySegment(this);

                    segment.setSegmentHeader(segmentHeader);

                    break;

                case Segment.INTERMEDIATE_TEXT_REGION:

                    segment = new TextRegionSegment(this, false);

                    segment.setSegmentHeader(segmentHeader);

                    break;

                case Segment.IMMEDIATE_TEXT_REGION:

                    segment = new TextRegionSegment(this, true);

                    segment.setSegmentHeader(segmentHeader);

                    break;

                case Segment.IMMEDIATE_LOSSLESS_TEXT_REGION:

                    segment = new TextRegionSegment(this, true);

                    segment.setSegmentHeader(segmentHeader);

                    break;

                case Segment.PATTERN_DICTIONARY:

                    segment = new PatternDictionarySegment(this);

                    segment.setSegmentHeader(segmentHeader);

                    break;

                case Segment.INTERMEDIATE_HALFTONE_REGION:

                    segment = new HalftoneRegionSegment(this, false);

                    segment.setSegmentHeader(segmentHeader);

                    break;

                case Segment.IMMEDIATE_HALFTONE_REGION:

                    segment = new HalftoneRegionSegment(this, true);

                    segment.setSegmentHeader(segmentHeader);

                    break;

                case Segment.IMMEDIATE_LOSSLESS_HALFTONE_REGION:

                    segment = new HalftoneRegionSegment(this, true);

                    segment.setSegmentHeader(segmentHeader);

                    break;

                case Segment.INTERMEDIATE_GENERIC_REGION:

                    segment = new GenericRegionSegment(this, false);

                    segment.setSegmentHeader(segmentHeader);

                    break;

                case Segment.IMMEDIATE_GENERIC_REGION:

                    segment = new GenericRegionSegment(this, true);

                    segment.setSegmentHeader(segmentHeader);

                    break;

                case Segment.IMMEDIATE_LOSSLESS_GENERIC_REGION:

                    segment = new GenericRegionSegment(this, true);

                    segment.setSegmentHeader(segmentHeader);

                    break;

                case Segment.INTERMEDIATE_GENERIC_REFINEMENT_REGION:

                    segment = new RefinementRegionSegment(this, false, referredToSegments, noOfReferredToSegments);

                    segment.setSegmentHeader(segmentHeader);

                    break;

                case Segment.IMMEDIATE_GENERIC_REFINEMENT_REGION:

                    segment = new RefinementRegionSegment(this, true, referredToSegments, noOfReferredToSegments);

                    segment.setSegmentHeader(segmentHeader);

                    break;

                case Segment.IMMEDIATE_LOSSLESS_GENERIC_REFINEMENT_REGION:

                    segment = new RefinementRegionSegment(this, true, referredToSegments, noOfReferredToSegments);

                    segment.setSegmentHeader(segmentHeader);

                    break;

                case Segment.PAGE_INFORMATION:

                    segment = new PageInformationSegment(this);

                    segment.setSegmentHeader(segmentHeader);

                    break;

                case Segment.END_OF_PAGE:
                    continue;

                case Segment.END_OF_STRIPE:

                    segment = new EndOfStripeSegment(this);

                    segment.setSegmentHeader(segmentHeader);
                    break;

                case Segment.END_OF_FILE:

                    finished = true;

                    continue;

                case Segment.PROFILES:
                    break;

                case Segment.TABLES:
                    break;

                case Segment.EXTENSION:

                    segment = new ExtensionSegment(this);

                    segment.setSegmentHeader(segmentHeader);

                    break;

                default:
                    break;
                }

                if (!randomAccessOrganisation)
                {
                    segment.readSegment();
                }
                segments.Add(segment);
            }

            if (randomAccessOrganisation)
            {
                foreach (Segment segment in segments)
                {
                    segment.readSegment();
                }
            }
        }
        public override void readSegment()
        {
            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);
            }
        }