]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/bitmap.cpp
2476c1b9e7d97716811e0d48a392d774dc90d7c3
   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" 
  23 #include "wx/rawbmp.h" 
  27 extern void gdk_wx_draw_bitmap     (GdkDrawable  
*drawable
, 
  37 //----------------------------------------------------------------------------- 
  39 //----------------------------------------------------------------------------- 
  41 extern GtkWidget 
*wxGetRootWindow(); 
  43 //----------------------------------------------------------------------------- 
  45 //----------------------------------------------------------------------------- 
  47 IMPLEMENT_DYNAMIC_CLASS(wxMask
,wxObject
) 
  51     m_bitmap 
= (GdkBitmap 
*) NULL
; 
  54 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour 
) 
  56     m_bitmap 
= (GdkBitmap 
*) NULL
; 
  57     Create( bitmap
, colour 
); 
  61 wxMask::wxMask( const wxBitmap
& bitmap
, int paletteIndex 
) 
  63     m_bitmap 
= (GdkBitmap 
*) NULL
; 
  64     Create( bitmap
, paletteIndex 
); 
  66 #endif // wxUSE_PALETTE 
  68 wxMask::wxMask( const wxBitmap
& bitmap 
) 
  70     m_bitmap 
= (GdkBitmap 
*) NULL
; 
  77         g_object_unref (m_bitmap
); 
  80 bool wxMask::Create( const wxBitmap
& bitmap
, 
  81                      const wxColour
& colour 
) 
  85         g_object_unref (m_bitmap
); 
  86         m_bitmap 
= (GdkBitmap
*) NULL
; 
  89     const int w 
= bitmap
.GetWidth(); 
  90     const int h 
= bitmap
.GetHeight(); 
  91     m_bitmap 
= gdk_pixmap_new(wxGetRootWindow()->window
, w
, h
, 1); 
  92     GdkGC 
*gc 
= gdk_gc_new( m_bitmap 
); 
  96     gdk_gc_set_foreground( gc
, &color 
); 
  97     gdk_draw_rectangle(m_bitmap
, gc
, true, 0, 0, w
, h
); 
  99     unsigned char red 
= colour
.Red(); 
 100     unsigned char green 
= colour
.Green(); 
 101     unsigned char blue 
= colour
.Blue(); 
 102     GdkImage
* image 
= NULL
; 
 104     guint32 mask_pixel 
= 1; 
 108     if (bitmap
.HasPixbuf()) 
 110         GdkPixbuf
* pixbuf 
= bitmap
.GetPixbuf(); 
 111         data 
= gdk_pixbuf_get_pixels(pixbuf
); 
 112         data_inc 
= 3 + int(gdk_pixbuf_get_has_alpha(pixbuf
) != 0); 
 113         rowpadding 
= gdk_pixbuf_get_rowstride(pixbuf
) - data_inc 
* w
; 
 117         image 
= gdk_drawable_get_image(bitmap
.GetPixmap(), 0, 0, w
, h
); 
 118         GdkColormap
* colormap 
= gdk_image_get_colormap(image
); 
 119         if (colormap 
!= NULL
) 
 122             c
.CalcPixel(colormap
); 
 123             mask_pixel 
= c
.GetPixel(); 
 128     gdk_gc_set_foreground( gc
, &color 
); 
 130     for (int y 
= 0; y 
< h
; y
++) 
 134         for (x 
= 0; x 
< w
; x
++) 
 138                 isMask 
= gdk_image_get_pixel(image
, x
, y
) == mask_pixel
; 
 141                 isMask 
= data
[0] == red 
&& data
[1] == green 
&& data
[2] == blue
; 
 153                     gdk_draw_line(m_bitmap
, gc
, start_x
, y
, x 
- 1, y
); 
 159             gdk_draw_line(m_bitmap
, gc
, start_x
, y
, x
, y
); 
 163         g_object_unref(image
); 
 170 bool wxMask::Create( const wxBitmap
& bitmap
, int paletteIndex 
) 
 173     wxPalette 
*pal 
= bitmap
.GetPalette(); 
 175     wxCHECK_MSG( pal
, false, wxT("Cannot create mask from bitmap without palette") ); 
 177     pal
->GetRGB(paletteIndex
, &r
, &g
, &b
); 
 179     return Create(bitmap
, wxColour(r
, g
, b
)); 
 181 #endif // wxUSE_PALETTE 
 183 bool wxMask::Create( const wxBitmap
& bitmap 
) 
 187         g_object_unref (m_bitmap
); 
 188         m_bitmap 
