]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/bitmap.cpp
d6d039b893809f01c8e159bd2b578980cb6b026c
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/bitmap.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
13 #include "wx/bitmap.h"
17 #include "wx/palette.h"
23 #include "wx/rawbmp.h"
27 //-----------------------------------------------------------------------------
29 //-----------------------------------------------------------------------------
31 extern GtkWidget
*wxGetRootWindow();
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
37 IMPLEMENT_DYNAMIC_CLASS(wxMask
,wxObject
)
41 m_bitmap
= (GdkBitmap
*) NULL
;
44 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
46 m_bitmap
= (GdkBitmap
*) NULL
;
47 Create( bitmap
, colour
);
51 wxMask::wxMask( const wxBitmap
& bitmap
, int paletteIndex
)
53 m_bitmap
= (GdkBitmap
*) NULL
;
54 Create( bitmap
, paletteIndex
);
56 #endif // wxUSE_PALETTE
58 wxMask::wxMask( const wxBitmap
& bitmap
)
60 m_bitmap
= (GdkBitmap
*) NULL
;
67 g_object_unref (m_bitmap
);
70 bool wxMask::Create( const wxBitmap
& bitmap
,
71 const wxColour
& colour
)
75 g_object_unref (m_bitmap
);
76 m_bitmap
= (GdkBitmap
*) NULL
;
79 const int w
= bitmap
.GetWidth();
80 const int h
= bitmap
.GetHeight();
82 // create mask as XBM format bitmap
84 // one bit per pixel, each row starts on a byte boundary
85 const size_t out_size
= size_t((w
+ 7) / 8) * unsigned(h
);
86 wxByte
* out
= new wxByte
[out_size
];
88 memset(out
, 0xff, out_size
);
89 unsigned bit_index
= 0;
90 if (bitmap
.HasPixbuf())
92 const wxByte r_mask
= colour
.Red();
93 const wxByte g_mask
= colour
.Green();
94 const wxByte b_mask
= colour
.Blue();
95 GdkPixbuf
* pixbuf
= bitmap
.GetPixbuf();
96 const wxByte
* in
= gdk_pixbuf_get_pixels(pixbuf
);
97 const int inc
= 3 + int(gdk_pixbuf_get_has_alpha(pixbuf
) != 0);
98 const int rowpadding
= gdk_pixbuf_get_rowstride(pixbuf
) - inc
* w
;
99 for (int y
= 0; y
< h
; y
++, in
+= rowpadding
)
101 for (int x
= 0; x
< w
; x
++, in
+= inc
, bit_index
++)
102 if (in
[0] == r_mask
&& in
[1] == g_mask
&& in
[2] == b_mask
)
103 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
104 // move index to next byte boundary
105 bit_index
= (bit_index
+ 7) & ~7u;
110 GdkImage
* image
= gdk_drawable_get_image(bitmap
.GetPixmap(), 0, 0, w
, h
);
111 GdkColormap
* colormap
= gdk_image_get_colormap(image
);
113 if (colormap
== NULL
)
114 // mono bitmap, white is pixel value 0
115 mask_pixel
= guint32(colour
.Red() != 255 || colour
.Green() != 255 || colour
.Blue() != 255);
119 c
.CalcPixel(colormap
);
120 mask_pixel
= c
.GetPixel();
122 for (int y
= 0; y
< h
; y
++)
124 for (int x
= 0; x
< w
; x
++, bit_index
++)
125 if (gdk_image_get_pixel(image
, x
, y
) == mask_pixel
)
126 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
127 bit_index
= (bit_index
+ 7) & ~7u;
129 g_object_unref(image
);
131 m_bitmap
= gdk_bitmap_create_from_data(wxGetRootWindow()->window
, (char*)out
, w
, h
);
137 bool wxMask::Create( const wxBitmap
& bitmap
, int paletteIndex
)
140 wxPalette
*pal
= bitmap
.GetPalette();
142 wxCHECK_MSG( pal
, false, wxT("Cannot create mask from bitmap without palette") );
144 pal
->GetRGB(paletteIndex
, &r
, &g
, &b
);
146 return Create(bitmap
, wxColour(r
, g
, b
));
148 #endif // wxUSE_PALETTE
150 bool wxMask::Create( const wxBitmap
& bitmap
)
154 g_object_unref (m_bitmap
);
155 m_bitmap
= (GdkBitmap
*) NULL
;
158 if (!bitmap
.Ok()) return false;
160 wxCHECK_MSG( bitmap
.GetDepth() == 1, false, wxT("Cannot create mask from colour bitmap") );
162 m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, bitmap
.GetWidth(), bitmap
.GetHeight(), 1 );
164 if (!m_bitmap
) return false;
166 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
167 gdk_gc_set_function(gc
, GDK_COPY_INVERT
);
168 gdk_draw_drawable(m_bitmap
, gc
, bitmap
.GetPixmap(), 0, 0, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight());
174 GdkBitmap
*wxMask::GetBitmap() const
179 //-----------------------------------------------------------------------------
181 //-----------------------------------------------------------------------------
183 class wxBitmapRefData
: public wxObjectRefData
195 wxPalette
*m_palette
;
198 wxBitmapRefData::wxBitmapRefData()
200 m_pixmap
= (GdkPixmap
*) NULL
;
201 m_pixbuf
= (GdkPixbuf
*) NULL
;
202 m_mask
= (wxMask
*) NULL
;
206 m_palette
= (wxPalette
*) NULL
;
209 wxBitmapRefData::~wxBitmapRefData()
212 g_object_unref (m_pixmap
);
214 g_object_unref (m_pixbuf
);
218 #endif // wxUSE_PALETTE
221 //-----------------------------------------------------------------------------
223 #define M_BMPDATA wx_static_cast(wxBitmapRefData*, m_refData)
225 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
231 wxBitmap::wxBitmap( int width
, int height
, int depth
)
233 Create( width
, height
, depth
);
236 bool wxBitmap::Create( int width
, int height
, int depth
)
240 if ( width
<= 0 || height
<= 0 )
247 SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB
, true, 8, width
, height
));
248 M_BMPDATA
->m_bpp
= 32;
254 const GdkVisual
* visual
= wxTheApp
->GetGdkVisual();
256 depth
= visual
->depth
;
258 wxCHECK_MSG(depth
== visual
->depth
, false, wxT("invalid bitmap depth"));
261 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window
, width
, height
, depth
));
267 bool wxBitmap::CreateFromXpm( const char **bits
)
271 wxCHECK_MSG( bits
!= NULL
, false, wxT("invalid bitmap data") );
273 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
274 SetPixmap(gdk_pixmap_create_from_xpm_d(wxGetRootWindow()->window
, &mask
, NULL
, (gchar
**)bits
));
276 wxCHECK_MSG( M_BMPDATA
->m_pixmap
, false, wxT("couldn't create pixmap") );
280 M_BMPDATA
->m_mask
= new wxMask
;
281 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
287 wxBitmap
wxBitmap::Rescale( int clipx
, int clipy
, int clipwidth
, int clipheight
, int newx
, int newy
)
291 wxCHECK_MSG(Ok(), bmp
, wxT("invalid bitmap"));
293 if (newy
==M_BMPDATA
->m_width
&& newy
==M_BMPDATA
->m_height
)
296 int width
= wxMax(newx
, 1);
297 int height
= wxMax(newy
, 1);
298 width
= wxMin(width
, clipwidth
);
299 height
= wxMin(height
, clipheight
);
303 bmp
.SetDepth(GetDepth());
304 bmp
.SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
305 true, //gdk_pixbuf_get_has_alpha(GetPixbuf()),
307 gdk_pixbuf_scale(GetPixbuf(), bmp
.GetPixbuf(),
310 (double)newx
/GetWidth(), (double)newy
/GetHeight(),
311 GDK_INTERP_BILINEAR
);
315 GdkImage
* img
= gdk_drawable_get_image(GetPixmap(), 0, 0, GetWidth(), GetHeight());
317 wxCHECK_MSG(img
, bmp
, wxT("couldn't create image"));
320 GdkPixmap
*dstpix
= NULL
;
322 long dstbyteperline
= 0;
326 GdkVisual
*visual
= gdk_drawable_get_visual( GetPixmap() );
328 visual
= wxTheApp
->GetGdkVisual();
330 bmp
= wxBitmap(width
, height
, visual
->depth
);
331 dstpix
= bmp
.GetPixmap();
332 gc
= gdk_gc_new( dstpix
);
336 dstbyteperline
= (width
+ 7) / 8;
337 dst
= (char*) malloc(dstbyteperline
*height
);
340 // be careful to use the right scaling factor
341 float scx
= (float)M_BMPDATA
->m_width
/(float)newx
;
342 float scy
= (float)M_BMPDATA
->m_height
/(float)newy
;
343 // prepare accel-tables
344 int *tablex
= (int *)calloc(width
,sizeof(int));
345 int *tabley
= (int *)calloc(height
,sizeof(int));
347 // accel table filled with clipped values
348 for (int x
= 0; x
< width
; x
++)
349 tablex
[x
] = (int) (scx
* (x
+clipx
));
350 for (int y
= 0; y
< height
; y
++)
351 tabley
[y
] = (int) (scy
* (y
+clipy
));
353 // Main rescaling routine starts here
354 for (int h
= 0; h
< height
; h
++)
358 guint32 old_pixval
= 0;
360 for (int w
= 0; w
< width
; w
++)
368 pixval
= gdk_image_get_pixel( img
, x
, tabley
[h
] );
378 char shift
= bit
<< (w
% 8);
384 dst
[h
*dstbyteperline
+w
/8] = outbyte
;
392 gdk_gc_set_foreground( gc
, &col
);
393 gdk_draw_point( dstpix
, gc
, w
, h
);
397 // do not forget the last byte
398 if ( dst
&& (width
% 8 != 0) )
399 dst
[h
*dstbyteperline
+width
/8] = outbyte
;
402 g_object_unref (img
);
403 if (gc
) g_object_unref (gc
);
407 bmp
= wxBitmap( (const char *)dst
, width
, height
, 1 );
413 dstbyteperline
= (width
+ 7) / 8;
414 dst
= (char*) malloc(dstbyteperline
*height
);
415 img
= gdk_drawable_get_image(GetMask()->GetBitmap(), 0, 0, GetWidth(), GetHeight());
417 for (int h
= 0; h
< height
; h
++)
421 guint32 old_pixval
= 0;
423 for (int w
= 0; w
< width
; w
++)
431 pixval
= gdk_image_get_pixel( img
, x
, tabley
[h
] );
439 char shift
= bit
<< (w
% 8);
445 dst
[h
*dstbyteperline
+w
/8] = outbyte
;
450 // do not forget the last byte
452 dst
[h
*dstbyteperline
+width
/8] = outbyte
;
454 wxMask
* mask
= new wxMask
;
455 mask
->m_bitmap
= gdk_bitmap_create_from_data( wxGetRootWindow()->window
, (gchar
*) dst
, width
, height
);
459 g_object_unref (img
);
469 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
473 wxCHECK_MSG( image
.Ok(), false, wxT("invalid image") );
474 wxCHECK_MSG( depth
== -1 || depth
== 1, false, wxT("invalid bitmap depth") );
476 if (image
.GetWidth() <= 0 || image
.GetHeight() <= 0)
480 return CreateFromImageAsPixmap(image
, depth
);
482 if (image
.HasAlpha())
483 return CreateFromImageAsPixbuf(image
);
485 return CreateFromImageAsPixmap(image
, depth
);
488 bool wxBitmap::CreateFromImageAsPixmap(const wxImage
& image
, int depth
)
490 const int w
= image
.GetWidth();
491 const int h
= image
.GetHeight();
494 // create XBM format bitmap
496 // one bit per pixel, each row starts on a byte boundary
497 const size_t out_size
= size_t((w
+ 7) / 8) * unsigned(h
);
498 wxByte
* out
= new wxByte
[out_size
];
499 // set bits are white
500 memset(out
, 0xff, out_size
);
501 const wxByte
* in
= image
.GetData();
502 unsigned bit_index
= 0;
503 for (int y
= 0; y
< h
; y
++)
505 for (int x
= 0; x
< w
; x
++, in
+= 3, bit_index
++)
506 if (in
[0] == 255 && in
[1] == 255 && in
[2] == 255)
507 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
508 // move index to next byte boundary
509 bit_index
= (bit_index
+ 7) & ~7u;
511 SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window
, (char*)out
, w
, h
));
516 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window
, w
, h
, depth
));
517 GdkGC
* gc
= gdk_gc_new(M_BMPDATA
->m_pixmap
);
519 M_BMPDATA
->m_pixmap
, gc
,
521 GDK_RGB_DITHER_NONE
, image
.GetData(), w
* 3);
525 const wxByte
* alpha
= image
.GetAlpha();
526 if (alpha
!= NULL
|| image
.HasMask())
528 // create mask as XBM format bitmap
530 const size_t out_size
= size_t((w
+ 7) / 8) * unsigned(h
);
531 wxByte
* out
= new wxByte
[out_size
];
532 memset(out
, 0xff, out_size
);
533 unsigned bit_index
= 0;
536 for (int y
= 0; y
< h
; y
++)
538 for (int x
= 0; x
< w
; x
++, bit_index
++)
539 if (*alpha
++ < wxIMAGE_ALPHA_THRESHOLD
)
540 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
541 bit_index
= (bit_index
+ 7) & ~7u;
546 const wxByte r_mask
= image
.GetMaskRed();
547 const wxByte g_mask
= image
.GetMaskGreen();
548 const wxByte b_mask
= image
.GetMaskBlue();
549 const wxByte
* in
= image
.GetData();
550 for (int y
= 0; y
< h
; y
++)
552 for (int x
= 0; x
< w
; x
++, in
+= 3, bit_index
++)
553 if (in
[0] == r_mask
&& in
[1] == g_mask
&& in
[2] == b_mask
)
554 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
555 bit_index
= (bit_index
+ 7) & ~7u;
558 wxMask
* mask
= new wxMask
;
559 mask
->m_bitmap
= gdk_bitmap_create_from_data(M_BMPDATA
->m_pixmap
, (char*)out
, w
, h
);
566 bool wxBitmap::CreateFromImageAsPixbuf(const wxImage
& image
)
568 int width
= image
.GetWidth();
569 int height
= image
.GetHeight();
571 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
572 true, //image.HasAlpha(),
573 8 /* bits per sample */,
578 wxASSERT( image
.HasAlpha() ); // for now
579 wxASSERT( gdk_pixbuf_get_n_channels(pixbuf
) == 4 );
580 wxASSERT( gdk_pixbuf_get_width(pixbuf
) == width
);
581 wxASSERT( gdk_pixbuf_get_height(pixbuf
) == height
);
583 SetDepth(wxTheApp
->GetGdkVisual()->depth
);
587 unsigned char *in
= image
.GetData();
588 unsigned char *out
= gdk_pixbuf_get_pixels(pixbuf
);
589 unsigned char *alpha
= image
.GetAlpha();
591 int rowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
593 for (int y
= 0; y
< height
; y
++, out
+= rowinc
)
595 for (int x
= 0; x
< width
; x
++, alpha
++, out
+= 4, in
+= 3)
607 wxImage
wxBitmap::ConvertToImage() const
611 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
613 const int w
= GetWidth();
614 const int h
= GetHeight();
616 unsigned char *data
= image
.GetData();
618 wxCHECK_MSG(data
!= NULL
, wxNullImage
, wxT("couldn't create image") );
622 GdkPixbuf
*pixbuf
= GetPixbuf();
623 wxASSERT( gdk_pixbuf_get_has_alpha(pixbuf
) );
627 unsigned char *alpha
= image
.GetAlpha();
628 unsigned char *in
= gdk_pixbuf_get_pixels(pixbuf
);
629 unsigned char *out
= data
;
630 int rowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * w
;
632 for (int y
= 0; y
< h
; y
++, in
+= rowinc
)
634 for (int x
= 0; x
< w
; x
++, in
+= 4, out
+= 3, alpha
++)
645 GdkPixmap
* pixmap
= GetPixmap();
646 GdkPixmap
* pixmap_invert
= NULL
;
650 // mono bitmaps are inverted
651 pixmap_invert
= gdk_pixmap_new(pixmap
, w
, h
, 1);
652 GdkGC
* gc
= gdk_gc_new(pixmap_invert
);
653 gdk_gc_set_function(gc
, GDK_COPY_INVERT
);
654 gdk_draw_drawable(pixmap_invert
, gc
, pixmap
, 0, 0, 0, 0, w
, h
);
656 pixmap
= pixmap_invert
;
659 // create a pixbuf which shares data with the wxImage
660 GdkPixbuf
* pixbuf
= gdk_pixbuf_new_from_data(
661 data
, GDK_COLORSPACE_RGB
, false, 8, w
, h
, 3 * w
, NULL
, NULL
);
663 gdk_pixbuf_get_from_drawable(pixbuf
, pixmap
, NULL
, 0, 0, 0, 0, w
, h
);
665 g_object_unref(pixbuf
);
666 if (pixmap_invert
!= NULL
)
667 g_object_unref(pixmap_invert
);
671 // the colour used as transparent one in wxImage and the one it is
672 // replaced with when it really occurs in the bitmap
673 const int MASK_RED
= 1;
674 const int MASK_GREEN
= 2;
675 const int MASK_BLUE
= 3;
676 const int MASK_BLUE_REPLACEMENT
= 2;
678 image
.SetMaskColour(MASK_RED
, MASK_GREEN
, MASK_BLUE
);
679 GdkImage
* image_mask
= gdk_drawable_get_image(GetMask()->GetBitmap(), 0, 0, w
, h
);
681 for (int y
= 0; y
< h
; y
++)
683 for (int x
= 0; x
< w
; x
++, data
+= 3)
685 if (gdk_image_get_pixel(image_mask
, x
, y
) == 0)
688 data
[1] = MASK_GREEN
;
691 else if (data
[0] == MASK_RED
&& data
[1] == MASK_GREEN
&& data
[2] == MASK_BLUE
)
693 data
[2] = MASK_BLUE_REPLACEMENT
;
697 g_object_unref(image_mask
);
704 wxBitmap::wxBitmap( const wxString
&filename
, wxBitmapType type
)
706 LoadFile( filename
, type
);
709 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int WXUNUSED(depth
))
711 if ( width
> 0 && height
> 0 )
713 SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window
, bits
, width
, height
));
715 wxASSERT_MSG( M_BMPDATA
->m_pixmap
, wxT("couldn't create bitmap") );
719 wxBitmap::~wxBitmap()
723 bool wxBitmap::operator == ( const wxBitmap
& bmp
) const
725 return m_refData
== bmp
.m_refData
;
728 bool wxBitmap::operator != ( const wxBitmap
& bmp
) const
730 return m_refData
!= bmp
.m_refData
;
733 bool wxBitmap::Ok() const
735 return (m_refData
!= NULL
) &&
737 M_BMPDATA
->m_pixbuf
||
742 int wxBitmap::GetHeight() const
744 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
746 return M_BMPDATA
->m_height
;
749 int wxBitmap::GetWidth() const
751 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
753 return M_BMPDATA
->m_width
;
756 int wxBitmap::GetDepth() const
758 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
760 return M_BMPDATA
->m_bpp
;
763 wxMask
*wxBitmap::GetMask() const
765 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
767 return M_BMPDATA
->m_mask
;
770 void wxBitmap::SetMask( wxMask
*mask
)
772 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
774 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
776 M_BMPDATA
->m_mask
= mask
;
779 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
785 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
790 (rect
.x
>= 0) && (rect
.y
>= 0) &&
791 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) && (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
792 ret
, wxT("invalid bitmap or bitmap region") );
796 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
797 true, //gdk_pixbuf_get_has_alpha(GetPixbuf()),
798 8, rect
.width
, rect
.height
);
799 ret
.SetPixbuf(pixbuf
);
800 ret
.SetDepth(M_BMPDATA
->m_bpp
);
801 gdk_pixbuf_copy_area(GetPixbuf(),
802 rect
.x
, rect
.y
, rect
.width
, rect
.height
,
807 ret
= wxBitmap(rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
808 GdkGC
*gc
= gdk_gc_new( ret
.GetPixmap() );
809 gdk_draw_drawable( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
815 wxMask
*mask
= new wxMask
;
816 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, rect
.width
, rect
.height
, 1 );
818 GdkGC
*gc
= gdk_gc_new( mask
->m_bitmap
);
819 gdk_draw_drawable(mask
->m_bitmap
, gc
, M_BMPDATA
->m_mask
->m_bitmap
, rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
828 bool wxBitmap::SaveFile( const wxString
&name
, wxBitmapType type
, const wxPalette
*WXUNUSED(palette
) ) const
830 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
832 // Try to save the bitmap via wxImage handlers:
833 wxImage image
= ConvertToImage();
834 return image
.Ok() && image
.SaveFile(name
, type
);
837 bool wxBitmap::LoadFile( const wxString
&name
, wxBitmapType type
)
841 if (type
== wxBITMAP_TYPE_XPM
)
843 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
844 SetPixmap(gdk_pixmap_create_from_xpm(wxGetRootWindow()->window
, &mask
, NULL
, name
.fn_str()));
848 M_BMPDATA
->m_mask
= new wxMask
;
849 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
852 else // try if wxImage can load it
855 if (image
.LoadFile(name
, type
) && image
.Ok())
856 *this = wxBitmap(image
);
863 wxPalette
*wxBitmap::GetPalette() const
866 return (wxPalette
*) NULL
;
868 return M_BMPDATA
->m_palette
;
871 void wxBitmap::SetPalette(const wxPalette
& WXUNUSED(palette
))
875 #endif // wxUSE_PALETTE
877 void wxBitmap::SetHeight( int height
)
880 m_refData
= new wxBitmapRefData
;
882 M_BMPDATA
->m_height
= height
;
885 void wxBitmap::SetWidth( int width
)
888 m_refData
= new wxBitmapRefData
;
890 M_BMPDATA
->m_width
= width
;
893 void wxBitmap::SetDepth( int depth
)
896 m_refData
= new wxBitmapRefData
;
898 M_BMPDATA
->m_bpp
= depth
;
901 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
904 m_refData
= new wxBitmapRefData
;
906 wxASSERT(M_BMPDATA
->m_pixmap
== NULL
);
907 M_BMPDATA
->m_pixmap
= pixmap
;
908 gdk_drawable_get_size(pixmap
, &M_BMPDATA
->m_width
, &M_BMPDATA
->m_height
);
909 M_BMPDATA
->m_bpp
= gdk_drawable_get_depth(pixmap
);
910 PurgeOtherRepresentations(Pixmap
);
913 GdkPixmap
*wxBitmap::GetPixmap() const
915 wxCHECK_MSG( Ok(), (GdkPixmap
*) NULL
, wxT("invalid bitmap") );
917 // create the pixmap on the fly if we use Pixbuf representation:
918 if (M_BMPDATA
->m_pixmap
== NULL
)
920 delete M_BMPDATA
->m_mask
;
921 M_BMPDATA
->m_mask
= new wxMask
;
922 gdk_pixbuf_render_pixmap_and_mask(M_BMPDATA
->m_pixbuf
,
923 &M_BMPDATA
->m_pixmap
,
924 &M_BMPDATA
->m_mask
->m_bitmap
,
928 return M_BMPDATA
->m_pixmap
;
931 bool wxBitmap::HasPixmap() const
933 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
935 return M_BMPDATA
->m_pixmap
!= NULL
;
938 GdkPixbuf
*wxBitmap::GetPixbuf() const
940 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
942 if (M_BMPDATA
->m_pixbuf
== NULL
)
944 int width
= GetWidth();
945 int height
= GetHeight();
947 // always create the alpha channel so raw bitmap access will work
949 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
950 true, // GetMask() != NULL,
952 M_BMPDATA
->m_pixbuf
=
953 gdk_pixbuf_get_from_drawable(pixbuf
, M_BMPDATA
->m_pixmap
, NULL
,
954 0, 0, 0, 0, width
, height
);
956 // apply the mask to created pixbuf:
957 if (M_BMPDATA
->m_pixbuf
&& M_BMPDATA
->m_mask
)
960 gdk_pixbuf_get_from_drawable(NULL
,
961 M_BMPDATA
->m_mask
->GetBitmap(),
963 0, 0, 0, 0, width
, height
);
966 guchar
*bmp
= gdk_pixbuf_get_pixels(pixbuf
);
967 guchar
*mask
= gdk_pixbuf_get_pixels(pmask
);
968 int bmprowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
969 int maskrowinc
= gdk_pixbuf_get_rowstride(pmask
) - 3 * width
;
971 for (int y
= 0; y
< height
;
972 y
++, bmp
+= bmprowinc
, mask
+= maskrowinc
)
974 for (int x
= 0; x
< width
; x
++, bmp
+= 4, mask
+= 3)
976 if (mask
[0] == 0 /*black pixel*/)
981 g_object_unref (pmask
);
986 return M_BMPDATA
->m_pixbuf
;
989 bool wxBitmap::HasPixbuf() const
991 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
993 return M_BMPDATA
->m_pixbuf
!= NULL
;
996 void wxBitmap::SetPixbuf( GdkPixbuf
*pixbuf
)
999 m_refData
= new wxBitmapRefData
;
1001 wxASSERT(M_BMPDATA
->m_pixbuf
== NULL
);
1002 M_BMPDATA
->m_pixbuf
= pixbuf
;
1003 M_BMPDATA
->m_width
= gdk_pixbuf_get_width(pixbuf
);
1004 M_BMPDATA
->m_height
= gdk_pixbuf_get_height(pixbuf
);
1005 PurgeOtherRepresentations(Pixbuf
);
1008 void wxBitmap::PurgeOtherRepresentations(wxBitmap::Representation keep
)
1010 if (keep
== Pixmap
&& HasPixbuf())
1012 g_object_unref (M_BMPDATA
->m_pixbuf
);
1013 M_BMPDATA
->m_pixbuf
= NULL
;
1015 if (keep
== Pixbuf
&& HasPixmap())
1017 g_object_unref (M_BMPDATA
->m_pixmap
);
1018 M_BMPDATA
->m_pixmap
= NULL
;
1022 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1027 GdkPixbuf
*pixbuf
= GetPixbuf();
1031 if (!gdk_pixbuf_get_has_alpha( pixbuf
))
1035 if (gdk_pixbuf_get_has_alpha( pixbuf
))
1036 wxPrintf( wxT("Has alpha, %d channels\n"), gdk_pixbuf_get_n_channels(pixbuf
) );
1038 wxPrintf( wxT("No alpha, %d channels.\n"), gdk_pixbuf_get_n_channels(pixbuf
) );
1041 data
.m_height
= gdk_pixbuf_get_height( pixbuf
);
1042 data
.m_width
= gdk_pixbuf_get_width( pixbuf
);
1043 data
.m_stride
= gdk_pixbuf_get_rowstride( pixbuf
);
1045 return gdk_pixbuf_get_pixels( pixbuf
);
1048 void wxBitmap::UngetRawData(wxPixelDataBase
& WXUNUSED(data
))
1053 bool wxBitmap::HasAlpha() const
1058 void wxBitmap::UseAlpha()
1063 //-----------------------------------------------------------------------------
1065 //-----------------------------------------------------------------------------
1067 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
,wxBitmapHandlerBase
)
1069 wxBitmapHandler::~wxBitmapHandler()
1073 bool wxBitmapHandler::Create(wxBitmap
* WXUNUSED(bitmap
),
1074 void * WXUNUSED(data
),
1075 long WXUNUSED(type
),
1076 int WXUNUSED(width
),
1077 int WXUNUSED(height
),
1078 int WXUNUSED(depth
))
1080 wxFAIL_MSG( _T("not implemented") );
1085 bool wxBitmapHandler::LoadFile(wxBitmap
* WXUNUSED(bitmap
),
1086 const wxString
& WXUNUSED(name
),
1087 long WXUNUSED(flags
),
1088 int WXUNUSED(desiredWidth
),
1089 int WXUNUSED(desiredHeight
))
1091 wxFAIL_MSG( _T("not implemented") );
1096 bool wxBitmapHandler::SaveFile(const wxBitmap
* WXUNUSED(bitmap
),
1097 const wxString
& WXUNUSED(name
),
1099 const wxPalette
* WXUNUSED(palette
))
1101 wxFAIL_MSG( _T("not implemented") );
1106 /* static */ void wxBitmap::InitStandardHandlers()
1108 // TODO: Insert handler based on GdkPixbufs handler later