]>
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"
21 #include "wx/colour.h"
24 #include "wx/rawbmp.h"
28 //-----------------------------------------------------------------------------
30 //-----------------------------------------------------------------------------
32 extern GtkWidget
*wxGetRootWindow();
34 //-----------------------------------------------------------------------------
36 //-----------------------------------------------------------------------------
38 IMPLEMENT_DYNAMIC_CLASS(wxMask
,wxObject
)
42 m_bitmap
= (GdkBitmap
*) NULL
;
45 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
47 m_bitmap
= (GdkBitmap
*) NULL
;
48 Create( bitmap
, colour
);
52 wxMask::wxMask( const wxBitmap
& bitmap
, int paletteIndex
)
54 m_bitmap
= (GdkBitmap
*) NULL
;
55 Create( bitmap
, paletteIndex
);
57 #endif // wxUSE_PALETTE
59 wxMask::wxMask( const wxBitmap
& bitmap
)
61 m_bitmap
= (GdkBitmap
*) NULL
;
68 g_object_unref (m_bitmap
);
71 bool wxMask::Create( const wxBitmap
& bitmap
,
72 const wxColour
& colour
)
76 g_object_unref (m_bitmap
);
77 m_bitmap
= (GdkBitmap
*) NULL
;
80 const int w
= bitmap
.GetWidth();
81 const int h
= bitmap
.GetHeight();
83 // create mask as XBM format bitmap
85 // one bit per pixel, each row starts on a byte boundary
86 const size_t out_size
= size_t((w
+ 7) / 8) * unsigned(h
);
87 wxByte
* out
= new wxByte
[out_size
];
88 // set bits are unmasked
89 memset(out
, 0xff, out_size
);
90 unsigned bit_index
= 0;
91 if (bitmap
.HasPixbuf())
93 const wxByte r_mask
= colour
.Red();
94 const wxByte g_mask
= colour
.Green();
95 const wxByte b_mask
= colour
.Blue();
96 GdkPixbuf
* pixbuf
= bitmap
.GetPixbuf();
97 const wxByte
* in
= gdk_pixbuf_get_pixels(pixbuf
);
98 const int inc
= 3 + int(gdk_pixbuf_get_has_alpha(pixbuf
) != 0);
99 const int rowpadding
= gdk_pixbuf_get_rowstride(pixbuf
) - inc
* w
;
100 for (int y
= 0; y
< h
; y
++, in
+= rowpadding
)
102 for (int x
= 0; x
< w
; x
++, in
+= inc
, bit_index
++)
103 if (in
[0] == r_mask
&& in
[1] == g_mask
&& in
[2] == b_mask
)
104 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
105 // move index to next byte boundary
106 bit_index
= (bit_index
+ 7) & ~7u;
111 GdkImage
* image
= gdk_drawable_get_image(bitmap
.GetPixmap(), 0, 0, w
, h
);
112 GdkColormap
* colormap
= gdk_image_get_colormap(image
);
114 if (colormap
== NULL
)
115 // mono bitmap, white is pixel value 0
116 mask_pixel
= guint32(colour
.Red() != 255 || colour
.Green() != 255 || colour
.Blue() != 255);
120 c
.CalcPixel(colormap
);
121 mask_pixel
= c
.GetPixel();
123 for (int y
= 0; y
< h
; y
++)
125 for (int x
= 0; x
< w
; x
++, bit_index
++)
126 if (gdk_image_get_pixel(image
, x
, y
) == mask_pixel
)
127 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
128 bit_index
= (bit_index
+ 7) & ~7u;
130 g_object_unref(image
);
132 m_bitmap
= gdk_bitmap_create_from_data(wxGetRootWindow()->window
, (char*)out
, w
, h
);
138 bool wxMask::Create( const wxBitmap
& bitmap
, int paletteIndex
)
141 wxPalette
*pal
= bitmap
.GetPalette();
143 wxCHECK_MSG( pal
, false, wxT("Cannot create mask from bitmap without palette") );
145 pal
->GetRGB(paletteIndex
, &r
, &g
, &b
);
147 return Create(bitmap
, wxColour(r
, g
, b
));
149 #endif // wxUSE_PALETTE
151 bool wxMask::Create( const wxBitmap
& bitmap
)
155 g_object_unref (m_bitmap
);
156 m_bitmap
= (GdkBitmap
*) NULL
;
159 if (!bitmap
.Ok()) return false;
161 wxCHECK_MSG( bitmap
.GetDepth() == 1, false, wxT("Cannot create mask from colour bitmap") );
163 m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, bitmap
.GetWidth(), bitmap
.GetHeight(), 1 );
165 if (!m_bitmap
) return false;
167 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
168 gdk_gc_set_function(gc
, GDK_COPY_INVERT
);
169 gdk_draw_drawable(m_bitmap
, gc
, bitmap
.GetPixmap(), 0, 0, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight());
175 GdkBitmap
*wxMask::GetBitmap() const
180 //-----------------------------------------------------------------------------
182 //-----------------------------------------------------------------------------
184 class wxBitmapRefData
: public wxObjectRefData
188 virtual ~wxBitmapRefData();
196 wxPalette
*m_palette
;
199 wxBitmapRefData::wxBitmapRefData()
201 m_pixmap
= (GdkPixmap
*) NULL
;
202 m_pixbuf
= (GdkPixbuf
*) NULL
;
203 m_mask
= (wxMask
*) NULL
;
207 m_palette
= (wxPalette
*) NULL
;
210 wxBitmapRefData::~wxBitmapRefData()
213 g_object_unref (m_pixmap
);
215 g_object_unref (m_pixbuf
);
219 #endif // wxUSE_PALETTE
222 //-----------------------------------------------------------------------------
224 #define M_BMPDATA wx_static_cast(wxBitmapRefData*, m_refData)
226 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
232 wxBitmap::wxBitmap( int width
, int height
, int depth
)
234 Create( width
, height
, depth
);
237 bool wxBitmap::Create( int width
, int height
, int depth
)
241 if ( width
<= 0 || height
<= 0 )
248 SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB
, true, 8, width
, height
));
249 M_BMPDATA
->m_bpp
= 32;
255 const GdkVisual
* visual
= wxTheApp
->GetGdkVisual();
257 depth
= visual
->depth
;
259 wxCHECK_MSG(depth
== visual
->depth
, false, wxT("invalid bitmap depth"));
262 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window
, width
, height
, depth
));
268 bool wxBitmap::CreateFromXpm( const char **bits
)
272 wxCHECK_MSG( bits
!= NULL
, false, wxT("invalid bitmap data") );
274 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
275 SetPixmap(gdk_pixmap_create_from_xpm_d(wxGetRootWindow()->window
, &mask
, NULL
, (gchar
**)bits
));
277 wxCHECK_MSG( M_BMPDATA
->m_pixmap
, false, wxT("couldn't create pixmap") );
281 M_BMPDATA
->m_mask
= new wxMask
;
282 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
288 wxBitmap
wxBitmap::Rescale( int clipx
, int clipy
, int clipwidth
, int clipheight
, int newx
, int newy
)
292 wxCHECK_MSG(Ok(), bmp
, wxT("invalid bitmap"));
294 if (newy
==M_BMPDATA
->m_width
&& newy
==M_BMPDATA
->m_height
)
297 int width
= wxMax(newx
, 1);
298 int height
= wxMax(newy
, 1);
299 width
= wxMin(width
, clipwidth
);
300 height
= wxMin(height
, clipheight
);
304 bmp
.SetDepth(GetDepth());
305 bmp
.SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
306 true, //gdk_pixbuf_get_has_alpha(GetPixbuf()),
308 gdk_pixbuf_scale(GetPixbuf(), bmp
.GetPixbuf(),
311 (double)newx
/GetWidth(), (double)newy
/GetHeight(),
312 GDK_INTERP_BILINEAR
);
316 GdkImage
* img
= gdk_drawable_get_image(GetPixmap(), 0, 0, GetWidth(), GetHeight());
318 wxCHECK_MSG(img
, bmp
, wxT("couldn't create image"));
321 GdkPixmap
*dstpix
= NULL
;
323 long dstbyteperline
= 0;
327 GdkVisual
*visual
= gdk_drawable_get_visual( GetPixmap() );
329 visual
= wxTheApp
->GetGdkVisual();
331 bmp
= wxBitmap(width
, height
, visual
->depth
);
332 dstpix
= bmp
.GetPixmap();
333 gc
= gdk_gc_new( dstpix
);
337 dstbyteperline
= (width
+ 7) / 8;
338 dst
= (char*) malloc(dstbyteperline
*height
);
341 // be careful to use the right scaling factor
342 float scx
= (float)M_BMPDATA
->m_width
/(float)newx
;
343 float scy
= (float)M_BMPDATA
->m_height
/(float)newy
;
344 // prepare accel-tables
345 int *tablex
= (int *)calloc(width
,sizeof(int));
346 int *tabley
= (int *)calloc(height
,sizeof(int));
348 // accel table filled with clipped values
349 for (int x
= 0; x
< width
; x
++)
350 tablex
[x
] = (int) (scx
* (x
+clipx
));
351 for (int y
= 0; y
< height
; y
++)
352 tabley
[y
] = (int) (scy
* (y
+clipy
));
354 // Main rescaling routine starts here
355 for (int h
= 0; h
< height
; h
++)
359 guint32 old_pixval
= 0;
361 for (int w
= 0; w
< width
; w
++)
369 pixval
= gdk_image_get_pixel( img
, x
, tabley
[h
] );
379 char shift
= bit
<< (w
% 8);
385 dst
[h
*dstbyteperline
+w
/8] = outbyte
;
393 gdk_gc_set_foreground( gc
, &col
);
394 gdk_draw_point( dstpix
, gc
, w
, h
);
398 // do not forget the last byte
399 if ( dst
&& (width
% 8 != 0) )
400 dst
[h
*dstbyteperline
+width
/8] = outbyte
;
403 g_object_unref (img
);
404 if (gc
) g_object_unref (gc
);
408 bmp
= wxBitmap( (const char *)dst
, width
, height
, 1 );
414 dstbyteperline
= (width
+ 7) / 8;
415 dst
= (char*) malloc(dstbyteperline
*height
);
416 img
= gdk_drawable_get_image(GetMask()->GetBitmap(), 0, 0, GetWidth(), GetHeight());
418 for (int h
= 0; h
< height
; h
++)
422 guint32 old_pixval
= 0;
424 for (int w
= 0; w
< width
; w
++)
432 pixval
= gdk_image_get_pixel( img
, x
, tabley
[h
] );
440 char shift
= bit
<< (w
% 8);
446 dst
[h
*dstbyteperline
+w
/8] = outbyte
;
451 // do not forget the last byte
453 dst
[h
*dstbyteperline
+width
/8] = outbyte
;
455 wxMask
* mask
= new wxMask
;
456 mask
->m_bitmap
= gdk_bitmap_create_from_data( wxGetRootWindow()->window
, (gchar
*) dst
, width
, height
);
460 g_object_unref (img
);
470 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
474 wxCHECK_MSG( image
.Ok(), false, wxT("invalid image") );
475 wxCHECK_MSG( depth
== -1 || depth
== 1, false, wxT("invalid bitmap depth") );
477 if (image
.GetWidth() <= 0 || image
.GetHeight() <= 0)
481 return CreateFromImageAsPixmap(image
, depth
);
483 if (image
.HasAlpha())
484 return CreateFromImageAsPixbuf(image
);
486 return CreateFromImageAsPixmap(image
, depth
);
489 bool wxBitmap::CreateFromImageAsPixmap(const wxImage
& image
, int depth
)
491 const int w
= image
.GetWidth();
492 const int h
= image
.GetHeight();
495 // create XBM format bitmap
497 // one bit per pixel, each row starts on a byte boundary
498 const size_t out_size
= size_t((w
+ 7) / 8) * unsigned(h
);
499 wxByte
* out
= new wxByte
[out_size
];
500 // set bits are black
501 memset(out
, 0xff, out_size
);
502 const wxByte
* in
= image
.GetData();
503 unsigned bit_index
= 0;
504 for (int y
= 0; y
< h
; y
++)
506 for (int x
= 0; x
< w
; x
++, in
+= 3, bit_index
++)
507 if (in
[0] == 255 && in
[1] == 255 && in
[2] == 255)
508 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
509 // move index to next byte boundary
510 bit_index
= (bit_index
+ 7) & ~7u;
512 SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window
, (char*)out
, w
, h
));
517 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window
, w
, h
, depth
));
518 GdkGC
* gc
= gdk_gc_new(M_BMPDATA
->m_pixmap
);
520 M_BMPDATA
->m_pixmap
, gc
,
522 GDK_RGB_DITHER_NONE
, image
.GetData(), w
* 3);
526 const wxByte
* alpha
= image
.GetAlpha();
527 if (alpha
!= NULL
|| image
.HasMask())
529 // create mask as XBM format bitmap
531 const size_t out_size
= size_t((w
+ 7) / 8) * unsigned(h
);
532 wxByte
* out
= new wxByte
[out_size
];
533 memset(out
, 0xff, out_size
);
534 unsigned bit_index
= 0;
537 for (int y
= 0; y
< h
; y
++)
539 for (int x
= 0; x
< w
; x
++, bit_index
++)
540 if (*alpha
++ < wxIMAGE_ALPHA_THRESHOLD
)
541 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
542 bit_index
= (bit_index
+ 7) & ~7u;
547 const wxByte r_mask
= image
.GetMaskRed();
548 const wxByte g_mask
= image
.GetMaskGreen();
549 const wxByte b_mask
= image
.GetMaskBlue();
550 const wxByte
* in
= image
.GetData();
551 for (int y
= 0; y
< h
; y
++)
553 for (int x
= 0; x
< w
; x
++, in
+= 3, bit_index
++)
554 if (in
[0] == r_mask
&& in
[1] == g_mask
&& in
[2] == b_mask
)
555 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
556 bit_index
= (bit_index
+ 7) & ~7u;
559 wxMask
* mask
= new wxMask
;
560 mask
->m_bitmap
= gdk_bitmap_create_from_data(M_BMPDATA
->m_pixmap
, (char*)out
, w
, h
);
567 bool wxBitmap::CreateFromImageAsPixbuf(const wxImage
& image
)
569 int width
= image
.GetWidth();
570 int height
= image
.GetHeight();
572 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
573 true, //image.HasAlpha(),
574 8 /* bits per sample */,
579 wxASSERT( image
.HasAlpha() ); // for now
580 wxASSERT( gdk_pixbuf_get_n_channels(pixbuf
) == 4 );
581 wxASSERT( gdk_pixbuf_get_width(pixbuf
) == width
);
582 wxASSERT( gdk_pixbuf_get_height(pixbuf
) == height
);
588 unsigned char *in
= image
.GetData();
589 unsigned char *out
= gdk_pixbuf_get_pixels(pixbuf
);
590 unsigned char *alpha
= image
.GetAlpha();
592 int rowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
594 for (int y
= 0; y
< height
; y
++, out
+= rowinc
)
596 for (int x
= 0; x
< width
; x
++, alpha
++, out
+= 4, in
+= 3)
608 wxImage
wxBitmap::ConvertToImage() const
612 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
614 const int w
= GetWidth();
615 const int h
= GetHeight();
617 unsigned char *data
= image
.GetData();
619 wxCHECK_MSG(data
!= NULL
, wxNullImage
, wxT("couldn't create image") );
623 GdkPixbuf
*pixbuf
= GetPixbuf();
624 wxASSERT( gdk_pixbuf_get_has_alpha(pixbuf
) );
628 unsigned char *alpha
= image
.GetAlpha();
629 unsigned char *in
= gdk_pixbuf_get_pixels(pixbuf
);
630 unsigned char *out
= data
;
631 int rowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * w
;
633 for (int y
= 0; y
< h
; y
++, in
+= rowinc
)
635 for (int x
= 0; x
< w
; x
++, in
+= 4, out
+= 3, alpha
++)
646 GdkPixmap
* pixmap
= GetPixmap();
647 GdkPixmap
* pixmap_invert
= NULL
;
650 // mono bitmaps are inverted, i.e. 0 is white
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
;
658 // create a pixbuf which shares data with the wxImage
659 GdkPixbuf
* pixbuf
= gdk_pixbuf_new_from_data(
660 data
, GDK_COLORSPACE_RGB
, false, 8, w
, h
, 3 * w
, NULL
, NULL
);
662 gdk_pixbuf_get_from_drawable(pixbuf
, pixmap
, NULL
, 0, 0, 0, 0, w
, h
);
664 g_object_unref(pixbuf
);
665 if (pixmap_invert
!= NULL
)
666 g_object_unref(pixmap_invert
);
670 // the colour used as transparent one in wxImage and the one it is
671 // replaced with when it really occurs in the bitmap
672 const int MASK_RED
= 1;
673 const int MASK_GREEN
= 2;
674 const int MASK_BLUE
= 3;
675 const int MASK_BLUE_REPLACEMENT
= 2;
677 image
.SetMaskColour(MASK_RED
, MASK_GREEN
, MASK_BLUE
);
678 GdkImage
* image_mask
= gdk_drawable_get_image(GetMask()->GetBitmap(), 0, 0, w
, h
);
680 for (int y
= 0; y
< h
; y
++)
682 for (int x
= 0; x
< w
; x
++, data
+= 3)
684 if (gdk_image_get_pixel(image_mask
, x
, y
) == 0)
687 data
[1] = MASK_GREEN
;
690 else if (data
[0] == MASK_RED
&& data
[1] == MASK_GREEN
&& data
[2] == MASK_BLUE
)
692 data
[2] = MASK_BLUE_REPLACEMENT
;
696 g_object_unref(image_mask
);
703 wxBitmap::wxBitmap( const wxString
&filename
, wxBitmapType type
)
705 LoadFile( filename
, type
);
708 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int WXUNUSED(depth
))
710 if ( width
> 0 && height
> 0 )
712 SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window
, bits
, width
, height
));
714 wxASSERT_MSG( M_BMPDATA
->m_pixmap
, wxT("couldn't create bitmap") );
718 wxBitmap::~wxBitmap()
722 bool wxBitmap::operator == ( const wxBitmap
& bmp
) const
724 return m_refData
== bmp
.m_refData
;
727 bool wxBitmap::operator != ( const wxBitmap
& bmp
) const
729 return m_refData
!= bmp
.m_refData
;
732 bool wxBitmap::Ok() const
734 return (m_refData
!= NULL
) &&
736 M_BMPDATA
->m_pixbuf
||
741 int wxBitmap::GetHeight() const
743 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
745 return M_BMPDATA
->m_height
;
748 int wxBitmap::GetWidth() const
750 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
752 return M_BMPDATA
->m_width
;
755 int wxBitmap::GetDepth() const
757 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
759 return M_BMPDATA
->m_bpp
;
762 wxMask
*wxBitmap::GetMask() const
764 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
766 return M_BMPDATA
->m_mask
;
769 void wxBitmap::SetMask( wxMask
*mask
)
771 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
773 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
775 M_BMPDATA
->m_mask
= mask
;
778 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
784 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
789 (rect
.x
>= 0) && (rect
.y
>= 0) &&
790 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) && (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
791 ret
, wxT("invalid bitmap or bitmap region") );
795 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
796 true, //gdk_pixbuf_get_has_alpha(GetPixbuf()),
797 8, rect
.width
, rect
.height
);
798 ret
.SetPixbuf(pixbuf
);
799 ret
.SetDepth(M_BMPDATA
->m_bpp
);
800 gdk_pixbuf_copy_area(GetPixbuf(),
801 rect
.x
, rect
.y
, rect
.width
, rect
.height
,
806 ret
= wxBitmap(rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
807 GdkGC
*gc
= gdk_gc_new( ret
.GetPixmap() );
808 gdk_draw_drawable( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
814 wxMask
*mask
= new wxMask
;
815 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, rect
.width
, rect
.height
, 1 );
817 GdkGC
*gc
= gdk_gc_new( mask
->m_bitmap
);
818 gdk_draw_drawable(mask
->m_bitmap
, gc
, M_BMPDATA
->m_mask
->m_bitmap
, rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
827 bool wxBitmap::SaveFile( const wxString
&name
, wxBitmapType type
, const wxPalette
*WXUNUSED(palette
) ) const
829 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
831 // Try to save the bitmap via wxImage handlers:
832 wxImage image
= ConvertToImage();
833 return image
.Ok() && image
.SaveFile(name
, type
);
836 bool wxBitmap::LoadFile( const wxString
&name
, wxBitmapType type
)
840 if (type
== wxBITMAP_TYPE_XPM
)
842 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
843 SetPixmap(gdk_pixmap_create_from_xpm(wxGetRootWindow()->window
, &mask
, NULL
, name
.fn_str()));
847 M_BMPDATA
->m_mask
= new wxMask
;
848 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
851 else // try if wxImage can load it
854 if (image
.LoadFile(name
, type
) && image
.Ok())
855 *this = wxBitmap(image
);
862 wxPalette
*wxBitmap::GetPalette() const
865 return (wxPalette
*) NULL
;
867 return M_BMPDATA
->m_palette
;
870 void wxBitmap::SetPalette(const wxPalette
& WXUNUSED(palette
))
874 #endif // wxUSE_PALETTE
876 void wxBitmap::SetHeight( int height
)
879 m_refData
= new wxBitmapRefData
;
881 M_BMPDATA
->m_height
= height
;
884 void wxBitmap::SetWidth( int width
)
887 m_refData
= new wxBitmapRefData
;
889 M_BMPDATA
->m_width
= width
;
892 void wxBitmap::SetDepth( int depth
)
895 m_refData
= new wxBitmapRefData
;
897 M_BMPDATA
->m_bpp
= depth
;
900 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
903 m_refData
= new wxBitmapRefData
;
905 wxASSERT(M_BMPDATA
->m_pixmap
== NULL
);
906 M_BMPDATA
->m_pixmap
= pixmap
;
907 gdk_drawable_get_size(pixmap
, &M_BMPDATA
->m_width
, &M_BMPDATA
->m_height
);
908 M_BMPDATA
->m_bpp
= gdk_drawable_get_depth(pixmap
);
909 PurgeOtherRepresentations(Pixmap
);
912 GdkPixmap
*wxBitmap::GetPixmap() const
914 wxCHECK_MSG( Ok(), (GdkPixmap
*) NULL
, wxT("invalid bitmap") );
916 // create the pixmap on the fly if we use Pixbuf representation:
917 if (M_BMPDATA
->m_pixmap
== NULL
)
919 delete M_BMPDATA
->m_mask
;
920 M_BMPDATA
->m_mask
= new wxMask
;
921 gdk_pixbuf_render_pixmap_and_mask(M_BMPDATA
->m_pixbuf
,
922 &M_BMPDATA
->m_pixmap
,
923 &M_BMPDATA
->m_mask
->m_bitmap
,
927 return M_BMPDATA
->m_pixmap
;
930 bool wxBitmap::HasPixmap() const
932 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
934 return M_BMPDATA
->m_pixmap
!= NULL
;
937 GdkPixbuf
*wxBitmap::GetPixbuf() const
939 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
941 if (M_BMPDATA
->m_pixbuf
== NULL
)
943 int width
= GetWidth();
944 int height
= GetHeight();
946 // always create the alpha channel so raw bitmap access will work
948 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
949 true, // GetMask() != NULL,
951 M_BMPDATA
->m_pixbuf
=
952 gdk_pixbuf_get_from_drawable(pixbuf
, M_BMPDATA
->m_pixmap
, NULL
,
953 0, 0, 0, 0, width
, height
);
955 // apply the mask to created pixbuf:
956 if (M_BMPDATA
->m_pixbuf
&& M_BMPDATA
->m_mask
)
959 gdk_pixbuf_get_from_drawable(NULL
,
960 M_BMPDATA
->m_mask
->GetBitmap(),
962 0, 0, 0, 0, width
, height
);
965 guchar
*bmp
= gdk_pixbuf_get_pixels(pixbuf
);
966 guchar
*mask
= gdk_pixbuf_get_pixels(pmask
);
967 int bmprowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
968 int maskrowinc
= gdk_pixbuf_get_rowstride(pmask
) - 3 * width
;
970 for (int y
= 0; y
< height
;
971 y
++, bmp
+= bmprowinc
, mask
+= maskrowinc
)
973 for (int x
= 0; x
< width
; x
++, bmp
+= 4, mask
+= 3)
975 if (mask
[0] == 0 /*black pixel*/)
980 g_object_unref (pmask
);
985 return M_BMPDATA
->m_pixbuf
;
988 bool wxBitmap::HasPixbuf() const
990 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
992 return M_BMPDATA
->m_pixbuf
!= NULL
;
995 void wxBitmap::SetPixbuf( GdkPixbuf
*pixbuf
)
998 m_refData
= new wxBitmapRefData
;
1000 wxASSERT(M_BMPDATA
->m_pixbuf
== NULL
);
1001 M_BMPDATA
->m_pixbuf
= pixbuf
;
1002 M_BMPDATA
->m_width
= gdk_pixbuf_get_width(pixbuf
);
1003 M_BMPDATA
->m_height
= gdk_pixbuf_get_height(pixbuf
);
1004 PurgeOtherRepresentations(Pixbuf
);
1007 void wxBitmap::PurgeOtherRepresentations(wxBitmap::Representation keep
)
1009 if (keep
== Pixmap
&& HasPixbuf())
1011 g_object_unref (M_BMPDATA
->m_pixbuf
);
1012 M_BMPDATA
->m_pixbuf
= NULL
;
1014 if (keep
== Pixbuf
&& HasPixmap())
1016 g_object_unref (M_BMPDATA
->m_pixmap
);
1017 M_BMPDATA
->m_pixmap
= NULL
;
1021 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1026 GdkPixbuf
*pixbuf
= GetPixbuf();
1030 if (!gdk_pixbuf_get_has_alpha( pixbuf
))
1034 if (gdk_pixbuf_get_has_alpha( pixbuf
))
1035 wxPrintf( wxT("Has alpha, %d channels\n"), gdk_pixbuf_get_n_channels(pixbuf
) );
1037 wxPrintf( wxT("No alpha, %d channels.\n"), gdk_pixbuf_get_n_channels(pixbuf
) );
1040 data
.m_height
= gdk_pixbuf_get_height( pixbuf
);
1041 data
.m_width
= gdk_pixbuf_get_width( pixbuf
);
1042 data
.m_stride
= gdk_pixbuf_get_rowstride( pixbuf
);
1044 return gdk_pixbuf_get_pixels( pixbuf
);
1047 void wxBitmap::UngetRawData(wxPixelDataBase
& WXUNUSED(data
))
1052 bool wxBitmap::HasAlpha() const
1057 void wxBitmap::UseAlpha()
1062 //-----------------------------------------------------------------------------
1064 //-----------------------------------------------------------------------------
1066 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
,wxBitmapHandlerBase
)
1068 wxBitmapHandler::~wxBitmapHandler()
1072 bool wxBitmapHandler::Create(wxBitmap
* WXUNUSED(bitmap
),
1073 void * WXUNUSED(data
),
1074 long WXUNUSED(type
),
1075 int WXUNUSED(width
),
1076 int WXUNUSED(height
),
1077 int WXUNUSED(depth
))
1079 wxFAIL_MSG( _T("not implemented") );
1084 bool wxBitmapHandler::LoadFile(wxBitmap
* WXUNUSED(bitmap
),
1085 const wxString
& WXUNUSED(name
),
1086 long WXUNUSED(flags
),
1087 int WXUNUSED(desiredWidth
),
1088 int WXUNUSED(desiredHeight
))
1090 wxFAIL_MSG( _T("not implemented") );
1095 bool wxBitmapHandler::SaveFile(const wxBitmap
* WXUNUSED(bitmap
),
1096 const wxString
& WXUNUSED(name
),
1098 const wxPalette
* WXUNUSED(palette
))
1100 wxFAIL_MSG( _T("not implemented") );
1105 /* static */ void wxBitmap::InitStandardHandlers()
1107 // TODO: Insert handler based on GdkPixbufs handler later