= (GdkBitmap
*) NULL
; 
 191     if (!bitmap
.Ok()) return false; 
 193     wxCHECK_MSG( bitmap
.GetDepth() == 1, false, wxT("Cannot create mask from colour bitmap") ); 
 195     m_bitmap 
= gdk_pixmap_new( wxGetRootWindow()->window
, bitmap
.GetWidth(), bitmap
.GetHeight(), 1 ); 
 197     if (!m_bitmap
) return false; 
 199     GdkGC 
*gc 
= gdk_gc_new( m_bitmap 
); 
 201     gdk_wx_draw_bitmap( m_bitmap
, gc
, bitmap
.GetPixmap(), 0, 0, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight() ); 
 208 GdkBitmap 
*wxMask::GetBitmap() const 
 213 //----------------------------------------------------------------------------- 
 215 //----------------------------------------------------------------------------- 
 217 class wxBitmapRefData
: public wxObjectRefData
 
 229     wxPalette      
*m_palette
; 
 232 wxBitmapRefData::wxBitmapRefData() 
 234     m_pixmap 
= (GdkPixmap 
*) NULL
; 
 235     m_pixbuf 
= (GdkPixbuf 
*) NULL
; 
 236     m_mask 
= (wxMask 
*) NULL
; 
 240     m_palette 
= (wxPalette 
*) NULL
; 
 243 wxBitmapRefData::~wxBitmapRefData() 
 246         g_object_unref (m_pixmap
); 
 248         g_object_unref (m_pixbuf
); 
 252 #endif // wxUSE_PALETTE 
 255 //----------------------------------------------------------------------------- 
 257 #define M_BMPDATA wx_static_cast(wxBitmapRefData*, m_refData) 
 259 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
) 
 265 wxBitmap::wxBitmap( int width
, int height
, int depth 
) 
 267     Create( width
, height
, depth 
); 
 270 bool wxBitmap::Create( int width
, int height
, int depth 
) 
 274     if ( width 
<= 0 || height 
<= 0 ) 
 281         SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB
, true, 8, width
, height
)); 
 282         M_BMPDATA
->m_bpp 
= 32; 
 288             const GdkVisual
* visual 
= wxTheApp
->GetGdkVisual(); 
 290                 depth 
= visual
->depth
; 
 292             wxCHECK_MSG(depth 
== visual
->depth
, false, wxT("invalid bitmap depth")); 
 295         SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window
, width
, height
, depth
)); 
 301 bool wxBitmap::CreateFromXpm( const char **bits 
) 
 305     wxCHECK_MSG( bits 
!= NULL
, false, wxT("invalid bitmap data") ); 
 307     GdkBitmap 
*mask 
= (GdkBitmap
*) NULL
; 
 308     SetPixmap(gdk_pixmap_create_from_xpm_d(wxGetRootWindow()->window
, &mask
, NULL
, (gchar
**)bits
)); 
 310     wxCHECK_MSG( M_BMPDATA
->m_pixmap
, false, wxT("couldn't create pixmap") ); 
 314         M_BMPDATA
->m_mask 
= new wxMask
; 
 315         M_BMPDATA
->m_mask
->m_bitmap 
= mask
; 
 321 wxBitmap 
wxBitmap::Rescale( int clipx
, int clipy
, int clipwidth
, int clipheight
, int newx
, int newy 
) 
 325     wxCHECK_MSG(Ok(), bmp
, wxT("invalid bitmap")); 
 327     if (newy
==M_BMPDATA
->m_width 
&& newy
==M_BMPDATA
->m_height
) 
 330     int width 
= wxMax(newx
, 1); 
 331     int height 
= wxMax(newy
, 1); 
 332     width 
= wxMin(width
, clipwidth
); 
 333     height 
= wxMin(height
, clipheight
); 
 337         bmp
.SetDepth(GetDepth()); 
 338         bmp
.SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB
, 
 339                                      gdk_pixbuf_get_has_alpha(GetPixbuf()), 
 341         gdk_pixbuf_scale(GetPixbuf(), bmp
.GetPixbuf(), 
 344                          (double)newx
/GetWidth(), (double)newy
/GetHeight(), 
 345                          GDK_INTERP_BILINEAR
); 
 349         GdkImage
