PulpCore

pulpcore.image
Class CoreImage

java.lang.Object
  extended by pulpcore.image.CoreImage
Direct Known Subclasses:
AnimatedImage

public class CoreImage
extends Object

The CoreImage class contains raster data and provides methods for creating transformed copies of the image.

Methods like crop(int, int, int, int), scale(double), and tint(int) return new CoreImages. The image's raster data can be manipulated using the createGraphics() method.


Constructor Summary
CoreImage(CoreImage image)
          Creates a CoreImage with the same properties as the specified CoreImage.
CoreImage(int width, int height)
          Creates an opaque image, initially black.
CoreImage(int width, int height, boolean isOpaque)
          Creates a blank image.
CoreImage(int width, int height, boolean isOpaque, int[] data)
          Creates a new image using the specified pixel data.
 
Method Summary
 CoreImage background(int argbColor)
           
 CoreGraphics createGraphics()
          Creates a new CoreGaphics context for drawing onto this image.
 CoreImage crop(int x, int y, int w, int h)
          Creates a cropped version of this image.
 CoreImage expandCanvas(int borderSize, int argbColor)
          Create a new image with an expanded canvas size.
 CoreImage expandCanvas(int top, int right, int bottom, int left, int argbColor)
          Create a new image with an expanded canvas size.
 CoreImage fade(int alpha)
           
 CoreImage flip()
          Returns a new CoreImage whose raster data represents a flipped version of this image.
 int getARGB(int x, int y)
          Gets the ARGB color at the specified location.
static CoreImage getBrokenImage()
          Gets the "broken" image, which is the image the system may use when a specified image could not be loaded.
 int[] getData()
          Gets the underlying raster data array.
 int getHeight()
          Gets the height of the image.
 int getHotspotX()
          Gets the x component of the hotspot.
 int getHotspotY()
          Gets the y component of the hotspot.
 int getWidth()
          Gets the width of the image.
 CoreImage halfSize()
          Returns a new CoreImage whose raster data represents a 50% scaled version of this image.
 boolean isOpaque()
          Returns true if the image is opaque.
 boolean isTransparent(int x, int y)
          Checks if the pixel at the specified location is transparent.
static CoreImage load(String imageAsset)
          Loads a PNG or JPEG image from the asset catalog.
 CoreImage mirror()
          Returns a new CoreImage whose raster data represents a mirrored version of this image.
 CoreImage rotate(double angle)
          Creates a rotated version of this image.
 CoreImage rotate(double angle, boolean sizeAsNeeded)
          Creates a rotated version of this image.
 CoreImage rotate(int angle)
          Creates a rotated version of this image.
 CoreImage rotate(int angle, boolean sizeAsNeeded)
          Creates a rotated version of this image.
 CoreImage rotate180()
          Returns a new CoreImage whose raster data represents this image rotated 180 degrees.
 CoreImage rotateLeft()
          Returns a new CoreImage whose raster data represents this image rotated to the left (counter-clockwise 90 degrees).
 CoreImage rotateRight()
          Returns a new CoreImage whose raster data represents this image rotated to the right (clockwise 90 degrees).
 CoreImage scale(double scale)
          Returns a new CoreImage whose raster data represents a scaled version of this image.
 CoreImage scale(int scaledWidth, int scaledHeight)
          Returns a new CoreImage whose raster data represents a scaled version of this image.
 void setHotspot(int x, int y)
          Sets the hotspot of the image.
 CoreImage setTransparentColor(int rgbColor)
          Returns a new CoreImage with all the opaque pixels of the specified color set to transparent.
 CoreImage[] split(int framesAcross)
          Splits the image into several tiles.
 CoreImage[] split(int framesAcross, int framesDown)
          Splits the image into several tiles.
 CoreImage tint(int rgbColor)
          Returns a new CoreImage with every color set to the specified color, without changing the alpha of each color.
 boolean update(int elapsedTime)
          Does nothing by default.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CoreImage

public CoreImage(int width,
                 int height)
Creates an opaque image, initially black.


CoreImage

public CoreImage(int width,
                 int height,
                 boolean isOpaque)
Creates a blank image. For opaque images, the initial image is black, otherwise the initial image is transparent.


CoreImage

public CoreImage(int width,
                 int height,
                 boolean isOpaque,
                 int[] data)
Creates a new image using the specified pixel data. The length of the data must be greater than or equal to width * height.

The raster format is premultiplied ARGB (the same as BufferedImage.TYPE_INT_ARGB_PRE). The raster data array is assumed to be unique to this CoreImage, and not used in any other CoreImages.


CoreImage

public CoreImage(CoreImage image)
Creates a CoreImage with the same properties as the specified CoreImage. The internal raster data array is shared.

Method Detail

createGraphics

public CoreGraphics createGraphics()
Creates a new CoreGaphics context for drawing onto this image.


isOpaque

public final boolean isOpaque()
Returns true if the image is opaque.


getWidth

public final int getWidth()
Gets the width of the image.


getHeight

public final int getHeight()
Gets the height of the image.


getData

public final int[] getData()
Gets the underlying raster data array. The raster format is premultiplied ARGB (the same as BufferedImage.TYPE_INT_ARGB_PRE).


isTransparent

public final boolean isTransparent(int x,
                                   int y)
Checks if the pixel at the specified location is transparent.

Returns:
true if the pixel is transpent or if the location is out of bounds.

getARGB

public final int getARGB(int x,
                         int y)
Gets the ARGB color at the specified location.

Returns:
the ARGB color in non-premultiplied format.
Throws:
IllegalArgumentException - if the specified location is out of bounds.

