Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Smoother rendering via rendering hints #112

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,14 @@ public static final BufferedImage readImageWithEXIFRotation(File input) throws I
}

public static java.awt.Image debugDisplayImage(java.awt.Image image) {
JPanel panel = new JPanel();
panel.add(new JLabel(new ImageIcon(image)));
JOptionPane.showMessageDialog(null, new JScrollPane(panel));
// note that the image does not render correctly
// if this function is called from an AWT Event thread
// wrapping it all in a thread seams to fix the problem
new Thread(() -> {
JPanel panel = new JPanel();
panel.add(new JLabel(new ImageIcon(image)));
JOptionPane.showMessageDialog(null, new JScrollPane(panel));
}).start();
return image;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,23 @@
package at.asit.pdfover.gui.composites;

// Imports
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.swing.JPanel;

import at.asit.pdfover.commons.Messages;
import at.asit.pdfover.signer.pdfas.PdfAs4SignatureParameter;
import lombok.extern.slf4j.Slf4j;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.rendering.PDFRenderer;

import at.asit.pdfover.commons.Messages;
import lombok.extern.slf4j.Slf4j;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
*
Expand Down Expand Up @@ -168,14 +163,13 @@ public void setDocument(PDDocument pdf) {
/**
* Set the signature placeholder image
* @param placeholder signature placeholder
* @param width width of the placeholder in page space
* @param height height of the placeholder in page space
*/
public void setSignaturePlaceholder(Image placeholder) {
this.sigPlaceholder = placeholder;
// TODO figure out why this is divided by 4 (factor ported from old code)
this.sigPageWidth = placeholder.getWidth(null) / 4;
this.sigPageHeight = placeholder.getHeight(null) / 4;

// the size gets scaled down so it looks better
this.sigPageWidth = placeholder.getWidth(null) / PdfAs4SignatureParameter.SIG_PREVIEW_SCALING_FACTOR;
this.sigPageHeight = placeholder.getHeight(null) / PdfAs4SignatureParameter.SIG_PREVIEW_SCALING_FACTOR;
renderPageToImage();
if (this.sigPagePos != null)
setSignaturePosition(this.sigPagePos.getX(), this.sigPagePos.getY());
Expand Down Expand Up @@ -304,6 +298,15 @@ public void paint(Graphics g) {
Dimension renderPanelSize = getSize();
g.setColor(getBackground());
g.fillRect(0, 0, getWidth(), getHeight());

if (g instanceof Graphics2D){
// this will make the rendering look much better
var g2d = ((Graphics2D) g);
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
}

if (this.currentImage == null) {
g.setColor(Color.black);
g.drawString(Messages.getString("common.working"), getWidth() / 2 - 30, getHeight() / 2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,35 @@

//Imports

import iaik.x509.X509Certificate;
import lombok.extern.slf4j.Slf4j;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.util.Locale;

import at.asit.pdfover.commons.BKUs;
import at.asit.pdfover.commons.Constants;
import at.asit.pdfover.commons.Profile;
import at.asit.pdfover.signer.DocumentSource;
import at.asit.pdfover.signer.Emblem;
import at.asit.pdfover.signer.SignaturePosition;
import at.gv.egiz.pdfas.lib.api.Configuration;
import at.gv.egiz.pdfas.lib.api.PdfAs;
import at.gv.egiz.pdfas.lib.api.PdfAsFactory;
import at.gv.egiz.pdfas.lib.api.sign.SignParameter;
import at.asit.pdfover.commons.BKUs;
import at.asit.pdfover.commons.Constants;
import at.asit.pdfover.commons.Profile;
import iaik.x509.X509Certificate;
import lombok.extern.slf4j.Slf4j;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Locale;

/**
* Implementation of SignatureParameter for PDF-AS 4 Library
*/
@Slf4j
public class PdfAs4SignatureParameter {


/**
* This value scales the preview image that is generated, we then downscale
* it with filtering to get a cleaner image
*/
public static final int SIG_PREVIEW_SCALING_FACTOR = 2;

/**
* this is set by CliArguments.InvisibleProfile
* TODO: this is a no good, very bad, hack
Expand Down Expand Up @@ -115,12 +120,15 @@ Image getPlaceholder() {
}
SignParameter param = PdfAsFactory.createSignParameter(conf, null, null);
param.setSignatureProfileId(sigProfile);

return pdfas.generateVisibleSignaturePreview(param, cert, 72 * 4);

// 72 is the number of typography dots in an inch
return pdfas.generateVisibleSignaturePreview(param, cert, 72 * SIG_PREVIEW_SCALING_FACTOR);
}
} catch (Exception e) {
log.error("Failed to get signature placeholder", e);
return new BufferedImage(229, 77, BufferedImage.TYPE_INT_RGB);
// these hardcoded values come from pdfas.generateVisibleSignaturePreview(param, cert, 72);
// we then scale them so the calculations in SignaturePanel.setSignaturePlaceholder are correct
return new BufferedImage(229 * SIG_PREVIEW_SCALING_FACTOR, 82 * SIG_PREVIEW_SCALING_FACTOR, BufferedImage.TYPE_INT_RGB);
}
}

Expand Down
Loading