Improve performance of "Image" component and greatly reduce file size

when images are present
This commit is contained in:
Branislav Stojkovic
2019-02-24 14:32:30 +01:00
parent 0d007e5737
commit ce43a5d2e6
9 changed files with 227 additions and 172 deletions
+1
View File
@@ -4,6 +4,7 @@
<classpathentry kind="lib" path="lib/log4j-1.2.15.jar"/> <classpathentry kind="lib" path="lib/log4j-1.2.15.jar"/>
<classpathentry kind="lib" path="lib/app-framework.jar"/> <classpathentry kind="lib" path="lib/app-framework.jar"/>
<classpathentry kind="lib" path="lib/jep-2.4.1.jar"/> <classpathentry kind="lib" path="lib/jep-2.4.1.jar"/>
<classpathentry kind="lib" path="lib/poi-3.5-FINAL-20090928.jar"/>
<classpathentry combineaccessrules="false" kind="src" path="/diylc-core"/> <classpathentry combineaccessrules="false" kind="src" path="/diylc-core"/>
<classpathentry kind="lib" path="lib/xstream-1.4.3.jar"/> <classpathentry kind="lib" path="lib/xstream-1.4.3.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jdk1.6.0_45"/> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jdk1.6.0_45"/>
Binary file not shown.
Binary file not shown.
@@ -1,24 +1,20 @@
/* /*
*
DIY Layout Creator (DIYLC). * DIY Layout Creator (DIYLC). Copyright (c) 2009-2018 held jointly by the individual authors.
Copyright (c) 2009-2018 held jointly by the individual authors. *
* This file is part of DIYLC.
This file is part of DIYLC. *
* DIYLC is free software: you can redistribute it and/or modify it under the terms of the GNU
DIYLC is free software: you can redistribute it and/or modify * General Public License as published by the Free Software Foundation, either version 3 of the
it under the terms of the GNU General Public License as published by * License, or (at your option) any later version.
the Free Software Foundation, either version 3 of the License, or *
(at your option) any later version. * DIYLC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
DIYLC is distributed in the hope that it will be useful, * Public License for more details.
but WITHOUT ANY WARRANTY; without even the implied warranty of *
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * You should have received a copy of the GNU General Public License along with DIYLC. If not, see
GNU General Public License for more details. * <http://www.gnu.org/licenses/>.
*/
You should have received a copy of the GNU General Public License
along with DIYLC. If not, see <http://www.gnu.org/licenses/>.
*/
package org.diylc.components.misc; package org.diylc.components.misc;
import java.awt.AlphaComposite; import java.awt.AlphaComposite;
@@ -27,10 +23,16 @@ import java.awt.Graphics2D;
import java.awt.Point; import java.awt.Point;
import java.awt.Shape; import java.awt.Shape;
import java.awt.geom.Rectangle2D; import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon; import javax.swing.ImageIcon;
import org.apache.poi.util.IOUtils;
import org.diylc.appframework.miscutils.IconImageConverter; import org.diylc.appframework.miscutils.IconImageConverter;
import org.diylc.common.ObjectCache;
import org.diylc.components.AbstractTransparentComponent; import org.diylc.components.AbstractTransparentComponent;
import org.diylc.core.ComponentState; import org.diylc.core.ComponentState;
import org.diylc.core.IDIYComponent; import org.diylc.core.IDIYComponent;
@@ -42,8 +44,11 @@ import org.diylc.core.annotations.ComponentDescriptor;
import org.diylc.core.annotations.EditableProperty; import org.diylc.core.annotations.EditableProperty;
import org.diylc.core.annotations.PercentEditor; import org.diylc.core.annotations.PercentEditor;
import sun.awt.image.ToolkitImage;
import com.thoughtworks.xstream.annotations.XStreamConverter; import com.thoughtworks.xstream.annotations.XStreamConverter;
@ComponentDescriptor(name = "Image", author = "Branislav Stojkovic", category = "Misc", @ComponentDescriptor(name = "Image", author = "Branislav Stojkovic", category = "Misc",
description = "User defined image", instanceNamePrefix = "Img", zOrder = IDIYComponent.COMPONENT, description = "User defined image", instanceNamePrefix = "Img", zOrder = IDIYComponent.COMPONENT,
flexibleZOrder = true, stretchable = false, bomPolicy = BomPolicy.NEVER_SHOW) flexibleZOrder = true, stretchable = false, bomPolicy = BomPolicy.NEVER_SHOW)
@@ -68,16 +73,27 @@ public class Image extends AbstractTransparentComponent<Void> {
} }
private Point point = new Point(0, 0); private Point point = new Point(0, 0);
@XStreamConverter(IconImageConverter.class) @XStreamConverter(IconImageConverter.class)
private ImageIcon image = ICON; @Deprecated
private ImageIcon image;
private byte[] data;
private byte scale = DEFAULT_SCALE; private byte scale = DEFAULT_SCALE;
public Image() {
try {
data = IOUtils.toByteArray(Image.class.getResourceAsStream("image.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
@Override @Override
public void draw(Graphics2D g2d, ComponentState componentState, boolean outlineMode, Project project, public void draw(Graphics2D g2d, ComponentState componentState, boolean outlineMode, Project project,
IDrawingObserver drawingObserver) { IDrawingObserver drawingObserver) {
double s = 1d * scale / DEFAULT_SCALE; double s = 1d * scale / DEFAULT_SCALE;
Shape clip = g2d.getClip().getBounds(); Shape clip = g2d.getClip().getBounds();
if (!clip.intersects(new Rectangle2D.Double(point.getX(), point.getY(), image.getIconWidth() * s, image if (!clip.intersects(new Rectangle2D.Double(point.getX(), point.getY(), getImage().getIconWidth() * s, getImage()
.getIconHeight() * s))) { .getIconHeight() * s))) {
return; return;
} }
@@ -87,12 +103,13 @@ public class Image extends AbstractTransparentComponent<Void> {
} }
g2d.scale(s, s); g2d.scale(s, s);
g2d.drawImage(image.getImage(), (int) (point.x / s), (int) (point.y / s), null); g2d.drawImage(getImage().getImage(), (int) (point.x / s), (int) (point.y / s), null);
if (componentState == ComponentState.SELECTED) { if (componentState == ComponentState.SELECTED) {
g2d.setComposite(oldComposite); g2d.setComposite(oldComposite);
g2d.scale(1 / s, 1 / s); g2d.scale(1 / s, 1 / s);
g2d.setColor(SELECTION_COLOR); g2d.setColor(SELECTION_COLOR);
g2d.drawRect(point.x, point.y, (int) (image.getIconWidth() * s), (int) (image.getIconHeight() * s)); g2d.setStroke(ObjectCache.getInstance().fetchBasicStroke(1f));
g2d.drawRect(point.x, point.y, (int) (getImage().getIconWidth() * s), (int) (getImage().getIconHeight() * s));
} }
} }
@@ -126,13 +143,31 @@ public class Image extends AbstractTransparentComponent<Void> {
this.point.setLocation(point); this.point.setLocation(point);
} }
@EditableProperty(defaultable = false)
public ImageIcon getImage() { public ImageIcon getImage() {
return image; if (image != null) {
BufferedImage bi = ((ToolkitImage) image.getImage()).getBufferedImage();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
ImageIO.write(bi, "png", baos);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
data = baos.toByteArray();
image = null;
}
return new ImageIcon(data);
} }
public void setImage(ImageIcon image) { @EditableProperty(name = "Image")
this.image = image; public byte[] getData() {
return data;
}
public void setData(byte[] data) {
this.data = data;
this.image = null;
} }
@PercentEditor(_100PercentValue = 50) @PercentEditor(_100PercentValue = 50)
Binary file not shown.
Binary file not shown.
@@ -25,7 +25,6 @@ import java.awt.Color;
import java.awt.Component; import java.awt.Component;
import java.awt.Font; import java.awt.Font;
import javax.swing.ImageIcon;
import javax.swing.JLabel; import javax.swing.JLabel;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
@@ -77,7 +76,7 @@ public class FieldEditorFactory {
MeasureArrayEditor editor = new MeasureArrayEditor(property); MeasureArrayEditor editor = new MeasureArrayEditor(property);
return editor; return editor;
} }
if (ImageIcon.class.isAssignableFrom(property.getType())) { if (byte[].class.isAssignableFrom(property.getType())) {
ImageEditor editor = new ImageEditor(property); ImageEditor editor = new ImageEditor(property);
return editor; return editor;
} }
@@ -23,10 +23,13 @@ package org.diylc.swing.gui.editor;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.swing.ImageIcon;
import javax.swing.JButton; import javax.swing.JButton;
import org.apache.poi.util.IOUtils;
import org.diylc.common.PropertyWrapper; import org.diylc.common.PropertyWrapper;
import org.diylc.swing.gui.DialogFactory; import org.diylc.swing.gui.DialogFactory;
import org.diylc.swing.plugins.file.FileFilterEnum; import org.diylc.swing.plugins.file.FileFilterEnum;
@@ -48,8 +51,21 @@ public class ImageEditor extends JButton {
FileFilterEnum.IMAGES.getExtensions()[0], null); FileFilterEnum.IMAGES.getExtensions()[0], null);
if (file != null) { if (file != null) {
property.setChanged(true); property.setChanged(true);
ImageIcon image = new ImageIcon(file.getAbsolutePath()); FileInputStream fis;
property.setValue(image); try {
fis = new FileInputStream(file);
byte[] byteArray = IOUtils.toByteArray(fis);
// ImageIcon image = new ImageIcon(file.getAbsolutePath());
property.setValue(byteArray);
fis.close();
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} }
} }
}); });
+4
View File
@@ -2478,6 +2478,10 @@
<changeType>NEW_FEATURE</changeType> <changeType>NEW_FEATURE</changeType>
<description>Added ability to mark common lugs on all "Lever Switch" types with a different color</description> <description>Added ability to mark common lugs on all "Lever Switch" types with a different color</description>
</org.diylc.appframework.update.Change> </org.diylc.appframework.update.Change>
<org.diylc.appframework.update.Change>
<changeType>IMPROVEMENT</changeType>
<description>Improve performance of "Image" component and greatly reduce file size when images are present</description>
</org.diylc.appframework.update.Change>
<org.diylc.appframework.update.Change> <org.diylc.appframework.update.Change>
<changeType>IMPROVEMENT</changeType> <changeType>IMPROVEMENT</changeType>
<description>Allow a file to be loaded if it references a missing component property, but show a warning.</description> <description>Allow a file to be loaded if it references a missing component property, but show a warning.</description>