Clipping regions and transparent bitmaps

The attached file contains an attempt to create a circular clipping region using a bitmap generated within wxPython. The
problem I’m having is that I can’t force subsequent drawing commands to ignore the transparent pixels in this bitmap.
I have acheived a circular clipping region using a mask, but the result is a little rough around the edges; ideally, I want the
draw commands to respect the degree of transparency. I know there is an issue with the type of Device Context used, but since I’m using
a graphics context in conjunction with a bitmap that has alpha channel info, I thought I’d be okay.

The order of operation proceeds as follows:

  1. create an instance of wx.EmptyBitmapRGBA()
  2. create a MemoryDC with this bitmap as the parameter
  3. make this instance of MemoryDC the target of a GraphicsContext
  4. Draw on a circle on my bitmap
  5. Retrieve the newly created bitmap with transparency info in place
  6. Use this bitmap as a clipping region.

Thanks, Paul.

wxQuestion.py (2.59 KB)

wx.RegionFromBitmap requires that the bitmap have a mask, which is all or nothing transparency, so it can't deal with variable levels of transparency like you would get with an anti-aliased drawing operation. Using a bitmap without a mask (whether it has an alpha channel or not) with wx.RegionFromBitmap will result in a region where none of the pixels are clipped.

Clipping regions work the same way. They specify that a pixel is either drawn or it isn't, there is no blending involved. So they fit well with using a mask for defining the region.

What are you wanting to use this clipping region for in your real application? Perhaps there is another way to do it.

···

On 10/19/12 8:20 AM, Paul wrote:

The attached file contains an attempt to create a circular clipping
region using a bitmap generated within wxPython. The
problem I'm having is that I can't force subsequent drawing commands to
ignore the transparent pixels in this bitmap.
I have acheived a circular clipping region using a mask, but the result
is a little rough around the edges; ideally, I want the
draw commands to respect the degree of transparency. I know there is an
issue with the type of Device Context used, but since I'm using
a graphics context in conjunction with a bitmap that has alpha channel
info, I thought I'd be okay.

The order of operation proceeds as follows:
1. create an instance of wx.EmptyBitmapRGBA()
2. create a MemoryDC with this bitmap as the parameter
3. make this instance of MemoryDC the target of a GraphicsContext
4. Draw on a circle on my bitmap
5. Retrieve the newly created bitmap with transparency info in place
6. Use this bitmap as a clipping region.

--
Robin Dunn
Software Craftsman

Thanks Robin.

The effect I’m after is very simple, and I’ve already achieved it, more or less, in a couple of other ways.
I set off down the clipping region route in an attempt to reduce the number of draw commands I was using in these
other methods; I often find it hard to follow such commands when I’m reading through old projects so
I was hoping to reduce them. Not to worry, I’ll just have to document a bit more thoroughly.

Paul.

···

On Friday, October 19, 2012 6:31:01 PM UTC+1, Robin Dunn wrote:

On 10/19/12 8:20 AM, Paul wrote:

The attached file contains an attempt to create a circular clipping

region using a bitmap generated within wxPython. The

problem I’m having is that I can’t force subsequent drawing commands to

ignore the transparent pixels in this bitmap.

I have acheived a circular clipping region using a mask, but the result

is a little rough around the edges; ideally, I want the

draw commands to respect the degree of transparency. I know there is an

issue with the type of Device Context used, but since I’m using

a graphics context in conjunction with a bitmap that has alpha channel

info, I thought I’d be okay.

The order of operation proceeds as follows:

  1. create an instance of wx.EmptyBitmapRGBA()
  1. create a MemoryDC with this bitmap as the parameter
  1. make this instance of MemoryDC the target of a GraphicsContext
  1. Draw on a circle on my bitmap
  1. Retrieve the newly created bitmap with transparency info in place
  1. Use this bitmap as a clipping region.

wx.RegionFromBitmap requires that the bitmap have a mask, which is all
or nothing transparency, so it can’t deal with variable levels of
transparency like you would get with an anti-aliased drawing operation.
Using a bitmap without a mask (whether it has an alpha channel or not)
with wx.RegionFromBitmap will result in a region where none of the
pixels are clipped.

Clipping regions work the same way. They specify that a pixel is either
drawn or it isn’t, there is no blending involved. So they fit well with
using a mask for defining the region.

What are you wanting to use this clipping region for in your real
application? Perhaps there is another way to do it.


Robin Dunn

Software Craftsman

http://wxPython.org