more accurate mask creation for low color displays

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@40595 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Paul Cornett
2006-08-13 05:12:18 +00:00
parent 005eba5abd
commit 13785a4b8f

View File

@@ -86,78 +86,81 @@ bool wxMask::Create( const wxBitmap& bitmap,
m_bitmap = (GdkBitmap*) NULL; m_bitmap = (GdkBitmap*) NULL;
} }
wxImage image = bitmap.ConvertToImage(); const int w = bitmap.GetWidth();
if (!image.Ok()) return false; const int h = bitmap.GetHeight();
m_bitmap = gdk_pixmap_new(wxGetRootWindow()->window, w, h, 1);
m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, image.GetWidth(), image.GetHeight(), 1 );
GdkGC *gc = gdk_gc_new( m_bitmap ); GdkGC *gc = gdk_gc_new( m_bitmap );
GdkColor color; GdkColor color;
color.pixel = 1; color.pixel = 1;
gdk_gc_set_foreground( gc, &color ); gdk_gc_set_foreground( gc, &color );
gdk_draw_rectangle( m_bitmap, gc, TRUE, 0, 0, image.GetWidth(), image.GetHeight() ); gdk_draw_rectangle(m_bitmap, gc, true, 0, 0, w, h);
unsigned char *data = image.GetData();
int index = 0;
unsigned char red = colour.Red(); unsigned char red = colour.Red();
unsigned char green = colour.Green(); unsigned char green = colour.Green();
unsigned char blue = colour.Blue(); unsigned char blue = colour.Blue();
GdkImage* image = NULL;
wxByte* data = NULL;
guint32 mask_pixel = 1;
int rowpadding = 0;
int data_inc = 0;
GdkVisual *visual = wxTheApp->GetGdkVisual(); if (bitmap.HasPixbuf())
int bpp = visual->depth;
if ((bpp == 16) && (visual->red_mask != 0xf800))
bpp = 15;
if (bpp == 15)
{ {
red = red & 0xf8; GdkPixbuf* pixbuf = bitmap.GetPixbuf();
green = green & 0xf8; data = gdk_pixbuf_get_pixels(pixbuf);
blue = blue & 0xf8; data_inc = 3 + int(gdk_pixbuf_get_has_alpha(pixbuf) != 0);
rowpadding = gdk_pixbuf_get_rowstride(pixbuf) - data_inc * w;
} }
else if (bpp == 16) else
{ {
red = red & 0xf8; image = gdk_drawable_get_image(bitmap.GetPixmap(), 0, 0, w, h);
green = green & 0xfc; GdkColormap* colormap = gdk_image_get_colormap(image);
blue = blue & 0xf8; if (colormap != NULL)
} {
else if (bpp == 12) wxColor c(colour);
{ c.CalcPixel(colormap);
red = red & 0xf0; mask_pixel = c.GetPixel();
green = green & 0xf0; }
blue = blue & 0xf0;
} }
color.pixel = 0; color.pixel = 0;
gdk_gc_set_foreground( gc, &color ); gdk_gc_set_foreground( gc, &color );
for (int j = 0; j < image.GetHeight(); j++) for (int y = 0; y < h; y++)
{ {
int start_x = -1; int start_x = -1;
int i; int x;
for (i = 0; i < image.GetWidth(); i++) for (x = 0; x < w; x++)
{ {
if ((data[index] == red) && bool isMask;
(data[index+1] == green) && if (image != NULL)
(data[index+2] == blue)) isMask = gdk_image_get_pixel(image, x, y) == mask_pixel;
else
{
isMask = data[0] == red && data[1] == green && data[2] == blue;
data += data_inc;
}
if (isMask)
{ {
if (start_x == -1) if (start_x == -1)
start_x = i; start_x = x;
} }
else else
{ {
if (start_x != -1) if (start_x != -1)
{ {
gdk_draw_line( m_bitmap, gc, start_x, j, i-1, j ); gdk_draw_line(m_bitmap, gc, start_x, y, x - 1, y);
start_x = -1; start_x = -1;
} }
} }
index += 3;
} }
if (start_x != -1) if (start_x != -1)
gdk_draw_line( m_bitmap, gc, start_x, j, i, j ); gdk_draw_line(m_bitmap, gc, start_x, y, x, y);
data += rowpadding;
} }
if (image != NULL)
g_object_unref(image);
g_object_unref (gc); g_object_unref (gc);
return true; return true;