Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Bitwise operations between two bitmaps?

This thread is locked; no one can reply to it. rss feed Print
Bitwise operations between two bitmaps?
nshade
Member #4,372
February 2004

Given two bitmaps how would you create a bitwise operation between the two without going pixel-by-pixel?

It appears you use al_set_blender() but I don't know how to derive the functions I need

Form my old legacy psudocode on a per-pixel basis I used the following operators:
t = the resulting texture
b = bitmap being applied

B_OR : t = t | b; //or
B_AND : t = t & b; //and
B_XOR : t = t ^ b; //xor
B_COPY : t = b; //normal blitting
B_NOT : t = ~b; //not
B_CUT : if(b != 0) t = b; //copy data only if the destination is black
B_REV : (Same as B_NOT due to turecolor palette)

I'm not sure how the ALLEGRO_ADD, ALLEGRO_ZERO, ALLEGRO_ONE stuff turns into bitwise math, unless I'm not seeing the mathematics properly or it's a type of math I don't understand.

From the documentation, it appears that I can make a B_COPY by
al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_ZERO)

Thinking this through, this means that of the bit on the source will be written over the destination which is treated as a "mask" of zero? Kinda? How does this work?

Elias
Member #358
May 2000

You can't do bitwise with blending. So for B_OR, B_AND, B_XOR, B_NOT I would say use a shader with two input textures. This shows a shader with two textures, but in your case simply use ~/&/^ on the rgb values. Here is the full example.

B_COPY should be the normal behavior (for r/g/b at least, not necessarily for alpha).

B_CUT won't work directly but Allegro supports a depth buffer which could be used for a very similar effect, like for example here.

Also, if this is something you can pre-process once and is not needed in real time, just use al_lock_bitmap and modify the pixels directly.

--
"Either help out or stop whining" - Evert

Go to: