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