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 wxObjectRefData
231 virtual ~wxBitmapRefData();
239 wxPalette
*m_palette
;
242 wxBitmapRefData::wxBitmapRefData()
244 m_pixmap
= (GdkPixmap
*) NULL
;
245 m_bitmap
= (GdkBitmap
*) NULL
;
246 m_mask
= (wxMask
*) NULL
;
250 m_palette
= (wxPalette
*) NULL
;
253 wxBitmapRefData::~wxBitmapRefData()
256 gdk_pixmap_unref( m_pixmap
);
258 gdk_bitmap_unref( m_bitmap
);
262 #endif // wxUSE_PALETTE
265 //-----------------------------------------------------------------------------
267 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
269 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
275 wxBitmap::wxBitmap( int width
, int height
, int depth
)
277 Create( width
, height
, depth
);
280 bool wxBitmap::Create( int width
, int height
, int depth
)
284 if ( width
<= 0 || height
<= 0 )
289 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
292 depth
= visual
->depth
;
294 wxCHECK_MSG( (depth
== visual
->depth
) || (depth
== 1) || (depth
== 32), false,
295 wxT("invalid bitmap depth") );
297 m_refData
= new wxBitmapRefData();
298 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
299 M_BMPDATA
->m_width
= width
;
300 M_BMPDATA
->m_height
= height
;
303 M_BMPDATA
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
304 M_BMPDATA
->m_bpp
= 1;
308 M_BMPDATA
->m_pixmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, depth
);
309 M_BMPDATA
->m_bpp
= visual
->depth
;
315 wxBitmap::wxBitmap(const char* const* bits
)
317 wxCHECK2_MSG(bits
!= NULL
, return, wxT("invalid bitmap data"));
319 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
321 m_refData
= new wxBitmapRefData();
323 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
325 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm_d( wxGetRootWindow()->window
, &mask
, NULL
, (gchar
**) bits
);
327 wxCHECK2_MSG(M_BMPDATA
->m_pixmap
, return, wxT("couldn't create pixmap"));
331 M_BMPDATA
->m_mask
= new wxMask();
332 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
335 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
337 M_BMPDATA
->m_bpp
= visual
->depth
; // Can we get a different depth from create_from_xpm_d() ?
340 wxBitmap
wxBitmap::Rescale( int clipx
, int clipy
, int clipwidth
, int clipheight
, int newx
, int newy
)
342 wxCHECK_MSG( Ok(), wxNullBitmap
, wxT("invalid bitmap") );
344 if (newy
==M_BMPDATA
->m_width
&& newy
==M_BMPDATA
->m_height
)
347 int width
= wxMax(newx
, 1);
348 int height
= wxMax(newy
, 1);
349 width
= wxMin(width
, clipwidth
);
350 height
= wxMin(height
, clipheight
);
354 GdkImage
*img
= (GdkImage
*) NULL
;
356 img
= gdk_image_get( GetPixmap(), 0, 0, GetWidth(), GetHeight() );
357 else if (GetBitmap())
358 img
= gdk_image_get( GetBitmap(), 0, 0, GetWidth(), GetHeight() );
360 wxFAIL_MSG( wxT("Ill-formed bitmap") );
362 wxCHECK_MSG( img
, wxNullBitmap
, wxT("couldn't create image") );
368 GdkPixmap
*dstpix
= NULL
;
371 GdkVisual
*visual
= gdk_window_get_visual( GetPixmap() );
373 visual
= wxTheApp
->GetGdkVisual();
376 bmp
= wxBitmap(width
,height
,bpp
);
377 dstpix
= bmp
.GetPixmap();
378 gc
= gdk_gc_new( dstpix
);
382 long dstbyteperline
= 0;
387 dstbyteperline
= width
/8*M_BMPDATA
->m_bpp
;
388 if (width
*M_BMPDATA
->m_bpp
% 8 != 0)
390 dst
= (char*) malloc(dstbyteperline
*height
);
393 // be careful to use the right scaling factor
394 float scx
= (float)M_BMPDATA
->m_width
/(float)newx
;
395 float scy
= (float)M_BMPDATA
->m_height
/(float)newy
;
396 // prepare accel-tables
397 int *tablex
= (int *)calloc(width
,sizeof(int));
398 int *tabley
= (int *)calloc(height
,sizeof(int));
400 // accel table filled with clipped values
401 for (int x
= 0; x
< width
; x
++)
402 tablex
[x
] = (int) (scx
* (x
+clipx
));
403 for (int y
= 0; y
< height
; y
++)
404 tabley
[y
] = (int) (scy
* (y
+clipy
));
406 // Main rescaling routine starts here
407 for (int h
= 0; h
< height
; h
++)
411 guint32 old_pixval
= 0;
413 for (int w
= 0; w
< width
; w
++)
421 pixval
= gdk_image_get_pixel( img
, x
, tabley
[h
] );
431 char shift
= bit
<< (w
% 8);
437 dst
[h
*dstbyteperline
+w
/8] = outbyte
;
445 gdk_gc_set_foreground( gc
, &col
);
446 gdk_draw_point( dstpix
, gc
, w
, h
);
450 // do not forget the last byte
451 if ((bpp
== 1) && (width
% 8 != 0))
452 dst
[h
*dstbyteperline
+width
/8] = outbyte
;
455 gdk_image_destroy( img
);
456 if (gc
) gdk_gc_unref( gc
);
460 bmp
= wxBitmap( (const char *)dst
, width
, height
, 1 );
466 dstbyteperline
= width
/8;
469 dst
= (char*) malloc(dstbyteperline
*height
);
470 img
= gdk_image_get( GetMask()->GetBitmap(), 0, 0, GetWidth(), GetHeight() );
472 for (int h
= 0; h
< height
; h
++)
476 guint32 old_pixval
= 0;
478 for (int w
= 0; w
< width
; w
++)
486 pixval
= gdk_image_get_pixel( img
, x
, tabley
[h
] );
494 char shift
= bit
<< (w
% 8);
500 dst
[h
*dstbyteperline
+w
/8] = outbyte
;
505 // do not forget the last byte
507 dst
[h
*dstbyteperline
+width
/8] = outbyte
;
509 wxMask
* mask
= new wxMask
;
510 mask
->m_bitmap
= gdk_bitmap_create_from_data( wxGetRootWindow()->window
, (gchar
*) dst
, width
, height
);
514 gdk_image_destroy( img
);
523 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
527 wxCHECK_MSG( image
.Ok(), false, wxT("invalid image") );
528 wxCHECK_MSG( depth
== -1 || depth
== 1, false, wxT("invalid bitmap depth") );
530 if (image
.GetWidth() <= 0 || image
.GetHeight() <= 0)
533 m_refData
= new wxBitmapRefData();
537 return CreateFromImageAsBitmap(image
);
541 return CreateFromImageAsPixmap(image
);
545 // conversion to mono bitmap:
546 bool wxBitmap::CreateFromImageAsBitmap(const wxImage
& img
)
548 // convert alpha channel to mask, if it is present:
550 image
.ConvertAlphaToMask();
552 int width
= image
.GetWidth();
553 int height
= image
.GetHeight();
558 SetBitmap( gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 ) );
562 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
564 // Create picture image
566 unsigned char *data_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
568 GdkImage
*data_image
=
569 gdk_image_new_bitmap( visual
, data_data
, width
, height
);
573 GdkImage
*mask_image
= (GdkImage
*) NULL
;
577 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
579 mask_image
= gdk_image_new_bitmap( visual
, mask_data
, width
, height
);
581 wxMask
*mask
= new wxMask();
582 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
587 int r_mask
= image
.GetMaskRed();
588 int g_mask
= image
.GetMaskGreen();
589 int b_mask
= image
.GetMaskBlue();
591 unsigned char* data
= image
.GetData();
594 for (int y
= 0; y
< height
; y
++)
596 for (int x
= 0; x
< width
; x
++)
607 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
608 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
610 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
613 if ((r
== 255) && (b
== 255) && (g
== 255))
614 gdk_image_put_pixel( data_image
, x
, y
, 1 );
616 gdk_image_put_pixel( data_image
, x
, y
, 0 );
623 GdkGC
*data_gc
= gdk_gc_new( GetBitmap() );
625 gdk_draw_image( GetBitmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
627 gdk_image_destroy( data_image
);
628 gdk_gc_unref( data_gc
);
634 GdkGC
*mask_gc
= gdk_gc_new( GetMask()->GetBitmap() );
636 gdk_draw_image( GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
638 gdk_image_destroy( mask_image
);
639 gdk_gc_unref( mask_gc
);
645 // conversion to colour bitmap:
646 bool wxBitmap::CreateFromImageAsPixmap(const wxImage
& img
)
648 // convert alpha channel to mask, if it is present:
650 image
.ConvertAlphaToMask();
652 int width
= image
.GetWidth();
653 int height
= image
.GetHeight();
658 SetPixmap( gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, -1 ) );
660 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
662 int bpp
= visual
->depth
;
666 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800))
671 // We handle 8-bit bitmaps ourselves using the colour cube, 12-bit
672 // visuals are not supported by GDK so we do these ourselves, too.
673 // 15-bit and 16-bit should actually work and 24-bit certainly does.
675 if (!image
.HasMask() && (bpp
> 16))
677 if (!image
.HasMask() && (bpp
> 12))
680 static bool s_hasInitialized
= false;
682 if (!s_hasInitialized
)
685 s_hasInitialized
= true;
688 GdkGC
*gc
= gdk_gc_new( GetPixmap() );
690 gdk_draw_rgb_image( GetPixmap(),
702 // Create picture image
704 GdkImage
*data_image
=
705 gdk_image_new( GDK_IMAGE_FASTEST
, visual
, width
, height
);
709 GdkImage
*mask_image
= (GdkImage
*) NULL
;
713 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
715 mask_image
= gdk_image_new_bitmap( visual
, mask_data
, width
, height
);
717 wxMask
*mask
= new wxMask();
718 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
725 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
726 byte_order b_o
= RGB
;
730 if ((visual
->red_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->blue_mask
)) b_o
= RGB
;
731 else if ((visual
->red_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->green_mask
)) b_o
= RBG
;
732 else if ((visual
->blue_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->green_mask
)) b_o
= BRG
;
733 else if ((visual
->blue_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->red_mask
)) b_o
= BGR
;
734 else if ((visual
->green_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->blue_mask
)) b_o
= GRB
;
735 else if ((visual
->green_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->red_mask
)) b_o
= GBR
;
738 int r_mask
= image
.GetMaskRed();
739 int g_mask
= image
.GetMaskGreen();
740 int b_mask
= image
.GetMaskBlue();
742 unsigned char* data
= image
.GetData();
745 for (int y
= 0; y
< height
; y
++)
747 for (int x
= 0; x
< width
; x
++)
758 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
759 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
761 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
769 if (wxTheApp
->m_colorCube
)
771 pixel
= wxTheApp
->m_colorCube
[ ((r
& 0xf8) << 7) + ((g
& 0xf8) << 2) + ((b
& 0xf8) >> 3) ];
775 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
776 GdkColor
*colors
= cmap
->colors
;
777 int max
= 3 * (65536);
779 for (int i
= 0; i
< cmap
->size
; i
++)
781 int rdiff
= (r
<< 8) - colors
[i
].red
;
782 int gdiff
= (g
<< 8) - colors
[i
].green
;
783 int bdiff
= (b
<< 8) - colors
[i
].blue
;
784 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
785 if (sum
< max
) { pixel
= i
; max
= sum
; }
789 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
798 case RGB
: pixel
= ((r
& 0xf0) << 4) | (g
& 0xf0) | ((b
& 0xf0) >> 4); break;
799 case RBG
: pixel
= ((r
& 0xf0) << 4) | (b
& 0xf0) | ((g
& 0xf0) >> 4); break;
800 case GRB
: pixel
= ((g
& 0xf0) << 4) | (r
& 0xf0) | ((b
& 0xf0) >> 4); break;
801 case GBR
: pixel
= ((g
& 0xf0) << 4) | (b
& 0xf0) | ((r
& 0xf0) >> 4); break;
802 case BRG
: pixel
= ((b
& 0xf0) << 4) | (r
& 0xf0) | ((g
& 0xf0) >> 4); break;
803 case BGR
: pixel
= ((b
& 0xf0) << 4) | (g
& 0xf0) | ((r
& 0xf0) >> 4); break;
805 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
813 case RGB
: pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
814 case RBG
: pixel
= ((r
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
815 case GRB
: pixel
= ((g
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
816 case GBR
: pixel
= ((g
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
817 case BRG
: pixel
= ((b
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
818 case BGR
: pixel
= ((b
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
820 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
825 // I actually don't know if for 16-bit displays, it is alway the green
826 // component or the second component which has 6 bits.
830 case RGB
: pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
831 case RBG
: pixel
= ((r
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
832 case GRB
: pixel
= ((g
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
833 case GBR
: pixel
= ((g
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
834 case BRG
: pixel
= ((b
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
835 case BGR
: pixel
= ((b
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
837 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
846 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
847 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
848 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
849 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
850 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
851 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
853 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
863 GdkGC
*data_gc
= gdk_gc_new( GetPixmap() );
865 gdk_draw_image( GetPixmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
867 gdk_image_destroy( data_image
);
868 gdk_gc_unref( data_gc
);
874 GdkGC
*mask_gc
= gdk_gc_new( GetMask()->GetBitmap() );
876 gdk_draw_image( GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
878 gdk_image_destroy( mask_image
);
879 gdk_gc_unref( mask_gc
);
885 wxImage
wxBitmap::ConvertToImage() const
889 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
891 image
.Create(GetWidth(), GetHeight());
892 unsigned char *data
= image
.GetData();
896 wxFAIL_MSG( wxT("couldn't create image") );
900 // the colour used as transparent one in wxImage and the one it is
901 // replaced with when it really occurs in the bitmap
902 static const int MASK_RED
= 1;
903 static const int MASK_GREEN
= 2;
904 static const int MASK_BLUE
= 3;
905 static const int MASK_BLUE_REPLACEMENT
= 2;
907 GdkImage
*gdk_image
= (GdkImage
*) NULL
;
911 gdk_image
= gdk_image_get( GetPixmap(),
913 GetWidth(), GetHeight() );
915 else if (GetBitmap())
917 gdk_image
= gdk_image_get( GetBitmap(),
919 GetWidth(), GetHeight() );
923 wxFAIL_MSG( wxT("Ill-formed bitmap") );
926 wxCHECK_MSG( gdk_image
, wxNullImage
, wxT("couldn't create image") );
928 GdkImage
*gdk_image_mask
= (GdkImage
*) NULL
;
931 gdk_image_mask
= gdk_image_get( GetMask()->GetBitmap(),
933 GetWidth(), GetHeight() );
935 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
939 int red_shift_right
= 0;
940 int green_shift_right
= 0;
941 int blue_shift_right
= 0;
942 int red_shift_left
= 0;
943 int green_shift_left
= 0;
944 int blue_shift_left
= 0;
945 bool use_shift
= false;
949 GdkVisual
*visual
= gdk_window_get_visual( GetPixmap() );
951 visual
= wxTheApp
->GetGdkVisual();
955 bpp
= visual
->red_prec
+ visual
->green_prec
+ visual
->blue_prec
;
956 red_shift_right
= visual
->red_shift
;
957 red_shift_left
= 8-visual
->red_prec
;
958 green_shift_right
= visual
->green_shift
;
959 green_shift_left
= 8-visual
->green_prec
;
960 blue_shift_right
= visual
->blue_shift
;
961 blue_shift_left
= 8-visual
->blue_prec
;
963 use_shift
= (visual
->type
== GDK_VISUAL_TRUE_COLOR
) || (visual
->type
== GDK_VISUAL_DIRECT_COLOR
);
971 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
974 for (int j
= 0; j
< GetHeight(); j
++)
976 for (int i
= 0; i
< GetWidth(); i
++)
978 wxUint32 pixel
= gdk_image_get_pixel( gdk_image
, i
, j
);
996 data
[pos
] = (pixel
>> red_shift_right
) << red_shift_left
;
997 data
[pos
+1] = (pixel
>> green_shift_right
) << green_shift_left
;
998 data
[pos
+2] = (pixel
>> blue_shift_right
) << blue_shift_left
;
1000 else if (cmap
->colors
)
1002 data
[pos
] = cmap
->colors
[pixel
].red
>> 8;
1003 data
[pos
+1] = cmap
->colors
[pixel
].green
>> 8;
1004 data
[pos
+2] = cmap
->colors
[pixel
].blue
>> 8;
1008 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
1013 int mask_pixel
= gdk_image_get_pixel( gdk_image_mask
, i
, j
);
1014 if (mask_pixel
== 0)
1016 data
[pos
] = MASK_RED
;
1017 data
[pos
+1] = MASK_GREEN
;
1018 data
[pos
+2] = MASK_BLUE
;
1020 else if ( data
[pos
] == MASK_RED
&&
1021 data
[pos
+1] == MASK_GREEN
&&
1022 data
[pos
+2] == MASK_BLUE
)
1024 data
[pos
+2] = MASK_BLUE_REPLACEMENT
;
1032 gdk_image_destroy( gdk_image
);
1033 if (gdk_image_mask
) gdk_image_destroy( gdk_image_mask
);
1038 wxBitmap::wxBitmap( const wxString
&filename
, wxBitmapType type
)
1040 LoadFile( filename
, type
);
1043 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int WXUNUSED(depth
))
1045 if ( width
> 0 && height
> 0 )
1047 m_refData
= new wxBitmapRefData();
1049 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
1050 M_BMPDATA
->m_bitmap
= gdk_bitmap_create_from_data
1052 wxGetRootWindow()->window
,
1057 M_BMPDATA
->m_width
= width
;
1058 M_BMPDATA
->m_height
= height
;
1059 M_BMPDATA
->m_bpp
= 1;
1061 wxASSERT_MSG( M_BMPDATA
->m_bitmap
, wxT("couldn't create bitmap") );
1065 wxBitmap::~wxBitmap()
1069 bool wxBitmap::operator == ( const wxBitmap
& bmp
) const
1071 return m_refData
== bmp
.m_refData
;
1074 bool wxBitmap::operator != ( const wxBitmap
& bmp
) const
1076 return m_refData
!= bmp
.m_refData
;
1079 bool wxBitmap::IsOk() const
1081 return (m_refData
!= NULL
) &&
1082 (M_BMPDATA
->m_bitmap
|| M_BMPDATA
->m_pixmap
);
1085 int wxBitmap::GetHeight() const
1087 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1089 return M_BMPDATA
->m_height
;
1092 int wxBitmap::GetWidth() const
1094 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1096 return M_BMPDATA
->m_width
;
1099 int wxBitmap::GetDepth() const
1101 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1103 return M_BMPDATA
->m_bpp
;
1106 wxMask
*wxBitmap::GetMask() const
1108 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
1110 return M_BMPDATA
->m_mask
;
1113 void wxBitmap::SetMask( wxMask
*mask
)
1115 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
1117 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
1119 M_BMPDATA
->m_mask
= mask
;
1122 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
1128 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
1130 wxCHECK_MSG( Ok() &&
1131 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1132 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) && (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
1133 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
1135 wxBitmap
ret( rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
1136 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
1138 if (ret
.GetPixmap())
1140 GdkGC
*gc
= gdk_gc_new( ret
.GetPixmap() );
1141 gdk_draw_pixmap( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1142 gdk_gc_destroy( gc
);
1146 GdkGC
*gc
= gdk_gc_new( ret
.GetBitmap() );
1148 col
.pixel
= 0xFFFFFF;
1149 gdk_gc_set_foreground( gc
, &col
);
1151 gdk_gc_set_background( gc
, &col
);
1152 gdk_wx_draw_bitmap( ret
.GetBitmap(), gc
, GetBitmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1153 gdk_gc_destroy( gc
);
1158 wxMask
*mask
= new wxMask
;
1159 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, rect
.width
, rect
.height
, 1 );
1161 GdkGC
*gc
= gdk_gc_new( mask
->m_bitmap
);
1163 col
.pixel
= 0xFFFFFF;
1164 gdk_gc_set_foreground( gc
, &col
);
1166 gdk_gc_set_background( gc
, &col
);
1167 gdk_wx_draw_bitmap( mask
->m_bitmap
, gc
, M_BMPDATA
->m_mask
->m_bitmap
, rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1168 gdk_gc_destroy( gc
);
1170 ret
.SetMask( mask
);
1176 bool wxBitmap::SaveFile( const wxString
&name
, wxBitmapType type
, const wxPalette
*WXUNUSED(palette
) ) const
1178 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1180 // Try to save the bitmap via wxImage handlers:
1182 wxImage image
= ConvertToImage();
1183 if (image
.Ok()) return image
.SaveFile( name
, type
);
1189 bool wxBitmap::LoadFile( const wxString
&name
, wxBitmapType type
)
1193 if (!wxFileExists(name
))
1196 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
1198 if (type
== wxBITMAP_TYPE_XPM
)
1200 m_refData
= new wxBitmapRefData();
1202 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
1204 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm
1206 wxGetRootWindow()->window
,
1214 M_BMPDATA
->m_mask
= new wxMask();
1215 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
1218 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
1220 M_BMPDATA
->m_bpp
= visual
->depth
;
1222 else // try if wxImage can load it
1225 if ( !image
.LoadFile( name
, type
) || !image
.Ok() )
1228 *this = wxBitmap(image
);
1235 wxPalette
*wxBitmap::GetPalette() const
1238 return (wxPalette
*) NULL
;
1240 return M_BMPDATA
->m_palette
;
1243 void wxBitmap::SetPalette(const wxPalette
& WXUNUSED(palette
))
1247 #endif // wxUSE_PALETTE
1249 void wxBitmap::SetHeight( int height
)
1252 m_refData
= new wxBitmapRefData();
1254 M_BMPDATA
->m_height
= height
;
1257 void wxBitmap::SetWidth( int width
)
1260 m_refData
= new wxBitmapRefData();
1262 M_BMPDATA
->m_width
= width
;
1265 void wxBitmap::SetDepth( int depth
)
1268 m_refData
= new wxBitmapRefData();
1270 M_BMPDATA
->m_bpp
= depth
;
1273 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
1276 m_refData
= new wxBitmapRefData();
1278 M_BMPDATA
->m_pixmap
= pixmap
;
1281 void wxBitmap::SetBitmap( GdkPixmap
*bitmap
)
1284 m_refData
= new wxBitmapRefData();
1286 M_BMPDATA
->m_bitmap
= bitmap
;
1289 GdkPixmap
*wxBitmap::GetPixmap() const
1291 wxCHECK_MSG( Ok(), (GdkPixmap
*) NULL
, wxT("invalid bitmap") );
1293 return M_BMPDATA
->m_pixmap
;
1296 bool wxBitmap::HasPixmap() const
1298 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1300 return M_BMPDATA
->m_pixmap
!= NULL
;
1303 GdkBitmap
*wxBitmap::GetBitmap() const
1305 wxCHECK_MSG( Ok(), (GdkBitmap
*) NULL
, wxT("invalid bitmap") );
1307 return M_BMPDATA
->m_bitmap
;
1310 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1315 void wxBitmap::UngetRawData(wxPixelDataBase
& WXUNUSED(data
))
1319 bool wxBitmap::HasAlpha() const
1324 void wxBitmap::UseAlpha()
1328 //-----------------------------------------------------------------------------
1330 //-----------------------------------------------------------------------------
1332 IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler
, wxBitmapHandlerBase
)
1334 /* static */ void wxBitmap::InitStandardHandlers()
1336 // TODO: Insert handler based on GdkPixbufs handler later