]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/bitmap.cpp
0f7ab2e9efd9ad397cc03c9c00e6c069fc5edb92
   1 ///////////////////////////////////////////////////////////////////////////// 
   2 // Name:        src/gtk/bitmap.cpp 
   4 // Author:      Robert Roebling 
   6 // Copyright:   (c) 1998 Robert Roebling 
   7 // Licence:     wxWindows licence 
   8 ///////////////////////////////////////////////////////////////////////////// 
  10 // For compilers that support precompilation, includes "wx.h". 
  11 #include "wx/wxprec.h" 
  13 #include "wx/bitmap.h" 
  17     #include "wx/palette.h" 
  21     #include "wx/colour.h" 
  24 #include "wx/rawbmp.h" 
  28 //----------------------------------------------------------------------------- 
  30 //----------------------------------------------------------------------------- 
  32 extern GtkWidget 
*wxGetRootWindow(); 
  34 //----------------------------------------------------------------------------- 
  36 //----------------------------------------------------------------------------- 
  38 IMPLEMENT_DYNAMIC_CLASS(wxMask
,wxObject
) 
  42     m_bitmap 
= (GdkBitmap 
*) NULL
; 
  45 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour 
) 
  47     m_bitmap 
= (GdkBitmap 
*) NULL
; 
  48     Create( bitmap
, colour 
); 
  52 wxMask::wxMask( const wxBitmap
& bitmap
, int paletteIndex 
) 
  54     m_bitmap 
= (GdkBitmap 
*) NULL
; 
  55     Create( bitmap
, paletteIndex 
); 
  57 #endif // wxUSE_PALETTE 
  59 wxMask::wxMask( const wxBitmap
& bitmap 
) 
  61     m_bitmap 
= (GdkBitmap 
*) NULL
; 
  68         g_object_unref (m_bitmap
); 
  71 bool wxMask::Create( const wxBitmap
& bitmap
, 
  72                      const wxColour
& colour 
) 
  76         g_object_unref (m_bitmap
); 
  77         m_bitmap 
= (GdkBitmap
*) NULL
; 
  80     const int w 
= bitmap
.GetWidth(); 
  81     const int h 
= bitmap
.GetHeight(); 
  83     // create mask as XBM format bitmap 
  85     // one bit per pixel, each row starts on a byte boundary 
  86     const size_t out_size 
= size_t((w 
+ 7) / 8) * unsigned(h
); 
  87     wxByte
* out 
= new wxByte
[out_size
]; 
  88     // set bits are unmasked 
  89     memset(out
, 0xff, out_size
); 
  90     unsigned bit_index 
= 0; 
  91     if (bitmap
.HasPixbuf()) 
  93         const wxByte r_mask 
= colour
.Red(); 
  94         const wxByte g_mask 
= colour
.Green(); 
  95         const wxByte b_mask 
= colour
.Blue(); 
  96         GdkPixbuf
* pixbuf 
= bitmap
.GetPixbuf(); 
  97         const wxByte
* in 
= gdk_pixbuf_get_pixels(pixbuf
); 
  98         const int inc 
= 3 + int(gdk_pixbuf_get_has_alpha(pixbuf
) != 0); 
  99         const int rowpadding 
= gdk_pixbuf_get_rowstride(pixbuf
) - inc 
* w
; 
 100         for (int y 
= 0; y 
< h
; y
++, in 
+= rowpadding
) 
 102             for (int x 
= 0; x 
< w
; x
++, in 
+= inc
, bit_index
++) 
 103                 if (in
[0] == r_mask 
&& in
[1] == g_mask 
&& in
[2] == b_mask
) 
 104                     out
[bit_index 
>> 3] ^= 1 << (bit_index 
& 7); 
 105             // move index to next byte boundary 
 106             bit_index 
= (bit_index 
+ 7) & ~7u; 
 111         GdkImage
* image 
= gdk_drawable_get_image(bitmap
.GetPixmap(), 0, 0, w
, h
); 
 112         GdkColormap
* colormap 
= gdk_image_get_colormap(image
); 
 114         if (colormap 
== NULL
) 
 115             // mono bitmap, white is pixel value 0 
 116             mask_pixel 
= guint32(colour
.Red() != 255 || colour
.Green() != 255 || colour
.Blue() != 255); 
 120             c
.CalcPixel(colormap
); 
 121             mask_pixel 
