APIdia Launch Partner
Top Description Inners Fields Constructors Methods
com.badlogic.gdx.graphics.g2d

public Class PixmapPacker

extends Object
implements Disposable
Class Inheritance
All Implemented Interfaces
com.badlogic.gdx.utils.Disposable
Imports
java.util.Arrays, .Comparator, java.util.regex.Matcher, .Pattern, com.badlogic.gdx.Gdx, com.badlogic.gdx.graphics.Color, .Pixmap, .Pixmap.Blending, .Pixmap.Format, .Texture, .Texture.TextureFilter, com.badlogic.gdx.graphics.g2d.PixmapPacker.SkylineStrategy.SkylinePage.Row, com.badlogic.gdx.graphics.glutils.PixmapTextureData, com.badlogic.gdx.utils.Array, .Disposable, .GdxRuntimeException, .OrderedMap

Packs pixmaps into one or more pages to generate an atlas of pixmap instances. Provides means to directly convert the pixmap atlas to a TextureAtlas. The packer supports padding and border pixel duplication, specified during construction. The packer supports incremental inserts and updates of TextureAtlases generated with this class. How bin packing is performed can be customized via PackStrategy.

All methods can be called from any thread unless otherwise noted.

One-off usage:

// 512x512 pixel pages, RGB565 format, 2 pixels of padding, border duplication
PixmapPacker packer = new PixmapPacker(512, 512, Format.RGB565, 2, true);
packer.pack("First Pixmap", pixmap1);
packer.pack("Second Pixmap", pixmap2);
TextureAtlas atlas = packer.generateTextureAtlas(TextureFilter.Nearest, TextureFilter.Nearest, false);
packer.dispose();
// ...
atlas.dispose();
With this usage pattern, disposing the packer will not dispose any pixmaps used by the texture atlas. The texture atlas must also be disposed when no longer needed. Incremental texture atlas usage:
// 512x512 pixel pages, RGB565 format, 2 pixels of padding, no border duplication
PixmapPacker packer = new PixmapPacker(512, 512, Format.RGB565, 2, false);
TextureAtlas atlas = new TextureAtlas();

// potentially on a separate thread, e.g. downloading thumbnails
packer.pack("thumbnail", thumbnail);

// on the rendering thread, every frame
packer.updateTextureAtlas(atlas, TextureFilter.Linear, TextureFilter.Linear, false);

// once the atlas is no longer needed, make sure you get the final additions. This might
// be more elaborate depending on your threading model.
packer.updateTextureAtlas(atlas, TextureFilter.Linear, TextureFilter.Linear, false);
// ...
atlas.dispose();
Pixmap-only usage:
PixmapPacker packer = new PixmapPacker(512, 512, Format.RGB565, 2, true);
packer.pack("First Pixmap", pixmap1);
packer.pack("Second Pixmap", pixmap2);

// do something interesting with the resulting pages
for (Page page : packer.getPages()) {
	// ...
}

packer.dispose();
Authors
mzechner, Nathan Sweet, Rob Rendell

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
public static class
PixmapPacker.GuillotineStrategy

Does bin packing by inserting to the right or below previously packed rectangles.

public static interface
PixmapPacker.PackStrategy

Choose the page and location for each rectangle.

public static class
public static class
public static class
PixmapPacker.SkylineStrategy

Does bin packing by inserting in rows.

Field Summary

Modifier and TypeField and Description
pack-priv int
private Color
c

pack-priv boolean
pack-priv boolean
pack-priv static Pattern
pack-priv PixmapPacker.PackStrategy
pack-priv boolean
pack-priv int
pack-priv Pixmap.Format
pack-priv int
pack-priv final Array<PixmapPacker.Page>
pack-priv int
pack-priv boolean
pack-priv boolean
pack-priv Color

Constructor Summary

AccessConstructor and Description
public
PixmapPacker(int pageWidth, int pageHeight, Pixmap.Format pageFormat, int padding, boolean duplicateBorder)

Uses GuillotineStrategy.

