aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/org/apache/fop/render/pcl/JAIMonochromeBitmapConverter.java
blob: 89e54b917ef3894c7f85f4d60f2fa60af49717d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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;
    
    /** {@inheritDoc} */
    public void setHint(String name, String value) {
        if ("quality".equalsIgnoreCase(name)) {
            isErrorDiffusion = "true".equalsIgnoreCase(value);
        }
    }
    
    /** {@inheritDoc} */
    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();
    }

}