= c
.GetPixel(); 
 123         for (int y 
= 0; y 
< h
; y
++) 
 125             for (int x 
= 0; x 
< w
; x
++, bit_index
++) 
 126                 if (gdk_image_get_pixel(image
, x
, y
) == mask_pixel
) 
 127                     out
[bit_index 
>> 3] ^= 1 << (bit_index 
& 7); 
 128             bit_index 
= (bit_index 
+ 7) & ~7u; 
 130         g_object_unref(image
); 
 132     m_bitmap 
= gdk_bitmap_create_from_data(wxGetRootWindow()->window
, (char*)out
, w
, h
); 
 138 bool wxMask::Create( const wxBitmap
& bitmap
, int paletteIndex 
) 
 141     wxPalette 
*pal 
= bitmap
.GetPalette(); 
 143     wxCHECK_MSG( pal
, false, wxT("Cannot create mask from bitmap without palette") ); 
 145     pal
->GetRGB(paletteIndex
, &r
, &g
, &b
); 
 147     return Create(bitmap
, wxColour(r
, g
, b
)); 
 149 #endif // wxUSE_PALETTE 
 151 bool wxMask::Create( const wxBitmap
& bitmap 
) 
 155         g_object_unref (m_bitmap
); 
 156         m_bitmap 
= (GdkBitmap
*) NULL
; 
 159     if (!bitmap
.Ok()) return false; 
 161     wxCHECK_MSG( bitmap
.GetDepth() == 1, false, wxT("Cannot create mask from colour bitmap") ); 
 163     m_bitmap 
= gdk_pixmap_new( wxGetRootWindow()->window
, bitmap
.GetWidth(), bitmap
.GetHeight(), 1 ); 
 165     if (!m_bitmap
) return false; 
 167     GdkGC 
*gc 
= gdk_gc_new( m_bitmap 
); 
 168     gdk_gc_set_function(gc
, GDK_COPY_INVERT
); 
 169     gdk_draw_drawable(m_bitmap
, gc
, bitmap
.GetPixmap(), 0, 0, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight()); 
 175 GdkBitmap 
*wxMask::GetBitmap() const 
 180 //----------------------------------------------------------------------------- 
 182 //----------------------------------------------------------------------------- 
 184 class wxBitmapRefData
: public wxGDIRefData
 
 188     virtual ~wxBitmapRefData(); 
 190     virtual bool IsOk() const { return m_pixmap 
|| m_pixbuf
; } 
 199     wxPalette      
*m_palette
; 
 200 #endif // wxUSE_PALETTE 
 203 wxBitmapRefData::wxBitmapRefData() 
 205     m_pixmap 
= (GdkPixmap 
*) NULL
; 
 206     m_pixbuf 
= (GdkPixbuf 
*) NULL
; 
 207     m_mask 
= (wxMask 
*) NULL
; 
 212     m_palette 
= (wxPalette 
*) NULL
; 
 213 #endif // wxUSE_PALETTE 
 216 wxBitmapRefData::~wxBitmapRefData() 
 219         g_object_unref (m_pixmap
); 
 221         g_object_unref (m_pixbuf
); 
 225 #endif // wxUSE_PALETTE 
 228 //----------------------------------------------------------------------------- 
 230 #define M_BMPDATA wx_static_cast(wxBitmapRefData*, m_refData) 
 232 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
) 
 234 wxBitmap::wxBitmap(int width
, int height
, int depth
) 
 236     Create(width
, height
, depth
); 
 239 wxBitmap::wxBitmap(const wxString 
&filename
, wxBitmapType type
) 
 241     LoadFile(filename
, type
); 
 244 wxBitmap::wxBitmap(const char bits
[], int width
, int height
, int depth
) 
 246     wxASSERT(depth 
== 1); 
 247     if (width 
> 0 && height 
> 0 && depth 
== 1) 
 249         SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window
, bits
, width
, height
)); 
 251         wxASSERT_MSG( M_BMPDATA
->m_pixmap
, wxT("couldn't create bitmap") ); 
 255 wxBitmap::wxBitmap(const char* const* bits
) 
 257     wxCHECK2_MSG(bits 
!= NULL
, return, wxT("invalid bitmap data")); 
 259     GdkBitmap
* mask 
= NULL
; 
 260     SetPixmap(gdk_pixmap_create_from_xpm_d(wxGetRootWindow()->window
, &mask
, NULL
, wx_const_cast(char**, bits
))); 
 262     if (M_BMPDATA
->m_pixmap 
!= NULL 
&& mask 
!= NULL
) 
 264         M_BMPDATA
