1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/bitmap.cpp
4 // Author: Robert Roebling
5 // Copyright: (c) 1998 Robert Roebling
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
9 // For compilers that support precompilation, includes "wx.h".
10 #include "wx/wxprec.h"
12 #include "wx/bitmap.h"
16 #include "wx/dcmemory.h"
17 #include "wx/palette.h"
23 #include "wx/filefn.h"
29 #include <gdk/gdkrgb.h>
32 void gdk_wx_draw_bitmap (GdkDrawable
*drawable
,
42 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
46 extern GtkWidget
*wxGetRootWindow();
48 //-----------------------------------------------------------------------------
50 //-----------------------------------------------------------------------------
52 IMPLEMENT_DYNAMIC_CLASS(wxMask
,wxObject
)
59 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
62 Create( bitmap
, colour
);
66 wxMask::wxMask( const wxBitmap
& bitmap
, int paletteIndex
)
69 Create( bitmap
, paletteIndex
);
71 #endif // wxUSE_PALETTE
73 wxMask::wxMask( const wxBitmap
& bitmap
)
82 gdk_bitmap_unref( m_bitmap
);
85 bool wxMask::Create( const wxBitmap
& bitmap
,
86 const wxColour
& colour
)
90 gdk_bitmap_unref( m_bitmap
);
94 wxImage image
= bitmap
.ConvertToImage();
95 if (!image
.IsOk()) return false;
97 m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, image
.GetWidth(), image
.GetHeight(), 1 );
98 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
105 gdk_gc_set_foreground( gc
, &color
);
106 gdk_gc_set_fill( gc
, GDK_SOLID
);
107 gdk_draw_rectangle( m_bitmap
, gc
, TRUE
, 0, 0, image
.GetWidth(), image
.GetHeight() );
109 unsigned char *data
= image
.GetData();
112 unsigned char red
= colour
.Red();
113 unsigned char green
= colour
.Green();
114 unsigned char blue
= colour
.Blue();
116 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
118 int bpp
= visual
->depth
;
119 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800))
124 green
= green
& 0xf8;
130 green
= green
& 0xfc;
136 green
= green
& 0xf0;
144 gdk_gc_set_foreground( gc
, &color
);
146 for (int j
= 0; j
< image
.GetHeight(); j
++)
150 for (i
= 0; i
< image
.GetWidth(); i
++)
152 if ((data
[index
] == red
) &&
153 (data
[index
+1] == green
) &&
154 (data
[index
+2] == blue
))
163 gdk_draw_line( m_bitmap
, gc
, start_x
, j
, i
-1, j
);
170 gdk_draw_line( m_bitmap
, gc
, start_x
, j
, i
, j
);
179 bool wxMask::Create( const wxBitmap
& bitmap
, int paletteIndex
)
182 wxPalette
*pal
= bitmap
.GetPalette();
184 wxCHECK_MSG( pal
, false, wxT("Cannot create mask from bitmap without palette") );
186 pal
->GetRGB(paletteIndex
, &r
, &g
, &b
);
188 return Create(bitmap
, wxColour(r
, g
, b
));
190 #endif // wxUSE_PALETTE
192 bool wxMask::Create( const wxBitmap
& bitmap
)
196 gdk_bitmap_unref( m_bitmap
);
200 if (!bitmap
.IsOk()) return false;
202 wxCHECK_MSG( bitmap
.GetBitmap(), false, wxT("Cannot create mask from colour bitmap") );
204 m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, bitmap
.GetWidth(), bitmap
.GetHeight(), 1 );
206 if (!m_bitmap
) return false;
208 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
210 gdk_wx_draw_bitmap( m_bitmap
, gc
, bitmap
.GetBitmap(), 0, 0, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight() );
217 GdkBitmap
*wxMask::GetBitmap() const
223 //-----------------------------------------------------------------------------
225 //-----------------------------------------------------------------------------
227 class wxBitmapRefData
: public wxGDIRefData
231 wxBitmapRefData(const wxBitmapRefData
& data
);
232 bool Create(int width
, int height
, int bpp
);
233 virtual ~wxBitmapRefData();
235 virtual bool IsOk() const { return m_pixmap
|| m_bitmap
; }
244 wxPalette
*m_palette
;
245 #endif // wxUSE_PALETTE
248 wxBitmapRefData::wxBitmapRefData()
258 #endif // wxUSE_PALETTE
261 wxBitmapRefData::wxBitmapRefData(const wxBitmapRefData
& data
)
263 Create(data
.m_width
, data
.m_height
, data
.m_bpp
);
265 m_mask
= data
.m_mask
? new wxMask(*data
.m_mask
) : NULL
;
268 wxASSERT_MSG( !data
.m_palette
,
269 wxT("copying bitmaps palette not implemented") );
270 #endif // wxUSE_PALETTE
273 // copy the bitmap data by simply drawing the source bitmap on this one
279 else if ( data
.m_bitmap
)
283 else // invalid bitmap?
288 GdkGC
*gc
= gdk_gc_new(*dst
);
291 gdk_wx_draw_bitmap(m_bitmap
, gc
, data
.m_bitmap
, 0, 0, 0, 0, -1, -1);
293 else // colour pixmap
295 gdk_draw_pixmap(m_pixmap
, gc
, data
.m_pixmap
, 0, 0, 0, 0, -1, -1);
301 bool wxBitmapRefData::Create(int width
, int height
, int bpp
)
312 // to understand how this compiles you should know that GdkPixmap and
313 // GdkBitmap are one and the same type in GTK+ 1
317 const GdkVisual
* const visual
= wxTheApp
->GetGdkVisual();
319 wxCHECK_MSG( (bpp
== -1) || (bpp
== visual
->depth
) || (bpp
== 32), false,
320 wxT("invalid bitmap depth") );
322 m_bpp
= visual
->depth
;
333 *ppix
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, m_bpp
);
335 return *ppix
!= NULL
;
338 wxBitmapRefData::~wxBitmapRefData()
341 gdk_pixmap_unref( m_pixmap
);
343 gdk_bitmap_unref( m_bitmap
);
347 #endif // wxUSE_PALETTE
351 //-----------------------------------------------------------------------------
353 //-----------------------------------------------------------------------------
355 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
357 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
359 wxGDIRefData
*wxBitmap::CreateGDIRefData() const
361 return new wxBitmapRefData
;
364 wxGDIRefData
*wxBitmap::CloneGDIRefData(const wxGDIRefData
*data
) const
366 return new wxBitmapRefData(*static_cast<const wxBitmapRefData
*>(data
));
369 bool wxBitmap::Create( int width
, int height
, int depth
)
373 if ( width
<= 0 || height
<= 0 )
378 m_refData
= new wxBitmapRefData();
379 return M_BMPDATA
->Create(width
, height
, depth
);
382 wxBitmap::wxBitmap(const char* const* bits
)
384 wxCHECK2_MSG(bits
!= NULL
, return, wxT("invalid bitmap data"));
386 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
388 m_refData
= new wxBitmapRefData();
390 GdkBitmap
*mask
= NULL
;
392 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm_d( wxGetRootWindow()->window
, &mask
, NULL
, (gchar
**) bits
);
394 wxCHECK2_MSG(M_BMPDATA
->m_pixmap
, return, wxT("couldn't create pixmap"));
398 M_BMPDATA
->m_mask
= new wxMask();
399 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
402 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
404 M_BMPDATA
->m_bpp
= visual
->depth
; // Can we get a different depth from create_from_xpm_d() ?
407 wxBitmap
wxBitmap::Rescale( int clipx
, int clipy
, int clipwidth
, int clipheight
, int newx
, int newy
)
409 wxCHECK_MSG( IsOk(), wxNullBitmap
, wxT("invalid bitmap") );
411 if (newy
==M_BMPDATA
->m_width
&& newy
==M_BMPDATA
->m_height
)
414 int width
= wxMax(newx
, 1);
415 int height
= wxMax(newy
, 1);
416 width
= wxMin(width
, clipwidth
);
417 height
= wxMin(height
, clipheight
);
421 GdkImage
*img
= NULL
;
423 img
= gdk_image_get( GetPixmap(), 0, 0, GetWidth(), GetHeight() );
424 else if (GetBitmap())
425 img
= gdk_image_get( GetBitmap(), 0, 0, GetWidth(), GetHeight() );
427 wxFAIL_MSG( wxT("Ill-formed bitmap") );
429 wxCHECK_MSG( img
, wxNullBitmap
, wxT("couldn't create image") );
435 GdkPixmap
*dstpix
= NULL
;
438 GdkVisual
*visual
= gdk_window_get_visual( GetPixmap() );
440 visual
= wxTheApp
->GetGdkVisual();
443 bmp
= wxBitmap(width
,height
,bpp
);
444 dstpix
= bmp
.GetPixmap();
445 gc
= gdk_gc_new( dstpix
);
449 long dstbyteperline
= 0;
454 dstbyteperline
= width
/8*M_BMPDATA
->m_bpp
;
455 if (width
*M_BMPDATA
->m_bpp
% 8 != 0)
457 dst
= (char*) malloc(dstbyteperline
*height
);
460 // be careful to use the right scaling factor
461 float scx
= (float)M_BMPDATA
->m_width
/(float)newx
;
462 float scy
= (float)M_BMPDATA
->m_height
/(float)newy
;
463 // prepare accel-tables
464 int *tablex
= (int *)calloc(width
,sizeof(int));
465 int *tabley
= (int *)calloc(height
,sizeof(int));
467 // accel table filled with clipped values
468 for (int x
= 0; x
< width
; x
++)
469 tablex
[x
] = (int) (scx
* (x
+clipx
));
470 for (int y
= 0; y
< height
; y
++)
471 tabley
[y
] = (int) (scy
* (y
+clipy
));
473 // Main rescaling routine starts here
474 for (int h
= 0; h
< height
; h
++)
478 guint32 old_pixval
= 0;
480 for (int w
= 0; w
< width
; w
++)
488 pixval
= gdk_image_get_pixel( img
, x
, tabley
[h
] );
498 char shift
= bit
<< (w
% 8);
504 dst
[h
*dstbyteperline
+w
/8] = outbyte
;
512 gdk_gc_set_foreground( gc
, &col
);
513 gdk_draw_point( dstpix
, gc
, w
, h
);
517 // do not forget the last byte
518 if ((bpp
== 1) && (width
% 8 != 0))
519 dst
[h
*dstbyteperline
+width
/8] = outbyte
;
522 gdk_image_destroy( img
);
523 if (gc
) gdk_gc_unref( gc
);
527 bmp
= wxBitmap( (const char *)dst
, width
, height
, 1 );
533 dstbyteperline
= width
/8;
536 dst
= (char*) malloc(dstbyteperline
*height
);
537 img
= gdk_image_get( GetMask()->GetBitmap(), 0, 0, GetWidth(), GetHeight() );
539 for (int h
= 0; h
< height
; h
++)
543 guint32 old_pixval
= 0;
545 for (int w
= 0; w
< width
; w
++)
553 pixval
= gdk_image_get_pixel( img
, x
, tabley
[h
] );
561 char shift
= bit
<< (w
% 8);
567 dst
[h
*dstbyteperline
+w
/8] = outbyte
;
572 // do not forget the last byte
574 dst
[h
*dstbyteperline
+width
/8] = outbyte
;
576 wxMask
* mask
= new wxMask
;
577 mask
->m_bitmap
= gdk_bitmap_create_from_data( wxGetRootWindow()->window
, (gchar
*) dst
, width
, height
);
581 gdk_image_destroy( img
);
590 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
594 wxCHECK_MSG( image
.IsOk(), false, wxT("invalid image") );
595 wxCHECK_MSG( depth
== -1 || depth
== 1, false, wxT("invalid bitmap depth") );
597 if (image
.GetWidth() <= 0 || image
.GetHeight() <= 0)
600 m_refData
= new wxBitmapRefData();
604 return CreateFromImageAsBitmap(image
);
608 return CreateFromImageAsPixmap(image
);
612 // conversion to mono bitmap:
613 bool wxBitmap::CreateFromImageAsBitmap(const wxImage
& img
)
615 // convert alpha channel to mask, if it is present:
617 image
.ConvertAlphaToMask();
619 int width
= image
.GetWidth();
620 int height
= image
.GetHeight();
625 SetBitmap( gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 ) );
629 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
631 // Create picture image
633 unsigned char *data_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
635 GdkImage
*data_image
=
636 gdk_image_new_bitmap( visual
, data_data
, width
, height
);
640 GdkImage
*mask_image
= NULL
;
644 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
646 mask_image
= gdk_image_new_bitmap( visual
, mask_data
, width
, height
);
648 wxMask
*mask
= new wxMask();
649 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
654 int r_mask
= image
.GetMaskRed();
655 int g_mask
= image
.GetMaskGreen();
656 int b_mask
= image
.GetMaskBlue();
658 unsigned char* data
= image
.GetData();
661 for (int y
= 0; y
< height
; y
++)
663 for (int x
= 0; x
< width
; x
++)
674 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
675 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
677 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
680 if ((r
== 255) && (b
== 255) && (g
== 255))
681 gdk_image_put_pixel( data_image
, x
, y
, 1 );
683 gdk_image_put_pixel( data_image
, x
, y
, 0 );
690 GdkGC
*data_gc
= gdk_gc_new( GetBitmap() );
692 gdk_draw_image( GetBitmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
694 gdk_image_destroy( data_image
);
695 gdk_gc_unref( data_gc
);
701 GdkGC
*mask_gc
= gdk_gc_new( GetMask()->GetBitmap() );
703 gdk_draw_image( GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
705 gdk_image_destroy( mask_image
);
706 gdk_gc_unref( mask_gc
);
712 // conversion to colour bitmap:
713 bool wxBitmap::CreateFromImageAsPixmap(const wxImage
& img
)
715 // convert alpha channel to mask, if it is present:
717 image
.ConvertAlphaToMask();
719 int width
= image
.GetWidth();
720 int height
= image
.GetHeight();
725 SetPixmap( gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, -1 ) );
727 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
729 int bpp
= visual
->depth
;
733 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800))
738 // We handle 8-bit bitmaps ourselves using the colour cube, 12-bit
739 // visuals are not supported by GDK so we do these ourselves, too.
740 // 15-bit and 16-bit should actually work and 24-bit certainly does.
742 if (!image
.HasMask() && (bpp
> 16))
744 if (!image
.HasMask() && (bpp
> 12))
747 static bool s_hasInitialized
= false;
749 if (!s_hasInitialized
)
752 s_hasInitialized
= true;
755 GdkGC
*gc
= gdk_gc_new( GetPixmap() );
757 gdk_draw_rgb_image( GetPixmap(),
769 // Create picture image
771 GdkImage
*data_image
=
772 gdk_image_new( GDK_IMAGE_FASTEST
, visual
, width
, height
);
776 GdkImage
*mask_image
= NULL
;
780 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
782 mask_image
= gdk_image_new_bitmap( visual
, mask_data
, width
, height
);
784 wxMask
*mask
= new wxMask();
785 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
792 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
793 byte_order b_o
= RGB
;
797 if ((visual
->red_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->blue_mask
)) b_o
= RGB
;
798 else if ((visual
->red_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->green_mask
)) b_o
= RBG
;
799 else if ((visual
->blue_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->green_mask
)) b_o
= BRG
;
800 else if ((visual
->blue_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->red_mask
)) b_o
= BGR
;
801 else if ((visual
->green_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->blue_mask
)) b_o
= GRB
;
802 else if ((visual
->green_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->red_mask
)) b_o
= GBR
;
805 int r_mask
= image
.GetMaskRed();
806 int g_mask
= image
.GetMaskGreen();
807 int b_mask
= image
.GetMaskBlue();
809 unsigned char* data
= image
.GetData();
812 for (int y
= 0; y
< height
; y
++)
814 for (int x
= 0; x
< width
; x
++)
825 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
826 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
828 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
836 if (wxTheApp
->m_colorCube
)
838 pixel
= wxTheApp
->m_colorCube
[ ((r
& 0xf8) << 7) + ((g
& 0xf8) << 2) + ((b
& 0xf8) >> 3) ];
842 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
843 GdkColor
*colors
= cmap
->colors
;
844 int max
= 3 * (65536);
846 for (int i
= 0; i
< cmap
->size
; i
++)
848 int rdiff
= (r
<< 8) - colors
[i
].red
;
849 int gdiff
= (g
<< 8) - colors
[i
].green
;
850 int bdiff
= (b
<< 8) - colors
[i
].blue
;
851 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
852 if (sum
< max
) { pixel
= i
; max
= sum
; }
856 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
865 case RGB
: pixel
= ((r
& 0xf0) << 4) | (g
& 0xf0) | ((b
& 0xf0) >> 4); break;
866 case RBG
: pixel
= ((r
& 0xf0) << 4) | (b
& 0xf0) | ((g
& 0xf0) >> 4); break;
867 case GRB
: pixel
= ((g
& 0xf0) << 4) | (r
& 0xf0) | ((b
& 0xf0) >> 4); break;
868 case GBR
: pixel
= ((g
& 0xf0) << 4) | (b
& 0xf0) | ((r
& 0xf0) >> 4); break;
869 case BRG
: pixel
= ((b
& 0xf0) << 4) | (r
& 0xf0) | ((g
& 0xf0) >> 4); break;
870 case BGR
: pixel
= ((b
& 0xf0) << 4) | (g
& 0xf0) | ((r
& 0xf0) >> 4); break;
872 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
880 case RGB
: pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
881 case RBG
: pixel
= ((r
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
882 case GRB
: pixel
= ((g
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
883 case GBR
: pixel
= ((g
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
884 case BRG
: pixel
= ((b
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
885 case BGR
: pixel
= ((b
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
887 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
892 // I actually don't know if for 16-bit displays, it is alway the green
893 // component or the second component which has 6 bits.
897 case RGB
: pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
898 case RBG
: pixel
= ((r
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
899 case GRB
: pixel
= ((g
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
900 case GBR
: pixel
= ((g
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
901 case BRG
: pixel
= ((b
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
902 case BGR
: pixel
= ((b
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
904 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
913 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
914 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
915 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
916 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
917 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
918 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
920 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
930 GdkGC
*data_gc
= gdk_gc_new( GetPixmap() );
932 gdk_draw_image( GetPixmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
934 gdk_image_destroy( data_image
);
935 gdk_gc_unref( data_gc
);
941 GdkGC
*mask_gc
= gdk_gc_new( GetMask()->GetBitmap() );
943 gdk_draw_image( GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
945 gdk_image_destroy( mask_image
);
946 gdk_gc_unref( mask_gc
);
952 wxImage
wxBitmap::ConvertToImage() const
956 wxCHECK_MSG( IsOk(), wxNullImage
, wxT("invalid bitmap") );
958 image
.Create(GetWidth(), GetHeight());
959 unsigned char *data
= image
.GetData();
963 wxFAIL_MSG( wxT("couldn't create image") );
967 // the colour used as transparent one in wxImage and the one it is
968 // replaced with when it really occurs in the bitmap
969 static const int MASK_RED
= 1;
970 static const int MASK_GREEN
= 2;
971 static const int MASK_BLUE
= 3;
972 static const int MASK_BLUE_REPLACEMENT
= 2;
974 GdkImage
*gdk_image
= NULL
;
978 gdk_image
= gdk_image_get( GetPixmap(),
980 GetWidth(), GetHeight() );
982 else if (GetBitmap())
984 gdk_image
= gdk_image_get( GetBitmap(),
986 GetWidth(), GetHeight() );
990 wxFAIL_MSG( wxT("Ill-formed bitmap") );
993 wxCHECK_MSG( gdk_image
, wxNullImage
, wxT("couldn't create image") );
995 GdkImage
*gdk_image_mask
= NULL
;
998 gdk_image_mask
= gdk_image_get( GetMask()->GetBitmap(),
1000 GetWidth(), GetHeight() );
1002 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
1006 int red_shift_right
= 0;
1007 int green_shift_right
= 0;
1008 int blue_shift_right
= 0;
1009 int red_shift_left
= 0;
1010 int green_shift_left
= 0;
1011 int blue_shift_left
= 0;
1012 bool use_shift
= false;
1016 GdkVisual
*visual
= gdk_window_get_visual( GetPixmap() );
1018 visual
= wxTheApp
->GetGdkVisual();
1020 bpp
= visual
->depth
;
1022 bpp
= visual
->red_prec
+ visual
->green_prec
+ visual
->blue_prec
;
1023 red_shift_right
= visual
->red_shift
;
1024 red_shift_left
= 8-visual
->red_prec
;
1025 green_shift_right
= visual
->green_shift
;
1026 green_shift_left
= 8-visual
->green_prec
;
1027 blue_shift_right
= visual
->blue_shift
;
1028 blue_shift_left
= 8-visual
->blue_prec
;
1030 use_shift
= (visual
->type
== GDK_VISUAL_TRUE_COLOR
) || (visual
->type
== GDK_VISUAL_DIRECT_COLOR
);
1038 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
1041 for (int j
= 0; j
< GetHeight(); j
++)
1043 for (int i
= 0; i
< GetWidth(); i
++)
1045 wxUint32 pixel
= gdk_image_get_pixel( gdk_image
, i
, j
);
1063 data
[pos
] = (pixel
>> red_shift_right
) << red_shift_left
;
1064 data
[pos
+1] = (pixel
>> green_shift_right
) << green_shift_left
;
1065 data
[pos
+2] = (pixel
>> blue_shift_right
) << blue_shift_left
;
1067 else if (cmap
->colors
)
1069 data
[pos
] = cmap
->colors
[pixel
].red
>> 8;
1070 data
[pos
+1] = cmap
->colors
[pixel
].green
>> 8;
1071 data
[pos
+2] = cmap
->colors
[pixel
].blue
>> 8;
1075 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
1080 int mask_pixel
= gdk_image_get_pixel( gdk_image_mask
, i
, j
);
1081 if (mask_pixel
== 0)
1083 data
[pos
] = MASK_RED
;
1084 data
[pos
+1] = MASK_GREEN
;
1085 data
[pos
+2] = MASK_BLUE
;
1087 else if ( data
[pos
] == MASK_RED
&&
1088 data
[pos
+1] == MASK_GREEN
&&
1089 data
[pos
+2] == MASK_BLUE
)
1091 data
[pos
+2] = MASK_BLUE_REPLACEMENT
;
1099 gdk_image_destroy( gdk_image
);
1100 if (gdk_image_mask
) gdk_image_destroy( gdk_image_mask
);
1105 wxBitmap::wxBitmap( const wxString
&filename
, wxBitmapType type
)
1107 LoadFile( filename
, type
);
1110 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int WXUNUSED(depth
))
1112 if ( width
> 0 && height
> 0 )
1114 m_refData
= new wxBitmapRefData();
1116 M_BMPDATA
->m_mask
= NULL
;
1117 M_BMPDATA
->m_bitmap
= gdk_bitmap_create_from_data
1119 wxGetRootWindow()->window
,
1124 M_BMPDATA
->m_width
= width
;
1125 M_BMPDATA
->m_height
= height
;
1126 M_BMPDATA
->m_bpp
= 1;
1128 wxASSERT_MSG( M_BMPDATA
->m_bitmap
, wxT("couldn't create bitmap") );
1132 wxBitmap::~wxBitmap()
1136 int wxBitmap::GetHeight() const
1138 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
1140 return M_BMPDATA
->m_height
;
1143 int wxBitmap::GetWidth() const
1145 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
1147 return M_BMPDATA
->m_width
;
1150 int wxBitmap::GetDepth() const
1152 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
1154 return M_BMPDATA
->m_bpp
;
1157 wxMask
*wxBitmap::GetMask() const
1159 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") );
1161 return M_BMPDATA
->m_mask
;
1164 void wxBitmap::SetMask( wxMask
*mask
)
1166 wxCHECK_RET( IsOk(), wxT("invalid bitmap") );
1169 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
1171 M_BMPDATA
->m_mask
= mask
;
1174 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
1180 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
1182 wxCHECK_MSG( IsOk() &&
1183 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1184 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) && (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
1185 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
1187 wxBitmap
ret( rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
1188 wxASSERT_MSG( ret
.IsOk(), wxT("GetSubBitmap error") );
1190 if (ret
.GetPixmap())
1192 GdkGC
*gc
= gdk_gc_new( ret
.GetPixmap() );
1193 gdk_draw_pixmap( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1194 gdk_gc_destroy( gc
);
1198 GdkGC
*gc
= gdk_gc_new( ret
.GetBitmap() );
1200 col
.pixel
= 0xFFFFFF;
1201 gdk_gc_set_foreground( gc
, &col
);
1203 gdk_gc_set_background( gc
, &col
);
1204 gdk_wx_draw_bitmap( ret
.GetBitmap(), gc
, GetBitmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1205 gdk_gc_destroy( gc
);
1210 wxMask
*mask
= new wxMask
;
1211 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, rect
.width
, rect
.height
, 1 );
1213 GdkGC
*gc
= gdk_gc_new( mask
->m_bitmap
);
1215 col
.pixel
= 0xFFFFFF;
1216 gdk_gc_set_foreground( gc
, &col
);
1218 gdk_gc_set_background( gc
, &col
);
1219 gdk_wx_draw_bitmap( mask
->m_bitmap
, gc
, M_BMPDATA
->m_mask
->m_bitmap
, rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1220 gdk_gc_destroy( gc
);
1222 ret
.SetMask( mask
);
1228 bool wxBitmap::SaveFile( const wxString
&name
, wxBitmapType type
, const wxPalette
*WXUNUSED(palette
) ) const
1230 wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") );
1232 // Try to save the bitmap via wxImage handlers:
1234 wxImage image
= ConvertToImage();
1235 if (image
.IsOk()) return image
.SaveFile( name
, type
);
1241 bool wxBitmap::LoadFile( const wxString
&name
, wxBitmapType type
)
1245 if (!wxFileExists(name
))
1248 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
1250 if (type
== wxBITMAP_TYPE_XPM
)
1252 m_refData
= new wxBitmapRefData();
1254 GdkBitmap
*mask
= NULL
;
1256 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm
1258 wxGetRootWindow()->window
,
1266 M_BMPDATA
->m_mask
= new wxMask();
1267 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
1270 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
1272 M_BMPDATA
->m_bpp
= visual
->depth
;
1274 else // try if wxImage can load it
1277 if ( !image
.LoadFile( name
, type
) || !image
.IsOk() )
1280 *this = wxBitmap(image
);
1287 wxPalette
*wxBitmap::GetPalette() const
1292 return M_BMPDATA
->m_palette
;
1295 void wxBitmap::SetPalette(const wxPalette
& WXUNUSED(palette
))
1299 #endif // wxUSE_PALETTE
1301 void wxBitmap::SetHeight( int height
)
1304 M_BMPDATA
->m_height
= height
;
1307 void wxBitmap::SetWidth( int width
)
1310 M_BMPDATA
->m_width
= width
;
1313 void wxBitmap::SetDepth( int depth
)
1316 M_BMPDATA
->m_bpp
= depth
;
1319 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
1322 m_refData
= new wxBitmapRefData();
1324 M_BMPDATA
->m_pixmap
= pixmap
;
1327 void wxBitmap::SetBitmap( GdkPixmap
*bitmap
)
1330 m_refData
= new wxBitmapRefData();
1332 M_BMPDATA
->m_bitmap
= bitmap
;
1335 GdkPixmap
*wxBitmap::GetPixmap() const
1337 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") );
1339 return M_BMPDATA
->m_pixmap
;
1342 bool wxBitmap::HasPixmap() const
1344 wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") );
1346 return M_BMPDATA
->m_pixmap
!= NULL
;
1349 GdkBitmap
*wxBitmap::GetBitmap() const
1351 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") );
1353 return M_BMPDATA
->m_bitmap
;
1356 void *wxBitmap::GetRawData(wxPixelDataBase
& WXUNUSED(data
), int WXUNUSED(bpp
))
1361 void wxBitmap::UngetRawData(wxPixelDataBase
& WXUNUSED(data
))
1365 bool wxBitmap::HasAlpha() const
1370 /* static */ void wxBitmap::InitStandardHandlers()
1372 // TODO: Insert handler based on GdkPixbufs handler later