danger.ui
Class Bitmap

java.lang.Object
  extended by danger.ui.Bitmap
Direct Known Subclasses:
Bitmap.FromResource, ImmutableBitmap, IntBitmap, ShortBitmap


public class Bitmap
extends Object

Rectangular grid of pixels, in one of several different colorspaces. Direct instances of this class may be modified, but instances of the subclass ImmutableBitmap may not.


Nested Class Summary
static class Bitmap.FromResource
          Bitmaps that come from resources are instances of this subclass of Bitmap.
 
Field Summary
static int COPYBITS_ALPHA
          Perform an alpha-blended copy, with the alpha channel specified in the high byte of the flags word.
static int COPYBITS_ALPHA_MASK
          Mask for extracting the alpha channel from the flags word.
static int COPYBITS_CLAMP_TRANSPARENCY
          Make partially transparent source pixels fully transparent in the desitnation.
static int COPYBITS_FLAGS_MASK
          Mask for extracting the flags bits from the flags word.
static int COPYBITS_INTERPOLATED
          Use bilinear interpolation for smooth scaling of copy.
static int COPYBITS_INVERTED
          Invert each pixel (XOR with -1) as it is copied.
static int COPYBITS_SRCOVER
          Copy will behave as a strided memcpy.
static int COPYBITS_SRCOVER_TRANS
          Like SRCOVER, but the source transparent color (if any) will be converted to the destination transparent color.
static boolean DONT_PRESERVE_TRANSPARENCY
          Used as a parameter to convertToScreenColorSpace.
protected  int mPaletteFlags
          Deprecated. Use getPaletteFlags() instead.
static int PALETTE_BGR16
          Image contains an RGB16_BGR palette at the end of the pixel data.
static int PALETTE_BGR16_ALPHA
          Image contains an RGB16_BGR palette at the end of the pixel data, and an alpha palette at the end of the palette data.
static int PALETTE_BGR16_OFFSET
          Image contains an RGB16_BGR palette at the offset specified in the low three bytes of the palette word.
static int PALETTE_FLAGS_MASK
          Mask for extracting the flags bits from the palette word.
static int PALETTE_NONE
          Image contains no palette.
static int PALETTE_RGB16_LE
          Image contains an RGB16_RGB_LE palette at the end of the pixel data.
static int PALETTE_RGB16_LE_ALPHA
          Image contains an RGB16_RGB_LE palette at the end of the pixel data, and an alpha palette at the end of the palette data.
static int PALETTE_RGB16_LE_OFFSET
          Image contains an RGB16_RGB_LE palette at the offset specified in the low three bytes of the palette word.
static int PALETTE_SIZE_MASK
          Mask for extracting the palette size or offset from the palette word.
static boolean PRESERVE_TRANSPARENCY
          Used as a parameter to convertToScreenColorSpace.
 
Constructor Summary
Bitmap()
          Construct an empty zero-sized instance.
Bitmap(Bitmap src)
          Construct an instance copied from another instance.
Bitmap(Bitmap src, int width, int height)
          Construct an instance copied from another instance.
Bitmap(Bitmap src, Rect r)
          Construct an instance copied from a portion of another instance.
Bitmap(byte[] pixels, int width, int height, int transparentColor)
          Construct a palette-less instance in the default colorspace.
Bitmap(byte[] pixels, int width, int height, int colorspace, int transparentColor)
          Construct a palette-less instance.
Bitmap(byte[] pixels, int width, int height, int colorspace, int transparentColor, int paletteSize)
          Construct an instance.
Bitmap(int width, int height)
          Construct an empty instance.
 
Method Summary
 boolean canDrawInverted()
           
static Bitmap convertToScreenColorSpace(Bitmap bitmap)
          Convert a bitmap to the native screen format.
static Bitmap convertToScreenColorSpace(Bitmap bitmap, boolean preserveTransparency)
          Convert to screen color space and optionally preserve the transparent color in a colorspace independent way.
static Bitmap convertToScreenColorSpace(Bitmap bitmap, int backgroundColor)
          Convert a bitmap to the native screen format.
static Bitmap copy(Bitmap src)
           
static Bitmap copy(Bitmap src, int width, int height)
           
static Bitmap copy(Bitmap src, Rect r)
           