->m_mask 
= new wxMask
; 
 265         M_BMPDATA
->m_mask
->m_bitmap 
= mask
; 
 269 wxBitmap::~wxBitmap() 
 273 bool wxBitmap::Create( int width
, int height
, int depth 
) 
 277     if ( width 
<= 0 || height 
<= 0 ) 
 284         SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB
, true, 8, width
, height
), 32); 
 290             const GdkVisual
* visual 
= wxTheApp
->GetGdkVisual(); 
 292                 depth 
= visual
->depth
; 
 294             wxCHECK_MSG(depth 
== visual
->depth
, false, wxT("invalid bitmap depth")); 
 297         SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window
, width
, height
, depth
)); 
 303 wxBitmap 
wxBitmap::Rescale(int clipx
, int clipy
, int clipwidth
, int clipheight
, int newx
, int newy
) const 
 307     wxCHECK_MSG(Ok(), bmp
, wxT("invalid bitmap")); 
 309     int width 
= wxMax(newx
, 1); 
 310     int height 
= wxMax(newy
, 1); 
 311     width 
= wxMin(width
, clipwidth
); 
 312     height 
= wxMin(height
, clipheight
); 
 314     const double scale_x 
= double(newx
) / M_BMPDATA
->m_width
; 
 315     const double scale_y 
= double(newy
) / M_BMPDATA
->m_height
; 
 317     // Converting to pixbuf, scaling with gdk_pixbuf_scale, and converting 
 318     // back, is faster than scaling pixmap ourselves. 
 320     // use pixbuf if already available, 
 321     // otherwise create temporary pixbuf from pixmap 
 322     GdkPixbuf
* pixbuf 
= M_BMPDATA
->m_pixbuf
; 
 324         g_object_ref(pixbuf
); 
 326         pixbuf 
= gdk_pixbuf_get_from_drawable( 
 327             NULL
, M_BMPDATA
->m_pixmap
, NULL
, 
 328             0, 0, 0, 0, M_BMPDATA
->m_width
, M_BMPDATA
->m_height
); 
 330     // new pixbuf for scaled wxBitmap 
 331     GdkPixbuf
* pixbuf_scaled 
= gdk_pixbuf_new( 
 332         GDK_COLORSPACE_RGB
, gdk_pixbuf_get_has_alpha(pixbuf
), 8, width
, height
); 
 334     // GDK_INTERP_NEAREST is the lowest-quality method for continuous-tone 
 335     // images, but the only one which preserves sharp edges 
 337         pixbuf
, pixbuf_scaled
, 
 338         0, 0, width
, height
, -clipx
, -clipy
, scale_x
, scale_y
, 
 341     g_object_unref(pixbuf
); 
 342     bmp
.SetPixbuf(pixbuf_scaled
, M_BMPDATA
->m_bpp
); 
 344     if (M_BMPDATA
->m_mask
) 
 346         pixbuf 
= gdk_pixbuf_get_from_drawable( 
 347             NULL
, M_BMPDATA
->m_mask
->m_bitmap
, NULL
, 
 348             0, 0, 0, 0, M_BMPDATA
->m_width
, M_BMPDATA
->m_height
); 
 350         pixbuf_scaled 
= gdk_pixbuf_new( 
 351             GDK_COLORSPACE_RGB
, false, 8, width
, height
); 
 354             pixbuf
, pixbuf_scaled
, 
 355             0, 0, width
, height
, -clipx
, -clipy
, scale_x
, scale_y
, 
 358         g_object_unref(pixbuf
); 
 360         // use existing functionality to create mask from scaled pixbuf 
 362         maskbmp
.SetPixbuf(pixbuf_scaled
); 
 363         bmp
.SetMask(new wxMask(maskbmp
, *wxBLACK
)); 
 370 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
) 
 374     wxCHECK_MSG( image
.Ok(), false, wxT("invalid image") ); 
 375     wxCHECK_MSG( depth 
== -1 || depth 
== 1, false, wxT("invalid bitmap depth") ); 
 377     if (image
.GetWidth() <= 0 || image
.GetHeight() <= 0) 
 380     // create pixbuf if image has alpha and requested depth is compatible 
 381     if (image
.HasAlpha() && (depth 
== -1 || depth 
== 32)) 
 382         return CreateFromImageAsPixbuf(image
); 
 384     // otherwise create pixmap, if alpha is present it will be converted to mask 
 385     return CreateFromImageAsPixmap(image
, depth
); 
 388 bool wxBitmap::CreateFromImageAsPixmap(const wxImage
& image
, int depth
) 
 390     const int w 
