1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "bitmap.h"
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
19 #include "wx/palette.h"
20 #include "wx/bitmap.h"
22 #include "wx/filefn.h"
24 #include "wx/dcmemory.h"
28 // need this to get gdk_image_new_bitmap()
29 #define GDK_ENABLE_BROKEN
37 #include <gdk/gdkimage.h>
39 #include <gdk/gdkrgb.h>
40 #endif // GTK+ 2.0/1.2
44 extern void gdk_wx_draw_bitmap (GdkDrawable
*drawable
,
54 //-----------------------------------------------------------------------------
56 //-----------------------------------------------------------------------------
58 extern GtkWidget
*wxGetRootWindow();
60 //-----------------------------------------------------------------------------
62 //-----------------------------------------------------------------------------
64 IMPLEMENT_DYNAMIC_CLASS(wxMask
,wxObject
)
68 m_bitmap
= (GdkBitmap
*) NULL
;
71 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
73 m_bitmap
= (GdkBitmap
*) NULL
;
74 Create( bitmap
, colour
);
77 wxMask::wxMask( const wxBitmap
& bitmap
, int paletteIndex
)
79 m_bitmap
= (GdkBitmap
*) NULL
;
80 Create( bitmap
, paletteIndex
);
83 wxMask::wxMask( const wxBitmap
& bitmap
)
85 m_bitmap
= (GdkBitmap
*) NULL
;
92 gdk_bitmap_unref( m_bitmap
);
95 bool wxMask::Create( const wxBitmap
& bitmap
,
96 const wxColour
& colour
)
100 gdk_bitmap_unref( m_bitmap
);
101 m_bitmap
= (GdkBitmap
*) NULL
;
104 wxImage image
= bitmap
.ConvertToImage();
105 if (!image
.Ok()) return FALSE
;
107 m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, image
.GetWidth(), image
.GetHeight(), 1 );
108 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
115 gdk_gc_set_foreground( gc
, &color
);
116 gdk_gc_set_fill( gc
, GDK_SOLID
);
117 gdk_draw_rectangle( m_bitmap
, gc
, TRUE
, 0, 0, image
.GetWidth(), image
.GetHeight() );
119 unsigned char *data
= image
.GetData();
122 unsigned char red
= colour
.Red();
123 unsigned char green
= colour
.Green();
124 unsigned char blue
= colour
.Blue();
126 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
128 int bpp
= visual
->depth
;
129 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800))
134 green
= green
& 0xf8;
140 green
= green
& 0xfc;
146 green
= green
& 0xf0;
154 gdk_gc_set_foreground( gc
, &color
);
156 for (int j
= 0; j
< image
.GetHeight(); j
++)
160 for (i
= 0; i
< image
.GetWidth(); i
++)
162 if ((data
[index
] == red
) &&
163 (data
[index
+1] == green
) &&
164 (data
[index
+2] == blue
))
173 gdk_draw_line( m_bitmap
, gc
, start_x
, j
, i
-1, j
);
180 gdk_draw_line( m_bitmap
, gc
, start_x
, j
, i
, j
);
188 bool wxMask::Create( const wxBitmap
& bitmap
, int paletteIndex
)
191 wxPalette
*pal
= bitmap
.GetPalette();
193 wxCHECK_MSG( pal
, FALSE
, wxT("Cannot create mask from bitmap without palette") );
195 pal
->GetRGB(paletteIndex
, &r
, &g
, &b
);
197 return Create(bitmap
, wxColour(r
, g
, b
));
200 bool wxMask::Create( const wxBitmap
& bitmap
)
204 gdk_bitmap_unref( m_bitmap
);
205 m_bitmap
= (GdkBitmap
*) NULL
;
208 if (!bitmap
.Ok()) return FALSE
;
210 wxCHECK_MSG( bitmap
.GetBitmap(), FALSE
, wxT("Cannot create mask from colour bitmap") );
212 m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, bitmap
.GetWidth(), bitmap
.GetHeight(), 1 );
214 if (!m_bitmap
) return FALSE
;
216 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
218 gdk_wx_draw_bitmap( m_bitmap
, gc
, bitmap
.GetBitmap(), 0, 0, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight() );
225 GdkBitmap
*wxMask::GetBitmap() const
230 //-----------------------------------------------------------------------------
232 //-----------------------------------------------------------------------------
234 class wxBitmapRefData
: public wxObjectRefData
246 wxPalette
*m_palette
;
249 wxBitmapRefData::wxBitmapRefData()
251 m_pixmap
= (GdkPixmap
*) NULL
;
252 m_bitmap
= (GdkBitmap
*) NULL
;
253 m_mask
= (wxMask
*) NULL
;
257 m_palette
= (wxPalette
*) NULL
;
260 wxBitmapRefData::~wxBitmapRefData()
263 gdk_pixmap_unref( m_pixmap
);
265 gdk_bitmap_unref( m_bitmap
);
270 //-----------------------------------------------------------------------------
272 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
274 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
280 wxBitmap::wxBitmap( int width
, int height
, int depth
)
282 Create( width
, height
, depth
);
285 bool wxBitmap::Create( int width
, int height
, int depth
)
289 if ( width
<= 0 || height
<= 0 )
294 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
297 depth
= visual
->depth
;
299 wxCHECK_MSG( (depth
== visual
->depth
) || (depth
== 1), FALSE
,
300 wxT("invalid bitmap depth") )
302 m_refData
= new wxBitmapRefData();
303 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
304 M_BMPDATA
->m_width
= width
;
305 M_BMPDATA
->m_height
= height
;
308 M_BMPDATA
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
309 M_BMPDATA
->m_bpp
= 1;
313 M_BMPDATA
->m_pixmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, depth
);
314 M_BMPDATA
->m_bpp
= visual
->depth
;
320 bool wxBitmap::CreateFromXpm( const char **bits
)
324 wxCHECK_MSG( bits
!= NULL
, FALSE
, wxT("invalid bitmap data") )
326 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
328 m_refData
= new wxBitmapRefData();
330 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
332 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm_d( wxGetRootWindow()->window
, &mask
, NULL
, (gchar
**) bits
);
334 wxCHECK_MSG( M_BMPDATA
->m_pixmap
, FALSE
, wxT("couldn't create pixmap") );
338 M_BMPDATA
->m_mask
= new wxMask();
339 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
342 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
344 M_BMPDATA
->m_bpp
= visual
->depth
; // Can we get a different depth from create_from_xpm_d() ?
349 wxBitmap
wxBitmap::Rescale( int clipx
, int clipy
, int clipwidth
, int clipheight
, int newx
, int newy
)
351 wxCHECK_MSG( Ok(), wxNullBitmap
, wxT("invalid bitmap") );
353 if (newy
==M_BMPDATA
->m_width
&& newy
==M_BMPDATA
->m_height
)
356 GdkImage
*img
= (GdkImage
*) NULL
;
358 img
= gdk_image_get( GetPixmap(), 0, 0, GetWidth(), GetHeight() );
359 else if (GetBitmap())
360 img
= gdk_image_get( GetBitmap(), 0, 0, GetWidth(), GetHeight() );
362 wxFAIL_MSG( wxT("Ill-formed bitmap") );
364 wxCHECK_MSG( img
, wxNullBitmap
, wxT("couldn't create image") );
369 int width
= wxMax(newx
, 1);
370 int height
= wxMax(newy
, 1);
371 width
= wxMin(width
, clipwidth
);
372 height
= wxMin(height
, clipheight
);
375 GdkPixmap
*dstpix
= NULL
;
378 GdkVisual
*visual
= gdk_window_get_visual( GetPixmap() );
380 visual
= wxTheApp
->GetGdkVisual();
383 bmp
= wxBitmap(width
,height
,bpp
);
384 dstpix
= bmp
.GetPixmap();
385 gc
= gdk_gc_new( dstpix
);
389 long dstbyteperline
= 0;
394 dstbyteperline
= width
/8*M_BMPDATA
->m_bpp
;
395 if (width
*M_BMPDATA
->m_bpp
% 8 != 0)
397 dst
= (char*) malloc(dstbyteperline
*height
);
400 // be careful to use the right scaling factor
401 float scx
= (float)M_BMPDATA
->m_width
/(float)newx
;
402 float scy
= (float)M_BMPDATA
->m_height
/(float)newy
;
403 // prepare accel-tables
404 int *tablex
= (int *)calloc(width
,sizeof(int));
405 int *tabley
= (int *)calloc(height
,sizeof(int));
407 // accel table filled with clipped values
408 for (int x
= 0; x
< width
; x
++)
409 tablex
[x
] = (int) (scx
* (x
+clipx
));
410 for (int y
= 0; y
< height
; y
++)
411 tabley
[y
] = (int) (scy
* (y
+clipy
));
413 // Main rescaling routine starts here
414 for (int h
= 0; h
< height
; h
++)
418 guint32 old_pixval
= 0;
420 for (int w
= 0; w
< width
; w
++)
428 pixval
= gdk_image_get_pixel( img
, x
, tabley
[h
] );
438 char shift
= bit
<< w
% 8;
444 dst
[h
*dstbyteperline
+w
/8] = outbyte
;
452 gdk_gc_set_foreground( gc
, &col
);
453 gdk_draw_point( dstpix
, gc
, w
, h
);
457 // do not forget the last byte
458 if ((bpp
== 1) && (width
% 8 != 0))
459 dst
[h
*dstbyteperline
+width
/8] = outbyte
;
462 gdk_image_destroy( img
);
463 if (gc
) gdk_gc_unref( gc
);
467 bmp
= wxBitmap( (const char *)dst
, width
, height
, 1 );
473 dstbyteperline
= width
/8;
476 dst
= (char*) malloc(dstbyteperline
*height
);
477 img
= gdk_image_get( GetMask()->GetBitmap(), 0, 0, GetWidth(), GetHeight() );
479 for (int h
= 0; h
< height
; h
++)
483 guint32 old_pixval
= 0;
485 for (int w
= 0; w
< width
; w
++)
493 pixval
= gdk_image_get_pixel( img
, x
, tabley
[h
] );
501 char shift
= bit
<< w
% 8;
507 dst
[h
*dstbyteperline
+w
/8] = outbyte
;
512 // do not forget the last byte
514 dst
[h
*dstbyteperline
+width
/8] = outbyte
;
516 wxMask
* mask
= new wxMask
;
517 mask
->m_bitmap
= gdk_bitmap_create_from_data( wxGetRootWindow()->window
, (gchar
*) dst
, width
, height
);
521 gdk_image_destroy( img
);
530 bool wxBitmap::CreateFromImage( const wxImage
& img
, int depth
)
534 wxCHECK_MSG( img
.Ok(), FALSE
, wxT("invalid image") )
535 wxCHECK_MSG( depth
== -1 || depth
== 1, FALSE
, wxT("invalid bitmap depth") )
537 // NB: wxGTK doesn't yet support alpha channel in bitmaps. The best we can
538 // do is to convert alpha channel to mask, if it is present:
540 image
.ConvertAlphaToMask();
542 int width
= image
.GetWidth();
543 int height
= image
.GetHeight();
545 if ( width
<= 0 || height
<= 0 )
550 m_refData
= new wxBitmapRefData();
556 // conversion to mono bitmap:
560 SetBitmap( gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 ) );
564 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
566 // Create picture image
568 unsigned char *data_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
570 GdkImage
*data_image
=
571 gdk_image_new_bitmap( visual
, data_data
, width
, height
);
575 GdkImage
*mask_image
= (GdkImage
*) NULL
;
579 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
581 mask_image
= gdk_image_new_bitmap( visual
, mask_data
, width
, height
);
583 wxMask
*mask
= new wxMask();
584 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
589 int r_mask
= image
.GetMaskRed();
590 int g_mask
= image
.GetMaskGreen();
591 int b_mask
= image
.GetMaskBlue();
593 unsigned char* data
= image
.GetData();
596 for (int y
= 0; y
< height
; y
++)
598 for (int x
= 0; x
< width
; x
++)
609 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
610 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
612 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
615 if ((r
== 255) && (b
== 255) && (g
== 255))
616 gdk_image_put_pixel( data_image
, x
, y
, 1 );
618 gdk_image_put_pixel( data_image
, x
, y
, 0 );
625 GdkGC
*data_gc
= gdk_gc_new( GetBitmap() );
627 gdk_draw_image( GetBitmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
629 gdk_image_destroy( data_image
);
630 gdk_gc_unref( data_gc
);
636 GdkGC
*mask_gc
= gdk_gc_new( GetMask()->GetBitmap() );
638 gdk_draw_image( GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
640 gdk_image_destroy( mask_image
);
641 gdk_gc_unref( mask_gc
);
646 // conversion to colour bitmap:
650 SetPixmap( gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, -1 ) );
652 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
654 int bpp
= visual
->depth
;
658 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800))
663 // We handle 8-bit bitmaps ourselves using the colour cube, 12-bit
664 // visuals are not supported by GDK so we do these ourselves, too.
665 // 15-bit and 16-bit should actually work and 24-bit certainly does.
667 if (!image
.HasMask() && (bpp
> 16))
669 if (!image
.HasMask() && (bpp
> 12))
672 static bool s_hasInitialized
= FALSE
;
674 if (!s_hasInitialized
)
677 s_hasInitialized
= TRUE
;
680 GdkGC
*gc
= gdk_gc_new( GetPixmap() );
682 gdk_draw_rgb_image( GetPixmap(),
694 // Create picture image
696 GdkImage
*data_image
=
697 gdk_image_new( GDK_IMAGE_FASTEST
, visual
, width
, height
);
701 GdkImage
*mask_image
= (GdkImage
*) NULL
;
705 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
707 mask_image
= gdk_image_new_bitmap( visual
, mask_data
, width
, height
);
709 wxMask
*mask
= new wxMask();
710 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
717 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
718 byte_order b_o
= RGB
;
722 if ((visual
->red_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->blue_mask
)) b_o
= RGB
;
723 else if ((visual
->red_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->green_mask
)) b_o
= RBG
;
724 else if ((visual
->blue_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->green_mask
)) b_o
= BRG
;
725 else if ((visual
->blue_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->red_mask
)) b_o
= BGR
;
726 else if ((visual
->green_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->blue_mask
)) b_o
= GRB
;
727 else if ((visual
->green_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->red_mask
)) b_o
= GBR
;
730 int r_mask
= image
.GetMaskRed();
731 int g_mask
= image
.GetMaskGreen();
732 int b_mask
= image
.GetMaskBlue();
734 unsigned char* data
= image
.GetData();
737 for (int y
= 0; y
< height
; y
++)
739 for (int x
= 0; x
< width
; x
++)
750 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
751 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
753 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
761 if (wxTheApp
->m_colorCube
)
763 pixel
= wxTheApp
->m_colorCube
[ ((r
& 0xf8) << 7) + ((g
& 0xf8) << 2) + ((b
& 0xf8) >> 3) ];
767 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
768 GdkColor
*colors
= cmap
->colors
;
769 int max
= 3 * (65536);
771 for (int i
= 0; i
< cmap
->size
; i
++)
773 int rdiff
= (r
<< 8) - colors
[i
].red
;
774 int gdiff
= (g
<< 8) - colors
[i
].green
;
775 int bdiff
= (b
<< 8) - colors
[i
].blue
;
776 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
777 if (sum
< max
) { pixel
= i
; max
= sum
; }
781 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
790 case RGB
: pixel
= ((r
& 0xf0) << 4) | (g
& 0xf0) | ((b
& 0xf0) >> 4); break;
791 case RBG
: pixel
= ((r
& 0xf0) << 4) | (b
& 0xf0) | ((g
& 0xf0) >> 4); break;
792 case GRB
: pixel
= ((g
& 0xf0) << 4) | (r
& 0xf0) | ((b
& 0xf0) >> 4); break;
793 case GBR
: pixel
= ((g
& 0xf0) << 4) | (b
& 0xf0) | ((r
& 0xf0) >> 4); break;
794 case BRG
: pixel
= ((b
& 0xf0) << 4) | (r
& 0xf0) | ((g
& 0xf0) >> 4); break;
795 case BGR
: pixel
= ((b
& 0xf0) << 4) | (g
& 0xf0) | ((r
& 0xf0) >> 4); break;
797 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
805 case RGB
: pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
806 case RBG
: pixel
= ((r
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
807 case GRB
: pixel
= ((g
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
808 case GBR
: pixel
= ((g
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
809 case BRG
: pixel
= ((b
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
810 case BGR
: pixel
= ((b
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
812 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
817 // I actually don't know if for 16-bit displays, it is alway the green
818 // component or the second component which has 6 bits.
822 case RGB
: pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
823 case RBG
: pixel
= ((r
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
824 case GRB
: pixel
= ((g
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
825 case GBR
: pixel
= ((g
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
826 case BRG
: pixel
= ((b
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
827 case BGR
: pixel
= ((b
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
829 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
838 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
839 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
840 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
841 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
842 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
843 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
845 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
854 GdkGC
*data_gc
= gdk_gc_new( GetPixmap() );
856 gdk_draw_image( GetPixmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
858 gdk_image_destroy( data_image
);
859 gdk_gc_unref( data_gc
);
865 GdkGC
*mask_gc
= gdk_gc_new( GetMask()->GetBitmap() );
867 gdk_draw_image( GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
869 gdk_image_destroy( mask_image
);
870 gdk_gc_unref( mask_gc
);
877 wxImage
wxBitmap::ConvertToImage() const
879 // the colour used as transparent one in wxImage and the one it is replaced
880 // with when it really occurs in the bitmap
881 static const int MASK_RED
= 1;
882 static const int MASK_GREEN
= 2;
883 static const int MASK_BLUE
= 3;
884 static const int MASK_BLUE_REPLACEMENT
= 2;
888 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
890 GdkImage
*gdk_image
= (GdkImage
*) NULL
;
893 gdk_image
= gdk_image_get( GetPixmap(),
895 GetWidth(), GetHeight() );
897 else if (GetBitmap())
899 gdk_image
= gdk_image_get( GetBitmap(),
901 GetWidth(), GetHeight() );
905 wxFAIL_MSG( wxT("Ill-formed bitmap") );
908 wxCHECK_MSG( gdk_image
, wxNullImage
, wxT("couldn't create image") );
910 image
.Create( GetWidth(), GetHeight() );
911 char unsigned *data
= image
.GetData();
915 gdk_image_destroy( gdk_image
);
916 wxFAIL_MSG( wxT("couldn't create image") );
920 GdkImage
*gdk_image_mask
= (GdkImage
*) NULL
;
923 gdk_image_mask
= gdk_image_get( GetMask()->GetBitmap(),
925 GetWidth(), GetHeight() );
927 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
931 int red_shift_right
= 0;
932 int green_shift_right
= 0;
933 int blue_shift_right
= 0;
934 int red_shift_left
= 0;
935 int green_shift_left
= 0;
936 int blue_shift_left
= 0;
937 bool use_shift
= FALSE
;
941 GdkVisual
*visual
= gdk_window_get_visual( GetPixmap() );
943 visual
= wxTheApp
->GetGdkVisual();
947 bpp
= visual
->red_prec
+ visual
->green_prec
+ visual
->blue_prec
;
948 red_shift_right
= visual
->red_shift
;
949 red_shift_left
= 8-visual
->red_prec
;
950 green_shift_right
= visual
->green_shift
;
951 green_shift_left
= 8-visual
->green_prec
;
952 blue_shift_right
= visual
->blue_shift
;
953 blue_shift_left
= 8-visual
->blue_prec
;
955 use_shift
= (visual
->type
== GDK_VISUAL_TRUE_COLOR
) || (visual
->type
== GDK_VISUAL_DIRECT_COLOR
);
963 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
966 for (int j
= 0; j
< GetHeight(); j
++)
968 for (int i
= 0; i
< GetWidth(); i
++)
970 wxUint32 pixel
= gdk_image_get_pixel( gdk_image
, i
, j
);
988 data
[pos
] = (pixel
>> red_shift_right
) << red_shift_left
;
989 data
[pos
+1] = (pixel
>> green_shift_right
) << green_shift_left
;
990 data
[pos
+2] = (pixel
>> blue_shift_right
) << blue_shift_left
;
992 else if (cmap
->colors
)
994 data
[pos
] = cmap
->colors
[pixel
].red
>> 8;
995 data
[pos
+1] = cmap
->colors
[pixel
].green
>> 8;
996 data
[pos
+2] = cmap
->colors
[pixel
].blue
>> 8;
1000 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
1005 int mask_pixel
= gdk_image_get_pixel( gdk_image_mask
, i
, j
);
1006 if (mask_pixel
== 0)
1008 data
[pos
] = MASK_RED
;
1009 data
[pos
+1] = MASK_GREEN
;
1010 data
[pos
+2] = MASK_BLUE
;
1012 else if ( data
[pos
] == MASK_RED
&&
1013 data
[pos
+1] == MASK_GREEN
&&
1014 data
[pos
+2] == MASK_BLUE
)
1016 data
[pos
+2] = MASK_BLUE_REPLACEMENT
;
1024 gdk_image_destroy( gdk_image
);
1025 if (gdk_image_mask
) gdk_image_destroy( gdk_image_mask
);
1030 wxBitmap::wxBitmap( const wxBitmap
& bmp
)
1036 wxBitmap::wxBitmap( const wxString
&filename
, int type
)
1038 LoadFile( filename
, type
);
1041 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int WXUNUSED(depth
))
1043 if ( width
> 0 && height
> 0 )
1045 m_refData
= new wxBitmapRefData();
1047 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
1048 M_BMPDATA
->m_bitmap
= gdk_bitmap_create_from_data
1050 wxGetRootWindow()->window
,
1055 M_BMPDATA
->m_width
= width
;
1056 M_BMPDATA
->m_height
= height
;
1057 M_BMPDATA
->m_bpp
= 1;
1059 wxASSERT_MSG( M_BMPDATA
->m_bitmap
, wxT("couldn't create bitmap") );
1063 wxBitmap::~wxBitmap()
1067 wxBitmap
& wxBitmap::operator = ( const wxBitmap
& bmp
)
1069 if ( m_refData
!= bmp
.m_refData
)
1075 bool wxBitmap::operator == ( const wxBitmap
& bmp
) const
1077 return m_refData
== bmp
.m_refData
;
1080 bool wxBitmap::operator != ( const wxBitmap
& bmp
) const
1082 return m_refData
!= bmp
.m_refData
;
1085 bool wxBitmap::Ok() const
1087 return (m_refData
!= NULL
) && (M_BMPDATA
->m_bitmap
|| M_BMPDATA
->m_pixmap
);
1090 int wxBitmap::GetHeight() const
1092 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1094 return M_BMPDATA
->m_height
;
1097 int wxBitmap::GetWidth() const
1099 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1101 return M_BMPDATA
->m_width
;
1104 int wxBitmap::GetDepth() const
1106 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1108 return M_BMPDATA
->m_bpp
;
1111 wxMask
*wxBitmap::GetMask() const
1113 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
1115 return M_BMPDATA
->m_mask
;
1118 void wxBitmap::SetMask( wxMask
*mask
)
1120 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
1122 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
1124 M_BMPDATA
->m_mask
= mask
;
1127 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
1133 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
1135 wxCHECK_MSG( Ok() &&
1136 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1137 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) && (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
1138 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
1140 wxBitmap
ret( rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
1141 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
1143 if (ret
.GetPixmap())
1145 GdkGC
*gc
= gdk_gc_new( ret
.GetPixmap() );
1146 gdk_draw_pixmap( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1147 gdk_gc_destroy( gc
);
1151 GdkGC
*gc
= gdk_gc_new( ret
.GetBitmap() );
1153 col
.pixel
= 0xFFFFFF;
1154 gdk_gc_set_foreground( gc
, &col
);
1156 gdk_gc_set_background( gc
, &col
);
1157 gdk_wx_draw_bitmap( ret
.GetBitmap(), gc
, GetBitmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1158 gdk_gc_destroy( gc
);
1163 wxMask
*mask
= new wxMask
;
1164 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, rect
.width
, rect
.height
, 1 );
1166 GdkGC
*gc
= gdk_gc_new( mask
->m_bitmap
);
1168 col
.pixel
= 0xFFFFFF;
1169 gdk_gc_set_foreground( gc
, &col
);
1171 gdk_gc_set_background( gc
, &col
);
1172 gdk_wx_draw_bitmap( mask
->m_bitmap
, gc
, M_BMPDATA
->m_mask
->m_bitmap
, rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1173 gdk_gc_destroy( gc
);
1175 ret
.SetMask( mask
);
1181 bool wxBitmap::SaveFile( const wxString
&name
, int type
, wxPalette
*WXUNUSED(palette
) )
1183 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid bitmap") );
1185 // Try to save the bitmap via wxImage handlers:
1187 wxImage image
= ConvertToImage();
1188 if (image
.Ok()) return image
.SaveFile( name
, type
);
1194 bool wxBitmap::LoadFile( const wxString
&name
, int type
)
1198 if (!wxFileExists(name
))
1201 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
1203 if (type
== wxBITMAP_TYPE_XPM
)
1205 m_refData
= new wxBitmapRefData();
1207 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
1209 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm
1211 wxGetRootWindow()->window
,
1219 M_BMPDATA
->m_mask
= new wxMask();
1220 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
1223 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
1225 M_BMPDATA
->m_bpp
= visual
->depth
;
1227 else // try if wxImage can load it
1230 if ( !image
.LoadFile( name
, type
) || !image
.Ok() )
1233 *this = wxBitmap(image
);
1239 wxPalette
*wxBitmap::GetPalette() const
1242 return (wxPalette
*) NULL
;
1244 return M_BMPDATA
->m_palette
;
1247 void wxBitmap::SetHeight( int height
)
1250 m_refData
= new wxBitmapRefData();
1252 M_BMPDATA
->m_height
= height
;
1255 void wxBitmap::SetWidth( int width
)
1258 m_refData
= new wxBitmapRefData();
1260 M_BMPDATA
->m_width
= width
;
1263 void wxBitmap::SetDepth( int depth
)
1266 m_refData
= new wxBitmapRefData();
1268 M_BMPDATA
->m_bpp
= depth
;
1271 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
1274 m_refData
= new wxBitmapRefData();
1276 M_BMPDATA
->m_pixmap
= pixmap
;
1279 void wxBitmap::SetBitmap( GdkPixmap
*bitmap
)
1282 m_refData
= new wxBitmapRefData();
1284 M_BMPDATA
->m_bitmap
= bitmap
;
1287 GdkPixmap
*wxBitmap::GetPixmap() const
1289 wxCHECK_MSG( Ok(), (GdkPixmap
*) NULL
, wxT("invalid bitmap") );
1291 return M_BMPDATA
->m_pixmap
;
1294 GdkBitmap
*wxBitmap::GetBitmap() const
1296 wxCHECK_MSG( Ok(), (GdkBitmap
*) NULL
, wxT("invalid bitmap") );
1298 return M_BMPDATA
->m_bitmap
;