* img 
= gdk_drawable_get_image(GetPixmap(), 0, 0, GetWidth(), GetHeight()); 
 351         wxCHECK_MSG(img
, bmp
, wxT("couldn't create image")); 
 354         GdkPixmap 
*dstpix 
= NULL
; 
 356         long dstbyteperline 
= 0; 
 360             GdkVisual 
*visual 
= gdk_drawable_get_visual( GetPixmap() ); 
 362                 visual 
= wxTheApp
->GetGdkVisual(); 
 364             bmp 
= wxBitmap(width
, height
, visual
->depth
); 
 365             dstpix 
= bmp
.GetPixmap(); 
 366             gc 
= gdk_gc_new( dstpix 
); 
 370             dstbyteperline 
= (width 
+ 7) / 8; 
 371             dst 
= (char*) malloc(dstbyteperline
*height
); 
 374         // be careful to use the right scaling factor 
 375         float scx 
= (float)M_BMPDATA
->m_width
/(float)newx
; 
 376         float scy 
= (float)M_BMPDATA
->m_height
/(float)newy
; 
 377         // prepare accel-tables 
 378         int *tablex 
= (int *)calloc(width
,sizeof(int)); 
 379         int *tabley 
= (int *)calloc(height
,sizeof(int)); 
 381         // accel table filled with clipped values 
 382         for (int x 
= 0; x 
< width
; x
++) 
 383             tablex
[x
] = (int) (scx 
* (x
+clipx
)); 
 384         for (int y 
= 0; y 
< height
; y
++) 
 385             tabley
[y
] = (int) (scy 
* (y
+clipy
)); 
 387         // Main rescaling routine starts here 
 388         for (int h 
= 0; h 
< height
; h
++) 
 392             guint32 old_pixval 
= 0; 
 394             for (int w 
= 0; w 
< width
; w
++) 
 402                     pixval 
= gdk_image_get_pixel( img
, x
, tabley
[h
] ); 
 412                         char shift 
= bit 
<< (w 
% 8); 
 418                         dst
[h
*dstbyteperline
+w
/8] = outbyte
; 
 426                     gdk_gc_set_foreground( gc
, &col 
); 
 427                     gdk_draw_point( dstpix
, gc
, w
, h
); 
 431             // do not forget the last byte 
 432             if ( dst 
&& (width 
% 8 != 0) ) 
 433                 dst
[h
*dstbyteperline
+width
/8] = outbyte
; 
 436         g_object_unref (img
); 
 437         if (gc
) g_object_unref (gc
); 
 441             bmp 
= wxBitmap( (const char *)dst
, width
, height
, 1 ); 
 447             dstbyteperline 
= (width 
+ 7) / 8; 
 448             dst 
= (char*) malloc(dstbyteperline
*height
); 
 449             img 
= gdk_drawable_get_image(GetMask()->GetBitmap(), 0, 0, GetWidth(), GetHeight()); 
 451             for (int h 
= 0; h 
< height
; h
++) 
 455                 guint32 old_pixval 
= 0; 
 457                 for (int w 
= 0; w 
< width
; w
++) 
 465                         pixval 
= gdk_image_get_pixel( img
, x
, tabley
[h
] ); 
 473                         char shift 
= bit 
<< (w 
% 8); 
 479                         dst
[h
*dstbyteperline
+w
/8] = outbyte
; 
 484                 // do not forget the last byte 
 486                     dst
[h
*dstbyteperline
+width
/8] = outbyte
; 
 488             wxMask
* mask 
= new wxMask
; 
 489             mask
->m_bitmap 
= gdk_bitmap_create_from_data( wxGetRootWindow()->window
, (gchar 
*) dst
, width
, height 
); 
 493             g_object_unref (img
); 
 503 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
) 
 507     wxCHECK_MSG( image
.Ok(), false, wxT("invalid image") ); 
 508     wxCHECK_MSG( depth 
== -1 || depth 
== 1, false, wxT("invalid bitmap depth") ); 
 510     if (image
.GetWidth() <= 0 || image
.GetHeight() <= 0) 
 514         return CreateFromImageAsBitmap(image
); 
 516     if (image
.HasAlpha()) 
 517         return CreateFromImageAsPixbuf(image
); 
 519     return CreateFromImageAsPixmap(image
); 
 522 // conversion to mono bitmap: 
 523 bool wxBitmap::CreateFromImageAsBitmap(const wxImage
& img
) 
 525     // convert alpha channel to mask, if it is present: 
 527     image
.ConvertAlphaToMask(); 
 529     int width 
