From b6638c65c94d5afb8ca0f6ea0b37632cf8fb11be Mon Sep 17 00:00:00 2001
From: Jeremias Maerki
Date: Fri, 2 Oct 2009 09:19:12 +0000
Subject: Added support for creating thumbnails or preview bitmaps of fixed
size for PNG and TIFF output. Documentation update for bitmap output.
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@820939 13f79535-47bb-0310-9956-ffa450edef68
---
src/documentation/content/xdocs/trunk/output.xml | 84 ++++++++++++++++++----
.../bitmap/AbstractBitmapDocumentHandler.java | 70 ++++++++++++++++--
.../render/bitmap/BitmapRendererConfigurator.java | 5 +-
3 files changed, 138 insertions(+), 21 deletions(-)
(limited to 'src')
diff --git a/src/documentation/content/xdocs/trunk/output.xml b/src/documentation/content/xdocs/trunk/output.xml
index 0ae9d586b..972fc1f19 100644
--- a/src/documentation/content/xdocs/trunk/output.xml
+++ b/src/documentation/content/xdocs/trunk/output.xml
@@ -1033,8 +1033,13 @@ out = proc.getOutputStream();]]>
Currently, two output formats are supported: PNG and TIFF. TIFF produces
one file with multiple pages, while PNG output produces one file per
- page. The quality of the bitmap depends on the target resolution setting
- on the FOUserAgent.
+ page. Note: FOP can only produce multiple files (with PNG output) if
+ you can set a java.io.File indicating the primary PNG file
+ using the FOUserAgent.setOutputFile(File) method.
+
+
+ The quality of the bitmap depends on the target resolution setting
+ on the FOUserAgent and on further settings described below.
Configuration
@@ -1042,15 +1047,52 @@ out = proc.getOutputStream();]]>
The TIFF and PNG renderer configuration currently allows the following settings:
- The default value for the "transparent-page-background" setting is "false" which
- paints an opaque, white background for the whole image. If you set this to true,
+ The default value for the "color-mode" setting is "rgba" which
+ is equivalent to a 24bit RGB image with an 8bit alpha channel for transparency.
+ Valid values are:
+
+
+
rgba: RGB with alpha channel (24bit + 8bit = 32bit)
+
rgb: RGB (24bit)
+
gray: gray (8bit)
+
bi-level (or binary): bi-level (1bit)
+
+
+ Please note that there is currently no dithering or error diffusion available for bi-level
+ bitmap output.
+
+
+ The default value for the "transparent-page-background" setting is
+ "false" which paints an opaque, white background for the whole image.
+ If you set this to "true",
no such background will be painted and you will get a transparent image if
an alpha channel is available in the output format.
+
+ The default value for the "background-color" setting is "white".
+ The color specifies in which color the page background is painted. It will only be
+ painted if "transparent-page-background" is not set to "true".
+ All XSL-FO colors (including color functions) can be used.
+
+
+ The default value for the "anti-aliasing" setting is "true".
+ You can set this value to "false" to disable anti-aliasing and
+ thus improve rendering speeds a bit at the loss of some image quality.
+
+
+ The default value for the "rendering" setting is "true".
+ You can set this value to "false" to improve rendering speeds a bit
+ at the loss of some image quality. If this setting has an actual effect depends
+ on the JVM's Java2D backend.
+
TIFF-specific Configuration
@@ -1071,17 +1113,22 @@ out = proc.getOutputStream();]]>
actual codecs being available. Here is a list of possible values:
-
NONE (no compression)
-
PackBits (RLE, run-length encoding)
-
JPEG
-
Deflate
-
LZW
-
ZLib
-
CCITT T.4 (Fax Group 3)
-
CCITT T.6 (Fax Group 4)
+
NONE (no compression)
+
PackBits (RLE, run-length encoding)
+
JPEG
+
Deflate
+
LZW
+
ZLib
+
CCITT T.4 (Fax Group 3)
+
CCITT T.6 (Fax Group 4)
+
+ This setting may override any setting made using the "color-mode". For example, if
+ "CCITT T.6" is selected, the color mode is automatically forced to "bi-level" because
+ this compression format only supports bi-level images.
+
- If you want to use CCITT compression, please make sure you've got a J2SE 1.4 or later and
+ If you want to use CCITT compression, please make sure you've got
Java Advanced Imaging Image I/O Tools
@@ -1090,6 +1137,17 @@ out = proc.getOutputStream();]]>
Deflate and JPEG compression for writing.
+
+ Runtime Rendering Options
+
+ The IF-based bitmap output implementations support a rendering option with the key
+ "target-bitmap-size" (value: java.awt.Dimension) that allows to force the pages to
+ be proportionally fit into a bitmap of a given size. This can be used to produce
+ thumbnails or little preview images of the individual pages. An example:
+
+
+ TXT
diff --git a/src/java/org/apache/fop/render/bitmap/AbstractBitmapDocumentHandler.java b/src/java/org/apache/fop/render/bitmap/AbstractBitmapDocumentHandler.java
index 3449ffe7e..e4606858b 100644
--- a/src/java/org/apache/fop/render/bitmap/AbstractBitmapDocumentHandler.java
+++ b/src/java/org/apache/fop/render/bitmap/AbstractBitmapDocumentHandler.java
@@ -22,9 +22,11 @@ package org.apache.fop.render.bitmap;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
+import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
+import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
@@ -53,6 +55,13 @@ public abstract class AbstractBitmapDocumentHandler extends AbstractBinaryWritin
/** logging instance */
private static Log log = LogFactory.getLog(AbstractBitmapDocumentHandler.class);
+ /**
+ * Rendering Options key for the controlling the required bitmap size to create.
+ * This is used to create thumbnails, for example. If used, the target resolution is ignored.
+ * Value type: java.awt.Dimension (size in pixels)
+ */
+ public static final String TARGET_BITMAP_SIZE = "target-bitmap-size";
+
private ImageWriter imageWriter;
private MultiImageWriter multiImageWriter;
@@ -66,6 +75,7 @@ public abstract class AbstractBitmapDocumentHandler extends AbstractBinaryWritin
private BitmapRenderingSettings bitmapSettings = new BitmapRenderingSettings();
private double scaleFactor = 1.0;
+ private Dimension targetBitmapSize;
/**
* Default constructor.
@@ -94,6 +104,9 @@ public abstract class AbstractBitmapDocumentHandler extends AbstractBinaryWritin
//Set target resolution
int dpi = Math.round(context.getUserAgent().getTargetResolution());
getSettings().getWriterParams().setResolution(dpi);
+
+ Map renderingOptions = getUserAgent().getRendererOptions();
+ setTargetBitmapSize((Dimension)renderingOptions.get(TARGET_BITMAP_SIZE));
}
/** {@inheritDoc} */
@@ -113,6 +126,17 @@ public abstract class AbstractBitmapDocumentHandler extends AbstractBinaryWritin
setFontInfo(fi);
}
+ /**
+ * Sets the target bitmap size (in pixels) of the bitmap that should be produced. Normally,
+ * the bitmap size is calculated automatically based on the page size and the target
+ * resolution. But for example, if you want to create thumbnails or small preview bitmaps
+ * from pages it is more practical (and efficient) to set the required bitmap size.
+ * @param size the target bitmap size (in pixels)
+ */
+ public void setTargetBitmapSize(Dimension size) {
+ this.targetBitmapSize = size;
+ }
+
//----------------------------------------------------------------------------------------------
/** {@inheritDoc} */
@@ -176,13 +200,43 @@ public abstract class AbstractBitmapDocumentHandler extends AbstractBinaryWritin
/** {@inheritDoc} */
public IFPainter startPageContent() throws IFException {
- double scale = scaleFactor
- * (25.4f / FopFactoryConfigurator.DEFAULT_TARGET_RESOLUTION)
- / getUserAgent().getTargetPixelUnitToMillimeter();
- int bitmapWidth = (int) ((this.currentPageDimensions.width * scale / 1000f) + 0.5f);
- int bitmapHeight = (int) ((this.currentPageDimensions.height * scale / 1000f) + 0.5f);
+ int bitmapWidth;
+ int bitmapHeight;
+ double scale;
+ Point2D offset = null;
+ if (targetBitmapSize != null) {
+ //Fit the generated page proportionally into the given rectangle (in pixels)
+ double scale2w = 1000 * targetBitmapSize.width
+ / this.currentPageDimensions.getWidth();
+ double scale2h = 1000 * targetBitmapSize.height
+ / this.currentPageDimensions.getHeight();
+ bitmapWidth = targetBitmapSize.width;
+ bitmapHeight = targetBitmapSize.height;
+
+ //Centering the page in the given bitmap
+ offset = new Point2D.Double();
+ if (scale2w < scale2h) {
+ scale = scale2w;
+ double h = this.currentPageDimensions.height * scale / 1000;
+ offset.setLocation(0, (bitmapHeight - h) / 2.0);
+ } else {
+ scale = scale2h;
+ double w = this.currentPageDimensions.width * scale / 1000;
+ offset.setLocation((bitmapWidth - w) / 2.0, 0);
+ }
+ } else {
+ //Normal case: just scale according to the target resolution
+ scale = scaleFactor
+ * getUserAgent().getTargetResolution()
+ / FopFactoryConfigurator.DEFAULT_TARGET_RESOLUTION;
+ bitmapWidth = (int) ((this.currentPageDimensions.width * scale / 1000f) + 0.5f);
+ bitmapHeight = (int) ((this.currentPageDimensions.height * scale / 1000f) + 0.5f);
+ }
+
+ //Set up bitmap to paint on
this.currentImage = createBufferedImage(bitmapWidth, bitmapHeight);
Graphics2D graphics2D = this.currentImage.createGraphics();
+
// draw page background
if (!getSettings().hasTransparentPageBackground()) {
graphics2D.setBackground(getSettings().getPageBackgroundColor());
@@ -190,6 +244,7 @@ public abstract class AbstractBitmapDocumentHandler extends AbstractBinaryWritin
graphics2D.fillRect(0, 0, bitmapWidth, bitmapHeight);
}
+ //Set rendering hints
graphics2D.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
RenderingHints.VALUE_FRACTIONALMETRICS_ON);
if (getSettings().isAntiAliasingEnabled()
@@ -213,6 +268,11 @@ public abstract class AbstractBitmapDocumentHandler extends AbstractBinaryWritin
}
graphics2D.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,
RenderingHints.VALUE_STROKE_PURE);
+
+ //Set up initial coordinate system for the page
+ if (offset != null) {
+ graphics2D.translate(offset.getX(), offset.getY());
+ }
graphics2D.scale(scale / 1000f, scale / 1000f);
return new Java2DPainter(graphics2D, getContext(), getFontInfo());
diff --git a/src/java/org/apache/fop/render/bitmap/BitmapRendererConfigurator.java b/src/java/org/apache/fop/render/bitmap/BitmapRendererConfigurator.java
index 5237dbe35..1b6c43700 100644
--- a/src/java/org/apache/fop/render/bitmap/BitmapRendererConfigurator.java
+++ b/src/java/org/apache/fop/render/bitmap/BitmapRendererConfigurator.java
@@ -19,7 +19,6 @@
package org.apache.fop.render.bitmap;
-import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.List;
@@ -80,8 +79,6 @@ public class BitmapRendererConfigurator extends Java2DRendererConfigurator
if (background != null) {
settings.setPageBackgroundColor(
ColorUtil.parseColorString(this.userAgent, background));
- } else {
- settings.setPageBackgroundColor(Color.WHITE);
}
}
@@ -106,6 +103,8 @@ public class BitmapRendererConfigurator extends Java2DRendererConfigurator
settings.setBufferedImageType(BufferedImage.TYPE_BYTE_GRAY);
} else if ("binary".equalsIgnoreCase(color)) {
settings.setBufferedImageType(BufferedImage.TYPE_BYTE_BINARY);
+ } else if ("bi-level".equalsIgnoreCase(color)) {
+ settings.setBufferedImageType(BufferedImage.TYPE_BYTE_BINARY);
} else {
throw new FOPException("Invalid value for color-mode: " + color);
}
--
cgit v1.2.3