Search in sources :

Example 1 with PDDeviceGray

use of org.apache.pdfbox.pdmodel.graphics.color.PDDeviceGray in project pdfbox by apache.

the class SampledImageReader method from1Bit.

private static BufferedImage from1Bit(PDImage pdImage, Rectangle clipped, final int subsampling, final int width, final int height) throws IOException {
    int currentSubsampling = subsampling;
    final PDColorSpace colorSpace = pdImage.getColorSpace();
    final float[] decode = getDecodeArray(pdImage);
    BufferedImage bim = null;
    WritableRaster raster;
    byte[] output;
    DecodeOptions options = new DecodeOptions(currentSubsampling);
    options.setSourceRegion(clipped);
    // read bit stream
    try (InputStream iis = pdImage.createInputStream(options)) {
        final int inputWidth;
        final int startx;
        final int starty;
        final int scanWidth;
        final int scanHeight;
        if (options.isFilterSubsampled()) {
            // Decode options were honored, and so there is no need for additional clipping or subsampling
            inputWidth = width;
            startx = 0;
            starty = 0;
            scanWidth = width;
            scanHeight = height;
            currentSubsampling = 1;
        } else {
            // Decode options not honored, so we need to clip and subsample ourselves.
            inputWidth = pdImage.getWidth();
            startx = clipped.x;
            starty = clipped.y;
            scanWidth = clipped.width;
            scanHeight = clipped.height;
        }
        if (colorSpace instanceof PDDeviceGray) {
            // TYPE_BYTE_GRAY and not TYPE_BYTE_BINARY because this one is handled
            // without conversion to RGB by Graphics.drawImage
            // this reduces the memory footprint, only one byte per pixel instead of three.
            bim = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
            raster = bim.getRaster();
        } else {
            raster = Raster.createBandedRaster(DataBuffer.TYPE_BYTE, width, height, 1, new Point(0, 0));
        }
        output = ((DataBufferByte) raster.getDataBuffer()).getData();
        final boolean isIndexed = colorSpace instanceof PDIndexed;
        int rowLen = inputWidth / 8;
        if (inputWidth % 8 > 0) {
            rowLen++;
        }
        // read stream
        byte value0;
        byte value1;
        if (isIndexed || decode[0] < decode[1]) {
            value0 = 0;
            value1 = (byte) 255;
        } else {
            value0 = (byte) 255;
            value1 = 0;
        }
        byte[] buff = new byte[rowLen];
        int idx = 0;
        for (int y = 0; y < starty + scanHeight; y++) {
            int x = 0;
            int readLen = iis.read(buff);
            if (y < starty || y % currentSubsampling > 0) {
                continue;
            }
            for (int r = 0; r < rowLen && r < readLen; r++) {
                int value = buff[r];
                int mask = 128;
                for (int i = 0; i < 8; i++) {
                    if (x >= startx + scanWidth) {
                        break;
                    }
                    int bit = value & mask;
                    mask >>= 1;
                    if (x >= startx && x % currentSubsampling == 0) {
                        output[idx++] = bit == 0 ? value0 : value1;
                    }
                    x++;
                }
            }
            if (readLen != rowLen) {
                LOG.warn("premature EOF, image will be incomplete");
                break;
            }
        }
        if (bim != null) {
            return bim;
        }
        // use the color space to convert the image to RGB
        return colorSpace.toRGBImage(raster);
    }
}
Also used : PDIndexed(org.apache.pdfbox.pdmodel.graphics.color.PDIndexed) ImageInputStream(javax.imageio.stream.ImageInputStream) MemoryCacheImageInputStream(javax.imageio.stream.MemoryCacheImageInputStream) InputStream(java.io.InputStream) Point(java.awt.Point) Point(java.awt.Point) Paint(java.awt.Paint) BufferedImage(java.awt.image.BufferedImage) DecodeOptions(org.apache.pdfbox.filter.DecodeOptions) PDDeviceGray(org.apache.pdfbox.pdmodel.graphics.color.PDDeviceGray) WritableRaster(java.awt.image.WritableRaster) PDColorSpace(org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace)

Example 2 with PDDeviceGray

use of org.apache.pdfbox.pdmodel.graphics.color.PDDeviceGray in project pdfbox by apache.

the class PreflightStreamEngine method validateDefaultColorSpace.

/**
 * In some cases, the colorspace isn't checked because defaults (/DeviceGray) is used. Thus we
 * need to check all text output, stroke and fill for /DeviceGray.
 *
 * @param operator an operator.
 * @throws ContentStreamException
 */
void validateDefaultColorSpace(Operator operator) throws ContentStreamException {
    boolean v = false;
    String op = operator.getName();
    if ("Tj".equals(op) || "TJ".equals(op) || "'".equals(op) || "\"".equals(op)) {
        RenderingMode rm = getGraphicsState().getTextState().getRenderingMode();
        if (rm.isFill() && getGraphicsState().getNonStrokingColor().getColorSpace() instanceof PDDeviceGray) {
            v = true;
        }
        if (rm.isStroke() && getGraphicsState().getStrokingColor().getColorSpace() instanceof PDDeviceGray) {
            v = true;
        }
    }
    // fills
    if (("f".equals(op) || "F".equals(op) || "f*".equals(op) || "B".equals(op) || "B*".equals(op) || "b".equals(op) || "b*".equals(op)) && getGraphicsState().getNonStrokingColor().getColorSpace() instanceof PDDeviceGray) {
        v = true;
    }
    // strokes
    if (("B".equals(op) || "B*".equals(op) || "b".equals(op) || "b*".equals(op) || "s".equals(op) || "S".equals(op)) && getGraphicsState().getStrokingColor().getColorSpace() instanceof PDDeviceGray) {
        v = true;
    }
    if (v && !validColorSpaceDestOutputProfile(PreflightStreamEngine.ColorSpaceType.ALL)) {
        registerError("/DeviceGray default for operator \"" + op + "\" can't be used without Color Profile", ERROR_GRAPHIC_INVALID_COLOR_SPACE_MISSING);
    }
}
Also used : PDDeviceGray(org.apache.pdfbox.pdmodel.graphics.color.PDDeviceGray) SetTextRenderingMode(org.apache.pdfbox.contentstream.operator.text.SetTextRenderingMode) RenderingMode(org.apache.pdfbox.pdmodel.graphics.state.RenderingMode) COSString(org.apache.pdfbox.cos.COSString)

Aggregations

PDDeviceGray (org.apache.pdfbox.pdmodel.graphics.color.PDDeviceGray)2 Paint (java.awt.Paint)1 Point (java.awt.Point)1 BufferedImage (java.awt.image.BufferedImage)1 WritableRaster (java.awt.image.WritableRaster)1 InputStream (java.io.InputStream)1 ImageInputStream (javax.imageio.stream.ImageInputStream)1 MemoryCacheImageInputStream (javax.imageio.stream.MemoryCacheImageInputStream)1 SetTextRenderingMode (org.apache.pdfbox.contentstream.operator.text.SetTextRenderingMode)1 COSString (org.apache.pdfbox.cos.COSString)1 DecodeOptions (org.apache.pdfbox.filter.DecodeOptions)1 PDColorSpace (org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace)1 PDIndexed (org.apache.pdfbox.pdmodel.graphics.color.PDIndexed)1 RenderingMode (org.apache.pdfbox.pdmodel.graphics.state.RenderingMode)1