= image
.GetWidth(); 
 530     int height 
= image
.GetHeight(); 
 532     SetPixmap( gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 ) ); 
 534     // Create picture image 
 536     GdkGC
* data_gc 
= gdk_gc_new(M_BMPDATA
->m_pixmap
); 
 539     gdk_gc_set_foreground(data_gc
, &color
); 
 540     gdk_draw_rectangle(M_BMPDATA
->m_pixmap
, data_gc
, true, 0, 0, width
, height
); 
 541     GdkImage
* data_image 
= gdk_drawable_get_image(M_BMPDATA
->m_pixmap
, 0, 0, width
, height
); 
 545     GdkImage 
*mask_image 
= (GdkImage
*) NULL
; 
 546     GdkGC
* mask_gc 
= NULL
; 
 550         wxMask
* mask 
= new wxMask
; 
 551         mask
->m_bitmap 
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 ); 
 552         mask_gc 
= gdk_gc_new(mask
->m_bitmap
); 
 553         gdk_gc_set_foreground(mask_gc
, &color
); 
 554         gdk_draw_rectangle(mask
->m_bitmap
, mask_gc
, true, 0, 0, width
, height
); 
 555         mask_image 
= gdk_drawable_get_image(mask
->m_bitmap
, 0, 0, width
, height
); 
 560     int r_mask 
= image
.GetMaskRed(); 
 561     int g_mask 
= image
.GetMaskGreen(); 
 562     int b_mask 
= image
.GetMaskBlue(); 
 564     unsigned char* data 
= image
.GetData(); 
 567     for (int y 
= 0; y 
< height
; y
++) 
 569         for (int x 
= 0; x 
< width
; x
++) 
 578             if (mask_image 
!= NULL
) 
 580                 if ((r 
== r_mask
) && (b 
== b_mask
) && (g 
== g_mask
)) 
 581                     gdk_image_put_pixel( mask_image
, x
, y
, 0 ); 
 584             if ((r 
== 255) && (b 
== 255) && (g 
== 255)) 
 585                 gdk_image_put_pixel( data_image
, x
, y
, 0 ); 
 592     gdk_draw_image( GetPixmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height 
); 
 594     g_object_unref (data_image
); 
 595     g_object_unref (data_gc
); 
 599     if (mask_image 
!= NULL
) 
 601         gdk_draw_image( GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height 
); 
 603         g_object_unref (mask_image
); 
 604         g_object_unref (mask_gc
); 
 610 // conversion to colour bitmap: 
 611 bool wxBitmap::CreateFromImageAsPixmap(const wxImage
& image
) 
 613     // alpha is handled by CreateFromImageAsPixbuf 
 614     wxASSERT(!image
.HasAlpha()); 
 616     int width 
= image
.GetWidth(); 
 617     int height 
= image
.GetHeight(); 
 619     SetPixmap( gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, -1 ) ); 
 621     GdkGC 
*gc 
= gdk_gc_new( GetPixmap() ); 
 623     gdk_draw_rgb_image( GetPixmap(), 
 635     if (!image
.HasMask()) 
 638     wxMask
* mask 
= new wxMask
; 
 639     mask
->m_bitmap 
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 ); 
 640     GdkGC
* mask_gc 
= gdk_gc_new(mask
->m_bitmap
); 
 643     gdk_gc_set_foreground(mask_gc
, &color
); 
 644     gdk_draw_rectangle(mask
->m_bitmap
, mask_gc
, true, 0, 0, width
, height
); 
 645     GdkImage
* mask_image 
= gdk_drawable_get_image(mask
->m_bitmap
, 0, 0, width
, height
); 
 649     int r_mask 
= image
.GetMaskRed(); 
 650     int g_mask 
= image
.GetMaskGreen(); 
 651     int b_mask 
= image
.GetMaskBlue(); 
 653     unsigned char* data 
= image
.GetData(); 
 656     for (int y 
= 0; y 
< height
; y
++) 
 658         for (int x 
= 0; x 
< width
; x
++) 
 667             if ((r 
== r_mask
) && (b 
== b_mask
) && (g 
== g_mask
)) 
 668                 gdk_image_put_pixel( mask_image
, x
, y
, 0 ); 
 674     gdk_draw_image( GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height 
); 
 676     g_object_unref (mask_image
); 
 677     g_object_unref (mask_gc
); 
 682 bool wxBitmap::CreateFromImageAsPixbuf(const wxImage
& image
) 
 684     int width 
