No real changes, just rearrange wxCursor::InitFromImage() in wxGTK.

Make it more clear that the function deals with two cases by using if/else
instead of if+return.

Also use smart wxGtkObject pointer instead of calling g_object_unref()
manually.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@65108 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2010-07-25 18:39:16 +00:00
parent 5a9dd92128
commit 833fb475ce

View File

@@ -21,6 +21,7 @@
#endif // WX_PRECOMP #endif // WX_PRECOMP
#include <gtk/gtk.h> #include <gtk/gtk.h>
#include "wx/gtk/private/object.h"
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
// wxCursorRefData // wxCursorRefData
@@ -231,106 +232,108 @@ void wxCursor::InitFromImage( const wxImage & image )
bitmap.GetPixbuf(), bitmap.GetPixbuf(),
hotSpotX, hotSpotY hotSpotX, hotSpotY
); );
return;
} }
else // no colour cursor support
unsigned long keyMaskColor = 0;
GdkPixmap* mask;
if (bHasMask)
{ {
keyMaskColor = wxImageHistogram::MakeKey( unsigned long keyMaskColor = 0;
image.GetMaskRed(), image.GetMaskGreen(), image.GetMaskBlue()); GdkPixmap *maskRaw;
// get mask before image is modified if (bHasMask)
wxBitmap bitmap(image, 1);
mask = bitmap.GetMask()->GetBitmap();
g_object_ref(mask);
}
else
{
const int size = ((w + 7) / 8) * h;
char* bits = new char[size];
memset(bits, 0xff, size);
mask = gdk_bitmap_create_from_data(
wxGetRootWindow()->window, bits, w, h);
delete[] bits;
}
// modify image so wxBitmap can be used to convert to pixmap
image_copy.SetMask(false);
wxByte* data = image_copy.GetData();
for (int j = 0; j < h; j++)
{
for (int i = 0; i < w; i++, data += 3)
{ {
// if average value of the pixel is > mid grey, convert it to keyMaskColor = wxImageHistogram::MakeKey(
// background (0), otherwise to foreground (255, using wxBitmap image.GetMaskRed(), image.GetMaskGreen(), image.GetMaskBlue());
// convention) // get mask before image is modified
data[0] = wxBitmap bitmap(image, 1);
data[1] = maskRaw = bitmap.GetMask()->GetBitmap();
data[2] = int(data[0]) + data[1] + data[2] >= 3 * 128 ? 0 : 255; g_object_ref(maskRaw);
} }
} else
wxBitmap bitmap(image_copy, 1);
// find the most frequent color(s)
wxImageHistogram histogram;
image.ComputeHistogram(histogram);
long colMostFreq = 0;
unsigned long nMost = 0;
long colNextMostFreq = 0;
unsigned long nNext = 0;
for ( wxImageHistogram::iterator entry = histogram.begin();
entry != histogram.end();
++entry )
{
unsigned long key = entry->first;
if ( !bHasMask || (key != keyMaskColor) )
{ {
unsigned long value = entry->second.value; const int size = ((w + 7) / 8) * h;
if (value > nMost) char* bits = new char[size];
memset(bits, 0xff, size);
maskRaw = gdk_bitmap_create_from_data(
wxGetRootWindow()->window, bits, w, h);
delete[] bits;
}
// assign the raw pointer to wxGtkObject to ensure it is unref'd later
wxGtkObject<GdkPixmap> mask(maskRaw);
// modify image so wxBitmap can be used to convert to pixmap
image_copy.SetMask(false);
wxByte* data = image_copy.GetData();
for (int j = 0; j < h; j++)
{
for (int i = 0; i < w; i++, data += 3)
{ {
nNext = nMost; // if average value of the pixel is > mid grey, convert it to
colNextMostFreq = colMostFreq; // background (0), otherwise to foreground (255, using wxBitmap
nMost = value; // convention)
colMostFreq = key; data[0] =
} data[1] =
else if (value > nNext) data[2] = int(data[0]) + data[1] + data[2] >= 3 * 128 ? 0 : 255;
{
nNext = value;
colNextMostFreq = key;
} }
} }
wxBitmap bitmap(image_copy, 1);
// find the most frequent color(s)
wxImageHistogram histogram;
image.ComputeHistogram(histogram);
long colMostFreq = 0;
unsigned long nMost = 0;
long colNextMostFreq = 0;
unsigned long nNext = 0;
for ( wxImageHistogram::iterator entry = histogram.begin();
entry != histogram.end();
++entry )
{
unsigned long key = entry->first;
if ( !bHasMask || (key != keyMaskColor) )
{
unsigned long value = entry->second.value;
if (value > nMost)
{
nNext = nMost;
colNextMostFreq = colMostFreq;
nMost = value;
colMostFreq = key;
}
else if (value > nNext)
{
nNext = value;
colNextMostFreq = key;
}
}
}
wxColour fg = wxColour ( (unsigned char)(colMostFreq >> 16),
(unsigned char)(colMostFreq >> 8),
(unsigned char)(colMostFreq) );
wxColour bg = wxColour ( (unsigned char)(colNextMostFreq >> 16),
(unsigned char)(colNextMostFreq >> 8),
(unsigned char)(colNextMostFreq) );
int fg_intensity = fg.Red() + fg.Green() + fg.Blue();
int bg_intensity = bg.Red() + bg.Green() + bg.Blue();
if (bg_intensity > fg_intensity)
{
//swap fg and bg
wxColour tmp = fg;
fg = bg;
bg = tmp;
}
M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixmap
(
bitmap.GetPixmap(),
mask,
fg.GetColor(), bg.GetColor(),
hotSpotX, hotSpotY
);
} }
wxColour fg = wxColour ( (unsigned char)(colMostFreq >> 16),
(unsigned char)(colMostFreq >> 8),
(unsigned char)(colMostFreq) );
wxColour bg = wxColour ( (unsigned char)(colNextMostFreq >> 16),
(unsigned char)(colNextMostFreq >> 8),
(unsigned char)(colNextMostFreq) );
int fg_intensity = fg.Red() + fg.Green() + fg.Blue();
int bg_intensity = bg.Red() + bg.Green() + bg.Blue();
if (bg_intensity > fg_intensity)
{
//swap fg and bg
wxColour tmp = fg;
fg = bg;
bg = tmp;
}
M_CURSORDATA->m_cursor = gdk_cursor_new_from_pixmap
(
bitmap.GetPixmap(),
mask,
fg.GetColor(), bg.GetColor(),
hotSpotX, hotSpotY
);
g_object_unref (mask);
} }
#endif // wxUSE_IMAGE #endif // wxUSE_IMAGE