imageutils

Support tools for creating images and image resources.
Constants
Image.create( width, height, [hasTransparency] ) static
Image.createForResolution( dpi, width, height, [hasTransparency] ) static
Image.fetchImageResource( relativeURL, [cacheResult] ) static
Image.fetchIconResource( relativeURL ) static
Creates an icon from an existing image. The icon will be resized to fit
Image.stitch( image1, image2, stitchEdge ) static
Image.resize( image, width, height, [fast] ) static
Image.fit( image, width, height, [fast] ) static
Image.crop( image, x, y, width, height ) static
Image.tint( image, h, s, b ) static
Image.mirror( image, [horiz], [vert] ) static
Image.invert( image ) static
Image.desaturate( image ) static
Image.read( file ) static
Image.write( image, file, [format], [quality], [progressive], [dpi] ) static
Image.save( image, [defaultFile] ) static
Image.view( image, [title], [modal] ) static

Support tools for creating images and image resources.

This library defines an object, Image, which contains utility methods for working with images. It is mostly used by resource creation scripts. A resource creation script replaces a resource file with a script capable of generating that resource. The script must define a function, createResource() that returns the new resource.

One way to use an image resource creation scripts is to break a large image into smaller ones in such a way that they can be stored more efficiently. This reduces the download footprint of the program. A good example is an image that is mostly opaque with a small transparent area. Transparent images are not stored as efficiently as non-transparent ones, so it may be more efficient to split the image into two or more pieces and store each in a separate file. The pieces are then combined into the final image at runtime by the resource creation script.

Constants

Image.STITCH_HORIZONTAL indicates that images should be stitched from left to right
Image.STITCH_VERTICAL indicates that images should be stitched from top to bottom

Image.create( width, height, [hasTransparency] ) static

Create an image that is width pixels by height pixels. If hasTransparency is true, the image will have an alpha channel (allowing transparency and translucency), otherwise the image is opaque.

width the image width, in pixels
height the image height, in pixels
hasTransparency if true, create an alpha channel for the image

returns a BufferedImage with the requested parameters

Image.createForResolution( dpi, width, height, [hasTransparency] ) static

Create an image that is width points by height points. The conversion of points into pixels is determined by dpi, the number of dots (pixels) per inch. If hasTransparency is true, the image will have an alpha channel (allowing transparency and translucency), otherwise the image is opaque.

dpi the image resolution, in dots (pixels) per inch
width the image width, in points
height the image height, in points
hasTransparency if true, create an alpha channel for the image

returns a BufferedImage with the requested parameters

Image.fetchImageResource( relativeURL, [cacheResult] ) static

Obtain an image from the Strange Eons resources. If the image cannot be obtained, null is returned. Typically this indicates that the URL is incorrect.

relativeURL a URL relative to the resources folder
cacheResult if true, the image may be cached to speed future requests

returns the image resource as a BufferedImage, or null

Image.fetchIconResource( relativeURL ) static