= image
.GetWidth(); 
 685     int height 
= image
.GetHeight(); 
 687     GdkPixbuf 
*pixbuf 
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
, 
 689                                        8 /* bits per sample */, 
 694     wxASSERT( image
.HasAlpha() ); // for now 
 695     wxASSERT( gdk_pixbuf_get_n_channels(pixbuf
) == 4 ); 
 696     wxASSERT( gdk_pixbuf_get_width(pixbuf
) == width 
); 
 697     wxASSERT( gdk_pixbuf_get_height(pixbuf
) == height 
); 
 699     SetDepth(wxTheApp
->GetGdkVisual()->depth
); 
 703     unsigned char *in 
= image
.GetData(); 
 704     unsigned char *out 
= gdk_pixbuf_get_pixels(pixbuf
); 
 705     unsigned char *alpha 
= image
.GetAlpha(); 
 707     int rowinc 
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
; 
 709     for (int y 
= 0; y 
< height
; y
++, out 
+= rowinc
) 
 711         for (int x 
= 0; x 
< width
; x
++, alpha
++, out 
+= 4, in 
+= 3) 
 723 wxImage 
wxBitmap::ConvertToImage() const 
 727     wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") ); 
 729     const int w 
= GetWidth(); 
 730     const int h 
= GetHeight(); 
 732     unsigned char *data 
= image
.GetData(); 
 734     wxCHECK_MSG(data 
!= NULL
, wxNullImage
, wxT("couldn't create image") ); 
 738         GdkPixbuf 
*pixbuf 
= GetPixbuf(); 
 739         wxASSERT( gdk_pixbuf_get_has_alpha(pixbuf
) ); 
 743         unsigned char *alpha 
= image
.GetAlpha(); 
 744         unsigned char *in 
= gdk_pixbuf_get_pixels(pixbuf
); 
 745         unsigned char *out 
= data
; 
 746         int rowinc 
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * w
; 
 748         for (int y 
= 0; y 
< h
; y
++, in 
+= rowinc
) 
 750             for (int x 
= 0; x 
< w
; x
++, in 
+= 4, out 
+= 3, alpha
++) 
 761         GdkPixmap
* pixmap 
= GetPixmap(); 
 762         GdkPixmap
* pixmap_invert 
= NULL
; 
 765             // mono bitmaps are inverted 
 766             pixmap_invert 
= gdk_pixmap_new(pixmap
, w
, h
, 1); 
 767             GdkGC
* gc 
= gdk_gc_new(pixmap_invert
); 
 768             gdk_gc_set_function(gc
, GDK_COPY_INVERT
); 
 769             gdk_draw_drawable(pixmap_invert
, gc
, pixmap
, 0, 0, 0, 0, w
, h
); 
 771             pixmap 
= pixmap_invert
; 
 773         // create a pixbuf which shares data with the wxImage 
 774         GdkPixbuf
* pixbuf 
= gdk_pixbuf_new_from_data( 
 775             data
, GDK_COLORSPACE_RGB
, false, 8, w
, h
, 3 * w
, NULL
, NULL
); 
 777         gdk_pixbuf_get_from_drawable(pixbuf
, pixmap
, NULL
, 0, 0, 0, 0, w
, h
); 
 779         g_object_unref(pixbuf
); 
 780         if (pixmap_invert 
!= NULL
) 
 781             g_object_unref(pixmap_invert
); 
 785             // the colour used as transparent one in wxImage and the one it is 
 786             // replaced with when it really occurs in the bitmap 
 787             const int MASK_RED 
= 1; 
 788             const int MASK_GREEN 
= 2; 
 789             const int MASK_BLUE 
= 3; 
 790             const int MASK_BLUE_REPLACEMENT 
= 2; 
 792             image
.SetMaskColour(MASK_RED
, MASK_GREEN
, MASK_BLUE
); 
 793             GdkImage
* image_mask 
= gdk_drawable_get_image(GetMask()->GetBitmap(), 0, 0, w
, h
); 
 795             for (int y 
= 0; y 
< h
; y
++) 
 797                 for (int x 
= 0; x 
< w
; x
++, data 
+= 3) 
 799                     if (gdk_image_get_pixel(image_mask
, x
, y
) == 0) 
 802                         data
[1] = MASK_GREEN
; 
 805                     else if (data
[0] == MASK_RED 
&& data
[1] == MASK_GREEN 
&& data
[2] == MASK_BLUE
) 
 807                         data
