relative pixels in -fx operator (Game of Life)

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
RetroJ
Posts: 108
Joined: 2006-10-25T08:12:50-07:00

relative pixels in -fx operator (Game of Life)

Post 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

User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Post 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.
RetroJ
Posts: 108
Joined: 2006-10-25T08:12:50-07:00

Post by RetroJ »

Ok, here is a more thorough look at the bug I'm seeing.

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

thanks!
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Post 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.
RetroJ
Posts: 108
Joined: 2006-10-25T08:12:50-07:00

Post by RetroJ »

Thank you! I made the correction here, and now it works as expected. Thanks again!
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Post 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
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
RetroJ
Posts: 108
Joined: 2006-10-25T08:12:50-07:00

Post 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).
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Post 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
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Post 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
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Post by anthony »

Thanks. I looked though the patterns but didn't find this one.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: The Game of Life

Post 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
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
RetroJ
Posts: 108
Joined: 2006-10-25T08:12:50-07:00

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

Post 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.
Post Reply