public static Bitmap extract()

in src/main/java/org/apache/pdfbox/jbig2/image/Bitmaps.java [310:359]


    public static Bitmap extract(final Rectangle roi, final Bitmap src)
    {
        final Bitmap dst = new Bitmap(roi.width, roi.height);

        final int upShift = roi.x & 0x07;
        final int downShift = 8 - upShift;
        int dstLineStartIdx = 0;

        final int padding = (8 - dst.getWidth() & 0x07);
        int srcLineStartIdx = src.getByteIndex(roi.x, roi.y);
        int srcLineEndIdx = src.getByteIndex(roi.x + roi.width - 1, roi.y);
        final boolean usePadding = dst.getRowStride() == srcLineEndIdx + 1 - srcLineStartIdx;

        for (int y = roi.y; y < roi.getMaxY(); y++)
        {
            int srcIdx = srcLineStartIdx;
            int dstIdx = dstLineStartIdx;

            if (srcLineStartIdx == srcLineEndIdx)
            {
                final byte pixels = (byte) (src.getByte(srcIdx) << upShift);
                dst.setByte(dstIdx, unpad(padding, pixels));
            }
            else if (upShift == 0)
            {
                for (int x = srcLineStartIdx; x <= srcLineEndIdx; x++)
                {
                    byte value = src.getByte(srcIdx++);

                    if (x == srcLineEndIdx && usePadding)
                    {
                        value = unpad(padding, value);
                    }

                    dst.setByte(dstIdx++, value);
                }
            }
            else
            {
                copyLine(src, dst, upShift, downShift, padding, srcLineStartIdx, srcLineEndIdx,
                        usePadding, srcIdx, dstIdx);
            }

            srcLineStartIdx += src.getRowStride();
            srcLineEndIdx += src.getRowStride();
            dstLineStartIdx += dst.getRowStride();
        }

        return dst;
    }