Page 1 of 1

Several questions (Colorkey, atlases and power-of-two).

Posted: 2008-09-02T05:06:46-07:00
by jaguard
1. Can I convert the so-called colorkey into alpha? I.e. I have a gif sprite,
which is filled with some mask, i.e. 0xFF0000. It would take this mask
away and replace it with alpha==0.

2. Can IM actually create irregular tiles/atlases? I.e. take a series of pictures
with different sizes, then montage them into one irregular image. Ideally,
it would find an optimal placement so that least space is used.

3. How do I convert images to make power-of-two textures? I.e. I supply
image.png with 54x22 resolution, and it will automatically expand (not resize!)
it and save with 64x32, filling extra space with zero alpha.
Or, as a nice option, square power-of-two texture, i.e. 64x64, 128x128, etc.

Re: Several questions (Colorkey, atlases and power-of-two).

Posted: 2008-09-02T22:04:15-07:00
by anthony
jaguard wrote:1. Can I convert the so-called colorkey into alpha? I.e. I have a gif sprite,
which is filled with some mask, i.e. 0xFF0000. It would take this mask
away and replace it with alpha==0.
-transparent {color}

see http://www.imagemagick.org/Usage/color/#replace
2. Can IM actually create irregular tiles/atlases? I.e. take a series of pictures with different sizes, then montage them into one irregular image. Ideally, it would find an optimal placement so that least space is used.
NO. That is a NP problem known as the 'packing problem' that is impossible to do in linear time.

Think of a Jigsaw puzzles!!!! there are however very expensive and specialized programs that are designed for this. For example to help piece together the fragments of the dead sea scroll!!!!

Of course there are algorithms that can do a reasonable job of this and these are often used in specialized places such as working out the best way to cut up material (like a fabric) for least waste.
3. How do I convert images to make power-of-two textures? I.e. I supply image.png with 54x22 resolution, and it will automatically expand (not resize!) it and save with 64x32, filling extra space with zero alpha. Or, as a nice option, square power-of-two texture, i.e. 64x64, 128x128, etc.
Do you want to enlarge images, generate a tilable image, or just add space around the image?

In any case finding the next higher power of two is not a difficult problem, but IM does not do it. The wrapping API is better off doing such a specialized task.

Re: Several questions (Colorkey, atlases and power-of-two).

Posted: 2008-09-02T22:40:33-07:00
by jaguard
anthony wrote: -transparent {color}
see http://www.imagemagick.org/Usage/color/#replace
Oh, that's what I want, thanks!
I can even get the color key from the image, that's great.
anthony wrote: NO. That is a NP problem known as the 'packing problem' that is impossible to do in linear time.
Of course there are algorithms that can do a reasonable job of this and these are often used in specialized places such as working out the best way to cut up material (like a fabric) for least waste.
Well, I don't need it to be really intelligent and nice. Just a simple heuristic method would do. Allright, it can't do it, very unfortunate.
Do you want to enlarge images, generate a tilable image, or just add space around the image?
The latter please :). Spacing around to avoid troubles on some older video cards that don't support non-power-2 textures.
In any case finding the next higher power of two is not a difficult problem, but IM does not do it. The wrapping API is better off doing such a specialized task.
That's sad. Maybe there's a way to feed it an info of the required size, using some bat/cmd file(using windows here)? I.e. get image size, calculate the closest power-2 and feed it into IM? I can do the math, but I have no idea how to get image size in bat file. Could it be that IM can return image size into console?

Re: Several questions (Colorkey, atlases and power-of-two).

Posted: 2008-09-02T23:42:16-07:00
by anthony
jaguard wrote:
Do you want to enlarge images, generate a tilable image, or just add space around the image?
The latter please :). Spacing around to avoid troubles on some older video cards that don't support non-power-2 textures.
then look at IM examples, Thumbnails, Padding..
which show how to fit images into a box of specific size.
http://www.imagemagick.org/Usage/thumbnails/#pad

Re: Several questions (Colorkey, atlases and power-of-two).

Posted: 2008-09-03T00:48:29-07:00
by jaguard
anthony,
There's no problem with padding, it's about finding proper power-of-two digit. Of course, I can manually set it every time, but it's not that great as it should be :(.

Re: Several questions (Colorkey, atlases and power-of-two).

Posted: 2008-09-03T11:07:27-07:00
by fmw42
jaguard wrote:anthony,
There's no problem with padding, it's about finding proper power-of-two digit. Of course, I can manually set it every time, but it's not that great as it should be :(.

You can compute that in IM using fx escapes as follows to get the next higher power of 2 size from the image width and image height (example using the rose: image):

convert rose: -format "%[fx:2^(ceil(log(w)/log(2)))]" info:
128
convert rose: -format "%[fx:2^(ceil(log(h)/log(2)))]" info:
64

for reference the size of rose: is

convert rose: -format "%w x %h" info:
70 x 46

you can put these calculations into variables as follows:

p2w=`convert rose: -format "%[fx:2^(ceil(log(w)/log(2)))]" info:`
p2h=`convert rose: -format "%[fx:2^(ceil(log(h)/log(2)))]" info:`

or as

p2w=$(convert rose: -format "%[fx:2^(ceil(log(w)/log(2)))]" info:)
p2h=$(convert rose: -format "%[fx:2^(ceil(log(h)/log(2)))]" info:)

then use $pw2 and $p2h where you need to specify image sizes

e.g.

convert rose: -background white -gravity center -extent ${pw2}x${ph2} padded_rose.png

If you want it square, then use:
p2=`convert rose: -format "%[fx:2^(ceil(log(max(w,h))/log(2)))]" info:`
convert rose: -background white -gravity center -extent ${p2}x${p2} padded_rose.png


P.S. I have added this an example on my tidbits pages at http://www.fmwconcepts.com/imagemagick/ ... index.html