public
PixmapPacker(int pageWidth, int pageHeight, Pixmap.Format pageFormat, int padding, boolean duplicateBorder, PixmapPacker.PackStrategy packStrategy)

Uses GuillotineStrategy.

public
PixmapPacker(int pageWidth, int pageHeight, Pixmap.Format pageFormat, int
the number of blank pixels to insert between pixmaps.
padding
,
boolean
duplicate the border pixels of the inserted images to avoid seams when rendering with bi-linear filtering on.
duplicateBorder
,
boolean
strip whitespace in x axis
stripWhitespaceX
,
boolean
strip whitespace in y axis
stripWhitespaceY
,
PixmapPacker.PackStrategy packStrategy)

Creates a new PixmapPacker which will insert all supplied pixmaps into one or more pageWidth by pageHeight pixmaps using the specified strategy.

Method Summary

Modifier and TypeMethod and Description
public synchronized void
dispose()

Implements com.badlogic.gdx.utils.Disposable.dispose.

Disposes any pixmap pages which don't have a texture.

public synchronized TextureAtlas
generateTextureAtlas(Texture.TextureFilter minFilter, Texture.TextureFilter magFilter, boolean useMipMaps)

Generates a new TextureAtlas from the pixmaps inserted so far.

public boolean
public boolean
public int
private int[]
getPads(Pixmap raster, int[] splits)

public synchronized PixmapPacker.Page
getPage(String
the name of the image
name
)

Returns: the page the image is stored in or null

public Pixmap.Format
public int
public synchronized int

Returns:

the index of the page the image is stored in or -1
getPageIndex
(String
the name of the image
name
)

Returns the index of the page containing the given packed rectangle.

public Array<PixmapPacker.Page>
getPages()

Returns: the Page instances created so far.

public int
public synchronized PixmapPacker.PixmapPackerRectangle
getRect(String
the name of the image
name
)

Returns: the rectangle for the image in the page it's stored in or null

private int
getSplitPoint(Pixmap raster, int startX, int startY, boolean startPoint, boolean xAxis)

private int[]
getSplits(Pixmap raster)

public Color
public synchronized PixmapPacker.PixmapPackerRectangle
pack(Pixmap image)

Inserts the pixmap without a name.

public synchronized PixmapPacker.PixmapPackerRectangle

Returns:

Rectangle describing the area the pixmap was rendered to.
pack
(String
If null, the image cannot be looked up by name.
name
,
Pixmap image)

Inserts the pixmap.

public void
setDuplicateBorder(boolean duplicateBorder)

public void
setPackToTexture(boolean packToTexture)

If true, when a pixmap is packed to a page that has a texture, the portion of the texture where the pixmap was packed is updated using glTexSubImage2D.

public void
setPadding(int padding)

public void
public void
setPageHeight(int pageHeight)

public void
setPageWidth(int pageWidth)

public void
setTransparentColor(Color color)

Sets the default color of the whole PixmapPacker.Page when a new one created.

public void
sort(Array<Pixmap> images)

Sorts the images to the optimzal order they should be packed.

public synchronized void
updatePageTextures(Texture.TextureFilter minFilter, Texture.TextureFilter magFilter, boolean useMipMaps)

Calls updateTexture for each page.

public synchronized void
updateTextureAtlas(TextureAtlas atlas, Texture.TextureFilter minFilter, Texture.TextureFilter magFilter, boolean useMipMaps)

Updates the TextureAtlas, adding any new Pixmap instances packed since the last call to this method.

public synchronized void
updateTextureAtlas(TextureAtlas atlas, Texture.TextureFilter minFilter, Texture.TextureFilter magFilter, boolean useMipMaps, boolean useIndexes)

Updates the TextureAtlas, adding any new Pixmap instances packed since the last call to this method.

public synchronized void
updateTextureRegions(Array<TextureRegion> regions, Texture.TextureFilter minFilter, Texture.TextureFilter magFilter, boolean useMipMaps)

Calls updateTexture for each page and adds a region to the specified array for each page texture.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait