Files
narya/src/java/com/threerings/media/image/ImageUtil.java
T
Michael Bayne 4641d00583 Moved ImageUtil into media.util with the rest of the util classes.
git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@1067 542714f4-19e9-0310-aa3c-eee0fc999fb1
2002-02-24 02:20:44 +00:00

97 lines
3.3 KiB
Java

//
// $Id: ImageUtil.java,v 1.4 2002/02/24 02:20:44 mdb Exp $
package com.threerings.media.util;
import java.awt.Image;
import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
/**
* Image related utility functions.
*/
public class ImageUtil
{
/**
* Extracts a subimage from the supplied image with the specified
* dimensions. If the supplied image is an instance of {@link
* BufferedImage}, then the subimage will simply reference the main
* image. If it is not, the subimage will be created and the data will
* be rendered into the newly created image.
*
* @param source the source image.
* @param x the left coordinate of the sub-image.
* @param y the top coordinate of the sub-image.
* @param width the sub-image width.
* @param height the sub-image height.
*
* @return the desired subimage.
*/
public static Image getSubimage (
Image source, int x, int y, int width, int height)
{
if (source instanceof BufferedImage) {
return ((BufferedImage)source).getSubimage(x, y, width, height);
} else {
BufferedImage target = createImage(width, height);
Graphics g = target.getGraphics();
g.drawImage(source, 0, 0, width, height,
x, y, x+width, y+height, null);
g.dispose();
return target;
}
}
/**
* Creates a new blank image with the given dimensions and
* transparency set to {@link Transparency#BITMASK}. The format of
* the created image is compatible with the graphics configuration of
* the default screen device, such that no format conversion will be
* necessary when rendering the image to that device.
*
* @param width the desired image width.
* @param height the desired image height.
*
* @return the blank image.
*/
public static BufferedImage createImage (int width, int height)
{
return createImage(width, height, Transparency.BITMASK);
}
/**
* Creates a new blank image with the given dimensions and
* transparency. The format of the created image is compatible with
* the graphics configuration of the default screen device, such that
* no format conversion will be necessary when rendering the image to
* that device.
*
* @param width the desired image width.
* @param height the desired image height.
* @param transparency the desired image transparency; one of the
* constants in {@link java.awt.Transparency}.
*
* @return the blank image.
*/
public static BufferedImage createImage (
int width, int height, int transparency)
{
return _gc.createCompatibleImage(width, height, transparency);
}
/** The graphics configuration for the default screen device. */
protected static GraphicsConfiguration _gc;
static {
// obtain information on our graphics environment
GraphicsEnvironment env =
GraphicsEnvironment.getLocalGraphicsEnvironment();
GraphicsDevice gd = env.getDefaultScreenDevice();
_gc = gd.getDefaultConfiguration();
};
}