Sign in to follow this  
Rubbio

How to merge multi small pngs into a single one with smallest size?

Recommended Posts

Rubbio    122
Hi all, I am writing a 2d animation editor, and the artist uses many small png as modules of a character. so i want to write a programe to auto merge those small pngs into a large single png. i googled a lot, but still can not find any good way to ensure the final single png's size(widthXheight) is smallest, is anyone can help me? Thx a lot!

Share this post


Link to post
Share on other sites
ToohrVyk    1595
Rectangle packing is an NP-complete problem. Enjoy.

However, why merge things in the same rectagle? Why not just zip your file folder and use a ZIP library to access individual images?

Share this post


Link to post
Share on other sites
sb01234    122
Quote:
Original post by ToohrVyk
However, why merge things in the same rectagle? Why not just zip your file folder and use a ZIP library to access individual images?

Texture atlas, perhaps?

Share this post


Link to post
Share on other sites
Cornstalks    7030
I found this to be very useful when I had to pack a bunch of character textures into a large texture for using fonts in my game. It's pretty quick, easy, straightforward, and actually does a great job I've found. While it doesn't guarantee that it actually generates the smallest texture, it does have great results. Of course, if you want to tackle this NP-complete problem and pack them into the smallest texture possible, then you'll want to use a different, much more complex, and expensive algorithm.

Share this post


Link to post
Share on other sites
Spodi    642
Quote:
Original post by MikeTacular
I found this to be very useful


I always recommend that site for those looking into making a texture atlas. It is, by far, the fastest way to get it done that I have seen. I was able to pack a couple thousand textures into a 4096x4096 atlas in around 100ms I believe it was.

It definitely won't give you the best fit, but it will give you great scalability and performance. If you want an example of bad scalability, the XNA website has an example that just scans every pixel from the start until a fit is found. Works fine up until you want to pack more than about 5 textures.

If the size of the atlas really is that important, I'd suggest you build the atlas first by guessing the size. You know the width will need to be at least >= the largest item's width, and >= the sum of all width. Same goes for height. The area will also need to be >= the sum of the area of all items packed. Using that, you can take the first two power-of-twos that fit those conditions.

Since it is not unlikely that the first atlas will fail, even if it fits those conditions, you just keep trying by moving to the next atlas that fits those conditions, ordering them by the atlas's total area (since that is what matters to you the most).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this