[
https://issues.apache.org/jira/browse/PDFBOX-4850?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17129658#comment-17129658
]
Jani Pehkonen commented on PDFBOX-4850:
---------------------------------------
Replace method drawBufferedImage() in class
org.apache.pdfbox.rendering.PageDrawer with this:
{code:java}
private void drawBufferedImage(BufferedImage image, AffineTransform at)
throws IOException
{
graphics.setComposite(getGraphicsState().getNonStrokingJavaComposite());
setClip();
AffineTransform imageTransform = new AffineTransform(at);
int width = image.getWidth();
int height = image.getHeight();
imageTransform.scale(1.0 / width, -1.0 / height);
imageTransform.translate(0, -height);
PDSoftMask softMask = getGraphicsState().getSoftMask();
if( softMask != null )
{
Rectangle2D rectangle = new Rectangle2D.Float(0, 0, width, height);
Paint awtPaint = new TexturePaint(image, rectangle);
awtPaint = applySoftMaskToPaint(awtPaint, softMask);
graphics.setPaint(awtPaint);
AffineTransform originalTransform = graphics.getTransform();
graphics.transform(imageTransform);
graphics.fill(rectangle);
graphics.setTransform(originalTransform);
}
else
{
COSBase transfer = getGraphicsState().getTransfer();
if (transfer instanceof COSArray || transfer instanceof
COSDictionary)
{
image = applyTransferFunction(image, transfer);
}
// PDFBOX-4516, PDFBOX-4527, PDFBOX-4815:
// graphics.drawImage() has terrible quality when scaling down,
even when
// RenderingHints.VALUE_INTERPOLATION_BICUBIC,
VALUE_ALPHA_INTERPOLATION_QUALITY,
// VALUE_COLOR_RENDER_QUALITY and VALUE_RENDER_QUALITY are all set.
// A workaround is to get a pre-scaled image with
Image.getScaledInstance()
// and then draw that one. To reduce differences in testing
// (partly because the method needs integer parameters), only
smaller scalings
// will trigger the workaround. Because of the slowness we only do
it if the user
// expects quality rendering and interpolation.
Matrix m = new Matrix(imageTransform);
float scaleX = Math.abs(m.getScalingFactorX());
float scaleY = Math.abs(m.getScalingFactorY());
Image imageToDraw = image;
if ((scaleX < 0.25f || scaleY < 0.25f) &&
RenderingHints.VALUE_RENDER_QUALITY.equals(graphics.getRenderingHint(RenderingHints.KEY_RENDERING))
&&
RenderingHints.VALUE_INTERPOLATION_BICUBIC.equals(graphics.getRenderingHint(RenderingHints.KEY_INTERPOLATION)))
{
int w = Math.round(image.getWidth() * scaleX);
int h = Math.round(image.getHeight() * scaleY);
if (w < 1)
{
w = 1;
}
if (h < 1)
{
h = 1;
}
imageToDraw = image.getScaledInstance(
w,
h,
Image.SCALE_SMOOTH);
imageTransform.scale(1 / scaleX, 1 / scaleY); // remove the
scale
}
graphics.drawImage(imageToDraw, imageTransform, null);
}
}
{code}
I have applied imageTransform to the Graphics2D object. This way imageTransform
rotates and scales the image and the anchor rectangle of the TexturePaint. Now
also the dimensions of the anchor rectangle are simply width and height.
This fixes also issue PDFBOX-4798.
> Image rendering issue
> ---------------------
>
> Key: PDFBOX-4850
> URL: https://issues.apache.org/jira/browse/PDFBOX-4850
> Project: PDFBox
> Issue Type: Bug
> Components: Rendering
> Affects Versions: 2.0.19
> Reporter: Daniel Persson
> Priority: Minor
> Attachments: issue-pdfbox.jpg, issue-poppler.jpg, issue.pdf
>
>
> Rendering file using PDFToImage creates a strange result where embedded
> images aren't rotated or scaled correctly.
>
> Rendering the same PDF using poppler will create a correct looking output.
--
This message was sent by Atlassian Jira
(v8.3.4#803005)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]