[2] = MASK_BLUE_REPLACEMENT
; 
 811             g_object_unref(image_mask
); 
 818 wxBitmap::wxBitmap( const wxString 
&filename
, wxBitmapType type 
) 
 820     LoadFile( filename
, type 
); 
 823 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int WXUNUSED(depth
)) 
 825     if ( width 
> 0 && height 
> 0 ) 
 827         SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window
, bits
, width
, height
)); 
 829         wxASSERT_MSG( M_BMPDATA
->m_pixmap
, wxT("couldn't create bitmap") ); 
 833 wxBitmap::~wxBitmap() 
 837 bool wxBitmap::operator == ( const wxBitmap
& bmp 
) const 
 839     return m_refData 
== bmp
.m_refData
; 
 842 bool wxBitmap::operator != ( const wxBitmap
& bmp 
) const 
 844     return m_refData 
!= bmp
.m_refData
; 
 847 bool wxBitmap::Ok() const 
 849     return (m_refData 
!= NULL
) && 
 851               M_BMPDATA
->m_pixbuf 
|| 
 856 int wxBitmap::GetHeight() const 
 858     wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); 
 860     return M_BMPDATA
->m_height
; 
 863 int wxBitmap::GetWidth() const 
 865     wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); 
 867     return M_BMPDATA
->m_width
; 
 870 int wxBitmap::GetDepth() const 
 872     wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); 
 874     return M_BMPDATA
->m_bpp
; 
 877 wxMask 
*wxBitmap::GetMask() const 
 879     wxCHECK_MSG( Ok(), (wxMask 
*) NULL
, wxT("invalid bitmap") ); 
 881     return M_BMPDATA
->m_mask
; 
 884 void wxBitmap::SetMask( wxMask 
*mask 
) 
 886     wxCHECK_RET( Ok(), wxT("invalid bitmap") ); 
 888     if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
; 
 890     M_BMPDATA
->m_mask 
= mask
; 
 893 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
) 
 899 wxBitmap 
wxBitmap::GetSubBitmap( const wxRect
& rect
) const 
 904                  (rect
.x 
>= 0) && (rect
.y 
>= 0) && 
 905                  (rect
.x
+rect
.width 
<= M_BMPDATA
->m_width
) && (rect
.y
+rect
.height 
<= M_BMPDATA
->m_height
), 
 906                  ret
, wxT("invalid bitmap or bitmap region") ); 
 910         GdkPixbuf 
*pixbuf 
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
, 
 911                                            gdk_pixbuf_get_has_alpha(GetPixbuf()), 
 912                                            8, rect
.width
, rect
.height
); 
 913         ret
.SetPixbuf(pixbuf
); 
 914         ret
