Skip to content

Commit

Permalink
Many changes related to RenderCanvas and the area viewer
Browse files Browse the repository at this point in the history
- RenderCanvas: Using VolatileImage for storing images to reduce memory
consumption and improve overall performance
- Area Viewer: Using a JTree component for sidebar controls
- Area Viewer: Internal changes in layer-specific classes
  • Loading branch information
Argent77 committed Mar 9, 2014
1 parent 2be19f5 commit 85116bb
Show file tree
Hide file tree
Showing 28 changed files with 547 additions and 266 deletions.
55 changes: 22 additions & 33 deletions src/infinity/gui/RenderCanvas.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,12 @@

package infinity.gui;

import infinity.resource.graphics.ColorConvert;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Point;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.awt.RenderingHints;

import javax.swing.JComponent;
import javax.swing.SwingConstants;
Expand All @@ -26,12 +21,12 @@
public class RenderCanvas extends JComponent implements SwingConstants
{
// interpolation types used in scaling
public static final int TYPE_NEAREST_NEIGHBOR = AffineTransformOp.TYPE_NEAREST_NEIGHBOR;
public static final int TYPE_BILINEAR = AffineTransformOp.TYPE_BILINEAR;
public static final int TYPE_BICUBIC = AffineTransformOp.TYPE_BICUBIC;
public static final Object TYPE_NEAREST_NEIGHBOR = RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR;
public static final Object TYPE_BILINEAR = RenderingHints.VALUE_INTERPOLATION_BILINEAR;
public static final Object TYPE_BICUBIC = RenderingHints.VALUE_INTERPOLATION_BICUBIC;

private Image currentImage;
private int interpolationType;
private Object interpolationType;
private boolean isScaling;
private int verticalAlignment, horizontalAlignment;

Expand All @@ -50,12 +45,12 @@ public RenderCanvas(Image image, boolean scaled)
this(image, scaled, TYPE_NEAREST_NEIGHBOR, CENTER, CENTER);
}

public RenderCanvas(Image image, boolean scaled, int interpolationType)
public RenderCanvas(Image image, boolean scaled, Object interpolationType)
{
this(image, scaled, interpolationType, CENTER, CENTER);
}

public RenderCanvas(Image image, boolean scaled, int interpolationType,
public RenderCanvas(Image image, boolean scaled, Object interpolationType,
int horizontalAlign, int verticalAlign)
{
setOpaque(false);
Expand Down Expand Up @@ -86,6 +81,9 @@ public Image getImage()
public void setImage(Image image)
{
if (currentImage != image) {
if (image == null) {
currentImage.flush();
}
currentImage = image;
updateSize();
}
Expand Down Expand Up @@ -180,7 +178,7 @@ public void setScalingEnabled(boolean enable)
* Returns the interpolation type used when scaling has been enabled.
* @return The interpolation type.
*/
public int getInterpolationType()
public Object getInterpolationType()
{
return interpolationType;
}
Expand All @@ -190,20 +188,16 @@ public int getInterpolationType()
* One of TYPE_NEAREST_NEIGHBOR, TYPE_BILINEAR and TYPE_BICUBIC (Default: TYPE_NEAREST_NEIGHBOR).
* @param interpolationType The new interpolation type to set.
*/
public void setInterpolationType(int interpolationType)
public void setInterpolationType(Object interpolationType)
{
if (this.interpolationType != interpolationType) {
switch (interpolationType) {
case TYPE_NEAREST_NEIGHBOR:
case TYPE_BILINEAR:
case TYPE_BICUBIC:
this.interpolationType = interpolationType;
break;
default:
return;
}
if (isScaling) {
repaint();
if (interpolationType == TYPE_NEAREST_NEIGHBOR ||
interpolationType == TYPE_BILINEAR ||
interpolationType == TYPE_BICUBIC) {
this.interpolationType = interpolationType;
if (isScaling) {
repaint();
}
}
}
}
Expand All @@ -227,18 +221,13 @@ protected void updateSize()
* Renders the image to the canvas.
* @param g The graphics context in which to paint.
*/
protected synchronized void paintCanvas(Graphics g)
protected void paintCanvas(Graphics g)
{
if (currentImage != null && currentImage.getWidth(null) > 0 && currentImage.getHeight(null) > 0) {
Graphics2D g2 = (Graphics2D)g;
if (isScaling) {
BufferedImage image = ColorConvert.toBufferedImage(currentImage, true);
double sx = (double)getWidth() / (double)currentImage.getWidth(null);
double sy = (double)getHeight() / (double)currentImage.getHeight(null);
BufferedImageOp op = new AffineTransformOp(AffineTransform.getScaleInstance(sx, sy),
interpolationType);
g2.drawImage(image, op, 0, 0);
image = null;
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, interpolationType);
g2.drawImage(currentImage, 0, 0, getWidth(), getHeight(), null);
} else {
Point srcPos = new Point(0, 0);
switch (horizontalAlignment) {
Expand Down
24 changes: 10 additions & 14 deletions src/infinity/gui/layeritem/AnimatedLayerItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class AnimatedLayerItem extends AbstractLayerItem implements LayerItemLis
private boolean isBlended, isMirrored, isLooping, isAuto, forcedInterpolation, isSelfIlluminated;
private Timer timer;
private int curFrame;
private int interpolationType;
private Object interpolationType;
private double zoomFactor;
private int lighting;
private Rectangle canvasBounds; // Rectangle.[x,y] points to frame center [0,0]
Expand Down Expand Up @@ -226,7 +226,7 @@ public void setZoomFactor(double zoomFactor)
/**
* Returns the currently used interpolation type.
*/
public int getInterpolationType()
public Object getInterpolationType()
{
return interpolationType;
}
Expand All @@ -235,20 +235,16 @@ public int getInterpolationType()
* Specifies the interpolation type used for scaled items.
* @param type One of the TYPE_xxx constants.
*/
public void setInterpolationType(int type)
public void setInterpolationType(Object type)
{
if (this.interpolationType != type) {
switch (type) {
case ViewerConstants.TYPE_NEAREST_NEIGHBOR:
case ViewerConstants.TYPE_BILINEAR:
case ViewerConstants.TYPE_BICUBIC:
this.interpolationType = type;
break;
default:
return;
}
if (forcedInterpolation) {
rcCanvas.setInterpolationType(interpolationType);
if (type == ViewerConstants.TYPE_NEAREST_NEIGHBOR ||
type == ViewerConstants.TYPE_BILINEAR ||
type == ViewerConstants.TYPE_BICUBIC) {
this.interpolationType = type;
if (forcedInterpolation) {
rcCanvas.setInterpolationType(interpolationType);
}
}
}
}
Expand Down
Loading

0 comments on commit 85116bb

Please sign in to comment.