views:

745

answers:

1

Is there a way in wxWidget to do alpha blending operations such as multiplying the alpha of a bitmap versus the RGB of other bitmap to generate new images (such as rendering a photo as an anti-aliased circular shape).

A: 

Though I haven't done alpha blending myself, I believe the wxGraphicContext is what you are after.

http://docs.wxwidgets.org/stable/wx_wxgraphicscontext.html

I've got flicker free drawing into the graphics context (on Win32) using the following in my paint event handler:

void OnPaint( wxPaintEvent& e )
{
    wxBufferedPaintDC dc( this );        
    wxGraphicsContext* gdc = wxGraphicsContext::Create( dc );
    // you drawing code here
}

EDIT: this email trail might offer some more insight:

http://www.nabble.com/Is-alpha-blending-implemented-when-using-DC's--td17183159.html
Daniel Paull
Hum, it's not exactly that. Let's say I have a square image. The blit method in the DC or the equivalent in the wxGraphicsContext only allows you to render the whole bitmap or rectangular portions. How would I be able to render just a circular area of my original image.Usually I would render a circle to the destination buffer making sure it was cleared out to 255 alpha beforehand. Then I would just alpha blend the image against the buffer multiplying alpha values against the individual RGB components. I guess I'll do this manually... I'll post the implementation.
ruibm
Have you confirmed that the DrawBitmap method of the wxGraphicsContext does not pay attention to the Alpha channel when drawing? As a complete aside, you could also use OpenGL to draw your images as wxWidgets has an OpenGL canvas.
Daniel Paull
This is the code I'd like to work:wxBitmap my_image = LoadSomePngWithNoAlpha();wxBitmap buffer;wxMemoryDc dc = new wxMemoryDc(buffer);dc.SetBrush(wxCoulor(0, 0, 0, 0));dc.Clear();dc.BlitBitmapAsCircle(bitmap, wxPoint(5,5), 42.0);In order to do this final step you would usually draw a normal circle with alpha set to 255 and then if I blend in the wxBitmap which is rectangular to the buffer, only the alpha == 255 bit will be copied so I would end up with a circular cropped section of the Png image.
ruibm
The link I posted shows you how to use a wxMemoryDc with the wxGraphicsContext. When using OpenGL (for example) you would draw the transparent circle into the alpha channel of the image before drawing the image. I expect this is what you need to do here - though there may not be good API support to do this sort of stuff.
Daniel Paull
Hum, I'll try that approach later on. I'll get back to this thread with results. Thanks!
ruibm
I will probably be playing with the wxGraphicsContext next week, so I'll have a look at alpha blending too if I get a chance.
Daniel Paull