]>
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();
197 wxPalette
*m_palette
;
198 #endif // wxUSE_PALETTE
201 wxBitmapRefData::wxBitmapRefData()
203 m_pixmap
= (GdkPixmap
*) NULL
;
204 m_pixbuf
= (GdkPixbuf
*) NULL
;
205 m_mask
= (wxMask
*) NULL
;
210 m_palette
= (wxPalette
*) NULL
;
211 #endif // wxUSE_PALETTE
214 wxBitmapRefData::~wxBitmapRefData()
217 g_object_unref (m_pixmap
);
219 g_object_unref (m_pixbuf
);
223 #endif // wxUSE_PALETTE
226 //-----------------------------------------------------------------------------
228 #define M_BMPDATA wx_static_cast(wxBitmapRefData*, m_refData)
230 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
232 wxBitmap::wxBitmap(int width
, int height
, int depth
)
234 Create(width
, height
, depth
);
237 wxBitmap::wxBitmap(const wxString
&filename
, wxBitmapType type
)
239 LoadFile(filename
, type
);
242 wxBitmap::wxBitmap(const char bits
[], int width
, int height
, int depth
)
244 wxASSERT(depth
== 1);
245 if (width
> 0 && height
> 0 && depth
== 1)
247 SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window
, bits
, width
, height
));
249 wxASSERT_MSG( M_BMPDATA
->m_pixmap
, wxT("couldn't create bitmap") );
253 wxBitmap::wxBitmap(const char* const* bits
)
255 wxCHECK2_MSG(bits
!= NULL
, return, wxT("invalid bitmap data"));
257 GdkBitmap
* mask
= NULL
;
258 SetPixmap(gdk_pixmap_create_from_xpm_d(wxGetRootWindow()->window
, &mask
, NULL
, wx_const_cast(char**, bits
)));
260 if (M_BMPDATA
->m_pixmap
!= NULL
&& mask
!= NULL
)
262 M_BMPDATA
->m_mask
= new wxMask
;
263 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
267 wxBitmap::~wxBitmap()
271 bool wxBitmap::Create( int width
, int height
, int depth
)
275 if ( width
<= 0 || height
<= 0 )
282 SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB
, true, 8, width
, height
), 32);
288 const GdkVisual
* visual
= wxTheApp
->GetGdkVisual();
290 depth
= visual
->depth
;
292 wxCHECK_MSG(depth
== visual
->depth
, false, wxT("invalid bitmap depth"));
295 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window
, width
, height
, depth
));
301 wxBitmap
wxBitmap::Rescale(int clipx
, int clipy
, int clipwidth
, int clipheight
, int newx
, int newy
) const
305 wxCHECK_MSG(Ok(), bmp
, wxT("invalid bitmap"));
307 if (newy
==M_BMPDATA
->m_width
&& newy
==M_BMPDATA
->m_height
)
310 int width
= wxMax(newx
, 1);
311 int height
= wxMax(newy
, 1);
312 width
= wxMin(width
, clipwidth
);
313 height
= wxMin(height
, clipheight
);
315 // scale pixbuf if available and it has alpha or there is no mask
316 if (M_BMPDATA
->m_pixbuf
!= NULL
&& (
317 M_BMPDATA
->m_mask
== NULL
|| gdk_pixbuf_get_has_alpha(M_BMPDATA
->m_pixbuf
)))
319 bmp
.SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
320 gdk_pixbuf_get_has_alpha(M_BMPDATA
->m_pixbuf
),
321 8, width
, height
), M_BMPDATA
->m_bpp
);
322 gdk_pixbuf_scale(M_BMPDATA
->m_pixbuf
, bmp
.GetPixbuf(),
325 (double)newx
/GetWidth(), (double)newy
/GetHeight(),
326 GDK_INTERP_BILINEAR
);
330 GdkImage
* img
= gdk_drawable_get_image(
331 M_BMPDATA
->m_pixmap
, 0, 0, M_BMPDATA
->m_width
, M_BMPDATA
->m_height
);
333 wxCHECK_MSG(img
, bmp
, wxT("couldn't create image"));
336 GdkPixmap
*dstpix
= NULL
;
338 long dstbyteperline
= 0;
342 bmp
.Create(width
, height
, gdk_drawable_get_depth(M_BMPDATA
->m_pixmap
));
343 dstpix
= bmp
.GetPixmap();
344 gc
= gdk_gc_new( dstpix
);
348 dstbyteperline
= (width
+ 7) / 8;
349 dst
= (char*) malloc(dstbyteperline
*height
);
352 // be careful to use the right scaling factor
353 float scx
= (float)M_BMPDATA
->m_width
/(float)newx
;
354 float scy
= (float)M_BMPDATA
->m_height
/(float)newy
;
355 // prepare accel-tables
356 int *tablex
= (int *)calloc(width
,sizeof(int));
357 int *tabley
= (int *)calloc(height
,sizeof(int));
359 // accel table filled with clipped values
360 for (int x
= 0; x
< width
; x
++)
361 tablex
[x
] = (int) (scx
* (x
+clipx
));
362 for (int y
= 0; y
< height
; y
++)
363 tabley
[y
] = (int) (scy
* (y
+clipy
));
365 // Main rescaling routine starts here
366 for (int h
= 0; h
< height
; h
++)
370 guint32 old_pixval
= 0;
372 for (int w
= 0; w
< width
; w
++)
380 pixval
= gdk_image_get_pixel( img
, x
, tabley
[h
] );
390 char shift
= bit
<< (w
% 8);
396 dst
[h
*dstbyteperline
+w
/8] = outbyte
;
404 gdk_gc_set_foreground( gc
, &col
);
405 gdk_draw_point( dstpix
, gc
, w
, h
);
409 // do not forget the last byte
410 if ( dst
&& (width
% 8 != 0) )
411 dst
[h
*dstbyteperline
+width
/8] = outbyte
;
414 g_object_unref (img
);
415 if (gc
) g_object_unref (gc
);
419 bmp
= wxBitmap(dst
, width
, height
, 1);
425 dstbyteperline
= (width
+ 7) / 8;
426 dst
= (char*) malloc(dstbyteperline
*height
);
427 img
= gdk_drawable_get_image(GetMask()->GetBitmap(), 0, 0, GetWidth(), GetHeight());
429 for (int h
= 0; h
< height
; h
++)
433 guint32 old_pixval
= 0;
435 for (int w
= 0; w
< width
; w
++)
443 pixval
= gdk_image_get_pixel( img
, x
, tabley
[h
] );
451 char shift
= bit
<< (w
% 8);
457 dst
[h
*dstbyteperline
+w
/8] = outbyte
;
462 // do not forget the last byte
464 dst
[h
*dstbyteperline
+width
/8] = outbyte
;
466 wxMask
* mask
= new wxMask
;
467 mask
->m_bitmap
= gdk_bitmap_create_from_data( wxGetRootWindow()->window
, (gchar
*) dst
, width
, height
);
471 g_object_unref (img
);
481 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
485 wxCHECK_MSG( image
.Ok(), false, wxT("invalid image") );
486 wxCHECK_MSG( depth
== -1 || depth
== 1, false, wxT("invalid bitmap depth") );
488 if (image
.GetWidth() <= 0 || image
.GetHeight() <= 0)
491 // create pixbuf if image has alpha and requested depth is compatible
492 if (image
.HasAlpha() && (depth
== -1 || depth
== 32))
493 return CreateFromImageAsPixbuf(image
);
495 // otherwise create pixmap, if alpha is present it will be converted to mask
496 return CreateFromImageAsPixmap(image
, depth
);
499 bool wxBitmap::CreateFromImageAsPixmap(const wxImage
& image
, int depth
)
501 const int w
= image
.GetWidth();
502 const int h
= image
.GetHeight();
505 // create XBM format bitmap
507 // one bit per pixel, each row starts on a byte boundary
508 const size_t out_size
= size_t((w
+ 7) / 8) * unsigned(h
);
509 wxByte
* out
= new wxByte
[out_size
];
510 // set bits are black
511 memset(out
, 0xff, out_size
);
512 const wxByte
* in
= image
.GetData();
513 unsigned bit_index
= 0;
514 for (int y
= 0; y
< h
; y
++)
516 for (int x
= 0; x
< w
; x
++, in
+= 3, bit_index
++)
517 if (in
[0] == 255 && in
[1] == 255 && in
[2] == 255)
518 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
519 // move index to next byte boundary
520 bit_index
= (bit_index
+ 7) & ~7u;
522 SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window
, (char*)out
, w
, h
));
527 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window
, w
, h
, depth
));
528 GdkGC
* gc
= gdk_gc_new(M_BMPDATA
->m_pixmap
);
530 M_BMPDATA
->m_pixmap
, gc
,
532 GDK_RGB_DITHER_NONE
, image
.GetData(), w
* 3);
536 const wxByte
* alpha
= image
.GetAlpha();
537 if (alpha
!= NULL
|| image
.HasMask())
539 // create mask as XBM format bitmap
541 const size_t out_size
= size_t((w
+ 7) / 8) * unsigned(h
);
542 wxByte
* out
= new wxByte
[out_size
];
543 memset(out
, 0xff, out_size
);
544 unsigned bit_index
= 0;
547 for (int y
= 0; y
< h
; y
++)
549 for (int x
= 0; x
< w
; x
++, bit_index
++)
550 if (*alpha
++ < wxIMAGE_ALPHA_THRESHOLD
)
551 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
552 bit_index
= (bit_index
+ 7) & ~7u;
557 const wxByte r_mask
= image
.GetMaskRed();
558 const wxByte g_mask
= image
.GetMaskGreen();
559 const wxByte b_mask
= image
.GetMaskBlue();
560 const wxByte
* in
= image
.GetData();
561 for (int y
= 0; y
< h
; y
++)
563 for (int x
= 0; x
< w
; x
++, in
+= 3, bit_index
++)
564 if (in
[0] == r_mask
&& in
[1] == g_mask
&& in
[2] == b_mask
)
565 out
[bit_index
>> 3] ^= 1 << (bit_index
& 7);
566 bit_index
= (bit_index
+ 7) & ~7u;
569 wxMask
* mask
= new wxMask
;
570 mask
->m_bitmap
= gdk_bitmap_create_from_data(M_BMPDATA
->m_pixmap
, (char*)out
, w
, h
);
577 bool wxBitmap::CreateFromImageAsPixbuf(const wxImage
& image
)
579 wxASSERT(image
.HasAlpha());
581 int width
= image
.GetWidth();
582 int height
= image
.GetHeight();
584 Create(width
, height
, 32);
585 GdkPixbuf
* pixbuf
= M_BMPDATA
->m_pixbuf
;
590 const unsigned char* in
= image
.GetData();
591 unsigned char *out
= gdk_pixbuf_get_pixels(pixbuf
);
592 unsigned char *alpha
= image
.GetAlpha();
594 int rowpad
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
596 for (int y
= 0; y
< height
; y
++, out
+= rowpad
)
598 for (int x
= 0; x
< width
; x
++, alpha
++, out
+= 4, in
+= 3)
610 wxImage
wxBitmap::ConvertToImage() const
612 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
614 const int w
= GetWidth();
615 const int h
= GetHeight();
616 wxImage
image(w
, h
, false);
617 unsigned char *data
= image
.GetData();
619 wxCHECK_MSG(data
!= NULL
, wxNullImage
, wxT("couldn't create image") );
621 // prefer pixbuf if available, it will preserve alpha and should be quicker
624 GdkPixbuf
*pixbuf
= GetPixbuf();
625 unsigned char* alpha
= NULL
;
626 if (gdk_pixbuf_get_has_alpha(pixbuf
))
629 alpha
= image
.GetAlpha();
631 const unsigned char* in
= gdk_pixbuf_get_pixels(pixbuf
);
632 unsigned char *out
= data
;
633 const int inc
= 3 + int(alpha
!= NULL
);
634 const int rowpad
= gdk_pixbuf_get_rowstride(pixbuf
) - inc
* w
;
636 for (int y
= 0; y
< h
; y
++, in
+= rowpad
)
638 for (int x
= 0; x
< w
; x
++, in
+= inc
, out
+= 3)
650 GdkPixmap
* pixmap
= GetPixmap();
651 GdkPixmap
* pixmap_invert
= NULL
;
654 // mono bitmaps are inverted, i.e. 0 is white
655 pixmap_invert
= gdk_pixmap_new(pixmap
, w
, h
, 1);
656 GdkGC
* gc
= gdk_gc_new(pixmap_invert
);
657 gdk_gc_set_function(gc
, GDK_COPY_INVERT
);
658 gdk_draw_drawable(pixmap_invert
, gc
, pixmap
, 0, 0, 0, 0, w
, h
);
660 pixmap
= pixmap_invert
;
662 // create a pixbuf which shares data with the wxImage
663 GdkPixbuf
* pixbuf
= gdk_pixbuf_new_from_data(
664 data
, GDK_COLORSPACE_RGB
, false, 8, w
, h
, 3 * w
, NULL
, NULL
);
666 gdk_pixbuf_get_from_drawable(pixbuf
, pixmap
, NULL
, 0, 0, 0, 0, w
, h
);
668 g_object_unref(pixbuf
);
669 if (pixmap_invert
!= NULL
)
670 g_object_unref(pixmap_invert
);
672 // convert mask, unless there is already alpha
673 if (GetMask() && !image
.HasAlpha())
675 // we hard code the mask colour for now but we could also make an
676 // effort (and waste time) to choose a colour not present in the
677 // image already to avoid having to fudge the pixels below --
678 // whether it's worth to do it is unclear however
679 const int MASK_RED
= 1;
680 const int MASK_GREEN
= 2;
681 const int MASK_BLUE
= 3;
682 const int MASK_BLUE_REPLACEMENT
= 2;
684 image
.SetMaskColour(MASK_RED
, MASK_GREEN
, MASK_BLUE
);
685 GdkImage
* image_mask
= gdk_drawable_get_image(GetMask()->GetBitmap(), 0, 0, w
, h
);
687 for (int y
= 0; y
< h
; y
++)
689 for (int x
= 0; x
< w
; x
++, data
+= 3)
691 if (gdk_image_get_pixel(image_mask
, x
, y
) == 0)
694 data
[1] = MASK_GREEN
;
697 else if (data
[0] == MASK_RED
&& data
[1] == MASK_GREEN
&& data
[2] == MASK_BLUE
)
699 // we have to fudge the colour a bit to prevent
700 // this pixel from appearing transparent
701 data
[2] = MASK_BLUE_REPLACEMENT
;
705 g_object_unref(image_mask
);
711 bool wxBitmap::operator == ( const wxBitmap
& bmp
) const
713 return m_refData
== bmp
.m_refData
;
716 bool wxBitmap::IsOk() const
718 return (m_refData
!= NULL
) &&
720 M_BMPDATA
->m_pixbuf
||
725 int wxBitmap::GetHeight() const
727 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
729 return M_BMPDATA
->m_height
;
732 int wxBitmap::GetWidth() const
734 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
736 return M_BMPDATA
->m_width
;
739 int wxBitmap::GetDepth() const
741 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
743 return M_BMPDATA
->m_bpp
;
746 wxMask
*wxBitmap::GetMask() const
748 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
750 return M_BMPDATA
->m_mask
;
753 void wxBitmap::SetMask( wxMask
*mask
)
755 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
758 delete M_BMPDATA
->m_mask
;
759 M_BMPDATA
->m_mask
= mask
;
762 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
768 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
772 wxCHECK_MSG(Ok(), ret
, wxT("invalid bitmap"));
773 wxCHECK_MSG(rect
.x
>= 0 && rect
.y
>= 0 &&
774 rect
.x
+ rect
.width
<= M_BMPDATA
->m_width
&&
775 rect
.y
+ rect
.height
<= M_BMPDATA
->m_height
,
776 ret
, wxT("invalid bitmap region"));
778 if (HasPixbuf() || M_BMPDATA
->m_bpp
== 32)
780 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
781 gdk_pixbuf_get_has_alpha(GetPixbuf()),
782 8, rect
.width
, rect
.height
);
783 ret
.SetPixbuf(pixbuf
, M_BMPDATA
->m_bpp
);
784 gdk_pixbuf_copy_area(GetPixbuf(),
785 rect
.x
, rect
.y
, rect
.width
, rect
.height
,
790 ret
.Create(rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
791 GdkGC
*gc
= gdk_gc_new( ret
.GetPixmap() );
792 gdk_draw_drawable( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
795 // make mask, unless there is already alpha
796 if (GetMask() && !HasAlpha())
798 wxMask
*mask
= new wxMask
;
799 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, rect
.width
, rect
.height
, 1 );
801 GdkGC
*gc
= gdk_gc_new( mask
->m_bitmap
);
802 gdk_draw_drawable(mask
->m_bitmap
, gc
, M_BMPDATA
->m_mask
->m_bitmap
, rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
811 bool wxBitmap::SaveFile( const wxString
&name
, wxBitmapType type
, const wxPalette
*WXUNUSED(palette
) ) const
813 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
815 // Try to save the bitmap via wxImage handlers:
816 wxImage image
= ConvertToImage();
817 return image
.Ok() && image
.SaveFile(name
, type
);
820 bool wxBitmap::LoadFile( const wxString
&name
, wxBitmapType type
)
824 if (type
== wxBITMAP_TYPE_XPM
)
826 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
827 SetPixmap(gdk_pixmap_create_from_xpm(wxGetRootWindow()->window
, &mask
, NULL
, name
.fn_str()));
831 M_BMPDATA
->m_mask
= new wxMask
;
832 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
835 else // try if wxImage can load it
838 if (image
.LoadFile(name
, type
) && image
.Ok())
839 CreateFromImage(image
, -1);
846 wxPalette
*wxBitmap::GetPalette() const
848 wxCHECK_MSG(Ok(), NULL
, wxT("invalid bitmap"));
850 return M_BMPDATA
->m_palette
;
853 void wxBitmap::SetPalette(const wxPalette
& WXUNUSED(palette
))
857 #endif // wxUSE_PALETTE
859 void wxBitmap::SetHeight( int height
)
862 M_BMPDATA
->m_height
= height
;
865 void wxBitmap::SetWidth( int width
)
868 M_BMPDATA
->m_width
= width
;
871 void wxBitmap::SetDepth( int depth
)
874 M_BMPDATA
->m_bpp
= depth
;
877 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
880 m_refData
= new wxBitmapRefData
;
882 // AllocExclusive should not be needed for this internal function
883 wxASSERT(m_refData
->GetRefCount() == 1);
884 wxASSERT(M_BMPDATA
->m_pixmap
== NULL
);
885 M_BMPDATA
->m_pixmap
= pixmap
;
886 gdk_drawable_get_size(pixmap
, &M_BMPDATA
->m_width
, &M_BMPDATA
->m_height
);
887 M_BMPDATA
->m_bpp
= gdk_drawable_get_depth(pixmap
);
888 PurgeOtherRepresentations(Pixmap
);
891 GdkPixmap
*wxBitmap::GetPixmap() const
893 wxCHECK_MSG( Ok(), (GdkPixmap
*) NULL
, wxT("invalid bitmap") );
895 // create the pixmap on the fly if we use Pixbuf representation:
896 if (M_BMPDATA
->m_pixmap
== NULL
)
898 GdkPixmap
** pmask
= NULL
;
899 if (gdk_pixbuf_get_has_alpha(M_BMPDATA
->m_pixbuf
))
901 // make new mask from alpha
902 delete M_BMPDATA
->m_mask
;
903 M_BMPDATA
->m_mask
= new wxMask
;
904 pmask
= &M_BMPDATA
->m_mask
->m_bitmap
;
906 gdk_pixbuf_render_pixmap_and_mask(M_BMPDATA
->m_pixbuf
,
907 &M_BMPDATA
->m_pixmap
,
909 wxIMAGE_ALPHA_THRESHOLD
);
912 return M_BMPDATA
->m_pixmap
;
915 bool wxBitmap::HasPixmap() const
917 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
919 return M_BMPDATA
->m_pixmap
!= NULL
;
922 GdkPixbuf
*wxBitmap::GetPixbuf() const
924 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
926 if (M_BMPDATA
->m_pixbuf
== NULL
)
928 int width
= GetWidth();
929 int height
= GetHeight();
931 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
934 M_BMPDATA
->m_pixbuf
= pixbuf
;
935 gdk_pixbuf_get_from_drawable(pixbuf
, M_BMPDATA
->m_pixmap
, NULL
,
936 0, 0, 0, 0, width
, height
);
938 // apply the mask to created pixbuf:
939 if (M_BMPDATA
->m_pixbuf
&& M_BMPDATA
->m_mask
)
942 gdk_pixbuf_get_from_drawable(NULL
,
943 M_BMPDATA
->m_mask
->GetBitmap(),
945 0, 0, 0, 0, width
, height
);
948 guchar
*bmp
= gdk_pixbuf_get_pixels(pixbuf
);
949 guchar
*mask
= gdk_pixbuf_get_pixels(pmask
);
950 int bmprowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
951 int maskrowinc
= gdk_pixbuf_get_rowstride(pmask
) - 3 * width
;
953 for (int y
= 0; y
< height
;
954 y
++, bmp
+= bmprowinc
, mask
+= maskrowinc
)
956 for (int x
= 0; x
< width
; x
++, bmp
+= 4, mask
+= 3)
958 if (mask
[0] == 0 /*black pixel*/)
963 g_object_unref (pmask
);
968 return M_BMPDATA
->m_pixbuf
;
971 bool wxBitmap::HasPixbuf() const
973 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
975 return M_BMPDATA
->m_pixbuf
!= NULL
;
978 void wxBitmap::SetPixbuf(GdkPixbuf
* pixbuf
, int depth
)
981 m_refData
= new wxBitmapRefData
;
983 // AllocExclusive should not be needed for this internal function
984 wxASSERT(m_refData
->GetRefCount() == 1);
985 wxASSERT(M_BMPDATA
->m_pixbuf
== NULL
);
986 M_BMPDATA
->m_pixbuf
= pixbuf
;
987 M_BMPDATA
->m_width
= gdk_pixbuf_get_width(pixbuf
);
988 M_BMPDATA
->m_height
= gdk_pixbuf_get_height(pixbuf
);
989 // if depth specified
991 M_BMPDATA
->m_bpp
= depth
;
992 else if (M_BMPDATA
->m_bpp
== 0)
993 // use something reasonable
994 M_BMPDATA
->m_bpp
= wxTheApp
->GetGdkVisual()->depth
;
995 PurgeOtherRepresentations(Pixbuf
);
998 void wxBitmap::PurgeOtherRepresentations(wxBitmap::Representation keep
)
1000 if (keep
== Pixmap
&& HasPixbuf())
1002 g_object_unref (M_BMPDATA
->m_pixbuf
);
1003 M_BMPDATA
->m_pixbuf
= NULL
;
1005 if (keep
== Pixbuf
&& HasPixmap())
1007 g_object_unref (M_BMPDATA
->m_pixmap
);
1008 M_BMPDATA
->m_pixmap
= NULL
;
1012 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1015 GdkPixbuf
*pixbuf
= GetPixbuf();
1016 const bool hasAlpha
= HasAlpha();
1017 // allow access if bpp is valid and matches existence of alpha
1018 if (pixbuf
!= NULL
&& (
1019 bpp
== 24 && !hasAlpha
||
1020 bpp
== 32 && hasAlpha
))
1022 data
.m_height
= gdk_pixbuf_get_height( pixbuf
);
1023 data
.m_width
= gdk_pixbuf_get_width( pixbuf
);
1024 data
.m_stride
= gdk_pixbuf_get_rowstride( pixbuf
);
1025 bits
= gdk_pixbuf_get_pixels(pixbuf
);
1030 void wxBitmap::UngetRawData(wxPixelDataBase
& WXUNUSED(data
))
1034 bool wxBitmap::HasAlpha() const
1036 return m_refData
!= NULL
&& M_BMPDATA
->m_pixbuf
!= NULL
&&
1037 gdk_pixbuf_get_has_alpha(M_BMPDATA
->m_pixbuf
);
1040 void wxBitmap::UseAlpha()
1042 GdkPixbuf
* pixbuf
= GetPixbuf();
1043 // add alpha if necessary
1044 if (!gdk_pixbuf_get_has_alpha(pixbuf
))
1046 M_BMPDATA
->m_pixbuf
= NULL
;
1048 M_BMPDATA
->m_pixbuf
= gdk_pixbuf_add_alpha(pixbuf
, false, 0, 0, 0);
1049 g_object_unref(pixbuf
);
1053 wxObjectRefData
* wxBitmap::CreateRefData() const
1055 return new wxBitmapRefData
;
1058 wxObjectRefData
* wxBitmap::CloneRefData(const wxObjectRefData
* data
) const
1060 const wxBitmapRefData
* oldRef
= wx_static_cast(const wxBitmapRefData
*, data
);
1061 wxBitmapRefData
* newRef
= new wxBitmapRefData
;
1062 newRef
->m_width
= oldRef
->m_width
;
1063 newRef
->m_height
= oldRef
->m_height
;
1064 newRef
->m_bpp
= oldRef
->m_bpp
;
1065 if (oldRef
->m_pixmap
!= NULL
)
1067 newRef
->m_pixmap
= gdk_pixmap_new(
1068 oldRef
->m_pixmap
, oldRef
->m_width
, oldRef
->m_height
,
1069 // use pixmap depth, m_bpp may not match
1070 gdk_drawable_get_depth(oldRef
->m_pixmap
));
1071 GdkGC
* gc
= gdk_gc_new(newRef
->m_pixmap
);
1073 newRef
->m_pixmap
, gc
, oldRef
->m_pixmap
, 0, 0, 0, 0, -1, -1);
1076 if (oldRef
->m_pixbuf
!= NULL
)
1078 newRef
->m_pixbuf
= gdk_pixbuf_copy(oldRef
->m_pixbuf
);
1080 if (oldRef
->m_mask
!= NULL
)
1082 newRef
->m_mask
= new wxMask
;
1083 newRef
->m_mask
->m_bitmap
= gdk_pixmap_new(
1084 oldRef
->m_mask
->m_bitmap
, oldRef
->m_width
, oldRef
->m_height
, 1);
1085 GdkGC
* gc
= gdk_gc_new(newRef
->m_mask
->m_bitmap
);
1086 gdk_draw_drawable(newRef
->m_mask
->m_bitmap
,
1087 gc
, oldRef
->m_mask
->m_bitmap
, 0, 0, 0, 0, -1, -1);
1091 // implement this if SetPalette is ever implemented
1092 wxASSERT(oldRef
->m_palette
== NULL
);
1098 //-----------------------------------------------------------------------------
1100 //-----------------------------------------------------------------------------
1102 IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler
, wxBitmapHandlerBase
)
1104 /* static */ void wxBitmap::InitStandardHandlers()
1106 // TODO: Insert handler based on GdkPixbufs handler later