Page 1 of 1

relative pixels in -fx operator (Game of Life)

Posted: 2006-10-26T18:32:02-07:00
by RetroJ
There seems to be a bug with relative pixels, specified by p[n,n] in the -fx operator. To see it, you can do the following steps, which I have written for bash command shell. This is actually an implementation of John Conway's Game of Life, so it's kind of interesting.

My imagemagick version is 6.3.0 and my OS is Debian.

Grab this image:
http://jjfoerch.com/bitbucket/gliders.png

The following code works as expected using p{n,n} syntax. I added in a -sample 400% to make the output nice and big.

results: http://jjfoerch.com/bitbucket/gliders_ok.png

Code: Select all


neighbors='(p{i-1,j-1} + p{i,j-1} + p{i+1,j-1} + p{i-1,j} + p{i+1,j} + p{i-1,j+1} + p{i,j+1} + p{i+1,j+1})'

rules="(($neighbors<5) | ($neighbors>6) | ($neighbors==6 & u==1)) ? 1.0 : 0.0"

convert gliders.png -channel R -fx "$rules" -channel BG -fx R -sample 400% gliders_ok.png


But if I change the p{n,n} codes to the equivalent p[n,n] codes, the resulting image does not come out as expected.

results: http://jjfoerch.com/bitbucket/gliders_bad.png

Code: Select all


neighbors='(p[-1,-1] + p[0,-1] + p[1,-1] + p[-1,0] + p[1,0] + p[-1,1] + p[0,1] + p[1,1])'

rules="(($neighbors<5) | ($neighbors>6) | ($neighbors==6 & u==1)) ? 1.0 : 0.0"

convert gliders.png -channel R -fx "$rules" -channel BG -fx R -sample 400% gliders_bad.png


Posted: 2006-10-26T21:18:01-07:00
by magick
We checked the parser code for relative pixels and its looks ok. If you can prove there is a bug we'll take a closer look. Try a small image of just a few pixels and design a short Fx expression that produces incorrect results and of course post your analysis here. If we can reproduce the problem, we will have a fix for you within a day or two.

The latest version of ImageMagick permits statements within an Fx expression and debugging. Add a debug() method to debug your expression.

Posted: 2006-10-27T08:56:03-07:00
by RetroJ
Ok, here is a more thorough look at the bug I'm seeing.

http://jjfoerch.com/bitbucket/relative- ... xperiment/

thanks!

Posted: 2006-10-27T09:31:23-07:00
by magick
Now thats the sort of problem presentation we can work with. Somehow unary subtraction recently losts its -1.0 multiplier. Instead its 1.0. We will have patch in the next few days. You can fix this yourself in the magick/fx.c source module around line 1727.

Posted: 2006-10-27T10:47:26-07:00
by RetroJ
Thank you! I made the correction here, and now it works as expected. Thanks again!

Posted: 2006-11-22T21:51:35-07:00
by anthony
With the latest IM you can spedd up things as follows.

Code: Select all

  neighbors='(p{i-1,j-1} + p{i,j-1} + p{i+1,j-1} + p{i-1,j} \
            + p{i+1,j} + p{i-1,j+1} + p{i,j+1} + p{i+1,j+1})'

  rules="nn=$neighbors; ((nn<5)|(nn>6)|(nn==6 & u==1)) ? 1.0 : 0.0"

  convert gliders.png -channel R -fx "$rules" -separate \
          -sample 400% gliders_ok.png
The variable assignment sppeds up the rule interpretation, while -separate removed the need for the second -fx expression.

I am sure however that a version using a tight "-blur 1x1 -threshold", could be developed that is a lot faster. See the 'edge expansion' forum http://redux.imagemagick.org/discussion ... 3946#23946

Also as a final point you can control what is thought of as existing beyond the image boundaries by using a "-virtual-pixel background -background white" setting to make it a 'kill zone' that is typically used. That will work for both -fx and -blur methods.

You wouldn't happen to have the 'glider gun' seed image would you?
I could probably find it though.

