]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/bitmap.cpp
7811f87e542b432641c79d9287d74445d96257bf
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 // HP aCC for PA-RISC can't compile rawbmp.h
25 #if !defined(__HP_aCC) || !defined(__hppa)
26 #include "wx/rawbmp.h"
29 #include "wx/gtk/private/object.h"
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
37 extern GtkWidget
*wxGetRootWindow();
39 //-----------------------------------------------------------------------------
41 //-----------------------------------------------------------------------------
43 IMPLEMENT_DYNAMIC_CLASS(wxMask
,wxObject
)
50 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
53 Create( bitmap
, colour
);
57 wxMask::wxMask( const wxBitmap
& bitmap
, int paletteIndex
)
60 Create( bitmap
, paletteIndex
);
62 #endif // wxUSE_PALETTE
64 wxMask::wxMask( const wxBitmap
& bitmap
)
73 g_object_unref (m_bitmap
);
76 bool wxMask::Create( const wxBitmap
& bitmap
,
77 const wxColour
& colour
)
81 g_object_unref (m_bitmap
);
85 const int w
= bitmap
.GetWidth();
86 const int h
= bitmap
.GetHeight();
88 // create mask as XBM format bitmap
90 // one bit per pixel, each row starts on a byte boundary
91 const size_t out_size
= size_t((w
+ 7) / 8) * unsigned(h
);
92 wxByte
* out
= new wxByte
[out_size
];
93 // set bits are unmasked
94 memset(out
, 0xff, out_size
);
95 unsigned bit_index
= 0;
96 if (bitmap
.HasPixbuf())
98 const wxByte r_mask
= colour
.Red();
99 const wxByte g_mask
= colour
.Green();
100 const wxByte b_mask
= colour
.Blue();
101 GdkPixbuf
* pixbuf
= bitmap
.GetPixbuf();
102 const wxByte
* in
= gdk_pixbuf_get_pixels(pixbuf
);
103 const int inc
= 3 + int(gdk_pixbuf_get_has_alpha(pixbuf
) != 0);
104 const int rowpadding
= gdk_pixbuf_get_rowstride(pixbuf
) - inc
* w
;
105 for (int y
= 0; y
< h
; y
++, in
+= rowpadding
)
107 for (int x
= 0; x
< w
; x
++, in
+= inc
, bit_index
++)
108 if (in
[0] == r_mask
&& in
[1] == g_mask
&& in
[2] == b_mask
)
109 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
110 // move index to next byte boundary
111 bit_index
= (bit_index
+ 7) & ~7u;
116 GdkImage
* image
= gdk_drawable_get_image(bitmap
.GetPixmap(), 0, 0, w
, h
);
117 GdkColormap
* colormap
= gdk_image_get_colormap(image
);
119 if (colormap
== NULL
)
120 // mono bitmap, white is pixel value 0
121 mask_pixel
= guint32(colour
.Red() != 255 || colour
.Green() != 255 || colour
.Blue() != 255);
125 c
.CalcPixel(colormap
);
126 mask_pixel
= c
.GetPixel();
128 for (int y
= 0; y
< h
; y
++)
130 for (int x
= 0; x
< w
; x
++, bit_index
++)
131 if (gdk_image_get_pixel(image
, x
, y
) == mask_pixel
)
132 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
133 bit_index
= (bit_index
+ 7) & ~7u;
135 g_object_unref(image
);
137 m_bitmap
= gdk_bitmap_create_from_data(wxGetRootWindow()->window
, (char*)out
, w
, h
);
143 bool wxMask::Create( const wxBitmap
& bitmap
, int paletteIndex
)
146 wxPalette
*pal
= bitmap
.GetPalette();
148 wxCHECK_MSG( pal
, false, wxT("Cannot create mask from bitmap without palette") );
150 pal
->GetRGB(paletteIndex
, &r
, &g
, &b
);
152 return Create(bitmap
, wxColour(r
, g
, b
));
154 #endif // wxUSE_PALETTE
156 bool wxMask::Create( const wxBitmap
& bitmap
)
160 g_object_unref (m_bitmap
);
164 if (!bitmap
.IsOk()) return false;
166 wxCHECK_MSG( bitmap
.GetDepth() == 1, false, wxT("Cannot create mask from colour bitmap") );
168 m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, bitmap
.GetWidth(), bitmap
.GetHeight(), 1 );
170 if (!m_bitmap
) return false;
172 wxGtkObject
<GdkGC
> gc(gdk_gc_new( m_bitmap
));
173 gdk_gc_set_function(gc
, GDK_COPY_INVERT
);
174 gdk_draw_drawable(m_bitmap
, gc
, bitmap
.GetPixmap(), 0, 0, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight());
179 GdkBitmap
*wxMask::GetBitmap() const
184 //-----------------------------------------------------------------------------
186 //-----------------------------------------------------------------------------
188 class wxBitmapRefData
: public wxGDIRefData
192 virtual ~wxBitmapRefData();
194 virtual bool IsOk() const { return m_pixmap
|| m_pixbuf
; }
203 wxPalette
*m_palette
;
204 #endif // wxUSE_PALETTE
207 wxBitmapRefData::wxBitmapRefData()
217 #endif // wxUSE_PALETTE
220 wxBitmapRefData::~wxBitmapRefData()
223 g_object_unref (m_pixmap
);
225 g_object_unref (m_pixbuf
);
229 #endif // wxUSE_PALETTE
232 //-----------------------------------------------------------------------------
234 #define M_BMPDATA static_cast<wxBitmapRefData*>(m_refData)
236 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
238 wxBitmap::wxBitmap(int width
, int height
, int depth
)
240 Create(width
, height
, depth
);
243 wxBitmap::wxBitmap(const wxString
&filename
, wxBitmapType type
)
245 LoadFile(filename
, type
);
248 wxBitmap::wxBitmap(const char bits
[], int width
, int height
, int depth
)
250 wxASSERT(depth
== 1);
251 if (width
> 0 && height
> 0 && depth
== 1)
253 SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window
, bits
, width
, height
));
257 wxBitmap::wxBitmap(const char* const* bits
)
259 wxCHECK2_MSG(bits
!= NULL
, return, wxT("invalid bitmap data"));
261 GdkBitmap
* mask
= NULL
;
262 SetPixmap(gdk_pixmap_create_from_xpm_d(wxGetRootWindow()->window
, &mask
, NULL
, const_cast<char**>(bits
)));
266 if (M_BMPDATA
->m_pixmap
!= NULL
&& mask
!= NULL
)
268 M_BMPDATA
->m_mask
= new wxMask
;
269 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
273 wxBitmap::~wxBitmap()
277 bool wxBitmap::Create( int width
, int height
, int depth
)
281 if ( width
<= 0 || height
<= 0 )
286 const GdkVisual
* visual
= wxTheApp
->GetGdkVisual();
290 SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB
, true, 8, width
, height
), 32);
295 // must initialize alpha, otherwise GetPixmap()
296 // will create a mask out of garbage
297 gdk_pixbuf_fill(M_BMPDATA
->m_pixbuf
, 0x000000ff);
299 else if (depth
== 24)
301 if (visual
->depth
== depth
)
302 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window
, width
, height
, depth
));
304 SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB
, false, 8, width
, height
), 24);
311 depth
= visual
->depth
;
313 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window
, width
, height
, depth
));
321 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
325 wxCHECK_MSG( image
.IsOk(), false, wxT("invalid image") );
326 wxCHECK_MSG( depth
== -1 || depth
== 1, false, wxT("invalid bitmap depth") );
328 if (image
.GetWidth() <= 0 || image
.GetHeight() <= 0)
331 // create pixbuf if image has alpha and requested depth is compatible
332 if (image
.HasAlpha() && (depth
== -1 || depth
== 32))
333 return CreateFromImageAsPixbuf(image
);
335 // otherwise create pixmap, if alpha is present it will be converted to mask
336 return CreateFromImageAsPixmap(image
, depth
);
339 bool wxBitmap::CreateFromImageAsPixmap(const wxImage
& image
, int depth
)
341 const int w
= image
.GetWidth();
342 const int h
= image
.GetHeight();
345 // create XBM format bitmap
347 // one bit per pixel, each row starts on a byte boundary
348 const size_t out_size
= size_t((w
+ 7) / 8) * unsigned(h
);
349 wxByte
* out
= new wxByte
[out_size
];
350 // set bits are black
351 memset(out
, 0xff, out_size
);
352 const wxByte
* in
= image
.GetData();
353 unsigned bit_index
= 0;
354 for (int y
= 0; y
< h
; y
++)
356 for (int x
= 0; x
< w
; x
++, in
+= 3, bit_index
++)
357 if (in
[0] == 255 && in
[1] == 255 && in
[2] == 255)
358 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
359 // move index to next byte boundary
360 bit_index
= (bit_index
+ 7) & ~7u;
362 SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window
, (char*)out
, w
, h
));
365 if (!M_BMPDATA
) // SetPixmap may have failed
370 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window
, w
, h
, depth
));
374 wxGtkObject
<GdkGC
> gc(gdk_gc_new(M_BMPDATA
->m_pixmap
));
376 M_BMPDATA
->m_pixmap
, gc
,
378 GDK_RGB_DITHER_NONE
, image
.GetData(), w
* 3);
381 const wxByte
* alpha
= image
.GetAlpha();
382 if (alpha
!= NULL
|| image
.HasMask())
384 // create mask as XBM format bitmap
386 const size_t out_size
= size_t((w
+ 7) / 8) * unsigned(h
);
387 wxByte
* out
= new wxByte
[out_size
];
388 memset(out
, 0xff, out_size
);
389 unsigned bit_index
= 0;
392 for (int y
= 0; y
< h
; y
++)
394 for (int x
= 0; x
< w
; x
++, bit_index
++)
395 if (*alpha
++ < wxIMAGE_ALPHA_THRESHOLD
)
396 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
397 bit_index
= (bit_index
+ 7) & ~7u;
402 const wxByte r_mask
= image
.GetMaskRed();
403 const wxByte g_mask
= image
.GetMaskGreen();
404 const wxByte b_mask
= image
.GetMaskBlue();
405 const wxByte
* in
= image
.GetData();
406 for (int y
= 0; y
< h
; y
++)
408 for (int x
= 0; x
< w
; x
++, in
+= 3, bit_index
++)
409 if (in
[0] == r_mask
&& in
[1] == g_mask
&& in
[2] == b_mask
)
410 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
411 bit_index
= (bit_index
+ 7) & ~7u;
414 wxMask
* mask
= new wxMask
;
415 mask
->m_bitmap
= gdk_bitmap_create_from_data(M_BMPDATA
->m_pixmap
, (char*)out
, w
, h
);
422 bool wxBitmap::CreateFromImageAsPixbuf(const wxImage
& image
)
424 wxASSERT(image
.HasAlpha());
426 int width
= image
.GetWidth();
427 int height
= image
.GetHeight();
429 Create(width
, height
, 32);
430 GdkPixbuf
* pixbuf
= M_BMPDATA
->m_pixbuf
;
435 const unsigned char* in
= image
.GetData();
436 unsigned char *out
= gdk_pixbuf_get_pixels(pixbuf
);
437 unsigned char *alpha
= image
.GetAlpha();
439 int rowpad
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
441 for (int y
= 0; y
< height
; y
++, out
+= rowpad
)
443 for (int x
= 0; x
< width
; x
++, alpha
++, out
+= 4, in
+= 3)
455 wxImage
wxBitmap::ConvertToImage() const
457 wxCHECK_MSG( IsOk(), wxNullImage
, wxT("invalid bitmap") );
459 const int w
= GetWidth();
460 const int h
= GetHeight();
461 wxImage
image(w
, h
, false);
462 unsigned char *data
= image
.GetData();
464 wxCHECK_MSG(data
!= NULL
, wxNullImage
, wxT("couldn't create image") );
466 // prefer pixbuf if available, it will preserve alpha and should be quicker
469 GdkPixbuf
*pixbuf
= GetPixbuf();
470 unsigned char* alpha
= NULL
;
471 if (gdk_pixbuf_get_has_alpha(pixbuf
))
474 alpha
= image
.GetAlpha();
476 const unsigned char* in
= gdk_pixbuf_get_pixels(pixbuf
);
477 unsigned char *out
= data
;
478 const int inc
= 3 + int(alpha
!= NULL
);
479 const int rowpad
= gdk_pixbuf_get_rowstride(pixbuf
) - inc
* w
;
481 for (int y
= 0; y
< h
; y
++, in
+= rowpad
)
483 for (int x
= 0; x
< w
; x
++, in
+= inc
, out
+= 3)
495 GdkPixmap
* pixmap
= GetPixmap();
496 GdkPixmap
* pixmap_invert
= NULL
;
499 // mono bitmaps are inverted, i.e. 0 is white
500 pixmap_invert
= gdk_pixmap_new(pixmap
, w
, h
, 1);
501 wxGtkObject
<GdkGC
> gc(gdk_gc_new(pixmap_invert
));
502 gdk_gc_set_function(gc
, GDK_COPY_INVERT
);
503 gdk_draw_drawable(pixmap_invert
, gc
, pixmap
, 0, 0, 0, 0, w
, h
);
504 pixmap
= pixmap_invert
;
506 // create a pixbuf which shares data with the wxImage
507 GdkPixbuf
* pixbuf
= gdk_pixbuf_new_from_data(
508 data
, GDK_COLORSPACE_RGB
, false, 8, w
, h
, 3 * w
, NULL
, NULL
);
510 gdk_pixbuf_get_from_drawable(pixbuf
, pixmap
, NULL
, 0, 0, 0, 0, w
, h
);
512 g_object_unref(pixbuf
);
513 if (pixmap_invert
!= NULL
)
514 g_object_unref(pixmap_invert
);
516 // convert mask, unless there is already alpha
517 if (GetMask() && !image
.HasAlpha())
519 // we hard code the mask colour for now but we could also make an
520 // effort (and waste time) to choose a colour not present in the
521 // image already to avoid having to fudge the pixels below --
522 // whether it's worth to do it is unclear however
523 const int MASK_RED
= 1;
524 const int MASK_GREEN
= 2;
525 const int MASK_BLUE
= 3;
526 const int MASK_BLUE_REPLACEMENT
= 2;
528 image
.SetMaskColour(MASK_RED
, MASK_GREEN
, MASK_BLUE
);
529 GdkImage
* image_mask
= gdk_drawable_get_image(GetMask()->GetBitmap(), 0, 0, w
, h
);
531 for (int y
= 0; y
< h
; y
++)
533 for (int x
= 0; x
< w
; x
++, data
+= 3)
535 if (gdk_image_get_pixel(image_mask
, x
, y
) == 0)
538 data
[1] = MASK_GREEN
;
541 else if (data
[0] == MASK_RED
&& data
[1] == MASK_GREEN
&& data
[2] == MASK_BLUE
)
543 // we have to fudge the colour a bit to prevent
544 // this pixel from appearing transparent
545 data
[2] = MASK_BLUE_REPLACEMENT
;
549 g_object_unref(image_mask
);
555 #endif // wxUSE_IMAGE
557 int wxBitmap::GetHeight() const
559 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
561 return M_BMPDATA
->m_height
;
564 int wxBitmap::GetWidth() const
566 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
568 return M_BMPDATA
->m_width
;
571 int wxBitmap::GetDepth() const
573 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
575 return M_BMPDATA
->m_bpp
;
578 wxMask
*wxBitmap::GetMask() const
580 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") );
582 return M_BMPDATA
->m_mask
;
585 void wxBitmap::SetMask( wxMask
*mask
)
587 wxCHECK_RET( IsOk(), wxT("invalid bitmap") );
590 delete M_BMPDATA
->m_mask
;
591 M_BMPDATA
->m_mask
= mask
;
594 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
600 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
604 wxCHECK_MSG(IsOk(), ret
, wxT("invalid bitmap"));
605 wxCHECK_MSG(rect
.x
>= 0 && rect
.y
>= 0 &&
606 rect
.x
+ rect
.width
<= M_BMPDATA
->m_width
&&
607 rect
.y
+ rect
.height
<= M_BMPDATA
->m_height
,
608 ret
, wxT("invalid bitmap region"));
610 if (HasPixbuf() || M_BMPDATA
->m_bpp
== 32)
612 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
613 gdk_pixbuf_get_has_alpha(GetPixbuf()),
614 8, rect
.width
, rect
.height
);
618 ret
.SetPixbuf(pixbuf
, M_BMPDATA
->m_bpp
);
619 gdk_pixbuf_copy_area(GetPixbuf(),
620 rect
.x
, rect
.y
, rect
.width
, rect
.height
,
625 ret
.Create(rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
626 wxGtkObject
<GdkGC
> gc(gdk_gc_new( ret
.GetPixmap() ));
627 gdk_draw_drawable( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
629 // make mask, unless there is already alpha
630 if (GetMask() && !HasAlpha())
632 wxMask
*mask
= new wxMask
;
633 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, rect
.width
, rect
.height
, 1 );
635 wxGtkObject
<GdkGC
> gc(gdk_gc_new( mask
->m_bitmap
));
636 gdk_draw_drawable(mask
->m_bitmap
, gc
, M_BMPDATA
->m_mask
->m_bitmap
, rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
644 bool wxBitmap::SaveFile( const wxString
&name
, wxBitmapType type
, const wxPalette
*WXUNUSED(palette
) ) const
646 wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") );
649 // Try to save the bitmap via wxImage handlers:
650 wxImage image
= ConvertToImage();
651 return image
.Ok() && image
.SaveFile(name
, type
);
652 #else // !wxUSE_IMAGE
657 #endif // wxUSE_IMAGE
660 bool wxBitmap::LoadFile( const wxString
&name
, wxBitmapType type
)
664 if (type
== wxBITMAP_TYPE_XPM
)
666 GdkBitmap
*mask
= NULL
;
667 SetPixmap(gdk_pixmap_create_from_xpm(wxGetRootWindow()->window
, &mask
, NULL
, name
.fn_str()));
669 return false; // do not set the mask
673 M_BMPDATA
->m_mask
= new wxMask
;
674 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
678 else // try if wxImage can load it
681 if (image
.LoadFile(name
, type
) && image
.Ok())
682 CreateFromImage(image
, -1);
684 #endif // wxUSE_IMAGE
690 wxPalette
*wxBitmap::GetPalette() const
692 wxCHECK_MSG(IsOk(), NULL
, wxT("invalid bitmap"));
694 return M_BMPDATA
->m_palette
;
697 void wxBitmap::SetPalette(const wxPalette
& WXUNUSED(palette
))
701 #endif // wxUSE_PALETTE
703 void wxBitmap::SetHeight( int height
)
706 M_BMPDATA
->m_height
= height
;
709 void wxBitmap::SetWidth( int width
)
712 M_BMPDATA
->m_width
= width
;
715 void wxBitmap::SetDepth( int depth
)
718 M_BMPDATA
->m_bpp
= depth
;
721 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
727 m_refData
= new wxBitmapRefData
;
729 // AllocExclusive should not be needed for this internal function
730 wxASSERT(m_refData
->GetRefCount() == 1);
731 wxASSERT(M_BMPDATA
->m_pixmap
== NULL
);
732 M_BMPDATA
->m_pixmap
= pixmap
;
733 gdk_drawable_get_size(pixmap
, &M_BMPDATA
->m_width
, &M_BMPDATA
->m_height
);
734 M_BMPDATA
->m_bpp
= gdk_drawable_get_depth(pixmap
);
735 PurgeOtherRepresentations(Pixmap
);
738 GdkPixmap
*wxBitmap::GetPixmap() const
740 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") );
742 // create the pixmap on the fly if we use Pixbuf representation:
743 if (M_BMPDATA
->m_pixmap
== NULL
)
745 GdkPixmap
** pmask
= NULL
;
746 if (gdk_pixbuf_get_has_alpha(M_BMPDATA
->m_pixbuf
))
748 // make new mask from alpha
749 delete M_BMPDATA
->m_mask
;
750 M_BMPDATA
->m_mask
= new wxMask
;
751 pmask
= &M_BMPDATA
->m_mask
->m_bitmap
;
753 gdk_pixbuf_render_pixmap_and_mask(M_BMPDATA
->m_pixbuf
,
754 &M_BMPDATA
->m_pixmap
,
756 0x80 /* alpha threshold */);
759 return M_BMPDATA
->m_pixmap
;
762 bool wxBitmap::HasPixmap() const
764 wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") );
766 return M_BMPDATA
->m_pixmap
!= NULL
;
769 GdkPixbuf
*wxBitmap::GetPixbuf() const
771 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") );
773 if (M_BMPDATA
->m_pixbuf
== NULL
)
776 int width
= GetWidth();
777 int height
= GetHeight();
779 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
782 M_BMPDATA
->m_pixbuf
= pixbuf
;
783 gdk_pixbuf_get_from_drawable(pixbuf
, M_BMPDATA
->m_pixmap
, NULL
,
784 0, 0, 0, 0, width
, height
);
785 // apply the mask to created pixbuf:
786 if (M_BMPDATA
->m_pixbuf
&& M_BMPDATA
->m_mask
)
789 gdk_pixbuf_get_from_drawable(NULL
,
790 M_BMPDATA
->m_mask
->GetBitmap(),
792 0, 0, 0, 0, width
, height
);
795 guchar
*bmp
= gdk_pixbuf_get_pixels(pixbuf
);
796 guchar
*mask
= gdk_pixbuf_get_pixels(pmask
);
797 int bmprowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
798 int maskrowinc
= gdk_pixbuf_get_rowstride(pmask
) - 3 * width
;
800 for (int y
= 0; y
< height
;
801 y
++, bmp
+= bmprowinc
, mask
+= maskrowinc
)
803 for (int x
= 0; x
< width
; x
++, bmp
+= 4, mask
+= 3)
805 if (mask
[0] == 0 /*black pixel*/)
810 g_object_unref (pmask
);
815 return M_BMPDATA
->m_pixbuf
;
818 bool wxBitmap::HasPixbuf() const
820 wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") );
822 return M_BMPDATA
->m_pixbuf
!= NULL
;
825 void wxBitmap::SetPixbuf(GdkPixbuf
* pixbuf
, int depth
)
831 m_refData
= new wxBitmapRefData
;
833 // AllocExclusive should not be needed for this internal function
834 wxASSERT(m_refData
->GetRefCount() == 1);
835 wxASSERT(M_BMPDATA
->m_pixbuf
== NULL
);
836 M_BMPDATA
->m_pixbuf
= pixbuf
;
837 M_BMPDATA
->m_width
= gdk_pixbuf_get_width(pixbuf
);
838 M_BMPDATA
->m_height
= gdk_pixbuf_get_height(pixbuf
);
839 // if depth specified
841 M_BMPDATA
->m_bpp
= depth
;
842 else if (M_BMPDATA
->m_bpp
== 0)
843 // use something reasonable
844 M_BMPDATA
->m_bpp
= wxTheApp
->GetGdkVisual()->depth
;
845 PurgeOtherRepresentations(Pixbuf
);
848 void wxBitmap::PurgeOtherRepresentations(wxBitmap::Representation keep
)
850 if (keep
== Pixmap
&& HasPixbuf())
852 g_object_unref (M_BMPDATA
->m_pixbuf
);
853 M_BMPDATA
->m_pixbuf
= NULL
;
855 if (keep
== Pixbuf
&& HasPixmap())
857 g_object_unref (M_BMPDATA
->m_pixmap
);
858 M_BMPDATA
->m_pixmap
= NULL
;
862 #if !defined(__HP_aCC) || !defined(__hppa)
863 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
866 GdkPixbuf
*pixbuf
= GetPixbuf();
867 const bool hasAlpha
= HasAlpha();
869 // allow access if bpp is valid and matches existence of alpha
870 if ( pixbuf
&& ((bpp
== 24 && !hasAlpha
) || (bpp
== 32 && hasAlpha
)) )
872 data
.m_height
= gdk_pixbuf_get_height( pixbuf
);
873 data
.m_width
= gdk_pixbuf_get_width( pixbuf
);
874 data
.m_stride
= gdk_pixbuf_get_rowstride( pixbuf
);
875 bits
= gdk_pixbuf_get_pixels(pixbuf
);
880 void wxBitmap::UngetRawData(wxPixelDataBase
& WXUNUSED(data
))
885 bool wxBitmap::HasAlpha() const
887 return m_refData
!= NULL
&& M_BMPDATA
->m_pixbuf
!= NULL
&&
888 gdk_pixbuf_get_has_alpha(M_BMPDATA
->m_pixbuf
);
891 wxGDIRefData
* wxBitmap::CreateGDIRefData() const
893 return new wxBitmapRefData
;
896 wxGDIRefData
* wxBitmap::CloneGDIRefData(const wxGDIRefData
* data
) const
898 const wxBitmapRefData
* oldRef
= static_cast<const wxBitmapRefData
*>(data
);
899 wxBitmapRefData
* newRef
= new wxBitmapRefData
;
900 newRef
->m_width
= oldRef
->m_width
;
901 newRef
->m_height
= oldRef
->m_height
;
902 newRef
->m_bpp
= oldRef
->m_bpp
;
903 if (oldRef
->m_pixmap
!= NULL
)
905 newRef
->m_pixmap
= gdk_pixmap_new(
906 oldRef
->m_pixmap
, oldRef
->m_width
, oldRef
->m_height
,
907 // use pixmap depth, m_bpp may not match
908 gdk_drawable_get_depth(oldRef
->m_pixmap
));
909 wxGtkObject
<GdkGC
> gc(gdk_gc_new(newRef
->m_pixmap
));
911 newRef
->m_pixmap
, gc
, oldRef
->m_pixmap
, 0, 0, 0, 0, -1, -1);
913 if (oldRef
->m_pixbuf
!= NULL
)
915 newRef
->m_pixbuf
= gdk_pixbuf_copy(oldRef
->m_pixbuf
);
917 if (oldRef
->m_mask
!= NULL
)
919 newRef
->m_mask
= new wxMask
;
920 newRef
->m_mask
->m_bitmap
= gdk_pixmap_new(
921 oldRef
->m_mask
->m_bitmap
, oldRef
->m_width
, oldRef
->m_height
, 1);
922 wxGtkObject
<GdkGC
> gc(gdk_gc_new(newRef
->m_mask
->m_bitmap
));
923 gdk_draw_drawable(newRef
->m_mask
->m_bitmap
,
924 gc
, oldRef
->m_mask
->m_bitmap
, 0, 0, 0, 0, -1, -1);
927 // implement this if SetPalette is ever implemented
928 wxASSERT(oldRef
->m_palette
== NULL
);
934 /* static */ void wxBitmap::InitStandardHandlers()
936 // TODO: Insert handler based on GdkPixbufs handler later