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.

HwmfImageRenderer.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hwmf.draw;
  16. import java.awt.Dimension;
  17. import java.awt.Graphics2D;
  18. import java.awt.Insets;
  19. import java.awt.RenderingHints;
  20. import java.awt.geom.Dimension2D;
  21. import java.awt.geom.Rectangle2D;
  22. import java.awt.image.BufferedImage;
  23. import java.awt.image.RescaleOp;
  24. import java.io.ByteArrayInputStream;
  25. import java.io.IOException;
  26. import java.io.InputStream;
  27. import org.apache.poi.hwmf.usermodel.HwmfPicture;
  28. import org.apache.poi.sl.draw.BitmapImageRenderer;
  29. import org.apache.poi.sl.draw.DrawPictureShape;
  30. import org.apache.poi.sl.draw.ImageRenderer;
  31. import org.apache.poi.sl.usermodel.PictureData.PictureType;
  32. import org.apache.poi.util.Units;
  33. /**
  34. * Helper class which is instantiated by {@link DrawPictureShape}
  35. * via reflection
  36. */
  37. public class HwmfImageRenderer implements ImageRenderer {
  38. HwmfPicture image;
  39. double alpha;
  40. @Override
  41. public boolean canRender(String contentType) {
  42. return PictureType.WMF.contentType.equalsIgnoreCase(contentType);
  43. }
  44. @Override
  45. public void loadImage(InputStream data, String contentType) throws IOException {
  46. if (!PictureType.WMF.contentType.equals(contentType)) {
  47. throw new IOException("Invalid picture type");
  48. }
  49. image = new HwmfPicture(data);
  50. }
  51. @Override
  52. public void loadImage(byte[] data, String contentType) throws IOException {
  53. if (!PictureType.WMF.contentType.equals(contentType)) {
  54. throw new IOException("Invalid picture type");
  55. }
  56. image = new HwmfPicture(new ByteArrayInputStream(data));
  57. }
  58. @Override
  59. public Dimension2D getDimension() {
  60. return Units.pointsToPixel(image == null ? new Dimension() : image.getSize());
  61. }
  62. @Override
  63. public void setAlpha(double alpha) {
  64. this.alpha = alpha;
  65. }
  66. @Override
  67. public BufferedImage getImage() {
  68. return getImage(getDimension());
  69. }
  70. @Override
  71. public BufferedImage getImage(Dimension2D dim) {
  72. if (image == null) {
  73. return new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
  74. }
  75. BufferedImage bufImg = new BufferedImage((int)dim.getWidth(), (int)dim.getHeight(), BufferedImage.TYPE_INT_ARGB);
  76. Graphics2D g = bufImg.createGraphics();
  77. g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  78. g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
  79. g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
  80. g.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON);
  81. image.draw(g, new Rectangle2D.Double(0,0,dim.getWidth(),dim.getHeight()));
  82. g.dispose();
  83. return BitmapImageRenderer.setAlpha(bufImg, alpha);
  84. }
  85. @Override
  86. public boolean drawImage(Graphics2D graphics, Rectangle2D anchor) {
  87. return drawImage(graphics, anchor, null);
  88. }
  89. @Override
  90. public boolean drawImage(Graphics2D graphics, Rectangle2D anchor, Insets clip) {
  91. if (image == null) {
  92. return false;
  93. } else {
  94. image.draw(graphics, anchor);
  95. return true;
  96. }
  97. }
  98. }