I've tried asking this question on LibGDX forums with no luck.
Basically, I've created my own packer that takes multiple spritesheets like these and packs them into something like this. As a result, I can load these packed files as arrays of TextureRegions with their X and Y offset, so that they can be drawn as if they actually had all these unnecessary transparent pixels.
Anyway, here's what I do:
- Load chosen images as pixmaps;
- Iterate over their "tiles" (for example, 128x128px parts of the image);
- Find offsets for each tile (offsets being amount of rows/columns with only transparent pixels);
- Draw non-transparent part of the tile into a new, small pixmap (continuing the example, 128-offsetLeft-offsetRight, 128-offsetTop-offsetBottom, format is the same as the loaded image);
- Save each pixmap with some drawing data (offsets) in a container.
- Repeat for all tiles and images.
- Find the most efficient way to pack the tiles from containers into a new, bigger pixmap (again, the same format).
- Save custom description file.
- Save pixmap as PNG.
Pixmap-related code snippets:
Image loading:
final Pixmap image = new Pixmap(imageData.getFileHandle());
Drawing image into smaller pieces:
final Pixmap tile =
new Pixmap(imageData.getTileWidth() - offsetLeft - offsetRight, imageData.getTileHeight()
- offsetTop - offsetBottom, image.getFormat());
tile.drawPixmap(image, -columnIndex * imageData.getTileWidth() - offsetLeft,
-rowIndex * imageData.getTileHeight() - offsetTop);
Creating new Pixmap for the packed image:
Pixmap packedImage =
new Pixmap(packedImageWidth, packedImageHeight, image.getFormat());
Drawing pieces into pixmap:
packedImage.drawPixmap(frame.getPixmap(), frame.getOriginX(), frame.getOriginY());
Saving packed image:
final PixmapIO.PNG png = new PixmapIO.PNG();
png.setCompression(Deflater.NO_COMPRESSION);
png.setFlipY(false);
try {
png.write(chosenDirectory.child(packedFileName + FILE_FORMAT), packedImage);
} catch (final IOException exception) {
throw new RuntimeException(exception);
}
As a result, colors are somewhat distorted:
(Left: after packing, right: before packing, loaded and rendered as texture.)
Could any step of the packing I do distort the image or is it the saving part? Do I have to look for another solution (pure Java image processing?) or is there a way to preserve colors using LibGDX API?