= image
.GetWidth(); 
 391     const int h 
= image
.GetHeight(); 
 394         // create XBM format bitmap 
 396         // one bit per pixel, each row starts on a byte boundary 
 397         const size_t out_size 
= size_t((w 
+ 7) / 8) * unsigned(h
); 
 398         wxByte
* out 
= new wxByte
[out_size
]; 
 399         // set bits are black 
 400         memset(out
, 0xff, out_size
); 
 401         const wxByte
* in 
= image
.GetData(); 
 402         unsigned bit_index 
= 0; 
 403         for (int y 
= 0; y 
< h
; y
++) 
 405             for (int x 
= 0; x 
< w
; x
++, in 
+= 3, bit_index
++) 
 406                 if (in
[0] == 255 && in
[1] == 255 && in
[2] == 255) 
 407                     out
[bit_index 
>> 3] ^= 1 << (bit_index 
& 7); 
 408             // move index to next byte boundary 
 409             bit_index 
= (bit_index 
+ 7) & ~7u; 
 411         SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window
, (char*)out
, w
, h
)); 
 416         SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window
, w
, h
, depth
)); 
 417         GdkGC
* gc 
= gdk_gc_new(M_BMPDATA
->m_pixmap
); 
 419             M_BMPDATA
->m_pixmap
, gc
, 
 421             GDK_RGB_DITHER_NONE
, image
.GetData(), w 
* 3); 
 425     const wxByte
* alpha 
= image
.GetAlpha(); 
 426     if (alpha 
!= NULL 
|| image
.HasMask()) 
 428         // create mask as XBM format bitmap 
 430         const size_t out_size 
= size_t((w 
+ 7) / 8) * unsigned(h
); 
 431         wxByte
* out 
= new wxByte
[out_size
]; 
 432         memset(out
, 0xff, out_size
); 
 433         unsigned bit_index 
= 0; 
 436             for (int y 
= 0; y 
< h
; y
++) 
 438                 for (int x 
= 0; x 
< w
; x
++, bit_index
++) 
 439                     if (*alpha
++ < wxIMAGE_ALPHA_THRESHOLD
) 
 440                         out
[bit_index 
>> 3] ^= 1 << (bit_index 
& 7); 
 441                 bit_index 
= (bit_index 
+ 7) & ~7u; 
 446             const wxByte r_mask 
= image
.GetMaskRed(); 
 447             const wxByte g_mask 
= image
.GetMaskGreen(); 
 448             const wxByte b_mask 
= image
.GetMaskBlue(); 
 449             const wxByte
* in 
= image
.GetData(); 
 450             for (int y 
= 0; y 
< h
; y
++) 
 452                 for (int x 
= 0; x 
< w
; x
++, in 
+= 3, bit_index
++) 
 453                     if (in
[0] == r_mask 
&& in
[1] == g_mask 
&& in
[2] == b_mask
) 
 454                         out
[bit_index 
>> 3] ^= 1 << (bit_index 
& 7); 
 455                 bit_index 
= (bit_index 
+ 7) & ~7u; 
 458         wxMask
* mask 
= new wxMask
; 
 459         mask
->m_bitmap 
= gdk_bitmap_create_from_data(M_BMPDATA
->m_pixmap
, (char*)out
, w
, h
); 
 466 bool wxBitmap::CreateFromImageAsPixbuf(const wxImage
& image
) 
 468     wxASSERT(image
.HasAlpha()); 
 470     int width 
= image
.GetWidth(); 
 471     int height 
= image
.GetHeight(); 
 473     Create(width
, height
, 32); 
 474     GdkPixbuf
* pixbuf 
= M_BMPDATA
->m_pixbuf
; 
 479     const unsigned char* in 
= image
.GetData(); 
 480     unsigned char *out 
= gdk_pixbuf_get_pixels(pixbuf
); 
 481     unsigned char *alpha 
= image
.GetAlpha(); 
 483     int rowpad 
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
; 
 485     for (int y 
= 0; y 
< height
; y
++, out 
+= rowpad
) 
 487         for (int x 
= 0; x 
< width
; x
++, alpha
++, out 
+= 4, in 
+= 3) 
 499 wxImage 
wxBitmap::ConvertToImage() const 
 501     wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") ); 
 503     const int w 
= GetWidth(); 
 504     const int h 
= GetHeight(); 
 505     wxImage 
