Search in sources :

Example 1 with PDSoftMask

use of org.apache.pdfbox.pdmodel.graphics.state.PDSoftMask in project pdfbox by apache.

the class PageDrawer method applySoftMaskToPaint.

// TODO: move soft mask apply to getPaint()?
private Paint applySoftMaskToPaint(Paint parentPaint, PDSoftMask softMask) throws IOException {
    if (softMask == null || softMask.getGroup() == null) {
        return parentPaint;
    }
    PDColor backdropColor = null;
    if (COSName.LUMINOSITY.equals(softMask.getSubType())) {
        COSArray backdropColorArray = softMask.getBackdropColor();
        PDColorSpace colorSpace = softMask.getGroup().getGroup().getColorSpace();
        if (colorSpace != null && backdropColorArray != null) {
            backdropColor = new PDColor(backdropColorArray, colorSpace);
        }
    }
    TransparencyGroup transparencyGroup = new TransparencyGroup(softMask.getGroup(), true, softMask.getInitialTransformationMatrix(), backdropColor);
    BufferedImage image = transparencyGroup.getImage();
    if (image == null) {
        // sample file: PDFJS-6967_reduced_outside_softmask.pdf
        return parentPaint;
    }
    BufferedImage gray = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
    if (COSName.ALPHA.equals(softMask.getSubType())) {
        gray.setData(image.getAlphaRaster());
    } else if (COSName.LUMINOSITY.equals(softMask.getSubType())) {
        Graphics g = gray.getGraphics();
        g.drawImage(image, 0, 0, null);
        g.dispose();
    } else {
        throw new IOException("Invalid soft mask subtype.");
    }
    gray = getRotatedImage(gray);
    Rectangle2D tpgBounds = transparencyGroup.getBounds();
    adjustRectangle(tpgBounds);
    return new SoftMask(parentPaint, gray, tpgBounds, backdropColor, softMask.getTransferFunction());
}
Also used : Graphics(java.awt.Graphics) PDTransparencyGroup(org.apache.pdfbox.pdmodel.graphics.form.PDTransparencyGroup) COSArray(org.apache.pdfbox.cos.COSArray) Rectangle2D(java.awt.geom.Rectangle2D) IOException(java.io.IOException) PDSoftMask(org.apache.pdfbox.pdmodel.graphics.state.PDSoftMask) BufferedImage(java.awt.image.BufferedImage) PDColor(org.apache.pdfbox.pdmodel.graphics.color.PDColor) PDColorSpace(org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace)

Example 2 with PDSoftMask

use of org.apache.pdfbox.pdmodel.graphics.state.PDSoftMask in project pdfbox by apache.

the class PageDrawer method drawBufferedImage.

private void drawBufferedImage(BufferedImage image, AffineTransform at) throws IOException {
    graphics.setComposite(getGraphicsState().getNonStrokingJavaComposite());
    setClip();
    AffineTransform imageTransform = new AffineTransform(at);
    PDSoftMask softMask = getGraphicsState().getSoftMask();
    if (softMask != null) {
        imageTransform.scale(1, -1);
        imageTransform.translate(0, -1);
        Paint awtPaint = new TexturePaint(image, new Rectangle2D.Double(imageTransform.getTranslateX(), imageTransform.getTranslateY(), imageTransform.getScaleX(), imageTransform.getScaleY()));
        awtPaint = applySoftMaskToPaint(awtPaint, softMask);
        graphics.setPaint(awtPaint);
        Rectangle2D unitRect = new Rectangle2D.Float(0, 0, 1, 1);
        graphics.fill(at.createTransformedShape(unitRect));
    } else {
        COSBase transfer = getGraphicsState().getTransfer();
        if (transfer instanceof COSArray || transfer instanceof COSDictionary) {
            image = applyTransferFunction(image, transfer);
        }
        int width = image.getWidth();
        int height = image.getHeight();
        imageTransform.scale(1.0 / width, -1.0 / height);
        imageTransform.translate(0, -height);
        graphics.drawImage(image, imageTransform, null);
    }
}
Also used : PDSoftMask(org.apache.pdfbox.pdmodel.graphics.state.PDSoftMask) TexturePaint(java.awt.TexturePaint) COSArray(org.apache.pdfbox.cos.COSArray) COSDictionary(org.apache.pdfbox.cos.COSDictionary) Rectangle2D(java.awt.geom.Rectangle2D) AffineTransform(java.awt.geom.AffineTransform) COSBase(org.apache.pdfbox.cos.COSBase) Paint(java.awt.Paint) TexturePaint(java.awt.TexturePaint) Point(java.awt.Point) Paint(java.awt.Paint) TexturePaint(java.awt.TexturePaint)

