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!
| Modifier and Type | Class and Description |
|---|---|
| pack-priv static class |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv boolean | |
| pack-priv BufferedImage | |
| pack-priv int | |
| pack-priv Map | |
| pack-priv ImagePacker. |
| Access | Constructor 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 |
| Modifier and Type | Method and Description |
|---|---|
| private static BufferedImage | |
| public BufferedImage | |
| public Map | |
| private ImagePacker. | |
| public void | insertImage(String
the name of the image name, BufferedImage the image image)Inserts the given image. |
| public static void |
| duplicateBorder | back to summary |
|---|---|
| pack-priv boolean duplicateBorder | |
| image | back to summary |
|---|---|
| pack-priv BufferedImage image | |
| padding | back to summary |
|---|---|
| pack-priv int padding | |
| rects | back to summary |
|---|---|
| pack-priv Map<String, Rectangle> rects | |
| root | back to summary |
|---|---|
| pack-priv ImagePacker. | |
| ImagePacker | back to summary |
|---|---|
| public ImagePacker(int width, int height, int padding, boolean duplicateBorder)
Creates a new ImagePacker which will insert all supplied images into a
| |
| createImage | back to summary |
|---|---|
| private static BufferedImage createImage(int width, int height, Color color) | |
| getImage | back to summary |
|---|---|
public BufferedImage getImage()
| |
| getRects | back to summary |
|---|---|
| public Map | |
| insert | back to summary |
|---|---|
| private ImagePacker. | |
| insertImage | back 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
| |
| main | back to summary |
|---|---|
| public static void main(String[] argv) throws IOException | |
| Modifier and Type | Field and Description |
|---|---|
| public String | |
| public ImagePacker. | |
| public Rectangle | |
| public ImagePacker. |
| Access | Constructor and Description |
|---|---|
| public | Node(int x, int y, int width, int height, ImagePacker.
|
| public |
| leaveName | back to summary |
|---|---|
| public String leaveName | |
| leftChild | back to summary |
|---|---|
| public ImagePacker. | |
| rect | back to summary |
|---|---|
| public Rectangle rect | |
| rightChild | back to summary |
|---|---|
| public ImagePacker. | |
| Node | back to summary |
|---|---|
| public Node(int x, int y, int width, int height, ImagePacker. | |
| Node | back to summary |
|---|---|
| public Node() | |