Searching for a way to do a real Bitwise XOR

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
RedJiggly
Posts: 4
Joined: 2011-06-13T13:32:43-07:00
Authentication code: 8675308

Searching for a way to do a real Bitwise XOR

Post by RedJiggly »

I want to do a Bitwise XOR on two images, so that

picA.png ^ picB.png = picC, in such a way that:
picA.png ^ picC.png = picB.png, and
picB.png ^picC.png = picA.png

(If you didn't know the ^ is the symbol for bitwise XOR in most programming languages)

I read about the -compose Difference and the -compose Exclusion commands, but those do not give the correct results:
convert picA.png picB.png -compose exclusion -composite picC.png

convert picC.png picB.png -compose exclusion -composite shouldEqualPicA.png
this does not work, shouldEqualPicA is not equal to picA. I'm left with some garbage. The same for difference.



Is there some other command I'm failing to see? Or is there another way to archieve this with ImageMagick?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Searching for a way to do a real Bitwise XOR

Post by fmw42 »

best I can suggest is to see -fx bitwise or http://www.imagemagick.org/script/fx.php

I don't know if there is bitwise xor as ^ is used for power.

You might try

convert imageA imageB -evaluate-sequence xor imageC

since -evaluate lists xor as a option see http://www.imagemagick.org/script/comma ... p#evaluate
and
see http://www.imagemagick.org/script/comma ... e-sequence
RedJiggly
Posts: 4
Joined: 2011-06-13T13:32:43-07:00
Authentication code: 8675308

Re: Searching for a way to do a real Bitwise XOR

Post by RedJiggly »

Thank you very much for your reply. :)

I have tried around with it, but it still does not work. ImageMagick (I have downloaded the newest 6.7.4 version, on windows) does not recognize "-evaluate-list" .
I also looked at the -fx command, since it is possible to construct an XOR from ((A & ~B)|(~A & B)) where & is a bitwise and, | is a bitwise or and ~ is not. However, it does not like my tildes(~). I keep on getting:
convert: missing expression `' @ fx.c/FxEvaluateSubexpression/2733.
Am I doing something wrong? Or is there a bug in ImageMagick? Or is this because I use IM on windows?(note: I already read trough the Usage under Windows section, and there was nothing that explained the broken ~)

What's going on?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Searching for a way to do a real Bitwise XOR

Post by fmw42 »

-evaluate-list
It is -evaluate-sequence. See http://www.imagemagick.org/script/comma ... e-sequence

I don't know much about bit-wise operations, but I tried this on IM 6.7.4.0 Q16 Mac OSX Snow Leopard and got the same error message.

convert -size 10x10 xc:white xc:black -fx "(u & ~v)|(~u & v)" tmp.png

convert: missing expression `' @ error/fx.c/FxEvaluateSubexpression/2092.


I am not sure I am doing something wrong, also.

I would suggest you report this as on the Bugs forum and also ask if they could add bitwise xor to -fx. That probably is not too hard for them to do.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Searching for a way to do a real Bitwise XOR

Post by anthony »

WARNING; the 'u' an d'v' values are normalized into a 0 to 1 floating point range. However bitwise operators expect interger values in a 0 to QuantumRange. FX makes no attempt to do this conversion for you, and provides no flag to avoid the value normalization that applied (might be a handy addition). As such you will have to de-normalize the values yourself before
applying the bitwise operators!

With care it can be done!

An example of this was done recently to merge color values into a single 5551 RGBA colormap value
as part of video processing. However while I managed to do this using FX (privatally), I ended up used faster level and evaluate methods instead of the slow FX for do the bitwise merge.
viewtopic.php?f=1&t=19752&p=77879&hilit=556#p77879
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Searching for a way to do a real Bitwise XOR

Post by fmw42 »

Anthony,

If I understand what you are saying, then neither of these work and still give the same error message:


convert -size 10x10 xc:white xc:black -fx "quantumrange*((u & ~v) | (~u & v))" tmp.png

convert -size 10x10 xc:white xc:black -fx "((quantumrange*u & ~quantumrange*v) | (~quantumrange*u & quantumrange*v))" tmp.png


convert: missing expression `' @ error/fx.c/FxEvaluateSubexpression/2092.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Searching for a way to do a real Bitwise XOR

Post by anthony »

You need to not only convert from normalized value but the result also has to go back to a normalized value.
(A flag to disable normalization would be useful for this :-) )

Also start simplier. For example mask off the lowest byte of a 16 bit color, and try it with a single pixel image...

Code: Select all

convert xc:grey  +depth txt:
# ImageMagick pixel enumeration: 1,1,65535,rgb
0,0: (48830,48830,48830)  #BEBEBEBEBEBE  grey

convert xc:grey -fx "((u*quantumrange) & 255 )/quantumrange" +depth txt:
# ImageMagick pixel enumeration: 1,1,65535,rgb
0,0: (  190,  190,  190)  #00BE00BE00BE  rgb(0.289921%,0.289921%,0.289921%)
See it works! the lowest 8 bits were masked out (zero the upper byte of the values)

Sorry exclusive-OR was replace with an exponential operation.

You can also use variables to simpify the expression.

Code: Select all

 convert xc:grey -fx "uu=u*quantumrange; rr=uu&255; rr/quantumrange" +depth txt:
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply