mirror of https://github.com/apache/poi.git
bug#44692 - Fixed HSSFPicture.resize() to properly resize pictures if the underlying columns/rows have modified size
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@664515 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
0cc7221cd1
commit
0c30e98366
|
@ -37,6 +37,7 @@
|
||||||
|
|
||||||
<!-- Don't forget to update status.xml too! -->
|
<!-- Don't forget to update status.xml too! -->
|
||||||
<release version="3.1-final" date="2008-06-??">
|
<release version="3.1-final" date="2008-06-??">
|
||||||
|
<action dev="POI-DEVELOPERS" type="fix">44692 - Fixed HSSFPicture.resize() to properly resize pictures if the underlying columns/rows have modified size</action>
|
||||||
<action dev="POI-DEVELOPERS" type="add">Support custom image renderers in HSLF</action>
|
<action dev="POI-DEVELOPERS" type="add">Support custom image renderers in HSLF</action>
|
||||||
<action dev="POI-DEVELOPERS" type="fix">Correctly increment the reference count of a blip when a picture is inserted</action>
|
<action dev="POI-DEVELOPERS" type="fix">Correctly increment the reference count of a blip when a picture is inserted</action>
|
||||||
<action dev="POI-DEVELOPERS" type="fix">45110 - Fixed TextShape.resizeToFitText() to properly resize TextShape</action>
|
<action dev="POI-DEVELOPERS" type="fix">45110 - Fixed TextShape.resizeToFitText() to properly resize TextShape</action>
|
||||||
|
|
|
@ -34,6 +34,7 @@
|
||||||
<!-- Don't forget to update changes.xml too! -->
|
<!-- Don't forget to update changes.xml too! -->
|
||||||
<changes>
|
<changes>
|
||||||
<release version="3.1-final" date="2008-06-??">
|
<release version="3.1-final" date="2008-06-??">
|
||||||
|
<action dev="POI-DEVELOPERS" type="fix">44692 - Fixed HSSFPicture.resize() to properly resize pictures if the underlying columns/rows have modified size</action>
|
||||||
<action dev="POI-DEVELOPERS" type="add">Support custom image renderers in HSLF</action>
|
<action dev="POI-DEVELOPERS" type="add">Support custom image renderers in HSLF</action>
|
||||||
<action dev="POI-DEVELOPERS" type="fix">Correctly increment the reference count of a blip when a picture is inserted</action>
|
<action dev="POI-DEVELOPERS" type="fix">Correctly increment the reference count of a blip when a picture is inserted</action>
|
||||||
<action dev="POI-DEVELOPERS" type="fix">45110 - Fixed TextShape.resizeToFitText() to properly resize TextShape</action>
|
<action dev="POI-DEVELOPERS" type="fix">45110 - Fixed TextShape.resizeToFitText() to properly resize TextShape</action>
|
||||||
|
|
|
@ -26,6 +26,7 @@ import javax.imageio.ImageIO;
|
||||||
import javax.imageio.ImageReader;
|
import javax.imageio.ImageReader;
|
||||||
import javax.imageio.stream.ImageInputStream;
|
import javax.imageio.stream.ImageInputStream;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.awt.*;
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
@ -46,6 +47,20 @@ public class HSSFPicture
|
||||||
public static final int PICTURE_TYPE_PNG = HSSFWorkbook.PICTURE_TYPE_PNG; // PNG
|
public static final int PICTURE_TYPE_PNG = HSSFWorkbook.PICTURE_TYPE_PNG; // PNG
|
||||||
public static final int PICTURE_TYPE_DIB = HSSFWorkbook.PICTURE_TYPE_DIB; // Windows DIB
|
public static final int PICTURE_TYPE_DIB = HSSFWorkbook.PICTURE_TYPE_DIB; // Windows DIB
|
||||||
|
|
||||||
|
/**
|
||||||
|
* width of 1px in columns with default width in units of 1/256 of a character width
|
||||||
|
*/
|
||||||
|
private static final float PX_DEFAULT = 32.00f;
|
||||||
|
/**
|
||||||
|
* width of 1px in columns with overridden width in units of 1/256 of a character width
|
||||||
|
*/
|
||||||
|
private static final float PX_MODIFIED = 36.56f;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Height of 1px of a row
|
||||||
|
*/
|
||||||
|
private static final int PX_ROW = 15;
|
||||||
|
|
||||||
int pictureIndex;
|
int pictureIndex;
|
||||||
HSSFPatriarch patriarch;
|
HSSFPatriarch patriarch;
|
||||||
|
|
||||||
|
@ -100,59 +115,77 @@ public class HSSFPicture
|
||||||
* @since POI 3.0.2
|
* @since POI 3.0.2
|
||||||
*/
|
*/
|
||||||
public HSSFClientAnchor getPreferredSize(){
|
public HSSFClientAnchor getPreferredSize(){
|
||||||
HSSFClientAnchor anchor = new HSSFClientAnchor();
|
HSSFClientAnchor anchor = (HSSFClientAnchor)getAnchor();
|
||||||
|
|
||||||
EscherBSERecord bse = (EscherBSERecord)patriarch.sheet.book.getBSERecord(pictureIndex);
|
Dimension size = getImageDimension();
|
||||||
byte[] data = bse.getBlipRecord().getPicturedata();
|
|
||||||
int type = bse.getBlipTypeWin32();
|
|
||||||
switch (type){
|
|
||||||
//we can calculate the preferred size only for JPEG and PNG
|
|
||||||
//other formats like WMF, EMF and PICT are not supported in Java
|
|
||||||
case HSSFWorkbook.PICTURE_TYPE_JPEG:
|
|
||||||
case HSSFWorkbook.PICTURE_TYPE_PNG:
|
|
||||||
BufferedImage img = null;
|
|
||||||
ImageReader r = null;
|
|
||||||
try {
|
|
||||||
//read the image using javax.imageio.*
|
|
||||||
ImageInputStream iis = ImageIO.createImageInputStream( new ByteArrayInputStream(data) );
|
|
||||||
Iterator i = ImageIO.getImageReaders( iis );
|
|
||||||
r = (ImageReader) i.next();
|
|
||||||
r.setInput( iis );
|
|
||||||
img = r.read(0);
|
|
||||||
|
|
||||||
int[] dpi = getResolution(r);
|
float w = 0;
|
||||||
int imgWidth = img.getWidth()*96/dpi[0];
|
|
||||||
int imgHeight = img.getHeight()*96/dpi[1];
|
|
||||||
|
|
||||||
//Excel measures cells in units of 1/256th of a character width.
|
//space in the leftmost cell
|
||||||
//The cell width calculated based on this info is always "off".
|
w += getColumnWidthInPixels(anchor.col1)*(1 - anchor.dx1/1024);
|
||||||
//A better approach seems to be to use empirically obtained cell width and row height
|
short col2 = (short)(anchor.col1 + 1);
|
||||||
int cellwidth = 64;
|
int dx2 = 0;
|
||||||
int rowheight = 17;
|
|
||||||
|
|
||||||
int col2 = imgWidth/cellwidth;
|
while(w < size.width){
|
||||||
int row2 = imgHeight/rowheight;
|
w += getColumnWidthInPixels(col2++);
|
||||||
|
|
||||||
int dx2 = (int)((float)(imgWidth % cellwidth)/cellwidth * 1024);
|
|
||||||
int dy2 = (int)((float)(imgHeight % rowheight)/rowheight * 256);
|
|
||||||
|
|
||||||
anchor.setCol2((short)col2);
|
|
||||||
anchor.setDx2(dx2);
|
|
||||||
|
|
||||||
anchor.setRow2(row2);
|
|
||||||
anchor.setDy2(dy2);
|
|
||||||
|
|
||||||
} catch (IOException e){
|
|
||||||
//silently return if ImageIO failed to read the image
|
|
||||||
log.log(POILogger.WARN, e);
|
|
||||||
img = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(w > size.width) {
|
||||||
|
//calculate dx2, offset in the rightmost cell
|
||||||
|
col2--;
|
||||||
|
float cw = getColumnWidthInPixels(col2);
|
||||||
|
float delta = w - size.width;
|
||||||
|
dx2 = (int)((cw-delta)/cw*1024);
|
||||||
|
}
|
||||||
|
anchor.col2 = col2;
|
||||||
|
anchor.dx2 = dx2;
|
||||||
|
|
||||||
|
float h = 0;
|
||||||
|
h += (1 - anchor.dy1/256)* getRowHeightInPixels(anchor.row1);
|
||||||
|
int row2 = anchor.row1 + 1;
|
||||||
|
int dy2 = 0;
|
||||||
|
|
||||||
|
while(h < size.height){
|
||||||
|
h += getRowHeightInPixels(row2++);
|
||||||
|
}
|
||||||
|
if(h > size.height) {
|
||||||
|
row2--;
|
||||||
|
float ch = getRowHeightInPixels(row2);
|
||||||
|
float delta = h - size.height;
|
||||||
|
dy2 = (int)((ch-delta)/ch*256);
|
||||||
|
}
|
||||||
|
anchor.row2 = row2;
|
||||||
|
anchor.dy2 = dy2;
|
||||||
|
|
||||||
return anchor;
|
return anchor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private float getColumnWidthInPixels(short column){
|
||||||
|
|
||||||
|
short cw = patriarch.sheet.getColumnWidth(column);
|
||||||
|
float px = getPixelWidth(column);
|
||||||
|
|
||||||
|
return cw/px;
|
||||||
|
}
|
||||||
|
|
||||||
|
private float getRowHeightInPixels(int i){
|
||||||
|
|
||||||
|
HSSFRow row = patriarch.sheet.getRow(i);
|
||||||
|
float height;
|
||||||
|
if(row != null) height = row.getHeight();
|
||||||
|
else height = patriarch.sheet.getDefaultRowHeight();
|
||||||
|
|
||||||
|
return height/PX_ROW;
|
||||||
|
}
|
||||||
|
|
||||||
|
private float getPixelWidth(short column){
|
||||||
|
|
||||||
|
int def = patriarch.sheet.getDefaultColumnWidth()*256;
|
||||||
|
short cw = patriarch.sheet.getColumnWidth(column);
|
||||||
|
|
||||||
|
return cw == def ? PX_DEFAULT : PX_MODIFIED;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The metadata of PNG and JPEG can contain the width of a pixel in millimeters.
|
* The metadata of PNG and JPEG can contain the width of a pixel in millimeters.
|
||||||
* Return the the "effective" dpi calculated as <code>25.4/HorizontalPixelSize</code>
|
* Return the the "effective" dpi calculated as <code>25.4/HorizontalPixelSize</code>
|
||||||
|
@ -176,4 +209,42 @@ public class HSSFPicture
|
||||||
return new int[]{hdpi, vdpi};
|
return new int[]{hdpi, vdpi};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the dimension of this image
|
||||||
|
*
|
||||||
|
* @return image dimension
|
||||||
|
*/
|
||||||
|
public Dimension getImageDimension(){
|
||||||
|
EscherBSERecord bse = patriarch.sheet.book.getBSERecord(pictureIndex);
|
||||||
|
byte[] data = bse.getBlipRecord().getPicturedata();
|
||||||
|
int type = bse.getBlipTypeWin32();
|
||||||
|
Dimension size = new Dimension();
|
||||||
|
|
||||||
|
switch (type){
|
||||||
|
//we can calculate the preferred size only for JPEG and PNG
|
||||||
|
//other formats like WMF, EMF and PICT are not supported in Java
|
||||||
|
case HSSFWorkbook.PICTURE_TYPE_JPEG:
|
||||||
|
case HSSFWorkbook.PICTURE_TYPE_PNG:
|
||||||
|
case HSSFWorkbook.PICTURE_TYPE_DIB:
|
||||||
|
try {
|
||||||
|
//read the image using javax.imageio.*
|
||||||
|
ImageInputStream iis = ImageIO.createImageInputStream( new ByteArrayInputStream(data) );
|
||||||
|
Iterator i = ImageIO.getImageReaders( iis );
|
||||||
|
ImageReader r = (ImageReader) i.next();
|
||||||
|
r.setInput( iis );
|
||||||
|
BufferedImage img = r.read(0);
|
||||||
|
|
||||||
|
int[] dpi = getResolution(r);
|
||||||
|
size.width = img.getWidth()*96/dpi[0];
|
||||||
|
size.height = img.getHeight()*96/dpi[1];
|
||||||
|
|
||||||
|
} catch (IOException e){
|
||||||
|
//silently return if ImageIO failed to read the image
|
||||||
|
log.log(POILogger.WARN, e);
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return size;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -54,7 +54,7 @@ import java.awt.*;
|
||||||
* PictureData.setImagePainter(Picture.WMF, new WMFPaiter());
|
* PictureData.setImagePainter(Picture.WMF, new WMFPaiter());
|
||||||
* ...
|
* ...
|
||||||
* </code>
|
* </code>
|
||||||
* Subsequenet calls of Slide.draw(Graphics gr) will use WMFPaiter for WMF images.
|
* Subsequent calls of Slide.draw(Graphics gr) will use WMFPaiter for WMF images.
|
||||||
*
|
*
|
||||||
* @author Yegor Kozlov.
|
* @author Yegor Kozlov.
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in New Issue