com.badlogic.gdx.graphics
Class Pixmap

java.lang.Object
  extended by com.badlogic.gdx.graphics.Pixmap
All Implemented Interfaces:
Disposable

public class Pixmap
extends Object
implements Disposable

A Pixmap represents an image in memory. It has a width and height expressed in pixels as well as a Pixmap.Format specifying the number and order of color components per pixel. Coordinates of pixels are specified with respect to the top left corner of the image, with the x-axis pointing to the right and the y-axis pointing downwards.

By default all methods use blending. You can disable blending with setBlending(Blending). The drawPixmap(Pixmap, int, int, int, int, int, int, int, int) method will scale and stretch the source image to a target image. There either nearest neighbour or bilinear filtering can be used.

A Pixmap stores its data in native heap memory. It is mandatory to call dispose() when the pixmap is no longer needed, otherwise memory leaks will result

Author:
[email protected]

Nested Class Summary
static class Pixmap.Blending
          Blending functions to be set with setBlending(com.badlogic.gdx.graphics.Pixmap.Blending).
static class Pixmap.Filter
          Filters to be used with drawPixmap(Pixmap, int, int, int, int, int, int, int, int).
static class Pixmap.Format
          Different pixel formats.
 
Constructor Summary
Pixmap(byte[] encodedData, int offset, int len)
          Creates a new Pixmap instance from the given encoded image data.
Pixmap(FileHandle file)
          Creates a new Pixmap instance from the given file.
Pixmap(Gdx2DPixmap pixmap)
          Constructs a new Pixmap from a Gdx2DPixmap.
Pixmap(int width, int height, Pixmap.Format format)
          Creates a new Pixmap instance with the given width, height and format.
 
Method Summary
 void dispose()
          Releases all resources associated with this Pixmap.
 void drawCircle(int x, int y, int radius)
          Draws a circle outline with the center at x,y and a radius using the current color and stroke width.
 void drawLine(int x, int y, int x2, int y2)
          Draws a line between the given coordinates using the currently set color.
 void drawPixel(int x, int y)
          Draws a pixel at the given location with the current color.
 void drawPixel(int x, int y, int color)
          Draws a pixel at the given location with the given color.
 void drawPixmap(Pixmap pixmap, int x, int y)
          Draws an area form another Pixmap to this Pixmap.
 void drawPixmap(Pixmap pixmap, int x, int y, int srcx, int srcy, int srcWidth, int srcHeight)
          Draws an area form another Pixmap to this Pixmap.
 void drawPixmap(Pixmap pixmap, int srcx, int srcy, int srcWidth, int srcHeight, int dstx, int dsty, int dstWidth, int dstHeight)
          Draws an area form another Pixmap to this Pixmap.
 void drawRectangle(int x, int y, int width, int height)
          Draws a rectangle outline starting at x, y extending by width to the right and by height downwards (y-axis points downwards) using the current color.
 void fill()
          Fills the complete bitmap with the currently set color.
 void fillCircle(int x, int y, int radius)
          Fills a circle with the center at x,y and a radius using the current color.
 void fillRectangle(int x, int y, int width, int height)
          Fills a rectangle starting at x, y extending by width to the right and by height downwards (y-axis points downwards) using the current color.
 void fillTriangle(int x1, int y1, int x2, int y2, int x3, int y3)
          Fills a triangle with vertices at x1,y1 and x2,y2 and x3,y3 using the current color.
static Pixmap.Blending getBlending()
           
 Pixmap.Format getFormat()
           
 int getGLFormat()
          Returns the OpenGL ES format of this Pixmap.
 int getGLInternalFormat()
          Returns the OpenGL ES format of this Pixmap.
 int getGLType()
          Returns the OpenGL ES type of this Pixmap.
 int getHeight()
           
 int getPixel(int x, int y)
          Returns the 32-bit RGBA8888 value of the pixel at x, y.
 ByteBuffer getPixels()
          Returns the direct ByteBuffer holding the pixel data.
 int getWidth()
           
static void setBlending(Pixmap.Blending blending)
          Sets the type of Pixmap.Blending to be used for all operations.
 void setColor(Color color)
          Sets the color for the following drawing operations.
 void setColor(float r, float g, float b, float a)
          Sets the color for the following drawing operations.
 void setColor(int color)
          Sets the color for the following drawing operations
