]>
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
229 //-----------------------------------------------------------------------------
231 #define M_BMPDATA static_cast<wxBitmapRefData*>(m_refData)
233 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
235 wxBitmap::wxBitmap(int width
, int height
, int depth
)
237 Create(width
, height
, depth
);
240 wxBitmap::wxBitmap(const wxString
&filename
, wxBitmapType type
)
242 LoadFile(filename
, type
);
245 wxBitmap::wxBitmap(const char bits
[], int width
, int height
, int depth
)
247 wxASSERT(depth
== 1);
248 if (width
> 0 && height
> 0 && depth
== 1)
250 SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window
, bits
, width
, height
));
254 wxBitmap::wxBitmap(const char* const* bits
)
256 wxCHECK2_MSG(bits
!= NULL
, return, wxT("invalid bitmap data"));
258 GdkBitmap
* mask
= NULL
;
259 SetPixmap(gdk_pixmap_create_from_xpm_d(wxGetRootWindow()->window
, &mask
, NULL
, const_cast<char**>(bits
)));
263 if (M_BMPDATA
->m_pixmap
!= NULL
&& mask
!= NULL
)
265 M_BMPDATA
->m_mask
= new wxMask
;
266 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
270 wxBitmap::~wxBitmap()
274 bool wxBitmap::Create( int width
, int height
, int depth
)
278 if ( width
<= 0 || height
<= 0 )
283 const GdkVisual
* visual
= wxTheApp
->GetGdkVisual();
287 SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB
, true, 8, width
, height
), 32);
292 // must initialize alpha, otherwise GetPixmap()
293 // will create a mask out of garbage
294 gdk_pixbuf_fill(M_BMPDATA
->m_pixbuf
, 0x000000ff);
296 else if (depth
== 24)
298 if (visual
->depth
== depth
)
299 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window
, width
, height
, depth
));
301 SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB
, false, 8, width
, height
), 24);
308 depth
= visual
->depth
;
310 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window
, width
, height
, depth
));
318 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
322 wxCHECK_MSG( image
.IsOk(), false, wxT("invalid image") );
323 wxCHECK_MSG( depth
== -1 || depth
== 1, false, wxT("invalid bitmap depth") );
325 if (image
.GetWidth() <= 0 || image
.GetHeight() <= 0)
328 // create pixbuf if image has alpha and requested depth is compatible
329 if (image
.HasAlpha() && (depth
== -1 || depth
== 32))
330 return CreateFromImageAsPixbuf(image
);
332 // otherwise create pixmap, if alpha is present it will be converted to mask
333 return CreateFromImageAsPixmap(image
, depth
);
336 bool wxBitmap::CreateFromImageAsPixmap(const wxImage
& image
, int depth
)
338 const int w
= image
.GetWidth();
339 const int h
= image
.GetHeight();
342 // create XBM format bitmap
344 // one bit per pixel, each row starts on a byte boundary
345 const size_t out_size
= size_t((w
+ 7) / 8) * unsigned(h
);
346 wxByte
* out
= new wxByte
[out_size
];
347 // set bits are black
348 memset(out
, 0xff, out_size
);
349 const wxByte
* in
= image
.GetData();
350 unsigned bit_index
= 0;
351 for (int y
= 0; y
< h
; y
++)
353 for (int x
= 0; x
< w
; x
++, in
+= 3, bit_index
++)
354 if (in
[0] == 255 && in
[1] == 255 && in
[2] == 255)
355 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
356 // move index to next byte boundary
357 bit_index
= (bit_index
+ 7) & ~7u;
359 SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window
, (char*)out
, w
, h
));
362 if (!M_BMPDATA
) // SetPixmap may have failed
367 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window
, w
, h
, depth
));
371 wxGtkObject
<GdkGC
> gc(gdk_gc_new(M_BMPDATA
->m_pixmap
));
373 M_BMPDATA
->m_pixmap
, gc
,
375 GDK_RGB_DITHER_NONE
, image
.GetData(), w
* 3);
378 const wxByte
* alpha
= image
.GetAlpha();
379 if (alpha
!= NULL
|| image
.HasMask())
381 // create mask as XBM format bitmap
383 const size_t out_size
= size_t((w
+ 7) / 8) * unsigned(h
);
384 wxByte
* out
= new wxByte
[out_size
];
385 memset(out
, 0xff, out_size
);
386 unsigned bit_index
= 0;
389 for (int y
= 0; y
< h
; y
++)
391 for (int x
= 0; x
< w
; x
++, bit_index
++)
392 if (*alpha
++ < wxIMAGE_ALPHA_THRESHOLD
)
393 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
394 bit_index
= (bit_index
+ 7) & ~7u;
399 const wxByte r_mask
= image
.GetMaskRed();
400 const wxByte g_mask
= image
.GetMaskGreen();
401 const wxByte b_mask
= image
.GetMaskBlue();
402 const wxByte
* in
= image
.GetData();
403 for (int y
= 0; y
< h
; y
++)
405 for (int x
= 0; x
< w
; x
++, in
+= 3, bit_index
++)
406 if (in
[0] == r_mask
&& in
[1] == g_mask
&& in
[2] == b_mask
)
407 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
408 bit_index
= (bit_index
+ 7) & ~7u;
411 wxMask
* mask
= new wxMask
;
412 mask
->m_bitmap
= gdk_bitmap_create_from_data(M_BMPDATA
->m_pixmap
, (char*)out
, w
, h
);
419 bool wxBitmap::CreateFromImageAsPixbuf(const wxImage
& image
)
421 wxASSERT(image
.HasAlpha());
423 int width
= image
.GetWidth();
424 int height
= image
.GetHeight();
426 Create(width
, height
, 32);
427 GdkPixbuf
* pixbuf
= M_BMPDATA
->m_pixbuf
;
432 const unsigned char* in
= image
.GetData();
433 unsigned char *out
= gdk_pixbuf_get_pixels(pixbuf
);
434 unsigned char *alpha
= image
.GetAlpha();
436 int rowpad
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
438 for (int y
= 0; y
< height
; y
++, out
+= rowpad
)
440 for (int x
= 0; x
< width
; x
++, alpha
++, out
+= 4, in
+= 3)
452 wxImage
wxBitmap::ConvertToImage() const
454 wxCHECK_MSG( IsOk(), wxNullImage
, wxT("invalid bitmap") );
456 const int w
= GetWidth();
457 const int h
= GetHeight();
458 wxImage
image(w
, h
, false);
459 unsigned char *data
= image
.GetData();
461 wxCHECK_MSG(data
!= NULL
, wxNullImage
, wxT("couldn't create image") );
463 // prefer pixbuf if available, it will preserve alpha and should be quicker
466 GdkPixbuf
*pixbuf
= GetPixbuf();
467 unsigned char* alpha
= NULL
;
468 if (gdk_pixbuf_get_has_alpha(pixbuf
))
471 alpha
= image
.GetAlpha();
473 const unsigned char* in
= gdk_pixbuf_get_pixels(pixbuf
);
474 unsigned char *out
= data
;
475 const int inc
= 3 + int(alpha
!= NULL
);
476 const int rowpad
= gdk_pixbuf_get_rowstride(pixbuf
) - inc
* w
;
478 for (int y
= 0; y
< h
; y
++, in
+= rowpad
)
480 for (int x
= 0; x
< w
; x
++, in
+= inc
, out
+= 3)
492 GdkPixmap
* pixmap
= GetPixmap();
493 GdkPixmap
* pixmap_invert
= NULL
;
496 // mono bitmaps are inverted, i.e. 0 is white
497 pixmap_invert
= gdk_pixmap_new(pixmap
, w
, h
, 1);
498 wxGtkObject
<GdkGC
> gc(gdk_gc_new(pixmap_invert
));
499 gdk_gc_set_function(gc
, GDK_COPY_INVERT
);
500 gdk_draw_drawable(pixmap_invert
, gc
, pixmap
, 0, 0, 0, 0, w
, h
);
501 pixmap
= pixmap_invert
;
503 // create a pixbuf which shares data with the wxImage
504 GdkPixbuf
* pixbuf
= gdk_pixbuf_new_from_data(
505 data
, GDK_COLORSPACE_RGB
, false, 8, w
, h
, 3 * w
, NULL
, NULL
);
507 gdk_pixbuf_get_from_drawable(pixbuf
, pixmap
, NULL
, 0, 0, 0, 0, w
, h
);
509 g_object_unref(pixbuf
);
510 if (pixmap_invert
!= NULL
)
511 g_object_unref(pixmap_invert
);
513 // convert mask, unless there is already alpha
514 if (GetMask() && !image
.HasAlpha())
516 // we hard code the mask colour for now but we could also make an
517 // effort (and waste time) to choose a colour not present in the
518 // image already to avoid having to fudge the pixels below --
519 // whether it's worth to do it is unclear however
520 const int MASK_RED
= 1;
521 const int MASK_GREEN
= 2;
522 const int MASK_BLUE
= 3;
523 const int MASK_BLUE_REPLACEMENT
= 2;
525 image
.SetMaskColour(MASK_RED
, MASK_GREEN
, MASK_BLUE
);
526 GdkImage
* image_mask
= gdk_drawable_get_image(GetMask()->GetBitmap(), 0, 0, w
, h
);
528 for (int y
= 0; y
< h
; y
++)
530 for (int x
= 0; x
< w
; x
++, data
+= 3)
532 if (gdk_image_get_pixel(image_mask
, x
, y
) == 0)
535 data
[1] = MASK_GREEN
;
538 else if (data
[0] == MASK_RED
&& data
[1] == MASK_GREEN
&& data
[2] == MASK_BLUE
)
540 // we have to fudge the colour a bit to prevent
541 // this pixel from appearing transparent
542 data
[2] = MASK_BLUE_REPLACEMENT
;
546 g_object_unref(image_mask
);
552 #endif // wxUSE_IMAGE
554 int wxBitmap::GetHeight() const
556 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
558 return M_BMPDATA
->m_height
;
561 int wxBitmap::GetWidth() const
563 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
565 return M_BMPDATA
->m_width
;
568 int wxBitmap::GetDepth() const
570 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
572 return M_BMPDATA
->m_bpp
;
575 wxMask
*wxBitmap::GetMask() const
577 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") );
579 return M_BMPDATA
->m_mask
;
582 void wxBitmap::SetMask( wxMask
*mask
)
584 wxCHECK_RET( IsOk(), wxT("invalid bitmap") );
587 delete M_BMPDATA
->m_mask
;
588 M_BMPDATA
->m_mask
= mask
;
591 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
597 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
601 wxCHECK_MSG(IsOk(), ret
, wxT("invalid bitmap"));
602 wxCHECK_MSG(rect
.x
>= 0 && rect
.y
>= 0 &&
603 rect
.x
+ rect
.width
<= M_BMPDATA
->m_width
&&
604 rect
.y
+ rect
.height
<= M_BMPDATA
->m_height
,
605 ret
, wxT("invalid bitmap region"));
607 if (HasPixbuf() || M_BMPDATA
->m_bpp
== 32)
609 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
610 gdk_pixbuf_get_has_alpha(GetPixbuf()),
611 8, rect
.width
, rect
.height
);
615 ret
.SetPixbuf(pixbuf
, M_BMPDATA
->m_bpp
);
616 gdk_pixbuf_copy_area(GetPixbuf(),
617 rect
.x
, rect
.y
, rect
.width
, rect
.height
,
622 ret
.Create(rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
623 wxGtkObject
<GdkGC
> gc(gdk_gc_new( ret
.GetPixmap() ));
624 gdk_draw_drawable( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
626 // make mask, unless there is already alpha
627 if (GetMask() && !HasAlpha())
629 wxMask
*mask
= new wxMask
;
630 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, rect
.width
, rect
.height
, 1 );
632 wxGtkObject
<GdkGC
> gc(gdk_gc_new( mask
->m_bitmap
));
633 gdk_draw_drawable(mask
->m_bitmap
, gc
, M_BMPDATA
->m_mask
->m_bitmap
, rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
641 bool wxBitmap::SaveFile( const wxString
&name
, wxBitmapType type
, const wxPalette
*WXUNUSED(palette
) ) const
643 wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") );
646 // Try to save the bitmap via wxImage handlers:
647 wxImage image
= ConvertToImage();
648 return image
.Ok() && image
.SaveFile(name
, type
);
649 #else // !wxUSE_IMAGE
654 #endif // wxUSE_IMAGE
657 bool wxBitmap::LoadFile( const wxString
&name
, wxBitmapType type
)
661 if (type
== wxBITMAP_TYPE_XPM
)
663 GdkBitmap
*mask
= NULL
;
664 SetPixmap(gdk_pixmap_create_from_xpm(wxGetRootWindow()->window
, &mask
, NULL
, name
.fn_str()));
666 return false; // do not set the mask
670 M_BMPDATA
->m_mask
= new wxMask
;
671 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
675 else // try if wxImage can load it
678 if (image
.LoadFile(name
, type
) && image
.Ok())
679 CreateFromImage(image
, -1);
681 #endif // wxUSE_IMAGE
687 wxPalette
*wxBitmap::GetPalette() const
689 wxCHECK_MSG(IsOk(), NULL
, wxT("invalid bitmap"));
691 return M_BMPDATA
->m_palette
;
694 void wxBitmap::SetPalette(const wxPalette
& WXUNUSED(palette
))
698 #endif // wxUSE_PALETTE
700 void wxBitmap::SetHeight( int height
)
703 M_BMPDATA
->m_height
= height
;
706 void wxBitmap::SetWidth( int width
)
709 M_BMPDATA
->m_width
= width
;
712 void wxBitmap::SetDepth( int depth
)
715 M_BMPDATA
->m_bpp
= depth
;
718 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
724 m_refData
= new wxBitmapRefData
;
726 // AllocExclusive should not be needed for this internal function
727 wxASSERT(m_refData
->GetRefCount() == 1);
728 wxASSERT(M_BMPDATA
->m_pixmap
== NULL
);
729 M_BMPDATA
->m_pixmap
= pixmap
;
730 gdk_drawable_get_size(pixmap
, &M_BMPDATA
->m_width
, &M_BMPDATA
->m_height
);
731 M_BMPDATA
->m_bpp
= gdk_drawable_get_depth(pixmap
);
732 PurgeOtherRepresentations(Pixmap
);
735 GdkPixmap
*wxBitmap::GetPixmap() const
737 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") );
739 // create the pixmap on the fly if we use Pixbuf representation:
740 if (M_BMPDATA
->m_pixmap
== NULL
)
742 GdkPixmap
** pmask
= NULL
;
743 if (gdk_pixbuf_get_has_alpha(M_BMPDATA
->m_pixbuf
))
745 // make new mask from alpha
746 delete M_BMPDATA
->m_mask
;
747 M_BMPDATA
->m_mask
= new wxMask
;
748 pmask
= &M_BMPDATA
->m_mask
->m_bitmap
;
750 gdk_pixbuf_render_pixmap_and_mask(M_BMPDATA
->m_pixbuf
,
751 &M_BMPDATA
->m_pixmap
,
753 0x80 /* alpha threshold */);
756 return M_BMPDATA
->m_pixmap
;
759 bool wxBitmap::HasPixmap() const
761 wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") );
763 return M_BMPDATA
->m_pixmap
!= NULL
;
766 GdkPixbuf
*wxBitmap::GetPixbuf() const
768 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") );
770 if (M_BMPDATA
->m_pixbuf
== NULL
)
773 int width
= GetWidth();
774 int height
= GetHeight();
776 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
779 M_BMPDATA
->m_pixbuf
= pixbuf
;
780 gdk_pixbuf_get_from_drawable(pixbuf
, M_BMPDATA
->m_pixmap
, NULL
,
781 0, 0, 0, 0, width
, height
);
782 // apply the mask to created pixbuf:
783 if (M_BMPDATA
->m_pixbuf
&& M_BMPDATA
->m_mask
)
786 gdk_pixbuf_get_from_drawable(NULL
,
787 M_BMPDATA
->m_mask
->GetBitmap(),
789 0, 0, 0, 0, width
, height
);
792 guchar
*bmp
= gdk_pixbuf_get_pixels(pixbuf
);
793 guchar
*mask
= gdk_pixbuf_get_pixels(pmask
);
794 int bmprowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
795 int maskrowinc
= gdk_pixbuf_get_rowstride(pmask
) - 3 * width
;
797 for (int y
= 0; y
< height
;
798 y
++, bmp
+= bmprowinc
, mask
+= maskrowinc
)
800 for (int x
= 0; x
< width
; x
++, bmp
+= 4, mask
+= 3)
802 if (mask
[0] == 0 /*black pixel*/)
807 g_object_unref (pmask
);
812 return M_BMPDATA
->m_pixbuf
;
815 bool wxBitmap::HasPixbuf() const
817 wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") );
819 return M_BMPDATA
->m_pixbuf
!= NULL
;
822 void wxBitmap::SetPixbuf(GdkPixbuf
* pixbuf
, int depth
)
828 m_refData
= new wxBitmapRefData
;
830 // AllocExclusive should not be needed for this internal function
831 wxASSERT(m_refData
->GetRefCount() == 1);
832 wxASSERT(M_BMPDATA
->m_pixbuf
== NULL
);
833 M_BMPDATA
->m_pixbuf
= pixbuf
;
834 M_BMPDATA
->m_width
= gdk_pixbuf_get_width(pixbuf
);
835 M_BMPDATA
->m_height
= gdk_pixbuf_get_height(pixbuf
);
836 // if depth specified
838 M_BMPDATA
->m_bpp
= depth
;
839 else if (M_BMPDATA
->m_bpp
== 0)
840 // use something reasonable
841 M_BMPDATA
->m_bpp
= wxTheApp
->GetGdkVisual()->depth
;
842 PurgeOtherRepresentations(Pixbuf
);
845 void wxBitmap::PurgeOtherRepresentations(wxBitmap::Representation keep
)
847 if (keep
== Pixmap
&& HasPixbuf())
849 g_object_unref (M_BMPDATA
->m_pixbuf
);
850 M_BMPDATA
->m_pixbuf
= NULL
;
852 if (keep
== Pixbuf
&& HasPixmap())
854 g_object_unref (M_BMPDATA
->m_pixmap
);
855 M_BMPDATA
->m_pixmap
= NULL
;
859 #ifdef wxHAS_RAW_BITMAP
860 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
863 GdkPixbuf
*pixbuf
= GetPixbuf();
864 const bool hasAlpha
= HasAlpha();
866 // allow access if bpp is valid and matches existence of alpha
867 if ( pixbuf
&& ((bpp
== 24 && !hasAlpha
) || (bpp
== 32 && hasAlpha
)) )
869 data
.m_height
= gdk_pixbuf_get_height( pixbuf
);
870 data
.m_width
= gdk_pixbuf_get_width( pixbuf
);
871 data
.m_stride
= gdk_pixbuf_get_rowstride( pixbuf
);
872 bits
= gdk_pixbuf_get_pixels(pixbuf
);
877 void wxBitmap::UngetRawData(wxPixelDataBase
& WXUNUSED(data
))
880 #endif // wxHAS_RAW_BITMAP
882 bool wxBitmap::HasAlpha() const
884 return m_refData
!= NULL
&& M_BMPDATA
->m_pixbuf
!= NULL
&&
885 gdk_pixbuf_get_has_alpha(M_BMPDATA
->m_pixbuf
);
888 wxGDIRefData
* wxBitmap::CreateGDIRefData() const
890 return new wxBitmapRefData
;
893 wxGDIRefData
* wxBitmap::CloneGDIRefData(const wxGDIRefData
* data
) const
895 const wxBitmapRefData
* oldRef
= static_cast<const wxBitmapRefData
*>(data
);
896 wxBitmapRefData
* newRef
= new wxBitmapRefData
;
897 newRef
->m_width
= oldRef
->m_width
;
898 newRef
->m_height
= oldRef
->m_height
;
899 newRef
->m_bpp
= oldRef
->m_bpp
;
900 if (oldRef
->m_pixmap
!= NULL
)
902 newRef
->m_pixmap
= gdk_pixmap_new(
903 oldRef
->m_pixmap
, oldRef
->m_width
, oldRef
->m_height
,
904 // use pixmap depth, m_bpp may not match
905 gdk_drawable_get_depth(oldRef
->m_pixmap
));
906 wxGtkObject
<GdkGC
> gc(gdk_gc_new(newRef
->m_pixmap
));
908 newRef
->m_pixmap
, gc
, oldRef
->m_pixmap
, 0, 0, 0, 0, -1, -1);
910 if (oldRef
->m_pixbuf
!= NULL
)
912 newRef
->m_pixbuf
= gdk_pixbuf_copy(oldRef
->m_pixbuf
);
914 if (oldRef
->m_mask
!= NULL
)
916 newRef
->m_mask
= new wxMask
;
917 newRef
->m_mask
->m_bitmap
= gdk_pixmap_new(
918 oldRef
->m_mask
->m_bitmap
, oldRef
->m_width
, oldRef
->m_height
, 1);
919 wxGtkObject
<GdkGC
> gc(gdk_gc_new(newRef
->m_mask
->m_bitmap
));
920 gdk_draw_drawable(newRef
->m_mask
->m_bitmap
,
921 gc
, oldRef
->m_mask
->m_bitmap
, 0, 0, 0, 0, -1, -1);
924 // implement this if SetPalette is ever implemented
925 wxASSERT(oldRef
->m_palette
== NULL
);
931 /* static */ void wxBitmap::InitStandardHandlers()
933 // TODO: Insert handler based on GdkPixbufs handler later