APIdia Launch Partner
Top Description Inners Fields Constructors Methods
com.badlogic.gdx.tools.imagepacker

public Class ImagePacker

extends Object
Class Inheritance
Imports
java.awt.Color, .Graphics2D, .Rectangle, java.awt.image.BufferedImage, java.io.File, .IOException, java.util.Arrays, .Comparator, .HashMap, .Map, .Random, javax.imageio.ImageIO

A simple image packer class based on the nice algorithm by blackpawn.

See http://www.blackpawn.com/texts/lightmaps/default.html for details.

Usage: instantiate an ImagePacker instance, load and optionally sort the images you want to add by size (e.g. area) then insert each image via a call to insertImage(String, BufferedImage). When you are done with inserting images you can call getImage() for the BufferedImage that holds the packed images. Additionally you can get a Map<String, Rectangle> where the keys the names you specified when inserting and the values are the rectangles within the packed image where that specific image is located. All things are given in pixels.

See the main(String[]) method for an example that will generate 100 random images, pack them and then output the packed image as a png along with a json file holding the image descriptors.

In some cases it is beneficial to add padding and to duplicate the border pixels of an inserted image so that there is no bleeding of neighbouring pixels when using the packed image as a texture. You can specify the padding as well as whether to duplicate the border pixels in the constructor.

Happy packing!

Author
mzechner

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class

Field Summary

Modifier and TypeField and Description
pack-priv boolean
pack-priv BufferedImage
pack-priv int
pack-priv Map<String, Rectangle>
pack-priv ImagePacker.Node

Constructor Summary

AccessConstructor and Description
public
ImagePacker(int
the width of the output image
width
,
int
the height of the output image
height
,
int
the number of padding pixels
padding
,
boolean
whether to duplicate the border
duplicateBorder
)

Creates a new ImagePacker which will insert all supplied images into a width by height image.

Method Summary

Modifier and TypeMethod and Description
private static BufferedImage
createImage(int width, int height, Color color)

public BufferedImage
getImage()

Returns: the output image

public Map<String, Rectangle>
getRects()

Returns: the rectangle in the output image of each inserted image

private ImagePacker.Node
public void
insertImage(String
the name of the image
name
,
BufferedImage
the image
image
)

Inserts the given image.

public static void
main(String[] argv)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

duplicateBorderback to summary
pack-priv boolean duplicateBorder
imageback to summary
pack-priv BufferedImage image
paddingback to summary
pack-priv int padding
rectsback to summary
pack-priv Map<String, Rectangle> rects
rootback to summary
pack-priv ImagePacker.Node root

Constructor Detail

ImagePackerback to summary
public ImagePacker(int width, int height, int padding, boolean duplicateBorder)

Creates a new ImagePacker which will insert all supplied images into a width by height image. padding specifies the minimum number of pixels to insert between images. border will duplicate the border pixels of the inserted images to avoid seams when rendering with bi-linear filtering on.

Parameters
width:int

the width of the output image

height:int

the height of the output image

padding:int

the number of padding pixels

duplicateBorder:boolean

whether to duplicate the border

Method Detail

createImageback to summary
private static BufferedImage createImage(int width, int height, Color color)
getImageback to summary
public BufferedImage getImage()
Returns:BufferedImage

the output image

getRectsback to summary
public Map<String, Rectangle> getRects()
Returns:Map<String, Rectangle>

the rectangle in the output image of each inserted image

insertback to summary
private ImagePacker.Node insert(ImagePacker.Node node, Rectangle rect)
insertImageback to summary
public void insertImage(String name, BufferedImage image)

Inserts the given image. You can later on retrieve the images position in the output image via the supplied name and the method getRects().

Parameters
name:String

the name of the image

image:BufferedImage

the image

Exceptions
RuntimeException:
in case the image did not fit or you specified a duplicate name
mainback to summary
public static void main(String[] argv) throws IOException
com.badlogic.gdx.tools.imagepacker back to summary

pack-priv final Class ImagePacker.Node

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
public String
public ImagePacker.Node
public Rectangle
public ImagePacker.Node

Constructor Summary

AccessConstructor and Description
public
Node(int x, int y, int width, int height, ImagePacker.Node leftChild, ImagePacker.Node rightChild, String leaveName)

public
Node()

Method Summary

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

leaveNameback to summary
public String leaveName
leftChildback to summary
public ImagePacker.Node leftChild
rectback to summary
public Rectangle rect
rightChildback to summary
public ImagePacker.Node rightChild

Constructor Detail

Nodeback to summary
public Node(int x, int y, int width, int height, ImagePacker.Node leftChild, ImagePacker.Node rightChild, String leaveName)
Nodeback to summary
public Node()