]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/bitmap.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/bitmap.cpp
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
13 #include "wx/bitmap.h"
17 #include "wx/palette.h"
23 #include "wx/rawbmp.h"
27 //-----------------------------------------------------------------------------
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
);
112 guint32 mask_pixel
= 1;
113 if (colormap
!= NULL
)
116 c
.CalcPixel(colormap
);
117 mask_pixel
= c
.GetPixel();
119 for (int y
= 0; y
< h
; y
++)
121 for (int x
= 0; x
< w
; x
++, bit_index
++)
122 if (gdk_image_get_pixel(image
, x
, y
) == mask_pixel
)
123 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
124 bit_index
= (bit_index
+ 7) & ~7u;
126 g_object_unref(image
);
128 m_bitmap
= gdk_bitmap_create_from_data(wxGetRootWindow()->window
, (char*)out
, w
, h
);
134 bool wxMask::Create( const wxBitmap
& bitmap
, int paletteIndex
)
137 wxPalette
*pal
= bitmap
.GetPalette();
139 wxCHECK_MSG( pal
, false, wxT("Cannot create mask from bitmap without palette") );
141 pal
->GetRGB(paletteIndex
, &r
, &g
, &b
);
143 return Create(bitmap
, wxColour(r
, g
, b
));
145 #endif // wxUSE_PALETTE
147 bool wxMask::Create( const wxBitmap
& bitmap
)
151 g_object_unref (m_bitmap
);
152 m_bitmap
= (GdkBitmap
*) NULL
;
155 if (!bitmap
.Ok()) return false;
157 wxCHECK_MSG( bitmap
.GetDepth() == 1, false, wxT("Cannot create mask from colour bitmap") );
159 m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, bitmap
.GetWidth(), bitmap
.GetHeight(), 1 );
161 if (!m_bitmap
) return false;
163 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
164 gdk_gc_set_function(gc
, GDK_COPY_INVERT
);
165 gdk_draw_drawable(m_bitmap
, gc
, bitmap
.GetPixmap(), 0, 0, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight());
171 GdkBitmap
*wxMask::GetBitmap() const
176 //-----------------------------------------------------------------------------
178 //-----------------------------------------------------------------------------
180 class wxBitmapRefData
: public wxObjectRefData
192 wxPalette
*m_palette
;
195 wxBitmapRefData::wxBitmapRefData()
197 m_pixmap
= (GdkPixmap
*) NULL
;
198 m_pixbuf
= (GdkPixbuf
*) NULL
;
199 m_mask
= (wxMask
*) NULL
;
203 m_palette
= (wxPalette
*) NULL
;
206 wxBitmapRefData::~wxBitmapRefData()
209 g_object_unref (m_pixmap
);
211 g_object_unref (m_pixbuf
);
215 #endif // wxUSE_PALETTE
218 //-----------------------------------------------------------------------------
220 #define M_BMPDATA wx_static_cast(wxBitmapRefData*, m_refData)
222 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
228 wxBitmap::wxBitmap( int width
, int height
, int depth
)
230 Create( width
, height
, depth
);
233 bool wxBitmap::Create( int width
, int height
, int depth
)
237 if ( width
<= 0 || height
<= 0 )
244 SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB
, true, 8, width
, height
));
245 M_BMPDATA
->m_bpp
= 32;
251 const GdkVisual
* visual
= wxTheApp
->GetGdkVisual();
253 depth
= visual
->depth
;
255 wxCHECK_MSG(depth
== visual
->depth
, false, wxT("invalid bitmap depth"));
258 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window
, width
, height
, depth
));
264 bool wxBitmap::CreateFromXpm( const char **bits
)
268 wxCHECK_MSG( bits
!= NULL
, false, wxT("invalid bitmap data") );
270 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
271 SetPixmap(gdk_pixmap_create_from_xpm_d(wxGetRootWindow()->window
, &mask
, NULL
, (gchar
**)bits
));
273 wxCHECK_MSG( M_BMPDATA
->m_pixmap
, false, wxT("couldn't create pixmap") );
277 M_BMPDATA
->m_mask
= new wxMask
;
278 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
284 wxBitmap
wxBitmap::Rescale( int clipx
, int clipy
, int clipwidth
, int clipheight
, int newx
, int newy
)
288 wxCHECK_MSG(Ok(), bmp
, wxT("invalid bitmap"));
290 if (newy
==M_BMPDATA
->m_width
&& newy
==M_BMPDATA
->m_height
)
293 int width
= wxMax(newx
, 1);
294 int height
= wxMax(newy
, 1);
295 width
= wxMin(width
, clipwidth
);
296 height
= wxMin(height
, clipheight
);
300 bmp
.SetDepth(GetDepth());
301 bmp
.SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
302 gdk_pixbuf_get_has_alpha(GetPixbuf()),
304 gdk_pixbuf_scale(GetPixbuf(), bmp
.GetPixbuf(),
307 (double)newx
/GetWidth(), (double)newy
/GetHeight(),
308 GDK_INTERP_BILINEAR
);
312 GdkImage
* img
= gdk_drawable_get_image(GetPixmap(), 0, 0, GetWidth(), GetHeight());
314 wxCHECK_MSG(img
, bmp
, wxT("couldn't create image"));
317 GdkPixmap
*dstpix
= NULL
;
319 long dstbyteperline
= 0;
323 GdkVisual
*visual
= gdk_drawable_get_visual( GetPixmap() );
325 visual
= wxTheApp
->GetGdkVisual();
327 bmp
= wxBitmap(width
, height
, visual
->depth
);
328 dstpix
= bmp
.GetPixmap();
329 gc
= gdk_gc_new( dstpix
);
333 dstbyteperline
= (width
+ 7) / 8;
334 dst
= (char*) malloc(dstbyteperline
*height
);
337 // be careful to use the right scaling factor
338 float scx
= (float)M_BMPDATA
->m_width
/(float)newx
;
339 float scy
= (float)M_BMPDATA
->m_height
/(float)newy
;
340 // prepare accel-tables
341 int *tablex
= (int *)calloc(width
,sizeof(int));
342 int *tabley
= (int *)calloc(height
,sizeof(int));
344 // accel table filled with clipped values
345 for (int x
= 0; x
< width
; x
++)
346 tablex
[x
] = (int) (scx
* (x
+clipx
));
347 for (int y
= 0; y
< height
; y
++)
348 tabley
[y
] = (int) (scy
* (y
+clipy
));
350 // Main rescaling routine starts here
351 for (int h
= 0; h
< height
; h
++)
355 guint32 old_pixval
= 0;
357 for (int w
= 0; w
< width
; w
++)
365 pixval
= gdk_image_get_pixel( img
, x
, tabley
[h
] );
375 char shift
= bit
<< (w
% 8);
381 dst
[h
*dstbyteperline
+w
/8] = outbyte
;
389 gdk_gc_set_foreground( gc
, &col
);
390 gdk_draw_point( dstpix
, gc
, w
, h
);
394 // do not forget the last byte
395 if ( dst
&& (width
% 8 != 0) )
396 dst
[h
*dstbyteperline
+width
/8] = outbyte
;
399 g_object_unref (img
);
400 if (gc
) g_object_unref (gc
);
404 bmp
= wxBitmap( (const char *)dst
, width
, height
, 1 );
410 dstbyteperline
= (width
+ 7) / 8;
411 dst
= (char*) malloc(dstbyteperline
*height
);
412 img
= gdk_drawable_get_image(GetMask()->GetBitmap(), 0, 0, GetWidth(), GetHeight());
414 for (int h
= 0; h
< height
; h
++)
418 guint32 old_pixval
= 0;
420 for (int w
= 0; w
< width
; w
++)
428 pixval
= gdk_image_get_pixel( img
, x
, tabley
[h
] );
436 char shift
= bit
<< (w
% 8);
442 dst
[h
*dstbyteperline
+w
/8] = outbyte
;
447 // do not forget the last byte
449 dst
[h
*dstbyteperline
+width
/8] = outbyte
;
451 wxMask
* mask
= new wxMask
;
452 mask
->m_bitmap
= gdk_bitmap_create_from_data( wxGetRootWindow()->window
, (gchar
*) dst
, width
, height
);
456 g_object_unref (img
);
466 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
470 wxCHECK_MSG( image
.Ok(), false, wxT("invalid image") );
471 wxCHECK_MSG( depth
== -1 || depth
== 1, false, wxT("invalid bitmap depth") );
473 if (image
.GetWidth() <= 0 || image
.GetHeight() <= 0)
477 return CreateFromImageAsPixmap(image
, depth
);
479 if (image
.HasAlpha())
480 return CreateFromImageAsPixbuf(image
);
482 return CreateFromImageAsPixmap(image
, depth
);
485 bool wxBitmap::CreateFromImageAsPixmap(const wxImage
& image
, int depth
)
487 const int w
= image
.GetWidth();
488 const int h
= image
.GetHeight();
491 // create XBM format bitmap
493 // one bit per pixel, each row starts on a byte boundary
494 const size_t out_size
= size_t((w
+ 7) / 8) * unsigned(h
);
495 wxByte
* out
= new wxByte
[out_size
];
496 // set bits are white
497 memset(out
, 0xff, out_size
);
498 const wxByte
* in
= image
.GetData();
499 unsigned bit_index
= 0;
500 for (int y
= 0; y
< h
; y
++)
502 for (int x
= 0; x
< w
; x
++, in
+= 3, bit_index
++)
503 if (in
[0] == 255 && in
[1] == 255 && in
[2] == 255)
504 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
505 // move index to next byte boundary
506 bit_index
= (bit_index
+ 7) & ~7u;
508 SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window
, (char*)out
, w
, h
));
513 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window
, w
, h
, depth
));
514 GdkGC
* gc
= gdk_gc_new(M_BMPDATA
->m_pixmap
);
516 M_BMPDATA
->m_pixmap
, gc
,
518 GDK_RGB_DITHER_NONE
, image
.GetData(), w
* 3);
522 const wxByte
* alpha
= image
.GetAlpha();
523 if (alpha
!= NULL
|| image
.HasMask())
525 // create mask as XBM format bitmap
527 const size_t out_size
= size_t((w
+ 7) / 8) * unsigned(h
);
528 wxByte
* out
= new wxByte
[out_size
];
529 memset(out
, 0xff, out_size
);
530 unsigned bit_index
= 0;
533 for (int y
= 0; y
< h
; y
++)
535 for (int x
= 0; x
< w
; x
++, bit_index
++)
536 if (*alpha
++ < wxIMAGE_ALPHA_THRESHOLD
)
537 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
538 bit_index
= (bit_index
+ 7) & ~7u;
543 const wxByte r_mask
= image
.GetMaskRed();
544 const wxByte g_mask
= image
.GetMaskGreen();
545 const wxByte b_mask
= image
.GetMaskBlue();
546 const wxByte
* in
= image
.GetData();
547 for (int y
= 0; y
< h
; y
++)
549 for (int x
= 0; x
< w
; x
++, in
+= 3, bit_index
++)
550 if (in
[0] == r_mask
&& in
[1] == g_mask
&& in
[2] == b_mask
)
551 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
552 bit_index
= (bit_index
+ 7) & ~7u;
555 wxMask
* mask
= new wxMask
;
556 mask
->m_bitmap
= gdk_bitmap_create_from_data(M_BMPDATA
->m_pixmap
, (char*)out
, w
, h
);
563 bool wxBitmap::CreateFromImageAsPixbuf(const wxImage
& image
)
565 int width
= image
.GetWidth();
566 int height
= image
.GetHeight();
568 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
570 8 /* bits per sample */,
575 wxASSERT( image
.HasAlpha() ); // for now
576 wxASSERT( gdk_pixbuf_get_n_channels(pixbuf
) == 4 );
577 wxASSERT( gdk_pixbuf_get_width(pixbuf
) == width
);
578 wxASSERT( gdk_pixbuf_get_height(pixbuf
) == height
);
580 SetDepth(wxTheApp
->GetGdkVisual()->depth
);
584 unsigned char *in
= image
.GetData();
585 unsigned char *out
= gdk_pixbuf_get_pixels(pixbuf
);
586 unsigned char *alpha
= image
.GetAlpha();
588 int rowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
590 for (int y
= 0; y
< height
; y
++, out
+= rowinc
)
592 for (int x
= 0; x
< width
; x
++, alpha
++, out
+= 4, in
+= 3)
604 wxImage
wxBitmap::ConvertToImage() const
608 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
610 const int w
= GetWidth();
611 const int h
= GetHeight();
613 unsigned char *data
= image
.GetData();
615 wxCHECK_MSG(data
!= NULL
, wxNullImage
, wxT("couldn't create image") );
619 GdkPixbuf
*pixbuf
= GetPixbuf();
620 wxASSERT( gdk_pixbuf_get_has_alpha(pixbuf
) );
624 unsigned char *alpha
= image
.GetAlpha();
625 unsigned char *in
= gdk_pixbuf_get_pixels(pixbuf
);
626 unsigned char *out
= data
;
627 int rowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * w
;
629 for (int y
= 0; y
< h
; y
++, in
+= rowinc
)
631 for (int x
= 0; x
< w
; x
++, in
+= 4, out
+= 3, alpha
++)
642 GdkPixmap
* pixmap
= GetPixmap();
643 GdkPixmap
* pixmap_invert
= NULL
;
646 // mono bitmaps are inverted
647 pixmap_invert
= gdk_pixmap_new(pixmap
, w
, h
, 1);
648 GdkGC
* gc
= gdk_gc_new(pixmap_invert
);
649 gdk_gc_set_function(gc
, GDK_COPY_INVERT
);
650 gdk_draw_drawable(pixmap_invert
, gc
, pixmap
, 0, 0, 0, 0, w
, h
);
652 pixmap
= pixmap_invert
;
654 // create a pixbuf which shares data with the wxImage
655 GdkPixbuf
* pixbuf
= gdk_pixbuf_new_from_data(
656 data
, GDK_COLORSPACE_RGB
, false, 8, w
, h
, 3 * w
, NULL
, NULL
);
658 gdk_pixbuf_get_from_drawable(pixbuf
, pixmap
, NULL
, 0, 0, 0, 0, w
, h
);
660 g_object_unref(pixbuf
);
661 if (pixmap_invert
!= NULL
)
662 g_object_unref(pixmap_invert
);
666 // the colour used as transparent one in wxImage and the one it is
667 // replaced with when it really occurs in the bitmap
668 const int MASK_RED
= 1;
669 const int MASK_GREEN
= 2;
670 const int MASK_BLUE
= 3;
671 const int MASK_BLUE_REPLACEMENT
= 2;
673 image
.SetMaskColour(MASK_RED
, MASK_GREEN
, MASK_BLUE
);
674 GdkImage
* image_mask
= gdk_drawable_get_image(GetMask()->GetBitmap(), 0, 0, w
, h
);
676 for (int y
= 0; y
< h
; y
++)
678 for (int x
= 0; x
< w
; x
++, data
+= 3)
680 if (gdk_image_get_pixel(image_mask
, x
, y
) == 0)
683 data
[1] = MASK_GREEN
;
686 else if (data
[0] == MASK_RED
&& data
[1] == MASK_GREEN
&& data
[2] == MASK_BLUE
)
688 data
[2] = MASK_BLUE_REPLACEMENT
;
692 g_object_unref(image_mask
);
699 wxBitmap::wxBitmap( const wxString
&filename
, wxBitmapType type
)
701 LoadFile( filename
, type
);
704 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int WXUNUSED(depth
))
706 if ( width
> 0 && height
> 0 )
708 SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window
, bits
, width
, height
));
710 wxASSERT_MSG( M_BMPDATA
->m_pixmap
, wxT("couldn't create bitmap") );
714 wxBitmap::~wxBitmap()
718 bool wxBitmap::operator == ( const wxBitmap
& bmp
) const
720 return m_refData
== bmp
.m_refData
;
723 bool wxBitmap::operator != ( const wxBitmap
& bmp
) const
725 return m_refData
!= bmp
.m_refData
;
728 bool wxBitmap::Ok() const
730 return (m_refData
!= NULL
) &&
732 M_BMPDATA
->m_pixbuf
||
737 int wxBitmap::GetHeight() const
739 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
741 return M_BMPDATA
->m_height
;
744 int wxBitmap::GetWidth() const
746 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
748 return M_BMPDATA
->m_width
;
751 int wxBitmap::GetDepth() const
753 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
755 return M_BMPDATA
->m_bpp
;
758 wxMask
*wxBitmap::GetMask() const
760 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
762 return M_BMPDATA
->m_mask
;
765 void wxBitmap::SetMask( wxMask
*mask
)
767 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
769 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
771 M_BMPDATA
->m_mask
= mask
;
774 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
780 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
785 (rect
.x
>= 0) && (rect
.y
>= 0) &&
786 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) && (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
787 ret
, wxT("invalid bitmap or bitmap region") );
791 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
792 gdk_pixbuf_get_has_alpha(GetPixbuf()),
793 8, rect
.width
, rect
.height
);
794 ret
.SetPixbuf(pixbuf
);
795 ret
.SetDepth(M_BMPDATA
->m_bpp
);
796 gdk_pixbuf_copy_area(GetPixbuf(),
797 rect
.x
, rect
.y
, rect
.width
, rect
.height
,
802 ret
= wxBitmap(rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
803 GdkGC
*gc
= gdk_gc_new( ret
.GetPixmap() );
804 gdk_draw_drawable( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
810 wxMask
*mask
= new wxMask
;
811 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, rect
.width
, rect
.height
, 1 );
813 GdkGC
*gc
= gdk_gc_new( mask
->m_bitmap
);
814 gdk_draw_drawable(mask
->m_bitmap
, gc
, M_BMPDATA
->m_mask
->m_bitmap
, rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
823 bool wxBitmap::SaveFile( const wxString
&name
, wxBitmapType type
, const wxPalette
*WXUNUSED(palette
) ) const
825 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
827 // Try to save the bitmap via wxImage handlers:
828 wxImage image
= ConvertToImage();
829 return image
.Ok() && image
.SaveFile(name
, type
);
832 bool wxBitmap::LoadFile( const wxString
&name
, wxBitmapType type
)
836 if (type
== wxBITMAP_TYPE_XPM
)
838 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
839 SetPixmap(gdk_pixmap_create_from_xpm(wxGetRootWindow()->window
, &mask
, NULL
, name
.fn_str()));
843 M_BMPDATA
->m_mask
= new wxMask
;
844 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
847 else // try if wxImage can load it
850 if (image
.LoadFile(name
, type
) && image
.Ok())
851 *this = wxBitmap(image
);
858 wxPalette
*wxBitmap::GetPalette() const
861 return (wxPalette
*) NULL
;
863 return M_BMPDATA
->m_palette
;
866 void wxBitmap::SetPalette(const wxPalette
& WXUNUSED(palette
))
870 #endif // wxUSE_PALETTE
872 void wxBitmap::SetHeight( int height
)
875 m_refData
= new wxBitmapRefData
;
877 M_BMPDATA
->m_height
= height
;
880 void wxBitmap::SetWidth( int width
)
883 m_refData
= new wxBitmapRefData
;
885 M_BMPDATA
->m_width
= width
;
888 void wxBitmap::SetDepth( int depth
)
891 m_refData
= new wxBitmapRefData
;
893 M_BMPDATA
->m_bpp
= depth
;
896 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
899 m_refData
= new wxBitmapRefData
;
901 wxASSERT(M_BMPDATA
->m_pixmap
== NULL
);
902 M_BMPDATA
->m_pixmap
= pixmap
;
903 gdk_drawable_get_size(pixmap
, &M_BMPDATA
->m_width
, &M_BMPDATA
->m_height
);
904 M_BMPDATA
->m_bpp
= gdk_drawable_get_depth(pixmap
);
905 PurgeOtherRepresentations(Pixmap
);
908 GdkPixmap
*wxBitmap::GetPixmap() const
910 wxCHECK_MSG( Ok(), (GdkPixmap
*) NULL
, wxT("invalid bitmap") );
912 // create the pixmap on the fly if we use Pixbuf representation:
913 if (M_BMPDATA
->m_pixmap
== NULL
)
915 delete M_BMPDATA
->m_mask
;
916 M_BMPDATA
->m_mask
= new wxMask
;
917 gdk_pixbuf_render_pixmap_and_mask(M_BMPDATA
->m_pixbuf
,
918 &M_BMPDATA
->m_pixmap
,
919 &M_BMPDATA
->m_mask
->m_bitmap
,
923 return M_BMPDATA
->m_pixmap
;
926 bool wxBitmap::HasPixmap() const
928 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
930 return M_BMPDATA
->m_pixmap
!= NULL
;
933 GdkPixbuf
*wxBitmap::GetPixbuf() const
935 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
937 if (M_BMPDATA
->m_pixbuf
== NULL
)
939 int width
= GetWidth();
940 int height
= GetHeight();
942 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
945 M_BMPDATA
->m_pixbuf
=
946 gdk_pixbuf_get_from_drawable(pixbuf
, M_BMPDATA
->m_pixmap
, NULL
,
947 0, 0, 0, 0, width
, height
);
949 // apply the mask to created pixbuf:
950 if (M_BMPDATA
->m_pixbuf
&& M_BMPDATA
->m_mask
)
953 gdk_pixbuf_get_from_drawable(NULL
,
954 M_BMPDATA
->m_mask
->GetBitmap(),
956 0, 0, 0, 0, width
, height
);
959 guchar
*bmp
= gdk_pixbuf_get_pixels(pixbuf
);
960 guchar
*mask
= gdk_pixbuf_get_pixels(pmask
);
961 int bmprowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
962 int maskrowinc
= gdk_pixbuf_get_rowstride(pmask
) - 3 * width
;
964 for (int y
= 0; y
< height
;
965 y
++, bmp
+= bmprowinc
, mask
+= maskrowinc
)
967 for (int x
= 0; x
< width
; x
++, bmp
+= 4, mask
+= 3)
969 if (mask
[0] == 0 /*black pixel*/)
974 g_object_unref (pmask
);
979 return M_BMPDATA
->m_pixbuf
;
982 bool wxBitmap::HasPixbuf() const
984 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
986 return M_BMPDATA
->m_pixbuf
!= NULL
;
989 void wxBitmap::SetPixbuf( GdkPixbuf
*pixbuf
)
992 m_refData
= new wxBitmapRefData
;
994 wxASSERT(M_BMPDATA
->m_pixbuf
== NULL
);
995 M_BMPDATA
->m_pixbuf
= pixbuf
;
996 M_BMPDATA
->m_width
= gdk_pixbuf_get_width(pixbuf
);
997 M_BMPDATA
->m_height
= gdk_pixbuf_get_height(pixbuf
);
998 PurgeOtherRepresentations(Pixbuf
);
1001 void wxBitmap::PurgeOtherRepresentations(wxBitmap::Representation keep
)
1003 if (keep
== Pixmap
&& HasPixbuf())
1005 g_object_unref (M_BMPDATA
->m_pixbuf
);
1006 M_BMPDATA
->m_pixbuf
= NULL
;
1008 if (keep
== Pixbuf
&& HasPixmap())
1010 g_object_unref (M_BMPDATA
->m_pixmap
);
1011 M_BMPDATA
->m_pixmap
= NULL
;
1015 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1020 GdkPixbuf
*pixbuf
= GetPixbuf();
1025 if (gdk_pixbuf_get_has_alpha( pixbuf
))
1026 wxPrintf( wxT("Has alpha\n") );
1028 wxPrintf( wxT("No alpha.\n") );
1031 data
.m_height
= gdk_pixbuf_get_height( pixbuf
);
1032 data
.m_width
= gdk_pixbuf_get_width( pixbuf
);
1033 data
.m_stride
= gdk_pixbuf_get_rowstride( pixbuf
);
1035 return gdk_pixbuf_get_pixels( pixbuf
);
1038 void wxBitmap::UngetRawData(wxPixelDataBase
& WXUNUSED(data
))
1043 bool wxBitmap::HasAlpha() const
1048 void wxBitmap::UseAlpha()
1053 //-----------------------------------------------------------------------------
1055 //-----------------------------------------------------------------------------
1057 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
,wxBitmapHandlerBase
)
1059 wxBitmapHandler::~wxBitmapHandler()
1063 bool wxBitmapHandler::Create(wxBitmap
* WXUNUSED(bitmap
),
1064 void * WXUNUSED(data
),
1065 long WXUNUSED(type
),
1066 int WXUNUSED(width
),
1067 int WXUNUSED(height
),
1068 int WXUNUSED(depth
))
1070 wxFAIL_MSG( _T("not implemented") );
1075 bool wxBitmapHandler::LoadFile(wxBitmap
* WXUNUSED(bitmap
),
1076 const wxString
& WXUNUSED(name
),
1077 long WXUNUSED(flags
),
1078 int WXUNUSED(desiredWidth
),
1079 int WXUNUSED(desiredHeight
))
1081 wxFAIL_MSG( _T("not implemented") );
1086 bool wxBitmapHandler::SaveFile(const wxBitmap
* WXUNUSED(bitmap
),
1087 const wxString
& WXUNUSED(name
),
1089 const wxPalette
* WXUNUSED(palette
))
1091 wxFAIL_MSG( _T("not implemented") );
1096 /* static */ void wxBitmap::InitStandardHandlers()
1098 // TODO: Insert handler based on GdkPixbufs handler later