From c4caf81dbcb58863da23a4fd5625c9ecc21c9c85 Mon Sep 17 00:00:00 2001 From: Robin Dunn Date: Sun, 27 Aug 2006 00:46:22 +0000 Subject: [PATCH] Do premultiplication in wx*PixelData_Iterator::Get and Set git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@40858 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- wxPython/src/_bitmap.i | 96 ++++++++++++++++++++++++------------------ wxPython/src/_colour.i | 2 + 2 files changed, 56 insertions(+), 42 deletions(-) diff --git a/wxPython/src/_bitmap.i b/wxPython/src/_bitmap.i index 2f018e31e9..e87bc4560a 100644 --- a/wxPython/src/_bitmap.i +++ b/wxPython/src/_bitmap.i @@ -327,8 +327,10 @@ the ``type`` parameter.", ""); // automatically do it for wxMSW here. #ifdef __WXMSW__ #define wxPy_premultiply(p, a) ((p) * (a) / 256) +#define wxPy_unpremultiply(p, a) ((a) ? ((p) * 256 / (a)) : (p)) #else #define wxPy_premultiply(p, a) (p) +#define wxPy_unpremultiply(p, a) (p) #endif %} @@ -351,16 +353,16 @@ the ``type`` parameter.", ""); } wxBitmap* bmp = new wxBitmap(width, height, 32); - wxAlphaPixelData pixels(*bmp, wxPoint(0,0), wxSize(width,height)); - if (! pixels) { + wxAlphaPixelData pixData(*bmp, wxPoint(0,0), wxSize(width,height)); + if (! pixData) { // raise an exception... wxPyErr_SetString(PyExc_RuntimeError, "Failed to gain raw access to bitmap data."); return NULL; } - pixels.UseAlpha(); - wxAlphaPixelData::Iterator p(pixels); + pixData.UseAlpha(); + wxAlphaPixelData::Iterator p(pixData); for (int y=0; yRed(); } - byte _get_Green() { return self->Green(); } - byte _get_Blue() { return self->Blue(); } - - void _set_Red(byte val) { self->Red() = val; } - void _set_Green(byte val) { self->Green() = val; } - void _set_Blue(byte val) { self->Blue() = val; } - } - - %pythoncode { - Red = property(_get_Red, _set_Red) - Green = property(_get_Green, _set_Green) - Blue = property(_get_Blue, _set_Blue) - } - +// NOTE: For now I'm not wrapping the Red, Green, Blue and Alpha functions +// because I can't hide the premultiplying needed on wxMSW if only the +// individual components are wrapped. Instead I've added the Set and Get +// functions and put the puemultiplying in there. + +// %extend { +// byte _get_Red() { return self->Red(); } +// byte _get_Green() { return self->Green(); } +// byte _get_Blue() { return self->Blue(); } + +// void _set_Red(byte val) { self->Red() = val; } +// void _set_Green(byte val) { self->Green() = val; } +// void _set_Blue(byte val) { self->Blue() = val; } +// } + +// %pythoncode { +// Red = property(_get_Red, _set_Red) +// Green = property(_get_Green, _set_Green) +// Blue = property(_get_Blue, _set_Blue) +// } }; %enddef @@ -623,26 +630,31 @@ PIXELDATA(wxAlphaPixelData) } %extend wxAlphaPixelData_Iterator { - byte _get_Alpha() { return self->Alpha(); } - void _set_Alpha(byte val) { self->Alpha() = val; } +// byte _get_Alpha() { return self->Alpha(); } +// void _set_Alpha(byte val) { self->Alpha() = val; } - %pythoncode { - Alpha = property(_get_Alpha, _set_Alpha) - } +// %pythoncode { +// Alpha = property(_get_Alpha, _set_Alpha) +// } void Set(byte red, byte green, byte blue, byte alpha) { - self->Red() = red; - self->Green() = green; - self->Blue() = blue; + self->Red() = wxPy_premultiply(red, alpha); + self->Green() = wxPy_premultiply(green, alpha); + self->Blue() = wxPy_premultiply(blue, alpha); self->Alpha() = alpha; } PyObject* Get() { PyObject* rv = PyTuple_New(4); - PyTuple_SetItem(rv, 0, PyInt_FromLong(self->Red())); - PyTuple_SetItem(rv, 1, PyInt_FromLong(self->Green())); - PyTuple_SetItem(rv, 2, PyInt_FromLong(self->Blue())); - PyTuple_SetItem(rv, 3, PyInt_FromLong(self->Alpha())); + int red = self->Red(); + int green = self->Green(); + int blue = self->Blue(); + int alpha = self->Alpha(); + + PyTuple_SetItem(rv, 0, PyInt_FromLong( wxPy_unpremultiply(red, alpha) )); + PyTuple_SetItem(rv, 1, PyInt_FromLong( wxPy_unpremultiply(green, alpha) )); + PyTuple_SetItem(rv, 2, PyInt_FromLong( wxPy_unpremultiply(blue, alpha) )); + PyTuple_SetItem(rv, 3, PyInt_FromLong( alpha )); return rv; } } diff --git a/wxPython/src/_colour.i b/wxPython/src/_colour.i index 9934bfca23..61ba3726fd 100644 --- a/wxPython/src/_colour.i +++ b/wxPython/src/_colour.i @@ -161,6 +161,7 @@ is returned if the pixel is invalid (on X, unallocated).", ""); %extend { + KeepGIL(Get); DocAStr(Get, "Get() -> (r, g, b)", "Returns the RGB intensity values as a tuple.", ""); @@ -184,6 +185,7 @@ is returned if the pixel is invalid (on X, unallocated).", ""); return rv; } + KeepGIL(GetRGB); DocStr(GetRGB, "Return the colour as a packed RGB value", ""); unsigned long GetRGB() { -- 2.47.2