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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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.fop.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 context the renderer context for the current renderer
  43. * @param targetDimension the target dimensions of the image to be converted to a bitmap
  44. * @param resolution the requested bitmap resolution
  45. * @param gray true if the generated image should be in grayscales
  46. * @param withAlpha true if an alpha channel should be created
  47. * @return the generated BufferedImage
  48. */
  49. protected BufferedImage paintToBufferedImage(
  50. org.apache.xmlgraphics.java2d.Graphics2DImagePainter painter,
  51. Dimension targetDimension,
  52. int resolution, boolean gray, boolean withAlpha) {
  53. int bmw = (int)Math.ceil(UnitConv.mpt2px(targetDimension.getWidth(), resolution));
  54. int bmh = (int)Math.ceil(UnitConv.mpt2px(targetDimension.getHeight(), resolution));
  55. BufferedImage bi;
  56. if (gray) {
  57. if (withAlpha) {
  58. bi = createGrayBufferedImageWithAlpha(bmw, bmh);
  59. } else {
  60. bi = new BufferedImage(bmw, bmh, BufferedImage.TYPE_BYTE_GRAY);
  61. }
  62. } else {
  63. if (withAlpha) {
  64. bi = new BufferedImage(bmw, bmh, BufferedImage.TYPE_INT_ARGB);
  65. } else {
  66. bi = new BufferedImage(bmw, bmh, BufferedImage.TYPE_INT_RGB);
  67. }
  68. }
  69. Graphics2D g2d = bi.createGraphics();
  70. try {
  71. g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
  72. RenderingHints.VALUE_FRACTIONALMETRICS_ON);
  73. setRenderingHintsForBufferedImage(g2d);
  74. g2d.setBackground(Color.white);
  75. g2d.setColor(Color.black);
  76. if (!withAlpha) {
  77. g2d.clearRect(0, 0, bmw, bmh);
  78. }
  79. /* debug code
  80. int off = 2;
  81. g2d.drawLine(off, 0, off, bmh);
  82. g2d.drawLine(bmw - off, 0, bmw - off, bmh);
  83. g2d.drawLine(0, off, bmw, off);
  84. g2d.drawLine(0, bmh - off, bmw, bmh - off);
  85. */
  86. double sx = (double)bmw / targetDimension.getWidth();
  87. double sy = (double)bmh / targetDimension.getHeight();
  88. g2d.scale(sx, sy);
  89. //Paint the image on the BufferedImage
  90. Rectangle2D area = new Rectangle2D.Double(
  91. 0.0, 0.0, targetDimension.getWidth(), targetDimension.getHeight());
  92. painter.paint(g2d, area);
  93. } finally {
  94. g2d.dispose();
  95. }
  96. return bi;
  97. }
  98. private static BufferedImage createGrayBufferedImageWithAlpha(int width, int height) {
  99. BufferedImage bi;
  100. boolean alphaPremultiplied = true;
  101. int bands = 2;
  102. int[] bits = new int[bands];
  103. for (int i = 0; i < bands; i++) {
  104. bits[i] = 8;
  105. }
  106. ColorModel cm = new ComponentColorModel(
  107. ColorSpace.getInstance(ColorSpace.CS_GRAY),
  108. bits,
  109. true, alphaPremultiplied,
  110. Transparency.TRANSLUCENT,
  111. DataBuffer.TYPE_BYTE);
  112. WritableRaster wr = Raster.createInterleavedRaster(
  113. DataBuffer.TYPE_BYTE,
  114. width, height, bands,
  115. new Point(0, 0));
  116. bi = new BufferedImage(cm, wr, alphaPremultiplied, null);
  117. return bi;
  118. }
  119. /**
  120. * Sets rendering hints on the Graphics2D created for painting to a BufferedImage. Subclasses
  121. * can modify the settings to customize the behavior.
  122. * @param g2d the Graphics2D instance
  123. */
  124. protected void setRenderingHintsForBufferedImage(Graphics2D g2d) {
  125. g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
  126. RenderingHints.VALUE_ANTIALIAS_ON);
  127. g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
  128. RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
  129. }
  130. }