Mask -> alpha channel

Is there a nice, clever way to copy a mask image into an alpha
channel?

Pursuant the advice given here, I’m in the process of learning to use
alpha channels with SDL. My situation is that I have a bunch of
sprites stored as tiles in a single bitmap. Each sprite is defined by
two separate tiles, the second tile being a black-and-white "mask"
image. (Black pixels are transparent, white pixels are opaque. Also,
the transparent pixels are white in the first tile.)

Right now, in order to merge the two into a single surface with an
alpha channel, I create a 32-bit RGBA surface with hardcoded Rmask et
al values and blit the image tile to the surface. I then lock the
surfaces and examine the pixels of the mask tile directly, and
directly munge the necessary pixels in my destination surface to be
transparent. Finally, I use SDL_DisplayFormatAlpha() to transform my
32-bit surface into the native format (or at least a faster one).

It seems like there ought to be a way to merge the image with the mask
without having to directly access pixels. Is there? I read through the
SDL_SetAlpha manpage and tried a few things, but none of them worked.

(Please, no noise about how I shouldn’t do this with separate mask
images, or I should switch to PNGs. My program is supporting a legacy
image file format.)

b

Is there a nice, clever way to copy a mask image into an alpha
channel?

Nope, not really. You have to hand-roll code to do it, as you’ve seen.
You can see an example of this in the icon code, which accepts a surface
and a mask and generates a colorkeyed surface from them.

See ya,
-Sam Lantinga, Software Engineer, Blizzard Entertainment

Sam Lantinga wrote:

You can see an example of this in the icon code, which accepts a surface
and a mask and generates a colorkeyed surface from them.

I’ve been in dire need of an alpha channel example. Could any of you
enlighten me where is this ‘icon’ code is?

-Williams

Sam Lantinga wrote:

Nope, not really. You have to hand-roll code to do it, as you’ve seen.
You can see an example of this in the icon code, which accepts a surface
and a mask and generates a colorkeyed surface from them.

See ya,
-Sam Lantinga, Software Engineer, Blizzard Entertainment

where might this icon code be? I’ve been trying to figure out alpha
masking /per-pixel alpha blending/ whatever you want to call it, and I
can’t find an example, nor is the code and documentation very obvious on
the fact…

-poor baffled college student

Sam Lantinga wrote:

Nope, not really. You have to hand-roll code to do it, as you’ve seen.
You can see an example of this in the icon code, which accepts a surface
and a mask and generates a colorkeyed surface from them.

See ya,
-Sam Lantinga, Software Engineer, Blizzard Entertainment

where might this icon code be? I’ve been trying to figure out alpha
masking /per-pixel alpha blending/ whatever you want to call it, and I
can’t find an example, nor is the code and documentation very obvious on
the fact…

SDL source code: src/video/SDL_video.c
Look for CreateMaskFromColorKeyOrAlpha()
Actually, this does the reverse - alpha from mask, but you get the idea.

See ya,
-Sam Lantinga, Software Engineer, Blizzard Entertainment