You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@pdfbox.apache.org by "Tilman Hausherr (Jira)" <ji...@apache.org> on 2022/04/01 15:12:00 UTC

[jira] [Commented] (PDFBOX-5403) Blurry / distorted rendering

    [ https://issues.apache.org/jira/browse/PDFBOX-5403?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17515959#comment-17515959 ] 

Tilman Hausherr commented on PDFBOX-5403:
-----------------------------------------

I played around with our code... the bad stuff happens here:
{code}
BufferedImage mask = pdImage.getImage();
BufferedImage renderedMask = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
g = (Graphics2D) renderedMask.getGraphics();
g.translate(-bounds.getMinX(), -bounds.getMinY());
AffineTransform imageTransform = new AffineTransform(at);
imageTransform.scale(1.0 / mask.getWidth(), -1.0 / mask.getHeight());
imageTransform.translate(0, -mask.getHeight());
g.setRenderingHints(graphics.getRenderingHints());
g.drawImage(mask, imageTransform, null);
g.dispose();
{code}
Save mask and renderedMask to files, and you'll see that renderedMask looks terrible.

I was able to create a better renderedMask by using {{getScaledInstance()}}:
{code}
BufferedImage renderedMask = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
g = (Graphics2D) renderedMask.getGraphics();
g.translate(-bounds.getMinX(), -bounds.getMinY());
AffineTransform imageTransform = new AffineTransform(at);
imageTransform.scale(1.0 / mask.getWidth(), -1.0 / mask.getHeight());
imageTransform.translate(0, -mask.getHeight());
g.setRenderingHints(graphics.getRenderingHints());

int w2 = (int) Math.round(mask.getWidth() * Math.abs(imageTransform.getScaleX()));
int h2 = (int) Math.round(mask.getHeight()* Math.abs(imageTransform.getScaleY()));
Image scaledMask = mask.getScaledInstance(w2, h2, Image.SCALE_SMOOTH);
imageTransform.scale(1f / Math.abs(imageTransform.getScaleY()), 1f / Math.abs(imageTransform.getScaleX()));
g.drawImage(scaledMask, imageTransform, null);
g.dispose();
{code}
and modifying code further down to use that as a gray mask:
{code}
for (int y = 0; y < h; y++)
{
	for (int x = 0; x < w; x++)
	{
		alphaPixel = alpha.getPixel(x, y, alphaPixel);
		rasterPixel = raster.getPixel(x, y, rasterPixel);
		rasterPixel[3] = 255 - alphaPixel[0];
		raster.setPixel(x, y, rasterPixel);
	}
}
{code}
However that has the drawback that the getScaledInstance() call produces a black line so the whole change is worthless.

What we could do (but I haven't tested) is to color-flip the mask, and then scale, and modify the code later down, i.e. not to substract from 255. This way the black line wouldn't bother. But all this gets really too complex.

> Blurry / distorted rendering
> ----------------------------
>
>                 Key: PDFBOX-5403
>                 URL: https://issues.apache.org/jira/browse/PDFBOX-5403
>             Project: PDFBox
>          Issue Type: Bug
>          Components: Rendering
>    Affects Versions: 2.0.25
>            Reporter: Oliver Schmidtmer
>            Priority: Major
>         Attachments: alpha_interpolation.patch, bad rendering.pdf, image-2022-03-29-18-29-03-860.png, image-2022-03-30-14-39-12-855.png, image-2022-03-31-11-55-36-061.png
>
>
> The attached PDF uses many stripes with an image and a mask.
> There seems to be an issue when mask and image are combined.
> !image-2022-03-29-18-29-03-860.png|width=518,height=91!



--
This message was sent by Atlassian Jira
(v8.20.1#820001)

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: dev-help@pdfbox.apache.org