]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/bitmap.cpp
7b944d6738b3a26ee22db5965168c0eba1f44ffe
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_image_get(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_image_get( 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 image
.Create(GetWidth(), GetHeight());
727 unsigned char *data
= image
.GetData();
729 wxCHECK_MSG(data
!= NULL
, wxNullImage
, wxT("couldn't create image") );
733 GdkPixbuf
*pixbuf
= GetPixbuf();
734 wxASSERT( gdk_pixbuf_get_has_alpha(pixbuf
) );
741 unsigned char *alpha
= image
.GetAlpha();
742 unsigned char *in
= gdk_pixbuf_get_pixels(pixbuf
);
743 unsigned char *out
= data
;
744 int rowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * w
;
746 for (int y
= 0; y
< h
; y
++, in
+= rowinc
)
748 for (int x
= 0; x
< w
; x
++, in
+= 4, out
+= 3, alpha
++)
759 // the colour used as transparent one in wxImage and the one it is
760 // replaced with when it really occurs in the bitmap
761 static const int MASK_RED
= 1;
762 static const int MASK_GREEN
= 2;
763 static const int MASK_BLUE
= 3;
764 static const int MASK_BLUE_REPLACEMENT
= 2;
766 GdkImage
*gdk_image
= (GdkImage
*) NULL
;
770 gdk_image
= gdk_image_get( GetPixmap(),
772 GetWidth(), GetHeight() );
776 wxFAIL_MSG( wxT("Ill-formed bitmap") );
779 wxCHECK_MSG( gdk_image
, wxNullImage
, wxT("couldn't create image") );
781 GdkImage
*gdk_image_mask
= (GdkImage
*) NULL
;
784 gdk_image_mask
= gdk_image_get( GetMask()->GetBitmap(),
786 GetWidth(), GetHeight() );
788 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
792 int red_shift_right
= 0;
793 int green_shift_right
= 0;
794 int blue_shift_right
= 0;
795 int red_shift_left
= 0;
796 int green_shift_left
= 0;
797 int blue_shift_left
= 0;
798 bool use_shift
= false;
802 GdkVisual
*visual
= gdk_drawable_get_visual( GetPixmap() );
804 visual
= wxTheApp
->GetGdkVisual();
808 bpp
= visual
->red_prec
+ visual
->green_prec
+ visual
->blue_prec
;
809 red_shift_right
= visual
->red_shift
;
810 red_shift_left
= 8-visual
->red_prec
;
811 green_shift_right
= visual
->green_shift
;
812 green_shift_left
= 8-visual
->green_prec
;
813 blue_shift_right
= visual
->blue_shift
;
814 blue_shift_left
= 8-visual
->blue_prec
;
816 use_shift
= (visual
->type
== GDK_VISUAL_TRUE_COLOR
) || (visual
->type
== GDK_VISUAL_DIRECT_COLOR
);
824 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
827 for (int j
= 0; j
< GetHeight(); j
++)
829 for (int i
= 0; i
< GetWidth(); i
++)
831 wxUint32 pixel
= gdk_image_get_pixel( gdk_image
, i
, j
);
849 data
[pos
] = (pixel
>> red_shift_right
) << red_shift_left
;
850 data
[pos
+1] = (pixel
>> green_shift_right
) << green_shift_left
;
851 data
[pos
+2] = (pixel
>> blue_shift_right
) << blue_shift_left
;
853 else if (cmap
->colors
)
855 data
[pos
] = cmap
->colors
[pixel
].red
>> 8;
856 data
[pos
+1] = cmap
->colors
[pixel
].green
>> 8;
857 data
[pos
+2] = cmap
->colors
[pixel
].blue
>> 8;
861 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
866 int mask_pixel
= gdk_image_get_pixel( gdk_image_mask
, i
, j
);
869 data
[pos
] = MASK_RED
;
870 data
[pos
+1] = MASK_GREEN
;
871 data
[pos
+2] = MASK_BLUE
;
873 else if ( data
[pos
] == MASK_RED
&&
874 data
[pos
+1] == MASK_GREEN
&&
875 data
[pos
+2] == MASK_BLUE
)
877 data
[pos
+2] = MASK_BLUE_REPLACEMENT
;
885 g_object_unref (gdk_image
);
886 if (gdk_image_mask
) g_object_unref (gdk_image_mask
);
892 wxBitmap::wxBitmap( const wxString
&filename
, wxBitmapType type
)
894 LoadFile( filename
, type
);
897 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int WXUNUSED(depth
))
899 if ( width
> 0 && height
> 0 )
901 SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window
, bits
, width
, height
));
903 wxASSERT_MSG( M_BMPDATA
->m_pixmap
, wxT("couldn't create bitmap") );
907 wxBitmap::~wxBitmap()
911 bool wxBitmap::operator == ( const wxBitmap
& bmp
) const
913 return m_refData
== bmp
.m_refData
;
916 bool wxBitmap::operator != ( const wxBitmap
& bmp
) const
918 return m_refData
!= bmp
.m_refData
;
921 bool wxBitmap::Ok() const
923 return (m_refData
!= NULL
) &&
925 M_BMPDATA
->m_pixbuf
||
930 int wxBitmap::GetHeight() const
932 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
934 return M_BMPDATA
->m_height
;
937 int wxBitmap::GetWidth() const
939 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
941 return M_BMPDATA
->m_width
;
944 int wxBitmap::GetDepth() const
946 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
948 return M_BMPDATA
->m_bpp
;
951 wxMask
*wxBitmap::GetMask() const
953 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
955 return M_BMPDATA
->m_mask
;
958 void wxBitmap::SetMask( wxMask
*mask
)
960 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
962 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
964 M_BMPDATA
->m_mask
= mask
;
967 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
973 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
978 (rect
.x
>= 0) && (rect
.y
>= 0) &&
979 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) && (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
980 ret
, wxT("invalid bitmap or bitmap region") );
984 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
985 gdk_pixbuf_get_has_alpha(GetPixbuf()),
986 8, rect
.width
, rect
.height
);
987 ret
.SetPixbuf(pixbuf
);
988 ret
.SetDepth(M_BMPDATA
->m_bpp
);
989 gdk_pixbuf_copy_area(GetPixbuf(),
990 rect
.x
, rect
.y
, rect
.width
, rect
.height
,
995 ret
= wxBitmap(rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
996 if (M_BMPDATA
->m_bpp
!= 1)
998 GdkGC
*gc
= gdk_gc_new( ret
.GetPixmap() );
999 gdk_draw_drawable( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1000 g_object_unref (gc
);
1004 GdkGC
*gc
= gdk_gc_new( ret
.GetPixmap() );
1006 col
.pixel
= 0xFFFFFF;
1007 gdk_gc_set_foreground( gc
, &col
);
1009 gdk_gc_set_background( gc
, &col
);
1010 gdk_wx_draw_bitmap( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1011 g_object_unref (gc
);
1017 wxMask
*mask
= new wxMask
;
1018 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, rect
.width
, rect
.height
, 1 );
1020 GdkGC
*gc
= gdk_gc_new( mask
->m_bitmap
);
1022 col
.pixel
= 0xFFFFFF;
1023 gdk_gc_set_foreground( gc
, &col
);
1025 gdk_gc_set_background( gc
, &col
);
1026 gdk_wx_draw_bitmap( mask
->m_bitmap
, gc
, M_BMPDATA
->m_mask
->m_bitmap
, rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1027 g_object_unref (gc
);
1029 ret
.SetMask( mask
);
1035 bool wxBitmap::SaveFile( const wxString
&name
, wxBitmapType type
, const wxPalette
*WXUNUSED(palette
) ) const
1037 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1039 // Try to save the bitmap via wxImage handlers:
1040 wxImage image
= ConvertToImage();
1041 return image
.Ok() && image
.SaveFile(name
, type
);
1044 bool wxBitmap::LoadFile( const wxString
&name
, wxBitmapType type
)
1048 if (type
== wxBITMAP_TYPE_XPM
)
1050 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
1051 SetPixmap(gdk_pixmap_create_from_xpm(wxGetRootWindow()->window
, &mask
, NULL
, name
.fn_str()));
1055 M_BMPDATA
->m_mask
= new wxMask
;
1056 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
1059 else // try if wxImage can load it
1062 if (image
.LoadFile(name
, type
) && image
.Ok())
1063 *this = wxBitmap(image
);
1070 wxPalette
*wxBitmap::GetPalette() const
1073 return (wxPalette
*) NULL
;
1075 return M_BMPDATA
->m_palette
;
1078 void wxBitmap::SetPalette(const wxPalette
& WXUNUSED(palette
))
1082 #endif // wxUSE_PALETTE
1084 void wxBitmap::SetHeight( int height
)
1087 m_refData
= new wxBitmapRefData
;
1089 M_BMPDATA
->m_height
= height
;
1092 void wxBitmap::SetWidth( int width
)
1095 m_refData
= new wxBitmapRefData
;
1097 M_BMPDATA
->m_width
= width
;
1100 void wxBitmap::SetDepth( int depth
)
1103 m_refData
= new wxBitmapRefData
;
1105 M_BMPDATA
->m_bpp
= depth
;
1108 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
1111 m_refData
= new wxBitmapRefData
;
1113 wxASSERT(M_BMPDATA
->m_pixmap
== NULL
);
1114 M_BMPDATA
->m_pixmap
= pixmap
;
1115 gdk_drawable_get_size(pixmap
, &M_BMPDATA
->m_width
, &M_BMPDATA
->m_height
);
1116 M_BMPDATA
->m_bpp
= gdk_drawable_get_depth(pixmap
);
1117 PurgeOtherRepresentations(Pixmap
);
1120 GdkPixmap
*wxBitmap::GetPixmap() const
1122 wxCHECK_MSG( Ok(), (GdkPixmap
*) NULL
, wxT("invalid bitmap") );
1124 // create the pixmap on the fly if we use Pixbuf representation:
1125 if (M_BMPDATA
->m_pixmap
== NULL
)
1127 delete M_BMPDATA
->m_mask
;
1128 M_BMPDATA
->m_mask
= new wxMask
;
1129 gdk_pixbuf_render_pixmap_and_mask(M_BMPDATA
->m_pixbuf
,
1130 &M_BMPDATA
->m_pixmap
,
1131 &M_BMPDATA
->m_mask
->m_bitmap
,
1135 return M_BMPDATA
->m_pixmap
;
1138 bool wxBitmap::HasPixmap() const
1140 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1142 return M_BMPDATA
->m_pixmap
!= NULL
;
1145 GdkPixbuf
*wxBitmap::GetPixbuf() const
1147 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
1149 if (M_BMPDATA
->m_pixbuf
== NULL
)
1151 int width
= GetWidth();
1152 int height
= GetHeight();
1154 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
1157 M_BMPDATA
->m_pixbuf
=
1158 gdk_pixbuf_get_from_drawable(pixbuf
, M_BMPDATA
->m_pixmap
, NULL
,
1159 0, 0, 0, 0, width
, height
);
1161 // apply the mask to created pixbuf:
1162 if (M_BMPDATA
->m_pixbuf
&& M_BMPDATA
->m_mask
)
1165 gdk_pixbuf_get_from_drawable(NULL
,
1166 M_BMPDATA
->m_mask
->GetBitmap(),
1168 0, 0, 0, 0, width
, height
);
1171 guchar
*bmp
= gdk_pixbuf_get_pixels(pixbuf
);
1172 guchar
*mask
= gdk_pixbuf_get_pixels(pmask
);
1173 int bmprowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
1174 int maskrowinc
= gdk_pixbuf_get_rowstride(pmask
) - 3 * width
;
1176 for (int y
= 0; y
< height
;
1177 y
++, bmp
+= bmprowinc
, mask
+= maskrowinc
)
1179 for (int x
= 0; x
< width
; x
++, bmp
+= 4, mask
+= 3)
1181 if (mask
[0] == 0 /*black pixel*/)
1186 g_object_unref (pmask
);
1191 return M_BMPDATA
->m_pixbuf
;
1194 bool wxBitmap::HasPixbuf() const
1196 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1198 return M_BMPDATA
->m_pixbuf
!= NULL
;
1201 void wxBitmap::SetPixbuf( GdkPixbuf
*pixbuf
)
1204 m_refData
= new wxBitmapRefData
;
1206 wxASSERT(M_BMPDATA
->m_pixbuf
== NULL
);
1207 M_BMPDATA
->m_pixbuf
= pixbuf
;
1208 M_BMPDATA
->m_width
= gdk_pixbuf_get_width(pixbuf
);
1209 M_BMPDATA
->m_height
= gdk_pixbuf_get_height(pixbuf
);
1210 PurgeOtherRepresentations(Pixbuf
);
1213 void wxBitmap::PurgeOtherRepresentations(wxBitmap::Representation keep
)
1215 if (keep
== Pixmap
&& HasPixbuf())
1217 g_object_unref (M_BMPDATA
->m_pixbuf
);
1218 M_BMPDATA
->m_pixbuf
= NULL
;
1220 if (keep
== Pixbuf
&& HasPixmap())
1222 g_object_unref (M_BMPDATA
->m_pixmap
);
1223 M_BMPDATA
->m_pixmap
= NULL
;
1227 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1232 GdkPixbuf
*pixbuf
= GetPixbuf();
1237 if (gdk_pixbuf_get_has_alpha( pixbuf
))
1238 wxPrintf( wxT("Has alpha\n") );
1240 wxPrintf( wxT("No alpha.\n") );
1243 data
.m_height
= gdk_pixbuf_get_height( pixbuf
);
1244 data
.m_width
= gdk_pixbuf_get_width( pixbuf
);
1245 data
.m_stride
= gdk_pixbuf_get_rowstride( pixbuf
);
1247 return gdk_pixbuf_get_pixels( pixbuf
);
1250 void wxBitmap::UngetRawData(wxPixelDataBase
& WXUNUSED(data
))
1255 bool wxBitmap::HasAlpha() const
1260 void wxBitmap::UseAlpha()
1265 //-----------------------------------------------------------------------------
1267 //-----------------------------------------------------------------------------
1269 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
,wxBitmapHandlerBase
)
1271 wxBitmapHandler::~wxBitmapHandler()
1275 bool wxBitmapHandler::Create(wxBitmap
* WXUNUSED(bitmap
),
1276 void * WXUNUSED(data
),
1277 long WXUNUSED(type
),
1278 int WXUNUSED(width
),
1279 int WXUNUSED(height
),
1280 int WXUNUSED(depth
))
1282 wxFAIL_MSG( _T("not implemented") );
1287 bool wxBitmapHandler::LoadFile(wxBitmap
* WXUNUSED(bitmap
),
1288 const wxString
& WXUNUSED(name
),
1289 long WXUNUSED(flags
),
1290 int WXUNUSED(desiredWidth
),
1291 int WXUNUSED(desiredHeight
))
1293 wxFAIL_MSG( _T("not implemented") );
1298 bool wxBitmapHandler::SaveFile(const wxBitmap
* WXUNUSED(bitmap
),
1299 const wxString
& WXUNUSED(name
),
1301 const wxPalette
* WXUNUSED(palette
))
1303 wxFAIL_MSG( _T("not implemented") );
1308 /* static */ void wxBitmap::InitStandardHandlers()
1310 // TODO: Insert handler based on GdkPixbufs handler later