1 17 18 19 20 package org.apache.fop.render.pcl; 21 22 import java.awt.RenderingHints ; 23 import java.awt.image.BufferedImage ; 24 import java.awt.image.ColorModel ; 25 import java.awt.image.DataBuffer ; 26 import java.awt.image.IndexColorModel ; 27 import java.awt.image.RenderedImage ; 28 import java.awt.image.renderable.ParameterBlock ; 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 45 public class JAIMonochromeBitmapConverter implements 46 MonochromeBitmapConverter { 47 48 private boolean isErrorDiffusion = false; 49 50 51 public void setHint(String name, String value) { 52 if ("quality".equalsIgnoreCase(name)) { 53 isErrorDiffusion = "true".equalsIgnoreCase(value); 54 } 55 } 56 57 58 public RenderedImage convertToMonochrome(BufferedImage img) { 59 if (img.getColorModel().getColorSpace().getNumComponents() != 1) { 60 throw new IllegalArgumentException ("Source image must be a grayscale image!"); 61 } 62 63 ParameterBlock pb = new ParameterBlock (); 66 pb.addSource(img); 67 String 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 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 ImageLayout layout = new ImageLayout(); 84 byte[] map = new byte[] {(byte)0x00, (byte)0xff}; 85 ColorModel cm = new IndexColorModel (1, 2, map, map, map); 86 layout.setColorModel(cm); 87 88 RenderingHints hints = new RenderingHints (JAI.KEY_IMAGE_LAYOUT, layout); 90 91 PlanarImage dst = JAI.create(opName, pb, hints); 93 94 return dst.getAsBufferedImage(); 96 } 97 98 } 99 | Popular Tags |