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.

BitmapPainter.java 3.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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.hslf.blip;
  16. import org.apache.poi.hslf.usermodel.HSLFPictureData;
  17. import org.apache.poi.hslf.usermodel.HSLFPictureShape;
  18. import org.apache.poi.util.POILogger;
  19. import org.apache.poi.util.POILogFactory;
  20. /* ====================================================================
  21. Licensed to the Apache Software Foundation (ASF) under one or more
  22. contributor license agreements. See the NOTICE file distributed with
  23. this work for additional information regarding copyright ownership.
  24. The ASF licenses this file to You under the Apache License, Version 2.0
  25. (the "License"); you may not use this file except in compliance with
  26. the License. You may obtain a copy of the License at
  27. http://www.apache.org/licenses/LICENSE-2.0
  28. Unless required by applicable law or agreed to in writing, software
  29. distributed under the License is distributed on an "AS IS" BASIS,
  30. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  31. See the License for the specific language governing permissions and
  32. limitations under the License.
  33. ==================================================================== */
  34. import javax.imageio.ImageIO;
  35. import java.awt.*;
  36. import java.awt.geom.AffineTransform;
  37. import java.awt.image.BufferedImage;
  38. import java.io.ByteArrayInputStream;
  39. /**
  40. * Creates BufferedImage using javax.imageio.ImageIO and draws it in the specified graphics.
  41. *
  42. * @author Yegor Kozlov.
  43. */
  44. public final class BitmapPainter implements ImagePainter {
  45. protected POILogger logger = POILogFactory.getLogger(this.getClass());
  46. public void paint(Graphics2D graphics, HSLFPictureData pict, HSLFPictureShape parent) {
  47. BufferedImage img;
  48. try {
  49. img = ImageIO.read(new ByteArrayInputStream(pict.getData()));
  50. } catch (Exception e) {
  51. logger.log(POILogger.WARN, "ImageIO failed to create image. image.type: " + pict.getType());
  52. return;
  53. }
  54. boolean isClipped = true;
  55. Insets clip = parent.getBlipClip();
  56. if (clip == null) {
  57. isClipped = false;
  58. clip = new Insets(0,0,0,0);
  59. }
  60. int iw = img.getWidth();
  61. int ih = img.getHeight();
  62. Rectangle anchor = parent.getLogicalAnchor2D().getBounds();
  63. double cw = (100000-clip.left-clip.right) / 100000.0;
  64. double ch = (100000-clip.top-clip.bottom) / 100000.0;
  65. double sx = anchor.getWidth()/(iw*cw);
  66. double sy = anchor.getHeight()/(ih*ch);
  67. double tx = anchor.getX()-(iw*sx*clip.left/100000.0);
  68. double ty = anchor.getY()-(ih*sy*clip.top/100000.0);
  69. AffineTransform at = new AffineTransform(sx, 0, 0, sy, tx, ty) ;
  70. Shape clipOld = graphics.getClip();
  71. if (isClipped) graphics.clip(anchor.getBounds2D());
  72. graphics.drawRenderedImage(img, at);
  73. graphics.setClip(clipOld);
  74. }
  75. }