Example 3 with PDSoftMask

use of org.apache.pdfbox.pdmodel.graphics.state.PDSoftMask in project pdfbox by apache.

the class PageDrawer method showTransparencyGroup.

@Override
public void showTransparencyGroup(PDTransparencyGroup form) throws IOException {
    TransparencyGroup group = new TransparencyGroup(form, false, getGraphicsState().getCurrentTransformationMatrix(), null);
    BufferedImage image = group.getImage();
    if (image == null) {
        // image is empty, don't bother
        return;
    }
    graphics.setComposite(getGraphicsState().getNonStrokingJavaComposite());
    setClip();
    // both the DPI xform and the CTM were already applied to the group, so all we do
    // here is draw it directly onto the Graphics2D device at the appropriate position
    PDRectangle bbox = group.getBBox();
    AffineTransform prev = graphics.getTransform();
    Matrix m = new Matrix(xform);
    float xScale = Math.abs(m.getScalingFactorX());
    float yScale = Math.abs(m.getScalingFactorY());
    // adjust the initial translation (includes the translation used to "help" the rotation)
    graphics.setTransform(AffineTransform.getTranslateInstance(xform.getTranslateX(), xform.getTranslateY()));
    graphics.rotate(Math.toRadians(pageRotation));
    // adjust bbox (x,y) position at the initial scale + cropbox
    float x = bbox.getLowerLeftX() - pageSize.getLowerLeftX();
    float y = pageSize.getUpperRightY() - bbox.getUpperRightY();
    if (flipTG) {
        graphics.translate(0, image.getHeight());
        graphics.scale(1, -1);
    } else {
        graphics.translate(x * xScale, y * yScale);
    }
    PDSoftMask softMask = getGraphicsState().getSoftMask();
    if (softMask != null) {
        Paint awtPaint = new TexturePaint(image, new Rectangle2D.Float(0, 0, image.getWidth(), image.getHeight()));
        awtPaint = applySoftMaskToPaint(awtPaint, softMask);
        graphics.setPaint(awtPaint);
        graphics.fill(new Rectangle2D.Float(0, 0, bbox.getWidth() * xScale, bbox.getHeight() * yScale));
    } else {
        graphics.drawImage(image, null, null);
    }
    graphics.setTransform(prev);
}
Also used : PDSoftMask(org.apache.pdfbox.pdmodel.graphics.state.PDSoftMask) TexturePaint(java.awt.TexturePaint) PDTransparencyGroup(org.apache.pdfbox.pdmodel.graphics.form.PDTransparencyGroup) Matrix(org.apache.pdfbox.util.Matrix) Rectangle2D(java.awt.geom.Rectangle2D) AffineTransform(java.awt.geom.AffineTransform) PDRectangle(org.apache.pdfbox.pdmodel.common.PDRectangle) Paint(java.awt.Paint) TexturePaint(java.awt.TexturePaint) BufferedImage(java.awt.image.BufferedImage)

Aggregations

Rectangle2D (java.awt.geom.Rectangle2D)3 PDSoftMask (org.apache.pdfbox.pdmodel.graphics.state.PDSoftMask)3 Paint (java.awt.Paint)2 TexturePaint (java.awt.TexturePaint)2 AffineTransform (java.awt.geom.AffineTransform)2 BufferedImage (java.awt.image.BufferedImage)2 COSArray (org.apache.pdfbox.cos.COSArray)2 PDTransparencyGroup (org.apache.pdfbox.pdmodel.graphics.form.PDTransparencyGroup)2 Graphics (java.awt.Graphics)1 Point (java.awt.Point)1 IOException (java.io.IOException)1 COSBase (org.apache.pdfbox.cos.COSBase)1 COSDictionary (org.apache.pdfbox.cos.COSDictionary)1 PDRectangle (org.apache.pdfbox.pdmodel.common.PDRectangle)1 PDColor (org.apache.pdfbox.pdmodel.graphics.color.PDColor)1 PDColorSpace (org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace)1 Matrix (org.apache.pdfbox.util.Matrix)1