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
)
60 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
63 Create( bitmap
, colour
);
67 wxMask::wxMask( const wxBitmap
& bitmap
, int paletteIndex
)
70 Create( bitmap
, paletteIndex
);
72 #endif // wxUSE_PALETTE
74 wxMask::wxMask( const wxBitmap
& bitmap
)
83 gdk_bitmap_unref( m_bitmap
);
86 bool wxMask::Create( const wxBitmap
& bitmap
,
87 const wxColour
& colour
)
91 gdk_bitmap_unref( m_bitmap
);
95 wxImage image
= bitmap
.ConvertToImage();
96 if (!image
.IsOk()) 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
);
201 if (!bitmap
.IsOk()) 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
224 //-----------------------------------------------------------------------------
226 //-----------------------------------------------------------------------------
228 class wxBitmapRefData
: public wxGDIRefData
232 wxBitmapRefData(const wxBitmapRefData
& data
);
233 bool Create(int width
, int height
, int bpp
);
234 virtual ~wxBitmapRefData();
236 virtual bool IsOk() const { return m_pixmap
|| m_bitmap
; }
245 wxPalette
*m_palette
;
246 #endif // wxUSE_PALETTE
249 wxBitmapRefData::wxBitmapRefData()
259 #endif // wxUSE_PALETTE
262 wxBitmapRefData::wxBitmapRefData(const wxBitmapRefData
& data
)
264 Create(data
.m_width
, data
.m_height
, data
.m_bpp
);
266 m_mask
= data
.m_mask
? new wxMask(*data
.m_mask
) : NULL
;
269 wxASSERT_MSG( !data
.m_palette
,
270 wxT("copying bitmaps palette not implemented") );
271 #endif // wxUSE_PALETTE
274 // copy the bitmap data by simply drawing the source bitmap on this one
280 else if ( data
.m_bitmap
)
284 else // invalid bitmap?
289 GdkGC
*gc
= gdk_gc_new(*dst
);
292 gdk_wx_draw_bitmap(m_bitmap
, gc
, data
.m_bitmap
, 0, 0, 0, 0, -1, -1);
294 else // colour pixmap
296 gdk_draw_pixmap(m_pixmap
, gc
, data
.m_pixmap
, 0, 0, 0, 0, -1, -1);
302 bool wxBitmapRefData::Create(int width
, int height
, int bpp
)
313 // to understand how this compiles you should know that GdkPixmap and
314 // GdkBitmap are one and the same type in GTK+ 1
318 const GdkVisual
* const visual
= wxTheApp
->GetGdkVisual();
320 wxCHECK_MSG( (bpp
== -1) || (bpp
== visual
->depth
) || (bpp
== 32), false,
321 wxT("invalid bitmap depth") );
323 m_bpp
= visual
->depth
;
334 *ppix
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, m_bpp
);
336 return *ppix
!= NULL
;
339 wxBitmapRefData::~wxBitmapRefData()
342 gdk_pixmap_unref( m_pixmap
);
344 gdk_bitmap_unref( m_bitmap
);
348 #endif // wxUSE_PALETTE
352 //-----------------------------------------------------------------------------
354 //-----------------------------------------------------------------------------
356 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
358 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
360 wxGDIRefData
*wxBitmap::CreateGDIRefData() const
362 return new wxBitmapRefData
;
365 wxGDIRefData
*wxBitmap::CloneGDIRefData(const wxGDIRefData
*data
) const
367 return new wxBitmapRefData(*static_cast<const wxBitmapRefData
*>(data
));
370 bool wxBitmap::Create( int width
, int height
, int depth
)
374 if ( width
<= 0 || height
<= 0 )
379 m_refData
= new wxBitmapRefData();
380 return M_BMPDATA
->Create(width
, height
, depth
);
383 wxBitmap::wxBitmap(const char* const* bits
)
385 wxCHECK2_MSG(bits
!= NULL
, return, wxT("invalid bitmap data"));
387 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
389 m_refData
= new wxBitmapRefData();
391 GdkBitmap
*mask
= NULL
;
393 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm_d( wxGetRootWindow()->window
, &mask
, NULL
, (gchar
**) bits
);
395 wxCHECK2_MSG(M_BMPDATA
->m_pixmap
, return, wxT("couldn't create pixmap"));
399 M_BMPDATA
->m_mask
= new wxMask();
400 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
403 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
405 M_BMPDATA
->m_bpp
= visual
->depth
; // Can we get a different depth from create_from_xpm_d() ?
408 wxBitmap
wxBitmap::Rescale( int clipx
, int clipy
, int clipwidth
, int clipheight
, int newx
, int newy
)
410 wxCHECK_MSG( IsOk(), wxNullBitmap
, wxT("invalid bitmap") );
412 if (newy
==M_BMPDATA
->m_width
&& newy
==M_BMPDATA
->m_height
)
415 int width
= wxMax(newx
, 1);
416 int height
= wxMax(newy
, 1);
417 width
= wxMin(width
, clipwidth
);
418 height
= wxMin(height
, clipheight
);
422 GdkImage
*img
= NULL
;
424 img
= gdk_image_get( GetPixmap(), 0, 0, GetWidth(), GetHeight() );
425 else if (GetBitmap())
426 img
= gdk_image_get( GetBitmap(), 0, 0, GetWidth(), GetHeight() );
428 wxFAIL_MSG( wxT("Ill-formed bitmap") );
430 wxCHECK_MSG( img
, wxNullBitmap
, wxT("couldn't create image") );
436 GdkPixmap
*dstpix
= NULL
;
439 GdkVisual
*visual
= gdk_window_get_visual( GetPixmap() );
441 visual
= wxTheApp
->GetGdkVisual();
444 bmp
= wxBitmap(width
,height
,bpp
);
445 dstpix
= bmp
.GetPixmap();
446 gc
= gdk_gc_new( dstpix
);
450 long dstbyteperline
= 0;
455 dstbyteperline
= width
/8*M_BMPDATA
->m_bpp
;
456 if (width
*M_BMPDATA
->m_bpp
% 8 != 0)
458 dst
= (char*) malloc(dstbyteperline
*height
);
461 // be careful to use the right scaling factor
462 float scx
= (float)M_BMPDATA
->m_width
/(float)newx
;
463 float scy
= (float)M_BMPDATA
->m_height
/(float)newy
;
464 // prepare accel-tables
465 int *tablex
= (int *)calloc(width
,sizeof(int));
466 int *tabley
= (int *)calloc(height
,sizeof(int));
468 // accel table filled with clipped values
469 for (int x
= 0; x
< width
; x
++)
470 tablex
[x
] = (int) (scx
* (x
+clipx
));
471 for (int y
= 0; y
< height
; y
++)
472 tabley
[y
] = (int) (scy
* (y
+clipy
));
474 // Main rescaling routine starts here
475 for (int h
= 0; h
< height
; h
++)
479 guint32 old_pixval
= 0;
481 for (int w
= 0; w
< width
; w
++)
489 pixval
= gdk_image_get_pixel( img
, x
, tabley
[h
] );
499 char shift
= bit
<< (w
% 8);
505 dst
[h
*dstbyteperline
+w
/8] = outbyte
;
513 gdk_gc_set_foreground( gc
, &col
);
514 gdk_draw_point( dstpix
, gc
, w
, h
);
518 // do not forget the last byte
519 if ((bpp
== 1) && (width
% 8 != 0))
520 dst
[h
*dstbyteperline
+width
/8] = outbyte
;
523 gdk_image_destroy( img
);
524 if (gc
) gdk_gc_unref( gc
);
528 bmp
= wxBitmap( (const char *)dst
, width
, height
, 1 );
534 dstbyteperline
= width
/8;
537 dst
= (char*) malloc(dstbyteperline
*height
);
538 img
= gdk_image_get( GetMask()->GetBitmap(), 0, 0, GetWidth(), GetHeight() );
540 for (int h
= 0; h
< height
; h
++)
544 guint32 old_pixval
= 0;
546 for (int w
= 0; w
< width
; w
++)
554 pixval
= gdk_image_get_pixel( img
, x
, tabley
[h
] );
562 char shift
= bit
<< (w
% 8);
568 dst
[h
*dstbyteperline
+w
/8] = outbyte
;
573 // do not forget the last byte
575 dst
[h
*dstbyteperline
+width
/8] = outbyte
;
577 wxMask
* mask
= new wxMask
;
578 mask
->m_bitmap
= gdk_bitmap_create_from_data( wxGetRootWindow()->window
, (gchar
*) dst
, width
, height
);
582 gdk_image_destroy( img
);
591 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
595 wxCHECK_MSG( image
.IsOk(), false, wxT("invalid image") );
596 wxCHECK_MSG( depth
== -1 || depth
== 1, false, wxT("invalid bitmap depth") );
598 if (image
.GetWidth() <= 0 || image
.GetHeight() <= 0)
601 m_refData
= new wxBitmapRefData();
605 return CreateFromImageAsBitmap(image
);
609 return CreateFromImageAsPixmap(image
);
613 // conversion to mono bitmap:
614 bool wxBitmap::CreateFromImageAsBitmap(const wxImage
& img
)
616 // convert alpha channel to mask, if it is present:
618 image
.ConvertAlphaToMask();
620 int width
= image
.GetWidth();
621 int height
= image
.GetHeight();
626 SetBitmap( gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 ) );
630 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
632 // Create picture image
634 unsigned char *data_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
636 GdkImage
*data_image
=
637 gdk_image_new_bitmap( visual
, data_data
, width
, height
);
641 GdkImage
*mask_image
= NULL
;
645 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
647 mask_image
= gdk_image_new_bitmap( visual
, mask_data
, width
, height
);
649 wxMask
*mask
= new wxMask();
650 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
655 int r_mask
= image
.GetMaskRed();
656 int g_mask
= image
.GetMaskGreen();
657 int b_mask
= image
.GetMaskBlue();
659 unsigned char* data
= image
.GetData();
662 for (int y
= 0; y
< height
; y
++)
664 for (int x
= 0; x
< width
; x
++)
675 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
676 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
678 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
681 if ((r
== 255) && (b
== 255) && (g
== 255))
682 gdk_image_put_pixel( data_image
, x
, y
, 1 );
684 gdk_image_put_pixel( data_image
, x
, y
, 0 );
691 GdkGC
*data_gc
= gdk_gc_new( GetBitmap() );
693 gdk_draw_image( GetBitmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
695 gdk_image_destroy( data_image
);
696 gdk_gc_unref( data_gc
);
702 GdkGC
*mask_gc
= gdk_gc_new( GetMask()->GetBitmap() );
704 gdk_draw_image( GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
706 gdk_image_destroy( mask_image
);
707 gdk_gc_unref( mask_gc
);
713 // conversion to colour bitmap:
714 bool wxBitmap::CreateFromImageAsPixmap(const wxImage
& img
)
716 // convert alpha channel to mask, if it is present:
718 image
.ConvertAlphaToMask();
720 int width
= image
.GetWidth();
721 int height
= image
.GetHeight();
726 SetPixmap( gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, -1 ) );
728 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
730 int bpp
= visual
->depth
;
734 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800))
739 // We handle 8-bit bitmaps ourselves using the colour cube, 12-bit
740 // visuals are not supported by GDK so we do these ourselves, too.
741 // 15-bit and 16-bit should actually work and 24-bit certainly does.
743 if (!image
.HasMask() && (bpp
> 16))
745 if (!image
.HasMask() && (bpp
> 12))
748 static bool s_hasInitialized
= false;
750 if (!s_hasInitialized
)
753 s_hasInitialized
= true;
756 GdkGC
*gc
= gdk_gc_new( GetPixmap() );
758 gdk_draw_rgb_image( GetPixmap(),
770 // Create picture image
772 GdkImage
*data_image
=
773 gdk_image_new( GDK_IMAGE_FASTEST
, visual
, width
, height
);
777 GdkImage
*mask_image
= NULL
;
781 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
783 mask_image
= gdk_image_new_bitmap( visual
, mask_data
, width
, height
);
785 wxMask
*mask
= new wxMask();
786 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
793 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
794 byte_order b_o
= RGB
;
798 if ((visual
->red_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->blue_mask
)) b_o
= RGB
;
799 else if ((visual
->red_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->green_mask
)) b_o
= RBG
;
800 else if ((visual
->blue_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->green_mask
)) b_o
= BRG
;
801 else if ((visual
->blue_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->red_mask
)) b_o
= BGR
;
802 else if ((visual
->green_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->blue_mask
)) b_o
= GRB
;
803 else if ((visual
->green_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->red_mask
)) b_o
= GBR
;
806 int r_mask
= image
.GetMaskRed();
807 int g_mask
= image
.GetMaskGreen();
808 int b_mask
= image
.GetMaskBlue();
810 unsigned char* data
= image
.GetData();
813 for (int y
= 0; y
< height
; y
++)
815 for (int x
= 0; x
< width
; x
++)
826 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
827 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
829 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
837 if (wxTheApp
->m_colorCube
)
839 pixel
= wxTheApp
->m_colorCube
[ ((r
& 0xf8) << 7) + ((g
& 0xf8) << 2) + ((b
& 0xf8) >> 3) ];
843 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
844 GdkColor
*colors
= cmap
->colors
;
845 int max
= 3 * (65536);
847 for (int i
= 0; i
< cmap
->size
; i
++)
849 int rdiff
= (r
<< 8) - colors
[i
].red
;
850 int gdiff
= (g
<< 8) - colors
[i
].green
;
851 int bdiff
= (b
<< 8) - colors
[i
].blue
;
852 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
853 if (sum
< max
) { pixel
= i
; max
= sum
; }
857 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
866 case RGB
: pixel
= ((r
& 0xf0) << 4) | (g
& 0xf0) | ((b
& 0xf0) >> 4); break;
867 case RBG
: pixel
= ((r
& 0xf0) << 4) | (b
& 0xf0) | ((g
& 0xf0) >> 4); break;
868 case GRB
: pixel
= ((g
& 0xf0) << 4) | (r
& 0xf0) | ((b
& 0xf0) >> 4); break;
869 case GBR
: pixel
= ((g
& 0xf0) << 4) | (b
& 0xf0) | ((r
& 0xf0) >> 4); break;
870 case BRG
: pixel
= ((b
& 0xf0) << 4) | (r
& 0xf0) | ((g
& 0xf0) >> 4); break;
871 case BGR
: pixel
= ((b
& 0xf0) << 4) | (g
& 0xf0) | ((r
& 0xf0) >> 4); break;
873 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
881 case RGB
: pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
882 case RBG
: pixel
= ((r
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
883 case GRB
: pixel
= ((g
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
884 case GBR
: pixel
= ((g
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
885 case BRG
: pixel
= ((b
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
886 case BGR
: pixel
= ((b
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
888 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
893 // I actually don't know if for 16-bit displays, it is alway the green
894 // component or the second component which has 6 bits.
898 case RGB
: pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
899 case RBG
: pixel
= ((r
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
900 case GRB
: pixel
= ((g
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
901 case GBR
: pixel
= ((g
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
902 case BRG
: pixel
= ((b
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
903 case BGR
: pixel
= ((b
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
905 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
914 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
915 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
916 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
917 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
918 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
919 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
921 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
931 GdkGC
*data_gc
= gdk_gc_new( GetPixmap() );
933 gdk_draw_image( GetPixmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
935 gdk_image_destroy( data_image
);
936 gdk_gc_unref( data_gc
);
942 GdkGC
*mask_gc
= gdk_gc_new( GetMask()->GetBitmap() );
944 gdk_draw_image( GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
946 gdk_image_destroy( mask_image
);
947 gdk_gc_unref( mask_gc
);
953 wxImage
wxBitmap::ConvertToImage() const
957 wxCHECK_MSG( IsOk(), wxNullImage
, wxT("invalid bitmap") );
959 image
.Create(GetWidth(), GetHeight());
960 unsigned char *data
= image
.GetData();
964 wxFAIL_MSG( wxT("couldn't create image") );
968 // the colour used as transparent one in wxImage and the one it is
969 // replaced with when it really occurs in the bitmap
970 static const int MASK_RED
= 1;
971 static const int MASK_GREEN
= 2;
972 static const int MASK_BLUE
= 3;
973 static const int MASK_BLUE_REPLACEMENT
= 2;
975 GdkImage
*gdk_image
= NULL
;
979 gdk_image
= gdk_image_get( GetPixmap(),
981 GetWidth(), GetHeight() );
983 else if (GetBitmap())
985 gdk_image
= gdk_image_get( GetBitmap(),
987 GetWidth(), GetHeight() );
991 wxFAIL_MSG( wxT("Ill-formed bitmap") );
994 wxCHECK_MSG( gdk_image
, wxNullImage
, wxT("couldn't create image") );
996 GdkImage
*gdk_image_mask
= NULL
;
999 gdk_image_mask
= gdk_image_get( GetMask()->GetBitmap(),
1001 GetWidth(), GetHeight() );
1003 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
1007 int red_shift_right
= 0;
1008 int green_shift_right
= 0;
1009 int blue_shift_right
= 0;
1010 int red_shift_left
= 0;
1011 int green_shift_left
= 0;
1012 int blue_shift_left
= 0;
1013 bool use_shift
= false;
1017 GdkVisual
*visual
= gdk_window_get_visual( GetPixmap() );
1019 visual
= wxTheApp
->GetGdkVisual();
1021 bpp
= visual
->depth
;
1023 bpp
= visual
->red_prec
+ visual
->green_prec
+ visual
->blue_prec
;
1024 red_shift_right
= visual
->red_shift
;
1025 red_shift_left
= 8-visual
->red_prec
;
1026 green_shift_right
= visual
->green_shift
;
1027 green_shift_left
= 8-visual
->green_prec
;
1028 blue_shift_right
= visual
->blue_shift
;
1029 blue_shift_left
= 8-visual
->blue_prec
;
1031 use_shift
= (visual
->type
== GDK_VISUAL_TRUE_COLOR
) || (visual
->type
== GDK_VISUAL_DIRECT_COLOR
);
1039 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
1042 for (int j
= 0; j
< GetHeight(); j
++)
1044 for (int i
= 0; i
< GetWidth(); i
++)
1046 wxUint32 pixel
= gdk_image_get_pixel( gdk_image
, i
, j
);
1064 data
[pos
] = (pixel
>> red_shift_right
) << red_shift_left
;
1065 data
[pos
+1] = (pixel
>> green_shift_right
) << green_shift_left
;
1066 data
[pos
+2] = (pixel
>> blue_shift_right
) << blue_shift_left
;
1068 else if (cmap
->colors
)
1070 data
[pos
] = cmap
->colors
[pixel
].red
>> 8;
1071 data
[pos
+1] = cmap
->colors
[pixel
].green
>> 8;
1072 data
[pos
+2] = cmap
->colors
[pixel
].blue
>> 8;
1076 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
1081 int mask_pixel
= gdk_image_get_pixel( gdk_image_mask
, i
, j
);
1082 if (mask_pixel
== 0)
1084 data
[pos
] = MASK_RED
;
1085 data
[pos
+1] = MASK_GREEN
;
1086 data
[pos
+2] = MASK_BLUE
;
1088 else if ( data
[pos
] == MASK_RED
&&
1089 data
[pos
+1] == MASK_GREEN
&&
1090 data
[pos
+2] == MASK_BLUE
)
1092 data
[pos
+2] = MASK_BLUE_REPLACEMENT
;
1100 gdk_image_destroy( gdk_image
);
1101 if (gdk_image_mask
) gdk_image_destroy( gdk_image_mask
);
1106 wxBitmap::wxBitmap( const wxString
&filename
, wxBitmapType type
)
1108 LoadFile( filename
, type
);
1111 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int WXUNUSED(depth
))
1113 if ( width
> 0 && height
> 0 )
1115 m_refData
= new wxBitmapRefData();
1117 M_BMPDATA
->m_mask
= NULL
;
1118 M_BMPDATA
->m_bitmap
= gdk_bitmap_create_from_data
1120 wxGetRootWindow()->window
,
1125 M_BMPDATA
->m_width
= width
;
1126 M_BMPDATA
->m_height
= height
;
1127 M_BMPDATA
->m_bpp
= 1;
1129 wxASSERT_MSG( M_BMPDATA
->m_bitmap
, wxT("couldn't create bitmap") );
1133 wxBitmap::~wxBitmap()
1137 int wxBitmap::GetHeight() const
1139 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
1141 return M_BMPDATA
->m_height
;
1144 int wxBitmap::GetWidth() const
1146 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
1148 return M_BMPDATA
->m_width
;
1151 int wxBitmap::GetDepth() const
1153 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
1155 return M_BMPDATA
->m_bpp
;
1158 wxMask
*wxBitmap::GetMask() const
1160 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") );
1162 return M_BMPDATA
->m_mask
;
1165 void wxBitmap::SetMask( wxMask
*mask
)
1167 wxCHECK_RET( IsOk(), wxT("invalid bitmap") );
1170 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
1172 M_BMPDATA
->m_mask
= mask
;
1175 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
1181 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
1183 wxCHECK_MSG( IsOk() &&
1184 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1185 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) && (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
1186 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
1188 wxBitmap
ret( rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
1189 wxASSERT_MSG( ret
.IsOk(), wxT("GetSubBitmap error") );
1191 if (ret
.GetPixmap())
1193 GdkGC
*gc
= gdk_gc_new( ret
.GetPixmap() );
1194 gdk_draw_pixmap( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1195 gdk_gc_destroy( gc
);
1199 GdkGC
*gc
= gdk_gc_new( ret
.GetBitmap() );
1201 col
.pixel
= 0xFFFFFF;
1202 gdk_gc_set_foreground( gc
, &col
);
1204 gdk_gc_set_background( gc
, &col
);
1205 gdk_wx_draw_bitmap( ret
.GetBitmap(), gc
, GetBitmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1206 gdk_gc_destroy( gc
);
1211 wxMask
*mask
= new wxMask
;
1212 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, rect
.width
, rect
.height
, 1 );
1214 GdkGC
*gc
= gdk_gc_new( mask
->m_bitmap
);
1216 col
.pixel
= 0xFFFFFF;
1217 gdk_gc_set_foreground( gc
, &col
);
1219 gdk_gc_set_background( gc
, &col
);
1220 gdk_wx_draw_bitmap( mask
->m_bitmap
, gc
, M_BMPDATA
->m_mask
->m_bitmap
, rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1221 gdk_gc_destroy( gc
);
1223 ret
.SetMask( mask
);
1229 bool wxBitmap::SaveFile( const wxString
&name
, wxBitmapType type
, const wxPalette
*WXUNUSED(palette
) ) const
1231 wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") );
1233 // Try to save the bitmap via wxImage handlers:
1235 wxImage image
= ConvertToImage();
1236 if (image
.IsOk()) return image
.SaveFile( name
, type
);
1242 bool wxBitmap::LoadFile( const wxString
&name
, wxBitmapType type
)
1246 if (!wxFileExists(name
))
1249 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
1251 if (type
== wxBITMAP_TYPE_XPM
)
1253 m_refData
= new wxBitmapRefData();
1255 GdkBitmap
*mask
= NULL
;
1257 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm
1259 wxGetRootWindow()->window
,
1267 M_BMPDATA
->m_mask
= new wxMask();
1268 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
1271 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
1273 M_BMPDATA
->m_bpp
= visual
->depth
;
1275 else // try if wxImage can load it
1278 if ( !image
.LoadFile( name
, type
) || !image
.IsOk() )
1281 *this = wxBitmap(image
);
1288 wxPalette
*wxBitmap::GetPalette() const
1293 return M_BMPDATA
->m_palette
;
1296 void wxBitmap::SetPalette(const wxPalette
& WXUNUSED(palette
))
1300 #endif // wxUSE_PALETTE
1302 void wxBitmap::SetHeight( int height
)
1305 M_BMPDATA
->m_height
= height
;
1308 void wxBitmap::SetWidth( int width
)
1311 M_BMPDATA
->m_width
= width
;
1314 void wxBitmap::SetDepth( int depth
)
1317 M_BMPDATA
->m_bpp
= depth
;
1320 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
1323 m_refData
= new wxBitmapRefData();
1325 M_BMPDATA
->m_pixmap
= pixmap
;
1328 void wxBitmap::SetBitmap( GdkPixmap
*bitmap
)
1331 m_refData
= new wxBitmapRefData();
1333 M_BMPDATA
->m_bitmap
= bitmap
;
1336 GdkPixmap
*wxBitmap::GetPixmap() const
1338 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") );
1340 return M_BMPDATA
->m_pixmap
;
1343 bool wxBitmap::HasPixmap() const
1345 wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") );
1347 return M_BMPDATA
->m_pixmap
!= NULL
;
1350 GdkBitmap
*wxBitmap::GetBitmap() const
1352 wxCHECK_MSG( IsOk(), NULL
, wxT("invalid bitmap") );
1354 return M_BMPDATA
->m_bitmap
;
1357 void *wxBitmap::GetRawData(wxPixelDataBase
& WXUNUSED(data
), int WXUNUSED(bpp
))
1362 void wxBitmap::UngetRawData(wxPixelDataBase
& WXUNUSED(data
))
1366 bool wxBitmap::HasAlpha() const
1371 /* static */ void wxBitmap::InitStandardHandlers()
1373 // TODO: Insert handler based on GdkPixbufs handler later