]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/bitmap.cpp
5754ccba92b0da73589f17880ea27a05905beccf
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/bitmap.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
13 #include "wx/bitmap.h"
17 #include "wx/palette.h"
21 #include "wx/colour.h"
24 #include "wx/rawbmp.h"
28 //-----------------------------------------------------------------------------
30 //-----------------------------------------------------------------------------
32 extern GtkWidget
*wxGetRootWindow();
34 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
38 IMPLEMENT_DYNAMIC_CLASS(wxMask
,wxObject
)
42 m_bitmap
= (GdkBitmap
*) NULL
;
45 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
47 m_bitmap
= (GdkBitmap
*) NULL
;
48 Create( bitmap
, colour
);
52 wxMask::wxMask( const wxBitmap
& bitmap
, int paletteIndex
)
54 m_bitmap
= (GdkBitmap
*) NULL
;
55 Create( bitmap
, paletteIndex
);
57 #endif // wxUSE_PALETTE
59 wxMask::wxMask( const wxBitmap
& bitmap
)
61 m_bitmap
= (GdkBitmap
*) NULL
;
68 g_object_unref (m_bitmap
);
71 bool wxMask::Create( const wxBitmap
& bitmap
,
72 const wxColour
& colour
)
76 g_object_unref (m_bitmap
);
77 m_bitmap
= (GdkBitmap
*) NULL
;
80 const int w
= bitmap
.GetWidth();
81 const int h
= bitmap
.GetHeight();
83 // create mask as XBM format bitmap
85 // one bit per pixel, each row starts on a byte boundary
86 const size_t out_size
= size_t((w
+ 7) / 8) * unsigned(h
);
87 wxByte
* out
= new wxByte
[out_size
];
88 // set bits are unmasked
89 memset(out
, 0xff, out_size
);
90 unsigned bit_index
= 0;
91 if (bitmap
.HasPixbuf())
93 const wxByte r_mask
= colour
.Red();
94 const wxByte g_mask
= colour
.Green();
95 const wxByte b_mask
= colour
.Blue();
96 GdkPixbuf
* pixbuf
= bitmap
.GetPixbuf();
97 const wxByte
* in
= gdk_pixbuf_get_pixels(pixbuf
);
98 const int inc
= 3 + int(gdk_pixbuf_get_has_alpha(pixbuf
) != 0);
99 const int rowpadding
= gdk_pixbuf_get_rowstride(pixbuf
) - inc
* w
;
100 for (int y
= 0; y
< h
; y
++, in
+= rowpadding
)
102 for (int x
= 0; x
< w
; x
++, in
+= inc
, bit_index
++)
103 if (in
[0] == r_mask
&& in
[1] == g_mask
&& in
[2] == b_mask
)
104 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
105 // move index to next byte boundary
106 bit_index
= (bit_index
+ 7) & ~7u;
111 GdkImage
* image
= gdk_drawable_get_image(bitmap
.GetPixmap(), 0, 0, w
, h
);
112 GdkColormap
* colormap
= gdk_image_get_colormap(image
);
114 if (colormap
== NULL
)
115 // mono bitmap, white is pixel value 0
116 mask_pixel
= guint32(colour
.Red() != 255 || colour
.Green() != 255 || colour
.Blue() != 255);
120 c
.CalcPixel(colormap
);
121 mask_pixel
= c
.GetPixel();
123 for (int y
= 0; y
< h
; y
++)
125 for (int x
= 0; x
< w
; x
++, bit_index
++)
126 if (gdk_image_get_pixel(image
, x
, y
) == mask_pixel
)
127 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
128 bit_index
= (bit_index
+ 7) & ~7u;
130 g_object_unref(image
);
132 m_bitmap
= gdk_bitmap_create_from_data(wxGetRootWindow()->window
, (char*)out
, w
, h
);
138 bool wxMask::Create( const wxBitmap
& bitmap
, int paletteIndex
)
141 wxPalette
*pal
= bitmap
.GetPalette();
143 wxCHECK_MSG( pal
, false, wxT("Cannot create mask from bitmap without palette") );
145 pal
->GetRGB(paletteIndex
, &r
, &g
, &b
);
147 return Create(bitmap
, wxColour(r
, g
, b
));
149 #endif // wxUSE_PALETTE
151 bool wxMask::Create( const wxBitmap
& bitmap
)
155 g_object_unref (m_bitmap
);
156 m_bitmap
= (GdkBitmap
*) NULL
;
159 if (!bitmap
.Ok()) return false;
161 wxCHECK_MSG( bitmap
.GetDepth() == 1, false, wxT("Cannot create mask from colour bitmap") );
163 m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, bitmap
.GetWidth(), bitmap
.GetHeight(), 1 );
165 if (!m_bitmap
) return false;
167 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
168 gdk_gc_set_function(gc
, GDK_COPY_INVERT
);
169 gdk_draw_drawable(m_bitmap
, gc
, bitmap
.GetPixmap(), 0, 0, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight());
175 GdkBitmap
*wxMask::GetBitmap() const
180 //-----------------------------------------------------------------------------
182 //-----------------------------------------------------------------------------
184 class wxBitmapRefData
: public wxObjectRefData
188 virtual ~wxBitmapRefData();
197 wxPalette
*m_palette
;
198 #endif // wxUSE_PALETTE
201 wxBitmapRefData::wxBitmapRefData()
203 m_pixmap
= (GdkPixmap
*) NULL
;
204 m_pixbuf
= (GdkPixbuf
*) NULL
;
205 m_mask
= (wxMask
*) NULL
;
210 m_palette
= (wxPalette
*) NULL
;
211 #endif // wxUSE_PALETTE
214 wxBitmapRefData::~wxBitmapRefData()
217 g_object_unref (m_pixmap
);
219 g_object_unref (m_pixbuf
);
223 #endif // wxUSE_PALETTE
226 //-----------------------------------------------------------------------------
228 #define M_BMPDATA wx_static_cast(wxBitmapRefData*, m_refData)
230 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
232 wxBitmap::wxBitmap(int width
, int height
, int depth
)
234 Create(width
, height
, depth
);
237 wxBitmap::wxBitmap(const wxString
&filename
, wxBitmapType type
)
239 LoadFile(filename
, type
);
242 wxBitmap::wxBitmap(const char bits
[], int width
, int height
, int depth
)
244 wxASSERT(depth
== 1);
245 if (width
> 0 && height
> 0 && depth
== 1)
247 SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window
, bits
, width
, height
));
249 wxASSERT_MSG( M_BMPDATA
->m_pixmap
, wxT("couldn't create bitmap") );
253 wxBitmap::~wxBitmap()
257 bool wxBitmap::Create( int width
, int height
, int depth
)
261 if ( width
<= 0 || height
<= 0 )
268 SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB
, true, 8, width
, height
), 32);
274 const GdkVisual
* visual
= wxTheApp
->GetGdkVisual();
276 depth
= visual
->depth
;
278 wxCHECK_MSG(depth
== visual
->depth
, false, wxT("invalid bitmap depth"));
281 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window
, width
, height
, depth
));
287 bool wxBitmap::CreateFromXpm(const char* const* bits
)
291 wxCHECK_MSG( bits
!= NULL
, false, wxT("invalid bitmap data") );
293 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
294 SetPixmap(gdk_pixmap_create_from_xpm_d(wxGetRootWindow()->window
, &mask
, NULL
, wx_const_cast(char**, bits
)));
296 wxCHECK_MSG( M_BMPDATA
->m_pixmap
, false, wxT("couldn't create pixmap") );
300 M_BMPDATA
->m_mask
= new wxMask
;
301 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
307 wxBitmap
wxBitmap::Rescale(int clipx
, int clipy
, int clipwidth
, int clipheight
, int newx
, int newy
) const
311 wxCHECK_MSG(Ok(), bmp
, wxT("invalid bitmap"));
313 if (newy
==M_BMPDATA
->m_width
&& newy
==M_BMPDATA
->m_height
)
316 int width
= wxMax(newx
, 1);
317 int height
= wxMax(newy
, 1);
318 width
= wxMin(width
, clipwidth
);
319 height
= wxMin(height
, clipheight
);
321 // scale pixbuf if available and it has alpha or there is no mask
322 if (M_BMPDATA
->m_pixbuf
!= NULL
&& (
323 M_BMPDATA
->m_mask
== NULL
|| gdk_pixbuf_get_has_alpha(M_BMPDATA
->m_pixbuf
)))
325 bmp
.SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
326 gdk_pixbuf_get_has_alpha(M_BMPDATA
->m_pixbuf
),
327 8, width
, height
), M_BMPDATA
->m_bpp
);
328 gdk_pixbuf_scale(M_BMPDATA
->m_pixbuf
, bmp
.GetPixbuf(),
331 (double)newx
/GetWidth(), (double)newy
/GetHeight(),
332 GDK_INTERP_BILINEAR
);
336 GdkImage
* img
= gdk_drawable_get_image(
337 M_BMPDATA
->m_pixmap
, 0, 0, M_BMPDATA
->m_width
, M_BMPDATA
->m_height
);
339 wxCHECK_MSG(img
, bmp
, wxT("couldn't create image"));
342 GdkPixmap
*dstpix
= NULL
;
344 long dstbyteperline
= 0;
348 bmp
.Create(width
, height
, gdk_drawable_get_depth(M_BMPDATA
->m_pixmap
));
349 dstpix
= bmp
.GetPixmap();
350 gc
= gdk_gc_new( dstpix
);
354 dstbyteperline
= (width
+ 7) / 8;
355 dst
= (char*) malloc(dstbyteperline
*height
);
358 // be careful to use the right scaling factor
359 float scx
= (float)M_BMPDATA
->m_width
/(float)newx
;
360 float scy
= (float)M_BMPDATA
->m_height
/(float)newy
;
361 // prepare accel-tables
362 int *tablex
= (int *)calloc(width
,sizeof(int));
363 int *tabley
= (int *)calloc(height
,sizeof(int));
365 // accel table filled with clipped values
366 for (int x
= 0; x
< width
; x
++)
367 tablex
[x
] = (int) (scx
* (x
+clipx
));
368 for (int y
= 0; y
< height
; y
++)
369 tabley
[y
] = (int) (scy
* (y
+clipy
));
371 // Main rescaling routine starts here
372 for (int h
= 0; h
< height
; h
++)
376 guint32 old_pixval
= 0;
378 for (int w
= 0; w
< width
; w
++)
386 pixval
= gdk_image_get_pixel( img
, x
, tabley
[h
] );
396 char shift
= bit
<< (w
% 8);
402 dst
[h
*dstbyteperline
+w
/8] = outbyte
;
410 gdk_gc_set_foreground( gc
, &col
);
411 gdk_draw_point( dstpix
, gc
, w
, h
);
415 // do not forget the last byte
416 if ( dst
&& (width
% 8 != 0) )
417 dst
[h
*dstbyteperline
+width
/8] = outbyte
;
420 g_object_unref (img
);
421 if (gc
) g_object_unref (gc
);
425 bmp
= wxBitmap(dst
, width
, height
, 1);
431 dstbyteperline
= (width
+ 7) / 8;
432 dst
= (char*) malloc(dstbyteperline
*height
);
433 img
= gdk_drawable_get_image(GetMask()->GetBitmap(), 0, 0, GetWidth(), GetHeight());
435 for (int h
= 0; h
< height
; h
++)
439 guint32 old_pixval
= 0;
441 for (int w
= 0; w
< width
; w
++)
449 pixval
= gdk_image_get_pixel( img
, x
, tabley
[h
] );
457 char shift
= bit
<< (w
% 8);
463 dst
[h
*dstbyteperline
+w
/8] = outbyte
;
468 // do not forget the last byte
470 dst
[h
*dstbyteperline
+width
/8] = outbyte
;
472 wxMask
* mask
= new wxMask
;
473 mask
->m_bitmap
= gdk_bitmap_create_from_data( wxGetRootWindow()->window
, (gchar
*) dst
, width
, height
);
477 g_object_unref (img
);
487 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
491 wxCHECK_MSG( image
.Ok(), false, wxT("invalid image") );
492 wxCHECK_MSG( depth
== -1 || depth
== 1, false, wxT("invalid bitmap depth") );
494 if (image
.GetWidth() <= 0 || image
.GetHeight() <= 0)
497 // create pixbuf if image has alpha and requested depth is compatible
498 if (image
.HasAlpha() && (depth
== -1 || depth
== 32))
499 return CreateFromImageAsPixbuf(image
);
501 // otherwise create pixmap, if alpha is present it will be converted to mask
502 return CreateFromImageAsPixmap(image
, depth
);
505 bool wxBitmap::CreateFromImageAsPixmap(const wxImage
& image
, int depth
)
507 const int w
= image
.GetWidth();
508 const int h
= image
.GetHeight();
511 // create XBM format bitmap
513 // one bit per pixel, each row starts on a byte boundary
514 const size_t out_size
= size_t((w
+ 7) / 8) * unsigned(h
);
515 wxByte
* out
= new wxByte
[out_size
];
516 // set bits are black
517 memset(out
, 0xff, out_size
);
518 const wxByte
* in
= image
.GetData();
519 unsigned bit_index
= 0;
520 for (int y
= 0; y
< h
; y
++)
522 for (int x
= 0; x
< w
; x
++, in
+= 3, bit_index
++)
523 if (in
[0] == 255 && in
[1] == 255 && in
[2] == 255)
524 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
525 // move index to next byte boundary
526 bit_index
= (bit_index
+ 7) & ~7u;
528 SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window
, (char*)out
, w
, h
));
533 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window
, w
, h
, depth
));
534 GdkGC
* gc
= gdk_gc_new(M_BMPDATA
->m_pixmap
);
536 M_BMPDATA
->m_pixmap
, gc
,
538 GDK_RGB_DITHER_NONE
, image
.GetData(), w
* 3);
542 const wxByte
* alpha
= image
.GetAlpha();
543 if (alpha
!= NULL
|| image
.HasMask())
545 // create mask as XBM format bitmap
547 const size_t out_size
= size_t((w
+ 7) / 8) * unsigned(h
);
548 wxByte
* out
= new wxByte
[out_size
];
549 memset(out
, 0xff, out_size
);
550 unsigned bit_index
= 0;
553 for (int y
= 0; y
< h
; y
++)
555 for (int x
= 0; x
< w
; x
++, bit_index
++)
556 if (*alpha
++ < wxIMAGE_ALPHA_THRESHOLD
)
557 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
558 bit_index
= (bit_index
+ 7) & ~7u;
563 const wxByte r_mask
= image
.GetMaskRed();
564 const wxByte g_mask
= image
.GetMaskGreen();
565 const wxByte b_mask
= image
.GetMaskBlue();
566 const wxByte
* in
= image
.GetData();
567 for (int y
= 0; y
< h
; y
++)
569 for (int x
= 0; x
< w
; x
++, in
+= 3, bit_index
++)
570 if (in
[0] == r_mask
&& in
[1] == g_mask
&& in
[2] == b_mask
)
571 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
572 bit_index
= (bit_index
+ 7) & ~7u;
575 wxMask
* mask
= new wxMask
;
576 mask
->m_bitmap
= gdk_bitmap_create_from_data(M_BMPDATA
->m_pixmap
, (char*)out
, w
, h
);
583 bool wxBitmap::CreateFromImageAsPixbuf(const wxImage
& image
)
585 wxASSERT(image
.HasAlpha());
587 int width
= image
.GetWidth();
588 int height
= image
.GetHeight();
590 Create(width
, height
, 32);
591 GdkPixbuf
* pixbuf
= M_BMPDATA
->m_pixbuf
;
596 const unsigned char* in
= image
.GetData();
597 unsigned char *out
= gdk_pixbuf_get_pixels(pixbuf
);
598 unsigned char *alpha
= image
.GetAlpha();
600 int rowpad
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
602 for (int y
= 0; y
< height
; y
++, out
+= rowpad
)
604 for (int x
= 0; x
< width
; x
++, alpha
++, out
+= 4, in
+= 3)
616 wxImage
wxBitmap::ConvertToImage() const
618 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
620 const int w
= GetWidth();
621 const int h
= GetHeight();
622 wxImage
image(w
, h
, false);
623 unsigned char *data
= image
.GetData();
625 wxCHECK_MSG(data
!= NULL
, wxNullImage
, wxT("couldn't create image") );
627 // prefer pixbuf if available, it will preserve alpha and should be quicker
630 GdkPixbuf
*pixbuf
= GetPixbuf();
631 unsigned char* alpha
= NULL
;
632 if (gdk_pixbuf_get_has_alpha(pixbuf
))
635 alpha
= image
.GetAlpha();
637 const unsigned char* in
= gdk_pixbuf_get_pixels(pixbuf
);
638 unsigned char *out
= data
;
639 const int inc
= 3 + int(alpha
!= NULL
);
640 const int rowpad
= gdk_pixbuf_get_rowstride(pixbuf
) - inc
* w
;
642 for (int y
= 0; y
< h
; y
++, in
+= rowpad
)
644 for (int x
= 0; x
< w
; x
++, in
+= inc
, out
+= 3)
656 GdkPixmap
* pixmap
= GetPixmap();
657 GdkPixmap
* pixmap_invert
= NULL
;
660 // mono bitmaps are inverted, i.e. 0 is white
661 pixmap_invert
= gdk_pixmap_new(pixmap
, w
, h
, 1);
662 GdkGC
* gc
= gdk_gc_new(pixmap_invert
);
663 gdk_gc_set_function(gc
, GDK_COPY_INVERT
);
664 gdk_draw_drawable(pixmap_invert
, gc
, pixmap
, 0, 0, 0, 0, w
, h
);
666 pixmap
= pixmap_invert
;
668 // create a pixbuf which shares data with the wxImage
669 GdkPixbuf
* pixbuf
= gdk_pixbuf_new_from_data(
670 data
, GDK_COLORSPACE_RGB
, false, 8, w
, h
, 3 * w
, NULL
, NULL
);
672 gdk_pixbuf_get_from_drawable(pixbuf
, pixmap
, NULL
, 0, 0, 0, 0, w
, h
);
674 g_object_unref(pixbuf
);
675 if (pixmap_invert
!= NULL
)
676 g_object_unref(pixmap_invert
);
678 // convert mask, unless there is already alpha
679 if (GetMask() && !image
.HasAlpha())
681 // we hard code the mask colour for now but we could also make an
682 // effort (and waste time) to choose a colour not present in the
683 // image already to avoid having to fudge the pixels below --
684 // whether it's worth to do it is unclear however
685 const int MASK_RED
= 1;
686 const int MASK_GREEN
= 2;
687 const int MASK_BLUE
= 3;
688 const int MASK_BLUE_REPLACEMENT
= 2;
690 image
.SetMaskColour(MASK_RED
, MASK_GREEN
, MASK_BLUE
);
691 GdkImage
* image_mask
= gdk_drawable_get_image(GetMask()->GetBitmap(), 0, 0, w
, h
);
693 for (int y
= 0; y
< h
; y
++)
695 for (int x
= 0; x
< w
; x
++, data
+= 3)
697 if (gdk_image_get_pixel(image_mask
, x
, y
) == 0)
700 data
[1] = MASK_GREEN
;
703 else if (data
[0] == MASK_RED
&& data
[1] == MASK_GREEN
&& data
[2] == MASK_BLUE
)
705 // we have to fudge the colour a bit to prevent
706 // this pixel from appearing transparent
707 data
[2] = MASK_BLUE_REPLACEMENT
;
711 g_object_unref(image_mask
);
717 bool wxBitmap::operator == ( const wxBitmap
& bmp
) const
719 return m_refData
== bmp
.m_refData
;
722 bool wxBitmap::Ok() const
724 return (m_refData
!= NULL
) &&
726 M_BMPDATA
->m_pixbuf
||
731 int wxBitmap::GetHeight() const
733 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
735 return M_BMPDATA
->m_height
;
738 int wxBitmap::GetWidth() const
740 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
742 return M_BMPDATA
->m_width
;
745 int wxBitmap::GetDepth() const
747 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
749 return M_BMPDATA
->m_bpp
;
752 wxMask
*wxBitmap::GetMask() const
754 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
756 return M_BMPDATA
->m_mask
;
759 void wxBitmap::SetMask( wxMask
*mask
)
761 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
763 delete M_BMPDATA
->m_mask
;
764 M_BMPDATA
->m_mask
= mask
;
767 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
773 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
777 wxCHECK_MSG(Ok(), ret
, wxT("invalid bitmap"));
778 wxCHECK_MSG(rect
.x
>= 0 && rect
.y
>= 0 &&
779 rect
.x
+ rect
.width
<= M_BMPDATA
->m_width
&&
780 rect
.y
+ rect
.height
<= M_BMPDATA
->m_height
,
781 ret
, wxT("invalid bitmap region"));
783 if (HasPixbuf() || M_BMPDATA
->m_bpp
== 32)
785 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
786 gdk_pixbuf_get_has_alpha(GetPixbuf()),
787 8, rect
.width
, rect
.height
);
788 ret
.SetPixbuf(pixbuf
, M_BMPDATA
->m_bpp
);
789 gdk_pixbuf_copy_area(GetPixbuf(),
790 rect
.x
, rect
.y
, rect
.width
, rect
.height
,
795 ret
.Create(rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
796 GdkGC
*gc
= gdk_gc_new( ret
.GetPixmap() );
797 gdk_draw_drawable( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
800 // make mask, unless there is already alpha
801 if (GetMask() && !HasAlpha())
803 wxMask
*mask
= new wxMask
;
804 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, rect
.width
, rect
.height
, 1 );
806 GdkGC
*gc
= gdk_gc_new( mask
->m_bitmap
);
807 gdk_draw_drawable(mask
->m_bitmap
, gc
, M_BMPDATA
->m_mask
->m_bitmap
, rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
816 bool wxBitmap::SaveFile( const wxString
&name
, wxBitmapType type
, const wxPalette
*WXUNUSED(palette
) ) const
818 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
820 // Try to save the bitmap via wxImage handlers:
821 wxImage image
= ConvertToImage();
822 return image
.Ok() && image
.SaveFile(name
, type
);
825 bool wxBitmap::LoadFile( const wxString
&name
, wxBitmapType type
)
829 if (type
== wxBITMAP_TYPE_XPM
)
831 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
832 SetPixmap(gdk_pixmap_create_from_xpm(wxGetRootWindow()->window
, &mask
, NULL
, name
.fn_str()));
836 M_BMPDATA
->m_mask
= new wxMask
;
837 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
840 else // try if wxImage can load it
843 if (image
.LoadFile(name
, type
) && image
.Ok())
844 CreateFromImage(image
, -1);
851 wxPalette
*wxBitmap::GetPalette() const
853 wxCHECK_MSG(Ok(), NULL
, wxT("invalid bitmap"));
855 return M_BMPDATA
->m_palette
;
858 void wxBitmap::SetPalette(const wxPalette
& WXUNUSED(palette
))
862 #endif // wxUSE_PALETTE
864 void wxBitmap::SetHeight( int height
)
867 m_refData
= new wxBitmapRefData
;
869 M_BMPDATA
->m_height
= height
;
872 void wxBitmap::SetWidth( int width
)
875 m_refData
= new wxBitmapRefData
;
877 M_BMPDATA
->m_width
= width
;
880 void wxBitmap::SetDepth( int depth
)
883 m_refData
= new wxBitmapRefData
;
885 M_BMPDATA
->m_bpp
= depth
;
888 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
891 m_refData
= new wxBitmapRefData
;
893 wxASSERT(M_BMPDATA
->m_pixmap
== NULL
);
894 M_BMPDATA
->m_pixmap
= pixmap
;
895 gdk_drawable_get_size(pixmap
, &M_BMPDATA
->m_width
, &M_BMPDATA
->m_height
);
896 M_BMPDATA
->m_bpp
= gdk_drawable_get_depth(pixmap
);
897 PurgeOtherRepresentations(Pixmap
);
900 GdkPixmap
*wxBitmap::GetPixmap() const
902 wxCHECK_MSG( Ok(), (GdkPixmap
*) NULL
, wxT("invalid bitmap") );
904 // create the pixmap on the fly if we use Pixbuf representation:
905 if (M_BMPDATA
->m_pixmap
== NULL
)
907 GdkPixmap
** pmask
= NULL
;
908 if (gdk_pixbuf_get_has_alpha(M_BMPDATA
->m_pixbuf
))
910 // make new mask from alpha
911 delete M_BMPDATA
->m_mask
;
912 M_BMPDATA
->m_mask
= new wxMask
;
913 pmask
= &M_BMPDATA
->m_mask
->m_bitmap
;
915 gdk_pixbuf_render_pixmap_and_mask(M_BMPDATA
->m_pixbuf
,
916 &M_BMPDATA
->m_pixmap
,
918 wxIMAGE_ALPHA_THRESHOLD
);
921 return M_BMPDATA
->m_pixmap
;
924 bool wxBitmap::HasPixmap() const
926 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
928 return M_BMPDATA
->m_pixmap
!= NULL
;
931 GdkPixbuf
*wxBitmap::GetPixbuf() const
933 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
935 if (M_BMPDATA
->m_pixbuf
== NULL
)
937 int width
= GetWidth();
938 int height
= GetHeight();
940 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
943 M_BMPDATA
->m_pixbuf
= pixbuf
;
944 gdk_pixbuf_get_from_drawable(pixbuf
, M_BMPDATA
->m_pixmap
, NULL
,
945 0, 0, 0, 0, width
, height
);
947 // apply the mask to created pixbuf:
948 if (M_BMPDATA
->m_pixbuf
&& M_BMPDATA
->m_mask
)
951 gdk_pixbuf_get_from_drawable(NULL
,
952 M_BMPDATA
->m_mask
->GetBitmap(),
954 0, 0, 0, 0, width
, height
);
957 guchar
*bmp
= gdk_pixbuf_get_pixels(pixbuf
);
958 guchar
*mask
= gdk_pixbuf_get_pixels(pmask
);
959 int bmprowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
960 int maskrowinc
= gdk_pixbuf_get_rowstride(pmask
) - 3 * width
;
962 for (int y
= 0; y
< height
;
963 y
++, bmp
+= bmprowinc
, mask
+= maskrowinc
)
965 for (int x
= 0; x
< width
; x
++, bmp
+= 4, mask
+= 3)
967 if (mask
[0] == 0 /*black pixel*/)
972 g_object_unref (pmask
);
977 return M_BMPDATA
->m_pixbuf
;
980 bool wxBitmap::HasPixbuf() const
982 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
984 return M_BMPDATA
->m_pixbuf
!= NULL
;
987 void wxBitmap::SetPixbuf(GdkPixbuf
* pixbuf
, int depth
)
990 m_refData
= new wxBitmapRefData
;
992 wxASSERT(M_BMPDATA
->m_pixbuf
== NULL
);
993 M_BMPDATA
->m_pixbuf
= pixbuf
;
994 M_BMPDATA
->m_width
= gdk_pixbuf_get_width(pixbuf
);
995 M_BMPDATA
->m_height
= gdk_pixbuf_get_height(pixbuf
);
996 // if depth specified
998 M_BMPDATA
->m_bpp
= depth
;
999 else if (M_BMPDATA
->m_bpp
== 0)
1000 // use something reasonable
1001 M_BMPDATA
->m_bpp
= wxTheApp
->GetGdkVisual()->depth
;
1002 PurgeOtherRepresentations(Pixbuf
);
1005 void wxBitmap::PurgeOtherRepresentations(wxBitmap::Representation keep
)
1007 if (keep
== Pixmap
&& HasPixbuf())
1009 g_object_unref (M_BMPDATA
->m_pixbuf
);
1010 M_BMPDATA
->m_pixbuf
= NULL
;
1012 if (keep
== Pixbuf
&& HasPixmap())
1014 g_object_unref (M_BMPDATA
->m_pixmap
);
1015 M_BMPDATA
->m_pixmap
= NULL
;
1019 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1022 GdkPixbuf
*pixbuf
= GetPixbuf();
1023 const bool hasAlpha
= HasAlpha();
1024 // allow access if bpp is valid and matches existence of alpha
1025 if (pixbuf
!= NULL
&& (
1026 bpp
== 24 && !hasAlpha
||
1027 bpp
== 32 && hasAlpha
))
1029 data
.m_height
= gdk_pixbuf_get_height( pixbuf
);
1030 data
.m_width
= gdk_pixbuf_get_width( pixbuf
);
1031 data
.m_stride
= gdk_pixbuf_get_rowstride( pixbuf
);
1032 bits
= gdk_pixbuf_get_pixels(pixbuf
);
1037 void wxBitmap::UngetRawData(wxPixelDataBase
& WXUNUSED(data
))
1041 bool wxBitmap::HasAlpha() const
1043 return m_refData
!= NULL
&& M_BMPDATA
->m_pixbuf
!= NULL
&&
1044 gdk_pixbuf_get_has_alpha(M_BMPDATA
->m_pixbuf
);
1047 void wxBitmap::UseAlpha()
1049 GdkPixbuf
* pixbuf
= GetPixbuf();
1050 // add alpha if necessary
1051 if (!gdk_pixbuf_get_has_alpha(pixbuf
))
1053 M_BMPDATA
->m_pixbuf
= NULL
;
1054 SetPixbuf(gdk_pixbuf_add_alpha(pixbuf
, false, 0, 0, 0));
1055 g_object_unref(pixbuf
);
1059 //-----------------------------------------------------------------------------
1061 //-----------------------------------------------------------------------------
1063 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
,wxBitmapHandlerBase
)
1065 wxBitmapHandler::~wxBitmapHandler()
1069 bool wxBitmapHandler::Create(wxBitmap
* WXUNUSED(bitmap
),
1070 void * WXUNUSED(data
),
1071 long WXUNUSED(type
),
1072 int WXUNUSED(width
),
1073 int WXUNUSED(height
),
1074 int WXUNUSED(depth
))
1076 wxFAIL_MSG( _T("not implemented") );
1081 bool wxBitmapHandler::LoadFile(wxBitmap
* WXUNUSED(bitmap
),
1082 const wxString
& WXUNUSED(name
),
1083 long WXUNUSED(flags
),
1084 int WXUNUSED(desiredWidth
),
1085 int WXUNUSED(desiredHeight
))
1087 wxFAIL_MSG( _T("not implemented") );
1092 bool wxBitmapHandler::SaveFile(const wxBitmap
* WXUNUSED(bitmap
),
1093 const wxString
& WXUNUSED(name
),
1095 const wxPalette
* WXUNUSED(palette
))
1097 wxFAIL_MSG( _T("not implemented") );
1102 /* static */ void wxBitmap::InitStandardHandlers()
1104 // TODO: Insert handler based on GdkPixbufs handler later