+
+ M_BMPDATA->m_bpp = visual->depth; // Can we get a different depth from create_from_xpm_d() ?
+
+ return TRUE;
+}
+
+bool wxBitmap::CreateFromImage( const wxImage& image, int depth )
+{
+ UnRef();
+
+ wxCHECK_MSG( image.Ok(), FALSE, wxT("invalid image") )
+ wxCHECK_MSG( depth == -1 || depth == 1, FALSE, wxT("invalid bitmap depth") )
+
+ m_refData = new wxBitmapRefData();
+
+ // ------
+ // conversion to mono bitmap:
+ // ------
+ if (depth == 1)
+ {
+ int width = image.GetWidth();
+ int height = image.GetHeight();
+
+ SetHeight( height );
+ SetWidth( width );
+
+ SetBitmap( gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 ) );
+
+ SetDepth( 1 );
+
+ GdkVisual *visual = wxTheApp->GetGdkVisual();
+
+ // Create picture image
+
+ unsigned char *data_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
+
+ GdkImage *data_image =
+ gdk_image_new_bitmap( visual, data_data, width, height );
+
+ // Create mask image
+
+ GdkImage *mask_image = (GdkImage*) NULL;
+
+ if (image.HasMask())
+ {
+ unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
+
+ mask_image = gdk_image_new_bitmap( visual, mask_data, width, height );
+
+ wxMask *mask = new wxMask();
+ mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 );
+
+ SetMask( mask );
+ }
+
+ int r_mask = image.GetMaskRed();
+ int g_mask = image.GetMaskGreen();
+ int b_mask = image.GetMaskBlue();
+
+ unsigned char* data = image.GetData();
+
+ int index = 0;
+ for (int y = 0; y < height; y++)
+ {
+ for (int x = 0; x < width; x++)
+ {
+ int r = data[index];
+ index++;
+ int g = data[index];
+ index++;
+ int b = data[index];
+ index++;
+
+ if (image.HasMask())
+ {
+ if ((r == r_mask) && (b == b_mask) && (g == g_mask))
+ gdk_image_put_pixel( mask_image, x, y, 1 );
+ else
+ gdk_image_put_pixel( mask_image, x, y, 0 );
+ }
+
+ if ((r == 255) && (b == 255) && (g == 255))
+ gdk_image_put_pixel( data_image, x, y, 1 );
+ else
+ gdk_image_put_pixel( data_image, x, y, 0 );
+
+ } // for
+ } // for
+
+ // Blit picture
+
+ GdkGC *data_gc = gdk_gc_new( GetBitmap() );
+
+ gdk_draw_image( GetBitmap(), data_gc, data_image, 0, 0, 0, 0, width, height );
+
+ gdk_image_destroy( data_image );
+ gdk_gc_unref( data_gc );
+
+ // Blit mask
+
+ if (image.HasMask())
+ {
+ GdkGC *mask_gc = gdk_gc_new( GetMask()->GetBitmap() );
+
+ gdk_draw_image( GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height );
+
+ gdk_image_destroy( mask_image );
+ gdk_gc_unref( mask_gc );
+ }
+ }
+
+ // ------
+ // conversion to colour bitmap:
+ // ------
+ else
+ {
+ int width = image.GetWidth();
+ int height = image.GetHeight();
+
+ SetHeight( height );
+ SetWidth( width );
+
+ SetPixmap( gdk_pixmap_new( wxGetRootWindow()->window, width, height, -1 ) );
+
+ GdkVisual *visual = wxTheApp->GetGdkVisual();
+
+ int bpp = visual->depth;
+
+ SetDepth( bpp );
+
+ if ((bpp == 16) && (visual->red_mask != 0xf800)) bpp = 15;
+ if (bpp < 8) bpp = 8;
+
+ // We handle 8-bit bitmaps ourselves using the colour cube, 12-bit
+ // visuals are not supported by GDK so we do these ourselves, too.
+ // 15-bit and 16-bit should actually work and 24-bit certainly does.
+#ifdef __sgi
+ if (!image.HasMask() && (bpp > 16))
+#else
+ if (!image.HasMask() && (bpp > 12))
+#endif
+ {
+ static bool s_hasInitialized = FALSE;
+
+ if (!s_hasInitialized)
+ {
+ gdk_rgb_init();
+ s_hasInitialized = TRUE;
+ }
+
+ GdkGC *gc = gdk_gc_new( GetPixmap() );
+
+ gdk_draw_rgb_image( GetPixmap(),
+ gc,
+ 0, 0,
+ width, height,
+ GDK_RGB_DITHER_NONE,
+ image.GetData(),
+ width*3 );
+
+ gdk_gc_unref( gc );
+ return TRUE;
+ }
+
+ // Create picture image
+
+ GdkImage *data_image =
+ gdk_image_new( GDK_IMAGE_FASTEST, visual, width, height );
+
+ // Create mask image
+
+ GdkImage *mask_image = (GdkImage*) NULL;
+
+ if (image.HasMask())
+ {
+ unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
+
+ mask_image = gdk_image_new_bitmap( visual, mask_data, width, height );