]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/bitmap.cpp
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"
26 #include "wx/gtk/private/object.h"
30 //-----------------------------------------------------------------------------
32 //-----------------------------------------------------------------------------
34 extern GtkWidget
*wxGetRootWindow();
36 //-----------------------------------------------------------------------------
38 //-----------------------------------------------------------------------------
40 IMPLEMENT_DYNAMIC_CLASS(wxMask
,wxObject
)
47 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
50 Create( bitmap
, colour
);
54 wxMask::wxMask( const wxBitmap
& bitmap
, int paletteIndex
)
57 Create( bitmap
, paletteIndex
);
59 #endif // wxUSE_PALETTE
61 wxMask::wxMask( const wxBitmap
& bitmap
)
70 g_object_unref (m_bitmap
);
73 bool wxMask::Create( const wxBitmap
& bitmap
,
74 const wxColour
& colour
)
78 g_object_unref (m_bitmap
);
82 const int w
= bitmap
.GetWidth();
83 const int h
= bitmap
.GetHeight();
85 // create mask as XBM format bitmap
87 // one bit per pixel, each row starts on a byte boundary
88 const size_t out_size
= size_t((w
+ 7) / 8) * unsigned(h
);
89 wxByte
* out
= new wxByte
[out_size
];
90 // set bits are unmasked
91 memset(out
, 0xff, out_size
);
92 unsigned bit_index
= 0;
93 if (bitmap
.HasPixbuf())
95 const wxByte r_mask
= colour
.Red();
96 const wxByte g_mask
= colour
.Green();
97 const wxByte b_mask
= colour
.Blue();
98 GdkPixbuf
* pixbuf
= bitmap
.GetPixbuf();
99 const wxByte
* in
= gdk_pixbuf_get_pixels(pixbuf
);
100 const int inc
= 3 + int(gdk_pixbuf_get_has_alpha(pixbuf
) != 0);
101 const int rowpadding
= gdk_pixbuf_get_rowstride(pixbuf
) - inc
* w
;
102 for (int y
= 0; y
< h
; y
++, in
+= rowpadding
)
104 for (int x
= 0; x
< w
; x
++, in
+= inc
, bit_index
++)
105 if (in
[0] == r_mask
&& in
[1] == g_mask
&& in
[2] == b_mask
)
106 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
107 // move index to next byte boundary
108 bit_index
= (bit_index
+ 7) & ~7u;
113 GdkImage
* image
= gdk_drawable_get_image(bitmap
.GetPixmap(), 0, 0, w
, h
);
114 GdkColormap
* colormap
= gdk_image_get_colormap(image
);
116 if (colormap
== NULL
)
117 // mono bitmap, white is pixel value 0
118 mask_pixel
= guint32(colour
.Red() != 255 || colour
.Green() != 255 || colour
.Blue() != 255);
122 c
.CalcPixel(colormap
);
123 mask_pixel
= c
.GetPixel();
125 for (int y
= 0; y
< h
; y
++)
127 for (int x
= 0; x
< w
; x
++, bit_index
++)
128 if (gdk_image_get_pixel(image
, x
, y
) == mask_pixel
)
129 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
130 bit_index
= (bit_index
+ 7) & ~7u;
132 g_object_unref(image
);
134 m_bitmap
= gdk_bitmap_create_from_data(wxGetRootWindow()->window
, (char*)out
, w
, h
);
140 bool wxMask::Create( const wxBitmap
& bitmap
, int paletteIndex
)
143 wxPalette
*pal
= bitmap
.GetPalette();
145 wxCHECK_MSG( pal
, false, wxT("Cannot create mask from bitmap without palette") );
147 pal
->GetRGB(paletteIndex
, &r
, &g
, &b
);
149 return Create(bitmap
, wxColour(r
, g
, b
));
151 #endif // wxUSE_PALETTE
153 bool wxMask::Create( const wxBitmap
& bitmap
)
157 g_object_unref (m_bitmap
);
161 if (!bitmap
.IsOk()) return false;
163 wxCHECK_MSG( bitmap
.GetDepth() == 1, false, wxT("Cannot create mask from colour bitmap") );
165 m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, bitmap
.GetWidth(), bitmap
.GetHeight(), 1 );
167 if (!m_bitmap
) return false;
169 wxGtkObject
<GdkGC
> gc(gdk_gc_new( m_bitmap
));
170 gdk_gc_set_function(gc
, GDK_COPY_INVERT
);
171 gdk_draw_drawable(m_bitmap
, gc
, bitmap
.GetPixmap(), 0, 0, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight());
176 GdkBitmap
*wxMask::GetBitmap() const
181 //-----------------------------------------------------------------------------
183 //-----------------------------------------------------------------------------
185 class wxBitmapRefData
: public wxGDIRefData
189 virtual ~wxBitmapRefData();
191 virtual bool IsOk() const { return m_pixmap
|| m_pixbuf
; }
200 wxPalette
*m_palette
;
201 #endif // wxUSE_PALETTE
204 wxBitmapRefData::wxBitmapRefData()
214 #endif // wxUSE_PALETTE
217 wxBitmapRefData::~wxBitmapRefData()
220 g_object_unref (m_pixmap
);
222 g_object_unref (m_pixbuf
);
226 #endif // wxUSE_PALETTE
230 //-----------------------------------------------------------------------------
232 //-----------------------------------------------------------------------------
234 #define M_BMPDATA static_cast<wxBitmapRefData*>(m_refData)
236 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
238 wxBitmap::wxBitmap(const wxString
&filename
, wxBitmapType type
)
240 LoadFile(filename
, type
);
243 wxBitmap::wxBitmap(const char bits
[], int width
, int height
, int depth
)
245 wxASSERT(depth
== 1);
246 if (width
> 0 && height
> 0 && depth
== 1)
248 SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window
, bits
, width
, height
));
252 wxBitmap::wxBitmap(const char* const* bits
)
254 wxCHECK2_MSG(bits
!= NULL
, return, wxT("invalid bitmap data"));
256 GdkBitmap
* mask
= NULL
;
257 SetPixmap(gdk_pixmap_create_from_xpm_d(wxGetRootWindow()->window
, &mask
, NULL
, const_cast<char**>(bits
)));
261 if (M_BMPDATA
->m_pixmap
!= NULL
&& mask
!= NULL
)
263 M_BMPDATA
->m_mask
= new wxMask
;
264 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
268 wxBitmap::~wxBitmap()
272 bool wxBitmap::Create( int width
, int height
, int depth
)
276 if ( width
<= 0 || height
<= 0 )
281 const GdkVisual
* visual
= wxTheApp
->GetGdkVisual();
285 SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB
, true, 8, width
, height
), 32);
290 // must initialize alpha, otherwise GetPixmap()
291 // will create a mask out of garbage
292 gdk_pixbuf_fill(M_BMPDATA
->m_pixbuf
, 0x000000ff);
294 else if (depth
== 24)
296 if (visual
->depth
== depth
)
297 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window
, width
, height
, depth
));
299 SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB
, false, 8, width
, height
), 24);
306 depth
= visual
->depth
;
308 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window
, width
, height
, depth
));
316 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
320 wxCHECK_MSG( image
.IsOk(), false, wxT("invalid image") );
321 wxCHECK_MSG( depth
== -1 || depth
== 1, false, wxT("invalid bitmap depth") );
323 if (image
.GetWidth() <= 0 || image
.GetHeight() <= 0)
326 // create pixbuf if image has alpha and requested depth is compatible
327 if (image
.HasAlpha() && (depth
== -1 || depth
== 32))
328 return CreateFromImageAsPixbuf(image
);
330 // otherwise create pixmap, if alpha is present it will be converted to mask
331 return CreateFromImageAsPixmap(image
, depth
);
334 bool wxBitmap::CreateFromImageAsPixmap(const wxImage
& image
, int depth
)
336 const int w
= image
.GetWidth();
337 const int h
= image
.GetHeight();
340 // create XBM format bitmap
342 // one bit per pixel, each row starts on a byte boundary
343 const size_t out_size
= size_t((w
+ 7) / 8) * unsigned(h
);
344 wxByte
* out
= new wxByte
[out_size
];
345 // set bits are black
346 memset(out
, 0xff, out_size
);
347 const wxByte
* in
= image
.GetData();
348 unsigned bit_index
= 0;
349 for (int y
= 0; y
< h
; y
++)
351 for (int x
= 0; x
< w
; x
++, in
+= 3, bit_index
++)
352 if (in
[0] == 255 && in
[1] == 255 && in
[2] == 255)
353 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
354 // move index to next byte boundary
355 bit_index
= (bit_index
+ 7) & ~7u;
357 SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window
, (char*)out
, w
, h
));
360 if (!M_BMPDATA
) // SetPixmap may have failed
365 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window
, w
, h
, depth
));
369 wxGtkObject
<GdkGC
> gc(gdk_gc_new(M_BMPDATA
->m_pixmap
));
371 M_BMPDATA
->m_pixmap
, gc
,
373 GDK_RGB_DITHER_NONE
, image
.GetData(), w
* 3);
376 const wxByte
* alpha
= image
.GetAlpha();
377 if (alpha
!= NULL
|| image
.HasMask())
379 // create mask as XBM format bitmap
381 const size_t out_size
= size_t((w
+ 7) / 8) * unsigned(h
);
382 wxByte
* out
= new wxByte
[out_size
];
383 memset(out
, 0xff, out_size
);
384 unsigned bit_index
= 0;
387 for (int y
= 0; y
< h
; y
++)
389 for (int x
= 0; x
< w
; x
++, bit_index
++)
390 if (*alpha
++ < wxIMAGE_ALPHA_THRESHOLD
)
391 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
392 bit_index
= (bit_index
+ 7) & ~7u;
397 const wxByte r_mask
= image
.GetMaskRed();
398 const wxByte g_mask
= image
.GetMaskGreen();
399 const wxByte b_mask
= image
.GetMaskBlue();
400 const wxByte
* in
= image
.GetData();
401 for (int y
= 0; y
< h
; y
++)
403 for (int x
= 0; x
< w
; x
++, in
+= 3, bit_index
++)
404 if (in
[0] == r_mask
&& in
[1] == g_mask
&& in
[2] == b_mask
)
405 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
406 bit_index
= (bit_index
+ 7) & ~7u;
409 wxMask
* mask
= new wxMask
;
410 mask
->m_bitmap
= gdk_bitmap_create_from_data(M_BMPDATA
->m_pixmap
, (char*)out
, w
, h
);
417 bool wxBitmap::CreateFromImageAsPixbuf(const wxImage
& image
)
419 wxASSERT(image
.HasAlpha());
421 int width
= image
.GetWidth();
422 int height
= image
.GetHeight();
424 Create(width
, height
, 32);
425 GdkPixbuf
* pixbuf
= M_BMPDATA
->m_pixbuf
;
430 const unsigned char* in
= image
.GetData();
431 unsigned char *out
= gdk_pixbuf_get_pixels(pixbuf
);
432 unsigned char *alpha
= image
.GetAlpha();
434 int rowpad
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
436 for (int y
= 0; y
< height
; y
++, out
+= rowpad
)
438 for (int x
= 0; x
< width
; x
++, alpha
++, out
+= 4, in
+= 3)
450 wxImage
wxBitmap::ConvertToImage() const
452 wxCHECK_MSG( IsOk(), wxNullImage
, wxT("invalid bitmap") );
454 const int w
= GetWidth();
455 const int h
= GetHeight();
456 wxImage
image(w
, h
, false);
457 unsigned char *data
= image
.GetData();
459 wxCHECK_MSG(data
!= NULL
, wxNullImage
, wxT("couldn't create image") );
461 // prefer pixbuf if available, it will preserve alpha and should be quicker
464 GdkPixbuf
*pixbuf
= GetPixbuf();
465 unsigned char* alpha
= NULL
;
466 if (gdk_pixbuf_get_has_alpha(pixbuf
))
469 alpha
= image
.GetAlpha();
471 const unsigned char* in
= gdk_pixbuf_get_pixels(pixbuf
);
472 unsigned char *out
= data
;
473 const int inc
= 3 + int(alpha
!= NULL
);
474 const int rowpad
= gdk_pixbuf_get_rowstride(pixbuf
) - inc
* w
;
476 for (int y
= 0; y
< h
; y
++, in
+= rowpad
)
478 for (int x
= 0; x
< w
; x
++, in
+= inc
, out
+= 3)
490 GdkPixmap
* pixmap
= GetPixmap();
491 GdkPixmap
* pixmap_invert
= NULL
;
494 // mono bitmaps are inverted, i.e. 0 is white
495 pixmap_invert
= gdk_pixmap_new(pixmap
, w
, h
, 1);
496 wxGtkObject
<GdkGC
> gc(gdk_gc_new(pixmap_invert
));
497 gdk_gc_set_function(gc
, GDK_COPY_INVERT
);
498 gdk_draw_drawable(pixmap_invert
, gc
, pixmap
, 0, 0, 0, 0, w
, h
);
499 pixmap
= pixmap_invert
;
501 // create a pixbuf which shares data with the wxImage
502 GdkPixbuf
* pixbuf
= gdk_pixbuf_new_from_data(
503 data
, GDK_COLORSPACE_RGB
, false, 8, w
, h
, 3 * w
, NULL
, NULL
);
505 gdk_pixbuf_get_from_drawable(pixbuf
, pixmap
, NULL
, 0, 0, 0, 0, w
, h
);
507 g_object_unref(pixbuf
);
508 if (pixmap_invert
!= NULL
)
509 g_object_unref(pixmap_invert
);
511 // convert mask, unless there is already alpha
512 if (GetMask() && !image
.HasAlpha())
514 // we hard code the mask colour for now but we could also make an
515 // effort (and waste time) to choose a colour not present in the
516 // image already to avoid having to fudge the pixels below --
517 // whether it's worth to do it is unclear however
518 const int MASK_RED
= 1;
519 const int MASK_GREEN
= 2;
520 const int MASK_BLUE
= 3;
521 const int MASK_BLUE_REPLACEMENT
= 2;
523 image
.SetMaskColour(MASK_RED
, MASK_GREEN
, MASK_BLUE
);
524 GdkImage
* image_mask
= gdk_drawable_get_image(GetMask()->GetBitmap(), 0, 0, w
, h
);
526 for (int y
= 0; y
< h
; y
++)
528 for (int x
= 0; x
< w
; x
++, data
+= 3)
530 if (gdk_image_get_pixel(image_mask
, x
, y
) == 0)
533 data
[1] = MASK_GREEN
;
536 else if (data
[0] == MASK_RED
&& data
[1] == MASK_GREEN
&& data
[2] == MASK_BLUE
)
538 // we have to fudge the colour a bit to prevent
539 // this pixel from appearing transparent
540 data
[2] = MASK_BLUE_REPLACEMENT
;
544 g_object_unref(image_mask
);
550 #endif // wxUSE_IMAGE
552 int wxBitmap::GetHeight() const
554 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
556 return M_BMPDATA
->m_height
;
559 int wxBitmap::GetWidth() const
561 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
563 return M_BMPDATA
->m_width
;
566 int wxBitmap::GetDepth() const
568 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
570 return M_BMPDATA
->m_bpp
;
573 wxMask
*wxBitmap::GetMask() const
575 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") );
577 return M_BMPDATA
->m_mask
;
580 void wxBitmap::SetMask( wxMask
*mask
)
582 wxCHECK_RET( IsOk(), wxT("invalid bitmap") );
585 delete M_BMPDATA
->m_mask
;
586 M_BMPDATA
->m_mask
= mask
;
589 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
595 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
599 wxCHECK_MSG(IsOk(), ret
, wxT("invalid bitmap"));
600 wxCHECK_MSG(rect
.x
>= 0 && rect
.y
>= 0 &&
601 rect
.x
+ rect
.width
<= M_BMPDATA
->m_width
&&
602 rect
.y
+ rect
.height
<= M_BMPDATA
->m_height
,
603 ret
, wxT("invalid bitmap region"));
605 if (HasPixbuf() || M_BMPDATA
->m_bpp
== 32)
607 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
608 gdk_pixbuf_get_has_alpha(GetPixbuf()),
609 8, rect
.width
, rect
.height
);
613 ret
.SetPixbuf(pixbuf
, M_BMPDATA
->m_bpp
);
614 gdk_pixbuf_copy_area(GetPixbuf(),
615 rect
.x
, rect
.y
, rect
.width
, rect
.height
,
620 ret
.Create(rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
621 wxGtkObject
<GdkGC
> gc(gdk_gc_new( ret
.GetPixmap() ));
622 gdk_draw_drawable( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
624 // make mask, unless there is already alpha
625 if (GetMask() && !HasAlpha())
627 wxMask
*mask
= new wxMask
;
628 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, rect
.width
, rect
.height
, 1 );
630 wxGtkObject
<GdkGC
> gc(gdk_gc_new( mask
->m_bitmap
));
631 gdk_draw_drawable(mask
->m_bitmap
, gc
, M_BMPDATA
->m_mask
->m_bitmap
, rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
639 bool wxBitmap::SaveFile( const wxString
&name
, wxBitmapType type
, const wxPalette
*WXUNUSED(palette
) ) const
641 wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") );
644 // Try to save the bitmap via wxImage handlers:
645 wxImage image
= ConvertToImage();
646 return image
.Ok() && image
.SaveFile(name
, type
);
647 #else // !wxUSE_IMAGE
652 #endif // wxUSE_IMAGE
655 bool wxBitmap::LoadFile( const wxString
&name
, wxBitmapType type
)
659 if (type
== wxBITMAP_TYPE_XPM
)
661 GdkBitmap
*mask
= NULL
;
662 SetPixmap(gdk_pixmap_create_from_xpm(wxGetRootWindow()->window
, &mask
, NULL
, name
.fn_str()));
664 return false; // do not set the mask
668 M_BMPDATA
->m_mask
= new wxMask
;
669 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
673 else // try if wxImage can load it
676 if (image
.LoadFile(name
, type
) && image
.Ok())
677 CreateFromImage(image
, -1);
679 #endif // wxUSE_IMAGE
685 wxPalette
*wxBitmap::GetPalette() const
687 wxCHECK_MSG(IsOk(), NULL
, wxT("invalid bitmap"));
689 return M_BMPDATA
->m_palette
;
692 void wxBitmap::SetPalette(const wxPalette
& WXUNUSED(palette
))
696 #endif // wxUSE_PALETTE
698 void wxBitmap::SetHeight( int height
)
701 M_BMPDATA
->m_height
= height
;
704 void wxBitmap::SetWidth( int width
)
707 M_BMPDATA
->m_width
= width
;
710 void wxBitmap::SetDepth( int depth
)
713 M_BMPDATA
->m_bpp
= depth
;
716 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
722 m_refData
= new wxBitmapRefData
;
724 // AllocExclusive should not be needed for this internal function
725 wxASSERT(m_refData
->GetRefCount() == 1);
726 wxASSERT(M_BMPDATA
->m_pixmap
== NULL
);
727 M_BMPDATA
->m_pixmap
= pixmap
;
728 gdk_drawable_get_size(pixmap
, &M_BMPDATA
->m_width
, &M_BMPDATA
->m_height
);
729 M_BMPDATA
->m_bpp
= gdk_drawable_get_depth(pixmap
);
730 PurgeOtherRepresentations(Pixmap
);
733 GdkPixmap
*wxBitmap::GetPixmap() const
735 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") );
737 // create the pixmap on the fly if we use Pixbuf representation:
738 if (M_BMPDATA
->m_pixmap
== NULL
)
740 GdkPixmap
** pmask
= NULL
;
741 if (gdk_pixbuf_get_has_alpha(M_BMPDATA
->m_pixbuf
))
743 // make new mask from alpha
744 delete M_BMPDATA
->m_mask
;
745 M_BMPDATA
->m_mask
= new wxMask
;
746 pmask
= &M_BMPDATA
->m_mask
->m_bitmap
;
748 gdk_pixbuf_render_pixmap_and_mask(M_BMPDATA
->m_pixbuf
,
749 &M_BMPDATA
->m_pixmap
,
751 0x80 /* alpha threshold */);
754 return M_BMPDATA
->m_pixmap
;
757 bool wxBitmap::HasPixmap() const
759 wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") );
761 return M_BMPDATA
->m_pixmap
!= NULL
;
764 GdkPixbuf
*wxBitmap::GetPixbuf() const
766 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") );
768 if (M_BMPDATA
->m_pixbuf
== NULL
)
771 int width
= GetWidth();
772 int height
= GetHeight();
774 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
777 M_BMPDATA
->m_pixbuf
= pixbuf
;
778 gdk_pixbuf_get_from_drawable(pixbuf
, M_BMPDATA
->m_pixmap
, NULL
,
779 0, 0, 0, 0, width
, height
);
780 // apply the mask to created pixbuf:
781 if (M_BMPDATA
->m_pixbuf
&& M_BMPDATA
->m_mask
)
784 gdk_pixbuf_get_from_drawable(NULL
,
785 M_BMPDATA
->m_mask
->GetBitmap(),
787 0, 0, 0, 0, width
, height
);
790 guchar
*bmp
= gdk_pixbuf_get_pixels(pixbuf
);
791 guchar
*mask
= gdk_pixbuf_get_pixels(pmask
);
792 int bmprowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
793 int maskrowinc
= gdk_pixbuf_get_rowstride(pmask
) - 3 * width
;
795 for (int y
= 0; y
< height
;
796 y
++, bmp
+= bmprowinc
, mask
+= maskrowinc
)
798 for (int x
= 0; x
< width
; x
++, bmp
+= 4, mask
+= 3)
800 if (mask
[0] == 0 /*black pixel*/)
805 g_object_unref (pmask
);
810 return M_BMPDATA
->m_pixbuf
;
813 bool wxBitmap::HasPixbuf() const
815 wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") );
817 return M_BMPDATA
->m_pixbuf
!= NULL
;
820 void wxBitmap::SetPixbuf(GdkPixbuf
* pixbuf
, int depth
)
826 m_refData
= new wxBitmapRefData
;
828 // AllocExclusive should not be needed for this internal function
829 wxASSERT(m_refData
->GetRefCount() == 1);
830 wxASSERT(M_BMPDATA
->m_pixbuf
== NULL
);
831 M_BMPDATA
->m_pixbuf
= pixbuf
;
832 M_BMPDATA
->m_width
= gdk_pixbuf_get_width(pixbuf
);
833 M_BMPDATA
->m_height
= gdk_pixbuf_get_height(pixbuf
);
834 // if depth specified
836 M_BMPDATA
->m_bpp
= depth
;
837 else if (M_BMPDATA
->m_bpp
== 0)
838 // use something reasonable
839 M_BMPDATA
->m_bpp
= wxTheApp
->GetGdkVisual()->depth
;
840 PurgeOtherRepresentations(Pixbuf
);
843 void wxBitmap::PurgeOtherRepresentations(wxBitmap::Representation keep
)
845 if (keep
== Pixmap
&& HasPixbuf())
847 g_object_unref (M_BMPDATA
->m_pixbuf
);
848 M_BMPDATA
->m_pixbuf
= NULL
;
850 if (keep
== Pixbuf
&& HasPixmap())
852 g_object_unref (M_BMPDATA
->m_pixmap
);
853 M_BMPDATA
->m_pixmap
= NULL
;
857 #ifdef wxHAS_RAW_BITMAP
858 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
861 GdkPixbuf
*pixbuf
= GetPixbuf();
862 const bool hasAlpha
= HasAlpha();
864 // allow access if bpp is valid and matches existence of alpha
865 if ( pixbuf
&& ((bpp
== 24 && !hasAlpha
) || (bpp
== 32 && hasAlpha
)) )
867 data
.m_height
= gdk_pixbuf_get_height( pixbuf
);
868 data
.m_width
= gdk_pixbuf_get_width( pixbuf
);
869 data
.m_stride
= gdk_pixbuf_get_rowstride( pixbuf
);
870 bits
= gdk_pixbuf_get_pixels(pixbuf
);
875 void wxBitmap::UngetRawData(wxPixelDataBase
& WXUNUSED(data
))
878 #endif // wxHAS_RAW_BITMAP
880 bool wxBitmap::HasAlpha() const
882 return m_refData
!= NULL
&& M_BMPDATA
->m_pixbuf
!= NULL
&&
883 gdk_pixbuf_get_has_alpha(M_BMPDATA
->m_pixbuf
);
886 wxGDIRefData
* wxBitmap::CreateGDIRefData() const
888 return new wxBitmapRefData
;
891 wxGDIRefData
* wxBitmap::CloneGDIRefData(const wxGDIRefData
* data
) const
893 const wxBitmapRefData
* oldRef
= static_cast<const wxBitmapRefData
*>(data
);
894 wxBitmapRefData
* newRef
= new wxBitmapRefData
;
895 newRef
->m_width
= oldRef
->m_width
;
896 newRef
->m_height
= oldRef
->m_height
;
897 newRef
->m_bpp
= oldRef
->m_bpp
;
898 if (oldRef
->m_pixmap
!= NULL
)
900 newRef
->m_pixmap
= gdk_pixmap_new(
901 oldRef
->m_pixmap
, oldRef
->m_width
, oldRef
->m_height
,
902 // use pixmap depth, m_bpp may not match
903 gdk_drawable_get_depth(oldRef
->m_pixmap
));
904 wxGtkObject
<GdkGC
> gc(gdk_gc_new(newRef
->m_pixmap
));
906 newRef
->m_pixmap
, gc
, oldRef
->m_pixmap
, 0, 0, 0, 0, -1, -1);
908 if (oldRef
->m_pixbuf
!= NULL
)
910 newRef
->m_pixbuf
= gdk_pixbuf_copy(oldRef
->m_pixbuf
);
912 if (oldRef
->m_mask
!= NULL
)
914 newRef
->m_mask
= new wxMask
;
915 newRef
->m_mask
->m_bitmap
= gdk_pixmap_new(
916 oldRef
->m_mask
->m_bitmap
, oldRef
->m_width
, oldRef
->m_height
, 1);
917 wxGtkObject
<GdkGC
> gc(gdk_gc_new(newRef
->m_mask
->m_bitmap
));
918 gdk_draw_drawable(newRef
->m_mask
->m_bitmap
,
919 gc
, oldRef
->m_mask
->m_bitmap
, 0, 0, 0, 0, -1, -1);
922 // implement this if SetPalette is ever implemented
923 wxASSERT(oldRef
->m_palette
== NULL
);
929 /* static */ void wxBitmap::InitStandardHandlers()
931 // TODO: Insert handler based on GdkPixbufs handler later