static void setFilter(Pixmap.Filter filter)
          Sets the type of interpolation Pixmap.Filter to be used in conjunction with drawPixmap(Pixmap, int, int, int, int, int, int, int, int).
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Pixmap

public Pixmap(int width,
              int height,
              Pixmap.Format format)
Creates a new Pixmap instance with the given width, height and format.

Parameters:
width - the width in pixels
height - the height in pixels
format - the Pixmap.Format

Pixmap

public Pixmap(byte[] encodedData,
              int offset,
              int len)
Creates a new Pixmap instance from the given encoded image data. The image can be encoded as JPEG, PNG or BMP.

Parameters:
encodedData - the encoded image data
offset - the offset
len - the length

Pixmap

public Pixmap(FileHandle file)
Creates a new Pixmap instance from the given file. The file must be a Png, Jpeg or Bitmap. Paletted formats are not supported.

Parameters:
file - the FileHandle

Pixmap

public Pixmap(Gdx2DPixmap pixmap)
Constructs a new Pixmap from a Gdx2DPixmap.

Parameters:
pixmap -
Method Detail

setBlending

public static void setBlending(Pixmap.Blending blending)
Sets the type of Pixmap.Blending to be used for all operations. Default is Pixmap.Blending.SourceOver.

Parameters:
blending - the blending type

setFilter

public static void setFilter(Pixmap.Filter filter)
Sets the type of interpolation Pixmap.Filter to be used in conjunction with drawPixmap(Pixmap, int, int, int, int, int, int, int, int).

Parameters:
filter - the filter.

setColor

public void setColor(int color)
Sets the color for the following drawing operations

Parameters:
color - the color, encoded as RGBA8888

setColor

public void setColor(float r,
                     float g,
                     float b,
                     float a)
Sets the color for the following drawing operations.

Parameters:
r - The red component.
g - The green component.
b - The blue component.
a - The alpha component.

setColor

public void setColor(Color color)
Sets the color for the following drawing operations.

Parameters:
color - The color.

fill

public void fill()
Fills the complete bitmap with the currently set color.


drawLine

public void drawLine(int x,
                     int y,
                     int x2,
                     int y2)
Draws a line between the given coordinates using the currently set color.

Parameters:
x - The x-coodinate of the first point
y - The y-coordinate of the first point
x2 - The x-coordinate of the first point
y2 - The y-coordinate of the first point

drawRectangle

public void drawRectangle(int x,
                          int y,
                          int width,
                          int height)
Draws a rectangle outline starting at x, y extending by width to the right and by height downwards (y-axis points downwards) using the current color.

Parameters:
x - The x coordinate
y - The y coordinate
width - The width in pixels
height - The height in pixels

drawPixmap

public void drawPixmap(Pixmap pixmap,
                       int x,
                       int y)
Draws an area form another Pixmap to this Pixmap.

Parameters:
pixmap - The other Pixmap
x - The target x-coordinate (top left corner)
y - The target y-coordinate (top left corner)

drawPixmap

public void drawPixmap(Pixmap pixmap,
                       int x,
                       int y,
                       int srcx,
                       int srcy,
                       int srcWidth,
                       int srcHeight)
Draws an area form another Pixmap to this Pixmap.

Parameters:
pixmap - The other Pixmap
x - The target x-coordinate (top left corner)
y - The target y-coordinate (top left corner)
srcx - The source x-coordinate (top left corner)
srcy - The source y-coordinate (top left corner);
srcWidth - The width of the area form the other Pixmap in pixels
srcHeight - The height of the area form the other Pixmap in pixles

drawPixmap

public void drawPixmap(Pixmap pixmap,
                       int srcx,
                       int srcy,
                       int srcWidth,
                       int srcHeight,
                       int dstx,
                       int dsty,
                       int dstWidth,
                       int dstHeight)
Draws an area form another Pixmap to this Pixmap. This will automatically scale and stretch the source image to the specified target rectangle. Use setFilter(Filter) to specify the type of filtering to be used (nearest neighbour or bilinear).

Parameters:
pixmap - The other Pixmap
srcx - The source x-coordinate (top left corner)
srcy - The source y-coordinate (top left corner);
srcWidth - The width of the area form the other Pixmap in pixels
srcHeight - The height of the area form the other Pixmap in pixles
dstx - The target x-coordinate (top left corner)
dsty - The target y-coordinate (top left corner)
dstWidth - The target width
dstHeight - the target height

