Bug 64213 - Picture.resize(double scale) scales width wrong for small pictures and when dx1 is set.
Summary: Picture.resize(double scale) scales width wrong for small pictures and when d...
Status: RESOLVED FIXED
Alias: None
Product: POI
Classification: Unclassified
Component: SS Common (show other bugs)
Version: 4.1.2-FINAL
Hardware: PC All
: P2 normal (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-03-10 12:52 UTC by Axel Richter
Modified: 2020-03-16 18:49 UTC (History)
1 user (show)



Attachments
Images used (139.13 KB, application/x-zip-compressed)
2020-03-10 12:52 UTC, Axel Richter
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Axel Richter 2020-03-10 12:52:42 UTC
Created attachment 37088 [details]
Images used

For small pictures and if there is dx1 set in anchor, Picture.resize(double scale) scales width wrong.

Example to reproduce:

import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;

import org.apache.poi.util.IOUtils;
import org.apache.poi.util.Units;

import java.io.InputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;

class PictureResizeBug {

 public static void main(String[] args) throws Exception {

  //Workbook wb = new HSSFWorkbook(); String resultName = "PictureResizeBug.xls";
  Workbook wb = new XSSFWorkbook(); String resultName = "PictureResizeBug.xlsx";
  Sheet sheet = wb.createSheet("Sheet1");

  String picturePath = "./candle200x200.png";
  //String picturePath = "./candle400x400.png";
  int pictureType = Workbook.PICTURE_TYPE_PNG;

  int col1 = 2;
  int dx1 = 100;
  int row1 = 2;
  float scale = 0.5f;

  sheet.setColumnWidth(col1, 50 * 256);

  //load the picture
  InputStream inputStream = new FileInputStream(picturePath);
  byte[] bytes = IOUtils.toByteArray(inputStream);
  int pictureIdx = wb.addPicture(bytes, pictureType);
  inputStream.close();

  //create an anchor with upper left cell column/startRow, only one cell anchor since bottom right depends on resizing
  CreationHelper helper = wb.getCreationHelper();
  ClientAnchor anchor = helper.createClientAnchor();
  anchor.setCol1(col1);
  if (wb instanceof XSSFWorkbook) {
   anchor.setDx1(dx1 * Units.EMU_PER_PIXEL);
  } else if (wb instanceof HSSFWorkbook) {
   anchor.setDx1(dx1);
  }
  anchor.setRow1(row1);

  //create a picture anchored to Col1 and Row1
  Drawing drawing = sheet.createDrawingPatriarch();
  Picture pict = drawing.createPicture(anchor, pictureIdx);

  //resize the picture to it's native size
  pict.resize();

  //resize the picture scaled proportional
  pict.resize(scale);

  FileOutputStream fileOut = new FileOutputStream(resultName);
  wb.write(fileOut);
  fileOut.close();
  wb.close();

 }
}


Here for candle200x200.png, a 200px + 200px PNG image, the result of this code is a picture in Excel which is correctla scaled 0.25 in heigth but 0.90 in width for HSSF and 1.37 in width for XSSF. If candle400x400.png is used, the scaliing works properly in height and width.
Comment 1 Andreas Beeker 2020-03-10 20:52:47 UTC
I haven't yet checked the problem, but I think the following stackoverflow entry corresponds to the issue (just as a reference ...):

https://stackoverflow.com/questions/60600112/apache-poi-keep-original-picture-scale-while-fitting-within-a-merged-region
Comment 2 Andreas Beeker 2020-03-16 18:49:15 UTC
Thank you for bringing this up ... and your continued support in StackOverflow.

Patched via r1875266