private byte[] compileHeader(ReportSkinModel style, int pageWidth, boolean isHeader) throws IOException {
final float PAGE_DPI = 72;
BufferedImage header = style.getHeaderImage();
BufferedImage logo = style.getLogoImage();
float headerHeight = style.getHeaderHeight();
float logoHeight = (float) (headerHeight * style.getLogoScale());
float headerDensity = header.getHeight() / (headerHeight / PAGE_DPI);
float logoDensity = logo.getHeight() / (logoHeight / PAGE_DPI);
float targetDpi = Math.min(300, Math.max(headerDensity, logoDensity));
boolean needToScaleHeader = headerDensity != targetDpi;
boolean needToScaleLogo = logoDensity != targetDpi;
java.awt.Image scaledHeader = header;
java.awt.Image scaledLogo = logo;
int width = header.getWidth();
int height = header.getHeight();
if (needToScaleHeader) {
float headerRatio = (float) header.getWidth() / (float) header.getHeight();
int newHeight = (int) (headerHeight / PAGE_DPI * targetDpi);
int proposedWidth = (int) (newHeight * headerRatio);
int newWidth = (int) Math.ceil(Math.max(proposedWidth, pageWidth / PAGE_DPI * targetDpi));
scaledHeader = header.getScaledInstance(newWidth, newHeight, BufferedImage.SCALE_SMOOTH);
width = newWidth;
height = newHeight;
}
if (needToScaleLogo) {
float logoRatio = (float) logo.getWidth() / (float) logo.getHeight();
int newHeight = (int) (logoHeight / PAGE_DPI * targetDpi);
int newWidth = (int) (Math.ceil(newHeight * logoRatio));
// int newWidth = (int) Math.ceil(Math.max(proposedWidth, pageWidth / PAGE_DPI * targetDpi));
scaledLogo = logo.getScaledInstance(newWidth, newHeight, BufferedImage.SCALE_SMOOTH);
}
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = (Graphics2D) bi.getGraphics();
g.setColor(Color.white);
g.fillRect(0, 0, width, height);
Tools.waitForImage(scaledHeader);
if (isHeader) {
g.drawImage(scaledHeader, 0, 0, null);
Tools.waitForImage(scaledLogo);
int iconX = (int) (style.getLogoX() / PAGE_DPI * targetDpi);
int iconY = (int) (style.getLogoY() / PAGE_DPI * targetDpi);
g.drawImage(scaledLogo, iconX, iconY, null);
} else {
g.rotate(Math.PI);
g.drawImage(scaledHeader, -width, -height, null);
g.rotate(-Math.PI);
Tools.waitForImage(scaledLogo);
int iconX = width - (int) (style.getLogoX() / PAGE_DPI * targetDpi) - scaledLogo.getWidth(null);
int iconY = height - (int) (style.getLogoY() / PAGE_DPI * targetDpi) - scaledLogo.getHeight(null);
g.drawImage(scaledLogo, iconX, iconY, null);
}
g.dispose();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bi, "png", baos);
return baos.toByteArray();
}