setHotspot

public final void setHotspot(int x,
                             int y)
Sets the hotspot of the image. The hotspot a the point within the image where the rendering is anchored. For photographs, the hotspot is typically the upper-left corner (0, 0). For cursor images, the hotspot the point that tracks the cursor's position. An ImageSprite also uses the hotspot as the point around which rotation occurs.

See Also:
getHotspotX(), getHotspotY()

getHotspotX

public final int getHotspotX()
Gets the x component of the hotspot.

See Also:
getHotspotY(), setHotspot(int, int)

getHotspotY

public final int getHotspotY()
Gets the y component of the hotspot.

See Also:
getHotspotX(), setHotspot(int, int)

update

public boolean update(int elapsedTime)
Does nothing by default. Subclasses can use this method for dynamically generated images or animations.

Returns:
true if the image changed since the last call to this method.

getBrokenImage

public static CoreImage getBrokenImage()
Gets the "broken" image, which is the image the system may use when a specified image could not be loaded.


load

public static CoreImage load(String imageAsset)
Loads a PNG or JPEG image from the asset catalog. If the PNG file has animation info, an AnimatedImage is returned.

This method never returns null. If the image is not found, an error is printed to the log and a "broken" image is returned. The broken image is similar to a red X image found in a web browser.

Images are internally cached (using a WeakReference), and if the image was previously loaded, this method may return an image with the same internal raster data.

Parameters:
imageAsset - The name of a PNG or JPEG image file.
Returns:
The image, (either a CoreImage or an AnimatedImage) or a broken image if the image cannot be found.

split

public CoreImage[] split(int framesAcross)
Splits the image into several tiles.


split

public CoreImage[] split(int framesAcross,
                         int framesDown)
Splits the image into several tiles.


expandCanvas

public CoreImage expandCanvas(int borderSize,
                              int argbColor)
Create a new image with an expanded canvas size. The borders are filled with the specified color. The borderSize parameter can be negative. The hotspot is translated accordingly.


expandCanvas

public CoreImage expandCanvas(int top,
                              int right,
                              int bottom,
                              int left,
                              int argbColor)
Create a new image with an expanded canvas size. The borders are filled with the specified color. The top, right, bottom, left parameters can be negative. The hotspot is translated accordingly.


crop

public CoreImage crop(int x,
                      int y,
                      int w,
                      int h)
Creates a cropped version of this image. The raster data is not shared. The hotspot is copied as-is, with no translation.


rotate

public CoreImage rotate(double angle)
Creates a rotated version of this image. Same as calling rotate(angle, true);

Parameters:
angle - an angle, typically in the range from 0 to Math.PI * 2

rotate

public CoreImage rotate(double angle,
                        boolean sizeAsNeeded)
Creates a rotated version of this image.

Parameters:
sizeAsNeeded - if true, the resulting image is sized to contain the entire rotated image. If false, the resulting image is the same size as this image. The hotspot is rotated accordingly.
angle - an angle, typically in the range from 0 to Math.PI * 2

rotate

public CoreImage rotate(int angle)
Creates a rotated version of this image. Same as calling rotate(angle, true);

Parameters:
angle - a fixed-point angle, typically in the range from 0 to CoreMath.TWO_PI.

rotate

public CoreImage rotate(int angle,
                        boolean sizeAsNeeded)
Creates a rotated version of this image.

Parameters:
sizeAsNeeded - if true, the resulting image is sized to contain the entire rotated image. If false, the resulting image is the same size as this image. The hotspot is rotated accordingly.
angle - a fixed-point angle, typically in the range from 0 to CoreMath.TWO_PI.

scale

public CoreImage scale(double scale)
Returns a new CoreImage whose raster data represents a scaled version of this image. The hotspot is scaled accordingly.


scale

public CoreImage scale(int scaledWidth,
                       int scaledHeight)
Returns a new CoreImage whose raster data represents a scaled version of this image. The hotspot is scaled accordingly.


halfSize

public CoreImage halfSize()
Returns a new CoreImage whose raster data represents a 50% scaled version of this image. This method uses a weighted average instead of bilinear interpolation. The hotspot is scaled accordingly.


mirror

public CoreImage mirror()
Returns a new CoreImage whose raster data represents a mirrored version of this image. The hotspot is mirrored accordingly.


flip

public CoreImage flip()
Returns a new CoreImage whose raster data represents a flipped version of this image. The hotspot is flipped accordingly.


rotateLeft

public CoreImage rotateLeft()
Returns a new CoreImage whose raster data represents this image rotated to the left (counter-clockwise 90 degrees). The hotspot is rotated accordingly.


rotateRight

public CoreImage rotateRight()
Returns a new CoreImage whose raster data represents this image rotated to the right (clockwise 90 degrees). The hotspot is rotated accordingly.


rotate180

public CoreImage rotate180()
Returns a new CoreImage whose raster data represents this image rotated 180 degrees. The hotspot is rotated accordingly.


setTransparentColor

public CoreImage setTransparentColor(int rgbColor)
Returns a new CoreImage with all the opaque pixels of the specified color set to transparent.

Parameters:
rgbColor - The color to convert to transparent. The alpha component is ignored.
Returns:
the new image.

tint

public CoreImage tint(int rgbColor)
Returns a new CoreImage with every color set to the specified color, without changing the alpha of each color. This method is useful for creating a variety of colored fonts or for creating a solid-color stencil of a sprite.


background

public CoreImage background(int argbColor)

fade

public CoreImage fade(int alpha)

PulpCore

Copyright © 2007-2009 Interactive Pulp, LLC.