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,160 +1,195 @@
/* /*
*
* DIY Layout Creator (DIYLC). Copyright (c) 2009-2018 held jointly by the individual authors.
*
* This file is part of DIYLC.
*
* DIYLC is free software: you can redistribute it and/or modify it under the terms of the GNU
* General Public License as published by 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
* Public License for more details.
*
* 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;
DIY Layout Creator (DIYLC). import java.awt.AlphaComposite;
Copyright (c) 2009-2018 held jointly by the individual authors. import java.awt.Composite;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Shape;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
This file is part of DIYLC. import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
DIYLC is free software: you can redistribute it and/or modify import org.apache.poi.util.IOUtils;
it under the terms of the GNU General Public License as published by import org.diylc.appframework.miscutils.IconImageConverter;
the Free Software Foundation, either version 3 of the License, or import org.diylc.common.ObjectCache;
(at your option) any later version. import org.diylc.components.AbstractTransparentComponent;
import org.diylc.core.ComponentState;
import org.diylc.core.IDIYComponent;
import org.diylc.core.IDrawingObserver;
import org.diylc.core.Project;
import org.diylc.core.VisibilityPolicy;
import org.diylc.core.annotations.BomPolicy;
import org.diylc.core.annotations.ComponentDescriptor;
import org.diylc.core.annotations.EditableProperty;
import org.diylc.core.annotations.PercentEditor;
DIYLC is distributed in the hope that it will be useful, import sun.awt.image.ToolkitImage;
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License import com.thoughtworks.xstream.annotations.XStreamConverter;
along with DIYLC. If not, see <http://www.gnu.org/licenses/>.
*/
package org.diylc.components.misc;
import java.awt.AlphaComposite;
import java.awt.Composite;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Shape;
import java.awt.geom.Rectangle2D;
import javax.swing.ImageIcon; @ComponentDescriptor(name = "Image", author = "Branislav Stojkovic", category = "Misc",
description = "User defined image", instanceNamePrefix = "Img", zOrder = IDIYComponent.COMPONENT,
flexibleZOrder = true, stretchable = false, bomPolicy = BomPolicy.NEVER_SHOW)
public class Image extends AbstractTransparentComponent<Void> {
import org.diylc.appframework.miscutils.IconImageConverter; private static final long serialVersionUID = 1L;
import org.diylc.components.AbstractTransparentComponent; public static String DEFAULT_TEXT = "Double click to edit text";
import org.diylc.core.ComponentState; private static ImageIcon ICON;
import org.diylc.core.IDIYComponent; private static byte DEFAULT_SCALE = 50;
import org.diylc.core.IDrawingObserver;
import org.diylc.core.Project;
import org.diylc.core.VisibilityPolicy;
import org.diylc.core.annotations.BomPolicy;
import org.diylc.core.annotations.ComponentDescriptor;
import org.diylc.core.annotations.EditableProperty;
import org.diylc.core.annotations.PercentEditor;
import com.thoughtworks.xstream.annotations.XStreamConverter; static {
String name = "image.png";
@ComponentDescriptor(name = "Image", author = "Branislav Stojkovic", category = "Misc", java.net.URL imgURL = Image.class.getResource(name);
description = "User defined image", instanceNamePrefix = "Img", zOrder = IDIYComponent.COMPONENT, if (imgURL != null) {
flexibleZOrder = true, stretchable = false, bomPolicy = BomPolicy.NEVER_SHOW) ICON = new ImageIcon(imgURL, name);
public class Image extends AbstractTransparentComponent<Void> { }
private static final long serialVersionUID = 1L;
public static String DEFAULT_TEXT = "Double click to edit text";
private static ImageIcon ICON;
private static byte DEFAULT_SCALE = 50;
static {
String name = "image.png";
java.net.URL imgURL = Image.class.getResource(name);
if (imgURL != null) {
ICON = new ImageIcon(imgURL, name);
}
} }
@Override @Override
public String getControlPointNodeName(int index) { public String getControlPointNodeName(int index) {
return null; return null;
} }
private Point point = new Point(0, 0); private Point point = new Point(0, 0);
@XStreamConverter(IconImageConverter.class)
private ImageIcon image = ICON; @XStreamConverter(IconImageConverter.class)
private byte scale = DEFAULT_SCALE; @Deprecated
private ImageIcon image;
@Override private byte[] data;
public void draw(Graphics2D g2d, ComponentState componentState, boolean outlineMode, Project project, private byte scale = DEFAULT_SCALE;
IDrawingObserver drawingObserver) {
double s = 1d * scale / DEFAULT_SCALE; public Image() {
Shape clip = g2d.getClip().getBounds(); try {
if (!clip.intersects(new Rectangle2D.Double(point.getX(), point.getY(), image.getIconWidth() * s, image data = IOUtils.toByteArray(Image.class.getResourceAsStream("image.png"));
.getIconHeight() * s))) { } catch (IOException e) {
return; e.printStackTrace();
} }
Composite oldComposite = g2d.getComposite(); }
if (alpha < MAX_ALPHA) {
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f * alpha / MAX_ALPHA)); @Override
} public void draw(Graphics2D g2d, ComponentState componentState, boolean outlineMode, Project project,
IDrawingObserver drawingObserver) {
g2d.scale(s, s); double s = 1d * scale / DEFAULT_SCALE;
g2d.drawImage(image.getImage(), (int) (point.x / s), (int) (point.y / s), null); Shape clip = g2d.getClip().getBounds();
if (componentState == ComponentState.SELECTED) { if (!clip.intersects(new Rectangle2D.Double(point.getX(), point.getY(), getImage().getIconWidth() * s, getImage()
g2d.setComposite(oldComposite); .getIconHeight() * s))) {
g2d.scale(1 / s, 1 / s); return;
g2d.setColor(SELECTION_COLOR); }
g2d.drawRect(point.x, point.y, (int) (image.getIconWidth() * s), (int) (image.getIconHeight() * s)); Composite oldComposite = g2d.getComposite();
} if (alpha < MAX_ALPHA) {
} g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f * alpha / MAX_ALPHA));
}
@Override
public void drawIcon(Graphics2D g2d, int width, int height) { g2d.scale(s, s);
g2d.drawImage(ICON.getImage(), point.x, point.y, null); g2d.drawImage(getImage().getImage(), (int) (point.x / s), (int) (point.y / s), null);
} if (componentState == ComponentState.SELECTED) {
g2d.setComposite(oldComposite);
@Override g2d.scale(1 / s, 1 / s);
public int getControlPointCount() { g2d.setColor(SELECTION_COLOR);
return 1; g2d.setStroke(ObjectCache.getInstance().fetchBasicStroke(1f));
} g2d.drawRect(point.x, point.y, (int) (getImage().getIconWidth() * s), (int) (getImage().getIconHeight() * s));
}
@Override }
public Point getControlPoint(int index) {
return point; @Override
} public void drawIcon(Graphics2D g2d, int width, int height) {
g2d.drawImage(ICON.getImage(), point.x, point.y, null);
@Override }
public boolean isControlPointSticky(int index) {
return false; @Override
} public int getControlPointCount() {
return 1;
@Override }
public VisibilityPolicy getControlPointVisibilityPolicy(int index) {
return VisibilityPolicy.NEVER; @Override
} public Point getControlPoint(int index) {
return point;
@Override }
public void setControlPoint(Point point, int index) {
this.point.setLocation(point); @Override
} public boolean isControlPointSticky(int index) {
return false;
@EditableProperty(defaultable = false) }
public ImageIcon getImage() {
return image; @Override
} public VisibilityPolicy getControlPointVisibilityPolicy(int index) {
return VisibilityPolicy.NEVER;
public void setImage(ImageIcon image) { }
this.image = image;
} @Override
public void setControlPoint(Point point, int index) {
@PercentEditor(_100PercentValue = 50) this.point.setLocation(point);
@EditableProperty(defaultable = false) }
public byte getScale() {
return scale; public ImageIcon getImage() {
} if (image != null) {
BufferedImage bi = ((ToolkitImage) image.getImage()).getBufferedImage();
public void setScale(byte scale) { ByteArrayOutputStream baos = new ByteArrayOutputStream();
this.scale = scale; try {
} ImageIO.write(bi, "png", baos);
} catch (IOException e) {
@Override // TODO Auto-generated catch block
public String getName() { e.printStackTrace();
return super.getName(); }
} data = baos.toByteArray();
image = null;
@Override }
public Void getValue() {
return null; return new ImageIcon(data);
} }
@Override @EditableProperty(name = "Image")
public void setValue(Void value) {} public byte[] getData() {
} return data;
}
public void setData(byte[] data) {
this.data = data;
this.image = null;
}
@PercentEditor(_100PercentValue = 50)
@EditableProperty(defaultable = false)
public byte getScale() {
return scale;
}
public void setScale(byte scale) {
this.scale = scale;
}
@Override
public String getName() {
return super.getName();
}
@Override
public Void getValue() {
return null;
}
@Override
public void setValue(Void value) {}
}
Binary file not shown.
Binary file not shown.
@@ -21,18 +21,17 @@
*/ */
package org.diylc.swing.gui.editor; package org.diylc.swing.gui.editor;
import java.awt.Color; 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; import org.diylc.common.PropertyWrapper;
import org.diylc.common.PropertyWrapper; import org.diylc.core.annotations.DynamicList;
import org.diylc.core.annotations.DynamicList; import org.diylc.core.annotations.MultiLineText;
import org.diylc.core.annotations.MultiLineText; import org.diylc.core.measures.AbstractMeasure;
import org.diylc.core.measures.AbstractMeasure;
/** /**
* Based on {@link PropertyWrapper#getType()}, creates an appropriate {@link Component} that can * Based on {@link PropertyWrapper#getType()}, creates an appropriate {@link Component} that can
@@ -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;
} }
@@ -21,15 +21,18 @@
*/ */
package org.diylc.swing.gui.editor; 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 javax.swing.ImageIcon; import java.io.FileNotFoundException;
import javax.swing.JButton; import java.io.IOException;
import org.diylc.common.PropertyWrapper; import javax.swing.JButton;
import org.diylc.swing.gui.DialogFactory;
import org.diylc.swing.plugins.file.FileFilterEnum; import org.apache.poi.util.IOUtils;
import org.diylc.common.PropertyWrapper;
import org.diylc.swing.gui.DialogFactory;
import org.diylc.swing.plugins.file.FileFilterEnum;
public class ImageEditor extends JButton { public class ImageEditor extends JButton {
@@ -47,9 +50,22 @@ public class ImageEditor extends JButton {
DialogFactory.getInstance().showOpenDialog(FileFilterEnum.IMAGES.getFilter(), null, DialogFactory.getInstance().showOpenDialog(FileFilterEnum.IMAGES.getFilter(), null,
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();
}
} }
} }
}); });
+5 -1
View File
@@ -2477,7 +2477,11 @@
<org.diylc.appframework.update.Change> <org.diylc.appframework.update.Change>
<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>