Class ImageUtil
- java.lang.Object
-
- com.twelvemonkeys.image.ImageUtil
-
public final class ImageUtil extends java.lang.Object
This class contains methods for basic image manipulation and conversion.- Version:
- $Id: common/common-image/src/main/java/com/twelvemonkeys/image/ImageUtil.java#3 $
-
-
Field Summary
Fields Modifier and Type Field Description private static int
BI_TYPE_ANY
Passed to the createXxx methods, to indicate that the type does not matterstatic int
COLOR_SELECTION_DEFAULT
Default color selectionstatic int
COLOR_SELECTION_FAST
Prioritize speedstatic int
COLOR_SELECTION_QUALITY
Prioritize qualityprivate static java.awt.GraphicsConfiguration
DEFAULT_CONFIGURATION
static int
DITHER_DEFAULT
Java default ditherstatic int
DITHER_DIFFUSION
Error diffusion ditherstatic int
DITHER_DIFFUSION_ALTSCANS
Error diffusion dither with alternating scansstatic int
DITHER_NONE
No ditherstatic int
EDGE_NO_OP
Alias forConvolveOp.EDGE_NO_OP
.static int
EDGE_REFLECT
Adds a border to the image while convolving.static int
EDGE_WRAP
Adds a border to the image while convolving.static int
EDGE_ZERO_FILL
Alias forConvolveOp.EDGE_ZERO_FILL
.static int
FLIP_HORIZONTAL
static int
FLIP_VERTICAL
protected static java.awt.geom.AffineTransform
IDENTITY_TRANSFORM
protected static java.awt.Point
LOCATION_UPPER_LEFT
private static java.awt.Component
NULL_COMPONENT
Component that can be used with the MediaTracker etc.static int
ROTATE_180
static int
ROTATE_90_CCW
static int
ROTATE_90_CW
private static java.awt.image.Kernel
SHARPEN_KERNEL
The sharpen kernel.private static float[]
SHARPEN_MATRIX
The sharpen matrixprivate static java.awt.MediaTracker
sTracker
Our static image trackerstatic int
TRANSPARENCY_BITMASK
Convert alpha to bitmaskstatic int
TRANSPARENCY_DEFAULT
Default transparency (none)static int
TRANSPARENCY_OPAQUE
Discard any alpha informationprotected static int
TRANSPARENCY_TRANSLUCENT
Keep original alpha (not supported yet)private static boolean
VM_SUPPORTS_ACCELERATION
Tells wether this WM may support acceleration of some images
-
Constructor Summary
Constructors Modifier Constructor Description private
ImageUtil()
Creates an ImageUtil.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.awt.image.BufferedImage
accelerate(java.awt.Image pImage)
Tries to use H/W-accelerated code for an image for display purposes.(package private) static java.awt.image.BufferedImage
accelerate(java.awt.Image pImage, java.awt.Color pBackgroundColor, java.awt.GraphicsConfiguration pConfiguration)
Tries to use H/W-accelerated code for an image for display purposes.static java.awt.image.BufferedImage
accelerate(java.awt.Image pImage, java.awt.GraphicsConfiguration pConfiguration)
Tries to use H/W-accelerated code for an image for display purposes.private static java.awt.image.BufferedImage
addBorder(java.awt.image.BufferedImage pOriginal, int pBorderX, int pBorderY, int pEdgeOperation)
(package private) static int
blend(int pRGB1, int pRGB2)
Blends two ARGB values half and half, to create a tone in between.static java.awt.Color
blend(java.awt.Color pColor, java.awt.Color pOther)
Blends two colors half and half, to create a tone in between.static java.awt.Color
blend(java.awt.Color pColor, java.awt.Color pOther, float pBlendFactor)
Blends two colors, controlled by the blending factor.static java.awt.image.BufferedImage
blur(java.awt.image.BufferedImage pOriginal)
Creates a blurred version of the given image.static java.awt.image.BufferedImage
blur(java.awt.image.BufferedImage pOriginal, float pRadius)
Creates a blurred version of the given image.static java.awt.Image
brightness(java.awt.Image pOriginal, float pAmount)
Changes the brightness of the original image.private static int
clamp(float f)
static java.awt.Image
contrast(java.awt.Image pOriginal)
Adds contraststatic java.awt.Image
contrast(java.awt.Image pOriginal, float pAmount)
Changes the contrast of the imageprivate static int
convertAWTHints(int pHints)
static java.awt.image.BufferedImage
convolve(java.awt.image.BufferedImage pOriginal, java.awt.image.Kernel pKernel, int pEdgeOperation)
Convolves an image, using a convolution matrix.private static java.awt.image.BufferedImage
createBuffered(int pWidth, int pHeight, int pType, int pTransparency)
Creates aBufferedImage
of the given size and type.(package private) static java.awt.image.BufferedImage
createBuffered(int pWidth, int pHeight, int pType, int pTransparency, java.awt.GraphicsConfiguration pConfiguration)
(package private) static java.awt.image.BufferedImage
createClear(int pWidth, int pHeight, int pType, int pTransparency, java.awt.Color pBackground, java.awt.GraphicsConfiguration pConfiguration)
static java.awt.image.BufferedImage
createClear(int pWidth, int pHeight, int pType, java.awt.Color pBackground)
Creates a clear image with the given background color.static java.awt.image.BufferedImage
createClear(int pWidth, int pHeight, java.awt.Color pBackground)
Creates a clear image with the given background color.(package private) static java.awt.image.WritableRaster
createCompatibleWritableRaster(java.awt.image.BufferedImage pOriginal, java.awt.image.ColorModel pModel, int width, int height)
Workaround for bug: TYPE_3BYTE_BGR, TYPE_4BYTE_ABGR and TYPE_4BYTE_ABGR_PRE are all converted to TYPE_CUSTOM when using the default createCompatibleWritableRaster from ComponentColorModel.static java.awt.image.BufferedImage
createCopy(java.awt.image.BufferedImage pImage)
Creates a deep copy of the given image.static java.awt.image.BufferedImage
createFlipped(java.awt.Image pImage, int pAxis)
Creates a flipped version of the given image.static java.awt.image.BufferedImage
createIndexed(java.awt.Image pImage)
Creates an indexed version of the given image (aBufferedImage
with anIndexColorModel
.static java.awt.image.BufferedImage
createIndexed(java.awt.Image pImage, int pColors, java.awt.Color pMatte, int pHints)
Creates an indexed version of the given image (aBufferedImage
with anIndexColorModel
.static java.awt.image.BufferedImage
createIndexed(java.awt.Image pImage, java.awt.image.IndexColorModel pColors, java.awt.Color pMatte, int pHints)
Creates an indexed version of the given image (aBufferedImage
with anIndexColorModel
.static java.awt.image.BufferedImage
createIndexed(java.awt.Image pImage, java.awt.Image pColors, java.awt.Color pMatte, int pHints)
Creates an indexed version of the given image (aBufferedImage
with anIndexColorModel
.(package private) static java.awt.image.WritableRaster
createRaster(int pWidth, int pHeight, java.lang.Object pPixels, java.awt.image.ColorModel pColorModel)
Creates aWritableRaster
for the givenColorModel
and pixel data.static java.awt.image.BufferedImage
createResampled(java.awt.image.BufferedImage pImage, int pWidth, int pHeight, int pHints)
Creates a scaled instance of the givenBufferedImage
.static java.awt.image.BufferedImage
createResampled(java.awt.image.RenderedImage pImage, int pWidth, int pHeight, int pHints)
Creates a scaled instance of the givenRenderedImage
, and converts it to aBufferedImage
if needed.static java.awt.image.BufferedImage
createResampled(java.awt.Image pImage, int pWidth, int pHeight, int pHints)
Creates a scaled instance of the givenImage
, and converts it to aBufferedImage
if needed.static java.awt.image.BufferedImage
createRotated(java.awt.Image pImage, double pAngle)
Rotates the image to the given angle.static java.awt.image.BufferedImage
createRotated(java.awt.Image pImage, int pDirection)
Rotates the image 90 degrees, clockwise (aka "rotate right"), counter-clockwise (aka "rotate left") or 180 degrees, depending on thepDirection
argument.private static java.awt.image.BufferedImage
createRotated0(java.awt.image.BufferedImage pSource, double pAngle)
static java.awt.image.BufferedImage
createScaled(java.awt.Image pImage, int pWidth, int pHeight, int pHints)
Creates a scaled instance of the givenImage
, and converts it to aBufferedImage
if needed.static java.awt.Color
createTranslucent(java.awt.Color pColor, int pTransparency)
Creates a translucent version of the given color.static java.awt.image.BufferedImage
createTransparent(int pWidth, int pHeight)
Creates a transparent image.static java.awt.image.BufferedImage
createTransparent(int pWidth, int pHeight, int pType)
Creates a transparent image.(package private) static void
drawOnto(java.awt.image.BufferedImage pDestination, java.awt.Image pSource)
Draws the source image onto the buffered image, usingAlphaComposite.Src
and coordinates0, 0
.private static boolean
equals(java.awt.image.ColorModel pLeft, java.awt.image.ColorModel pRight)
static java.awt.Image
filter(java.awt.Image pOriginal, java.awt.image.ImageFilter pFilter)
Filters an image, using the givenImageFilter
.private static java.awt.GraphicsConfiguration
getConfiguration(java.awt.GraphicsConfiguration pConfiguration)
private static java.awt.GraphicsConfiguration
getDefaultGraphicsConfiguration()
static int
getHeight(java.awt.Image pImage)
Gets the height of an Image.private static int
getImageType(int pType, int pTransparency)
static java.awt.image.IndexColorModel
getIndexColorModel(java.awt.Image pImage, int pColors, int pHints)
Extracts anIndexColorModel
from the given image.private static int
getTransparency(java.awt.Image pImage)
static int
getWidth(java.awt.Image pImage)
Gets the width of an Image.static java.awt.Image
grayscale(java.awt.Image pOriginal)
Converts an image to grayscale.static boolean
hasTransparentPixels(java.awt.image.RenderedImage pImage, boolean pFast)
Tests whether the image has any transparent or semi-transparent pixels.private static boolean
isIndexedPacked(java.awt.image.IndexColorModel pColorModel)
private static java.awt.image.Kernel
makeKernel(float radius)
Make a Gaussian blurKernel
.static java.awt.image.BufferedImage
sharpen(java.awt.image.BufferedImage pOriginal)
Sharpens an image using a convolution matrix.static java.awt.image.BufferedImage
sharpen(java.awt.image.BufferedImage pOriginal, float pAmount)
Sharpens an image using a convolution matrix.private static boolean
supportsAcceleration(java.awt.GraphicsEnvironment pEnv)
Tests if the givenGraphicsEnvironment
supports accellerationstatic java.awt.image.BufferedImage
toBuffered(java.awt.Image pOriginal)
Converts theImage
to aBufferedImage
.static java.awt.image.BufferedImage
toBuffered(java.awt.image.BufferedImage pOriginal, int pType)
Converts theBufferedImage
to aBufferedImage
of the given type.static java.awt.image.BufferedImage
toBuffered(java.awt.image.RenderedImage pOriginal)
Converts theRenderedImage
to aBufferedImage
.static java.awt.image.BufferedImage
toBuffered(java.awt.image.RenderedImage pOriginal, int pType)
Converts theRenderedImage
to aBufferedImage
of the given type.static java.awt.image.BufferedImage
toBuffered(java.awt.Image pOriginal, int pType)
Converts theImage
to aBufferedImage
of the given type.private static java.awt.image.BufferedImage
toBuffered(java.awt.Image pOriginal, int pType, java.awt.image.IndexColorModel pICM)
static boolean
waitForImage(java.awt.Image pImage)
Waits for an image to load completely.static boolean
waitForImage(java.awt.Image pImage, long pTimeOut)
Waits for an image to load completely.static boolean
waitForImages(java.awt.Image[] pImages)
Waits for a number of images to load completely.static boolean
waitForImages(java.awt.Image[] pImages, long pTimeOut)
Waits for a number of images to load completely.
-
-
-
Field Detail
-
ROTATE_90_CCW
public static final int ROTATE_90_CCW
- See Also:
- Constant Field Values
-
ROTATE_90_CW
public static final int ROTATE_90_CW
- See Also:
- Constant Field Values
-
ROTATE_180
public static final int ROTATE_180
- See Also:
- Constant Field Values
-
FLIP_VERTICAL
public static final int FLIP_VERTICAL
- See Also:
- Constant Field Values
-
FLIP_HORIZONTAL
public static final int FLIP_HORIZONTAL
- See Also:
- Constant Field Values
-
EDGE_ZERO_FILL
public static final int EDGE_ZERO_FILL
Alias forConvolveOp.EDGE_ZERO_FILL
.
-
EDGE_NO_OP
public static final int EDGE_NO_OP
Alias forConvolveOp.EDGE_NO_OP
.
-
EDGE_REFLECT
public static final int EDGE_REFLECT
Adds a border to the image while convolving. The border will reflect the edges of the original image. This is usually a good default. Note that while this mode typically provides better quality than the standard modesEDGE_ZERO_FILL
andEDGE_NO_OP
, it does so at the expense of higher memory consumption and considerable more computation.
-
EDGE_WRAP
public static final int EDGE_WRAP
Adds a border to the image while convolving. The border will wrap the edges of the original image. This is usually the best choice for tiles. Note that while this mode typically provides better quality than the standard modesEDGE_ZERO_FILL
andEDGE_NO_OP
, it does so at the expense of higher memory consumption and considerable more computation.
-
DITHER_DEFAULT
public static final int DITHER_DEFAULT
Java default dither- See Also:
- Constant Field Values
-
DITHER_NONE
public static final int DITHER_NONE
No dither- See Also:
- Constant Field Values
-
DITHER_DIFFUSION
public static final int DITHER_DIFFUSION
Error diffusion dither- See Also:
- Constant Field Values
-
DITHER_DIFFUSION_ALTSCANS
public static final int DITHER_DIFFUSION_ALTSCANS
Error diffusion dither with alternating scans- See Also:
- Constant Field Values
-
COLOR_SELECTION_DEFAULT
public static final int COLOR_SELECTION_DEFAULT
Default color selection- See Also:
- Constant Field Values
-
COLOR_SELECTION_FAST
public static final int COLOR_SELECTION_FAST
Prioritize speed- See Also:
- Constant Field Values
-
COLOR_SELECTION_QUALITY
public static final int COLOR_SELECTION_QUALITY
Prioritize quality- See Also:
- Constant Field Values
-
TRANSPARENCY_DEFAULT
public static final int TRANSPARENCY_DEFAULT
Default transparency (none)- See Also:
- Constant Field Values
-
TRANSPARENCY_OPAQUE
public static final int TRANSPARENCY_OPAQUE
Discard any alpha information- See Also:
- Constant Field Values
-
TRANSPARENCY_BITMASK
public static final int TRANSPARENCY_BITMASK
Convert alpha to bitmask- See Also:
- Constant Field Values
-
TRANSPARENCY_TRANSLUCENT
protected static final int TRANSPARENCY_TRANSLUCENT
Keep original alpha (not supported yet)- See Also:
- Constant Field Values
-
BI_TYPE_ANY
private static final int BI_TYPE_ANY
Passed to the createXxx methods, to indicate that the type does not matter- See Also:
- Constant Field Values
-
VM_SUPPORTS_ACCELERATION
private static boolean VM_SUPPORTS_ACCELERATION
Tells wether this WM may support acceleration of some images
-
SHARPEN_MATRIX
private static final float[] SHARPEN_MATRIX
The sharpen matrix
-
SHARPEN_KERNEL
private static final java.awt.image.Kernel SHARPEN_KERNEL
The sharpen kernel. Uses the following 3 by 3 matrix:Sharpen Kernel Matrix 0.0 -0.3 0.0 -0.3 2.2 -0.3 0.0 -0.3 0.0
-
NULL_COMPONENT
private static final java.awt.Component NULL_COMPONENT
Component that can be used with the MediaTracker etc.
-
sTracker
private static java.awt.MediaTracker sTracker
Our static image tracker
-
IDENTITY_TRANSFORM
protected static final java.awt.geom.AffineTransform IDENTITY_TRANSFORM
-
LOCATION_UPPER_LEFT
protected static final java.awt.Point LOCATION_UPPER_LEFT
-
DEFAULT_CONFIGURATION
private static final java.awt.GraphicsConfiguration DEFAULT_CONFIGURATION
-
-
Method Detail
-
getDefaultGraphicsConfiguration
private static java.awt.GraphicsConfiguration getDefaultGraphicsConfiguration()
-
toBuffered
public static java.awt.image.BufferedImage toBuffered(java.awt.image.RenderedImage pOriginal)
Converts theRenderedImage
to aBufferedImage
. The new image will have the sameColorModel
,Raster
and properties as the original image, if possible.If the image is already a
BufferedImage
, it is simply returned and no conversion takes place.- Parameters:
pOriginal
- the image to convert.- Returns:
- a
BufferedImage
-
toBuffered
public static java.awt.image.BufferedImage toBuffered(java.awt.image.RenderedImage pOriginal, int pType)
Converts theRenderedImage
to aBufferedImage
of the given type.If the image is already a
BufferedImage
of the given type, it is simply returned and no conversion takes place.- Parameters:
pOriginal
- the image to convert.pType
- the type of buffered image- Returns:
- a
BufferedImage
- Throws:
java.lang.IllegalArgumentException
- ifpOriginal == null
orpType
is not a valid type forBufferedImage
- See Also:
BufferedImage.getType()
-
toBuffered
public static java.awt.image.BufferedImage toBuffered(java.awt.image.BufferedImage pOriginal, int pType)
Converts theBufferedImage
to aBufferedImage
of the given type. The new image will have the sameColorModel
,Raster
and properties as the original image, if possible.If the image is already a
BufferedImage
of the given type, it is simply returned and no conversion takes place.This method simply invokes
toBuffered((RenderedImage) pOriginal, pType)
.- Parameters:
pOriginal
- the image to convert.pType
- the type of buffered image- Returns:
- a
BufferedImage
- Throws:
java.lang.IllegalArgumentException
- ifpOriginal == null
or ifpType
is not a valid type forBufferedImage
- See Also:
BufferedImage.getType()
-
toBuffered
public static java.awt.image.BufferedImage toBuffered(java.awt.Image pOriginal)
Converts theImage
to aBufferedImage
. The new image will have the sameColorModel
,Raster
and properties as the original image, if possible.If the image is already a
BufferedImage
, it is simply returned and no conversion takes place.- Parameters:
pOriginal
- the image to convert.- Returns:
- a
BufferedImage
- Throws:
java.lang.IllegalArgumentException
- ifpOriginal == null
ImageConversionException
- if the image cannot be converted
-
createCopy
public static java.awt.image.BufferedImage createCopy(java.awt.image.BufferedImage pImage)
Creates a deep copy of the given image. The image will have the same color model and raster type, but will not share image (pixel) data with the input image.- Parameters:
pImage
- the image to clone.- Returns:
- a new
BufferedImage
- Throws:
java.lang.IllegalArgumentException
- ifpImage
isnull
-
createRaster
static java.awt.image.WritableRaster createRaster(int pWidth, int pHeight, java.lang.Object pPixels, java.awt.image.ColorModel pColorModel)
Creates aWritableRaster
for the givenColorModel
and pixel data.This method is optimized for the most common cases of
ColorModel
and pixel data combinations. The raster's backingDataBuffer
is created directly from the pixel data, as this is faster and more resource-friendly than usingColorModel.createCompatibleWritableRaster(w, h)
.For uncommon combinations, the method will fallback to using
ColorModel.createCompatibleWritableRaster(w, h)
andWritableRaster.setDataElements(w, h, pixels)
Note that the
ColorModel
and pixel data are not cloned (in most cases).- Parameters:
pWidth
- the requested raster widthpHeight
- the requested raster heightpPixels
- the pixels, as an array, of a type supported by the differentDataBuffer
pColorModel
- the color model to use- Returns:
- a new
WritableRaster
- Throws:
java.lang.NullPointerException
- if eitherpColorModel
orpPixels
arenull
.java.lang.RuntimeException
- ifpWidth
andpHeight
does not match the pixel data inpPixels
.- See Also:
ColorModel.createCompatibleWritableRaster(int, int)
,ColorModel.createCompatibleSampleModel(int, int)
,WritableRaster.setDataElements(int, int, Object)
,DataBuffer
-
isIndexedPacked
private static boolean isIndexedPacked(java.awt.image.IndexColorModel pColorModel)
-
createCompatibleWritableRaster
static java.awt.image.WritableRaster createCompatibleWritableRaster(java.awt.image.BufferedImage pOriginal, java.awt.image.ColorModel pModel, int width, int height)
Workaround for bug: TYPE_3BYTE_BGR, TYPE_4BYTE_ABGR and TYPE_4BYTE_ABGR_PRE are all converted to TYPE_CUSTOM when using the default createCompatibleWritableRaster from ComponentColorModel.- Parameters:
pOriginal
- the orignal imagepModel
- the original color modelwidth
- the requested width of the rasterheight
- the requested height of the raster- Returns:
- a new WritableRaster
-
toBuffered
public static java.awt.image.BufferedImage toBuffered(java.awt.Image pOriginal, int pType)
Converts theImage
to aBufferedImage
of the given type. The new image will have the sameColorModel
,Raster
and properties as the original image, if possible.If the image is already a
BufferedImage
of the given type, it is simply returned and no conversion takes place.- Parameters:
pOriginal
- the image to convert.pType
- the type of buffered image- Returns:
- a
BufferedImage
- Throws:
java.lang.IllegalArgumentException
- ifpOriginal == null
or ifpType
is not a valid type forBufferedImage
- See Also:
BufferedImage.getType()
-
toBuffered
private static java.awt.image.BufferedImage toBuffered(java.awt.Image pOriginal, int pType, java.awt.image.IndexColorModel pICM)
- Parameters:
pOriginal
- the original imagepType
- the type ofBufferedImage
to createpICM
- the optionalIndexColorModel
to use. If notnull
thepType
must be compatible with the color model- Returns:
- a
BufferedImage
- Throws:
java.lang.IllegalArgumentException
- ifpType
is not compatible with the color model
-
drawOnto
static void drawOnto(java.awt.image.BufferedImage pDestination, java.awt.Image pSource)
Draws the source image onto the buffered image, usingAlphaComposite.Src
and coordinates0, 0
.- Parameters:
pDestination
- the image to draw onpSource
- the source image to draw- Throws:
java.lang.NullPointerException
- ifpDestination
orpSource
isnull
-
createFlipped
public static java.awt.image.BufferedImage createFlipped(java.awt.Image pImage, int pAxis)
Creates a flipped version of the given image.- Parameters:
pImage
- the image to flippAxis
- the axis to flip around- Returns:
- a new
BufferedImage
-
createRotated
public static java.awt.image.BufferedImage createRotated(java.awt.Image pImage, int pDirection)
Rotates the image 90 degrees, clockwise (aka "rotate right"), counter-clockwise (aka "rotate left") or 180 degrees, depending on thepDirection
argument.The new image will be completely covered with pixels from the source image.
- Parameters:
pImage
- the source image.pDirection
- the direction, must be eitherROTATE_90_CW
,ROTATE_90_CCW
orROTATE_180
- Returns:
- a new
BufferedImage
-
createRotated
public static java.awt.image.BufferedImage createRotated(java.awt.Image pImage, double pAngle)
Rotates the image to the given angle. Areas not covered with pixels from the source image will be left transparent, if possible.- Parameters:
pImage
- the source imagepAngle
- the angle of rotation, in radians- Returns:
- a new
BufferedImage
, unlesspAngle == 0.0
-
createRotated0
private static java.awt.image.BufferedImage createRotated0(java.awt.image.BufferedImage pSource, double pAngle)
-
createScaled
public static java.awt.image.BufferedImage createScaled(java.awt.Image pImage, int pWidth, int pHeight, int pHints)
Creates a scaled instance of the givenImage
, and converts it to aBufferedImage
if needed. If the original image is aBufferedImage
the result will have same type and color model. Note that this implies overhead, and is probably not useful for anything butIndexColorModel
images.- Parameters:
pImage
- theImage
to scalepWidth
- width in pixelspHeight
- height in pixelspHints
- scaling ints- Returns:
- a
BufferedImage
- Throws:
java.lang.NullPointerException
- ifpImage
isnull
.- See Also:
createResampled(java.awt.Image, int, int, int)
,Image.getScaledInstance(int,int,int)
,Image.SCALE_AREA_AVERAGING
,Image.SCALE_DEFAULT
,Image.SCALE_FAST
,Image.SCALE_REPLICATE
,Image.SCALE_SMOOTH
-
equals
private static boolean equals(java.awt.image.ColorModel pLeft, java.awt.image.ColorModel pRight)
-
createResampled
public static java.awt.image.BufferedImage createResampled(java.awt.Image pImage, int pWidth, int pHeight, int pHints)
Creates a scaled instance of the givenImage
, and converts it to aBufferedImage
if needed.- Parameters:
pImage
- theImage
to scalepWidth
- width in pixelspHeight
- height in pixelspHints
- scaling mHints- Returns:
- a
BufferedImage
- Throws:
java.lang.NullPointerException
- ifpImage
isnull
.- See Also:
Image.SCALE_AREA_AVERAGING
,Image.SCALE_DEFAULT
,Image.SCALE_FAST
,Image.SCALE_REPLICATE
,Image.SCALE_SMOOTH
,ResampleOp
-
createResampled
public static java.awt.image.BufferedImage createResampled(java.awt.image.RenderedImage pImage, int pWidth, int pHeight, int pHints)
Creates a scaled instance of the givenRenderedImage
, and converts it to aBufferedImage
if needed.- Parameters:
pImage
- theRenderedImage
to scalepWidth
- width in pixelspHeight
- height in pixelspHints
- scaling mHints- Returns:
- a
BufferedImage
- Throws:
java.lang.NullPointerException
- ifpImage
isnull
.- See Also:
Image.SCALE_AREA_AVERAGING
,Image.SCALE_DEFAULT
,Image.SCALE_FAST
,Image.SCALE_REPLICATE
,Image.SCALE_SMOOTH
,ResampleOp
-
createResampled
public static java.awt.image.BufferedImage createResampled(java.awt.image.BufferedImage pImage, int pWidth, int pHeight, int pHints)
Creates a scaled instance of the givenBufferedImage
.- Parameters:
pImage
- theBufferedImage
to scalepWidth
- width in pixelspHeight
- height in pixelspHints
- scaling mHints- Returns:
- a
BufferedImage
- Throws:
java.lang.NullPointerException
- ifpImage
isnull
.- See Also:
Image.SCALE_AREA_AVERAGING
,Image.SCALE_DEFAULT
,Image.SCALE_FAST
,Image.SCALE_REPLICATE
,Image.SCALE_SMOOTH
,ResampleOp
-
convertAWTHints
private static int convertAWTHints(int pHints)
-
getIndexColorModel
public static java.awt.image.IndexColorModel getIndexColorModel(java.awt.Image pImage, int pColors, int pHints)
Extracts anIndexColorModel
from the given image.- Parameters:
pImage
- the image to get the color model frompColors
- the maximum number of colors in the resulting color modelpHints
- hints controlling transparency and color selection- Returns:
- the extracted
IndexColorModel
- See Also:
COLOR_SELECTION_DEFAULT
,COLOR_SELECTION_FAST
,COLOR_SELECTION_QUALITY
,TRANSPARENCY_DEFAULT
,TRANSPARENCY_OPAQUE
,TRANSPARENCY_BITMASK
,TRANSPARENCY_TRANSLUCENT
-
createIndexed
public static java.awt.image.BufferedImage createIndexed(java.awt.Image pImage)
Creates an indexed version of the given image (aBufferedImage
with anIndexColorModel
. The resulting image will have a maximum of 256 different colors. Transparent parts of the original will be replaced with solid black. Default (possibly HW accelerated) dither will be used.- Parameters:
pImage
- the image to convert- Returns:
- an indexed version of the given image
-
createIndexed
public static java.awt.image.BufferedImage createIndexed(java.awt.Image pImage, int pColors, java.awt.Color pMatte, int pHints)
Creates an indexed version of the given image (aBufferedImage
with anIndexColorModel
.- Parameters:
pImage
- the image to convertpColors
- number of colors in the resulting imagepMatte
- color to replace transparent parts of the original.pHints
- hints controlling dither, transparency and color selection- Returns:
- an indexed version of the given image
- See Also:
COLOR_SELECTION_DEFAULT
,COLOR_SELECTION_FAST
,COLOR_SELECTION_QUALITY
,DITHER_NONE
,DITHER_DEFAULT
,DITHER_DIFFUSION
,DITHER_DIFFUSION_ALTSCANS
,TRANSPARENCY_DEFAULT
,TRANSPARENCY_OPAQUE
,TRANSPARENCY_BITMASK
,TRANSPARENCY_TRANSLUCENT
-
createIndexed
public static java.awt.image.BufferedImage createIndexed(java.awt.Image pImage, java.awt.image.IndexColorModel pColors, java.awt.Color pMatte, int pHints)
Creates an indexed version of the given image (aBufferedImage
with anIndexColorModel
.- Parameters:
pImage
- the image to convertpColors
- theIndexColorModel
to be used in the resulting image.pMatte
- color to replace transparent parts of the original.pHints
- hints controlling dither, transparency and color selection- Returns:
- an indexed version of the given image
- See Also:
COLOR_SELECTION_DEFAULT
,COLOR_SELECTION_FAST
,COLOR_SELECTION_QUALITY
,DITHER_NONE
,DITHER_DEFAULT
,DITHER_DIFFUSION
,DITHER_DIFFUSION_ALTSCANS
,TRANSPARENCY_DEFAULT
,TRANSPARENCY_OPAQUE
,TRANSPARENCY_BITMASK
,TRANSPARENCY_TRANSLUCENT
-
createIndexed
public static java.awt.image.BufferedImage createIndexed(java.awt.Image pImage, java.awt.Image pColors, java.awt.Color pMatte, int pHints)
Creates an indexed version of the given image (aBufferedImage
with anIndexColorModel
.- Parameters:
pImage
- the image to convertpColors
- anImage
used to get colors from. If the image is has anIndexColorModel
, it will be uesd, otherwise anIndexColorModel
is created from the image.pMatte
- color to replace transparent parts of the original.pHints
- hints controlling dither, transparency and color selection- Returns:
- an indexed version of the given image
- See Also:
COLOR_SELECTION_DEFAULT
,COLOR_SELECTION_FAST
,COLOR_SELECTION_QUALITY
,DITHER_NONE
,DITHER_DEFAULT
,DITHER_DIFFUSION
,DITHER_DIFFUSION_ALTSCANS
,TRANSPARENCY_DEFAULT
,TRANSPARENCY_OPAQUE
,TRANSPARENCY_BITMASK
,TRANSPARENCY_TRANSLUCENT
-
sharpen
public static java.awt.image.BufferedImage sharpen(java.awt.image.BufferedImage pOriginal)
Sharpens an image using a convolution matrix. The sharpen kernel used, is defined by the following 3 by 3 matrix:Sharpen Kernel Matrix 0.0 -0.3 0.0 -0.3 2.2 -0.3 0.0 -0.3 0.0 This is the same result returned as
sharpen(pOriginal, 0.3f)
.- Parameters:
pOriginal
- the BufferedImage to sharpen- Returns:
- a new BufferedImage, containing the sharpened image.
-
sharpen
public static java.awt.image.BufferedImage sharpen(java.awt.image.BufferedImage pOriginal, float pAmount)
Sharpens an image using a convolution matrix. The sharpen kernel used, is defined by the following 3 by 3 matrix:Sharpen Kernel Matrix 0.0 - pAmount
0.0 - pAmount
4.0 * pAmount
+ 1.0- pAmount
0.0 - pAmount
0.0 - Parameters:
pOriginal
- the BufferedImage to sharpenpAmount
- the amount of sharpening- Returns:
- a BufferedImage, containing the sharpened image.
-
blur
public static java.awt.image.BufferedImage blur(java.awt.image.BufferedImage pOriginal)
Creates a blurred version of the given image.- Parameters:
pOriginal
- the original image- Returns:
- a new
BufferedImage
with a blurred version of the given image
-
blur
public static java.awt.image.BufferedImage blur(java.awt.image.BufferedImage pOriginal, float pRadius)
Creates a blurred version of the given image.- Parameters:
pOriginal
- the original imagepRadius
- the amount to blur- Returns:
- a new
BufferedImage
with a blurred version of the given image
-
makeKernel
private static java.awt.image.Kernel makeKernel(float radius)
Make a Gaussian blurKernel
.- Parameters:
radius
- the blur radius- Returns:
- a new blur
Kernel
-
convolve
public static java.awt.image.BufferedImage convolve(java.awt.image.BufferedImage pOriginal, java.awt.image.Kernel pKernel, int pEdgeOperation)
Convolves an image, using a convolution matrix.- Parameters:
pOriginal
- the BufferedImage to sharpenpKernel
- the kernelpEdgeOperation
- the edge operation. Must be one ofEDGE_NO_OP
,EDGE_ZERO_FILL
,EDGE_REFLECT
orEDGE_WRAP
- Returns:
- a new BufferedImage, containing the sharpened image.
-
addBorder
private static java.awt.image.BufferedImage addBorder(java.awt.image.BufferedImage pOriginal, int pBorderX, int pBorderY, int pEdgeOperation)
-
contrast
public static java.awt.Image contrast(java.awt.Image pOriginal)
Adds contrast- Parameters:
pOriginal
- the BufferedImage to add contrast to- Returns:
- an
Image
, containing the contrasted image.
-
contrast
public static java.awt.Image contrast(java.awt.Image pOriginal, float pAmount)
Changes the contrast of the image- Parameters:
pOriginal
- theImage
to changepAmount
- the amount of contrast in the range [-1.0..1.0].- Returns:
- an
Image
, containing the contrasted image.
-
brightness
public static java.awt.Image brightness(java.awt.Image pOriginal, float pAmount)
Changes the brightness of the original image.- Parameters:
pOriginal
- theImage
to changepAmount
- the amount of brightness in the range [-2.0..2.0].- Returns:
- an
Image
-
grayscale
public static java.awt.Image grayscale(java.awt.Image pOriginal)
Converts an image to grayscale.- Parameters:
pOriginal
- the image to convert.- Returns:
- a new Image, containing the gray image data.
- See Also:
GrayFilter
,RGBImageFilter
-
filter
public static java.awt.Image filter(java.awt.Image pOriginal, java.awt.image.ImageFilter pFilter)
Filters an image, using the givenImageFilter
.- Parameters:
pOriginal
- the original imagepFilter
- the filter to apply- Returns:
- the new
Image
-
accelerate
public static java.awt.image.BufferedImage accelerate(java.awt.Image pImage)
Tries to use H/W-accelerated code for an image for display purposes. Note that transparent parts of the image might be replaced by solid color. Additional image information not used by the current diplay hardware may be discarded, like extra bith depth etc.- Parameters:
pImage
- anyImage
- Returns:
- a
BufferedImage
-
accelerate
public static java.awt.image.BufferedImage accelerate(java.awt.Image pImage, java.awt.GraphicsConfiguration pConfiguration)
Tries to use H/W-accelerated code for an image for display purposes. Note that transparent parts of the image might be replaced by solid color. Additional image information not used by the current diplay hardware may be discarded, like extra bith depth etc.- Parameters:
pImage
- anyImage
pConfiguration
- theGraphicsConfiguration
to accelerate for- Returns:
- a
BufferedImage
-
accelerate
static java.awt.image.BufferedImage accelerate(java.awt.Image pImage, java.awt.Color pBackgroundColor, java.awt.GraphicsConfiguration pConfiguration)
Tries to use H/W-accelerated code for an image for display purposes. Note that transparent parts of the image will be replaced by solid color. Additional image information not used by the current diplay hardware may be discarded, like extra bith depth etc.- Parameters:
pImage
- anyImage
pBackgroundColor
- the background color to replace any transparent parts of the image. May benull
, in such case the color is undefined.pConfiguration
- the graphics configuration May benull
, in such case the color is undefined.- Returns:
- a
BufferedImage
-
getTransparency
private static int getTransparency(java.awt.Image pImage)
-
createTransparent
public static java.awt.image.BufferedImage createTransparent(int pWidth, int pHeight)
Creates a transparent image.- Parameters:
pWidth
- the requested width of the imagepHeight
- the requested height of the image- Returns:
- the new image
- Throws:
java.lang.IllegalArgumentException
- ifpType
is not a valid type forBufferedImage
-
createTransparent
public static java.awt.image.BufferedImage createTransparent(int pWidth, int pHeight, int pType)
Creates a transparent image.- Parameters:
pWidth
- the requested width of the imagepHeight
- the requested height of the imagepType
- the type ofBufferedImage
to create- Returns:
- the new image
- Throws:
java.lang.IllegalArgumentException
- ifpType
is not a valid type forBufferedImage
- See Also:
BufferedImage(int,int,int)
-
createClear
public static java.awt.image.BufferedImage createClear(int pWidth, int pHeight, java.awt.Color pBackground)
Creates a clear image with the given background color.- Parameters:
pWidth
- the requested width of the imagepHeight
- the requested height of the imagepBackground
- the background color. The color may be translucent. May benull
, in such case the color is undefined.- Returns:
- the new image
- Throws:
java.lang.IllegalArgumentException
- ifpType
is not a valid type forBufferedImage
- See Also:
BufferedImage(int,int,int)
-
createClear
public static java.awt.image.BufferedImage createClear(int pWidth, int pHeight, int pType, java.awt.Color pBackground)
Creates a clear image with the given background color.- Parameters:
pWidth
- the width of the image to createpHeight
- the height of the image to createpType
- the type of image to create (one of the constants fromBufferedImage
orBI_TYPE_ANY
)pBackground
- the background color. The color may be translucent. May benull
, in such case the color is undefined.- Returns:
- the new image
- Throws:
java.lang.IllegalArgumentException
- ifpType
is not a valid type forBufferedImage
- See Also:
BufferedImage(int,int,int)
-
createClear
static java.awt.image.BufferedImage createClear(int pWidth, int pHeight, int pType, int pTransparency, java.awt.Color pBackground, java.awt.GraphicsConfiguration pConfiguration)
-
createBuffered
private static java.awt.image.BufferedImage createBuffered(int pWidth, int pHeight, int pType, int pTransparency)
Creates aBufferedImage
of the given size and type. If possible, uses accelerated versions of BufferedImage from GraphicsConfiguration.- Parameters:
pWidth
- the width of the image to createpHeight
- the height of the image to createpType
- the type of image to create (one of the constants fromBufferedImage
orBI_TYPE_ANY
)pTransparency
- the transparency type (fromTransparency
)- Returns:
- a
BufferedImage
-
createBuffered
static java.awt.image.BufferedImage createBuffered(int pWidth, int pHeight, int pType, int pTransparency, java.awt.GraphicsConfiguration pConfiguration)
-
getConfiguration
private static java.awt.GraphicsConfiguration getConfiguration(java.awt.GraphicsConfiguration pConfiguration)
-
getImageType
private static int getImageType(int pType, int pTransparency)
-
supportsAcceleration
private static boolean supportsAcceleration(java.awt.GraphicsEnvironment pEnv)
Tests if the givenGraphicsEnvironment
supports accelleration- Parameters:
pEnv
- the environment- Returns:
true
if theGraphicsEnvironment
supports acceleration
-
getWidth
public static int getWidth(java.awt.Image pImage)
Gets the width of an Image. This method has the side-effect of completely loading the image.- Parameters:
pImage
- an image.- Returns:
- the width of the image, or -1 if the width could not be determined (i.e. an error occured while waiting for the image to load).
-
getHeight
public static int getHeight(java.awt.Image pImage)
Gets the height of an Image. This method has the side-effect of completely loading the image.- Parameters:
pImage
- an image.- Returns:
- the height of the image, or -1 if the height could not be determined (i.e. an error occured while waiting for the image to load).
-
waitForImage
public static boolean waitForImage(java.awt.Image pImage)
Waits for an image to load completely. Will wait forever.- Parameters:
pImage
- an Image object to wait for.- Returns:
- true if the image was loaded successfully, false if an error occured, or the wait was interrupted.
- See Also:
waitForImage(Image,long)
-
waitForImage
public static boolean waitForImage(java.awt.Image pImage, long pTimeOut)
Waits for an image to load completely. Will wait the specified time.- Parameters:
pImage
- an Image object to wait for.pTimeOut
- the time to wait, in milliseconds.- Returns:
- true if the image was loaded successfully, false if an error occurred, or the wait was interrupted.
- See Also:
waitForImages(Image[],long)
-
waitForImages
public static boolean waitForImages(java.awt.Image[] pImages)
Waits for a number of images to load completely. Will wait forever.- Parameters:
pImages
- an array of Image objects to wait for.- Returns:
- true if the images was loaded successfully, false if an error occurred, or the wait was interrupted.
- See Also:
waitForImages(Image[],long)
-
waitForImages
public static boolean waitForImages(java.awt.Image[] pImages, long pTimeOut)
Waits for a number of images to load completely. Will wait the specified time.- Parameters:
pImages
- an array of Image objects to wait forpTimeOut
- the time to wait, in milliseconds- Returns:
- true if the images was loaded successfully, false if an error occurred, or the wait was interrupted.
-
hasTransparentPixels
public static boolean hasTransparentPixels(java.awt.image.RenderedImage pImage, boolean pFast)
Tests whether the image has any transparent or semi-transparent pixels.- Parameters:
pImage
- the imagepFast
- iftrue
, the method tests maximum 10 x 10 pixels, evenly spaced out in the image.- Returns:
true
if transparent pixels are found, otherwisefalse
.
-
createTranslucent
public static java.awt.Color createTranslucent(java.awt.Color pColor, int pTransparency)
Creates a translucent version of the given color.- Parameters:
pColor
- the original colorpTransparency
- the transparency level (0 - 255
)- Returns:
- a translucent color
- Throws:
java.lang.NullPointerException
- ifpColor
isnull
-
blend
static int blend(int pRGB1, int pRGB2)
Blends two ARGB values half and half, to create a tone in between.- Parameters:
pRGB1
- color 1pRGB2
- color 2- Returns:
- the new rgb value
-
blend
public static java.awt.Color blend(java.awt.Color pColor, java.awt.Color pOther)
Blends two colors half and half, to create a tone in between.- Parameters:
pColor
- color 1pOther
- color 2- Returns:
- a new
Color
-
blend
public static java.awt.Color blend(java.awt.Color pColor, java.awt.Color pOther, float pBlendFactor)
Blends two colors, controlled by the blending factor. A factor of0.0
will return the first color, a factor of1.0
will return the second.- Parameters:
pColor
- color 1pOther
- color 2pBlendFactor
-[0...1]
- Returns:
- a new
Color
-
clamp
private static int clamp(float f)
-
-