fillRectangle

public void fillRectangle(int x,
                          int y,
                          int width,
                          int height)
Fills a rectangle starting at x, y extending by width to the right and by height downwards (y-axis points downwards) using the current color.

Parameters:
x - The x coordinate
y - The y coordinate
width - The width in pixels
height - The height in pixels

drawCircle

public void drawCircle(int x,
                       int y,
                       int radius)
Draws a circle outline with the center at x,y and a radius using the current color and stroke width.

Parameters:
x - The x-coordinate of the center
y - The y-coordinate of the center
radius - The radius in pixels

fillCircle

public void fillCircle(int x,
                       int y,
                       int radius)
Fills a circle with the center at x,y and a radius using the current color.

Parameters:
x - The x-coordinate of the center
y - The y-coordinate of the center
radius - The radius in pixels

fillTriangle

public void fillTriangle(int x1,
                         int y1,
                         int x2,
                         int y2,
                         int x3,
                         int y3)
Fills a triangle with vertices at x1,y1 and x2,y2 and x3,y3 using the current color.

Parameters:
x1 - The x-coordinate of vertex 1
y1 - The y-coordinate of vertex 1
x2 - The x-coordinate of vertex 2
y2 - The y-coordinate of vertex 2
x3 - The x-coordinate of vertex 3
y3 - The y-coordinate of vertex 3

getPixel

public int getPixel(int x,
                    int y)
Returns the 32-bit RGBA8888 value of the pixel at x, y. For Alpha formats the RGB components will be one.

Parameters:
x - The x-coordinate
y - The y-coordinate
Returns:
The pixel color in RGBA8888 format.

getWidth

public int getWidth()
Returns:
The width of the Pixmap in pixels.

getHeight

public int getHeight()
Returns:
The height of the Pixmap in pixels.

dispose

public void dispose()
Releases all resources associated with this Pixmap.

Specified by:
dispose in interface Disposable

drawPixel

public void drawPixel(int x,
                      int y)
Draws a pixel at the given location with the current color.

Parameters:
x - the x-coordinate
y - the y-coordinate

drawPixel

public void drawPixel(int x,
                      int y,
                      int color)
Draws a pixel at the given location with the given color.

Parameters:
x - the x-coordinate
y - the y-coordinate
color - the color in RGBA8888 format.

getGLFormat

public int getGLFormat()
Returns the OpenGL ES format of this Pixmap. Used as the seventh parameter to GLCommon.glTexImage2D(int, int, int, int, int, int, int, int, java.nio.Buffer).

Returns:
one of GL_ALPHA, GL_RGB, GL_RGBA, GL_LUMINANCE, or GL_LUMINANCE_ALPHA.

getGLInternalFormat

public int getGLInternalFormat()
Returns the OpenGL ES format of this Pixmap. Used as the third parameter to GLCommon.glTexImage2D(int, int, int, int, int, int, int, int, java.nio.Buffer).

Returns:
one of GL_ALPHA, GL_RGB, GL_RGBA, GL_LUMINANCE, or GL_LUMINANCE_ALPHA.

getGLType

public int getGLType()
Returns the OpenGL ES type of this Pixmap. Used as the eighth parameter to GLCommon.glTexImage2D(int, int, int, int, int, int, int, int, java.nio.Buffer).

Returns:
one of GL_UNSIGNED_BYTE, GL_UNSIGNED_SHORT_5_6_5, GL_UNSIGNED_SHORT_4_4_4_4

getPixels

public ByteBuffer getPixels()
Returns the direct ByteBuffer holding the pixel data. For the format Alpha each value is encoded as a byte. For the format LuminanceAlpha the luminance is the first byte and the alpha is the second byte of the pixel. For the formats RGB888 and RGBA8888 the color components are stored in a single byte each in the order red, green, blue (alpha). For the formats RGB565 and RGBA4444 the pixel colors are stored in shorts in machine dependent order.

Returns:
the direct ByteBuffer holding the pixel data.

getFormat

public Pixmap.Format getFormat()
Returns:
the Pixmap.Format of this Pixmap.

getBlending

public static Pixmap.Blending getBlending()
Returns:
the currently set Pixmap.Blending


Copyright © 2013. All Rights Reserved.