Whitespace

git-svn-id: svn+ssh://src.earth.threerings.net/nenya/trunk@887 ed5b42cb-e716-0410-a449-f6a68f950b19
This commit is contained in:
Dave Hoover
2010-02-18 06:42:56 +00:00
parent 1bd06bf2cb
commit dc58efd6a9
2 changed files with 84 additions and 123 deletions
@@ -53,22 +53,17 @@ public class ImageUtil
{
public static interface ImageCreator
{
/** Used by routines that need to create new images to allow the
* caller to dictate the format (which may mean using
* createCompatibleImage). */
public BufferedImage createImage (
int width, int height, int transparency);
/** Used by routines that need to create new images to allow the caller to dictate the
* format (which may mean using createCompatibleImage). */
public BufferedImage createImage (int width, int height, int transparency);
}
/**
* Creates a new buffered image with the same sample model and color
* model as the source image but with the new width and height.
*/
public static BufferedImage createCompatibleImage (
BufferedImage source, int width, int height)
* Creates a new buffered image with the same sample model and color model as the source image
* but with the new width and height. */
public static BufferedImage createCompatibleImage (BufferedImage source, int width, int height)
{
WritableRaster raster =
source.getRaster().createCompatibleWritableRaster(width, height);
WritableRaster raster = source.getRaster().createCompatibleWritableRaster(width, height);
return new BufferedImage(source.getColorModel(), raster, false, null);
}
@@ -77,8 +72,7 @@ public class ImageUtil
*/
public static BufferedImage createErrorImage (int width, int height)
{
BufferedImage img = new BufferedImage(
width, height, BufferedImage.TYPE_BYTE_INDEXED);
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_INDEXED);
Graphics2D g = (Graphics2D)img.getGraphics();
g.setColor(Color.red);
Label l = new Label("Error");
@@ -95,10 +89,9 @@ public class ImageUtil
}
/**
* Used to recolor images by shifting bands of color (in HSV color
* space) to a new hue. The source images must be 8-bit color mapped
* images, as the recoloring process works by analysing the color map
* and modifying it.
* Used to recolor images by shifting bands of color (in HSV color space) to a new hue. The
* source images must be 8-bit color mapped images, as the recoloring process works by
* analysing the color map and modifying it.
*/
public static BufferedImage recolorImage (
BufferedImage image, Color rootColor, float[] dists, float[] offsets)
@@ -108,13 +101,11 @@ public class ImageUtil
}
/**
* Recolors the supplied image as in {@link
* #recolorImage(BufferedImage,Color,float[],float[])} obtaining the
* recoloring parameters from the supplied {@link Colorization}
* instance.
* Recolors the supplied image as in
* {@link #recolorImage(BufferedImage,Color,float[],float[])} obtaining the recoloring
* parameters from the supplied {@link Colorization} instance.
*/
public static BufferedImage recolorImage (
BufferedImage image, Colorization cz)
public static BufferedImage recolorImage (BufferedImage image, Colorization cz)
{
return recolorImage(image, new Colorization[] { cz });
}
@@ -177,9 +168,8 @@ public class ImageUtil
}
/**
* Paints multiple copies of the supplied image using the supplied
* graphics context such that the requested area is filled with the
* image.
* Paints multiple copies of the supplied image using the supplied graphics context such that
* the requested area is filled with the image.
*/
public static void tileImage (
Graphics2D gfx, Mirage image, int x, int y, int width, int height)
@@ -224,35 +214,29 @@ public class ImageUtil
}
/**
* Paints multiple copies of the supplied image using the supplied
* graphics context such that the requested width is filled with the
* image.
* Paints multiple copies of the supplied image using the supplied graphics context such that
* the requested width is filled with the image.
*/
public static void tileImageAcross (Graphics2D gfx, Mirage image,
int x, int y, int width)
public static void tileImageAcross (Graphics2D gfx, Mirage image, int x, int y, int width)
{
tileImage(gfx, image, x, y, width, image.getHeight());
}
/**
* Paints multiple copies of the supplied image using the supplied
* graphics context such that the requested height is filled with the
* image.
* Paints multiple copies of the supplied image using the supplied graphics context such that
* the requested height is filled with the image.
*/
public static void tileImageDown (Graphics2D gfx, Mirage image,
int x, int y, int height)
public static void tileImageDown (Graphics2D gfx, Mirage image, int x, int y, int height)
{
tileImage(gfx, image, x, y, image.getWidth(), height);
}
// Not fully added because we're not using it anywhere, plus
// it's probably a little sketchy to create Area objects with all
// this pixely data.
// Also, the Area was getting zeroed out when it was translated. Something
// to look into someday if anyone wants to use this method.
// Not fully added because we're not using it anywhere, plus it's probably a little sketchy
// to create Area objects with all this pixely data.
// Also, the Area was getting zeroed out when it was translated. Something to look into someday
// if anyone wants to use this method.
// /**
// * Creates a mask that is opaque in the non-transparent areas of the
// * source image.
// * Creates a mask that is opaque in the non-transparent areas of the source image.
// */
// public static Area createImageMask (BufferedImage src)
// {
@@ -275,8 +259,8 @@ public class ImageUtil
// }
/**
* Creates and returns a new image consisting of the supplied image
* traced with the given color and thickness.
* Creates and returns a new image consisting of the supplied image traced with the given
* color and thickness.
*/
public static BufferedImage createTracedImage (
ImageCreator isrc, BufferedImage src, Color tcolor, int thickness)
@@ -285,8 +269,8 @@ public class ImageUtil
}
/**
* Creates and returns a new image consisting of the supplied image
* traced with the given color, thickness and alpha transparency.
* Creates and returns a new image consisting of the supplied image traced with the given
* color, thickness and alpha transparency.
*/
public static BufferedImage createTracedImage (
ImageCreator isrc, BufferedImage src, Color tcolor, int thickness,
@@ -294,15 +278,13 @@ public class ImageUtil
{
// create the destination image
int wid = src.getWidth(), hei = src.getHeight();
BufferedImage dest = isrc.createImage(
wid, hei, Transparency.TRANSLUCENT);
return createTracedImage(
src, dest, tcolor, thickness, startAlpha, endAlpha);
BufferedImage dest = isrc.createImage(wid, hei, Transparency.TRANSLUCENT);
return createTracedImage(src, dest, tcolor, thickness, startAlpha, endAlpha);
}
/**
* Creates and returns a new image consisting of the supplied image
* traced with the given color, thickness and alpha transparency.
* Creates and returns a new image consisting of the supplied image traced with the given
* color, thickness and alpha transparency.
*/
public static BufferedImage createTracedImage (
BufferedImage src, BufferedImage dest, Color tcolor, int thickness,
@@ -317,14 +299,12 @@ public class ImageUtil
int stepAlpha = (thickness <= 1) ? 0 :
(int)(((startAlpha - endAlpha) * 255) / (thickness - 1));
// TODO: this could be made more efficient, e.g., if we made four
// passes through the image in a vertical scan, horizontal scan,
// and opposing diagonal scans, making sure each non-transparent
// pixel found during each scan is traced on both sides of the
// respective scan direction. for now, we just naively check all
// eight pixels surrounding each pixel in the image and fill the
// center pixel with the tracing color if it's transparent but has
// a non-transparent pixel around it.
// TODO: this could be made more efficient, e.g., if we made four passes through the image
// in a vertical scan, horizontal scan, and opposing diagonal scans, making sure each
// non-transparent pixel found during each scan is traced on both sides of the respective
// scan direction. For now, we just naively check all eight pixels surrounding each pixel
// in the image and fill the center pixel with the tracing color if it's transparent but
// has a non-transparent pixel around it.
for (int tt = 0; tt < thickness; tt++) {
if (tt > 0) {
// clear out the array of pixels traced this go-around
@@ -345,8 +325,7 @@ public class ImageUtil
// copy any pixel that isn't transparent
dest.setRGB(xx, yy, argb);
} else if (bordersNonTransparentPixel(
src, wid, hei, traced, xx, yy)) {
} else if (bordersNonTransparentPixel(src, wid, hei, traced, xx, yy)) {
dest.setRGB(xx, yy, tpixel);
// note that we traced this pixel this pass so
// that it doesn't impact other-pixel borderedness
@@ -360,8 +339,7 @@ public class ImageUtil
}
/**
* Returns whether the given pixel is bordered by any non-transparent
* pixel.
* Returns whether the given pixel is bordered by any non-transparent pixel.
*/
protected static boolean bordersNonTransparentPixel (
BufferedImage data, int wid, int hei, boolean[] traced, int x, int y)
@@ -410,8 +388,7 @@ public class ImageUtil
}
/**
* Create an image using the alpha channel from the first and the RGB
* values from the second.
* Create an image using the alpha channel from the first and the RGB values from the second.
*/
public static BufferedImage composeMaskedImage (
ImageCreator isrc, BufferedImage mask, BufferedImage base)
@@ -424,8 +401,7 @@ public class ImageUtil
// create a new image using the rasters if possible
if (maskdata.getNumBands() == 4 && basedata.getNumBands() >= 3) {
WritableRaster target =
basedata.createCompatibleWritableRaster(wid, hei);
WritableRaster target = basedata.createCompatibleWritableRaster(wid, hei);
// copy the alpha from the mask image
int[] adata = maskdata.getSamples(0, 0, wid, hei, 3, (int[]) null);
@@ -433,8 +409,7 @@ public class ImageUtil
// copy the RGB from the base image
for (int ii=0; ii < 3; ii++) {
int[] cdata = basedata.getSamples(
0, 0, wid, hei, ii, (int[]) null);
int[] cdata = basedata.getSamples(0, 0, wid, hei, ii, (int[]) null);
target.setSamples(0, 0, wid, hei, ii, cdata);
}
@@ -457,10 +432,9 @@ public class ImageUtil
}
/**
* Create a new image using the supplied shape as a mask from which to
* cut out pixels from the supplied image. Pixels inside the shape
* will be added to the final image, pixels outside the shape will be
* clear.
* Create a new image using the supplied shape as a mask from which to cut out pixels from the
* supplied image. Pixels inside the shape will be added to the final image, pixels outside
* the shape will be clear.
*/
public static BufferedImage composeMaskedImage (
ImageCreator isrc, Shape mask, BufferedImage base)
@@ -470,14 +444,11 @@ public class ImageUtil
// alternate method for composition:
// 1. create WriteableRaster with base data
// 2. test each pixel with mask.contains() and set the alpha
// channel to fully-alpha if false
// 2. test each pixel with mask.contains() and set the alpha channel to fully-alpha if false
// 3. create buffered image from raster
// (I didn't use this method because it depends on the colormodel
// of the source image, and was booching when the souce image was
// a cut-up from a tileset, and it seems like it would take
// longer than the method we are using.
// But it's something to consider)
// (I didn't use this method because it depends on the colormodel of the source image, and
// was booching when the souce image was a cut-up from a tileset, and it seems like it
// would take longer than the method we are using. But it's something to consider)
// composite them by rendering them with an alpha rule
BufferedImage target = isrc.createImage(wid, hei, Transparency.TRANSLUCENT);
@@ -494,8 +465,8 @@ public class ImageUtil
}
/**
* Returns true if the supplied image contains a non-transparent pixel
* at the specified coordinates, false otherwise.
* Returns true if the supplied image contains a non-transparent pixel at the specified
* coordinates, false otherwise.
*/
public static boolean hitTest (BufferedImage image, int x, int y)
{
@@ -505,15 +476,14 @@ public class ImageUtil
}
/**
* Computes the bounds of the smallest rectangle that contains all
* non-transparent pixels of this image. This isn't extremely
* efficient, so you shouldn't be doing this anywhere exciting.
* Computes the bounds of the smallest rectangle that contains all non-transparent pixels of
* this image. This isn't extremely efficient, so you shouldn't be doing this anywhere
* exciting.
*/
public static void computeTrimmedBounds (
BufferedImage image, Rectangle tbounds)
public static void computeTrimmedBounds (BufferedImage image, Rectangle tbounds)
{
// this could be more efficient, but it's run as a batch process
// and doesn't really take that long anyway
// this could be more efficient, but it's run as a batch process and doesn't really take
// that long anyway
int width = image.getWidth(), height = image.getHeight();
int firstrow = -1, lastrow = -1, minx = width, maxx = 0;
@@ -527,9 +497,8 @@ public class ImageUtil
continue;
}
// otherwise, if we've not seen a non-transparent pixel,
// make a note that this is the first non-transparent
// pixel in the row
// otherwise, if we've not seen a non-transparent pixel, make a note that this is
// the first non-transparent pixel in the row
if (firstidx == -1) {
firstidx = xx;
}
@@ -546,8 +515,8 @@ public class ImageUtil
minx = Math.min(firstidx, minx);
maxx = Math.max(lastidx, maxx);
// otherwise keep track of the first row on which we see
// pixels and the last row on which we see pixels
// otherwise keep track of the first row on which we see pixels and the last row on
// which we see pixels
if (firstrow == -1) {
firstrow = yy;
}
@@ -570,8 +539,7 @@ public class ImageUtil
}
/**
* Returns the estimated memory usage in bytes for the specified
* image.
* Returns the estimated memory usage in bytes for the specified image.
*/
public static long getEstimatedMemoryUsage (BufferedImage image)
{
@@ -583,23 +551,21 @@ public class ImageUtil
}
/**
* Returns the estimated memory usage in bytes for the specified
* raster.
* Returns the estimated memory usage in bytes for the specified raster.
*/
public static long getEstimatedMemoryUsage (Raster raster)
{
// we assume that the data buffer stores each element in a
// byte-rounded memory element; maybe the buffer is smarter about
// things than this, but we're better to err on the safe side
// we assume that the data buffer stores each element in a byte-rounded memory element;
// maybe the buffer is smarter about things than this, but we're better to err on the safe
// side
DataBuffer db = raster.getDataBuffer();
int bpe = (int)Math.ceil(
DataBuffer.getDataTypeSize(db.getDataType()) / 8f);
int bpe = (int)Math.ceil(DataBuffer.getDataTypeSize(db.getDataType()) / 8f);
return bpe * db.getSize();
}
/**
* Returns the estimated memory usage in bytes for all buffered images
* in the supplied iterator.
* Returns the estimated memory usage in bytes for all buffered images in the supplied
* iterator.
*/
public static long getEstimatedMemoryUsage (Iterator<BufferedImage> iter)
{
@@ -612,16 +578,15 @@ public class ImageUtil
}
/**
* Obtains the default graphics configuration for this VM. If the JVM
* is in headless mode, this method will return null.
* Obtains the default graphics configuration for this VM. If the JVM is in headless mode,
* this method will return null.
*/
protected static GraphicsConfiguration getDefGC ()
{
if (_gc == null) {
// obtain information on our graphics environment
try {
GraphicsEnvironment env =
GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = env.getDefaultScreenDevice();
_gc = gd.getDefaultConfiguration();
} catch (HeadlessException e) {
@@ -25,15 +25,13 @@ import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
/**
* Provides an interface via which images can be accessed in a way that
* allows them to optionally be located in video memory where that affords
* performance improvements.
* Provides an interface via which images can be accessed in a way that allows them to optionally
* be located in video memory where that affords performance improvements.
*/
public interface Mirage
{
/**
* Renders this mirage at the specified position in the supplied
* graphics context.
* Renders this mirage at the specified position in the supplied graphics context.
*/
public void paint (Graphics2D gfx, int x, int y);
@@ -48,20 +46,18 @@ public interface Mirage
public int getHeight ();
/**
* Returns true if this mirage contains a non-transparent pixel at the
* specified coordinate.
* Returns true if this mirage contains a non-transparent pixel at the specified coordinate.
*/
public boolean hitTest (int x, int y);
/**
* Returns a snapshot of this mirage as a buffered image. The snapshot
* should <em>not</em> be modified by the caller.
* Returns a snapshot of this mirage as a buffered image. The snapshot should <em>not</em> be
* modified by the caller.
*/
public BufferedImage getSnapshot ();
/**
* Returns an estimate of the memory consumed by this mirage's image
* raster data.
* Returns an estimate of the memory consumed by this mirage's image raster data.
*/
public long getEstimatedMemoryUsage ();
}