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,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).
Copyright (c) 2009-2018 held jointly by the individual authors.
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 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
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.
import org.apache.poi.util.IOUtils;
import org.diylc.appframework.miscutils.IconImageConverter;
import org.diylc.common.ObjectCache;
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,
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.
import sun.awt.image.ToolkitImage;
You should have received a copy of the GNU General Public License
along with DIYLC. If not, see <http://www.gnu.org/licenses/>.
import com.thoughtworks.xstream.annotations.XStreamConverter;
*/
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;
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;
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;
import com.thoughtworks.xstream.annotations.XStreamConverter;
@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> {
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);
}
static {
String name = "image.png";
java.net.URL imgURL = Image.class.getResource(name);
if (imgURL != null) {
ICON = new ImageIcon(imgURL, name);
}
}
@Override
public String getControlPointNodeName(int index) {
return null;
}
private Point point = new Point(0, 0);
@XStreamConverter(IconImageConverter.class)
private ImageIcon image = ICON;
private byte scale = DEFAULT_SCALE;
@Override
public void draw(Graphics2D g2d, ComponentState componentState, boolean outlineMode, Project project,
IDrawingObserver drawingObserver) {
double s = 1d * scale / DEFAULT_SCALE;
Shape clip = g2d.getClip().getBounds();
if (!clip.intersects(new Rectangle2D.Double(point.getX(), point.getY(), image.getIconWidth() * s, image
.getIconHeight() * s))) {
return;
}
Composite oldComposite = g2d.getComposite();
if (alpha < MAX_ALPHA) {
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f * alpha / MAX_ALPHA));
}
g2d.scale(s, s);
g2d.drawImage(image.getImage(), (int) (point.x / s), (int) (point.y / s), null);
if (componentState == ComponentState.SELECTED) {
g2d.setComposite(oldComposite);
g2d.scale(1 / s, 1 / s);
g2d.setColor(SELECTION_COLOR);
g2d.drawRect(point.x, point.y, (int) (image.getIconWidth() * s), (int) (image.getIconHeight() * s));
}
}
@Override
public void drawIcon(Graphics2D g2d, int width, int height) {
g2d.drawImage(ICON.getImage(), point.x, point.y, null);
}
@Override
public int getControlPointCount() {
return 1;
}
@Override
public Point getControlPoint(int index) {
return point;
}
@Override
public boolean isControlPointSticky(int index) {
return false;
}
@Override
public VisibilityPolicy getControlPointVisibilityPolicy(int index) {
return VisibilityPolicy.NEVER;
}
@Override
public void setControlPoint(Point point, int index) {
this.point.setLocation(point);
}
@EditableProperty(defaultable = false)
public ImageIcon getImage() {
return image;
}
public void setImage(ImageIcon image) {
this.image = image;
}
@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) {}
}
}
private Point point = new Point(0, 0);
@XStreamConverter(IconImageConverter.class)
@Deprecated
private ImageIcon image;
private byte[] data;
private byte scale = DEFAULT_SCALE;
public Image() {
try {
data = IOUtils.toByteArray(Image.class.getResourceAsStream("image.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void draw(Graphics2D g2d, ComponentState componentState, boolean outlineMode, Project project,
IDrawingObserver drawingObserver) {
double s = 1d * scale / DEFAULT_SCALE;
Shape clip = g2d.getClip().getBounds();
if (!clip.intersects(new Rectangle2D.Double(point.getX(), point.getY(), getImage().getIconWidth() * s, getImage()
.getIconHeight() * s))) {
return;
}
Composite oldComposite = g2d.getComposite();
if (alpha < MAX_ALPHA) {
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1f * alpha / MAX_ALPHA));
}
g2d.scale(s, s);
g2d.drawImage(getImage().getImage(), (int) (point.x / s), (int) (point.y / s), null);
if (componentState == ComponentState.SELECTED) {
g2d.setComposite(oldComposite);
g2d.scale(1 / s, 1 / s);
g2d.setColor(SELECTION_COLOR);
g2d.setStroke(ObjectCache.getInstance().fetchBasicStroke(1f));
g2d.drawRect(point.x, point.y, (int) (getImage().getIconWidth() * s), (int) (getImage().getIconHeight() * s));
}
}
@Override
public void drawIcon(Graphics2D g2d, int width, int height) {
g2d.drawImage(ICON.getImage(), point.x, point.y, null);
}
@Override
public int getControlPointCount() {
return 1;
}
@Override
public Point getControlPoint(int index) {
return point;
}
@Override
public boolean isControlPointSticky(int index) {
return false;
}
@Override
public VisibilityPolicy getControlPointVisibilityPolicy(int index) {
return VisibilityPolicy.NEVER;
}
@Override
public void setControlPoint(Point point, int index) {
this.point.setLocation(point);
}
public ImageIcon getImage() {
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);
}
@EditableProperty(name = "Image")
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) {}
}