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.

AbstractGraphics2DAdapter.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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.Graphics2D;
  21. import java.awt.Point;
  22. import java.awt.RenderingHints;
  23. import java.awt.Transparency;
  24. import java.awt.color.ColorSpace;
  25. import java.awt.geom.Rectangle2D;
  26. import java.awt.image.BufferedImage;
  27. import java.awt.image.ColorModel;
  28. import java.awt.image.ComponentColorModel;
  29. import java.awt.image.DataBuffer;
  30. import java.awt.image.Raster;
  31. import java.awt.image.WritableRaster;
  32. import java.io.IOException;
  33. import org.apache.xmlgraphics.util.UnitConv;
  34. import org.apache.fop.render.RendererContext.RendererContextWrapper;
  35. /**
  36. * Abstract base class for Graphics2DAdapter implementations.
  37. */
  38. public abstract class AbstractGraphics2DAdapter implements Graphics2DAdapter {
  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 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. RendererContextWrapper context, int resolution, boolean gray, boolean withAlpha) {
  51. int bmw = mpt2px(context.getWidth(), resolution);
  52. int bmh = mpt2px(context.getHeight(), resolution);
  53. BufferedImage bi;
  54. if (gray) {
  55. if (withAlpha) {
  56. bi = createGrayBufferedImageWithAlpha(bmw, bmh);
  57. } else {
  58. bi = new BufferedImage(bmw, bmh, BufferedImage.TYPE_BYTE_GRAY);
  59. }
  60. } else {
  61. if (withAlpha) {
  62. bi = new BufferedImage(bmw, bmh, BufferedImage.TYPE_INT_ARGB);
  63. } else {
  64. bi = new BufferedImage(bmw, bmh, BufferedImage.TYPE_INT_RGB);
  65. }
  66. }
  67. Graphics2D g2d = bi.createGraphics();
  68. try {
  69. g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
  70. RenderingHints.VALUE_FRACTIONALMETRICS_ON);
  71. setRenderingHintsForBufferedImage(g2d);
  72. g2d.setBackground(Color.white);
  73. g2d.setColor(Color.black);
  74. if (!withAlpha) {
  75. g2d.clearRect(0, 0, bmw, bmh);
  76. }
  77. /* debug code
  78. int off = 2;
  79. g2d.drawLine(off, 0, off, bmh);
  80. g2d.drawLine(bmw - off, 0, bmw - off, bmh);
  81. g2d.drawLine(0, off, bmw, off);
  82. g2d.drawLine(0, bmh - off, bmw, bmh - off);
  83. */
  84. double sx = (double)bmw / context.getWidth();
  85. double sy = (double)bmh / context.getHeight();
  86. g2d.scale(sx, sy);
  87. //Paint the image on the BufferedImage
  88. Rectangle2D area = new Rectangle2D.Double(
  89. 0.0, 0.0, context.getWidth(), context.getHeight());
  90. painter.paint(g2d, area);
  91. } finally {
  92. g2d.dispose();
  93. }
  94. return bi;
  95. }
  96. /**
  97. * Converts millipoints to pixels
  98. *
  99. * @param unit the unit to convert in mpts
  100. * @param resolution the target resolution
  101. * @return the converted unit in pixels
  102. */
  103. protected int mpt2px(int unit, int resolution) {
  104. return (int)Math.ceil(UnitConv.mpt2px(unit, resolution));
  105. }
  106. private static BufferedImage createGrayBufferedImageWithAlpha(int width, int height) {
  107. BufferedImage bi;
  108. boolean alphaPremultiplied = true;
  109. int bands = 2;
  110. int[] bits = new int[bands];
  111. for (int i = 0; i < bands; i++) {
  112. bits[i] = 8;
  113. }
  114. ColorModel cm = new ComponentColorModel(
  115. ColorSpace.getInstance(ColorSpace.CS_GRAY),
  116. bits,
  117. true, alphaPremultiplied,
  118. Transparency.TRANSLUCENT,
  119. DataBuffer.TYPE_BYTE);
  120. WritableRaster wr = Raster.createInterleavedRaster(
  121. DataBuffer.TYPE_BYTE,
  122. width, height, bands,
  123. new Point(0, 0));
  124. bi = new BufferedImage(cm, wr, alphaPremultiplied, null);
  125. return bi;
  126. }
  127. /**
  128. * Sets rendering hints on the Graphics2D created for painting to a BufferedImage. Subclasses
  129. * can modify the settings to customize the behaviour.
  130. * @param g2d the Graphics2D instance
  131. */
  132. protected void setRenderingHintsForBufferedImage(Graphics2D g2d) {
  133. g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
  134. RenderingHints.VALUE_ANTIALIAS_OFF);
  135. g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
  136. RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
  137. }
  138. /**
  139. * {@inheritDoc}
  140. * @deprecated
  141. */
  142. public void paintImage(Graphics2DImagePainter painter,
  143. RendererContext context,
  144. int x, int y, int width, int height) throws IOException {
  145. //TODO Deprecated method to be removed once Barcode4J 2.1 is released.
  146. paintImage((org.apache.xmlgraphics.java2d.Graphics2DImagePainter)painter,
  147. context, x, y, width, height);
  148. }
  149. }