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.

BaseTestPicture.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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.ss.usermodel;
  16. import static org.junit.Assert.assertEquals;
  17. import static org.junit.Assert.assertNotNull;
  18. import java.awt.Dimension;
  19. import java.awt.Point;
  20. import java.awt.image.BufferedImage;
  21. import java.io.ByteArrayInputStream;
  22. import java.io.IOException;
  23. import javax.imageio.ImageIO;
  24. import org.apache.poi.hssf.HSSFITestDataProvider;
  25. import org.apache.poi.ss.ITestDataProvider;
  26. import org.apache.poi.ss.util.ImageUtils;
  27. import org.apache.poi.util.Units;
  28. import org.junit.Test;
  29. /**
  30. * @author Yegor Kozlov
  31. */
  32. public abstract class BaseTestPicture {
  33. private final ITestDataProvider _testDataProvider;
  34. protected BaseTestPicture(ITestDataProvider testDataProvider) {
  35. _testDataProvider = testDataProvider;
  36. }
  37. public void baseTestResize(Picture input, Picture compare, double scaleX, double scaleY) {
  38. input.resize(scaleX, scaleY);
  39. ClientAnchor inpCA = input.getClientAnchor();
  40. ClientAnchor cmpCA = compare.getClientAnchor();
  41. Dimension inpDim = ImageUtils.getDimensionFromAnchor(input);
  42. Dimension cmpDim = ImageUtils.getDimensionFromAnchor(compare);
  43. double emuPX = Units.EMU_PER_PIXEL;
  44. assertEquals("the image height differs", inpDim.getHeight(), cmpDim.getHeight(), emuPX*6);
  45. assertEquals("the image width differs", inpDim.getWidth(), cmpDim.getWidth(), emuPX*6);
  46. assertEquals("the starting column differs", inpCA.getCol1(), cmpCA.getCol1());
  47. assertEquals("the column x-offset differs", inpCA.getDx1(), cmpCA.getDx1(), 1);
  48. assertEquals("the column y-offset differs", inpCA.getDy1(), cmpCA.getDy1(), 1);
  49. assertEquals("the ending columns differs", inpCA.getCol2(), cmpCA.getCol2());
  50. // can't compare row heights because of variable test heights
  51. input.resize();
  52. inpDim = ImageUtils.getDimensionFromAnchor(input);
  53. Dimension imgDim = input.getImageDimension();
  54. assertEquals("the image height differs", imgDim.getHeight(), inpDim.getHeight()/emuPX, 1);
  55. assertEquals("the image width differs", imgDim.getWidth(), inpDim.getWidth()/emuPX, 1);
  56. }
  57. @Test
  58. public void testResizeNoColumns() throws IOException {
  59. Workbook wb = _testDataProvider.createWorkbook();
  60. try {
  61. Sheet sheet = wb.createSheet();
  62. Row row = sheet.createRow(0);
  63. handleResize(wb, sheet, row);
  64. } finally {
  65. wb.close();
  66. }
  67. }
  68. @Test
  69. public void testResizeWithColumns() throws IOException {
  70. Workbook wb = _testDataProvider.createWorkbook();
  71. try {
  72. Sheet sheet = wb.createSheet();
  73. Row row = sheet.createRow(0);
  74. row.createCell(0);
  75. handleResize(wb, sheet, row);
  76. } finally {
  77. wb.close();
  78. }
  79. }
  80. private void handleResize(Workbook wb, Sheet sheet, Row row) throws IOException {
  81. Drawing drawing = sheet.createDrawingPatriarch();
  82. CreationHelper createHelper = wb.getCreationHelper();
  83. final byte[] bytes = HSSFITestDataProvider.instance.getTestDataFileContent("logoKarmokar4.png");
  84. row.setHeightInPoints(getImageSize(bytes).y);
  85. int pictureIdx = wb.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
  86. //add a picture shape
  87. ClientAnchor anchor = createHelper.createClientAnchor();
  88. //set top-left corner of the picture,
  89. //subsequent call of Picture#resize() will operate relative to it
  90. anchor.setCol1(0);
  91. anchor.setRow1(0);
  92. Picture pict = drawing.createPicture(anchor, pictureIdx);
  93. //auto-size picture relative to its top-left corner
  94. pict.resize();
  95. }
  96. private static Point getImageSize( byte [] image) throws IOException {
  97. BufferedImage img = ImageIO.read(new ByteArrayInputStream(image));
  98. assertNotNull(img);
  99. return new Point(img.getWidth(), img.getHeight());
  100. }
  101. }