image(w
, h
, false); 
 506     unsigned char *data 
= image
.GetData(); 
 508     wxCHECK_MSG(data 
!= NULL
, wxNullImage
, wxT("couldn't create image") ); 
 510     // prefer pixbuf if available, it will preserve alpha and should be quicker 
 513         GdkPixbuf 
*pixbuf 
= GetPixbuf(); 
 514         unsigned char* alpha 
= NULL
; 
 515         if (gdk_pixbuf_get_has_alpha(pixbuf
)) 
 518             alpha 
= image
.GetAlpha(); 
 520         const unsigned char* in 
= gdk_pixbuf_get_pixels(pixbuf
); 
 521         unsigned char *out 
= data
; 
 522         const int inc 
= 3 + int(alpha 
!= NULL
); 
 523         const int rowpad 
= gdk_pixbuf_get_rowstride(pixbuf
) - inc 
* w
; 
 525         for (int y 
= 0; y 
< h
; y
++, in 
+= rowpad
) 
 527             for (int x 
= 0; x 
< w
; x
++, in 
+= inc
, out 
+= 3) 
 539         GdkPixmap
* pixmap 
= GetPixmap(); 
 540         GdkPixmap
* pixmap_invert 
= NULL
; 
 543             // mono bitmaps are inverted, i.e. 0 is white 
 544             pixmap_invert 
= gdk_pixmap_new(pixmap
, w
, h
, 1); 
 545             GdkGC
* gc 
= gdk_gc_new(pixmap_invert
); 
 546             gdk_gc_set_function(gc
, GDK_COPY_INVERT
); 
 547             gdk_draw_drawable(pixmap_invert
, gc
, pixmap
, 0, 0, 0, 0, w
, h
); 
 549             pixmap 
= pixmap_invert
; 
 551         // create a pixbuf which shares data with the wxImage 
 552         GdkPixbuf
* pixbuf 
= gdk_pixbuf_new_from_data( 
 553             data
, GDK_COLORSPACE_RGB
, false, 8, w
, h
, 3 * w
, NULL
, NULL
); 
 555         gdk_pixbuf_get_from_drawable(pixbuf
, pixmap
, NULL
, 0, 0, 0, 0, w
, h
); 
 557         g_object_unref(pixbuf
); 
 558         if (pixmap_invert 
!= NULL
) 
 559             g_object_unref(pixmap_invert
); 
 561     // convert mask, unless there is already alpha 
 562     if (GetMask() && !image
.HasAlpha()) 
 564         // we hard code the mask colour for now but we could also make an 
 565         // effort (and waste time) to choose a colour not present in the 
 566         // image already to avoid having to fudge the pixels below -- 
 567         // whether it's worth to do it is unclear however 
 568         const int MASK_RED 
= 1; 
 569         const int MASK_GREEN 
= 2; 
 570         const int MASK_BLUE 
= 3; 
 571         const int MASK_BLUE_REPLACEMENT 
= 2; 
 573         image
.SetMaskColour(MASK_RED
, MASK_GREEN
, MASK_BLUE
); 
 574         GdkImage
* image_mask 
= gdk_drawable_get_image(GetMask()->GetBitmap(), 0, 0, w
, h
); 
 576         for (int y 
= 0; y 
< h
; y
++) 
 578             for (int x 
= 0; x 
< w
; x
++, data 
+= 3) 
 580                 if (gdk_image_get_pixel(image_mask
, x
, y
) == 0) 
 583                     data
[1] = MASK_GREEN
; 
 586                 else if (data
[0] == MASK_RED 
&& data
[1] == MASK_GREEN 
&& data
[2] == MASK_BLUE
) 
 588                     // we have to fudge the colour a bit to prevent 
 589                     // this pixel from appearing transparent 
 590                     data
[2] = MASK_BLUE_REPLACEMENT
; 
 594         g_object_unref(image_mask
); 
 600 #endif // wxUSE_IMAGE 
 602 int wxBitmap::GetHeight() const 
 604     wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); 
 606     return M_BMPDATA
->m_height
; 
 609 int wxBitmap::GetWidth() const 
 611     wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); 
 613     return M_BMPDATA
->m_width
; 
 616 int wxBitmap::GetDepth() const 
 618     wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); 
 620     return M_BMPDATA
->m_bpp
; 
 623 wxMask 
*wxBitmap::GetMask() const 
 625     wxCHECK_MSG( Ok(), (wxMask 
*) NULL
, wxT("invalid bitmap") ); 
 627     return M_BMPDATA
