How to merge multi small pngs into a single one with smallest size?
5 replies to this topic
Members - Reputation: 122
Posted 22 December 2008 - 09:47 PM
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!
Crossbones+ - Reputation: 6866
Posted 23 December 2008 - 08:50 PM
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.
[ 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 ]
Members - Reputation: 642
Posted 24 December 2008 - 07:59 AM
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