KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > render > pcl > JAIMonochromeBitmapConverter


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id$ */
19
20 package org.apache.fop.render.pcl;
21
22 import java.awt.RenderingHints JavaDoc;
23 import java.awt.image.BufferedImage JavaDoc;
24 import java.awt.image.ColorModel JavaDoc;
25 import java.awt.image.DataBuffer JavaDoc;
26 import java.awt.image.IndexColorModel JavaDoc;
27 import java.awt.image.RenderedImage JavaDoc;
28 import java.awt.image.renderable.ParameterBlock JavaDoc;
29
30 import javax.media.jai.ColorCube;
31 import javax.media.jai.ImageLayout;
32 import javax.media.jai.JAI;
33 import javax.media.jai.KernelJAI;
34 import javax.media.jai.LookupTableJAI;
35 import javax.media.jai.PlanarImage;
36
37 /**
38  * Implementation of the MonochromeBitmapConverter which uses Java Advanced Imaging (JAI)
39  * to convert grayscale bitmaps to monochrome bitmaps. JAI provides better dithering options
40  * including error diffusion dithering.
41  * <p>
42  * If you call setHint("quality", "true") on the instance you can enabled error diffusion
43  * dithering which produces a nicer result but is also a lot slower.
44  */

45 public class JAIMonochromeBitmapConverter implements
46         MonochromeBitmapConverter {
47
48     private boolean isErrorDiffusion = false;
49     
50     /** @see MonochromeBitmapConverter#setHint(java.lang.String, java.lang.String) */
51     public void setHint(String JavaDoc name, String JavaDoc value) {
52         if ("quality".equalsIgnoreCase(name)) {
53             isErrorDiffusion = "true".equalsIgnoreCase(value);
54         }
55     }
56     
57     /** @see MonochromeBitmapConverter#convertToMonochrome(java.awt.image.BufferedImage) */
58     public RenderedImage JavaDoc convertToMonochrome(BufferedImage JavaDoc img) {
59         if (img.getColorModel().getColorSpace().getNumComponents() != 1) {
60             throw new IllegalArgumentException JavaDoc("Source image must be a grayscale image!");
61         }
62         
63         // Load the ParameterBlock for the dithering operation
64
// and set the operation name.
65
ParameterBlock JavaDoc pb = new ParameterBlock JavaDoc();
66         pb.addSource(img);
67         String JavaDoc opName = null;
68         if (isErrorDiffusion) {
69             opName = "errordiffusion";
70             LookupTableJAI lut = new LookupTableJAI(new byte[] {(byte)0x00, (byte)0xff});
71             pb.add(lut);
72             pb.add(KernelJAI.ERROR_FILTER_FLOYD_STEINBERG);
73         } else {
74             opName = "ordereddither";
75             //Create the color cube.
76
ColorCube colorMap = ColorCube.createColorCube(DataBuffer.TYPE_BYTE,
77                     0, new int[] {2});
78             pb.add(colorMap);
79             pb.add(KernelJAI.DITHER_MASK_441);
80         }
81         
82         //Create an image layout for a monochrome b/w image
83
ImageLayout layout = new ImageLayout();
84         byte[] map = new byte[] {(byte)0x00, (byte)0xff};
85         ColorModel JavaDoc cm = new IndexColorModel JavaDoc(1, 2, map, map, map);
86         layout.setColorModel(cm);
87
88         // Create a hint containing the layout.
89
RenderingHints JavaDoc hints = new RenderingHints JavaDoc(JAI.KEY_IMAGE_LAYOUT, layout);
90
91         // Dither the image.
92
PlanarImage dst = JAI.create(opName, pb, hints);
93         
94         //Convert it to a BufferedImage
95
return dst.getAsBufferedImage();
96     }
97
98 }
99
Popular Tags