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.

ImageRenderer.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * ====================================================================
  3. * Licensed to the Apache Software Foundation (ASF) under one or more
  4. * contributor license agreements. See the NOTICE file distributed with
  5. * this work for additional information regarding copyright ownership.
  6. * The ASF licenses this file to You under the Apache License, Version 2.0
  7. * (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. * ====================================================================
  18. */
  19. package org.apache.poi.sl.draw;
  20. import java.awt.*;
  21. import java.awt.geom.AffineTransform;
  22. import java.awt.geom.Rectangle2D;
  23. import java.awt.image.BufferedImage;
  24. import java.awt.image.RescaleOp;
  25. import java.io.*;
  26. import javax.imageio.ImageIO;
  27. /**
  28. * For now this class renders only images supported by the javax.imageio.ImageIO
  29. * framework. Subclasses can override this class to support other formats, for
  30. * example, use Apache Batik to render WMF, PICT can be rendered using Apple QuickTime API for Java:
  31. *
  32. * <pre>
  33. * <code>
  34. * public class MyImageRendener extends ImageRendener {
  35. * InputStream data;
  36. *
  37. * public boolean drawImage(Graphics2D graphics,Rectangle2D anchor,Insets clip) {
  38. * // draw image
  39. * DataInputStream is = new DataInputStream(data);
  40. * org.apache.batik.transcoder.wmf.tosvg.WMFRecordStore wmfStore =
  41. * new org.apache.batik.transcoder.wmf.tosvg.WMFRecordStore();
  42. * try {
  43. * wmfStore.read(is);
  44. * } catch (IOException e){
  45. * return;
  46. * }
  47. *
  48. * float scale = (float)anchor.width/wmfStore.getWidthPixels();
  49. *
  50. * org.apache.batik.transcoder.wmf.tosvg.WMFPainter painter =
  51. * new org.apache.batik.transcoder.wmf.tosvg.WMFPainter(wmfStore, 0, 0, scale);
  52. * graphics.translate(anchor.x, anchor.y);
  53. * painter.paint(graphics);
  54. * }
  55. *
  56. * public void loadImage(InputStream data, String contentType) throws IOException {
  57. * if ("image/wmf".equals(contentType)) {
  58. * this.data = data;
  59. * // use Apache Batik to handle WMF
  60. * } else {
  61. * super.loadImage(data,contentType);
  62. * }
  63. * }
  64. * }
  65. * </code>
  66. * </pre>
  67. *
  68. * and then pass this class to your instance of java.awt.Graphics2D:
  69. *
  70. * <pre>
  71. * <code>
  72. * graphics.setRenderingHint(Drawable.IMAGE_RENDERER, new MyImageRendener());
  73. * </code>
  74. * </pre>
  75. */
  76. public class ImageRenderer {
  77. protected BufferedImage img;
  78. /**
  79. * Load and buffer the image
  80. *
  81. * @param data the raw image stream
  82. * @param contentType the content type
  83. */
  84. public void loadImage(InputStream data, String contentType) throws IOException {
  85. img = ImageIO.read(data);
  86. }
  87. /**
  88. * Load and buffer the image
  89. *
  90. * @param data the raw image stream
  91. * @param contentType the content type
  92. */
  93. public void loadImage(byte data[], String contentType) throws IOException {
  94. img = ImageIO.read(new ByteArrayInputStream(data));
  95. }
  96. /**
  97. * @return the buffered image
  98. */
  99. public BufferedImage getImage() {
  100. return img;
  101. }
  102. /**
  103. * @return the dimension of the buffered image
  104. */
  105. public Dimension getDimension() {
  106. return (img == null)
  107. ? new Dimension(0,0)
  108. : new Dimension(img.getWidth(),img.getHeight());
  109. }
  110. /**
  111. * @param alpha the alpha [0..1] to be added to the image (possibly already containing an alpha channel)
  112. */
  113. public void setAlpha(double alpha) {
  114. if (img == null) return;
  115. Dimension dim = getDimension();
  116. BufferedImage newImg = new BufferedImage((int)dim.getWidth(), (int)dim.getHeight(), BufferedImage.TYPE_INT_ARGB);
  117. Graphics2D g = newImg.createGraphics();
  118. RescaleOp op = new RescaleOp(new float[]{1.0f, 1.0f, 1.0f, (float)alpha}, new float[]{0,0,0,0}, null);
  119. g.drawImage(img, op, 0, 0);
  120. g.dispose();
  121. img = newImg;
  122. }
  123. /**
  124. * Render picture data into the supplied graphics
  125. *
  126. * @return true if the picture data was successfully rendered
  127. */
  128. public boolean drawImage(
  129. Graphics2D graphics,
  130. Rectangle2D anchor) {
  131. return drawImage(graphics, anchor, null);
  132. }
  133. /**
  134. * Render picture data into the supplied graphics
  135. *
  136. * @return true if the picture data was successfully rendered
  137. */
  138. public boolean drawImage(
  139. Graphics2D graphics,
  140. Rectangle2D anchor,
  141. Insets clip) {
  142. if (img == null) return false;
  143. boolean isClipped = true;
  144. if (clip == null) {
  145. isClipped = false;
  146. clip = new Insets(0,0,0,0);
  147. }
  148. int iw = img.getWidth();
  149. int ih = img.getHeight();
  150. double cw = (100000-clip.left-clip.right) / 100000.0;
  151. double ch = (100000-clip.top-clip.bottom) / 100000.0;
  152. double sx = anchor.getWidth()/(iw*cw);
  153. double sy = anchor.getHeight()/(ih*ch);
  154. double tx = anchor.getX()-(iw*sx*clip.left/100000.0);
  155. double ty = anchor.getY()-(ih*sy*clip.top/100000.0);
  156. AffineTransform at = new AffineTransform(sx, 0, 0, sy, tx, ty) ;
  157. Shape clipOld = graphics.getClip();
  158. if (isClipped) graphics.clip(anchor.getBounds2D());
  159. graphics.drawRenderedImage(img, at);
  160. graphics.setClip(clipOld);
  161. return true;
  162. }
  163. }