* @param scaleX the amount by which image width is multiplied relative to the original width.
* @param scaleY the amount by which image height is multiplied relative to the original height.
* @return the new Dimensions of the scaled picture in EMUs
*/
public static Dimension setPreferredSize(Picture picture, double scaleX, double scaleY){
ClientAnchor anchor = picture.getClientAnchor();
boolean isHSSF = (anchor instanceof HSSFClientAnchor);
PictureData data = picture.getPictureData();
Sheet sheet = picture.getSheet();
// in pixel
Dimension imgSize = getImageDimension(new ByteArrayInputStream(data.getData()), data.getPictureType());
// in emus
Dimension anchorSize = ImageUtils.getDimensionFromAnchor(picture);
final double scaledWidth = (scaleX == Double.MAX_VALUE)
? imgSize.getWidth() : anchorSize.getWidth()/EMU_PER_PIXEL * scaleX;
final double scaledHeight = (scaleY == Double.MAX_VALUE)
? imgSize.getHeight() : anchorSize.getHeight()/EMU_PER_PIXEL * scaleY;
double w = 0;
int col2 = anchor.getCol1();
int dx2 = 0;
//space in the leftmost cell
w = sheet.getColumnWidthInPixels(col2++);
if (isHSSF) {
w *= 1 - anchor.getDx1()/1024d;
} else {
w -= anchor.getDx1()/EMU_PER_PIXEL;
}
while(w < scaledWidth){
w += sheet.getColumnWidthInPixels(col2++);
}
if(w > scaledWidth) {
//calculate dx2, offset in the rightmost cell
double cw = sheet.getColumnWidthInPixels(--col2);
double delta = w - scaledWidth;
if (isHSSF) {
dx2 = (int)((cw-delta)/cw*1024);
} else {
dx2 = (int)((cw-delta)*EMU_PER_PIXEL);
}
if (dx2 < 0) dx2 = 0;
}
anchor.setCol2(col2);
anchor.setDx2(dx2);
double h = 0;
int row2 = anchor.getRow1();
int dy2 = 0;
h = getRowHeightInPixels(sheet,row2++);
if (isHSSF) {
h *= 1 - anchor.getDy1()/256d;
} else {
h -= anchor.getDy1()/EMU_PER_PIXEL;
}
while(h < scaledHeight){
h += getRowHeightInPixels(sheet,row2++);
}
if(h > scaledHeight) {
double ch = getRowHeightInPixels(sheet,--row2);
double delta = h - scaledHeight;
if (isHSSF) {
dy2 = (int)((ch-delta)/ch*256);
} else {
dy2 = (int)((ch-delta)*EMU_PER_PIXEL);
}
if (dy2 < 0) dy2 = 0;
}
anchor.setRow2(row2);
anchor.setDy2(dy2);
Dimension dim = new Dimension(
(int)Math.round(scaledWidth*EMU_PER_PIXEL),
(int)Math.round(scaledHeight*EMU_PER_PIXEL)
);