static void copyBits(Bitmap to, Rect toRect, Bitmap from, Rect fromRect)
          Copy a region from one bitmap to another
static void copyBits(Bitmap to, Rect toRect, Bitmap from, Rect fromRect, int flags)
           
static void copyBitsInterpolated(Bitmap to, Rect toRect, Bitmap from, Rect fromRect)
          Copy a region from one bitmap to another
 void copyBitsTo(Bitmap to, Rect toRect, Rect fromRect)
          Copy the contents of fromRect in this bitmap, to toRect in the provided destination Bitmap
 void copyInterpolated(Bitmap src)
          Copy the contents of the provided source bitmap to this bitmap, scaling them to fit using bilinear interpolation
 boolean copyRotated(Bitmap from, int degrees)
           
 void copyScaled(Bitmap src)
          Copy the contents of the provided source bitmap to this bitmap, scaling them to fit
static Bitmap createOptimalBitmap(Object pixels, int width, int height, int colorspace, int transparentColor, int paletteSize)
           
 Pen createPen()
          Create a pen that draws on this instance, with location (0, 0) corresponding to the upper-left corner of the bitmap area.
 Pen createPen(int dx, int dy)
          Create a pen that draws on this instance, offset by the given amount from the upper-left corner of the bitmap area.
static void flatten(Bitmap src, byte[] dst, int dstOffset)
           
 int getColorSpace()
           
static int getFlattenedSize(Bitmap src)
           
 int getHeight()
           
protected  int getPaletteFlags()
          Return the palette flags.
protected  int getPaletteOffset()
           
 int getPaletteSize()
           
 byte[] getPixels()
          return the raw pixel data array
 int getPoint(int x, int y)
           
 int getPoint(Point point)
           
 int getStride()
           
 int getTransparentColor()
           
 int getTransparentColorRGB()
          Get the transparent color for this bitmap in RGB components.
 int getWidth()
           
 boolean isGrayscale()
          Determine if this bitmap is color or black/white.
 void mirror()
           
static Bitmap newFromEncodedData(byte[] data)
           
static Bitmap newFromFlattenedData(byte[] data, int offset)
           
static Bitmap newFromJPEGData(byte[] jpegData)
           
static Bitmap newFromPNGData(byte[] pngData)
           
 void replaceColor(int color, int newColor)
           
 void replacePaletteColor(int color, int newColor)
           
 void replacePaletteColors(int[] colorPairs)
          Replace the palette colors for the given color pairs.
 Bitmap rotateBy(int degrees)
          return a bitmap rotated by 0, 90, 180, or 270 degrees.
 Bitmap scaleTo(int width, int height)
          Return a new bitmap, containing the contents of this bitmap, scaled to the provided size
 void setTo(Bitmap src)
          Sets the values of this bitmap to those of a passed in bitmap
 void setTransparentColor(int color)
          Directly set the transparent color for this bitmap.
 void setTransparentColorRGB(int rgb)
          Set the transparent color for this bitmap from rgb components.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

COPYBITS_INVERTED

public static final int COPYBITS_INVERTED
Invert each pixel (XOR with -1) as it is copied.

See Also:
Constant Field Values

COPYBITS_SRCOVER

public static final int COPYBITS_SRCOVER
Copy will behave as a strided memcpy.

See Also:
Constant Field Values

COPYBITS_CLAMP_TRANSPARENCY

public static final int COPYBITS_CLAMP_TRANSPARENCY
Make partially transparent source pixels fully transparent in the desitnation.

See Also:
Constant Field Values

COPYBITS_SRCOVER_TRANS

public static final int COPYBITS_SRCOVER_TRANS
Like SRCOVER, but the source transparent color (if any) will be converted to the destination transparent color.

See Also:
Constant Field Values

COPYBITS_ALPHA

public static final int COPYBITS_ALPHA
Perform an alpha-blended copy, with the alpha channel specified in the high byte of the flags word.

See Also:
Constant Field Values

COPYBITS_INTERPOLATED

public static final int COPYBITS_INTERPOLATED
Use bilinear interpolation for smooth scaling of copy.

See Also:
Constant Field Values

COPYBITS_FLAGS_MASK

public static final int COPYBITS_FLAGS_MASK
Mask for extracting the flags bits from the flags word.

See Also:
Constant Field Values