.SetDepth(M_BMPDATA
->m_bpp
); 
 915         gdk_pixbuf_copy_area(GetPixbuf(), 
 916                              rect
.x
, rect
.y
, rect
.width
, rect
.height
, 
 921         ret 
= wxBitmap(rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
); 
 922         if (M_BMPDATA
->m_bpp 
!= 1) 
 924             GdkGC 
*gc 
= gdk_gc_new( ret
.GetPixmap() ); 
 925             gdk_draw_drawable( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height 
); 
 930             GdkGC 
*gc 
= gdk_gc_new( ret
.GetPixmap() ); 
 932             col
.pixel 
= 0xFFFFFF; 
 933             gdk_gc_set_foreground( gc
, &col 
); 
 935             gdk_gc_set_background( gc
, &col 
); 
 936             gdk_wx_draw_bitmap( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height 
); 
 943         wxMask 
*mask 
= new wxMask
; 
 944         mask
->m_bitmap 
= gdk_pixmap_new( wxGetRootWindow()->window
, rect
.width
, rect
.height
, 1 ); 
 946         GdkGC 
*gc 
= gdk_gc_new( mask
->m_bitmap 
); 
 948         col
.pixel 
= 0xFFFFFF; 
 949         gdk_gc_set_foreground( gc
, &col 
); 
 951         gdk_gc_set_background( gc
, &col 
); 
 952         gdk_wx_draw_bitmap( mask
->m_bitmap
, gc
, M_BMPDATA
->m_mask
->m_bitmap
, rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height 
); 
 961 bool wxBitmap::SaveFile( const wxString 
&name
, wxBitmapType type
, const wxPalette 
*WXUNUSED(palette
) ) const 
 963     wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") ); 
 965     // Try to save the bitmap via wxImage handlers: 
 966     wxImage image 
= ConvertToImage(); 
 967     return image
.Ok() && image
.SaveFile(name
, type
); 
 970 bool wxBitmap::LoadFile( const wxString 
&name
, wxBitmapType type 
) 
 974     if (type 
== wxBITMAP_TYPE_XPM
) 
 976         GdkBitmap 
*mask 
= (GdkBitmap
*) NULL
; 
 977         SetPixmap(gdk_pixmap_create_from_xpm(wxGetRootWindow()->window
, &mask
, NULL
, name
.fn_str())); 
 981             M_BMPDATA
->m_mask 
= new wxMask
; 
 982             M_BMPDATA
->m_mask
->m_bitmap 
= mask
; 
 985     else // try if wxImage can load it 
 988         if (image
.LoadFile(name
, type
) && image
.Ok()) 
 989             *this = wxBitmap(image
); 
 996 wxPalette 
*wxBitmap::GetPalette() const 
 999         return (wxPalette 
*) NULL
; 
1001     return M_BMPDATA
->m_palette
; 
1004 void wxBitmap::SetPalette(const wxPalette
& WXUNUSED(palette
)) 
1008 #endif // wxUSE_PALETTE 
1010 void wxBitmap::SetHeight( int height 
) 
1013         m_refData 
= new wxBitmapRefData
; 
1015     M_BMPDATA
->m_height 
= height
; 
1018 void wxBitmap::SetWidth( int width 
) 
1021         m_refData 
= new wxBitmapRefData
; 
1023     M_BMPDATA
->m_width 
= width
; 
1026 void wxBitmap::SetDepth( int depth 
) 
1029         m_refData 
= new wxBitmapRefData
; 
1031     M_BMPDATA
->m_bpp 
= depth
; 
1034 void wxBitmap::SetPixmap( GdkPixmap 
*pixmap 
) 
1037         m_refData 
= new wxBitmapRefData
; 
1039     wxASSERT(M_BMPDATA
->m_pixmap 
== NULL
); 
1040     M_BMPDATA
->m_pixmap 
= pixmap
; 
1041     gdk_drawable_get_size(pixmap
, &M_BMPDATA
->m_width
, &M_BMPDATA
->m_height
); 
1042     M_BMPDATA
->m_bpp 
= gdk_drawable_get_depth(pixmap
); 
1043     PurgeOtherRepresentations(Pixmap
); 
1046 GdkPixmap 
*wxBitmap::GetPixmap() const 
1048     wxCHECK_MSG( Ok(), (GdkPixmap 
*) NULL
, wxT("invalid bitmap") ); 
1050     // create the pixmap on the fly if we use Pixbuf representation: 
1051     if (M_BMPDATA
->m_pixmap 
== NULL
) 
1053         delete M_BMPDATA
->m_mask
; 
1054         M_BMPDATA
->m_mask 
= new wxMask
; 
1055         gdk_pixbuf_render_pixmap_and_mask(M_BMPDATA
->m_pixbuf
, 
1056                                           &M_BMPDATA
->m_pixmap
, 
1057                                           &M_BMPDATA
->m_mask
->m_bitmap
, 
1061     return M_BMPDATA
->m_pixmap
; 
1064 bool wxBitmap::HasPixmap() const 
1066     wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") ); 
1068     return M_BMPDATA
->m_pixmap 
!= NULL
; 
1071 GdkPixbuf 
*wxBitmap::GetPixbuf() const 
1073     wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") ); 
1075     if (M_BMPDATA
->m_pixbuf 
== NULL
) 
1077         int width 
= GetWidth(); 
1078         int height 
= GetHeight(); 
1080         GdkPixbuf 
*pixbuf 
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
, 
1083         M_BMPDATA
->m_pixbuf 
= 
1084             gdk_pixbuf_get_from_drawable(pixbuf
, M_BMPDATA
->m_pixmap
, NULL
, 
1085                                          0, 0, 0, 0, width
, height
); 
1087         // apply the mask to created pixbuf: 
1088         if (M_BMPDATA
->m_pixbuf 
&& M_BMPDATA
->m_mask
) 
1091                 gdk_pixbuf_get_from_drawable(NULL
, 
1092                                              M_BMPDATA
->m_mask
->GetBitmap(), 
1094                                              0, 0, 0, 0, width
, height
); 
1097                 guchar 
*bmp 
= gdk_pixbuf_get_pixels(pixbuf
); 
1098                 guchar 
*mask 
= gdk_pixbuf_get_pixels(pmask
); 
1099                 int bmprowinc 
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
; 
1100                 int maskrowinc 
= gdk_pixbuf_get_rowstride(pmask
) - 3 * width
; 
1102                 for (int y 
= 0; y 
< height
; 
1103                      y
++, bmp 
+= bmprowinc
, mask 
+= maskrowinc
) 
1105                     for (int x 
= 0; x 
< width
; x
++, bmp 
+= 4, mask 
+= 3) 
1107                         if (mask
[0] == 0 /*black pixel*/) 
1112                 g_object_unref (pmask
); 
1117     return M_BMPDATA
->m_pixbuf
; 
1120 bool wxBitmap::HasPixbuf() const 
1122     wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") ); 
1124     return M_BMPDATA
->m_pixbuf 
!= NULL
; 
1127 void wxBitmap::SetPixbuf( GdkPixbuf 
*pixbuf 
) 
1130         m_refData 
= new wxBitmapRefData
; 
1132     wxASSERT(M_BMPDATA
->m_pixbuf 
== NULL
); 
1133     M_BMPDATA
->m_pixbuf 
= pixbuf
; 
1134     M_BMPDATA
->m_width 
= gdk_pixbuf_get_width(pixbuf
); 
1135     M_BMPDATA
->m_height 
= gdk_pixbuf_get_height(pixbuf
); 
1136     PurgeOtherRepresentations(Pixbuf
); 
1139 void wxBitmap::PurgeOtherRepresentations(wxBitmap::Representation keep
) 
1141     if (keep 
== Pixmap 
&& HasPixbuf()) 
1143         g_object_unref (M_BMPDATA
->m_pixbuf
); 
1144         M_BMPDATA
->m_pixbuf 
= NULL
; 
1146     if (keep 
== Pixbuf 
&& HasPixmap()) 
1148         g_object_unref (M_BMPDATA
->m_pixmap
); 
1149         M_BMPDATA
->m_pixmap 
= NULL
; 
1153 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
) 
1158     GdkPixbuf 
*pixbuf 
= GetPixbuf(); 
1163     if (gdk_pixbuf_get_has_alpha( pixbuf 
)) 
1164         wxPrintf( wxT("Has alpha\n") ); 
1166         wxPrintf( wxT("No alpha.\n") ); 
1169     data
.m_height 
= gdk_pixbuf_get_height( pixbuf 
); 
1170     data
.m_width 
= gdk_pixbuf_get_width( pixbuf 
); 
1171     data
.m_stride 
= gdk_pixbuf_get_rowstride( pixbuf 
); 
1173     return gdk_pixbuf_get_pixels( pixbuf 
); 
1176 void wxBitmap::UngetRawData(wxPixelDataBase
& WXUNUSED(data
)) 
1181 bool wxBitmap::HasAlpha() const 
1186 void wxBitmap::UseAlpha() 
1191 //----------------------------------------------------------------------------- 
1193 //----------------------------------------------------------------------------- 
1195 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
,wxBitmapHandlerBase
) 
1197 wxBitmapHandler::~wxBitmapHandler() 
1201 bool wxBitmapHandler::Create(wxBitmap 
* WXUNUSED(bitmap
), 
1202                              void * WXUNUSED(data
), 
1203                              long WXUNUSED(type
), 
1204                              int WXUNUSED(width
), 
1205                              int WXUNUSED(height
), 
1206                              int WXUNUSED(depth
)) 
1208     wxFAIL_MSG( _T("not implemented") ); 
1213 bool wxBitmapHandler::LoadFile(wxBitmap 
* WXUNUSED(bitmap
), 
1214                                const wxString
& WXUNUSED(name
), 
1215                                long WXUNUSED(flags
), 
1216                                int WXUNUSED(desiredWidth
), 
1217                                int WXUNUSED(desiredHeight
)) 
1219     wxFAIL_MSG( _T("not implemented") ); 
1224 bool wxBitmapHandler::SaveFile(const wxBitmap 
* WXUNUSED(bitmap
), 
1225                                const wxString
& WXUNUSED(name
), 
1227                                const wxPalette 
* WXUNUSED(palette
)) 
1229     wxFAIL_MSG( _T("not implemented") ); 
1234 /* static */ void wxBitmap::InitStandardHandlers() 
1236     // TODO: Insert handler based on GdkPixbufs handler later