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/dcmemory.h"
18 #include "wx/palette.h"
24 #include "wx/filefn.h"
26 #include "wx/rawbmp.h"
27 // need this to get gdk_image_new_bitmap()
28 #define GDK_ENABLE_BROKEN
34 #include <gdk/gdkimage.h>
36 extern void gdk_wx_draw_bitmap (GdkDrawable
*drawable
,
46 //-----------------------------------------------------------------------------
48 //-----------------------------------------------------------------------------
50 extern GtkWidget
*wxGetRootWindow();
52 //-----------------------------------------------------------------------------
54 //-----------------------------------------------------------------------------
56 IMPLEMENT_DYNAMIC_CLASS(wxMask
,wxObject
)
60 m_bitmap
= (GdkBitmap
*) NULL
;
63 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
65 m_bitmap
= (GdkBitmap
*) NULL
;
66 Create( bitmap
, colour
);
70 wxMask::wxMask( const wxBitmap
& bitmap
, int paletteIndex
)
72 m_bitmap
= (GdkBitmap
*) NULL
;
73 Create( bitmap
, paletteIndex
);
75 #endif // wxUSE_PALETTE
77 wxMask::wxMask( const wxBitmap
& bitmap
)
79 m_bitmap
= (GdkBitmap
*) NULL
;
86 g_object_unref (m_bitmap
);
89 bool wxMask::Create( const wxBitmap
& bitmap
,
90 const wxColour
& colour
)
94 g_object_unref (m_bitmap
);
95 m_bitmap
= (GdkBitmap
*) NULL
;
98 wxImage image
= bitmap
.ConvertToImage();
99 if (!image
.Ok()) return false;
101 m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, image
.GetWidth(), image
.GetHeight(), 1 );
102 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
109 gdk_gc_set_foreground( gc
, &color
);
110 gdk_gc_set_fill( gc
, GDK_SOLID
);
111 gdk_draw_rectangle( m_bitmap
, gc
, TRUE
, 0, 0, image
.GetWidth(), image
.GetHeight() );
113 unsigned char *data
= image
.GetData();
116 unsigned char red
= colour
.Red();
117 unsigned char green
= colour
.Green();
118 unsigned char blue
= colour
.Blue();
120 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
122 int bpp
= visual
->depth
;
123 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800))
128 green
= green
& 0xf8;
134 green
= green
& 0xfc;
140 green
= green
& 0xf0;
148 gdk_gc_set_foreground( gc
, &color
);
150 for (int j
= 0; j
< image
.GetHeight(); j
++)
154 for (i
= 0; i
< image
.GetWidth(); i
++)
156 if ((data
[index
] == red
) &&
157 (data
[index
+1] == green
) &&
158 (data
[index
+2] == blue
))
167 gdk_draw_line( m_bitmap
, gc
, start_x
, j
, i
-1, j
);
174 gdk_draw_line( m_bitmap
, gc
, start_x
, j
, i
, j
);
183 bool wxMask::Create( const wxBitmap
& bitmap
, int paletteIndex
)
186 wxPalette
*pal
= bitmap
.GetPalette();
188 wxCHECK_MSG( pal
, false, wxT("Cannot create mask from bitmap without palette") );
190 pal
->GetRGB(paletteIndex
, &r
, &g
, &b
);
192 return Create(bitmap
, wxColour(r
, g
, b
));
194 #endif // wxUSE_PALETTE
196 bool wxMask::Create( const wxBitmap
& bitmap
)
200 g_object_unref (m_bitmap
);
201 m_bitmap
= (GdkBitmap
*) NULL
;
204 if (!bitmap
.Ok()) return false;
206 wxCHECK_MSG( bitmap
.GetDepth() == 1, false, wxT("Cannot create mask from colour bitmap") );
208 m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, bitmap
.GetWidth(), bitmap
.GetHeight(), 1 );
210 if (!m_bitmap
) return false;
212 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
214 gdk_wx_draw_bitmap( m_bitmap
, gc
, bitmap
.GetPixmap(), 0, 0, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight() );
221 GdkBitmap
*wxMask::GetBitmap() const
226 //-----------------------------------------------------------------------------
228 //-----------------------------------------------------------------------------
230 class wxBitmapRefData
: public wxObjectRefData
242 wxPalette
*m_palette
;
245 wxBitmapRefData::wxBitmapRefData()
247 m_pixmap
= (GdkPixmap
*) NULL
;
248 m_pixbuf
= (GdkPixbuf
*) NULL
;
249 m_mask
= (wxMask
*) NULL
;
253 m_palette
= (wxPalette
*) NULL
;
256 wxBitmapRefData::~wxBitmapRefData()
259 g_object_unref (m_pixmap
);
261 g_object_unref (m_pixbuf
);
265 #endif // wxUSE_PALETTE
268 //-----------------------------------------------------------------------------
270 #define M_BMPDATA wx_static_cast(wxBitmapRefData*, m_refData)
272 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
278 wxBitmap::wxBitmap( int width
, int height
, int depth
)
280 Create( width
, height
, depth
);
283 bool wxBitmap::Create( int width
, int height
, int depth
)
287 if ( width
<= 0 || height
<= 0 )
292 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
295 depth
= visual
->depth
;
297 wxCHECK_MSG( (depth
== visual
->depth
) || (depth
== 1) || (depth
== 32), false,
298 wxT("invalid bitmap depth") );
300 m_refData
= new wxBitmapRefData();
301 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
302 M_BMPDATA
->m_width
= width
;
303 M_BMPDATA
->m_height
= height
;
304 M_BMPDATA
->m_bpp
= depth
;
307 M_BMPDATA
->m_pixbuf
= gdk_pixbuf_new( GDK_COLORSPACE_RGB
, true,
312 M_BMPDATA
->m_pixmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, depth
);
318 bool wxBitmap::CreateFromXpm( const char **bits
)
322 wxCHECK_MSG( bits
!= NULL
, false, wxT("invalid bitmap data") );
324 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
326 m_refData
= new wxBitmapRefData();
328 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
330 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm_d( wxGetRootWindow()->window
, &mask
, NULL
, (gchar
**) bits
);
332 wxCHECK_MSG( M_BMPDATA
->m_pixmap
, false, wxT("couldn't create pixmap") );
336 M_BMPDATA
->m_mask
= new wxMask();
337 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
340 gdk_drawable_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
342 M_BMPDATA
->m_bpp
= visual
->depth
; // Can we get a different depth from create_from_xpm_d() ?
347 wxBitmap
wxBitmap::Rescale( int clipx
, int clipy
, int clipwidth
, int clipheight
, int newx
, int newy
)
349 wxCHECK_MSG( Ok(), wxNullBitmap
, wxT("invalid bitmap") );
351 if (newy
==M_BMPDATA
->m_width
&& newy
==M_BMPDATA
->m_height
)
354 int width
= wxMax(newx
, 1);
355 int height
= wxMax(newy
, 1);
356 width
= wxMin(width
, clipwidth
);
357 height
= wxMin(height
, clipheight
);
364 bmp
.SetHeight(height
);
365 bmp
.SetDepth(GetDepth());
366 bmp
.SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
367 gdk_pixbuf_get_has_alpha(GetPixbuf()),
369 gdk_pixbuf_scale(GetPixbuf(), bmp
.GetPixbuf(),
372 (double)newx
/GetWidth(), (double)newy
/GetHeight(),
373 GDK_INTERP_BILINEAR
);
377 GdkImage
*img
= (GdkImage
*) NULL
;
379 img
= gdk_image_get( GetPixmap(), 0, 0, GetWidth(), GetHeight() );
381 wxFAIL_MSG( wxT("Ill-formed bitmap") );
383 wxCHECK_MSG( img
, wxNullBitmap
, wxT("couldn't create image") );
389 GdkPixmap
*dstpix
= NULL
;
392 GdkVisual
*visual
= gdk_drawable_get_visual( GetPixmap() );
394 visual
= wxTheApp
->GetGdkVisual();
397 bmp
= wxBitmap(width
,height
,bpp
);
398 dstpix
= bmp
.GetPixmap();
399 gc
= gdk_gc_new( dstpix
);
403 long dstbyteperline
= 0;
408 dstbyteperline
= width
/8*M_BMPDATA
->m_bpp
;
409 if (width
*M_BMPDATA
->m_bpp
% 8 != 0)
411 dst
= (char*) malloc(dstbyteperline
*height
);
414 // be careful to use the right scaling factor
415 float scx
= (float)M_BMPDATA
->m_width
/(float)newx
;
416 float scy
= (float)M_BMPDATA
->m_height
/(float)newy
;
417 // prepare accel-tables
418 int *tablex
= (int *)calloc(width
,sizeof(int));
419 int *tabley
= (int *)calloc(height
,sizeof(int));
421 // accel table filled with clipped values
422 for (int x
= 0; x
< width
; x
++)
423 tablex
[x
] = (int) (scx
* (x
+clipx
));
424 for (int y
= 0; y
< height
; y
++)
425 tabley
[y
] = (int) (scy
* (y
+clipy
));
427 // Main rescaling routine starts here
428 for (int h
= 0; h
< height
; h
++)
432 guint32 old_pixval
= 0;
434 for (int w
= 0; w
< width
; w
++)
442 pixval
= gdk_image_get_pixel( img
, x
, tabley
[h
] );
452 char shift
= bit
<< (w
% 8);
458 dst
[h
*dstbyteperline
+w
/8] = outbyte
;
466 gdk_gc_set_foreground( gc
, &col
);
467 gdk_draw_point( dstpix
, gc
, w
, h
);
471 // do not forget the last byte
472 if ( dst
&& (width
% 8 != 0) )
473 dst
[h
*dstbyteperline
+width
/8] = outbyte
;
476 g_object_unref (img
);
477 if (gc
) g_object_unref (gc
);
481 bmp
= wxBitmap( (const char *)dst
, width
, height
, 1 );
487 dstbyteperline
= width
/8;
490 dst
= (char*) malloc(dstbyteperline
*height
);
491 img
= gdk_image_get( GetMask()->GetBitmap(), 0, 0, GetWidth(), GetHeight() );
493 for (int h
= 0; h
< height
; h
++)
497 guint32 old_pixval
= 0;
499 for (int w
= 0; w
< width
; w
++)
507 pixval
= gdk_image_get_pixel( img
, x
, tabley
[h
] );
515 char shift
= bit
<< (w
% 8);
521 dst
[h
*dstbyteperline
+w
/8] = outbyte
;
526 // do not forget the last byte
528 dst
[h
*dstbyteperline
+width
/8] = outbyte
;
530 wxMask
* mask
= new wxMask
;
531 mask
->m_bitmap
= gdk_bitmap_create_from_data( wxGetRootWindow()->window
, (gchar
*) dst
, width
, height
);
535 g_object_unref (img
);
545 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
549 wxCHECK_MSG( image
.Ok(), false, wxT("invalid image") );
550 wxCHECK_MSG( depth
== -1 || depth
== 1, false, wxT("invalid bitmap depth") );
552 if (image
.GetWidth() <= 0 || image
.GetHeight() <= 0)
555 m_refData
= new wxBitmapRefData();
559 return CreateFromImageAsBitmap(image
);
563 if (image
.HasAlpha())
564 return CreateFromImageAsPixbuf(image
);
566 return CreateFromImageAsPixmap(image
);
570 // conversion to mono bitmap:
571 bool wxBitmap::CreateFromImageAsBitmap(const wxImage
& img
)
573 // convert alpha channel to mask, if it is present:
575 image
.ConvertAlphaToMask();
577 int width
= image
.GetWidth();
578 int height
= image
.GetHeight();
583 SetPixmap( gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 ) );
587 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
589 // Create picture image
591 unsigned char *data_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
593 GdkImage
*data_image
=
594 gdk_image_new_bitmap( visual
, data_data
, width
, height
);
598 GdkImage
*mask_image
= (GdkImage
*) NULL
;
602 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
604 mask_image
= gdk_image_new_bitmap( visual
, mask_data
, width
, height
);
606 wxMask
*mask
= new wxMask();
607 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
612 int r_mask
= image
.GetMaskRed();
613 int g_mask
= image
.GetMaskGreen();
614 int b_mask
= image
.GetMaskBlue();
616 unsigned char* data
= image
.GetData();
619 for (int y
= 0; y
< height
; y
++)
621 for (int x
= 0; x
< width
; x
++)
632 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
633 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
635 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
638 if ((r
== 255) && (b
== 255) && (g
== 255))
639 gdk_image_put_pixel( data_image
, x
, y
, 1 );
641 gdk_image_put_pixel( data_image
, x
, y
, 0 );
648 GdkGC
*data_gc
= gdk_gc_new( GetPixmap() );
650 gdk_draw_image( GetPixmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
652 g_object_unref (data_image
);
653 g_object_unref (data_gc
);
659 GdkGC
*mask_gc
= gdk_gc_new( GetMask()->GetBitmap() );
661 gdk_draw_image( GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
663 g_object_unref (mask_image
);
664 g_object_unref (mask_gc
);
670 // conversion to colour bitmap:
671 bool wxBitmap::CreateFromImageAsPixmap(const wxImage
& img
)
673 // convert alpha channel to mask, if it is present:
675 image
.ConvertAlphaToMask();
677 int width
= image
.GetWidth();
678 int height
= image
.GetHeight();
683 SetPixmap( gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, -1 ) );
685 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
687 int bpp
= visual
->depth
;
691 GdkGC
*gc
= gdk_gc_new( GetPixmap() );
693 gdk_draw_rgb_image( GetPixmap(),
705 GdkImage
*mask_image
= (GdkImage
*) NULL
;
707 if (!image
.HasMask())
710 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
712 mask_image
= gdk_image_new_bitmap( visual
, mask_data
, width
, height
);
714 wxMask
*mask
= new wxMask();
715 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
719 int r_mask
= image
.GetMaskRed();
720 int g_mask
= image
.GetMaskGreen();
721 int b_mask
= image
.GetMaskBlue();
723 unsigned char* data
= image
.GetData();
726 for (int y
= 0; y
< height
; y
++)
728 for (int x
= 0; x
< width
; x
++)
737 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
738 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
740 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
746 GdkGC
*mask_gc
= gdk_gc_new( GetMask()->GetBitmap() );
748 gdk_draw_image( GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
750 g_object_unref (mask_image
);
751 g_object_unref (mask_gc
);
756 bool wxBitmap::CreateFromImageAsPixbuf(const wxImage
& image
)
758 int width
= image
.GetWidth();
759 int height
= image
.GetHeight();
761 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
763 8 /* bits per sample */,
768 wxASSERT( image
.HasAlpha() ); // for now
769 wxASSERT( gdk_pixbuf_get_n_channels(pixbuf
) == 4 );
770 wxASSERT( gdk_pixbuf_get_width(pixbuf
) == width
);
771 wxASSERT( gdk_pixbuf_get_height(pixbuf
) == height
);
773 M_BMPDATA
->m_pixbuf
= pixbuf
;
776 SetDepth(wxTheApp
->GetGdkVisual()->depth
);
779 unsigned char *in
= image
.GetData();
780 unsigned char *out
= gdk_pixbuf_get_pixels(pixbuf
);
781 unsigned char *alpha
= image
.GetAlpha();
783 int rowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
785 for (int y
= 0; y
< height
; y
++, out
+= rowinc
)
787 for (int x
= 0; x
< width
; x
++, alpha
++, out
+= 4, in
+= 3)
799 wxImage
wxBitmap::ConvertToImage() const
803 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
805 image
.Create(GetWidth(), GetHeight());
806 unsigned char *data
= image
.GetData();
810 wxFAIL_MSG( wxT("couldn't create image") );
816 GdkPixbuf
*pixbuf
= GetPixbuf();
817 wxASSERT( gdk_pixbuf_get_has_alpha(pixbuf
) );
824 unsigned char *alpha
= image
.GetAlpha();
825 unsigned char *in
= gdk_pixbuf_get_pixels(pixbuf
);
826 unsigned char *out
= data
;
827 int rowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * w
;
829 for (int y
= 0; y
< h
; y
++, in
+= rowinc
)
831 for (int x
= 0; x
< w
; x
++, in
+= 4, out
+= 3, alpha
++)
842 // the colour used as transparent one in wxImage and the one it is
843 // replaced with when it really occurs in the bitmap
844 static const int MASK_RED
= 1;
845 static const int MASK_GREEN
= 2;
846 static const int MASK_BLUE
= 3;
847 static const int MASK_BLUE_REPLACEMENT
= 2;
849 GdkImage
*gdk_image
= (GdkImage
*) NULL
;
853 gdk_image
= gdk_image_get( GetPixmap(),
855 GetWidth(), GetHeight() );
859 wxFAIL_MSG( wxT("Ill-formed bitmap") );
862 wxCHECK_MSG( gdk_image
, wxNullImage
, wxT("couldn't create image") );
864 GdkImage
*gdk_image_mask
= (GdkImage
*) NULL
;
867 gdk_image_mask
= gdk_image_get( GetMask()->GetBitmap(),
869 GetWidth(), GetHeight() );
871 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
875 int red_shift_right
= 0;
876 int green_shift_right
= 0;
877 int blue_shift_right
= 0;
878 int red_shift_left
= 0;
879 int green_shift_left
= 0;
880 int blue_shift_left
= 0;
881 bool use_shift
= false;
885 GdkVisual
*visual
= gdk_drawable_get_visual( GetPixmap() );
887 visual
= wxTheApp
->GetGdkVisual();
891 bpp
= visual
->red_prec
+ visual
->green_prec
+ visual
->blue_prec
;
892 red_shift_right
= visual
->red_shift
;
893 red_shift_left
= 8-visual
->red_prec
;
894 green_shift_right
= visual
->green_shift
;
895 green_shift_left
= 8-visual
->green_prec
;
896 blue_shift_right
= visual
->blue_shift
;
897 blue_shift_left
= 8-visual
->blue_prec
;
899 use_shift
= (visual
->type
== GDK_VISUAL_TRUE_COLOR
) || (visual
->type
== GDK_VISUAL_DIRECT_COLOR
);
907 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
910 for (int j
= 0; j
< GetHeight(); j
++)
912 for (int i
= 0; i
< GetWidth(); i
++)
914 wxUint32 pixel
= gdk_image_get_pixel( gdk_image
, i
, j
);
932 data
[pos
] = (pixel
>> red_shift_right
) << red_shift_left
;
933 data
[pos
+1] = (pixel
>> green_shift_right
) << green_shift_left
;
934 data
[pos
+2] = (pixel
>> blue_shift_right
) << blue_shift_left
;
936 else if (cmap
->colors
)
938 data
[pos
] = cmap
->colors
[pixel
].red
>> 8;
939 data
[pos
+1] = cmap
->colors
[pixel
].green
>> 8;
940 data
[pos
+2] = cmap
->colors
[pixel
].blue
>> 8;
944 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
949 int mask_pixel
= gdk_image_get_pixel( gdk_image_mask
, i
, j
);
952 data
[pos
] = MASK_RED
;
953 data
[pos
+1] = MASK_GREEN
;
954 data
[pos
+2] = MASK_BLUE
;
956 else if ( data
[pos
] == MASK_RED
&&
957 data
[pos
+1] == MASK_GREEN
&&
958 data
[pos
+2] == MASK_BLUE
)
960 data
[pos
+2] = MASK_BLUE_REPLACEMENT
;
968 g_object_unref (gdk_image
);
969 if (gdk_image_mask
) g_object_unref (gdk_image_mask
);
975 wxBitmap::wxBitmap( const wxString
&filename
, wxBitmapType type
)
977 LoadFile( filename
, type
);
980 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int WXUNUSED(depth
))
982 if ( width
> 0 && height
> 0 )
984 m_refData
= new wxBitmapRefData();
986 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
987 M_BMPDATA
->m_pixmap
= gdk_bitmap_create_from_data
989 wxGetRootWindow()->window
,
994 M_BMPDATA
->m_width
= width
;
995 M_BMPDATA
->m_height
= height
;
996 M_BMPDATA
->m_bpp
= 1;
998 wxASSERT_MSG( M_BMPDATA
->m_pixmap
, wxT("couldn't create bitmap") );
1002 wxBitmap::~wxBitmap()
1006 bool wxBitmap::operator == ( const wxBitmap
& bmp
) const
1008 return m_refData
== bmp
.m_refData
;
1011 bool wxBitmap::operator != ( const wxBitmap
& bmp
) const
1013 return m_refData
!= bmp
.m_refData
;
1016 bool wxBitmap::Ok() const
1018 return (m_refData
!= NULL
) &&
1020 M_BMPDATA
->m_pixbuf
||
1025 int wxBitmap::GetHeight() const
1027 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1029 return M_BMPDATA
->m_height
;
1032 int wxBitmap::GetWidth() const
1034 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1036 return M_BMPDATA
->m_width
;
1039 int wxBitmap::GetDepth() const
1041 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1043 return M_BMPDATA
->m_bpp
;
1046 wxMask
*wxBitmap::GetMask() const
1048 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
1050 return M_BMPDATA
->m_mask
;
1053 void wxBitmap::SetMask( wxMask
*mask
)
1055 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
1057 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
1059 M_BMPDATA
->m_mask
= mask
;
1062 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
1068 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
1070 wxCHECK_MSG( Ok() &&
1071 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1072 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) && (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
1073 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
1075 wxBitmap
ret( rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
1076 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
1080 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
1081 gdk_pixbuf_get_has_alpha(GetPixbuf()),
1082 8, rect
.width
, rect
.height
);
1083 ret
.SetPixbuf(pixbuf
);
1084 gdk_pixbuf_copy_area(GetPixbuf(),
1085 rect
.x
, rect
.y
, rect
.width
, rect
.height
,
1090 if (ret
.GetDepth() != 1)
1092 GdkGC
*gc
= gdk_gc_new( ret
.GetPixmap() );
1093 gdk_draw_drawable( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1094 g_object_unref (gc
);
1098 GdkGC
*gc
= gdk_gc_new( ret
.GetPixmap() );
1100 col
.pixel
= 0xFFFFFF;
1101 gdk_gc_set_foreground( gc
, &col
);
1103 gdk_gc_set_background( gc
, &col
);
1104 gdk_wx_draw_bitmap( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1105 g_object_unref (gc
);
1111 wxMask
*mask
= new wxMask
;
1112 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, rect
.width
, rect
.height
, 1 );
1114 GdkGC
*gc
= gdk_gc_new( mask
->m_bitmap
);
1116 col
.pixel
= 0xFFFFFF;
1117 gdk_gc_set_foreground( gc
, &col
);
1119 gdk_gc_set_background( gc
, &col
);
1120 gdk_wx_draw_bitmap( mask
->m_bitmap
, gc
, M_BMPDATA
->m_mask
->m_bitmap
, rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1121 g_object_unref (gc
);
1123 ret
.SetMask( mask
);
1129 bool wxBitmap::SaveFile( const wxString
&name
, wxBitmapType type
, const wxPalette
*WXUNUSED(palette
) ) const
1131 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1133 // Try to save the bitmap via wxImage handlers:
1135 wxImage image
= ConvertToImage();
1136 if (image
.Ok()) return image
.SaveFile( name
, type
);
1142 bool wxBitmap::LoadFile( const wxString
&name
, wxBitmapType type
)
1146 if (!wxFileExists(name
))
1149 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
1151 if (type
== wxBITMAP_TYPE_XPM
)
1153 m_refData
= new wxBitmapRefData();
1155 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
1157 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm
1159 wxGetRootWindow()->window
,
1167 M_BMPDATA
->m_mask
= new wxMask();
1168 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
1171 gdk_drawable_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
1173 M_BMPDATA
->m_bpp
= visual
->depth
;
1175 else // try if wxImage can load it
1178 if ( !image
.LoadFile( name
, type
) || !image
.Ok() )
1181 *this = wxBitmap(image
);
1188 wxPalette
*wxBitmap::GetPalette() const
1191 return (wxPalette
*) NULL
;
1193 return M_BMPDATA
->m_palette
;
1196 void wxBitmap::SetPalette(const wxPalette
& WXUNUSED(palette
))
1200 #endif // wxUSE_PALETTE
1202 void wxBitmap::SetHeight( int height
)
1205 m_refData
= new wxBitmapRefData();
1207 M_BMPDATA
->m_height
= height
;
1210 void wxBitmap::SetWidth( int width
)
1213 m_refData
= new wxBitmapRefData();
1215 M_BMPDATA
->m_width
= width
;
1218 void wxBitmap::SetDepth( int depth
)
1221 m_refData
= new wxBitmapRefData();
1223 M_BMPDATA
->m_bpp
= depth
;
1226 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
1229 m_refData
= new wxBitmapRefData();
1231 M_BMPDATA
->m_pixmap
= pixmap
;
1232 PurgeOtherRepresentations(Pixmap
);
1235 GdkPixmap
*wxBitmap::GetPixmap() const
1237 wxCHECK_MSG( Ok(), (GdkPixmap
*) NULL
, wxT("invalid bitmap") );
1239 // create the pixmap on the fly if we use Pixbuf representation:
1240 if (HasPixbuf() && !HasPixmap())
1242 delete M_BMPDATA
->m_mask
;
1243 M_BMPDATA
->m_mask
= new wxMask();
1244 gdk_pixbuf_render_pixmap_and_mask(M_BMPDATA
->m_pixbuf
,
1245 &M_BMPDATA
->m_pixmap
,
1246 &M_BMPDATA
->m_mask
->m_bitmap
,
1250 return M_BMPDATA
->m_pixmap
;
1253 bool wxBitmap::HasPixmap() const
1255 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1257 return M_BMPDATA
->m_pixmap
!= NULL
;
1260 GdkPixbuf
*wxBitmap::GetPixbuf() const
1262 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
1264 if (HasPixmap() && !HasPixbuf())
1266 int width
= GetWidth();
1267 int height
= GetHeight();
1269 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
1272 M_BMPDATA
->m_pixbuf
=
1273 gdk_pixbuf_get_from_drawable(pixbuf
, M_BMPDATA
->m_pixmap
, NULL
,
1274 0, 0, 0, 0, width
, height
);
1276 // apply the mask to created pixbuf:
1277 if (M_BMPDATA
->m_pixbuf
&& M_BMPDATA
->m_mask
)
1280 gdk_pixbuf_get_from_drawable(NULL
,
1281 M_BMPDATA
->m_mask
->GetBitmap(),
1283 0, 0, 0, 0, width
, height
);
1286 guchar
*bmp
= gdk_pixbuf_get_pixels(pixbuf
);
1287 guchar
*mask
= gdk_pixbuf_get_pixels(pmask
);
1288 int bmprowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
1289 int maskrowinc
= gdk_pixbuf_get_rowstride(pmask
) - 3 * width
;
1291 for (int y
= 0; y
< height
;
1292 y
++, bmp
+= bmprowinc
, mask
+= maskrowinc
)
1294 for (int x
= 0; x
< width
; x
++, bmp
+= 4, mask
+= 3)
1296 if (mask
[0] == 0 /*black pixel*/)
1301 g_object_unref (pmask
);
1306 return M_BMPDATA
->m_pixbuf
;
1309 bool wxBitmap::HasPixbuf() const
1311 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1313 return M_BMPDATA
->m_pixbuf
!= NULL
;
1316 void wxBitmap::SetPixbuf( GdkPixbuf
*pixbuf
)
1319 m_refData
= new wxBitmapRefData();
1321 M_BMPDATA
->m_pixbuf
= pixbuf
;
1322 PurgeOtherRepresentations(Pixbuf
);
1325 void wxBitmap::PurgeOtherRepresentations(wxBitmap::Representation keep
)
1327 if (keep
== Pixmap
&& HasPixbuf())
1329 g_object_unref (M_BMPDATA
->m_pixbuf
);
1330 M_BMPDATA
->m_pixbuf
= NULL
;
1332 if (keep
== Pixbuf
&& HasPixmap())
1334 g_object_unref (M_BMPDATA
->m_pixmap
);
1335 M_BMPDATA
->m_pixmap
= NULL
;
1339 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1344 GdkPixbuf
*pixbuf
= GetPixbuf();
1349 if (gdk_pixbuf_get_has_alpha( pixbuf
))
1350 wxPrintf( wxT("Has alpha\n") );
1352 wxPrintf( wxT("No alpha.\n") );
1355 data
.m_height
= gdk_pixbuf_get_height( pixbuf
);
1356 data
.m_width
= gdk_pixbuf_get_width( pixbuf
);
1357 data
.m_stride
= gdk_pixbuf_get_rowstride( pixbuf
);
1359 return gdk_pixbuf_get_pixels( pixbuf
);
1362 void wxBitmap::UngetRawData(wxPixelDataBase
& WXUNUSED(data
))
1367 bool wxBitmap::HasAlpha() const
1372 void wxBitmap::UseAlpha()
1377 //-----------------------------------------------------------------------------
1379 //-----------------------------------------------------------------------------
1381 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
,wxBitmapHandlerBase
)
1383 wxBitmapHandler::~wxBitmapHandler()
1387 bool wxBitmapHandler::Create(wxBitmap
* WXUNUSED(bitmap
),
1388 void * WXUNUSED(data
),
1389 long WXUNUSED(type
),
1390 int WXUNUSED(width
),
1391 int WXUNUSED(height
),
1392 int WXUNUSED(depth
))
1394 wxFAIL_MSG( _T("not implemented") );
1399 bool wxBitmapHandler::LoadFile(wxBitmap
* WXUNUSED(bitmap
),
1400 const wxString
& WXUNUSED(name
),
1401 long WXUNUSED(flags
),
1402 int WXUNUSED(desiredWidth
),
1403 int WXUNUSED(desiredHeight
))
1405 wxFAIL_MSG( _T("not implemented") );
1410 bool wxBitmapHandler::SaveFile(const wxBitmap
* WXUNUSED(bitmap
),
1411 const wxString
& WXUNUSED(name
),
1413 const wxPalette
* WXUNUSED(palette
))
1415 wxFAIL_MSG( _T("not implemented") );
1420 /* static */ void wxBitmap::InitStandardHandlers()
1422 // TODO: Insert handler based on GdkPixbufs handler later