Posted: 2006-11-22T23:23:42-07:00
by el_supremo
anthony wrote: You wouldn't happen to have the 'glider gun' seed image would you?


http://members.shaw.ca/el_supremo/life_glider_gun.png

Pete

Posted: 2006-11-23T00:44:25-07:00
by anthony
That's a glider gun, but not a glider gun seed...

Okay... I search my old archives and while I have a big 6 pert collecttion about space ships, It does not contain the glider gun 'seed'. (A set of 13 gliders that clash to form a glider gun).

Do sorry I have not been able to find the pattern. Though I found lots of other patterns.

Posted: 2006-11-24T10:46:38-07:00
by RetroJ
anthony wrote: With the latest IM you can spedd up things as follows.

Code: Select all

  neighbors='(p{i-1,j-1} + p{i,j-1} + p{i+1,j-1} + p{i-1,j} \
            + p{i+1,j} + p{i-1,j+1} + p{i,j+1} + p{i+1,j+1})'

  rules="nn=$neighbors; ((nn<5)|(nn>6)|(nn==6 & u==1)) ? 1.0 : 0.0"

  convert gliders.png -channel R -fx "$rules" -separate \
          -sample 400% gliders_ok.png
The variable assignment sppeds up the rule interpretation, while -separate removed the need for the second -fx expression.

I am sure however that a version using a tight "-blur 1x1 -threshold", could be developed that is a lot faster. See the 'edge expansion' forum http://redux.imagemagick.org/discussion ... 3946#23946


Variables in -fx, vary nice! I was hoping that feature would be added eventually. The possibility of implementing the game with a -blur operation is very intriguing. I'll look into that some rainy day.


That image didn't work for me. (I added a nice wide white border to it first) Are you sure it is correct?
anthony wrote: Okay... I search my old archives and while I have a big 6 pert collecttion about space ships, It does not contain the glider gun 'seed'. (A set of 13 gliders that clash to form a glider gun).

Do sorry I have not been able to find the pattern. Though I found lots of other patterns.


Anthony, will you share your collection of patterns?

Here is my Imagemagick Game of Life script:

http://jjfoerch.com/projects/imagemagic ... ys-life.sh

I have not yet updated it with the improvements you showed (variables and -separate).

Posted: 2006-11-24T11:12:27-07:00
by el_supremo
RetroJ wrote:


That image didn't work for me. (I added a nice wide white border to it first) Are you sure it is correct?


I didn't test it but I copied it from http://en.wikipedia.org/wiki/Gun_(CA)

Pete

Posted: 2006-11-25T02:46:32-07:00
by anthony
The spaceship articals is on the web at...
http://www.tip.net.au/~dbell/
Another site I just found with lots of patterns is at...
http://www.radicaleye.com/lifepage/
That last one as a very good java implimentation of the life processor too.

Posted: 2006-11-25T17:06:02-07:00
by el_supremo
Anthony: at that second URL is a page with "makegun.lif". It has 8 gliders which make a glider gun.
http://www.radicaleye.com/lifepage/patterns/cat7.html

Pete

Posted: 2006-11-27T16:52:35-07:00
by anthony
Thanks. I looked though the patterns but didn't find this one.

Re: The Game of Life

Posted: 2010-06-27T21:37:02-07:00
by anthony
Yes this is an old thread, but one that I have kept in mind for a long time.

I have finally worked out how to use 'convolution' and 'color lookup tables' to implement the Game of Life.

The trick was to generate different 'neighbour counts' depending on if the current pixel was alive or dead.


See IM examples...
The Game of Life
http://www.imagemagick.org/Usage/convolve/#life

Re: relative pixels in -fx operator (Game of Life)

Posted: 2010-07-02T07:23:52-07:00
by RetroJ
anthony wrote:Yes this is an old thread, but one that I have kept in mind for a long time.

I have finally worked out how to use 'convolution' and 'color lookup tables' to implement the Game of Life.

The trick was to generate different 'neighbour counts' depending on if the current pixel was alive or dead.


See IM examples...
The Game of Life
http://www.imagemagick.org/Usage/convolve/#life
incredible. bravo.