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());
}
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);
}
}
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);
}
Aggregations