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.

Java2DImageHandlerRenderedImage.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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.java2d;
  19. import java.awt.Graphics2D;
  20. import java.awt.Rectangle;
  21. import java.awt.geom.AffineTransform;
  22. import java.awt.image.BufferedImage;
  23. import java.awt.image.RenderedImage;
  24. import java.awt.image.WritableRaster;
  25. import java.io.IOException;
  26. import org.apache.xmlgraphics.image.GraphicsConstants;
  27. import org.apache.xmlgraphics.image.loader.Image;
  28. import org.apache.xmlgraphics.image.loader.ImageFlavor;
  29. import org.apache.xmlgraphics.image.loader.ImageInfo;
  30. import org.apache.xmlgraphics.image.loader.impl.ImageRendered;
  31. import org.apache.fop.render.ImageHandler;
  32. import org.apache.fop.render.RenderingContext;
  33. /**
  34. * Image handler implementation that paints {@link RenderedImage} instances on a {@link Graphics2D}
  35. * object.
  36. */
  37. public class Java2DImageHandlerRenderedImage implements ImageHandler {
  38. /** {@inheritDoc} */
  39. public int getPriority() {
  40. return 300;
  41. }
  42. /** {@inheritDoc} */
  43. public Class getSupportedImageClass() {
  44. return ImageRendered.class;
  45. }
  46. /** {@inheritDoc} */
  47. public ImageFlavor[] getSupportedImageFlavors() {
  48. return new ImageFlavor[] {
  49. ImageFlavor.BUFFERED_IMAGE,
  50. ImageFlavor.RENDERED_IMAGE,
  51. };
  52. }
  53. /** {@inheritDoc} */
  54. public void handleImage(RenderingContext context, Image image, Rectangle pos)
  55. throws IOException {
  56. Java2DRenderingContext java2dContext = (Java2DRenderingContext)context;
  57. ImageInfo info = image.getInfo();
  58. ImageRendered imageRend = (ImageRendered)image;
  59. Graphics2D g2d = java2dContext.getGraphics2D();
  60. AffineTransform at = new AffineTransform();
  61. at.translate(pos.x, pos.y);
  62. //scaling based on layout instructions
  63. double sx = pos.getWidth() / (double)info.getSize().getWidthMpt();
  64. double sy = pos.getHeight() / (double)info.getSize().getHeightMpt();
  65. //scaling because of image resolution
  66. //float sourceResolution = java2dContext.getUserAgent().getSourceResolution();
  67. //source resolution seems to be a bad idea, not sure why
  68. float sourceResolution = GraphicsConstants.DEFAULT_DPI;
  69. sourceResolution *= 1000; //we're working in the millipoint area
  70. sx *= sourceResolution / info.getSize().getDpiHorizontal();
  71. sy *= sourceResolution / info.getSize().getDpiVertical();
  72. at.scale(sx, sy);
  73. RenderedImage rend = imageRend.getRenderedImage();
  74. if (imageRend.getTransparentColor() != null && !rend.getColorModel().hasAlpha()) {
  75. int transCol = imageRend.getTransparentColor().getRGB();
  76. BufferedImage bufImage = makeTransparentImage(rend);
  77. WritableRaster alphaRaster = bufImage.getAlphaRaster();
  78. //TODO Masked images: Does anyone know a more efficient method to do this?
  79. final int[] transparent = new int[] {0x00};
  80. for (int y = 0, maxy = bufImage.getHeight(); y < maxy; y++) {
  81. for (int x = 0, maxx = bufImage.getWidth(); x < maxx; x++) {
  82. int col = bufImage.getRGB(x, y);
  83. if (col == transCol) {
  84. //Mask out all pixels that match the transparent color
  85. alphaRaster.setPixel(x, y, transparent);
  86. }
  87. }
  88. }
  89. g2d.drawRenderedImage(bufImage, at);
  90. } else {
  91. g2d.drawRenderedImage(rend, at);
  92. }
  93. }
  94. private BufferedImage makeTransparentImage(RenderedImage src) {
  95. BufferedImage bufImage = new BufferedImage(src.getWidth(), src.getHeight(),
  96. BufferedImage.TYPE_INT_ARGB);
  97. Graphics2D g2d = bufImage.createGraphics();
  98. g2d.drawRenderedImage(src, new AffineTransform());
  99. g2d.dispose();
  100. return bufImage;
  101. }
  102. /** {@inheritDoc} */
  103. public boolean isCompatible(RenderingContext targetContext, Image image) {
  104. return (image == null || image instanceof ImageRendered)
  105. && targetContext instanceof Java2DRenderingContext;
  106. }
  107. }