You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JAIMonochromeBitmapConverter.java 3.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright 2006 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id$ */
  17. package org.apache.fop.render.pcl;
  18. import java.awt.RenderingHints;
  19. import java.awt.image.BufferedImage;
  20. import java.awt.image.ColorModel;
  21. import java.awt.image.DataBuffer;
  22. import java.awt.image.IndexColorModel;
  23. import java.awt.image.RenderedImage;
  24. import java.awt.image.renderable.ParameterBlock;
  25. import javax.media.jai.ColorCube;
  26. import javax.media.jai.ImageLayout;
  27. import javax.media.jai.JAI;
  28. import javax.media.jai.KernelJAI;
  29. import javax.media.jai.LookupTableJAI;
  30. import javax.media.jai.PlanarImage;
  31. /**
  32. * Implementation of the MonochromeBitmapConverter which uses Java Advanced Imaging (JAI)
  33. * to convert grayscale bitmaps to monochrome bitmaps. JAI provides better dithering options
  34. * including error diffusion dithering.
  35. * <p>
  36. * If you call setHint("quality", "true") on the instance you can enabled error diffusion
  37. * dithering which produces a nicer result but is also a lot slower.
  38. */
  39. public class JAIMonochromeBitmapConverter implements
  40. MonochromeBitmapConverter {
  41. private boolean isErrorDiffusion = false;
  42. /** @see MonochromeBitmapConverter#setHint(java.lang.String, java.lang.String) */
  43. public void setHint(String name, String value) {
  44. if ("quality".equalsIgnoreCase(name)) {
  45. isErrorDiffusion = "true".equalsIgnoreCase(value);
  46. }
  47. }
  48. /** @see MonochromeBitmapConverter#convertToMonochrome(java.awt.image.BufferedImage) */
  49. public RenderedImage convertToMonochrome(BufferedImage img) {
  50. if (img.getColorModel().getColorSpace().getNumComponents() != 1) {
  51. throw new IllegalArgumentException("Source image must be a grayscale image!");
  52. }
  53. // Load the ParameterBlock for the dithering operation
  54. // and set the operation name.
  55. ParameterBlock pb = new ParameterBlock();
  56. pb.addSource(img);
  57. String opName = null;
  58. if (isErrorDiffusion) {
  59. opName = "errordiffusion";
  60. LookupTableJAI lut = new LookupTableJAI(new byte[] {(byte)0x00, (byte)0xff});
  61. pb.add(lut);
  62. pb.add(KernelJAI.ERROR_FILTER_FLOYD_STEINBERG);
  63. } else {
  64. opName = "ordereddither";
  65. //Create the color cube.
  66. ColorCube colorMap = ColorCube.createColorCube(DataBuffer.TYPE_BYTE,
  67. 0, new int[] {2});
  68. pb.add(colorMap);
  69. pb.add(KernelJAI.DITHER_MASK_441);
  70. }
  71. //Create an image layout for a monochrome b/w image
  72. ImageLayout layout = new ImageLayout();
  73. byte[] map = new byte[] {(byte)0x00, (byte)0xff};
  74. ColorModel cm = new IndexColorModel(1, 2, map, map, map);
  75. layout.setColorModel(cm);
  76. // Create a hint containing the layout.
  77. RenderingHints hints = new RenderingHints(JAI.KEY_IMAGE_LAYOUT, layout);
  78. // Dither the image.
  79. PlanarImage dst = JAI.create(opName, pb, hints);
  80. //Convert it to a BufferedImage
  81. return dst.getAsBufferedImage();
  82. }
  83. }