1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/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/dcmemory.h"
18 #include "wx/palette.h"
24 #include "wx/filefn.h"
30 #include <gdk/gdkrgb.h>
33 void gdk_wx_draw_bitmap (GdkDrawable
*drawable
,
43 //-----------------------------------------------------------------------------
45 //-----------------------------------------------------------------------------
47 extern GtkWidget
*wxGetRootWindow();
49 //-----------------------------------------------------------------------------
51 //-----------------------------------------------------------------------------
53 IMPLEMENT_DYNAMIC_CLASS(wxMask
,wxObject
)
57 m_bitmap
= (GdkBitmap
*) NULL
;
60 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
62 m_bitmap
= (GdkBitmap
*) NULL
;
63 Create( bitmap
, colour
);
67 wxMask::wxMask( const wxBitmap
& bitmap
, int paletteIndex
)
69 m_bitmap
= (GdkBitmap
*) NULL
;
70 Create( bitmap
, paletteIndex
);
72 #endif // wxUSE_PALETTE
74 wxMask::wxMask( const wxBitmap
& bitmap
)
76 m_bitmap
= (GdkBitmap
*) NULL
;
83 gdk_bitmap_unref( m_bitmap
);
86 bool wxMask::Create( const wxBitmap
& bitmap
,
87 const wxColour
& colour
)
91 gdk_bitmap_unref( m_bitmap
);
92 m_bitmap
= (GdkBitmap
*) NULL
;
95 wxImage image
= bitmap
.ConvertToImage();
96 if (!image
.Ok()) return false;
98 m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, image
.GetWidth(), image
.GetHeight(), 1 );
99 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
106 gdk_gc_set_foreground( gc
, &color
);
107 gdk_gc_set_fill( gc
, GDK_SOLID
);
108 gdk_draw_rectangle( m_bitmap
, gc
, TRUE
, 0, 0, image
.GetWidth(), image
.GetHeight() );
110 unsigned char *data
= image
.GetData();
113 unsigned char red
= colour
.Red();
114 unsigned char green
= colour
.Green();
115 unsigned char blue
= colour
.Blue();
117 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
119 int bpp
= visual
->depth
;
120 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800))
125 green
= green
& 0xf8;
131 green
= green
& 0xfc;
137 green
= green
& 0xf0;
145 gdk_gc_set_foreground( gc
, &color
);
147 for (int j
= 0; j
< image
.GetHeight(); j
++)
151 for (i
= 0; i
< image
.GetWidth(); i
++)
153 if ((data
[index
] == red
) &&
154 (data
[index
+1] == green
) &&
155 (data
[index
+2] == blue
))
164 gdk_draw_line( m_bitmap
, gc
, start_x
, j
, i
-1, j
);
171 gdk_draw_line( m_bitmap
, gc
, start_x
, j
, i
, j
);
180 bool wxMask::Create( const wxBitmap
& bitmap
, int paletteIndex
)
183 wxPalette
*pal
= bitmap
.GetPalette();
185 wxCHECK_MSG( pal
, false, wxT("Cannot create mask from bitmap without palette") );
187 pal
->GetRGB(paletteIndex
, &r
, &g
, &b
);
189 return Create(bitmap
, wxColour(r
, g
, b
));
191 #endif // wxUSE_PALETTE
193 bool wxMask::Create( const wxBitmap
& bitmap
)
197 gdk_bitmap_unref( m_bitmap
);
198 m_bitmap
= (GdkBitmap
*) NULL
;
201 if (!bitmap
.Ok()) return false;
203 wxCHECK_MSG( bitmap
.GetBitmap(), false, wxT("Cannot create mask from colour bitmap") );
205 m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, bitmap
.GetWidth(), bitmap
.GetHeight(), 1 );
207 if (!m_bitmap
) return false;
209 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
211 gdk_wx_draw_bitmap( m_bitmap
, gc
, bitmap
.GetBitmap(), 0, 0, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight() );
218 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()
250 m_pixmap
= (GdkPixmap
*) NULL
;
251 m_bitmap
= (GdkBitmap
*) NULL
;
252 m_mask
= (wxMask
*) NULL
;
257 m_palette
= (wxPalette
*) NULL
;
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 _T("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
350 //-----------------------------------------------------------------------------
352 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
354 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
360 wxBitmap::wxBitmap( int width
, int height
, int depth
)
362 Create( width
, height
, depth
);
365 wxGDIRefData
*wxBitmap::CreateGDIRefData() const
367 return new wxBitmapRefData
;
370 wxGDIRefData
*wxBitmap::CloneGDIRefData(const wxGDIRefData
*data
) const
372 return new wxBitmapRefData(*wx_static_cast(const wxBitmapRefData
*, data
));
375 bool wxBitmap::Create( int width
, int height
, int depth
)
379 if ( width
<= 0 || height
<= 0 )
384 m_refData
= new wxBitmapRefData();
385 return M_BMPDATA
->Create(width
, height
, depth
);
388 wxBitmap::wxBitmap(const char* const* bits
)
390 wxCHECK2_MSG(bits
!= NULL
, return, wxT("invalid bitmap data"));
392 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
394 m_refData
= new wxBitmapRefData();
396 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
398 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm_d( wxGetRootWindow()->window
, &mask
, NULL
, (gchar
**) bits
);
400 wxCHECK2_MSG(M_BMPDATA
->m_pixmap
, return, wxT("couldn't create pixmap"));
404 M_BMPDATA
->m_mask
= new wxMask();
405 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
408 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
410 M_BMPDATA
->m_bpp
= visual
->depth
; // Can we get a different depth from create_from_xpm_d() ?
413 wxBitmap
wxBitmap::Rescale( int clipx
, int clipy
, int clipwidth
, int clipheight
, int newx
, int newy
)
415 wxCHECK_MSG( Ok(), wxNullBitmap
, wxT("invalid bitmap") );
417 if (newy
==M_BMPDATA
->m_width
&& newy
==M_BMPDATA
->m_height
)
420 int width
= wxMax(newx
, 1);
421 int height
= wxMax(newy
, 1);
422 width
= wxMin(width
, clipwidth
);
423 height
= wxMin(height
, clipheight
);
427 GdkImage
*img
= (GdkImage
*) NULL
;
429 img
= gdk_image_get( GetPixmap(), 0, 0, GetWidth(), GetHeight() );
430 else if (GetBitmap())
431 img
= gdk_image_get( GetBitmap(), 0, 0, GetWidth(), GetHeight() );
433 wxFAIL_MSG( wxT("Ill-formed bitmap") );
435 wxCHECK_MSG( img
, wxNullBitmap
, wxT("couldn't create image") );
441 GdkPixmap
*dstpix
= NULL
;
444 GdkVisual
*visual
= gdk_window_get_visual( GetPixmap() );
446 visual
= wxTheApp
->GetGdkVisual();
449 bmp
= wxBitmap(width
,height
,bpp
);
450 dstpix
= bmp
.GetPixmap();
451 gc
= gdk_gc_new( dstpix
);
455 long dstbyteperline
= 0;
460 dstbyteperline
= width
/8*M_BMPDATA
->m_bpp
;
461 if (width
*M_BMPDATA
->m_bpp
% 8 != 0)
463 dst
= (char*) malloc(dstbyteperline
*height
);
466 // be careful to use the right scaling factor
467 float scx
= (float)M_BMPDATA
->m_width
/(float)newx
;
468 float scy
= (float)M_BMPDATA
->m_height
/(float)newy
;
469 // prepare accel-tables
470 int *tablex
= (int *)calloc(width
,sizeof(int));
471 int *tabley
= (int *)calloc(height
,sizeof(int));
473 // accel table filled with clipped values
474 for (int x
= 0; x
< width
; x
++)
475 tablex
[x
] = (int) (scx
* (x
+clipx
));
476 for (int y
= 0; y
< height
; y
++)
477 tabley
[y
] = (int) (scy
* (y
+clipy
));
479 // Main rescaling routine starts here
480 for (int h
= 0; h
< height
; h
++)
484 guint32 old_pixval
= 0;
486 for (int w
= 0; w
< width
; w
++)
494 pixval
= gdk_image_get_pixel( img
, x
, tabley
[h
] );
504 char shift
= bit
<< (w
% 8);
510 dst
[h
*dstbyteperline
+w
/8] = outbyte
;
518 gdk_gc_set_foreground( gc
, &col
);
519 gdk_draw_point( dstpix
, gc
, w
, h
);
523 // do not forget the last byte
524 if ((bpp
== 1) && (width
% 8 != 0))
525 dst
[h
*dstbyteperline
+width
/8] = outbyte
;
528 gdk_image_destroy( img
);
529 if (gc
) gdk_gc_unref( gc
);
533 bmp
= wxBitmap( (const char *)dst
, width
, height
, 1 );
539 dstbyteperline
= width
/8;
542 dst
= (char*) malloc(dstbyteperline
*height
);
543 img
= gdk_image_get( GetMask()->GetBitmap(), 0, 0, GetWidth(), GetHeight() );
545 for (int h
= 0; h
< height
; h
++)
549 guint32 old_pixval
= 0;
551 for (int w
= 0; w
< width
; w
++)
559 pixval
= gdk_image_get_pixel( img
, x
, tabley
[h
] );
567 char shift
= bit
<< (w
% 8);
573 dst
[h
*dstbyteperline
+w
/8] = outbyte
;
578 // do not forget the last byte
580 dst
[h
*dstbyteperline
+width
/8] = outbyte
;
582 wxMask
* mask
= new wxMask
;
583 mask
->m_bitmap
= gdk_bitmap_create_from_data( wxGetRootWindow()->window
, (gchar
*) dst
, width
, height
);
587 gdk_image_destroy( img
);
596 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
600 wxCHECK_MSG( image
.Ok(), false, wxT("invalid image") );
601 wxCHECK_MSG( depth
== -1 || depth
== 1, false, wxT("invalid bitmap depth") );
603 if (image
.GetWidth() <= 0 || image
.GetHeight() <= 0)
606 m_refData
= new wxBitmapRefData();
610 return CreateFromImageAsBitmap(image
);
614 return CreateFromImageAsPixmap(image
);
618 // conversion to mono bitmap:
619 bool wxBitmap::CreateFromImageAsBitmap(const wxImage
& img
)
621 // convert alpha channel to mask, if it is present:
623 image
.ConvertAlphaToMask();
625 int width
= image
.GetWidth();
626 int height
= image
.GetHeight();
631 SetBitmap( gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 ) );
635 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
637 // Create picture image
639 unsigned char *data_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
641 GdkImage
*data_image
=
642 gdk_image_new_bitmap( visual
, data_data
, width
, height
);
646 GdkImage
*mask_image
= (GdkImage
*) NULL
;
650 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
652 mask_image
= gdk_image_new_bitmap( visual
, mask_data
, width
, height
);
654 wxMask
*mask
= new wxMask();
655 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
660 int r_mask
= image
.GetMaskRed();
661 int g_mask
= image
.GetMaskGreen();
662 int b_mask
= image
.GetMaskBlue();
664 unsigned char* data
= image
.GetData();
667 for (int y
= 0; y
< height
; y
++)
669 for (int x
= 0; x
< width
; x
++)
680 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
681 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
683 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
686 if ((r
== 255) && (b
== 255) && (g
== 255))
687 gdk_image_put_pixel( data_image
, x
, y
, 1 );
689 gdk_image_put_pixel( data_image
, x
, y
, 0 );
696 GdkGC
*data_gc
= gdk_gc_new( GetBitmap() );
698 gdk_draw_image( GetBitmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
700 gdk_image_destroy( data_image
);
701 gdk_gc_unref( data_gc
);
707 GdkGC
*mask_gc
= gdk_gc_new( GetMask()->GetBitmap() );
709 gdk_draw_image( GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
711 gdk_image_destroy( mask_image
);
712 gdk_gc_unref( mask_gc
);
718 // conversion to colour bitmap:
719 bool wxBitmap::CreateFromImageAsPixmap(const wxImage
& img
)
721 // convert alpha channel to mask, if it is present:
723 image
.ConvertAlphaToMask();
725 int width
= image
.GetWidth();
726 int height
= image
.GetHeight();
731 SetPixmap( gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, -1 ) );
733 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
735 int bpp
= visual
->depth
;
739 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800))
744 // We handle 8-bit bitmaps ourselves using the colour cube, 12-bit
745 // visuals are not supported by GDK so we do these ourselves, too.
746 // 15-bit and 16-bit should actually work and 24-bit certainly does.
748 if (!image
.HasMask() && (bpp
> 16))
750 if (!image
.HasMask() && (bpp
> 12))
753 static bool s_hasInitialized
= false;
755 if (!s_hasInitialized
)
758 s_hasInitialized
= true;
761 GdkGC
*gc
= gdk_gc_new( GetPixmap() );
763 gdk_draw_rgb_image( GetPixmap(),
775 // Create picture image
777 GdkImage
*data_image
=
778 gdk_image_new( GDK_IMAGE_FASTEST
, visual
, width
, height
);
782 GdkImage
*mask_image
= (GdkImage
*) NULL
;
786 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
788 mask_image
= gdk_image_new_bitmap( visual
, mask_data
, width
, height
);
790 wxMask
*mask
= new wxMask();
791 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
798 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
799 byte_order b_o
= RGB
;
803 if ((visual
->red_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->blue_mask
)) b_o
= RGB
;
804 else if ((visual
->red_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->green_mask
)) b_o
= RBG
;
805 else if ((visual
->blue_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->green_mask
)) b_o
= BRG
;
806 else if ((visual
->blue_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->red_mask
)) b_o
= BGR
;
807 else if ((visual
->green_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->blue_mask
)) b_o
= GRB
;
808 else if ((visual
->green_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->red_mask
)) b_o
= GBR
;
811 int r_mask
= image
.GetMaskRed();
812 int g_mask
= image
.GetMaskGreen();
813 int b_mask
= image
.GetMaskBlue();
815 unsigned char* data
= image
.GetData();
818 for (int y
= 0; y
< height
; y
++)
820 for (int x
= 0; x
< width
; x
++)
831 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
832 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
834 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
842 if (wxTheApp
->m_colorCube
)
844 pixel
= wxTheApp
->m_colorCube
[ ((r
& 0xf8) << 7) + ((g
& 0xf8) << 2) + ((b
& 0xf8) >> 3) ];
848 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
849 GdkColor
*colors
= cmap
->colors
;
850 int max
= 3 * (65536);
852 for (int i
= 0; i
< cmap
->size
; i
++)
854 int rdiff
= (r
<< 8) - colors
[i
].red
;
855 int gdiff
= (g
<< 8) - colors
[i
].green
;
856 int bdiff
= (b
<< 8) - colors
[i
].blue
;
857 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
858 if (sum
< max
) { pixel
= i
; max
= sum
; }
862 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
871 case RGB
: pixel
= ((r
& 0xf0) << 4) | (g
& 0xf0) | ((b
& 0xf0) >> 4); break;
872 case RBG
: pixel
= ((r
& 0xf0) << 4) | (b
& 0xf0) | ((g
& 0xf0) >> 4); break;
873 case GRB
: pixel
= ((g
& 0xf0) << 4) | (r
& 0xf0) | ((b
& 0xf0) >> 4); break;
874 case GBR
: pixel
= ((g
& 0xf0) << 4) | (b
& 0xf0) | ((r
& 0xf0) >> 4); break;
875 case BRG
: pixel
= ((b
& 0xf0) << 4) | (r
& 0xf0) | ((g
& 0xf0) >> 4); break;
876 case BGR
: pixel
= ((b
& 0xf0) << 4) | (g
& 0xf0) | ((r
& 0xf0) >> 4); break;
878 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
886 case RGB
: pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
887 case RBG
: pixel
= ((r
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
888 case GRB
: pixel
= ((g
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
889 case GBR
: pixel
= ((g
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
890 case BRG
: pixel
= ((b
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
891 case BGR
: pixel
= ((b
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
893 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
898 // I actually don't know if for 16-bit displays, it is alway the green
899 // component or the second component which has 6 bits.
903 case RGB
: pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
904 case RBG
: pixel
= ((r
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
905 case GRB
: pixel
= ((g
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
906 case GBR
: pixel
= ((g
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
907 case BRG
: pixel
= ((b
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
908 case BGR
: pixel
= ((b
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
910 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
919 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
920 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
921 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
922 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
923 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
924 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
926 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
936 GdkGC
*data_gc
= gdk_gc_new( GetPixmap() );
938 gdk_draw_image( GetPixmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
940 gdk_image_destroy( data_image
);
941 gdk_gc_unref( data_gc
);
947 GdkGC
*mask_gc
= gdk_gc_new( GetMask()->GetBitmap() );
949 gdk_draw_image( GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
951 gdk_image_destroy( mask_image
);
952 gdk_gc_unref( mask_gc
);
958 wxImage
wxBitmap::ConvertToImage() const
962 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
964 image
.Create(GetWidth(), GetHeight());
965 unsigned char *data
= image
.GetData();
969 wxFAIL_MSG( wxT("couldn't create image") );
973 // the colour used as transparent one in wxImage and the one it is
974 // replaced with when it really occurs in the bitmap
975 static const int MASK_RED
= 1;
976 static const int MASK_GREEN
= 2;
977 static const int MASK_BLUE
= 3;
978 static const int MASK_BLUE_REPLACEMENT
= 2;
980 GdkImage
*gdk_image
= (GdkImage
*) NULL
;
984 gdk_image
= gdk_image_get( GetPixmap(),
986 GetWidth(), GetHeight() );
988 else if (GetBitmap())
990 gdk_image
= gdk_image_get( GetBitmap(),
992 GetWidth(), GetHeight() );
996 wxFAIL_MSG( wxT("Ill-formed bitmap") );
999 wxCHECK_MSG( gdk_image
, wxNullImage
, wxT("couldn't create image") );
1001 GdkImage
*gdk_image_mask
= (GdkImage
*) NULL
;
1004 gdk_image_mask
= gdk_image_get( GetMask()->GetBitmap(),
1006 GetWidth(), GetHeight() );
1008 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
1012 int red_shift_right
= 0;
1013 int green_shift_right
= 0;
1014 int blue_shift_right
= 0;
1015 int red_shift_left
= 0;
1016 int green_shift_left
= 0;
1017 int blue_shift_left
= 0;
1018 bool use_shift
= false;
1022 GdkVisual
*visual
= gdk_window_get_visual( GetPixmap() );
1024 visual
= wxTheApp
->GetGdkVisual();
1026 bpp
= visual
->depth
;
1028 bpp
= visual
->red_prec
+ visual
->green_prec
+ visual
->blue_prec
;
1029 red_shift_right
= visual
->red_shift
;
1030 red_shift_left
= 8-visual
->red_prec
;
1031 green_shift_right
= visual
->green_shift
;
1032 green_shift_left
= 8-visual
->green_prec
;
1033 blue_shift_right
= visual
->blue_shift
;
1034 blue_shift_left
= 8-visual
->blue_prec
;
1036 use_shift
= (visual
->type
== GDK_VISUAL_TRUE_COLOR
) || (visual
->type
== GDK_VISUAL_DIRECT_COLOR
);
1044 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
1047 for (int j
= 0; j
< GetHeight(); j
++)
1049 for (int i
= 0; i
< GetWidth(); i
++)
1051 wxUint32 pixel
= gdk_image_get_pixel( gdk_image
, i
, j
);
1069 data
[pos
] = (pixel
>> red_shift_right
) << red_shift_left
;
1070 data
[pos
+1] = (pixel
>> green_shift_right
) << green_shift_left
;
1071 data
[pos
+2] = (pixel
>> blue_shift_right
) << blue_shift_left
;
1073 else if (cmap
->colors
)
1075 data
[pos
] = cmap
->colors
[pixel
].red
>> 8;
1076 data
[pos
+1] = cmap
->colors
[pixel
].green
>> 8;
1077 data
[pos
+2] = cmap
->colors
[pixel
].blue
>> 8;
1081 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
1086 int mask_pixel
= gdk_image_get_pixel( gdk_image_mask
, i
, j
);
1087 if (mask_pixel
== 0)
1089 data
[pos
] = MASK_RED
;
1090 data
[pos
+1] = MASK_GREEN
;
1091 data
[pos
+2] = MASK_BLUE
;
1093 else if ( data
[pos
] == MASK_RED
&&
1094 data
[pos
+1] == MASK_GREEN
&&
1095 data
[pos
+2] == MASK_BLUE
)
1097 data
[pos
+2] = MASK_BLUE_REPLACEMENT
;
1105 gdk_image_destroy( gdk_image
);
1106 if (gdk_image_mask
) gdk_image_destroy( gdk_image_mask
);
1111 wxBitmap::wxBitmap( const wxString
&filename
, wxBitmapType type
)
1113 LoadFile( filename
, type
);
1116 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int WXUNUSED(depth
))
1118 if ( width
> 0 && height
> 0 )
1120 m_refData
= new wxBitmapRefData();
1122 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
1123 M_BMPDATA
->m_bitmap
= gdk_bitmap_create_from_data
1125 wxGetRootWindow()->window
,
1130 M_BMPDATA
->m_width
= width
;
1131 M_BMPDATA
->m_height
= height
;
1132 M_BMPDATA
->m_bpp
= 1;
1134 wxASSERT_MSG( M_BMPDATA
->m_bitmap
, wxT("couldn't create bitmap") );
1138 wxBitmap::~wxBitmap()
1142 int wxBitmap::GetHeight() const
1144 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1146 return M_BMPDATA
->m_height
;
1149 int wxBitmap::GetWidth() const
1151 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1153 return M_BMPDATA
->m_width
;
1156 int wxBitmap::GetDepth() const
1158 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1160 return M_BMPDATA
->m_bpp
;
1163 wxMask
*wxBitmap::GetMask() const
1165 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
1167 return M_BMPDATA
->m_mask
;
1170 void wxBitmap::SetMask( wxMask
*mask
)
1172 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
1175 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
1177 M_BMPDATA
->m_mask
= mask
;
1180 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
1186 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
1188 wxCHECK_MSG( Ok() &&
1189 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1190 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) && (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
1191 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
1193 wxBitmap
ret( rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
1194 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
1196 if (ret
.GetPixmap())
1198 GdkGC
*gc
= gdk_gc_new( ret
.GetPixmap() );
1199 gdk_draw_pixmap( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1200 gdk_gc_destroy( gc
);
1204 GdkGC
*gc
= gdk_gc_new( ret
.GetBitmap() );
1206 col
.pixel
= 0xFFFFFF;
1207 gdk_gc_set_foreground( gc
, &col
);
1209 gdk_gc_set_background( gc
, &col
);
1210 gdk_wx_draw_bitmap( ret
.GetBitmap(), gc
, GetBitmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1211 gdk_gc_destroy( gc
);
1216 wxMask
*mask
= new wxMask
;
1217 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, rect
.width
, rect
.height
, 1 );
1219 GdkGC
*gc
= gdk_gc_new( mask
->m_bitmap
);
1221 col
.pixel
= 0xFFFFFF;
1222 gdk_gc_set_foreground( gc
, &col
);
1224 gdk_gc_set_background( gc
, &col
);
1225 gdk_wx_draw_bitmap( mask
->m_bitmap
, gc
, M_BMPDATA
->m_mask
->m_bitmap
, rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1226 gdk_gc_destroy( gc
);
1228 ret
.SetMask( mask
);
1234 bool wxBitmap::SaveFile( const wxString
&name
, wxBitmapType type
, const wxPalette
*WXUNUSED(palette
) ) const
1236 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1238 // Try to save the bitmap via wxImage handlers:
1240 wxImage image
= ConvertToImage();
1241 if (image
.Ok()) return image
.SaveFile( name
, type
);
1247 bool wxBitmap::LoadFile( const wxString
&name
, wxBitmapType type
)
1251 if (!wxFileExists(name
))
1254 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
1256 if (type
== wxBITMAP_TYPE_XPM
)
1258 m_refData
= new wxBitmapRefData();
1260 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
1262 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm
1264 wxGetRootWindow()->window
,
1272 M_BMPDATA
->m_mask
= new wxMask();
1273 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
1276 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
1278 M_BMPDATA
->m_bpp
= visual
->depth
;
1280 else // try if wxImage can load it
1283 if ( !image
.LoadFile( name
, type
) || !image
.Ok() )
1286 *this = wxBitmap(image
);
1293 wxPalette
*wxBitmap::GetPalette() const
1296 return (wxPalette
*) NULL
;
1298 return M_BMPDATA
->m_palette
;
1301 void wxBitmap::SetPalette(const wxPalette
& WXUNUSED(palette
))
1305 #endif // wxUSE_PALETTE
1307 void wxBitmap::SetHeight( int height
)
1310 M_BMPDATA
->m_height
= height
;
1313 void wxBitmap::SetWidth( int width
)
1316 M_BMPDATA
->m_width
= width
;
1319 void wxBitmap::SetDepth( int depth
)
1322 M_BMPDATA
->m_bpp
= depth
;
1325 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
1328 m_refData
= new wxBitmapRefData();
1330 M_BMPDATA
->m_pixmap
= pixmap
;
1333 void wxBitmap::SetBitmap( GdkPixmap
*bitmap
)
1336 m_refData
= new wxBitmapRefData();
1338 M_BMPDATA
->m_bitmap
= bitmap
;
1341 GdkPixmap
*wxBitmap::GetPixmap() const
1343 wxCHECK_MSG( Ok(), (GdkPixmap
*) NULL
, wxT("invalid bitmap") );
1345 return M_BMPDATA
->m_pixmap
;
1348 bool wxBitmap::HasPixmap() const
1350 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1352 return M_BMPDATA
->m_pixmap
!= NULL
;
1355 GdkBitmap
*wxBitmap::GetBitmap() const
1357 wxCHECK_MSG( Ok(), (GdkBitmap
*) NULL
, wxT("invalid bitmap") );
1359 return M_BMPDATA
->m_bitmap
;
1362 void *wxBitmap::GetRawData(wxPixelDataBase
& WXUNUSED(data
), int WXUNUSED(bpp
))
1367 void wxBitmap::UngetRawData(wxPixelDataBase
& WXUNUSED(data
))
1371 bool wxBitmap::HasAlpha() const
1376 //-----------------------------------------------------------------------------
1378 //-----------------------------------------------------------------------------
1380 IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler
, wxBitmapHandlerBase
)
1382 /* static */ void wxBitmap::InitStandardHandlers()
1384 // TODO: Insert handler based on GdkPixbufs handler later