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

Started by
4 comments, last by Spodi 15 years, 3 months ago
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!
Advertisement
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?
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?
http://www.better-than-real.net/sb/
Thansk for your replies!!

yes, it's texture problem~~

bind texture is a heavy job, so combine textures may helpful a lot~
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.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
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).
NetGore - Open source multiplayer RPG engine

This topic is closed to new replies.

Advertisement