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.

AbstractImageHandlerGraphics2D.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. /* $Id$ */
  18. package org.apache.fop.render;
  19. import java.awt.Color;
  20. import java.awt.Dimension;
  21. import java.awt.Graphics2D;
  22. import java.awt.Point;
  23. import java.awt.RenderingHints;
  24. import java.awt.Transparency;
  25. import java.awt.color.ColorSpace;
  26. import java.awt.geom.Rectangle2D;
  27. import java.awt.image.BufferedImage;
  28. import java.awt.image.ColorModel;
  29. import java.awt.image.ComponentColorModel;
  30. import java.awt.image.DataBuffer;
  31. import java.awt.image.Raster;
  32. import java.awt.image.WritableRaster;
  33. import org.apache.xmlgraphics.util.UnitConv;
  34. /**
  35. * Abstract base class for ImageHandler implementations that process Java2D images through
  36. * the Graphics2DImagePainter interface.
  37. */
  38. public abstract class AbstractImageHandlerGraphics2D implements ImageHandler {
  39. /**
  40. * Paints the image to a BufferedImage and returns that.
  41. * @param painter the painter which will paint the actual image
  42. * @param targetDimension the target dimensions of the image to be converted to a bitmap
  43. * @param resolution the requested bitmap resolution
  44. * @param gray true if the generated image should be in grayscales
  45. * @param withAlpha true if an alpha channel should be created
  46. * @return the generated BufferedImage
  47. */
  48. protected BufferedImage paintToBufferedImage(
  49. org.apache.xmlgraphics.java2d.Graphics2DImagePainter painter,
  50. Dimension targetDimension,
  51. int resolution, boolean gray, boolean withAlpha) {
  52. int bmw = (int)Math.ceil(UnitConv.mpt2px(targetDimension.getWidth(), resolution));
  53. int bmh = (int)Math.ceil(UnitConv.mpt2px(targetDimension.getHeight(), resolution));
  54. BufferedImage bi;
  55. if (gray) {
  56. if (withAlpha) {
  57. bi = createGrayBufferedImageWithAlpha(bmw, bmh);
  58. } else {
  59. bi = new BufferedImage(bmw, bmh, BufferedImage.TYPE_BYTE_GRAY);
  60. }
  61. } else {
  62. if (withAlpha) {
  63. bi = new BufferedImage(bmw, bmh, BufferedImage.TYPE_INT_ARGB);
  64. } else {
  65. bi = new BufferedImage(bmw, bmh, BufferedImage.TYPE_INT_RGB);
  66. }
  67. }
  68. Graphics2D g2d = bi.createGraphics();
  69. try {
  70. g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
  71. RenderingHints.VALUE_FRACTIONALMETRICS_ON);
  72. setRenderingHintsForBufferedImage(g2d);
  73. g2d.setBackground(Color.white);
  74. g2d.setColor(Color.black);
  75. if (!withAlpha) {
  76. g2d.clearRect(0, 0, bmw, bmh);
  77. }
  78. /* debug code
  79. int off = 2;
  80. g2d.drawLine(off, 0, off, bmh);
  81. g2d.drawLine(bmw - off, 0, bmw - off, bmh);
  82. g2d.drawLine(0, off, bmw, off);
  83. g2d.drawLine(0, bmh - off, bmw, bmh - off);
  84. */
  85. double sx = (double)bmw / targetDimension.getWidth();
  86. double sy = (double)bmh / targetDimension.getHeight();
  87. g2d.scale(sx, sy);
  88. //Paint the image on the BufferedImage
  89. Rectangle2D area = new Rectangle2D.Double(
  90. 0.0, 0.0, targetDimension.getWidth(), targetDimension.getHeight());
  91. painter.paint(g2d, area);
  92. } finally {
  93. g2d.dispose();
  94. }
  95. return bi;
  96. }
  97. private static BufferedImage createGrayBufferedImageWithAlpha(int width, int height) {
  98. BufferedImage bi;
  99. boolean alphaPremultiplied = true;
  100. int bands = 2;
  101. int[] bits = new int[bands];
  102. for (int i = 0; i < bands; i++) {
  103. bits[i] = 8;
  104. }
  105. ColorModel cm = new ComponentColorModel(
  106. ColorSpace.getInstance(ColorSpace.CS_GRAY),
  107. bits,
  108. true, alphaPremultiplied,
  109. Transparency.TRANSLUCENT,
  110. DataBuffer.TYPE_BYTE);
  111. WritableRaster wr = Raster.createInterleavedRaster(
  112. DataBuffer.TYPE_BYTE,
  113. width, height, bands,
  114. new Point(0, 0));
  115. bi = new BufferedImage(cm, wr, alphaPremultiplied, null);
  116. return bi;
  117. }
  118. /**
  119. * Sets rendering hints on the Graphics2D created for painting to a BufferedImage. Subclasses
  120. * can modify the settings to customize the behavior.
  121. * @param g2d the Graphics2D instance
  122. */
  123. protected void setRenderingHintsForBufferedImage(Graphics2D g2d) {
  124. g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
  125. RenderingHints.VALUE_ANTIALIAS_ON);
  126. g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
  127. RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
  128. }
  129. }