->m_mask
; 
 630 void wxBitmap::SetMask( wxMask 
*mask 
) 
 632     wxCHECK_RET( Ok(), wxT("invalid bitmap") ); 
 635     delete M_BMPDATA
->m_mask
; 
 636     M_BMPDATA
->m_mask 
= mask
; 
 639 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
) 
 645 wxBitmap 
wxBitmap::GetSubBitmap( const wxRect
& rect
) const 
 649     wxCHECK_MSG(Ok(), ret
, wxT("invalid bitmap")); 
 650     wxCHECK_MSG(rect
.x 
>= 0 && rect
.y 
>= 0 && 
 651                 rect
.x 
+ rect
.width 
<= M_BMPDATA
->m_width 
&& 
 652                 rect
.y 
+ rect
.height 
<= M_BMPDATA
->m_height
, 
 653                 ret
, wxT("invalid bitmap region")); 
 655     if (HasPixbuf() || M_BMPDATA
->m_bpp 
== 32) 
 657         GdkPixbuf 
*pixbuf 
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
, 
 658                                            gdk_pixbuf_get_has_alpha(GetPixbuf()), 
 659                                            8, rect
.width
, rect
.height
); 
 660         ret
.SetPixbuf(pixbuf
, M_BMPDATA
->m_bpp
); 
 661         gdk_pixbuf_copy_area(GetPixbuf(), 
 662                              rect
.x
, rect
.y
, rect
.width
, rect
.height
, 
 667         ret
.Create(rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
); 
 668         GdkGC 
*gc 
= gdk_gc_new( ret
.GetPixmap() ); 
 669         gdk_draw_drawable( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height 
); 
 672     // make mask, unless there is already alpha 
 673     if (GetMask() && !HasAlpha()) 
 675         wxMask 
*mask 
= new wxMask
; 
 676         mask
->m_bitmap 
= gdk_pixmap_new( wxGetRootWindow()->window
, rect
.width
, rect
.height
, 1 ); 
 678         GdkGC 
*gc 
= gdk_gc_new( mask
->m_bitmap 
); 
 679         gdk_draw_drawable(mask
->m_bitmap
, gc
, M_BMPDATA
->m_mask
->m_bitmap
, rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
); 
 688 bool wxBitmap::SaveFile( const wxString 
&name
, wxBitmapType type
, const wxPalette 
*WXUNUSED(palette
) ) const 
 690     wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") ); 
 693     // Try to save the bitmap via wxImage handlers: 
 694     wxImage image 
= ConvertToImage(); 
 695     return image
.Ok() && image
.SaveFile(name
, type
); 
 696 #else // !wxUSE_IMAGE 
 701 #endif // wxUSE_IMAGE 
 704 bool wxBitmap::LoadFile( const wxString 
&name
, wxBitmapType type 
) 
 708     if (type 
== wxBITMAP_TYPE_XPM
) 
 710         GdkBitmap 
*mask 
= (GdkBitmap
*) NULL
; 
 711         SetPixmap(gdk_pixmap_create_from_xpm(wxGetRootWindow()->window
, &mask
, NULL
, name
.fn_str())); 
 715             M_BMPDATA
->m_mask 
= new wxMask
; 
 716             M_BMPDATA
->m_mask
->m_bitmap 
= mask
; 
 720     else // try if wxImage can load it 
 723         if (image
.LoadFile(name
, type
) && image
.Ok()) 
 724             CreateFromImage(image
, -1); 
 726 #endif // wxUSE_IMAGE 
 732 wxPalette 
*wxBitmap::GetPalette() const 
 734     wxCHECK_MSG(Ok(), NULL
, wxT("invalid bitmap")); 
 736     return M_BMPDATA
->m_palette
; 
 739 void wxBitmap::SetPalette(const wxPalette
& WXUNUSED(palette
)) 
 743 #endif // wxUSE_PALETTE 
 745 void wxBitmap::SetHeight( int height 
) 
 748     M_BMPDATA
->m_height 
= height
; 
 751 void wxBitmap::SetWidth( int width 
) 
 754     M_BMPDATA
->m_width 
= width
; 
 757 void wxBitmap::SetDepth( int depth 
) 
 760     M_BMPDATA
->m_bpp 
= depth
; 
 763 void wxBitmap::SetPixmap( GdkPixmap 
*pixmap 
) 
 766         m_refData 
= new wxBitmapRefData
; 
 768     // AllocExclusive should not be needed for this internal function 
 769     wxASSERT(m_refData
->GetRefCount() == 1); 
 770     wxASSERT(M_BMPDATA
->m_pixmap 
== NULL
); 
 771     M_BMPDATA
->m_pixmap 
= pixmap
; 
 772     gdk_drawable_get_size(pixmap
, &M_BMPDATA
->m_width
, &M_BMPDATA
->m_height
); 
 773     M_BMPDATA
->m_bpp 
= gdk_drawable_get_depth(pixmap
); 
 774     PurgeOtherRepresentations(Pixmap
); 
 777 GdkPixmap 
*wxBitmap::GetPixmap() const 
 779     wxCHECK_MSG( Ok(), (GdkPixmap 
*) NULL
, wxT("invalid bitmap") ); 
 781     // create the pixmap on the fly if we use Pixbuf representation: 
 782     if (M_BMPDATA
->m_pixmap 
== NULL
) 
 784         GdkPixmap
** pmask 
= NULL
; 
 785         if (gdk_pixbuf_get_has_alpha(M_BMPDATA
->m_pixbuf
)) 
 787             // make new mask from alpha 
 788             delete M_BMPDATA
->m_mask
; 
 789             M_BMPDATA
->m_mask 
= new wxMask
; 
 790             pmask 
= &M_BMPDATA
->m_mask
->m_bitmap
; 
 792         gdk_pixbuf_render_pixmap_and_mask(M_BMPDATA
->m_pixbuf
, 
 793                                           &M_BMPDATA
->m_pixmap
, 
 795                                           0x80 /* alpha threshold */); 
 798     return M_BMPDATA
->m_pixmap
; 
 801 bool wxBitmap::HasPixmap() const 
 803     wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") ); 
 805     return M_BMPDATA
->m_pixmap 
!= NULL
; 
 808 GdkPixbuf 
*wxBitmap::GetPixbuf() const 
 810     wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ); 
 812     if (M_BMPDATA
->m_pixbuf 
== NULL
) 
 814         int width 
