mirror of
https://github.com/bancika/diy-layout-creator
synced 2026-11-10 17:22:09 +10:00
optimize image saving/loading
This commit is contained in:
Binary file not shown.
+3
-1
@@ -64,7 +64,7 @@ public class ProjectFileManager {
|
|||||||
V1_COLOR_MAP.put("yellow", Color.yellow);
|
V1_COLOR_MAP.put("yellow", Color.yellow);
|
||||||
}
|
}
|
||||||
|
|
||||||
private XStream xStream = new XStream(new DomDriver());
|
private XStream xStream;
|
||||||
|
|
||||||
private String currentFileName = null;
|
private String currentFileName = null;
|
||||||
private boolean modified = false;
|
private boolean modified = false;
|
||||||
@@ -73,6 +73,8 @@ public class ProjectFileManager {
|
|||||||
|
|
||||||
public ProjectFileManager(MessageDispatcher<EventType> messageDispatcher) {
|
public ProjectFileManager(MessageDispatcher<EventType> messageDispatcher) {
|
||||||
super();
|
super();
|
||||||
|
this.xStream = new XStream(new DomDriver());
|
||||||
|
xStream.autodetectAnnotations(true);
|
||||||
this.messageDispatcher = messageDispatcher;
|
this.messageDispatcher = messageDispatcher;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,5 +3,7 @@
|
|||||||
<classpathentry kind="src" path="src"/>
|
<classpathentry kind="src" path="src"/>
|
||||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
|
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
|
||||||
<classpathentry combineaccessrules="false" kind="src" path="/DIYLC"/>
|
<classpathentry combineaccessrules="false" kind="src" path="/DIYLC"/>
|
||||||
|
<classpathentry kind="lib" path="lib/xstream-1.3.1.jar"/>
|
||||||
|
<classpathentry kind="lib" path="lib/log4j-1.2.15.jar"/>
|
||||||
<classpathentry kind="output" path="bin"/>
|
<classpathentry kind="output" path="bin"/>
|
||||||
</classpath>
|
</classpath>
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
+93
@@ -0,0 +1,93 @@
|
|||||||
|
package org.diylc.components.misc;
|
||||||
|
|
||||||
|
import java.awt.Image;
|
||||||
|
import java.awt.Toolkit;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.awt.image.ColorModel;
|
||||||
|
import java.awt.image.ImageObserver;
|
||||||
|
import java.awt.image.MemoryImageSource;
|
||||||
|
import java.awt.image.PixelGrabber;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import javax.imageio.ImageIO;
|
||||||
|
import javax.swing.ImageIcon;
|
||||||
|
|
||||||
|
import org.apache.log4j.Logger;
|
||||||
|
|
||||||
|
import com.thoughtworks.xstream.converters.Converter;
|
||||||
|
import com.thoughtworks.xstream.converters.MarshallingContext;
|
||||||
|
import com.thoughtworks.xstream.converters.UnmarshallingContext;
|
||||||
|
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
|
||||||
|
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
|
||||||
|
|
||||||
|
public class IconImageConverter implements Converter {
|
||||||
|
|
||||||
|
private static final Logger LOG = Logger.getLogger(IconImageConverter.class);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void marshal(Object object, HierarchicalStreamWriter writer, MarshallingContext context) {
|
||||||
|
ImageIcon image = (ImageIcon) object;
|
||||||
|
int width = image.getIconWidth();
|
||||||
|
int height = image.getIconHeight();
|
||||||
|
int[] pixels = new int[width * height];
|
||||||
|
|
||||||
|
if (image != null) {
|
||||||
|
try {
|
||||||
|
PixelGrabber pg = new PixelGrabber(image.getImage(), 0, 0, width, height, pixels,
|
||||||
|
0, width);
|
||||||
|
pg.grabPixels();
|
||||||
|
if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
|
||||||
|
LOG.error("Failed to load image contents");
|
||||||
|
}
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
LOG.error("Image load interrupted");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
LOG.debug("Writing image to file: " + width + "x" + height);
|
||||||
|
writer.addAttribute("width", Integer.toString(width));
|
||||||
|
writer.addAttribute("height", Integer.toString(height));
|
||||||
|
StringBuilder dataBuilder = new StringBuilder();
|
||||||
|
for (int pixel : pixels) {
|
||||||
|
dataBuilder.append(Integer.toString(pixel, 16));
|
||||||
|
dataBuilder.append(",");
|
||||||
|
}
|
||||||
|
writer.addAttribute("data", dataBuilder.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
|
||||||
|
int width = Integer.parseInt(reader.getAttribute("width"));
|
||||||
|
int height = Integer.parseInt(reader.getAttribute("height"));
|
||||||
|
LOG.debug("Reading image from file: " + width + "x" + height);
|
||||||
|
int[] pixels = new int[width * height];
|
||||||
|
String data = reader.getAttribute("data");
|
||||||
|
String[] splitData = data.split(",");
|
||||||
|
for (int i = 0; i < splitData.length; i++) {
|
||||||
|
if (!splitData[i].isEmpty()) {
|
||||||
|
pixels[i] = Integer.parseInt(splitData[i], 16);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Toolkit toolkit = Toolkit.getDefaultToolkit();
|
||||||
|
ColorModel colorModel = ColorModel.getRGBdefault();
|
||||||
|
Image image = toolkit.createImage(new MemoryImageSource(width, height, colorModel, pixels,
|
||||||
|
0, width));
|
||||||
|
|
||||||
|
BufferedImage bimg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
||||||
|
bimg.setRGB(0, 0, width, height, pixels, 0, width);
|
||||||
|
File outputfile = new File("c:\\saved.png");
|
||||||
|
try {
|
||||||
|
ImageIO.write(bimg, "png", outputfile);
|
||||||
|
} catch (IOException e) {
|
||||||
|
// TODO Auto-generated catch block
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return new ImageIcon(image);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean canConvert(Class clazz) {
|
||||||
|
return ImageIcon.class.isAssignableFrom(clazz);
|
||||||
|
}
|
||||||
|
}
|
||||||
+4
-1
@@ -16,6 +16,8 @@ import org.diylc.core.VisibilityPolicy;
|
|||||||
import org.diylc.core.annotations.ComponentDescriptor;
|
import org.diylc.core.annotations.ComponentDescriptor;
|
||||||
import org.diylc.core.annotations.EditableProperty;
|
import org.diylc.core.annotations.EditableProperty;
|
||||||
|
|
||||||
|
import com.thoughtworks.xstream.annotations.XStreamConverter;
|
||||||
|
|
||||||
@ComponentDescriptor(name = "Image", author = "Branislav Stojkovic", category = "Misc", description = "User defined image", instanceNamePrefix = "Img", zOrder = IDIYComponent.TEXT, stretchable = false)
|
@ComponentDescriptor(name = "Image", author = "Branislav Stojkovic", category = "Misc", description = "User defined image", instanceNamePrefix = "Img", zOrder = IDIYComponent.TEXT, stretchable = false)
|
||||||
public class Image extends AbstractTransparentComponent<Void> {
|
public class Image extends AbstractTransparentComponent<Void> {
|
||||||
|
|
||||||
@@ -33,6 +35,7 @@ public class Image extends AbstractTransparentComponent<Void> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private Point point = new Point(0, 0);
|
private Point point = new Point(0, 0);
|
||||||
|
@XStreamConverter(IconImageConverter.class)
|
||||||
private ImageIcon image = ICON;
|
private ImageIcon image = ICON;
|
||||||
private byte scale = 50;
|
private byte scale = 50;
|
||||||
|
|
||||||
@@ -47,9 +50,9 @@ public class Image extends AbstractTransparentComponent<Void> {
|
|||||||
double s = 1d * scale / DEFAULT_SCALE;
|
double s = 1d * scale / DEFAULT_SCALE;
|
||||||
g2d.scale(s, s);
|
g2d.scale(s, s);
|
||||||
g2d.drawImage(image.getImage(), (int) (point.x / s), (int) (point.y / s), null);
|
g2d.drawImage(image.getImage(), (int) (point.x / s), (int) (point.y / s), null);
|
||||||
|
if (componentState == ComponentState.SELECTED) {
|
||||||
g2d.setComposite(oldComposite);
|
g2d.setComposite(oldComposite);
|
||||||
g2d.scale(1 / s, 1 / s);
|
g2d.scale(1 / s, 1 / s);
|
||||||
if (componentState == ComponentState.SELECTED) {
|
|
||||||
g2d.setColor(SELECTION_COLOR);
|
g2d.setColor(SELECTION_COLOR);
|
||||||
g2d.drawRect(point.x, point.y, (int) (image.getIconWidth() * s), (int) (image
|
g2d.drawRect(point.x, point.y, (int) (image.getIconWidth() * s), (int) (image
|
||||||
.getIconHeight() * s));
|
.getIconHeight() * s));
|
||||||
|
|||||||
Reference in New Issue
Block a user