COPYBITS_ALPHA_MASK

public static final int COPYBITS_ALPHA_MASK
Mask for extracting the alpha channel from the flags word.

See Also:
Constant Field Values

PALETTE_NONE

public static final int PALETTE_NONE
Image contains no palette.

See Also:
Constant Field Values

PALETTE_BGR16

public static final int PALETTE_BGR16
Image contains an RGB16_BGR palette at the end of the pixel data.

See Also:
Constant Field Values

PALETTE_BGR16_ALPHA

public static final int PALETTE_BGR16_ALPHA
Image contains an RGB16_BGR palette at the end of the pixel data, and an alpha palette at the end of the palette data.

See Also:
Constant Field Values

PALETTE_BGR16_OFFSET

public static final int PALETTE_BGR16_OFFSET
Image contains an RGB16_BGR palette at the offset specified in the low three bytes of the palette word.

See Also:
Constant Field Values

PALETTE_RGB16_LE

public static final int PALETTE_RGB16_LE
Image contains an RGB16_RGB_LE palette at the end of the pixel data.

See Also:
Constant Field Values

PALETTE_RGB16_LE_ALPHA

public static final int PALETTE_RGB16_LE_ALPHA
Image contains an RGB16_RGB_LE palette at the end of the pixel data, and an alpha palette at the end of the palette data.

See Also:
Constant Field Values

PALETTE_RGB16_LE_OFFSET

public static final int PALETTE_RGB16_LE_OFFSET
Image contains an RGB16_RGB_LE palette at the offset specified in the low three bytes of the palette word.

See Also:
Constant Field Values

PALETTE_FLAGS_MASK

public static final int PALETTE_FLAGS_MASK
Mask for extracting the flags bits from the palette word.

See Also:
Constant Field Values

PALETTE_SIZE_MASK

public static final int PALETTE_SIZE_MASK
Mask for extracting the palette size or offset from the palette word.

See Also:
Constant Field Values

PRESERVE_TRANSPARENCY

public static final boolean PRESERVE_TRANSPARENCY
Used as a parameter to convertToScreenColorSpace.

See Also:
Constant Field Values

DONT_PRESERVE_TRANSPARENCY

public static final boolean DONT_PRESERVE_TRANSPARENCY
Used as a parameter to convertToScreenColorSpace.

See Also:
Constant Field Values

mPaletteFlags

protected int mPaletteFlags
Deprecated. Use getPaletteFlags() instead.
Constructor Detail

Bitmap

public Bitmap(byte[] pixels,
              int width,
              int height,
              int colorspace,
              int transparentColor,
              int paletteSize)
Construct an instance.

Parameters:
pixels - null-ok; the pixels of the bitmap, or null if the bitmap is to be initially blank; if non-null then the pixels become shared (modifying the original array affects the constructed instance)
width - >= 0; the width of the bitmap
height - >= 0; the height of the bitmap
colorspace - the colorspace; see ColorSpace
transparentColor - what color ought to be considered "transparent" when drawing the bitmap; use -1 if there is to be no transparency
paletteSize - the number of colors in the palette; if >= 0, then the palette is located just past the end of the pixel data per se in pixels

Bitmap

public Bitmap()
Construct an empty zero-sized instance. This constructor is equivalent to the full constructor with the following parameters: Bitmap(null, 0, 0, Screen.getColorSpace(), -1, 0)


Bitmap

public Bitmap(int width,
              int height)
Construct an empty instance. This constructor is equivalent to the full constructor with the following parameters: Bitmap(null, width, height, Screen.getColorSpace(), -1, 0)

Parameters:
width - >= 0; the width of the bitmap
height - >= 0; the height of the bitmap

Bitmap

public Bitmap(byte[] pixels,
              int width,
              int height,
              int colorspace,
              int transparentColor)
Construct a palette-less instance. This constructor is equivalent to the full constructor with the following parameters: Bitmap(pixels, width, height, colorspace, transparentColor, 0)

Parameters:
pixels - null-ok; the pixels of the bitmap, or null if the bitmap is to be initially blank; if non-null then the pixels become shared (modifying the original array affects the constructed instance)
width - >= 0; the width of the bitmap
height - >= 0; the height of the bitmap
colorspace - the colorspace; see ColorSpace
transparentColor - what color ought to be considered "transparent" when drawing the bitmap; use -1 if there is to be no transparency

