private void decodeTypicalPredictedLineTemplate1()

in src/main/java/org/apache/pdfbox/jbig2/segments/GenericRefinementRegion.java [684:815]


    private void decodeTypicalPredictedLineTemplate1(final int lineNumber, final int width,
            final int rowStride, final int refRowStride, final int paddedWidth,
            final int deltaRefStride, int byteIndex, final int currentLine, int refByteIndex)
            throws IOException
    {
        int context;
        int grReferenceValue;

        int previousLine;
        int previousReferenceLine;
        int currentReferenceLine;
        int nextReferenceLine;

        if (lineNumber > 0)
        {
            previousLine = regionBitmap.getByteAsInteger(byteIndex - rowStride);
        }
        else
        {
            previousLine = 0;
        }

        if (currentLine > 0 && currentLine <= referenceBitmap.getHeight())
        {
            previousReferenceLine = referenceBitmap
                    .getByteAsInteger(byteIndex - refRowStride + deltaRefStride) << 2;
        }
        else
        {
            previousReferenceLine = 0;
        }

        if (currentLine >= 0 && currentLine < referenceBitmap.getHeight())
        {
            currentReferenceLine = referenceBitmap.getByteAsInteger(byteIndex + deltaRefStride);
        }
        else
        {
            currentReferenceLine = 0;
        }

        if (currentLine > -2 && currentLine < (referenceBitmap.getHeight() - 1))
        {
            nextReferenceLine = referenceBitmap
                    .getByteAsInteger(byteIndex + refRowStride + deltaRefStride);
        }
        else
        {
            nextReferenceLine = 0;
        }

        context = ((previousLine >> 5) & 0x6) | ((nextReferenceLine >> 2) & 0x30)
                | (currentReferenceLine & 0xc0) | (previousReferenceLine & 0x200);

        grReferenceValue = ((nextReferenceLine >> 2) & 0x70) | (currentReferenceLine & 0xc0)
                | (previousReferenceLine & 0x700);

        int nextByte;
        for (int x = 0; x < paddedWidth; x = nextByte)
        {
            byte result = 0;
            nextByte = x + 8;
            final int minorWidth = width - x > 8 ? 8 : width - x;
            final boolean readNextByte = nextByte < width;
            final boolean refReadNextByte = nextByte < referenceBitmap.getWidth();

            final int yOffset = deltaRefStride + 1;

            if (lineNumber > 0)
            {
                previousLine = (previousLine << 8) | (readNextByte
                        ? regionBitmap.getByteAsInteger(byteIndex - rowStride + 1) : 0);
            }

            if (currentLine > 0 && currentLine <= referenceBitmap.getHeight())
            {
                previousReferenceLine = (previousReferenceLine << 8)
                        | (refReadNextByte ? referenceBitmap
                                .getByteAsInteger(refByteIndex - refRowStride + yOffset) << 2 : 0);
            }

            if (currentLine >= 0 && currentLine < referenceBitmap.getHeight())
            {
                currentReferenceLine = (currentReferenceLine << 8) | (refReadNextByte
                        ? referenceBitmap.getByteAsInteger(refByteIndex + yOffset) : 0);
            }

            if (currentLine > -2 && currentLine < (referenceBitmap.getHeight() - 1))
            {
                nextReferenceLine = (nextReferenceLine << 8) | (refReadNextByte
                        ? referenceBitmap.getByteAsInteger(refByteIndex + refRowStride + yOffset)
                        : 0);
            }

            for (int minorX = 0; minorX < minorWidth; minorX++)
            {
                int bit = 0;

                // i)
                final int bitmapValue = (grReferenceValue >> 4) & 0x1ff;

                if (bitmapValue == 0x1ff)
                {
                    bit = 1;
                }
                else if (bitmapValue == 0x00)
                {
                    bit = 0;
                }
                else
                {
                    cx.setIndex(context);
                    bit = arithDecoder.decode(cx);
                }

                final int toShift = 7 - minorX;
                result |= bit << toShift;

                context = ((context & 0x0d6) << 1) | bit | ((previousLine >> toShift + 5) & 0x002)
                        | ((nextReferenceLine >> toShift + 2) & 0x010)
                        | ((currentReferenceLine >> toShift) & 0x040)
                        | ((previousReferenceLine >> toShift) & 0x200);

                grReferenceValue = ((grReferenceValue & 0x0db) << 1)
                        | ((nextReferenceLine >> toShift + 2) & 0x010)
                        | ((currentReferenceLine >> toShift) & 0x080)
                        | ((previousReferenceLine >> toShift) & 0x400);
            }
            regionBitmap.setByte(byteIndex++, result);
            refByteIndex++;
        }
    }