2009-02-13 22:11:16 +00:00
|
|
|
/* ====================================================================
|
|
|
|
|
Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
|
contributor license agreements. See the NOTICE file distributed with
|
|
|
|
|
this work for additional information regarding copyright ownership.
|
|
|
|
|
The ASF licenses this file to You under the Apache License, Version 2.0
|
|
|
|
|
(the "License"); you may not use this file except in compliance with
|
|
|
|
|
the License. You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License.
|
|
|
|
|
==================================================================== */
|
|
|
|
|
|
2005-05-01 22:00:12 +00:00
|
|
|
package org.apache.poi.hssf.usermodel;
|
|
|
|
|
|
2007-10-01 18:07:32 +00:00
|
|
|
import org.apache.poi.ddf.EscherBSERecord;
|
|
|
|
|
import org.apache.poi.util.POILogFactory;
|
|
|
|
|
import org.apache.poi.util.POILogger;
|
2008-11-12 07:15:37 +00:00
|
|
|
import org.apache.poi.ss.usermodel.Picture;
|
2009-11-06 19:08:41 +00:00
|
|
|
import org.apache.poi.ss.util.ImageUtils;
|
2007-10-01 18:07:32 +00:00
|
|
|
import org.w3c.dom.Element;
|
|
|
|
|
import org.w3c.dom.NodeList;
|
|
|
|
|
|
|
|
|
|
import javax.imageio.ImageIO;
|
|
|
|
|
import javax.imageio.ImageReader;
|
|
|
|
|
import javax.imageio.stream.ImageInputStream;
|
|
|
|
|
import java.awt.image.BufferedImage;
|
2008-06-08 15:31:05 +00:00
|
|
|
import java.awt.*;
|
2007-10-01 18:07:32 +00:00
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.util.Iterator;
|
|
|
|
|
|
2005-05-01 22:00:12 +00:00
|
|
|
/**
|
|
|
|
|
* Represents a escher picture. Eg. A GIF, JPEG etc...
|
|
|
|
|
*
|
|
|
|
|
* @author Glen Stampoultzis
|
2007-10-01 18:07:32 +00:00
|
|
|
* @author Yegor Kozlov (yegor at apache.org)
|
2005-05-01 22:00:12 +00:00
|
|
|
*/
|
2009-02-13 22:11:16 +00:00
|
|
|
public final class HSSFPicture extends HSSFSimpleShape implements Picture {
|
2007-10-01 18:07:32 +00:00
|
|
|
public static final int PICTURE_TYPE_EMF = HSSFWorkbook.PICTURE_TYPE_EMF; // Windows Enhanced Metafile
|
|
|
|
|
public static final int PICTURE_TYPE_WMF = HSSFWorkbook.PICTURE_TYPE_WMF; // Windows Metafile
|
|
|
|
|
public static final int PICTURE_TYPE_PICT = HSSFWorkbook.PICTURE_TYPE_PICT; // Macintosh PICT
|
|
|
|
|
public static final int PICTURE_TYPE_JPEG = HSSFWorkbook.PICTURE_TYPE_JPEG; // JFIF
|
|
|
|
|
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
|
2005-05-01 22:00:12 +00:00
|
|
|
|
2008-06-08 15:31:05 +00:00
|
|
|
/**
|
|
|
|
|
* 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;
|
|
|
|
|
|
2009-02-13 22:11:16 +00:00
|
|
|
private int _pictureIndex;
|
|
|
|
|
HSSFPatriarch _patriarch; // TODO make private
|
2007-10-01 18:07:32 +00:00
|
|
|
|
|
|
|
|
private static final POILogger log = POILogFactory.getLogger(HSSFPicture.class);
|
2005-05-01 22:00:12 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Constructs a picture object.
|
|
|
|
|
*/
|
|
|
|
|
HSSFPicture( HSSFShape parent, HSSFAnchor anchor )
|
|
|
|
|
{
|
|
|
|
|
super( parent, anchor );
|
|
|
|
|
setShapeType(OBJECT_TYPE_PICTURE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int getPictureIndex()
|
|
|
|
|
{
|
2009-02-13 22:11:16 +00:00
|
|
|
return _pictureIndex;
|
2005-05-01 22:00:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void setPictureIndex( int pictureIndex )
|
|
|
|
|
{
|
2009-02-13 22:11:16 +00:00
|
|
|
this._pictureIndex = pictureIndex;
|
2005-05-01 22:00:12 +00:00
|
|
|
}
|
2007-10-01 18:07:32 +00:00
|
|
|
|
|
|
|
|
/**
|
2008-11-16 13:24:42 +00:00
|
|
|
* Resize the image
|
2007-10-01 18:07:32 +00:00
|
|
|
*
|
2008-11-16 13:24:42 +00:00
|
|
|
* @param scale the amount by which image dimensions are multiplied relative to the original size.
|
|
|
|
|
* <code>resize(1.0)</code> sets the original size, <code>resize(0.5)</code> resize to 50% of the original,
|
|
|
|
|
* <code>resize(2.0)</code> resizes to 200% of the original.
|
2007-10-01 18:07:32 +00:00
|
|
|
*/
|
2008-11-16 13:24:42 +00:00
|
|
|
public void resize(double scale){
|
2007-10-01 18:07:32 +00:00
|
|
|
HSSFClientAnchor anchor = (HSSFClientAnchor)getAnchor();
|
|
|
|
|
anchor.setAnchorType(2);
|
|
|
|
|
|
2008-11-16 13:24:42 +00:00
|
|
|
HSSFClientAnchor pref = getPreferredSize(scale);
|
2007-10-01 18:07:32 +00:00
|
|
|
|
|
|
|
|
int row2 = anchor.getRow1() + (pref.getRow2() - pref.getRow1());
|
|
|
|
|
int col2 = anchor.getCol1() + (pref.getCol2() - pref.getCol1());
|
|
|
|
|
|
|
|
|
|
anchor.setCol2((short)col2);
|
|
|
|
|
anchor.setDx1(0);
|
|
|
|
|
anchor.setDx2(pref.getDx2());
|
|
|
|
|
|
|
|
|
|
anchor.setRow2(row2);
|
|
|
|
|
anchor.setDy1(0);
|
|
|
|
|
anchor.setDy2(pref.getDy2());
|
|
|
|
|
}
|
|
|
|
|
|
2008-11-16 13:24:42 +00:00
|
|
|
/**
|
|
|
|
|
* Reset the image to the original size.
|
|
|
|
|
*/
|
|
|
|
|
public void resize(){
|
|
|
|
|
resize(1.0);
|
|
|
|
|
}
|
|
|
|
|
|
2007-10-01 18:07:32 +00:00
|
|
|
/**
|
2007-11-03 19:50:40 +00:00
|
|
|
* Calculate the preferred size for this picture.
|
2007-10-01 18:07:32 +00:00
|
|
|
*
|
2007-11-03 19:50:40 +00:00
|
|
|
* @return HSSFClientAnchor with the preferred size for this image
|
2007-10-01 18:07:32 +00:00
|
|
|
* @since POI 3.0.2
|
|
|
|
|
*/
|
2007-11-03 19:50:40 +00:00
|
|
|
public HSSFClientAnchor getPreferredSize(){
|
2008-11-16 13:24:42 +00:00
|
|
|
return getPreferredSize(1.0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Calculate the preferred size for this picture.
|
|
|
|
|
*
|
|
|
|
|
* @param scale the amount by which image dimensions are multiplied relative to the original size.
|
|
|
|
|
* @return HSSFClientAnchor with the preferred size for this image
|
|
|
|
|
* @since POI 3.0.2
|
|
|
|
|
*/
|
|
|
|
|
public HSSFClientAnchor getPreferredSize(double scale){
|
2008-06-08 15:31:05 +00:00
|
|
|
HSSFClientAnchor anchor = (HSSFClientAnchor)getAnchor();
|
2007-10-01 18:07:32 +00:00
|
|
|
|
2008-06-08 15:31:05 +00:00
|
|
|
Dimension size = getImageDimension();
|
2008-11-16 13:24:42 +00:00
|
|
|
double scaledWidth = size.getWidth() * scale;
|
|
|
|
|
double scaledHeight = size.getHeight() * scale;
|
2007-10-01 18:07:32 +00:00
|
|
|
|
2008-06-08 15:31:05 +00:00
|
|
|
float w = 0;
|
2007-10-01 18:07:32 +00:00
|
|
|
|
2008-06-08 15:31:05 +00:00
|
|
|
//space in the leftmost cell
|
2008-11-16 13:24:42 +00:00
|
|
|
w += getColumnWidthInPixels(anchor.col1)*(1 - (float)anchor.dx1/1024);
|
2008-06-08 15:31:05 +00:00
|
|
|
short col2 = (short)(anchor.col1 + 1);
|
|
|
|
|
int dx2 = 0;
|
2007-10-01 18:07:32 +00:00
|
|
|
|
2008-11-16 13:24:42 +00:00
|
|
|
while(w < scaledWidth){
|
2008-06-08 15:31:05 +00:00
|
|
|
w += getColumnWidthInPixels(col2++);
|
|
|
|
|
}
|
2007-10-01 18:07:32 +00:00
|
|
|
|
2008-11-16 13:24:42 +00:00
|
|
|
if(w > scaledWidth) {
|
2008-06-08 15:31:05 +00:00
|
|
|
//calculate dx2, offset in the rightmost cell
|
|
|
|
|
col2--;
|
2008-11-16 13:24:42 +00:00
|
|
|
double cw = getColumnWidthInPixels(col2);
|
|
|
|
|
double delta = w - scaledWidth;
|
2008-06-08 15:31:05 +00:00
|
|
|
dx2 = (int)((cw-delta)/cw*1024);
|
|
|
|
|
}
|
|
|
|
|
anchor.col2 = col2;
|
|
|
|
|
anchor.dx2 = dx2;
|
2007-10-01 18:07:32 +00:00
|
|
|
|
2008-06-08 15:31:05 +00:00
|
|
|
float h = 0;
|
2008-11-16 13:24:42 +00:00
|
|
|
h += (1 - (float)anchor.dy1/256)* getRowHeightInPixels(anchor.row1);
|
2008-06-08 15:31:05 +00:00
|
|
|
int row2 = anchor.row1 + 1;
|
|
|
|
|
int dy2 = 0;
|
2007-10-01 18:07:32 +00:00
|
|
|
|
2008-11-16 13:24:42 +00:00
|
|
|
while(h < scaledHeight){
|
2008-06-08 15:31:05 +00:00
|
|
|
h += getRowHeightInPixels(row2++);
|
|
|
|
|
}
|
2008-11-16 13:24:42 +00:00
|
|
|
if(h > scaledHeight) {
|
2008-06-08 15:31:05 +00:00
|
|
|
row2--;
|
2008-11-16 13:24:42 +00:00
|
|
|
double ch = getRowHeightInPixels(row2);
|
|
|
|
|
double delta = h - scaledHeight;
|
2008-06-08 15:31:05 +00:00
|
|
|
dy2 = (int)((ch-delta)/ch*256);
|
2007-10-01 18:07:32 +00:00
|
|
|
}
|
2008-06-08 15:31:05 +00:00
|
|
|
anchor.row2 = row2;
|
|
|
|
|
anchor.dy2 = dy2;
|
|
|
|
|
|
2007-10-01 18:07:32 +00:00
|
|
|
return anchor;
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-16 21:56:36 +00:00
|
|
|
private float getColumnWidthInPixels(int column){
|
2008-06-08 15:31:05 +00:00
|
|
|
|
2009-02-13 22:11:16 +00:00
|
|
|
int cw = _patriarch._sheet.getColumnWidth(column);
|
2008-06-08 15:31:05 +00:00
|
|
|
float px = getPixelWidth(column);
|
|
|
|
|
|
|
|
|
|
return cw/px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private float getRowHeightInPixels(int i){
|
|
|
|
|
|
2009-02-13 22:11:16 +00:00
|
|
|
HSSFRow row = _patriarch._sheet.getRow(i);
|
2008-06-08 15:31:05 +00:00
|
|
|
float height;
|
|
|
|
|
if(row != null) height = row.getHeight();
|
2009-02-13 22:11:16 +00:00
|
|
|
else height = _patriarch._sheet.getDefaultRowHeight();
|
2008-06-08 15:31:05 +00:00
|
|
|
|
|
|
|
|
return height/PX_ROW;
|
|
|
|
|
}
|
|
|
|
|
|
2008-09-16 21:56:36 +00:00
|
|
|
private float getPixelWidth(int column){
|
2008-06-08 15:31:05 +00:00
|
|
|
|
2009-02-13 22:11:16 +00:00
|
|
|
int def = _patriarch._sheet.getDefaultColumnWidth()*256;
|
|
|
|
|
int cw = _patriarch._sheet.getColumnWidth(column);
|
2008-06-08 15:31:05 +00:00
|
|
|
|
|
|
|
|
return cw == def ? PX_DEFAULT : PX_MODIFIED;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return the dimension of this image
|
|
|
|
|
*
|
|
|
|
|
* @return image dimension
|
|
|
|
|
*/
|
|
|
|
|
public Dimension getImageDimension(){
|
2009-02-13 22:11:16 +00:00
|
|
|
EscherBSERecord bse = _patriarch._sheet._book.getBSERecord(_pictureIndex);
|
2008-06-08 15:31:05 +00:00
|
|
|
byte[] data = bse.getBlipRecord().getPicturedata();
|
|
|
|
|
int type = bse.getBlipTypeWin32();
|
2009-11-06 19:08:41 +00:00
|
|
|
return ImageUtils.getImageDimension(new ByteArrayInputStream(data), type);
|
2008-06-08 15:31:05 +00:00
|
|
|
}
|
2005-05-01 22:00:12 +00:00
|
|
|
}
|