Bitmap

public Bitmap(Bitmap src)
Construct an instance copied from another instance. The new instance has all of the properties of the original but it does not share pixel data (which is instead cloned).

Parameters:
src - non-null; the original instance

Bitmap

public Bitmap(Bitmap src,
              int width,
              int height)
Construct an instance copied from another instance. The new instance has all of the properties of the original except for possibly the width and height. The pixel data for the new instance is copied from the original: if the original is larger than the new instance's dimensions, then the new instance has only the upper-left portion of the original; if the original is smaller, then the new instance is filled with zero pixel values in the excess area.

Parameters:
src - non-null; the original instance
width - >= 0; the width of the bitmap
height - >= 0; the height of the bitmap

Bitmap

public Bitmap(Bitmap src,
              Rect r)
Construct an instance copied from a portion of another instance. The new instance has all of the properties of the original except for possibly the width and height. The pixel data for the new instance is copied from a specified portion of the original: if the original is larger than the new instance's dimensions, then the new instance has only the upper-left portion of the original; if the original is smaller, then the new instance is filled with zero pixel values in the excess area.

Parameters:
src - non-null; the original instance
r - non-null; a rectangle specifying the portion of the original pixel data

Bitmap

public Bitmap(byte[] pixels,
              int width,
              int height,
              int transparentColor)
Construct a palette-less instance in the default colorspace. This constructor is equivalent to the full constructor with the following parameters: Bitmap(pixels, width, height, Screen.getColorSpace(), transparentColor, 0)

Parameters:
pixels - null-ok; the pixels of the bitmap, or null if the bitmap is to be initially blank; if non-null then the pixels become shared (modifying the original array affects the constructed instance)
width - >= 0; the width of the bitmap
height - >= 0; the height of the bitmap
transparentColor - what color ought to be considered "transparent" when drawing the bitmap; use -1 if there is to be no transparency
Method Detail

copy

public static Bitmap copy(Bitmap src)

copy

public static Bitmap copy(Bitmap src,
                          int width,
                          int height)

copy

public static Bitmap copy(Bitmap src,
                          Rect r)

createOptimalBitmap

public static Bitmap createOptimalBitmap(Object pixels,
                                         int width,
                                         int height,
                                         int colorspace,
                                         int transparentColor,
                                         int paletteSize)

newFromPNGData

public static Bitmap newFromPNGData(byte[] pngData)
                             throws IOException
Throws:
IOException

newFromJPEGData

public static Bitmap newFromJPEGData(byte[] jpegData)
                              throws IOException
Throws:
IOException

newFromEncodedData

public static Bitmap newFromEncodedData(byte[] data)
                                 throws IOException
Throws:
IOException

newFromFlattenedData

public static Bitmap newFromFlattenedData(byte[] data,
                                          int offset)

flatten

public static void flatten(Bitmap src,
                           byte[] dst,
                           int dstOffset)

getFlattenedSize

public static int getFlattenedSize(Bitmap src)

createPen

public Pen createPen(int dx,
                     int dy)
Create a pen that draws on this instance, offset by the given amount from the upper-left corner of the bitmap area.

Parameters:
dx - the x offset
dy - the y offset
Returns:
non-null; the pen

createPen

public Pen createPen()
Create a pen that draws on this instance, with location (0, 0) corresponding to the upper-left corner of the bitmap area.

Returns:
non-null; the pen

getPoint

public int getPoint(int x,
                    int y)

getPoint

public int getPoint(Point point)

getPaletteSize

public int getPaletteSize()

setTo

public void setTo(Bitmap src)
Sets the values of this bitmap to those of a passed in bitmap


scaleTo

public Bitmap scaleTo(int width,
                      int height)
Return a new bitmap, containing the contents of this bitmap, scaled to the provided size


copyScaled

public void copyScaled(Bitmap src)
Copy the contents of the provided source bitmap to this bitmap, scaling them to fit


copyInterpolated

public void copyInterpolated(Bitmap src)
Copy the contents of the provided source bitmap to this bitmap, scaling them to fit using bilinear interpolation


copyBitsTo

public final void copyBitsTo(Bitmap to,
                             Rect toRect,
                             Rect fromRect)
