]>
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 extern void gdk_wx_draw_bitmap (GdkDrawable
*drawable
,
37 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
41 extern GtkWidget
*wxGetRootWindow();
43 //-----------------------------------------------------------------------------
45 //-----------------------------------------------------------------------------
47 IMPLEMENT_DYNAMIC_CLASS(wxMask
,wxObject
)
51 m_bitmap
= (GdkBitmap
*) NULL
;
54 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
56 m_bitmap
= (GdkBitmap
*) NULL
;
57 Create( bitmap
, colour
);
61 wxMask::wxMask( const wxBitmap
& bitmap
, int paletteIndex
)
63 m_bitmap
= (GdkBitmap
*) NULL
;
64 Create( bitmap
, paletteIndex
);
66 #endif // wxUSE_PALETTE
68 wxMask::wxMask( const wxBitmap
& bitmap
)
70 m_bitmap
= (GdkBitmap
*) NULL
;
77 g_object_unref (m_bitmap
);
80 bool wxMask::Create( const wxBitmap
& bitmap
,
81 const wxColour
& colour
)
85 g_object_unref (m_bitmap
);
86 m_bitmap
= (GdkBitmap
*) NULL
;
89 wxImage image
= bitmap
.ConvertToImage();
90 if (!image
.Ok()) return false;
92 m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, image
.GetWidth(), image
.GetHeight(), 1 );
93 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
97 gdk_gc_set_foreground( gc
, &color
);
98 gdk_draw_rectangle( m_bitmap
, gc
, TRUE
, 0, 0, image
.GetWidth(), image
.GetHeight() );
100 unsigned char *data
= image
.GetData();
103 unsigned char red
= colour
.Red();
104 unsigned char green
= colour
.Green();
105 unsigned char blue
= colour
.Blue();
107 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
109 int bpp
= visual
->depth
;
110 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800))
115 green
= green
& 0xf8;
121 green
= green
& 0xfc;
127 green
= green
& 0xf0;
132 gdk_gc_set_foreground( gc
, &color
);
134 for (int j
= 0; j
< image
.GetHeight(); j
++)
138 for (i
= 0; i
< image
.GetWidth(); i
++)
140 if ((data
[index
] == red
) &&
141 (data
[index
+1] == green
) &&
142 (data
[index
+2] == blue
))
151 gdk_draw_line( m_bitmap
, gc
, start_x
, j
, i
-1, j
);
158 gdk_draw_line( m_bitmap
, gc
, start_x
, j
, i
, j
);
167 bool wxMask::Create( const wxBitmap
& bitmap
, int paletteIndex
)
170 wxPalette
*pal
= bitmap
.GetPalette();
172 wxCHECK_MSG( pal
, false, wxT("Cannot create mask from bitmap without palette") );
174 pal
->GetRGB(paletteIndex
, &r
, &g
, &b
);
176 return Create(bitmap
, wxColour(r
, g
, b
));
178 #endif // wxUSE_PALETTE
180 bool wxMask::Create( const wxBitmap
& bitmap
)
184 g_object_unref (m_bitmap
);
185 m_bitmap
= (GdkBitmap
*) NULL
;
188 if (!bitmap
.Ok()) return false;
190 wxCHECK_MSG( bitmap
.GetDepth() == 1, false, wxT("Cannot create mask from colour bitmap") );
192 m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, bitmap
.GetWidth(), bitmap
.GetHeight(), 1 );
194 if (!m_bitmap
) return false;
196 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
198 gdk_wx_draw_bitmap( m_bitmap
, gc
, bitmap
.GetPixmap(), 0, 0, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight() );
205 GdkBitmap
*wxMask::GetBitmap() const
210 //-----------------------------------------------------------------------------
212 //-----------------------------------------------------------------------------
214 class wxBitmapRefData
: public wxObjectRefData
226 wxPalette
*m_palette
;
229 wxBitmapRefData::wxBitmapRefData()
231 m_pixmap
= (GdkPixmap
*) NULL
;
232 m_pixbuf
= (GdkPixbuf
*) NULL
;
233 m_mask
= (wxMask
*) NULL
;
237 m_palette
= (wxPalette
*) NULL
;
240 wxBitmapRefData::~wxBitmapRefData()
243 g_object_unref (m_pixmap
);
245 g_object_unref (m_pixbuf
);
249 #endif // wxUSE_PALETTE
252 //-----------------------------------------------------------------------------
254 #define M_BMPDATA wx_static_cast(wxBitmapRefData*, m_refData)
256 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
262 wxBitmap::wxBitmap( int width
, int height
, int depth
)
264 Create( width
, height
, depth
);
267 bool wxBitmap::Create( int width
, int height
, int depth
)
271 if ( width
<= 0 || height
<= 0 )
278 SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB
, true, 8, width
, height
));
279 M_BMPDATA
->m_bpp
= 32;
285 const GdkVisual
* visual
= wxTheApp
->GetGdkVisual();
287 depth
= visual
->depth
;
289 wxCHECK_MSG(depth
== visual
->depth
, false, wxT("invalid bitmap depth"));
292 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window
, width
, height
, depth
));
298 bool wxBitmap::CreateFromXpm( const char **bits
)
302 wxCHECK_MSG( bits
!= NULL
, false, wxT("invalid bitmap data") );
304 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
305 SetPixmap(gdk_pixmap_create_from_xpm_d(wxGetRootWindow()->window
, &mask
, NULL
, (gchar
**)bits
));
307 wxCHECK_MSG( M_BMPDATA
->m_pixmap
, false, wxT("couldn't create pixmap") );
311 M_BMPDATA
->m_mask
= new wxMask
;
312 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
318 wxBitmap
wxBitmap::Rescale( int clipx
, int clipy
, int clipwidth
, int clipheight
, int newx
, int newy
)
322 wxCHECK_MSG(Ok(), bmp
, wxT("invalid bitmap"));
324 if (newy
==M_BMPDATA
->m_width
&& newy
==M_BMPDATA
->m_height
)
327 int width
= wxMax(newx
, 1);
328 int height
= wxMax(newy
, 1);
329 width
= wxMin(width
, clipwidth
);
330 height
= wxMin(height
, clipheight
);
334 bmp
.SetDepth(GetDepth());
335 bmp
.SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
336 gdk_pixbuf_get_has_alpha(GetPixbuf()),
338 gdk_pixbuf_scale(GetPixbuf(), bmp
.GetPixbuf(),
341 (double)newx
/GetWidth(), (double)newy
/GetHeight(),
342 GDK_INTERP_BILINEAR
);
346 GdkImage
* img
= gdk_drawable_get_image(GetPixmap(), 0, 0, GetWidth(), GetHeight());
348 wxCHECK_MSG(img
, bmp
, wxT("couldn't create image"));
351 GdkPixmap
*dstpix
= NULL
;
353 long dstbyteperline
= 0;
357 GdkVisual
*visual
= gdk_drawable_get_visual( GetPixmap() );
359 visual
= wxTheApp
->GetGdkVisual();
361 bmp
= wxBitmap(width
, height
, visual
->depth
);
362 dstpix
= bmp
.GetPixmap();
363 gc
= gdk_gc_new( dstpix
);
367 dstbyteperline
= (width
+ 7) / 8;
368 dst
= (char*) malloc(dstbyteperline
*height
);
371 // be careful to use the right scaling factor
372 float scx
= (float)M_BMPDATA
->m_width
/(float)newx
;
373 float scy
= (float)M_BMPDATA
->m_height
/(float)newy
;
374 // prepare accel-tables
375 int *tablex
= (int *)calloc(width
,sizeof(int));
376 int *tabley
= (int *)calloc(height
,sizeof(int));
378 // accel table filled with clipped values
379 for (int x
= 0; x
< width
; x
++)
380 tablex
[x
] = (int) (scx
* (x
+clipx
));
381 for (int y
= 0; y
< height
; y
++)
382 tabley
[y
] = (int) (scy
* (y
+clipy
));
384 // Main rescaling routine starts here
385 for (int h
= 0; h
< height
; h
++)
389 guint32 old_pixval
= 0;
391 for (int w
= 0; w
< width
; w
++)
399 pixval
= gdk_image_get_pixel( img
, x
, tabley
[h
] );
409 char shift
= bit
<< (w
% 8);
415 dst
[h
*dstbyteperline
+w
/8] = outbyte
;
423 gdk_gc_set_foreground( gc
, &col
);
424 gdk_draw_point( dstpix
, gc
, w
, h
);
428 // do not forget the last byte
429 if ( dst
&& (width
% 8 != 0) )
430 dst
[h
*dstbyteperline
+width
/8] = outbyte
;
433 g_object_unref (img
);
434 if (gc
) g_object_unref (gc
);
438 bmp
= wxBitmap( (const char *)dst
, width
, height
, 1 );
444 dstbyteperline
= (width
+ 7) / 8;
445 dst
= (char*) malloc(dstbyteperline
*height
);
446 img
= gdk_drawable_get_image(GetMask()->GetBitmap(), 0, 0, GetWidth(), GetHeight());
448 for (int h
= 0; h
< height
; h
++)
452 guint32 old_pixval
= 0;
454 for (int w
= 0; w
< width
; w
++)
462 pixval
= gdk_image_get_pixel( img
, x
, tabley
[h
] );
470 char shift
= bit
<< (w
% 8);
476 dst
[h
*dstbyteperline
+w
/8] = outbyte
;
481 // do not forget the last byte
483 dst
[h
*dstbyteperline
+width
/8] = outbyte
;
485 wxMask
* mask
= new wxMask
;
486 mask
->m_bitmap
= gdk_bitmap_create_from_data( wxGetRootWindow()->window
, (gchar
*) dst
, width
, height
);
490 g_object_unref (img
);
500 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
504 wxCHECK_MSG( image
.Ok(), false, wxT("invalid image") );
505 wxCHECK_MSG( depth
== -1 || depth
== 1, false, wxT("invalid bitmap depth") );
507 if (image
.GetWidth() <= 0 || image
.GetHeight() <= 0)
511 return CreateFromImageAsBitmap(image
);
513 if (image
.HasAlpha())
514 return CreateFromImageAsPixbuf(image
);
516 return CreateFromImageAsPixmap(image
);
519 // conversion to mono bitmap:
520 bool wxBitmap::CreateFromImageAsBitmap(const wxImage
& img
)
522 // convert alpha channel to mask, if it is present:
524 image
.ConvertAlphaToMask();
526 int width
= image
.GetWidth();
527 int height
= image
.GetHeight();
529 SetPixmap( gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 ) );
531 // Create picture image
533 GdkGC
* data_gc
= gdk_gc_new(M_BMPDATA
->m_pixmap
);
536 gdk_gc_set_foreground(data_gc
, &color
);
537 gdk_draw_rectangle(M_BMPDATA
->m_pixmap
, data_gc
, true, 0, 0, width
, height
);
538 GdkImage
* data_image
= gdk_drawable_get_image(M_BMPDATA
->m_pixmap
, 0, 0, width
, height
);
542 GdkImage
*mask_image
= (GdkImage
*) NULL
;
543 GdkGC
* mask_gc
= NULL
;
547 wxMask
* mask
= new wxMask
;
548 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
549 mask_gc
= gdk_gc_new(mask
->m_bitmap
);
550 gdk_gc_set_foreground(mask_gc
, &color
);
551 gdk_draw_rectangle(mask
->m_bitmap
, mask_gc
, true, 0, 0, width
, height
);
552 mask_image
= gdk_drawable_get_image(mask
->m_bitmap
, 0, 0, width
, height
);
557 int r_mask
= image
.GetMaskRed();
558 int g_mask
= image
.GetMaskGreen();
559 int b_mask
= image
.GetMaskBlue();
561 unsigned char* data
= image
.GetData();
564 for (int y
= 0; y
< height
; y
++)
566 for (int x
= 0; x
< width
; x
++)
575 if (mask_image
!= NULL
)
577 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
578 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
581 if ((r
== 255) && (b
== 255) && (g
== 255))
582 gdk_image_put_pixel( data_image
, x
, y
, 0 );
589 gdk_draw_image( GetPixmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
591 g_object_unref (data_image
);
592 g_object_unref (data_gc
);
596 if (mask_image
!= NULL
)
598 gdk_draw_image( GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
600 g_object_unref (mask_image
);
601 g_object_unref (mask_gc
);
607 // conversion to colour bitmap:
608 bool wxBitmap::CreateFromImageAsPixmap(const wxImage
& image
)
610 // alpha is handled by CreateFromImageAsPixbuf
611 wxASSERT(!image
.HasAlpha());
613 int width
= image
.GetWidth();
614 int height
= image
.GetHeight();
616 SetPixmap( gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, -1 ) );
618 GdkGC
*gc
= gdk_gc_new( GetPixmap() );
620 gdk_draw_rgb_image( GetPixmap(),
632 if (!image
.HasMask())
635 wxMask
* mask
= new wxMask
;
636 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
637 GdkGC
* mask_gc
= gdk_gc_new(mask
->m_bitmap
);
640 gdk_gc_set_foreground(mask_gc
, &color
);
641 gdk_draw_rectangle(mask
->m_bitmap
, mask_gc
, true, 0, 0, width
, height
);
642 GdkImage
* mask_image
= gdk_drawable_get_image(mask
->m_bitmap
, 0, 0, width
, height
);
646 int r_mask
= image
.GetMaskRed();
647 int g_mask
= image
.GetMaskGreen();
648 int b_mask
= image
.GetMaskBlue();
650 unsigned char* data
= image
.GetData();
653 for (int y
= 0; y
< height
; y
++)
655 for (int x
= 0; x
< width
; x
++)
664 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
665 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
671 gdk_draw_image( GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
673 g_object_unref (mask_image
);
674 g_object_unref (mask_gc
);
679 bool wxBitmap::CreateFromImageAsPixbuf(const wxImage
& image
)
681 int width
= image
.GetWidth();
682 int height
= image
.GetHeight();
684 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
686 8 /* bits per sample */,
691 wxASSERT( image
.HasAlpha() ); // for now
692 wxASSERT( gdk_pixbuf_get_n_channels(pixbuf
) == 4 );
693 wxASSERT( gdk_pixbuf_get_width(pixbuf
) == width
);
694 wxASSERT( gdk_pixbuf_get_height(pixbuf
) == height
);
696 SetDepth(wxTheApp
->GetGdkVisual()->depth
);
700 unsigned char *in
= image
.GetData();
701 unsigned char *out
= gdk_pixbuf_get_pixels(pixbuf
);
702 unsigned char *alpha
= image
.GetAlpha();
704 int rowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
706 for (int y
= 0; y
< height
; y
++, out
+= rowinc
)
708 for (int x
= 0; x
< width
; x
++, alpha
++, out
+= 4, in
+= 3)
720 wxImage
wxBitmap::ConvertToImage() const
724 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
726 const int w
= GetWidth();
727 const int h
= GetHeight();
729 unsigned char *data
= image
.GetData();
731 wxCHECK_MSG(data
!= NULL
, wxNullImage
, wxT("couldn't create image") );
735 GdkPixbuf
*pixbuf
= GetPixbuf();
736 wxASSERT( gdk_pixbuf_get_has_alpha(pixbuf
) );
740 unsigned char *alpha
= image
.GetAlpha();
741 unsigned char *in
= gdk_pixbuf_get_pixels(pixbuf
);
742 unsigned char *out
= data
;
743 int rowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * w
;
745 for (int y
= 0; y
< h
; y
++, in
+= rowinc
)
747 for (int x
= 0; x
< w
; x
++, in
+= 4, out
+= 3, alpha
++)
758 GdkPixmap
* pixmap
= GetPixmap();
759 GdkPixmap
* pixmap_invert
= NULL
;
762 // mono bitmaps are inverted
763 pixmap_invert
= gdk_pixmap_new(pixmap
, w
, h
, 1);
764 GdkGC
* gc
= gdk_gc_new(pixmap_invert
);
765 gdk_gc_set_function(gc
, GDK_COPY_INVERT
);
766 gdk_draw_drawable(pixmap_invert
, gc
, pixmap
, 0, 0, 0, 0, w
, h
);
768 pixmap
= pixmap_invert
;
770 // create a pixbuf which shares data with the wxImage
771 GdkPixbuf
* pixbuf
= gdk_pixbuf_new_from_data(
772 data
, GDK_COLORSPACE_RGB
, false, 8, w
, h
, 3 * w
, NULL
, NULL
);
774 gdk_pixbuf_get_from_drawable(pixbuf
, pixmap
, NULL
, 0, 0, 0, 0, w
, h
);
776 g_object_unref(pixbuf
);
777 if (pixmap_invert
!= NULL
)
778 g_object_unref(pixmap_invert
);
782 // the colour used as transparent one in wxImage and the one it is
783 // replaced with when it really occurs in the bitmap
784 const int MASK_RED
= 1;
785 const int MASK_GREEN
= 2;
786 const int MASK_BLUE
= 3;
787 const int MASK_BLUE_REPLACEMENT
= 2;
789 image
.SetMaskColour(MASK_RED
, MASK_GREEN
, MASK_BLUE
);
790 GdkImage
* image_mask
= gdk_drawable_get_image(GetMask()->GetBitmap(), 0, 0, w
, h
);
792 for (int y
= 0; y
< h
; y
++)
794 for (int x
= 0; x
< w
; x
++, data
+= 3)
796 if (gdk_image_get_pixel(image_mask
, x
, y
) == 0)
799 data
[1] = MASK_GREEN
;
802 else if (data
[0] == MASK_RED
&& data
[1] == MASK_GREEN
&& data
[2] == MASK_BLUE
)
804 data
[2] = MASK_BLUE_REPLACEMENT
;
808 g_object_unref(image_mask
);
815 wxBitmap::wxBitmap( const wxString
&filename
, wxBitmapType type
)
817 LoadFile( filename
, type
);
820 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int WXUNUSED(depth
))
822 if ( width
> 0 && height
> 0 )
824 SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window
, bits
, width
, height
));
826 wxASSERT_MSG( M_BMPDATA
->m_pixmap
, wxT("couldn't create bitmap") );
830 wxBitmap::~wxBitmap()
834 bool wxBitmap::operator == ( const wxBitmap
& bmp
) const
836 return m_refData
== bmp
.m_refData
;
839 bool wxBitmap::operator != ( const wxBitmap
& bmp
) const
841 return m_refData
!= bmp
.m_refData
;
844 bool wxBitmap::Ok() const
846 return (m_refData
!= NULL
) &&
848 M_BMPDATA
->m_pixbuf
||
853 int wxBitmap::GetHeight() const
855 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
857 return M_BMPDATA
->m_height
;
860 int wxBitmap::GetWidth() const
862 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
864 return M_BMPDATA
->m_width
;
867 int wxBitmap::GetDepth() const
869 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
871 return M_BMPDATA
->m_bpp
;
874 wxMask
*wxBitmap::GetMask() const
876 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
878 return M_BMPDATA
->m_mask
;
881 void wxBitmap::SetMask( wxMask
*mask
)
883 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
885 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
887 M_BMPDATA
->m_mask
= mask
;
890 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
896 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
901 (rect
.x
>= 0) && (rect
.y
>= 0) &&
902 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) && (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
903 ret
, wxT("invalid bitmap or bitmap region") );
907 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
908 gdk_pixbuf_get_has_alpha(GetPixbuf()),
909 8, rect
.width
, rect
.height
);
910 ret
.SetPixbuf(pixbuf
);
911 ret
.SetDepth(M_BMPDATA
->m_bpp
);
912 gdk_pixbuf_copy_area(GetPixbuf(),
913 rect
.x
, rect
.y
, rect
.width
, rect
.height
,
918 ret
= wxBitmap(rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
919 if (M_BMPDATA
->m_bpp
!= 1)
921 GdkGC
*gc
= gdk_gc_new( ret
.GetPixmap() );
922 gdk_draw_drawable( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
927 GdkGC
*gc
= gdk_gc_new( ret
.GetPixmap() );
929 col
.pixel
= 0xFFFFFF;
930 gdk_gc_set_foreground( gc
, &col
);
932 gdk_gc_set_background( gc
, &col
);
933 gdk_wx_draw_bitmap( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
940 wxMask
*mask
= new wxMask
;
941 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, rect
.width
, rect
.height
, 1 );
943 GdkGC
*gc
= gdk_gc_new( mask
->m_bitmap
);
945 col
.pixel
= 0xFFFFFF;
946 gdk_gc_set_foreground( gc
, &col
);
948 gdk_gc_set_background( gc
, &col
);
949 gdk_wx_draw_bitmap( mask
->m_bitmap
, gc
, M_BMPDATA
->m_mask
->m_bitmap
, rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
958 bool wxBitmap::SaveFile( const wxString
&name
, wxBitmapType type
, const wxPalette
*WXUNUSED(palette
) ) const
960 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
962 // Try to save the bitmap via wxImage handlers:
963 wxImage image
= ConvertToImage();
964 return image
.Ok() && image
.SaveFile(name
, type
);
967 bool wxBitmap::LoadFile( const wxString
&name
, wxBitmapType type
)
971 if (type
== wxBITMAP_TYPE_XPM
)
973 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
974 SetPixmap(gdk_pixmap_create_from_xpm(wxGetRootWindow()->window
, &mask
, NULL
, name
.fn_str()));
978 M_BMPDATA
->m_mask
= new wxMask
;
979 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
982 else // try if wxImage can load it
985 if (image
.LoadFile(name
, type
) && image
.Ok())
986 *this = wxBitmap(image
);
993 wxPalette
*wxBitmap::GetPalette() const
996 return (wxPalette
*) NULL
;
998 return M_BMPDATA
->m_palette
;
1001 void wxBitmap::SetPalette(const wxPalette
& WXUNUSED(palette
))
1005 #endif // wxUSE_PALETTE
1007 void wxBitmap::SetHeight( int height
)
1010 m_refData
= new wxBitmapRefData
;
1012 M_BMPDATA
->m_height
= height
;
1015 void wxBitmap::SetWidth( int width
)
1018 m_refData
= new wxBitmapRefData
;
1020 M_BMPDATA
->m_width
= width
;
1023 void wxBitmap::SetDepth( int depth
)
1026 m_refData
= new wxBitmapRefData
;
1028 M_BMPDATA
->m_bpp
= depth
;
1031 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
1034 m_refData
= new wxBitmapRefData
;
1036 wxASSERT(M_BMPDATA
->m_pixmap
== NULL
);
1037 M_BMPDATA
->m_pixmap
= pixmap
;
1038 gdk_drawable_get_size(pixmap
, &M_BMPDATA
->m_width
, &M_BMPDATA
->m_height
);
1039 M_BMPDATA
->m_bpp
= gdk_drawable_get_depth(pixmap
);
1040 PurgeOtherRepresentations(Pixmap
);
1043 GdkPixmap
*wxBitmap::GetPixmap() const
1045 wxCHECK_MSG( Ok(), (GdkPixmap
*) NULL
, wxT("invalid bitmap") );
1047 // create the pixmap on the fly if we use Pixbuf representation:
1048 if (M_BMPDATA
->m_pixmap
== NULL
)
1050 delete M_BMPDATA
->m_mask
;
1051 M_BMPDATA
->m_mask
= new wxMask
;
1052 gdk_pixbuf_render_pixmap_and_mask(M_BMPDATA
->m_pixbuf
,
1053 &M_BMPDATA
->m_pixmap
,
1054 &M_BMPDATA
->m_mask
->m_bitmap
,
1058 return M_BMPDATA
->m_pixmap
;
1061 bool wxBitmap::HasPixmap() const
1063 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1065 return M_BMPDATA
->m_pixmap
!= NULL
;
1068 GdkPixbuf
*wxBitmap::GetPixbuf() const
1070 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
1072 if (M_BMPDATA
->m_pixbuf
== NULL
)
1074 int width
= GetWidth();
1075 int height
= GetHeight();
1077 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
1080 M_BMPDATA
->m_pixbuf
=
1081 gdk_pixbuf_get_from_drawable(pixbuf
, M_BMPDATA
->m_pixmap
, NULL
,
1082 0, 0, 0, 0, width
, height
);
1084 // apply the mask to created pixbuf:
1085 if (M_BMPDATA
->m_pixbuf
&& M_BMPDATA
->m_mask
)
1088 gdk_pixbuf_get_from_drawable(NULL
,
1089 M_BMPDATA
->m_mask
->GetBitmap(),
1091 0, 0, 0, 0, width
, height
);
1094 guchar
*bmp
= gdk_pixbuf_get_pixels(pixbuf
);
1095 guchar
*mask
= gdk_pixbuf_get_pixels(pmask
);
1096 int bmprowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
1097 int maskrowinc
= gdk_pixbuf_get_rowstride(pmask
) - 3 * width
;
1099 for (int y
= 0; y
< height
;
1100 y
++, bmp
+= bmprowinc
, mask
+= maskrowinc
)
1102 for (int x
= 0; x
< width
; x
++, bmp
+= 4, mask
+= 3)
1104 if (mask
[0] == 0 /*black pixel*/)
1109 g_object_unref (pmask
);
1114 return M_BMPDATA
->m_pixbuf
;
1117 bool wxBitmap::HasPixbuf() const
1119 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1121 return M_BMPDATA
->m_pixbuf
!= NULL
;
1124 void wxBitmap::SetPixbuf( GdkPixbuf
*pixbuf
)
1127 m_refData
= new wxBitmapRefData
;
1129 wxASSERT(M_BMPDATA
->m_pixbuf
== NULL
);
1130 M_BMPDATA
->m_pixbuf
= pixbuf
;
1131 M_BMPDATA
->m_width
= gdk_pixbuf_get_width(pixbuf
);
1132 M_BMPDATA
->m_height
= gdk_pixbuf_get_height(pixbuf
);
1133 PurgeOtherRepresentations(Pixbuf
);
1136 void wxBitmap::PurgeOtherRepresentations(wxBitmap::Representation keep
)
1138 if (keep
== Pixmap
&& HasPixbuf())
1140 g_object_unref (M_BMPDATA
->m_pixbuf
);
1141 M_BMPDATA
->m_pixbuf
= NULL
;
1143 if (keep
== Pixbuf
&& HasPixmap())
1145 g_object_unref (M_BMPDATA
->m_pixmap
);
1146 M_BMPDATA
->m_pixmap
= NULL
;
1150 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1155 GdkPixbuf
*pixbuf
= GetPixbuf();
1160 if (gdk_pixbuf_get_has_alpha( pixbuf
))
1161 wxPrintf( wxT("Has alpha\n") );
1163 wxPrintf( wxT("No alpha.\n") );
1166 data
.m_height
= gdk_pixbuf_get_height( pixbuf
);
1167 data
.m_width
= gdk_pixbuf_get_width( pixbuf
);
1168 data
.m_stride
= gdk_pixbuf_get_rowstride( pixbuf
);
1170 return gdk_pixbuf_get_pixels( pixbuf
);
1173 void wxBitmap::UngetRawData(wxPixelDataBase
& WXUNUSED(data
))
1178 bool wxBitmap::HasAlpha() const
1183 void wxBitmap::UseAlpha()
1188 //-----------------------------------------------------------------------------
1190 //-----------------------------------------------------------------------------
1192 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
,wxBitmapHandlerBase
)
1194 wxBitmapHandler::~wxBitmapHandler()
1198 bool wxBitmapHandler::Create(wxBitmap
* WXUNUSED(bitmap
),
1199 void * WXUNUSED(data
),
1200 long WXUNUSED(type
),
1201 int WXUNUSED(width
),
1202 int WXUNUSED(height
),
1203 int WXUNUSED(depth
))
1205 wxFAIL_MSG( _T("not implemented") );
1210 bool wxBitmapHandler::LoadFile(wxBitmap
* WXUNUSED(bitmap
),
1211 const wxString
& WXUNUSED(name
),
1212 long WXUNUSED(flags
),
1213 int WXUNUSED(desiredWidth
),
1214 int WXUNUSED(desiredHeight
))
1216 wxFAIL_MSG( _T("not implemented") );
1221 bool wxBitmapHandler::SaveFile(const wxBitmap
* WXUNUSED(bitmap
),
1222 const wxString
& WXUNUSED(name
),
1224 const wxPalette
* WXUNUSED(palette
))
1226 wxFAIL_MSG( _T("not implemented") );
1231 /* static */ void wxBitmap::InitStandardHandlers()
1233 // TODO: Insert handler based on GdkPixbufs handler later