Combining image mask and alpha channel

Hi,

I have an application where I would like to combine the alpha channel from an image file with a polygon mask that the software creates. I can use either to create a transparency mask without difficulty but I can't seem to find a way to combine the two, other than by going pixel by pixel through the image. Since performance is important that's not really an option for this application (unless I go to C code).

When I say I would like to combine the two I mean I would like a logical OR of the two transparency masks. That is, the final image would be transparent where either the alpha channel or polygon were transparent.

I was hoping that I could use ConvertAlphaToMask() and then use SetMaskFromImage() to 'OR' in the polygon bitmap's pixels. But I see from the source code that SetMaskFromImage() picks a new mask colour rather than using the existing mask colour, so that will just blow the first mask away.

Any ideas??

Thanks!
Phil

Phil Rittenhouse wrote:

Hi,

I have an application where I would like to combine the alpha channel from an image file with a polygon mask that the software creates. I can use either to create a transparency mask without difficulty but I can't seem to find a way to combine the two, other than by going pixel by pixel through the image. Since performance is important that's not really an option for this application (unless I go to C code).

When I say I would like to combine the two I mean I would like a logical OR of the two transparency masks. That is, the final image would be transparent where either the alpha channel or polygon were transparent.

I was hoping that I could use ConvertAlphaToMask() and then use SetMaskFromImage() to 'OR' in the polygon bitmap's pixels. But I see from the source code that SetMaskFromImage() picks a new mask colour rather than using the existing mask colour, so that will just blow the first mask away.

Any ideas??

If you're not on Mac then you can do it by drawing the polygon and blitting the image to a memory DC with the right logical functions set. The result will be a bitmap that you can use for creating a new mask. Unfortunately restrictions in the new drawing APIs on Mac don't allow the wxDC logical functions to be implemented (other than wx.COPY).

A similar approach that might work okay on all platforms is to use wx.Regions. You can create a region from a bitmap with wx.RegionFromBitmap or wx.RegionFromBitmapColour which will define the region either from the bitmap's mask, or from the areas with the specified color in the bitmap, then you can combine the two regions with the various operators that the class provides, and then either use the region for clipping when you draw the bitmap to a DC, or generate a bitmap from the region that can be used as a mask.

ยทยทยท

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!