]>
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"
23 #include "wx/rawbmp.h"
27 //-----------------------------------------------------------------------------
29 //-----------------------------------------------------------------------------
31 extern GtkWidget
*wxGetRootWindow();
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
37 IMPLEMENT_DYNAMIC_CLASS(wxMask
,wxObject
)
41 m_bitmap
= (GdkBitmap
*) NULL
;
44 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
46 m_bitmap
= (GdkBitmap
*) NULL
;
47 Create( bitmap
, colour
);
51 wxMask::wxMask( const wxBitmap
& bitmap
, int paletteIndex
)
53 m_bitmap
= (GdkBitmap
*) NULL
;
54 Create( bitmap
, paletteIndex
);
56 #endif // wxUSE_PALETTE
58 wxMask::wxMask( const wxBitmap
& bitmap
)
60 m_bitmap
= (GdkBitmap
*) NULL
;
67 g_object_unref (m_bitmap
);
70 bool wxMask::Create( const wxBitmap
& bitmap
,
71 const wxColour
& colour
)
75 g_object_unref (m_bitmap
);
76 m_bitmap
= (GdkBitmap
*) NULL
;
79 const int w
= bitmap
.GetWidth();
80 const int h
= bitmap
.GetHeight();
82 // create mask as XBM format bitmap
84 // one bit per pixel, each row starts on a byte boundary
85 const size_t out_size
= size_t((w
+ 7) / 8) * unsigned(h
);
86 wxByte
* out
= new wxByte
[out_size
];
88 memset(out
, 0xff, out_size
);
89 unsigned bit_index
= 0;
90 if (bitmap
.HasPixbuf())
92 const wxByte r_mask
= colour
.Red();
93 const wxByte g_mask
= colour
.Green();
94 const wxByte b_mask
= colour
.Blue();
95 GdkPixbuf
* pixbuf
= bitmap
.GetPixbuf();
96 const wxByte
* in
= gdk_pixbuf_get_pixels(pixbuf
);
97 const int inc
= 3 + int(gdk_pixbuf_get_has_alpha(pixbuf
) != 0);
98 const int rowpadding
= gdk_pixbuf_get_rowstride(pixbuf
) - inc
* w
;
99 for (int y
= 0; y
< h
; y
++, in
+= rowpadding
)
101 for (int x
= 0; x
< w
; x
++, in
+= inc
, bit_index
++)
102 if (in
[0] == r_mask
&& in
[1] == g_mask
&& in
[2] == b_mask
)
103 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
104 // move index to next byte boundary
105 bit_index
= (bit_index
+ 7) & ~7u;
110 GdkImage
* image
= gdk_drawable_get_image(bitmap
.GetPixmap(), 0, 0, w
, h
);
111 GdkColormap
* colormap
= gdk_image_get_colormap(image
);
113 if (colormap
== NULL
)
114 // mono bitmap, white is pixel value 0
115 mask_pixel
= guint32(colour
.Red() != 255 || colour
.Green() != 255 || colour
.Blue() != 255);
119 c
.CalcPixel(colormap
);
120 mask_pixel
= c
.GetPixel();
122 for (int y
= 0; y
< h
; y
++)
124 for (int x
= 0; x
< w
; x
++, bit_index
++)
125 if (gdk_image_get_pixel(image
, x
, y
) == mask_pixel
)
126 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
127 bit_index
= (bit_index
+ 7) & ~7u;
129 g_object_unref(image
);
131 m_bitmap
= gdk_bitmap_create_from_data(wxGetRootWindow()->window
, (char*)out
, w
, h
);
137 bool wxMask::Create( const wxBitmap
& bitmap
, int paletteIndex
)
140 wxPalette
*pal
= bitmap
.GetPalette();
142 wxCHECK_MSG( pal
, false, wxT("Cannot create mask from bitmap without palette") );
144 pal
->GetRGB(paletteIndex
, &r
, &g
, &b
);
146 return Create(bitmap
, wxColour(r
, g
, b
));
148 #endif // wxUSE_PALETTE
150 bool wxMask::Create( const wxBitmap
& bitmap
)
154 g_object_unref (m_bitmap
);
155 m_bitmap
= (GdkBitmap
*) NULL
;
158 if (!bitmap
.Ok()) return false;
160 wxCHECK_MSG( bitmap
.GetDepth() == 1, false, wxT("Cannot create mask from colour bitmap") );
162 m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, bitmap
.GetWidth(), bitmap
.GetHeight(), 1 );
164 if (!m_bitmap
) return false;
166 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
167 gdk_gc_set_function(gc
, GDK_COPY_INVERT
);
168 gdk_draw_drawable(m_bitmap
, gc
, bitmap
.GetPixmap(), 0, 0, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight());
174 GdkBitmap
*wxMask::GetBitmap() const
179 //-----------------------------------------------------------------------------
181 //-----------------------------------------------------------------------------
183 class wxBitmapRefData
: public wxObjectRefData
195 wxPalette
*m_palette
;
198 wxBitmapRefData::wxBitmapRefData()
200 m_pixmap
= (GdkPixmap
*) NULL
;
201 m_pixbuf
= (GdkPixbuf
*) NULL
;
202 m_mask
= (wxMask
*) NULL
;
206 m_palette
= (wxPalette
*) NULL
;
209 wxBitmapRefData::~wxBitmapRefData()
212 g_object_unref (m_pixmap
);
214 g_object_unref (m_pixbuf
);
218 #endif // wxUSE_PALETTE
221 //-----------------------------------------------------------------------------
223 #define M_BMPDATA wx_static_cast(wxBitmapRefData*, m_refData)
225 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
231 wxBitmap::wxBitmap( int width
, int height
, int depth
)
233 Create( width
, height
, depth
);
236 bool wxBitmap::Create( int width
, int height
, int depth
)
240 if ( width
<= 0 || height
<= 0 )
247 SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB
, true, 8, width
, height
));
248 M_BMPDATA
->m_bpp
= 32;
254 const GdkVisual
* visual
= wxTheApp
->GetGdkVisual();
256 depth
= visual
->depth
;
258 wxCHECK_MSG(depth
== visual
->depth
, false, wxT("invalid bitmap depth"));
261 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window
, width
, height
, depth
));
267 bool wxBitmap::CreateFromXpm( const char **bits
)
271 wxCHECK_MSG( bits
!= NULL
, false, wxT("invalid bitmap data") );
273 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
274 SetPixmap(gdk_pixmap_create_from_xpm_d(wxGetRootWindow()->window
, &mask
, NULL
, (gchar
**)bits
));
276 wxCHECK_MSG( M_BMPDATA
->m_pixmap
, false, wxT("couldn't create pixmap") );
280 M_BMPDATA
->m_mask
= new wxMask
;
281 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
287 wxBitmap
wxBitmap::Rescale( int clipx
, int clipy
, int clipwidth
, int clipheight
, int newx
, int newy
)
291 wxCHECK_MSG(Ok(), bmp
, wxT("invalid bitmap"));
293 if (newy
==M_BMPDATA
->m_width
&& newy
==M_BMPDATA
->m_height
)
296 int width
= wxMax(newx
, 1);
297 int height
= wxMax(newy
, 1);
298 width
= wxMin(width
, clipwidth
);
299 height
= wxMin(height
, clipheight
);
303 bmp
.SetDepth(GetDepth());
304 bmp
.SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
305 true, //gdk_pixbuf_get_has_alpha(GetPixbuf()),
307 gdk_pixbuf_scale(GetPixbuf(), bmp
.GetPixbuf(),
310 (double)newx
/GetWidth(), (double)newy
/GetHeight(),
311 GDK_INTERP_BILINEAR
);
315 GdkImage
* img
= gdk_drawable_get_image(GetPixmap(), 0, 0, GetWidth(), GetHeight());
317 wxCHECK_MSG(img
, bmp
, wxT("couldn't create image"));
320 GdkPixmap
*dstpix
= NULL
;
322 long dstbyteperline
= 0;
326 GdkVisual
*visual
= gdk_drawable_get_visual( GetPixmap() );
328 visual
= wxTheApp
->GetGdkVisual();
330 bmp
= wxBitmap(width
, height
, visual
->depth
);
331 dstpix
= bmp
.GetPixmap();
332 gc
= gdk_gc_new( dstpix
);
336 dstbyteperline
= (width
+ 7) / 8;
337 dst
= (char*) malloc(dstbyteperline
*height
);
340 // be careful to use the right scaling factor
341 float scx
= (float)M_BMPDATA
->m_width
/(float)newx
;
342 float scy
= (float)M_BMPDATA
->m_height
/(float)newy
;
343 // prepare accel-tables
344 int *tablex
= (int *)calloc(width
,sizeof(int));
345 int *tabley
= (int *)calloc(height
,sizeof(int));
347 // accel table filled with clipped values
348 for (int x
= 0; x
< width
; x
++)
349 tablex
[x
] = (int) (scx
* (x
+clipx
));
350 for (int y
= 0; y
< height
; y
++)
351 tabley
[y
] = (int) (scy
* (y
+clipy
));
353 // Main rescaling routine starts here
354 for (int h
= 0; h
< height
; h
++)
358 guint32 old_pixval
= 0;
360 for (int w
= 0; w
< width
; w
++)
368 pixval
= gdk_image_get_pixel( img
, x
, tabley
[h
] );
378 char shift
= bit
<< (w
% 8);
384 dst
[h
*dstbyteperline
+w
/8] = outbyte
;
392 gdk_gc_set_foreground( gc
, &col
);
393 gdk_draw_point( dstpix
, gc
, w
, h
);
397 // do not forget the last byte
398 if ( dst
&& (width
% 8 != 0) )
399 dst
[h
*dstbyteperline
+width
/8] = outbyte
;
402 g_object_unref (img
);
403 if (gc
) g_object_unref (gc
);
407 bmp
= wxBitmap( (const char *)dst
, width
, height
, 1 );
413 dstbyteperline
= (width
+ 7) / 8;
414 dst
= (char*) malloc(dstbyteperline
*height
);
415 img
= gdk_drawable_get_image(GetMask()->GetBitmap(), 0, 0, GetWidth(), GetHeight());
417 for (int h
= 0; h
< height
; h
++)
421 guint32 old_pixval
= 0;
423 for (int w
= 0; w
< width
; w
++)
431 pixval
= gdk_image_get_pixel( img
, x
, tabley
[h
] );
439 char shift
= bit
<< (w
% 8);
445 dst
[h
*dstbyteperline
+w
/8] = outbyte
;
450 // do not forget the last byte
452 dst
[h
*dstbyteperline
+width
/8] = outbyte
;
454 wxMask
* mask
= new wxMask
;
455 mask
->m_bitmap
= gdk_bitmap_create_from_data( wxGetRootWindow()->window
, (gchar
*) dst
, width
, height
);
459 g_object_unref (img
);
469 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
473 wxCHECK_MSG( image
.Ok(), false, wxT("invalid image") );
474 wxCHECK_MSG( depth
== -1 || depth
== 1, false, wxT("invalid bitmap depth") );
476 if (image
.GetWidth() <= 0 || image
.GetHeight() <= 0)
480 return CreateFromImageAsPixmap(image
, depth
);
482 if (image
.HasAlpha())
483 return CreateFromImageAsPixbuf(image
);
485 return CreateFromImageAsPixmap(image
, depth
);
488 bool wxBitmap::CreateFromImageAsPixmap(const wxImage
& image
, int depth
)
490 const int w
= image
.GetWidth();
491 const int h
= image
.GetHeight();
494 // create XBM format bitmap
496 // one bit per pixel, each row starts on a byte boundary
497 const size_t out_size
= size_t((w
+ 7) / 8) * unsigned(h
);
498 wxByte
* out
= new wxByte
[out_size
];
499 // set bits are white
500 memset(out
, 0xff, out_size
);
501 const wxByte
* in
= image
.GetData();
502 unsigned bit_index
= 0;
503 for (int y
= 0; y
< h
; y
++)
505 for (int x
= 0; x
< w
; x
++, in
+= 3, bit_index
++)
506 if (in
[0] == 255 && in
[1] == 255 && in
[2] == 255)
507 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
508 // move index to next byte boundary
509 bit_index
= (bit_index
+ 7) & ~7u;
511 SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window
, (char*)out
, w
, h
));
516 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window
, w
, h
, depth
));
517 GdkGC
* gc
= gdk_gc_new(M_BMPDATA
->m_pixmap
);
519 M_BMPDATA
->m_pixmap
, gc
,
521 GDK_RGB_DITHER_NONE
, image
.GetData(), w
* 3);
525 const wxByte
* alpha
= image
.GetAlpha();
526 if (alpha
!= NULL
|| image
.HasMask())
528 // create mask as XBM format bitmap
530 const size_t out_size
= size_t((w
+ 7) / 8) * unsigned(h
);
531 wxByte
* out
= new wxByte
[out_size
];
532 memset(out
, 0xff, out_size
);
533 unsigned bit_index
= 0;
536 for (int y
= 0; y
< h
; y
++)
538 for (int x
= 0; x
< w
; x
++, bit_index
++)
539 if (*alpha
++ < wxIMAGE_ALPHA_THRESHOLD
)
540 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
541 bit_index
= (bit_index
+ 7) & ~7u;
546 const wxByte r_mask
= image
.GetMaskRed();
547 const wxByte g_mask
= image
.GetMaskGreen();
548 const wxByte b_mask
= image
.GetMaskBlue();
549 const wxByte
* in
= image
.GetData();
550 for (int y
= 0; y
< h
; y
++)
552 for (int x
= 0; x
< w
; x
++, in
+= 3, bit_index
++)
553 if (in
[0] == r_mask
&& in
[1] == g_mask
&& in
[2] == b_mask
)
554 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
555 bit_index
= (bit_index
+ 7) & ~7u;
558 wxMask
* mask
= new wxMask
;
559 mask
->m_bitmap
= gdk_bitmap_create_from_data(M_BMPDATA
->m_pixmap
, (char*)out
, w
, h
);
566 bool wxBitmap::CreateFromImageAsPixbuf(const wxImage
& image
)
568 int width
= image
.GetWidth();
569 int height
= image
.GetHeight();
571 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
572 true, //image.HasAlpha(),
573 8 /* bits per sample */,
578 wxASSERT( image
.HasAlpha() ); // for now
579 wxASSERT( gdk_pixbuf_get_n_channels(pixbuf
) == 4 );
580 wxASSERT( gdk_pixbuf_get_width(pixbuf
) == width
);
581 wxASSERT( gdk_pixbuf_get_height(pixbuf
) == height
);
583 SetDepth(wxTheApp
->GetGdkVisual()->depth
);
587 unsigned char *in
= image
.GetData();
588 unsigned char *out
= gdk_pixbuf_get_pixels(pixbuf
);
589 unsigned char *alpha
= image
.GetAlpha();
591 int rowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
593 for (int y
= 0; y
< height
; y
++, out
+= rowinc
)
595 for (int x
= 0; x
< width
; x
++, alpha
++, out
+= 4, in
+= 3)
607 wxImage
wxBitmap::ConvertToImage() const
611 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
613 const int w
= GetWidth();
614 const int h
= GetHeight();
616 unsigned char *data
= image
.GetData();
618 wxCHECK_MSG(data
!= NULL
, wxNullImage
, wxT("couldn't create image") );
622 GdkPixbuf
*pixbuf
= GetPixbuf();
623 wxASSERT( gdk_pixbuf_get_has_alpha(pixbuf
) );
627 unsigned char *alpha
= image
.GetAlpha();
628 unsigned char *in
= gdk_pixbuf_get_pixels(pixbuf
);
629 unsigned char *out
= data
;
630 int rowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * w
;
632 for (int y
= 0; y
< h
; y
++, in
+= rowinc
)
634 for (int x
= 0; x
< w
; x
++, in
+= 4, out
+= 3, alpha
++)
645 GdkPixmap
* pixmap
= GetPixmap();
646 GdkPixmap
* pixmap_invert
= NULL
;
649 // mono bitmaps are inverted
650 pixmap_invert
= gdk_pixmap_new(pixmap
, w
, h
, 1);
651 GdkGC
* gc
= gdk_gc_new(pixmap_invert
);
652 gdk_gc_set_function(gc
, GDK_COPY_INVERT
);
653 gdk_draw_drawable(pixmap_invert
, gc
, pixmap
, 0, 0, 0, 0, w
, h
);
655 pixmap
= pixmap_invert
;
657 // create a pixbuf which shares data with the wxImage
658 GdkPixbuf
* pixbuf
= gdk_pixbuf_new_from_data(
659 data
, GDK_COLORSPACE_RGB
, false, 8, w
, h
, 3 * w
, NULL
, NULL
);
661 gdk_pixbuf_get_from_drawable(pixbuf
, pixmap
, NULL
, 0, 0, 0, 0, w
, h
);
663 g_object_unref(pixbuf
);
664 if (pixmap_invert
!= NULL
)
665 g_object_unref(pixmap_invert
);
669 // the colour used as transparent one in wxImage and the one it is
670 // replaced with when it really occurs in the bitmap
671 const int MASK_RED
= 1;
672 const int MASK_GREEN
= 2;
673 const int MASK_BLUE
= 3;
674 const int MASK_BLUE_REPLACEMENT
= 2;
676 image
.SetMaskColour(MASK_RED
, MASK_GREEN
, MASK_BLUE
);
677 GdkImage
* image_mask
= gdk_drawable_get_image(GetMask()->GetBitmap(), 0, 0, w
, h
);
679 for (int y
= 0; y
< h
; y
++)
681 for (int x
= 0; x
< w
; x
++, data
+= 3)
683 if (gdk_image_get_pixel(image_mask
, x
, y
) == 0)
686 data
[1] = MASK_GREEN
;
689 else if (data
[0] == MASK_RED
&& data
[1] == MASK_GREEN
&& data
[2] == MASK_BLUE
)
691 data
[2] = MASK_BLUE_REPLACEMENT
;
695 g_object_unref(image_mask
);
702 wxBitmap::wxBitmap( const wxString
&filename
, wxBitmapType type
)
704 LoadFile( filename
, type
);
707 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int WXUNUSED(depth
))
709 if ( width
> 0 && height
> 0 )
711 SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window
, bits
, width
, height
));
713 wxASSERT_MSG( M_BMPDATA
->m_pixmap
, wxT("couldn't create bitmap") );
717 wxBitmap::~wxBitmap()
721 bool wxBitmap::operator == ( const wxBitmap
& bmp
) const
723 return m_refData
== bmp
.m_refData
;
726 bool wxBitmap::operator != ( const wxBitmap
& bmp
) const
728 return m_refData
!= bmp
.m_refData
;
731 bool wxBitmap::Ok() const
733 return (m_refData
!= NULL
) &&
735 M_BMPDATA
->m_pixbuf
||
740 int wxBitmap::GetHeight() const
742 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
744 return M_BMPDATA
->m_height
;
747 int wxBitmap::GetWidth() const
749 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
751 return M_BMPDATA
->m_width
;
754 int wxBitmap::GetDepth() const
756 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
758 return M_BMPDATA
->m_bpp
;
761 wxMask
*wxBitmap::GetMask() const
763 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
765 return M_BMPDATA
->m_mask
;
768 void wxBitmap::SetMask( wxMask
*mask
)
770 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
772 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
774 M_BMPDATA
->m_mask
= mask
;
777 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
783 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
788 (rect
.x
>= 0) && (rect
.y
>= 0) &&
789 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) && (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
790 ret
, wxT("invalid bitmap or bitmap region") );
794 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
795 true, //gdk_pixbuf_get_has_alpha(GetPixbuf()),
796 8, rect
.width
, rect
.height
);
797 ret
.SetPixbuf(pixbuf
);
798 ret
.SetDepth(M_BMPDATA
->m_bpp
);
799 gdk_pixbuf_copy_area(GetPixbuf(),
800 rect
.x
, rect
.y
, rect
.width
, rect
.height
,
805 ret
= wxBitmap(rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
806 GdkGC
*gc
= gdk_gc_new( ret
.GetPixmap() );
807 gdk_draw_drawable( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
813 wxMask
*mask
= new wxMask
;
814 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, rect
.width
, rect
.height
, 1 );
816 GdkGC
*gc
= gdk_gc_new( mask
->m_bitmap
);
817 gdk_draw_drawable(mask
->m_bitmap
, gc
, M_BMPDATA
->m_mask
->m_bitmap
, rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
826 bool wxBitmap::SaveFile( const wxString
&name
, wxBitmapType type
, const wxPalette
*WXUNUSED(palette
) ) const
828 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
830 // Try to save the bitmap via wxImage handlers:
831 wxImage image
= ConvertToImage();
832 return image
.Ok() && image
.SaveFile(name
, type
);
835 bool wxBitmap::LoadFile( const wxString
&name
, wxBitmapType type
)
839 if (type
== wxBITMAP_TYPE_XPM
)
841 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
842 SetPixmap(gdk_pixmap_create_from_xpm(wxGetRootWindow()->window
, &mask
, NULL
, name
.fn_str()));
846 M_BMPDATA
->m_mask
= new wxMask
;
847 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
850 else // try if wxImage can load it
853 if (image
.LoadFile(name
, type
) && image
.Ok())
854 *this = wxBitmap(image
);
861 wxPalette
*wxBitmap::GetPalette() const
864 return (wxPalette
*) NULL
;
866 return M_BMPDATA
->m_palette
;
869 void wxBitmap::SetPalette(const wxPalette
& WXUNUSED(palette
))
873 #endif // wxUSE_PALETTE
875 void wxBitmap::SetHeight( int height
)
878 m_refData
= new wxBitmapRefData
;
880 M_BMPDATA
->m_height
= height
;
883 void wxBitmap::SetWidth( int width
)
886 m_refData
= new wxBitmapRefData
;
888 M_BMPDATA
->m_width
= width
;
891 void wxBitmap::SetDepth( int depth
)
894 m_refData
= new wxBitmapRefData
;
896 M_BMPDATA
->m_bpp
= depth
;
899 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
902 m_refData
= new wxBitmapRefData
;
904 wxASSERT(M_BMPDATA
->m_pixmap
== NULL
);
905 M_BMPDATA
->m_pixmap
= pixmap
;
906 gdk_drawable_get_size(pixmap
, &M_BMPDATA
->m_width
, &M_BMPDATA
->m_height
);
907 M_BMPDATA
->m_bpp
= gdk_drawable_get_depth(pixmap
);
908 PurgeOtherRepresentations(Pixmap
);
911 GdkPixmap
*wxBitmap::GetPixmap() const
913 wxCHECK_MSG( Ok(), (GdkPixmap
*) NULL
, wxT("invalid bitmap") );
915 // create the pixmap on the fly if we use Pixbuf representation:
916 if (M_BMPDATA
->m_pixmap
== NULL
)
918 delete M_BMPDATA
->m_mask
;
919 M_BMPDATA
->m_mask
= new wxMask
;
920 gdk_pixbuf_render_pixmap_and_mask(M_BMPDATA
->m_pixbuf
,
921 &M_BMPDATA
->m_pixmap
,
922 &M_BMPDATA
->m_mask
->m_bitmap
,
926 return M_BMPDATA
->m_pixmap
;
929 bool wxBitmap::HasPixmap() const
931 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
933 return M_BMPDATA
->m_pixmap
!= NULL
;
936 GdkPixbuf
*wxBitmap::GetPixbuf() const
938 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
940 if (M_BMPDATA
->m_pixbuf
== NULL
)
942 int width
= GetWidth();
943 int height
= GetHeight();
945 // always create the alpha channel so raw bitmap access will work
947 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
948 true, // GetMask() != NULL,
950 M_BMPDATA
->m_pixbuf
=
951 gdk_pixbuf_get_from_drawable(pixbuf
, M_BMPDATA
->m_pixmap
, NULL
,
952 0, 0, 0, 0, width
, height
);
954 // apply the mask to created pixbuf:
955 if (M_BMPDATA
->m_pixbuf
&& M_BMPDATA
->m_mask
)
958 gdk_pixbuf_get_from_drawable(NULL
,
959 M_BMPDATA
->m_mask
->GetBitmap(),
961 0, 0, 0, 0, width
, height
);
964 guchar
*bmp
= gdk_pixbuf_get_pixels(pixbuf
);
965 guchar
*mask
= gdk_pixbuf_get_pixels(pmask
);
966 int bmprowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
967 int maskrowinc
= gdk_pixbuf_get_rowstride(pmask
) - 3 * width
;
969 for (int y
= 0; y
< height
;
970 y
++, bmp
+= bmprowinc
, mask
+= maskrowinc
)
972 for (int x
= 0; x
< width
; x
++, bmp
+= 4, mask
+= 3)
974 if (mask
[0] == 0 /*black pixel*/)
979 g_object_unref (pmask
);
984 return M_BMPDATA
->m_pixbuf
;
987 bool wxBitmap::HasPixbuf() const
989 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
991 return M_BMPDATA
->m_pixbuf
!= NULL
;
994 void wxBitmap::SetPixbuf( GdkPixbuf
*pixbuf
)
997 m_refData
= new wxBitmapRefData
;
999 wxASSERT(M_BMPDATA
->m_pixbuf
== NULL
);
1000 M_BMPDATA
->m_pixbuf
= pixbuf
;
1001 M_BMPDATA
->m_width
= gdk_pixbuf_get_width(pixbuf
);
1002 M_BMPDATA
->m_height
= gdk_pixbuf_get_height(pixbuf
);
1003 PurgeOtherRepresentations(Pixbuf
);
1006 void wxBitmap::PurgeOtherRepresentations(wxBitmap::Representation keep
)
1008 if (keep
== Pixmap
&& HasPixbuf())
1010 g_object_unref (M_BMPDATA
->m_pixbuf
);
1011 M_BMPDATA
->m_pixbuf
= NULL
;
1013 if (keep
== Pixbuf
&& HasPixmap())
1015 g_object_unref (M_BMPDATA
->m_pixmap
);
1016 M_BMPDATA
->m_pixmap
= NULL
;
1020 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1025 GdkPixbuf
*pixbuf
= GetPixbuf();
1029 if (!gdk_pixbuf_get_has_alpha( pixbuf
))
1033 if (gdk_pixbuf_get_has_alpha( pixbuf
))
1034 wxPrintf( wxT("Has alpha, %d channels\n"), gdk_pixbuf_get_n_channels(pixbuf
) );
1036 wxPrintf( wxT("No alpha, %d channels.\n"), gdk_pixbuf_get_n_channels(pixbuf
) );
1039 data
.m_height
= gdk_pixbuf_get_height( pixbuf
);
1040 data
.m_width
= gdk_pixbuf_get_width( pixbuf
);
1041 data
.m_stride
= gdk_pixbuf_get_rowstride( pixbuf
);
1043 return gdk_pixbuf_get_pixels( pixbuf
);
1046 void wxBitmap::UngetRawData(wxPixelDataBase
& WXUNUSED(data
))
1051 bool wxBitmap::HasAlpha() const
1056 void wxBitmap::UseAlpha()
1061 //-----------------------------------------------------------------------------
1063 //-----------------------------------------------------------------------------
1065 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
,wxBitmapHandlerBase
)
1067 wxBitmapHandler::~wxBitmapHandler()
1071 bool wxBitmapHandler::Create(wxBitmap
* WXUNUSED(bitmap
),
1072 void * WXUNUSED(data
),
1073 long WXUNUSED(type
),
1074 int WXUNUSED(width
),
1075 int WXUNUSED(height
),
1076 int WXUNUSED(depth
))
1078 wxFAIL_MSG( _T("not implemented") );
1083 bool wxBitmapHandler::LoadFile(wxBitmap
* WXUNUSED(bitmap
),
1084 const wxString
& WXUNUSED(name
),
1085 long WXUNUSED(flags
),
1086 int WXUNUSED(desiredWidth
),
1087 int WXUNUSED(desiredHeight
))
1089 wxFAIL_MSG( _T("not implemented") );
1094 bool wxBitmapHandler::SaveFile(const wxBitmap
* WXUNUSED(bitmap
),
1095 const wxString
& WXUNUSED(name
),
1097 const wxPalette
* WXUNUSED(palette
))
1099 wxFAIL_MSG( _T("not implemented") );
1104 /* static */ void wxBitmap::InitStandardHandlers()
1106 // TODO: Insert handler based on GdkPixbufs handler later