aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/org/apache/fop/render/pcl/JAIMonochromeBitmapConverter.java
diff options
context:
space:
mode:
authorJeremias Maerki <jeremias@apache.org>2006-07-24 13:16:23 +0000
committerJeremias Maerki <jeremias@apache.org>2006-07-24 13:16:23 +0000
commitfe9c40f495aee9919b86a746dfd587069f7cc3f3 (patch)
tree140ec1ebac55584dc3e5917b38856f0862c9c419 /src/java/org/apache/fop/render/pcl/JAIMonochromeBitmapConverter.java
parent423453976de29088ee76156baec1a83bc58b9518 (diff)
downloadxmlgraphics-fop-fe9c40f495aee9919b86a746dfd587069f7cc3f3.tar.gz
xmlgraphics-fop-fe9c40f495aee9919b86a746dfd587069f7cc3f3.zip
Promoting the PCL renderer from sandbox to the main source tree.
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@425038 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java/org/apache/fop/render/pcl/JAIMonochromeBitmapConverter.java')
-rw-r--r--src/java/org/apache/fop/render/pcl/JAIMonochromeBitmapConverter.java97
1 files changed, 97 insertions, 0 deletions
diff --git a/src/java/org/apache/fop/render/pcl/JAIMonochromeBitmapConverter.java b/src/java/org/apache/fop/render/pcl/JAIMonochromeBitmapConverter.java
new file mode 100644
index 000000000..4818b2b1d
--- /dev/null
+++ b/src/java/org/apache/fop/render/pcl/JAIMonochromeBitmapConverter.java
@@ -0,0 +1,97 @@
+/*
+ * Copyright 2006 The Apache Software Foundation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/* $Id$ */
+
+package org.apache.fop.render.pcl;
+
+import java.awt.RenderingHints;
+import java.awt.image.BufferedImage;
+import java.awt.image.ColorModel;
+import java.awt.image.DataBuffer;
+import java.awt.image.IndexColorModel;
+import java.awt.image.RenderedImage;
+import java.awt.image.renderable.ParameterBlock;
+
+import javax.media.jai.ColorCube;
+import javax.media.jai.ImageLayout;
+import javax.media.jai.JAI;
+import javax.media.jai.KernelJAI;
+import javax.media.jai.LookupTableJAI;
+import javax.media.jai.PlanarImage;
+
+/**
+ * Implementation of the MonochromeBitmapConverter which uses Java Advanced Imaging (JAI)
+ * to convert grayscale bitmaps to monochrome bitmaps. JAI provides better dithering options
+ * including error diffusion dithering.
+ * <p>
+ * If you call setHint("quality", "true") on the instance you can enabled error diffusion
+ * dithering which produces a nicer result but is also a lot slower.
+ */
+public class JAIMonochromeBitmapConverter implements
+ MonochromeBitmapConverter {
+
+ private boolean isErrorDiffusion = false;
+
+ /** @see MonochromeBitmapConverter#setHint(java.lang.String, java.lang.String) */
+ public void setHint(String name, String value) {
+ if ("quality".equalsIgnoreCase(name)) {
+ isErrorDiffusion = "true".equalsIgnoreCase(value);
+ }
+ }
+
+ /** @see MonochromeBitmapConverter#convertToMonochrome(java.awt.image.BufferedImage) */
+ public RenderedImage convertToMonochrome(BufferedImage img) {
+ if (img.getColorModel().getColorSpace().getNumComponents() != 1) {
+ throw new IllegalArgumentException("Source image must be a grayscale image!");
+ }
+
+ // Load the ParameterBlock for the dithering operation
+ // and set the operation name.
+ ParameterBlock pb = new ParameterBlock();
+ pb.addSource(img);
+ String opName = null;
+ if (isErrorDiffusion) {
+ opName = "errordiffusion";
+ LookupTableJAI lut = new LookupTableJAI(new byte[] {(byte)0x00, (byte)0xff});
+ pb.add(lut);
+ pb.add(KernelJAI.ERROR_FILTER_FLOYD_STEINBERG);
+ } else {
+ opName = "ordereddither";
+ //Create the color cube.
+ ColorCube colorMap = ColorCube.createColorCube(DataBuffer.TYPE_BYTE,
+ 0, new int[] {2});
+ pb.add(colorMap);
+ pb.add(KernelJAI.DITHER_MASK_441);
+ }
+
+ //Create an image layout for a monochrome b/w image
+ ImageLayout layout = new ImageLayout();
+ byte[] map = new byte[] {(byte)0x00, (byte)0xff};
+ ColorModel cm = new IndexColorModel(1, 2, map, map, map);
+ layout.setColorModel(cm);
+
+ // Create a hint containing the layout.
+ RenderingHints hints = new RenderingHints(JAI.KEY_IMAGE_LAYOUT, layout);
+
+ // Dither the image.
+ PlanarImage dst = JAI.create(opName, pb, hints);
+
+ //Convert it to a BufferedImage
+ return dst.getAsBufferedImage();
+ }
+
+}