Copy the contents of fromRect in this bitmap, to toRect in the provided destination Bitmap


copyBits

public static void copyBits(Bitmap to,
                            Rect toRect,
                            Bitmap from,
                            Rect fromRect)
Copy a region from one bitmap to another


copyBits

public static void copyBits(Bitmap to,
                            Rect toRect,
                            Bitmap from,
                            Rect fromRect,
                            int flags)

copyBitsInterpolated

public static void copyBitsInterpolated(Bitmap to,
                                        Rect toRect,
                                        Bitmap from,
                                        Rect fromRect)
Copy a region from one bitmap to another


rotateBy

public Bitmap rotateBy(int degrees)
return a bitmap rotated by 0, 90, 180, or 270 degrees. Other rotations currently return null.


copyRotated

public boolean copyRotated(Bitmap from,
                           int degrees)

mirror

public void mirror()

getWidth

public final int getWidth()

getHeight

public final int getHeight()

getStride

public final int getStride()

getColorSpace

public final int getColorSpace()

getTransparentColor

public final int getTransparentColor()

getTransparentColorRGB

public int getTransparentColorRGB()
Get the transparent color for this bitmap in RGB components.

Returns:
Transparent color in packed RGB format.

getPixels

public byte[] getPixels()
return the raw pixel data array


setTransparentColor

public void setTransparentColor(int color)
Directly set the transparent color for this bitmap. No processing is done on the given color. It is installed as is.

Parameters:
color - A colorspace dependent integer to use as the transparent color.

setTransparentColorRGB

public void setTransparentColorRGB(int rgb)
Set the transparent color for this bitmap from rgb components. the rgb parameter is processed according to the bitmap's before being installed.

Parameters:
rgb - An RGB color packed into an integer. The alpha component is masked out and ignored.

replacePaletteColor

public void replacePaletteColor(int color,
                                int newColor)

replacePaletteColors

public void replacePaletteColors(int[] colorPairs)
Replace the palette colors for the given color pairs. Odd items in the array are the source color. Even items are the destination color.


replaceColor

public void replaceColor(int color,
                         int newColor)

convertToScreenColorSpace

public static Bitmap convertToScreenColorSpace(Bitmap bitmap,
                                               int backgroundColor)
Convert a bitmap to the native screen format. This is needed in order to be able to draw onto a bitmap. You cannot draw into some kinds of bitmaps (for example, indexed). NOTE: This method does NOT preserve transparency. If you need to preserve transparency use convertToScreenColorSpace(Bitmap, boolean).

Parameters:
bitmap - Bitmap to be converted.
backgroundColor - Color to fill destination with before copying source.
Returns:
A drawable duplicate of the source.

convertToScreenColorSpace

public static Bitmap convertToScreenColorSpace(Bitmap bitmap)
Convert a bitmap to the native screen format. This is needed in order to be able to draw onto a bitmap. You cannot draw into some kinds of bitmaps (for example, indexed). NOTE: This method does NOT preserve transparency. The source is drawn onto a white background. If you need to preserve transparency use convertToScreenColorSpace(Bitmap, boolean).

Parameters:
bitmap - Bitmap to be converted.
Returns:
A drawable duplicate of the source.

convertToScreenColorSpace

public static Bitmap convertToScreenColorSpace(Bitmap bitmap,
                                               boolean preserveTransparency)
Convert to screen color space and optionally preserve the transparent color in a colorspace independent way. Previously defined versions of this method did NOT preserve the transparent color. You can use the constants (@link PRESERVE_TRANSPARENCY), (@ling DONT_PRESERVE_TRANSPARENCY) to document the preserveTransparency parameter.

Parameters:
bitmap - Bitmap to be converted.
preserveTransparency - True if you want the transparency to be maintained.
Returns:
Bitmap containing a copy of the original converted to default colorspace.

isGrayscale

public boolean isGrayscale()
Determine if this bitmap is color or black/white. Returns true if the bitmap has a black/white colorspace.


canDrawInverted

public boolean canDrawInverted()

toString

public String toString()
Overrides:
toString in class Object

getPaletteOffset

protected int getPaletteOffset()

getPaletteFlags

protected final int getPaletteFlags()
Return the palette flags. The upper 8 bits of this field contain the palette type, the lower 24 are specific to the type (but usually palette size).

Since:
3.0