Create an Icon from an image resource, suitable for use with Swing components. If the image cannot be obtained, null is returned. Typically, this indicates that the URL is incorrect. (Note that is usually safe to set a component's Icon to null, as it is interpreted as setting no icon.)

relativeURL a URL relative to the resources folder

returns the image resource as a javax.swing.ImageIcon, or null

Creates an icon from an existing image. The icon will be resized to fit

within size by size pixels.

image the image to use in the icon
size the target size for the icon (default is 16)

returns an icon that displays the image and is the requested size

Image.stitch( image1, image2, stitchEdge ) static

Returns a new image that combines two source images by "stitching" them together along an edge. If stitchEdge is Image.STITCH_HORIZONTAL, then the right edge of image1 will be stitched to the left edge of image2. If stitchEdge is Image.STITCH_VERTICAL, then the bottom edge of image1 will be stitched to the top edge of image2 If either source image has transparency, then the returned image will also; otherwise it is opaque.

image1 the first BufferedImage to be stitched
image2 the second BufferedImage to be stitched
stitchEdge a constant indicating which edges to join

returns a BufferedImage that joins the source images at their edges

Image.resize( image, width, height, [fast] ) static

Create a copy of a BufferedImage that is resampled at a new size, either smaller or larger in either dimension.

image the image to create a resized copy of
width the width of the new image, in pixels
height the height of the new image, in pixels
fast an optional hint; if true, less accurate but faster resampling is used (default is false)

returns a resized copy of image

Image.fit( image, width, height, [fast] ) static

Ensures that image is as large as it can be and still fit within width and height pixels, without changing the aspect ratio. If the image already just fits, it is returned. Otherwise a new, resized image is returned.

image the image to fit within the space
width the maximum width of the resized image
height the maximum height of the resized image
fast if true a faster but lower quality algorithm is used (default is false)

Image.crop( image, x, y, width, height ) static

Create an image by cropping a source image. The resulting image will consist of the subimage of the source image that starts at pixel (x,y) in the source image and is width by height pixels in size. Either width or height (or both) may be less than 1, in which case the width will be set to the number of pixels from the upper-left corner of the crop to the edge.

image the source image, which is not changed
x the x-coordinate of the upper-left corner of the region to retain in the destination
y the y-coordinate of the upper-left corner of the region to retain in the destination
width the width of the region to retain in the destination
height the height of the region to retain in the destination

Image.tint( image, h, s, b ) static

Returns a tinted copy of an image. The value of h is a relative number of degrees. The hue of each pixel in the source image will be shifted by h degrees around the HSB colour wheel; a value of 0 leaves the hue unchanged. The value of s is a saturation value between 0 and 1. The saturation of each pixel in the source image will be set to s; a value of 0 will convert the image to grayscale. The value of b is a positive brightness factor. A value of 1 retains the pixel's original brightness. (If a pixel's brightness exceeds 1 after being multiplied by the brightness factor, it will be clamped to 1.)

image a BufferedImage to tint
h the hue shift to apply
s the saturation of the result
b the brightness factor

returns a tinted copy of image

Image.mirror( image, [horiz], [vert] ) static

Returns a new BufferedImage that is a mirror image of image.

image a BufferedImage to mirror
horiz if true, flip horizontally (default is true)
vert if true, flip vertically (default is false)

returns a mirrored copy of image

Image.invert( image ) static

Returns a copy of the image with all of the pixels inverted.

image the image to invert

returns the inverse image

Image.desaturate( image ) static

Returns a copy of the image converted to greyscale.

image the image to desaturate

returns the desaturated image

Image.read( file ) static

Read and return an image from a file.

file the name of the file to read

returns the image, or throws an error if reading fails

Image.write( image, file, [format], [quality], [progressive], [dpi] ) static

Write an image to a file. The value of format is a string that describes the desired image format. Both "png" and "jpg" are acceptable. If format is not defined, it defaults to "png".

image the image to write
file the file to write to; either a file name string or a File object
format a string describing the format to write the image in (png or jpg)
quality a value between 0 and 1 (inclusive) to control compression quality (higher is better quality, -1 for default, >1 to request lossless compression if possible)
progressive if true requests an image that can be displayed progressively as it downloads (default is false)
dpi if set, and supported by the image format, the image's metadata will ..
indicate that this is the resolution of the image (in pixels/dots per inch)

Image.save( image, [defaultFile] ) static

Prompts the user to choose a file, then saves image to the selected file. If no defaultFile is specified, then a default location is selected based on the last saved file, or if no image file has been saved previously, a platform-specific default location is used. The value of defaultFile may either be a java.io.File object, or else it can be another object whose string representation is a valid path on this platform.

returns true if the image was saved, or false if the user cancelled the save operation

Image.view( image, [title], [modal] ) static

Display an image in a simple image viewer window.

image a BufferedImage to display
title an optional title for the view window
modal if true, the viewer should be modal (blocks the application until closed)

Index    Contents