= GetWidth(); 
 815         int height 
= GetHeight(); 
 817         GdkPixbuf 
*pixbuf 
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
, 
 820         M_BMPDATA
->m_pixbuf 
= pixbuf
; 
 821         gdk_pixbuf_get_from_drawable(pixbuf
, M_BMPDATA
->m_pixmap
, NULL
, 
 822                                      0, 0, 0, 0, width
, height
); 
 824         // apply the mask to created pixbuf: 
 825         if (M_BMPDATA
->m_pixbuf 
&& M_BMPDATA
->m_mask
) 
 828                 gdk_pixbuf_get_from_drawable(NULL
, 
 829                                              M_BMPDATA
->m_mask
->GetBitmap(), 
 831                                              0, 0, 0, 0, width
, height
); 
 834                 guchar 
*bmp 
= gdk_pixbuf_get_pixels(pixbuf
); 
 835                 guchar 
*mask 
= gdk_pixbuf_get_pixels(pmask
); 
 836                 int bmprowinc 
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
; 
 837                 int maskrowinc 
= gdk_pixbuf_get_rowstride(pmask
) - 3 * width
; 
 839                 for (int y 
= 0; y 
< height
; 
 840                      y
++, bmp 
+= bmprowinc
, mask 
+= maskrowinc
) 
 842                     for (int x 
= 0; x 
< width
; x
++, bmp 
+= 4, mask 
+= 3) 
 844                         if (mask
[0] == 0 /*black pixel*/) 
 849                 g_object_unref (pmask
); 
 854     return M_BMPDATA
->m_pixbuf
; 
 857 bool wxBitmap::HasPixbuf() const 
 859     wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") ); 
 861     return M_BMPDATA
->m_pixbuf 
!= NULL
; 
 864 void wxBitmap::SetPixbuf(GdkPixbuf
* pixbuf
, int depth
) 
 867         m_refData 
= new wxBitmapRefData
; 
 869     // AllocExclusive should not be needed for this internal function 
 870     wxASSERT(m_refData
->GetRefCount() == 1); 
 871     wxASSERT(M_BMPDATA
->m_pixbuf 
== NULL
); 
 872     M_BMPDATA
->m_pixbuf 
= pixbuf
; 
 873     M_BMPDATA
->m_width 
= gdk_pixbuf_get_width(pixbuf
); 
 874     M_BMPDATA
->m_height 
= gdk_pixbuf_get_height(pixbuf
); 
 875     // if depth specified 
 877         M_BMPDATA
