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"
32 #include <gdk/gdkimage.h>
34 extern void gdk_wx_draw_bitmap (GdkDrawable
*drawable
,
44 //-----------------------------------------------------------------------------
46 //-----------------------------------------------------------------------------
48 extern GtkWidget
*wxGetRootWindow();
50 //-----------------------------------------------------------------------------
52 //-----------------------------------------------------------------------------
54 IMPLEMENT_DYNAMIC_CLASS(wxMask
,wxObject
)
58 m_bitmap
= (GdkBitmap
*) NULL
;
61 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
63 m_bitmap
= (GdkBitmap
*) NULL
;
64 Create( bitmap
, colour
);
68 wxMask::wxMask( const wxBitmap
& bitmap
, int paletteIndex
)
70 m_bitmap
= (GdkBitmap
*) NULL
;
71 Create( bitmap
, paletteIndex
);
73 #endif // wxUSE_PALETTE
75 wxMask::wxMask( const wxBitmap
& bitmap
)
77 m_bitmap
= (GdkBitmap
*) NULL
;
84 g_object_unref (m_bitmap
);
87 bool wxMask::Create( const wxBitmap
& bitmap
,
88 const wxColour
& colour
)
92 g_object_unref (m_bitmap
);
93 m_bitmap
= (GdkBitmap
*) NULL
;
96 wxImage image
= bitmap
.ConvertToImage();
97 if (!image
.Ok()) return false;
99 m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, image
.GetWidth(), image
.GetHeight(), 1 );
100 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
107 gdk_gc_set_foreground( gc
, &color
);
108 gdk_gc_set_fill( gc
, GDK_SOLID
);
109 gdk_draw_rectangle( m_bitmap
, gc
, TRUE
, 0, 0, image
.GetWidth(), image
.GetHeight() );
111 unsigned char *data
= image
.GetData();
114 unsigned char red
= colour
.Red();
115 unsigned char green
= colour
.Green();
116 unsigned char blue
= colour
.Blue();
118 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
120 int bpp
= visual
->depth
;
121 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800))
126 green
= green
& 0xf8;
132 green
= green
& 0xfc;
138 green
= green
& 0xf0;
146 gdk_gc_set_foreground( gc
, &color
);
148 for (int j
= 0; j
< image
.GetHeight(); j
++)
152 for (i
= 0; i
< image
.GetWidth(); i
++)
154 if ((data
[index
] == red
) &&
155 (data
[index
+1] == green
) &&
156 (data
[index
+2] == blue
))
165 gdk_draw_line( m_bitmap
, gc
, start_x
, j
, i
-1, j
);
172 gdk_draw_line( m_bitmap
, gc
, start_x
, j
, i
, j
);
181 bool wxMask::Create( const wxBitmap
& bitmap
, int paletteIndex
)
184 wxPalette
*pal
= bitmap
.GetPalette();
186 wxCHECK_MSG( pal
, false, wxT("Cannot create mask from bitmap without palette") );
188 pal
->GetRGB(paletteIndex
, &r
, &g
, &b
);
190 return Create(bitmap
, wxColour(r
, g
, b
));
192 #endif // wxUSE_PALETTE
194 bool wxMask::Create( const wxBitmap
& bitmap
)
198 g_object_unref (m_bitmap
);
199 m_bitmap
= (GdkBitmap
*) NULL
;
202 if (!bitmap
.Ok()) return false;
204 wxCHECK_MSG( bitmap
.GetDepth() == 1, false, wxT("Cannot create mask from colour bitmap") );
206 m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, bitmap
.GetWidth(), bitmap
.GetHeight(), 1 );
208 if (!m_bitmap
) return false;
210 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
212 gdk_wx_draw_bitmap( m_bitmap
, gc
, bitmap
.GetPixmap(), 0, 0, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight() );
219 GdkBitmap
*wxMask::GetBitmap() const
224 //-----------------------------------------------------------------------------
226 //-----------------------------------------------------------------------------
228 class wxBitmapRefData
: public wxObjectRefData
240 wxPalette
*m_palette
;
243 wxBitmapRefData::wxBitmapRefData()
245 m_pixmap
= (GdkPixmap
*) NULL
;
246 m_pixbuf
= (GdkPixbuf
*) NULL
;
247 m_mask
= (wxMask
*) NULL
;
251 m_palette
= (wxPalette
*) NULL
;
254 wxBitmapRefData::~wxBitmapRefData()
257 g_object_unref (m_pixmap
);
259 g_object_unref (m_pixbuf
);
263 #endif // wxUSE_PALETTE
266 //-----------------------------------------------------------------------------
268 #define M_BMPDATA wx_static_cast(wxBitmapRefData*, m_refData)
270 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
276 wxBitmap::wxBitmap( int width
, int height
, int depth
)
278 Create( width
, height
, depth
);
281 bool wxBitmap::Create( int width
, int height
, int depth
)
285 if ( width
<= 0 || height
<= 0 )
290 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
293 depth
= visual
->depth
;
295 wxCHECK_MSG( (depth
== visual
->depth
) || (depth
== 1) || (depth
== 32), false,
296 wxT("invalid bitmap depth") );
298 m_refData
= new wxBitmapRefData();
299 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
300 M_BMPDATA
->m_width
= width
;
301 M_BMPDATA
->m_height
= height
;
302 M_BMPDATA
->m_bpp
= depth
;
305 M_BMPDATA
->m_pixbuf
= gdk_pixbuf_new( GDK_COLORSPACE_RGB
, true,
310 M_BMPDATA
->m_pixmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, depth
);
316 bool wxBitmap::CreateFromXpm( const char **bits
)
320 wxCHECK_MSG( bits
!= NULL
, false, wxT("invalid bitmap data") );
322 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
324 m_refData
= new wxBitmapRefData();
326 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
328 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm_d( wxGetRootWindow()->window
, &mask
, NULL
, (gchar
**) bits
);
330 wxCHECK_MSG( M_BMPDATA
->m_pixmap
, false, wxT("couldn't create pixmap") );
334 M_BMPDATA
->m_mask
= new wxMask();
335 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
338 gdk_drawable_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
340 M_BMPDATA
->m_bpp
= visual
->depth
; // Can we get a different depth from create_from_xpm_d() ?
345 wxBitmap
wxBitmap::Rescale( int clipx
, int clipy
, int clipwidth
, int clipheight
, int newx
, int newy
)
347 wxCHECK_MSG( Ok(), wxNullBitmap
, wxT("invalid bitmap") );
349 if (newy
==M_BMPDATA
->m_width
&& newy
==M_BMPDATA
->m_height
)
352 int width
= wxMax(newx
, 1);
353 int height
= wxMax(newy
, 1);
354 width
= wxMin(width
, clipwidth
);
355 height
= wxMin(height
, clipheight
);
362 bmp
.SetHeight(height
);
363 bmp
.SetDepth(GetDepth());
364 bmp
.SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
365 gdk_pixbuf_get_has_alpha(GetPixbuf()),
367 gdk_pixbuf_scale(GetPixbuf(), bmp
.GetPixbuf(),
370 (double)newx
/GetWidth(), (double)newy
/GetHeight(),
371 GDK_INTERP_BILINEAR
);
375 GdkImage
*img
= (GdkImage
*) NULL
;
377 img
= gdk_image_get( GetPixmap(), 0, 0, GetWidth(), GetHeight() );
379 wxFAIL_MSG( wxT("Ill-formed bitmap") );
381 wxCHECK_MSG( img
, wxNullBitmap
, wxT("couldn't create image") );
387 GdkPixmap
*dstpix
= NULL
;
390 GdkVisual
*visual
= gdk_drawable_get_visual( GetPixmap() );
392 visual
= wxTheApp
->GetGdkVisual();
395 bmp
= wxBitmap(width
,height
,bpp
);
396 dstpix
= bmp
.GetPixmap();
397 gc
= gdk_gc_new( dstpix
);
401 long dstbyteperline
= 0;
406 dstbyteperline
= width
/8*M_BMPDATA
->m_bpp
;
407 if (width
*M_BMPDATA
->m_bpp
% 8 != 0)
409 dst
= (char*) malloc(dstbyteperline
*height
);
412 // be careful to use the right scaling factor
413 float scx
= (float)M_BMPDATA
->m_width
/(float)newx
;
414 float scy
= (float)M_BMPDATA
->m_height
/(float)newy
;
415 // prepare accel-tables
416 int *tablex
= (int *)calloc(width
,sizeof(int));
417 int *tabley
= (int *)calloc(height
,sizeof(int));
419 // accel table filled with clipped values
420 for (int x
= 0; x
< width
; x
++)
421 tablex
[x
] = (int) (scx
* (x
+clipx
));
422 for (int y
= 0; y
< height
; y
++)
423 tabley
[y
] = (int) (scy
* (y
+clipy
));
425 // Main rescaling routine starts here
426 for (int h
= 0; h
< height
; h
++)
430 guint32 old_pixval
= 0;
432 for (int w
= 0; w
< width
; w
++)
440 pixval
= gdk_image_get_pixel( img
, x
, tabley
[h
] );
450 char shift
= bit
<< (w
% 8);
456 dst
[h
*dstbyteperline
+w
/8] = outbyte
;
464 gdk_gc_set_foreground( gc
, &col
);
465 gdk_draw_point( dstpix
, gc
, w
, h
);
469 // do not forget the last byte
470 if ( dst
&& (width
% 8 != 0) )
471 dst
[h
*dstbyteperline
+width
/8] = outbyte
;
474 g_object_unref (img
);
475 if (gc
) g_object_unref (gc
);
479 bmp
= wxBitmap( (const char *)dst
, width
, height
, 1 );
485 dstbyteperline
= width
/8;
488 dst
= (char*) malloc(dstbyteperline
*height
);
489 img
= gdk_image_get( GetMask()->GetBitmap(), 0, 0, GetWidth(), GetHeight() );
491 for (int h
= 0; h
< height
; h
++)
495 guint32 old_pixval
= 0;
497 for (int w
= 0; w
< width
; w
++)
505 pixval
= gdk_image_get_pixel( img
, x
, tabley
[h
] );
513 char shift
= bit
<< (w
% 8);
519 dst
[h
*dstbyteperline
+w
/8] = outbyte
;
524 // do not forget the last byte
526 dst
[h
*dstbyteperline
+width
/8] = outbyte
;
528 wxMask
* mask
= new wxMask
;
529 mask
->m_bitmap
= gdk_bitmap_create_from_data( wxGetRootWindow()->window
, (gchar
*) dst
, width
, height
);
533 g_object_unref (img
);
543 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
547 wxCHECK_MSG( image
.Ok(), false, wxT("invalid image") );
548 wxCHECK_MSG( depth
== -1 || depth
== 1, false, wxT("invalid bitmap depth") );
550 if (image
.GetWidth() <= 0 || image
.GetHeight() <= 0)
553 m_refData
= new wxBitmapRefData();
557 return CreateFromImageAsBitmap(image
);
561 if (image
.HasAlpha())
562 return CreateFromImageAsPixbuf(image
);
564 return CreateFromImageAsPixmap(image
);
568 // conversion to mono bitmap:
569 bool wxBitmap::CreateFromImageAsBitmap(const wxImage
& img
)
571 // convert alpha channel to mask, if it is present:
573 image
.ConvertAlphaToMask();
575 int width
= image
.GetWidth();
576 int height
= image
.GetHeight();
581 SetPixmap( gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 ) );
585 // Create picture image
587 GdkGC
* data_gc
= gdk_gc_new(M_BMPDATA
->m_pixmap
);
590 gdk_gc_set_foreground(data_gc
, &color
);
591 gdk_draw_rectangle(M_BMPDATA
->m_pixmap
, data_gc
, true, 0, 0, width
, height
);
592 GdkImage
* data_image
= gdk_drawable_get_image(M_BMPDATA
->m_pixmap
, 0, 0, width
, height
);
596 GdkImage
*mask_image
= (GdkImage
*) NULL
;
597 GdkGC
* mask_gc
= NULL
;
601 wxMask
*mask
= new wxMask();
602 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
603 mask_gc
= gdk_gc_new(mask
->m_bitmap
);
604 gdk_gc_set_foreground(mask_gc
, &color
);
605 gdk_draw_rectangle(mask
->m_bitmap
, mask_gc
, true, 0, 0, width
, height
);
606 mask_image
= gdk_drawable_get_image(mask
->m_bitmap
, 0, 0, width
, height
);
611 int r_mask
= image
.GetMaskRed();
612 int g_mask
= image
.GetMaskGreen();
613 int b_mask
= image
.GetMaskBlue();
615 unsigned char* data
= image
.GetData();
618 for (int y
= 0; y
< height
; y
++)
620 for (int x
= 0; x
< width
; x
++)
629 if (mask_image
!= NULL
)
631 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
632 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
635 if ((r
== 255) && (b
== 255) && (g
== 255))
636 gdk_image_put_pixel( data_image
, x
, y
, 0 );
643 gdk_draw_image( GetPixmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
645 g_object_unref (data_image
);
646 g_object_unref (data_gc
);
650 if (mask_image
!= NULL
)
652 gdk_draw_image( GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
654 g_object_unref (mask_image
);
655 g_object_unref (mask_gc
);
661 // conversion to colour bitmap:
662 bool wxBitmap::CreateFromImageAsPixmap(const wxImage
& img
)
664 // convert alpha channel to mask, if it is present:
666 image
.ConvertAlphaToMask();
668 int width
= image
.GetWidth();
669 int height
= image
.GetHeight();
674 SetPixmap( gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, -1 ) );
676 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
678 int bpp
= visual
->depth
;
682 GdkGC
*gc
= gdk_gc_new( GetPixmap() );
684 gdk_draw_rgb_image( GetPixmap(),
696 if (!image
.HasMask())
699 wxMask
*mask
= new wxMask();
700 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
701 GdkGC
* mask_gc
= gdk_gc_new(mask
->m_bitmap
);
704 gdk_gc_set_foreground(mask_gc
, &color
);
705 gdk_draw_rectangle(mask
->m_bitmap
, mask_gc
, true, 0, 0, width
, height
);
706 GdkImage
* mask_image
= gdk_drawable_get_image(mask
->m_bitmap
, 0, 0, width
, height
);
710 int r_mask
= image
.GetMaskRed();
711 int g_mask
= image
.GetMaskGreen();
712 int b_mask
= image
.GetMaskBlue();
714 unsigned char* data
= image
.GetData();
717 for (int y
= 0; y
< height
; y
++)
719 for (int x
= 0; x
< width
; x
++)
728 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
729 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
735 gdk_draw_image( GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
737 g_object_unref (mask_image
);
738 g_object_unref (mask_gc
);
743 bool wxBitmap::CreateFromImageAsPixbuf(const wxImage
& image
)
745 int width
= image
.GetWidth();
746 int height
= image
.GetHeight();
748 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
750 8 /* bits per sample */,
755 wxASSERT( image
.HasAlpha() ); // for now
756 wxASSERT( gdk_pixbuf_get_n_channels(pixbuf
) == 4 );
757 wxASSERT( gdk_pixbuf_get_width(pixbuf
) == width
);
758 wxASSERT( gdk_pixbuf_get_height(pixbuf
) == height
);
760 M_BMPDATA
->m_pixbuf
= pixbuf
;
763 SetDepth(wxTheApp
->GetGdkVisual()->depth
);
766 unsigned char *in
= image
.GetData();
767 unsigned char *out
= gdk_pixbuf_get_pixels(pixbuf
);
768 unsigned char *alpha
= image
.GetAlpha();
770 int rowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
772 for (int y
= 0; y
< height
; y
++, out
+= rowinc
)
774 for (int x
= 0; x
< width
; x
++, alpha
++, out
+= 4, in
+= 3)
786 wxImage
wxBitmap::ConvertToImage() const
790 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
792 image
.Create(GetWidth(), GetHeight());
793 unsigned char *data
= image
.GetData();
797 wxFAIL_MSG( wxT("couldn't create image") );
803 GdkPixbuf
*pixbuf
= GetPixbuf();
804 wxASSERT( gdk_pixbuf_get_has_alpha(pixbuf
) );
811 unsigned char *alpha
= image
.GetAlpha();
812 unsigned char *in
= gdk_pixbuf_get_pixels(pixbuf
);
813 unsigned char *out
= data
;
814 int rowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * w
;
816 for (int y
= 0; y
< h
; y
++, in
+= rowinc
)
818 for (int x
= 0; x
< w
; x
++, in
+= 4, out
+= 3, alpha
++)
829 // the colour used as transparent one in wxImage and the one it is
830 // replaced with when it really occurs in the bitmap
831 static const int MASK_RED
= 1;
832 static const int MASK_GREEN
= 2;
833 static const int MASK_BLUE
= 3;
834 static const int MASK_BLUE_REPLACEMENT
= 2;
836 GdkImage
*gdk_image
= (GdkImage
*) NULL
;
840 gdk_image
= gdk_image_get( GetPixmap(),
842 GetWidth(), GetHeight() );
846 wxFAIL_MSG( wxT("Ill-formed bitmap") );
849 wxCHECK_MSG( gdk_image
, wxNullImage
, wxT("couldn't create image") );
851 GdkImage
*gdk_image_mask
= (GdkImage
*) NULL
;
854 gdk_image_mask
= gdk_image_get( GetMask()->GetBitmap(),
856 GetWidth(), GetHeight() );
858 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
862 int red_shift_right
= 0;
863 int green_shift_right
= 0;
864 int blue_shift_right
= 0;
865 int red_shift_left
= 0;
866 int green_shift_left
= 0;
867 int blue_shift_left
= 0;
868 bool use_shift
= false;
872 GdkVisual
*visual
= gdk_drawable_get_visual( GetPixmap() );
874 visual
= wxTheApp
->GetGdkVisual();
878 bpp
= visual
->red_prec
+ visual
->green_prec
+ visual
->blue_prec
;
879 red_shift_right
= visual
->red_shift
;
880 red_shift_left
= 8-visual
->red_prec
;
881 green_shift_right
= visual
->green_shift
;
882 green_shift_left
= 8-visual
->green_prec
;
883 blue_shift_right
= visual
->blue_shift
;
884 blue_shift_left
= 8-visual
->blue_prec
;
886 use_shift
= (visual
->type
== GDK_VISUAL_TRUE_COLOR
) || (visual
->type
== GDK_VISUAL_DIRECT_COLOR
);
894 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
897 for (int j
= 0; j
< GetHeight(); j
++)
899 for (int i
= 0; i
< GetWidth(); i
++)
901 wxUint32 pixel
= gdk_image_get_pixel( gdk_image
, i
, j
);
919 data
[pos
] = (pixel
>> red_shift_right
) << red_shift_left
;
920 data
[pos
+1] = (pixel
>> green_shift_right
) << green_shift_left
;
921 data
[pos
+2] = (pixel
>> blue_shift_right
) << blue_shift_left
;
923 else if (cmap
->colors
)
925 data
[pos
] = cmap
->colors
[pixel
].red
>> 8;
926 data
[pos
+1] = cmap
->colors
[pixel
].green
>> 8;
927 data
[pos
+2] = cmap
->colors
[pixel
].blue
>> 8;
931 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
936 int mask_pixel
= gdk_image_get_pixel( gdk_image_mask
, i
, j
);
939 data
[pos
] = MASK_RED
;
940 data
[pos
+1] = MASK_GREEN
;
941 data
[pos
+2] = MASK_BLUE
;
943 else if ( data
[pos
] == MASK_RED
&&
944 data
[pos
+1] == MASK_GREEN
&&
945 data
[pos
+2] == MASK_BLUE
)
947 data
[pos
+2] = MASK_BLUE_REPLACEMENT
;
955 g_object_unref (gdk_image
);
956 if (gdk_image_mask
) g_object_unref (gdk_image_mask
);
962 wxBitmap::wxBitmap( const wxString
&filename
, wxBitmapType type
)
964 LoadFile( filename
, type
);
967 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int WXUNUSED(depth
))
969 if ( width
> 0 && height
> 0 )
971 m_refData
= new wxBitmapRefData();
973 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
974 M_BMPDATA
->m_pixmap
= gdk_bitmap_create_from_data
976 wxGetRootWindow()->window
,
981 M_BMPDATA
->m_width
= width
;
982 M_BMPDATA
->m_height
= height
;
983 M_BMPDATA
->m_bpp
= 1;
985 wxASSERT_MSG( M_BMPDATA
->m_pixmap
, wxT("couldn't create bitmap") );
989 wxBitmap::~wxBitmap()
993 bool wxBitmap::operator == ( const wxBitmap
& bmp
) const
995 return m_refData
== bmp
.m_refData
;
998 bool wxBitmap::operator != ( const wxBitmap
& bmp
) const
1000 return m_refData
!= bmp
.m_refData
;
1003 bool wxBitmap::Ok() const
1005 return (m_refData
!= NULL
) &&
1007 M_BMPDATA
->m_pixbuf
||
1012 int wxBitmap::GetHeight() const
1014 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1016 return M_BMPDATA
->m_height
;
1019 int wxBitmap::GetWidth() const
1021 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1023 return M_BMPDATA
->m_width
;
1026 int wxBitmap::GetDepth() const
1028 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1030 return M_BMPDATA
->m_bpp
;
1033 wxMask
*wxBitmap::GetMask() const
1035 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
1037 return M_BMPDATA
->m_mask
;
1040 void wxBitmap::SetMask( wxMask
*mask
)
1042 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
1044 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
1046 M_BMPDATA
->m_mask
= mask
;
1049 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
1055 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
1057 wxCHECK_MSG( Ok() &&
1058 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1059 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) && (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
1060 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
1062 wxBitmap
ret( rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
1063 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
1067 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
1068 gdk_pixbuf_get_has_alpha(GetPixbuf()),
1069 8, rect
.width
, rect
.height
);
1070 ret
.SetPixbuf(pixbuf
);
1071 gdk_pixbuf_copy_area(GetPixbuf(),
1072 rect
.x
, rect
.y
, rect
.width
, rect
.height
,
1077 if (ret
.GetDepth() != 1)
1079 GdkGC
*gc
= gdk_gc_new( ret
.GetPixmap() );
1080 gdk_draw_drawable( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1081 g_object_unref (gc
);
1085 GdkGC
*gc
= gdk_gc_new( ret
.GetPixmap() );
1087 col
.pixel
= 0xFFFFFF;
1088 gdk_gc_set_foreground( gc
, &col
);
1090 gdk_gc_set_background( gc
, &col
);
1091 gdk_wx_draw_bitmap( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1092 g_object_unref (gc
);
1098 wxMask
*mask
= new wxMask
;
1099 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, rect
.width
, rect
.height
, 1 );
1101 GdkGC
*gc
= gdk_gc_new( mask
->m_bitmap
);
1103 col
.pixel
= 0xFFFFFF;
1104 gdk_gc_set_foreground( gc
, &col
);
1106 gdk_gc_set_background( gc
, &col
);
1107 gdk_wx_draw_bitmap( mask
->m_bitmap
, gc
, M_BMPDATA
->m_mask
->m_bitmap
, rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1108 g_object_unref (gc
);
1110 ret
.SetMask( mask
);
1116 bool wxBitmap::SaveFile( const wxString
&name
, wxBitmapType type
, const wxPalette
*WXUNUSED(palette
) ) const
1118 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1120 // Try to save the bitmap via wxImage handlers:
1122 wxImage image
= ConvertToImage();
1123 if (image
.Ok()) return image
.SaveFile( name
, type
);
1129 bool wxBitmap::LoadFile( const wxString
&name
, wxBitmapType type
)
1133 if (!wxFileExists(name
))
1136 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
1138 if (type
== wxBITMAP_TYPE_XPM
)
1140 m_refData
= new wxBitmapRefData();
1142 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
1144 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm
1146 wxGetRootWindow()->window
,
1154 M_BMPDATA
->m_mask
= new wxMask();
1155 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
1158 gdk_drawable_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
1160 M_BMPDATA
->m_bpp
= visual
->depth
;
1162 else // try if wxImage can load it
1165 if ( !image
.LoadFile( name
, type
) || !image
.Ok() )
1168 *this = wxBitmap(image
);
1175 wxPalette
*wxBitmap::GetPalette() const
1178 return (wxPalette
*) NULL
;
1180 return M_BMPDATA
->m_palette
;
1183 void wxBitmap::SetPalette(const wxPalette
& WXUNUSED(palette
))
1187 #endif // wxUSE_PALETTE
1189 void wxBitmap::SetHeight( int height
)
1192 m_refData
= new wxBitmapRefData();
1194 M_BMPDATA
->m_height
= height
;
1197 void wxBitmap::SetWidth( int width
)
1200 m_refData
= new wxBitmapRefData();
1202 M_BMPDATA
->m_width
= width
;
1205 void wxBitmap::SetDepth( int depth
)
1208 m_refData
= new wxBitmapRefData();
1210 M_BMPDATA
->m_bpp
= depth
;
1213 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
1216 m_refData
= new wxBitmapRefData();
1218 M_BMPDATA
->m_pixmap
= pixmap
;
1219 PurgeOtherRepresentations(Pixmap
);
1222 GdkPixmap
*wxBitmap::GetPixmap() const
1224 wxCHECK_MSG( Ok(), (GdkPixmap
*) NULL
, wxT("invalid bitmap") );
1226 // create the pixmap on the fly if we use Pixbuf representation:
1227 if (HasPixbuf() && !HasPixmap())
1229 delete M_BMPDATA
->m_mask
;
1230 M_BMPDATA
->m_mask
= new wxMask();
1231 gdk_pixbuf_render_pixmap_and_mask(M_BMPDATA
->m_pixbuf
,
1232 &M_BMPDATA
->m_pixmap
,
1233 &M_BMPDATA
->m_mask
->m_bitmap
,
1237 return M_BMPDATA
->m_pixmap
;
1240 bool wxBitmap::HasPixmap() const
1242 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1244 return M_BMPDATA
->m_pixmap
!= NULL
;
1247 GdkPixbuf
*wxBitmap::GetPixbuf() const
1249 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
1251 if (HasPixmap() && !HasPixbuf())
1253 int width
= GetWidth();
1254 int height
= GetHeight();
1256 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
1259 M_BMPDATA
->m_pixbuf
=
1260 gdk_pixbuf_get_from_drawable(pixbuf
, M_BMPDATA
->m_pixmap
, NULL
,
1261 0, 0, 0, 0, width
, height
);
1263 // apply the mask to created pixbuf:
1264 if (M_BMPDATA
->m_pixbuf
&& M_BMPDATA
->m_mask
)
1267 gdk_pixbuf_get_from_drawable(NULL
,
1268 M_BMPDATA
->m_mask
->GetBitmap(),
1270 0, 0, 0, 0, width
, height
);
1273 guchar
*bmp
= gdk_pixbuf_get_pixels(pixbuf
);
1274 guchar
*mask
= gdk_pixbuf_get_pixels(pmask
);
1275 int bmprowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
1276 int maskrowinc
= gdk_pixbuf_get_rowstride(pmask
) - 3 * width
;
1278 for (int y
= 0; y
< height
;
1279 y
++, bmp
+= bmprowinc
, mask
+= maskrowinc
)
1281 for (int x
= 0; x
< width
; x
++, bmp
+= 4, mask
+= 3)
1283 if (mask
[0] == 0 /*black pixel*/)
1288 g_object_unref (pmask
);
1293 return M_BMPDATA
->m_pixbuf
;
1296 bool wxBitmap::HasPixbuf() const
1298 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1300 return M_BMPDATA
->m_pixbuf
!= NULL
;
1303 void wxBitmap::SetPixbuf( GdkPixbuf
*pixbuf
)
1306 m_refData
= new wxBitmapRefData();
1308 M_BMPDATA
->m_pixbuf
= pixbuf
;
1309 PurgeOtherRepresentations(Pixbuf
);
1312 void wxBitmap::PurgeOtherRepresentations(wxBitmap::Representation keep
)
1314 if (keep
== Pixmap
&& HasPixbuf())
1316 g_object_unref (M_BMPDATA
->m_pixbuf
);
1317 M_BMPDATA
->m_pixbuf
= NULL
;
1319 if (keep
== Pixbuf
&& HasPixmap())
1321 g_object_unref (M_BMPDATA
->m_pixmap
);
1322 M_BMPDATA
->m_pixmap
= NULL
;
1326 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1331 GdkPixbuf
*pixbuf
= GetPixbuf();
1336 if (gdk_pixbuf_get_has_alpha( pixbuf
))
1337 wxPrintf( wxT("Has alpha\n") );
1339 wxPrintf( wxT("No alpha.\n") );
1342 data
.m_height
= gdk_pixbuf_get_height( pixbuf
);
1343 data
.m_width
= gdk_pixbuf_get_width( pixbuf
);
1344 data
.m_stride
= gdk_pixbuf_get_rowstride( pixbuf
);
1346 return gdk_pixbuf_get_pixels( pixbuf
);
1349 void wxBitmap::UngetRawData(wxPixelDataBase
& WXUNUSED(data
))
1354 bool wxBitmap::HasAlpha() const
1359 void wxBitmap::UseAlpha()
1364 //-----------------------------------------------------------------------------
1366 //-----------------------------------------------------------------------------
1368 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
,wxBitmapHandlerBase
)
1370 wxBitmapHandler::~wxBitmapHandler()
1374 bool wxBitmapHandler::Create(wxBitmap
* WXUNUSED(bitmap
),
1375 void * WXUNUSED(data
),
1376 long WXUNUSED(type
),
1377 int WXUNUSED(width
),
1378 int WXUNUSED(height
),
1379 int WXUNUSED(depth
))
1381 wxFAIL_MSG( _T("not implemented") );
1386 bool wxBitmapHandler::LoadFile(wxBitmap
* WXUNUSED(bitmap
),
1387 const wxString
& WXUNUSED(name
),
1388 long WXUNUSED(flags
),
1389 int WXUNUSED(desiredWidth
),
1390 int WXUNUSED(desiredHeight
))
1392 wxFAIL_MSG( _T("not implemented") );
1397 bool wxBitmapHandler::SaveFile(const wxBitmap
* WXUNUSED(bitmap
),
1398 const wxString
& WXUNUSED(name
),
1400 const wxPalette
* WXUNUSED(palette
))
1402 wxFAIL_MSG( _T("not implemented") );
1407 /* static */ void wxBitmap::InitStandardHandlers()
1409 // TODO: Insert handler based on GdkPixbufs handler later