->m_bpp 
= depth
; 
 878     else if (M_BMPDATA
->m_bpp 
== 0) 
 879         // use something reasonable 
 880         M_BMPDATA
->m_bpp 
= wxTheApp
->GetGdkVisual()->depth
; 
 881     PurgeOtherRepresentations(Pixbuf
); 
 884 void wxBitmap::PurgeOtherRepresentations(wxBitmap::Representation keep
) 
 886     if (keep 
== Pixmap 
&& HasPixbuf()) 
 888         g_object_unref (M_BMPDATA
->m_pixbuf
); 
 889         M_BMPDATA
->m_pixbuf 
= NULL
; 
 891     if (keep 
== Pixbuf 
&& HasPixmap()) 
 893         g_object_unref (M_BMPDATA
->m_pixmap
); 
 894         M_BMPDATA
->m_pixmap 
= NULL
; 
 898 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
) 
 901     GdkPixbuf 
*pixbuf 
= GetPixbuf(); 
 902     const bool hasAlpha 
= HasAlpha(); 
 903     // allow access if bpp is valid and matches existence of alpha 
 904     if ( pixbuf 
&& ((bpp 
== 24 && !hasAlpha
) || (bpp 
== 32 && hasAlpha
)) ) 
 906         data
.m_height 
= gdk_pixbuf_get_height( pixbuf 
); 
 907         data
.m_width 
= gdk_pixbuf_get_width( pixbuf 
); 
 908         data
.m_stride 
= gdk_pixbuf_get_rowstride( pixbuf 
); 
 909         bits 
= gdk_pixbuf_get_pixels(pixbuf
); 
 914 void wxBitmap::UngetRawData(wxPixelDataBase
& WXUNUSED(data
)) 
 918 bool wxBitmap::HasAlpha() const 
 920     return m_refData 
!= NULL 
&& M_BMPDATA
->m_pixbuf 
!= NULL 
&& 
 921         gdk_pixbuf_get_has_alpha(M_BMPDATA
->m_pixbuf
); 
 924 wxGDIRefData
* wxBitmap::CreateGDIRefData() const 
 926     return new wxBitmapRefData
; 
 929 wxGDIRefData
* wxBitmap::CloneGDIRefData(const wxGDIRefData
* data
) const 
 931     const wxBitmapRefData
* oldRef 
= wx_static_cast(const wxBitmapRefData
*, data
); 
 932     wxBitmapRefData
* newRef 
= new wxBitmapRefData
; 
 933     newRef
->m_width 
= oldRef
->m_width
; 
 934     newRef
->m_height 
= oldRef
->m_height
; 
 935     newRef
->m_bpp 
= oldRef
->m_bpp
; 
 936     if (oldRef
->m_pixmap 
!= NULL
) 
 938         newRef
->m_pixmap 
= gdk_pixmap_new( 
 939             oldRef
->m_pixmap
, oldRef
->m_width
, oldRef
->m_height
, 
 940             // use pixmap depth, m_bpp may not match 
 941             gdk_drawable_get_depth(oldRef
->m_pixmap
)); 
 942         GdkGC
* gc 
= gdk_gc_new(newRef
->m_pixmap
); 
 944             newRef
->m_pixmap
, gc
, oldRef
->m_pixmap
, 0, 0, 0, 0, -1, -1); 
 947     if (oldRef
->m_pixbuf 
!= NULL
) 
 949         newRef
->m_pixbuf 
= gdk_pixbuf_copy(oldRef
->m_pixbuf
); 
 951     if (oldRef
->m_mask 
!= NULL
) 
 953         newRef
->m_mask 
= new wxMask
; 
 954         newRef
->m_mask
->m_bitmap 
= gdk_pixmap_new( 
 955             oldRef
->m_mask
->m_bitmap
, oldRef
->m_width
, oldRef
->m_height
, 1); 
 956         GdkGC
* gc 
= gdk_gc_new(newRef
->m_mask
->m_bitmap
); 
 957         gdk_draw_drawable(newRef
->m_mask
->m_bitmap
, 
 958             gc
, oldRef
->m_mask
->m_bitmap
, 0, 0, 0, 0, -1, -1); 
 962     // implement this if SetPalette is ever implemented 
 963     wxASSERT(oldRef
->m_palette 
== NULL
); 
 969 /* static */ void wxBitmap::InitStandardHandlers() 
 971     // TODO: Insert handler based on GdkPixbufs handler later