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 wxBitmapRefData(const wxBitmapRefData
& data
);
232 bool Create(int width
, int height
, int bpp
);
233 virtual ~wxBitmapRefData();
242 wxPalette
*m_palette
;
243 #endif // wxUSE_PALETTE
246 wxBitmapRefData::wxBitmapRefData()
248 m_pixmap
= (GdkPixmap
*) NULL
;
249 m_bitmap
= (GdkBitmap
*) NULL
;
250 m_mask
= (wxMask
*) NULL
;
255 m_palette
= (wxPalette
*) NULL
;
256 #endif // wxUSE_PALETTE
259 wxBitmapRefData::wxBitmapRefData(const wxBitmapRefData
& data
)
261 Create(data
.m_width
, data
.m_height
, data
.m_bpp
);
263 m_mask
= data
.m_mask
? new wxMask(*data
.m_mask
) : NULL
;
266 wxASSERT_MSG( !data
.m_palette
,
267 _T("copying bitmaps palette not implemented") );
268 #endif // wxUSE_PALETTE
271 // copy the bitmap data by simply drawing the source bitmap on this one
277 else if ( data
.m_bitmap
)
281 else // invalid bitmap?
286 GdkGC
*gc
= gdk_gc_new(*dst
);
289 gdk_wx_draw_bitmap(m_bitmap
, gc
, data
.m_bitmap
, 0, 0, 0, 0, -1, -1);
291 else // colour pixmap
293 gdk_draw_pixmap(m_pixmap
, gc
, data
.m_pixmap
, 0, 0, 0, 0, -1, -1);
299 bool wxBitmapRefData::Create(int width
, int height
, int bpp
)
310 // to understand how this compiles you should know that GdkPixmap and
311 // GdkBitmap are one and the same type in GTK+ 1
315 const GdkVisual
* const visual
= wxTheApp
->GetGdkVisual();
317 wxCHECK_MSG( (bpp
== -1) || (bpp
== visual
->depth
) || (bpp
== 32), false,
318 wxT("invalid bitmap depth") );
320 m_bpp
= visual
->depth
;
331 *ppix
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, m_bpp
);
333 return *ppix
!= NULL
;
336 wxBitmapRefData::~wxBitmapRefData()
339 gdk_pixmap_unref( m_pixmap
);
341 gdk_bitmap_unref( m_bitmap
);
345 #endif // wxUSE_PALETTE
348 //-----------------------------------------------------------------------------
350 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
352 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
358 wxBitmap::wxBitmap( int width
, int height
, int depth
)
360 Create( width
, height
, depth
);
363 wxObjectRefData
*wxBitmap::CreateRefData() const
365 return new wxBitmapRefData
;
368 wxObjectRefData
*wxBitmap::CloneRefData(const wxObjectRefData
*data
) const
370 return new wxBitmapRefData(*wx_static_cast(const wxBitmapRefData
*, data
));
373 bool wxBitmap::Create( int width
, int height
, int depth
)
377 if ( width
<= 0 || height
<= 0 )
382 m_refData
= new wxBitmapRefData();
383 return M_BMPDATA
->Create(width
, height
, depth
);
386 wxBitmap::wxBitmap(const char* const* bits
)
388 wxCHECK2_MSG(bits
!= NULL
, return, wxT("invalid bitmap data"));
390 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
392 m_refData
= new wxBitmapRefData();
394 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
396 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm_d( wxGetRootWindow()->window
, &mask
, NULL
, (gchar
**) bits
);
398 wxCHECK2_MSG(M_BMPDATA
->m_pixmap
, return, wxT("couldn't create pixmap"));
402 M_BMPDATA
->m_mask
= new wxMask();
403 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
406 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
408 M_BMPDATA
->m_bpp
= visual
->depth
; // Can we get a different depth from create_from_xpm_d() ?
411 wxBitmap
wxBitmap::Rescale( int clipx
, int clipy
, int clipwidth
, int clipheight
, int newx
, int newy
)
413 wxCHECK_MSG( Ok(), wxNullBitmap
, wxT("invalid bitmap") );
415 if (newy
==M_BMPDATA
->m_width
&& newy
==M_BMPDATA
->m_height
)
418 int width
= wxMax(newx
, 1);
419 int height
= wxMax(newy
, 1);
420 width
= wxMin(width
, clipwidth
);
421 height
= wxMin(height
, clipheight
);
425 GdkImage
*img
= (GdkImage
*) NULL
;
427 img
= gdk_image_get( GetPixmap(), 0, 0, GetWidth(), GetHeight() );
428 else if (GetBitmap())
429 img
= gdk_image_get( GetBitmap(), 0, 0, GetWidth(), GetHeight() );
431 wxFAIL_MSG( wxT("Ill-formed bitmap") );
433 wxCHECK_MSG( img
, wxNullBitmap
, wxT("couldn't create image") );
439 GdkPixmap
*dstpix
= NULL
;
442 GdkVisual
*visual
= gdk_window_get_visual( GetPixmap() );
444 visual
= wxTheApp
->GetGdkVisual();
447 bmp
= wxBitmap(width
,height
,bpp
);
448 dstpix
= bmp
.GetPixmap();
449 gc
= gdk_gc_new( dstpix
);
453 long dstbyteperline
= 0;
458 dstbyteperline
= width
/8*M_BMPDATA
->m_bpp
;
459 if (width
*M_BMPDATA
->m_bpp
% 8 != 0)
461 dst
= (char*) malloc(dstbyteperline
*height
);
464 // be careful to use the right scaling factor
465 float scx
= (float)M_BMPDATA
->m_width
/(float)newx
;
466 float scy
= (float)M_BMPDATA
->m_height
/(float)newy
;
467 // prepare accel-tables
468 int *tablex
= (int *)calloc(width
,sizeof(int));
469 int *tabley
= (int *)calloc(height
,sizeof(int));
471 // accel table filled with clipped values
472 for (int x
= 0; x
< width
; x
++)
473 tablex
[x
] = (int) (scx
* (x
+clipx
));
474 for (int y
= 0; y
< height
; y
++)
475 tabley
[y
] = (int) (scy
* (y
+clipy
));
477 // Main rescaling routine starts here
478 for (int h
= 0; h
< height
; h
++)
482 guint32 old_pixval
= 0;
484 for (int w
= 0; w
< width
; w
++)
492 pixval
= gdk_image_get_pixel( img
, x
, tabley
[h
] );
502 char shift
= bit
<< (w
% 8);
508 dst
[h
*dstbyteperline
+w
/8] = outbyte
;
516 gdk_gc_set_foreground( gc
, &col
);
517 gdk_draw_point( dstpix
, gc
, w
, h
);
521 // do not forget the last byte
522 if ((bpp
== 1) && (width
% 8 != 0))
523 dst
[h
*dstbyteperline
+width
/8] = outbyte
;
526 gdk_image_destroy( img
);
527 if (gc
) gdk_gc_unref( gc
);
531 bmp
= wxBitmap( (const char *)dst
, width
, height
, 1 );
537 dstbyteperline
= width
/8;
540 dst
= (char*) malloc(dstbyteperline
*height
);
541 img
= gdk_image_get( GetMask()->GetBitmap(), 0, 0, GetWidth(), GetHeight() );
543 for (int h
= 0; h
< height
; h
++)
547 guint32 old_pixval
= 0;
549 for (int w
= 0; w
< width
; w
++)
557 pixval
= gdk_image_get_pixel( img
, x
, tabley
[h
] );
565 char shift
= bit
<< (w
% 8);
571 dst
[h
*dstbyteperline
+w
/8] = outbyte
;
576 // do not forget the last byte
578 dst
[h
*dstbyteperline
+width
/8] = outbyte
;
580 wxMask
* mask
= new wxMask
;
581 mask
->m_bitmap
= gdk_bitmap_create_from_data( wxGetRootWindow()->window
, (gchar
*) dst
, width
, height
);
585 gdk_image_destroy( img
);
594 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
598 wxCHECK_MSG( image
.Ok(), false, wxT("invalid image") );
599 wxCHECK_MSG( depth
== -1 || depth
== 1, false, wxT("invalid bitmap depth") );
601 if (image
.GetWidth() <= 0 || image
.GetHeight() <= 0)
604 m_refData
= new wxBitmapRefData();
608 return CreateFromImageAsBitmap(image
);
612 return CreateFromImageAsPixmap(image
);
616 // conversion to mono bitmap:
617 bool wxBitmap::CreateFromImageAsBitmap(const wxImage
& img
)
619 // convert alpha channel to mask, if it is present:
621 image
.ConvertAlphaToMask();
623 int width
= image
.GetWidth();
624 int height
= image
.GetHeight();
629 SetBitmap( gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 ) );
633 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
635 // Create picture image
637 unsigned char *data_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
639 GdkImage
*data_image
=
640 gdk_image_new_bitmap( visual
, data_data
, width
, height
);
644 GdkImage
*mask_image
= (GdkImage
*) NULL
;
648 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
650 mask_image
= gdk_image_new_bitmap( visual
, mask_data
, width
, height
);
652 wxMask
*mask
= new wxMask();
653 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
658 int r_mask
= image
.GetMaskRed();
659 int g_mask
= image
.GetMaskGreen();
660 int b_mask
= image
.GetMaskBlue();
662 unsigned char* data
= image
.GetData();
665 for (int y
= 0; y
< height
; y
++)
667 for (int x
= 0; x
< width
; x
++)
678 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
679 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
681 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
684 if ((r
== 255) && (b
== 255) && (g
== 255))
685 gdk_image_put_pixel( data_image
, x
, y
, 1 );
687 gdk_image_put_pixel( data_image
, x
, y
, 0 );
694 GdkGC
*data_gc
= gdk_gc_new( GetBitmap() );
696 gdk_draw_image( GetBitmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
698 gdk_image_destroy( data_image
);
699 gdk_gc_unref( data_gc
);
705 GdkGC
*mask_gc
= gdk_gc_new( GetMask()->GetBitmap() );
707 gdk_draw_image( GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
709 gdk_image_destroy( mask_image
);
710 gdk_gc_unref( mask_gc
);
716 // conversion to colour bitmap:
717 bool wxBitmap::CreateFromImageAsPixmap(const wxImage
& img
)
719 // convert alpha channel to mask, if it is present:
721 image
.ConvertAlphaToMask();
723 int width
= image
.GetWidth();
724 int height
= image
.GetHeight();
729 SetPixmap( gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, -1 ) );
731 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
733 int bpp
= visual
->depth
;
737 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800))
742 // We handle 8-bit bitmaps ourselves using the colour cube, 12-bit
743 // visuals are not supported by GDK so we do these ourselves, too.
744 // 15-bit and 16-bit should actually work and 24-bit certainly does.
746 if (!image
.HasMask() && (bpp
> 16))
748 if (!image
.HasMask() && (bpp
> 12))
751 static bool s_hasInitialized
= false;
753 if (!s_hasInitialized
)
756 s_hasInitialized
= true;
759 GdkGC
*gc
= gdk_gc_new( GetPixmap() );
761 gdk_draw_rgb_image( GetPixmap(),
773 // Create picture image
775 GdkImage
*data_image
=
776 gdk_image_new( GDK_IMAGE_FASTEST
, visual
, width
, height
);
780 GdkImage
*mask_image
= (GdkImage
*) NULL
;
784 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
786 mask_image
= gdk_image_new_bitmap( visual
, mask_data
, width
, height
);
788 wxMask
*mask
= new wxMask();
789 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
796 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
797 byte_order b_o
= RGB
;
801 if ((visual
->red_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->blue_mask
)) b_o
= RGB
;
802 else if ((visual
->red_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->green_mask
)) b_o
= RBG
;
803 else if ((visual
->blue_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->green_mask
)) b_o
= BRG
;
804 else if ((visual
->blue_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->red_mask
)) b_o
= BGR
;
805 else if ((visual
->green_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->blue_mask
)) b_o
= GRB
;
806 else if ((visual
->green_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->red_mask
)) b_o
= GBR
;
809 int r_mask
= image
.GetMaskRed();
810 int g_mask
= image
.GetMaskGreen();
811 int b_mask
= image
.GetMaskBlue();
813 unsigned char* data
= image
.GetData();
816 for (int y
= 0; y
< height
; y
++)
818 for (int x
= 0; x
< width
; x
++)
829 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
830 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
832 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
840 if (wxTheApp
->m_colorCube
)
842 pixel
= wxTheApp
->m_colorCube
[ ((r
& 0xf8) << 7) + ((g
& 0xf8) << 2) + ((b
& 0xf8) >> 3) ];
846 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
847 GdkColor
*colors
= cmap
->colors
;
848 int max
= 3 * (65536);
850 for (int i
= 0; i
< cmap
->size
; i
++)
852 int rdiff
= (r
<< 8) - colors
[i
].red
;
853 int gdiff
= (g
<< 8) - colors
[i
].green
;
854 int bdiff
= (b
<< 8) - colors
[i
].blue
;
855 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
856 if (sum
< max
) { pixel
= i
; max
= sum
; }
860 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
869 case RGB
: pixel
= ((r
& 0xf0) << 4) | (g
& 0xf0) | ((b
& 0xf0) >> 4); break;
870 case RBG
: pixel
= ((r
& 0xf0) << 4) | (b
& 0xf0) | ((g
& 0xf0) >> 4); break;
871 case GRB
: pixel
= ((g
& 0xf0) << 4) | (r
& 0xf0) | ((b
& 0xf0) >> 4); break;
872 case GBR
: pixel
= ((g
& 0xf0) << 4) | (b
& 0xf0) | ((r
& 0xf0) >> 4); break;
873 case BRG
: pixel
= ((b
& 0xf0) << 4) | (r
& 0xf0) | ((g
& 0xf0) >> 4); break;
874 case BGR
: pixel
= ((b
& 0xf0) << 4) | (g
& 0xf0) | ((r
& 0xf0) >> 4); break;
876 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
884 case RGB
: pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
885 case RBG
: pixel
= ((r
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
886 case GRB
: pixel
= ((g
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
887 case GBR
: pixel
= ((g
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
888 case BRG
: pixel
= ((b
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
889 case BGR
: pixel
= ((b
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
891 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
896 // I actually don't know if for 16-bit displays, it is alway the green
897 // component or the second component which has 6 bits.
901 case RGB
: pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
902 case RBG
: pixel
= ((r
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
903 case GRB
: pixel
= ((g
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
904 case GBR
: pixel
= ((g
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
905 case BRG
: pixel
= ((b
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
906 case BGR
: pixel
= ((b
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
908 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
917 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
918 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
919 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
920 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
921 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
922 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
924 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
934 GdkGC
*data_gc
= gdk_gc_new( GetPixmap() );
936 gdk_draw_image( GetPixmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
938 gdk_image_destroy( data_image
);
939 gdk_gc_unref( data_gc
);
945 GdkGC
*mask_gc
= gdk_gc_new( GetMask()->GetBitmap() );
947 gdk_draw_image( GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
949 gdk_image_destroy( mask_image
);
950 gdk_gc_unref( mask_gc
);
956 wxImage
wxBitmap::ConvertToImage() const
960 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
962 image
.Create(GetWidth(), GetHeight());
963 unsigned char *data
= image
.GetData();
967 wxFAIL_MSG( wxT("couldn't create image") );
971 // the colour used as transparent one in wxImage and the one it is
972 // replaced with when it really occurs in the bitmap
973 static const int MASK_RED
= 1;
974 static const int MASK_GREEN
= 2;
975 static const int MASK_BLUE
= 3;
976 static const int MASK_BLUE_REPLACEMENT
= 2;
978 GdkImage
*gdk_image
= (GdkImage
*) NULL
;
982 gdk_image
= gdk_image_get( GetPixmap(),
984 GetWidth(), GetHeight() );
986 else if (GetBitmap())
988 gdk_image
= gdk_image_get( GetBitmap(),
990 GetWidth(), GetHeight() );
994 wxFAIL_MSG( wxT("Ill-formed bitmap") );
997 wxCHECK_MSG( gdk_image
, wxNullImage
, wxT("couldn't create image") );
999 GdkImage
*gdk_image_mask
= (GdkImage
*) NULL
;
1002 gdk_image_mask
= gdk_image_get( GetMask()->GetBitmap(),
1004 GetWidth(), GetHeight() );
1006 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
1010 int red_shift_right
= 0;
1011 int green_shift_right
= 0;
1012 int blue_shift_right
= 0;
1013 int red_shift_left
= 0;
1014 int green_shift_left
= 0;
1015 int blue_shift_left
= 0;
1016 bool use_shift
= false;
1020 GdkVisual
*visual
= gdk_window_get_visual( GetPixmap() );
1022 visual
= wxTheApp
->GetGdkVisual();
1024 bpp
= visual
->depth
;
1026 bpp
= visual
->red_prec
+ visual
->green_prec
+ visual
->blue_prec
;
1027 red_shift_right
= visual
->red_shift
;
1028 red_shift_left
= 8-visual
->red_prec
;
1029 green_shift_right
= visual
->green_shift
;
1030 green_shift_left
= 8-visual
->green_prec
;
1031 blue_shift_right
= visual
->blue_shift
;
1032 blue_shift_left
= 8-visual
->blue_prec
;
1034 use_shift
= (visual
->type
== GDK_VISUAL_TRUE_COLOR
) || (visual
->type
== GDK_VISUAL_DIRECT_COLOR
);
1042 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
1045 for (int j
= 0; j
< GetHeight(); j
++)
1047 for (int i
= 0; i
< GetWidth(); i
++)
1049 wxUint32 pixel
= gdk_image_get_pixel( gdk_image
, i
, j
);
1067 data
[pos
] = (pixel
>> red_shift_right
) << red_shift_left
;
1068 data
[pos
+1] = (pixel
>> green_shift_right
) << green_shift_left
;
1069 data
[pos
+2] = (pixel
>> blue_shift_right
) << blue_shift_left
;
1071 else if (cmap
->colors
)
1073 data
[pos
] = cmap
->colors
[pixel
].red
>> 8;
1074 data
[pos
+1] = cmap
->colors
[pixel
].green
>> 8;
1075 data
[pos
+2] = cmap
->colors
[pixel
].blue
>> 8;
1079 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
1084 int mask_pixel
= gdk_image_get_pixel( gdk_image_mask
, i
, j
);
1085 if (mask_pixel
== 0)
1087 data
[pos
] = MASK_RED
;
1088 data
[pos
+1] = MASK_GREEN
;
1089 data
[pos
+2] = MASK_BLUE
;
1091 else if ( data
[pos
] == MASK_RED
&&
1092 data
[pos
+1] == MASK_GREEN
&&
1093 data
[pos
+2] == MASK_BLUE
)
1095 data
[pos
+2] = MASK_BLUE_REPLACEMENT
;
1103 gdk_image_destroy( gdk_image
);
1104 if (gdk_image_mask
) gdk_image_destroy( gdk_image_mask
);
1109 wxBitmap::wxBitmap( const wxString
&filename
, wxBitmapType type
)
1111 LoadFile( filename
, type
);
1114 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int WXUNUSED(depth
))
1116 if ( width
> 0 && height
> 0 )
1118 m_refData
= new wxBitmapRefData();
1120 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
1121 M_BMPDATA
->m_bitmap
= gdk_bitmap_create_from_data
1123 wxGetRootWindow()->window
,
1128 M_BMPDATA
->m_width
= width
;
1129 M_BMPDATA
->m_height
= height
;
1130 M_BMPDATA
->m_bpp
= 1;
1132 wxASSERT_MSG( M_BMPDATA
->m_bitmap
, wxT("couldn't create bitmap") );
1136 wxBitmap::~wxBitmap()
1140 bool wxBitmap::IsOk() const
1142 return (m_refData
!= NULL
) &&
1143 (M_BMPDATA
->m_bitmap
|| M_BMPDATA
->m_pixmap
);
1146 int wxBitmap::GetHeight() const
1148 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1150 return M_BMPDATA
->m_height
;
1153 int wxBitmap::GetWidth() const
1155 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1157 return M_BMPDATA
->m_width
;
1160 int wxBitmap::GetDepth() const
1162 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1164 return M_BMPDATA
->m_bpp
;
1167 wxMask
*wxBitmap::GetMask() const
1169 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
1171 return M_BMPDATA
->m_mask
;
1174 void wxBitmap::SetMask( wxMask
*mask
)
1176 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
1179 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
1181 M_BMPDATA
->m_mask
= mask
;
1184 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
1190 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
1192 wxCHECK_MSG( Ok() &&
1193 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1194 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) && (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
1195 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
1197 wxBitmap
ret( rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
1198 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
1200 if (ret
.GetPixmap())
1202 GdkGC
*gc
= gdk_gc_new( ret
.GetPixmap() );
1203 gdk_draw_pixmap( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1204 gdk_gc_destroy( gc
);
1208 GdkGC
*gc
= gdk_gc_new( ret
.GetBitmap() );
1210 col
.pixel
= 0xFFFFFF;
1211 gdk_gc_set_foreground( gc
, &col
);
1213 gdk_gc_set_background( gc
, &col
);
1214 gdk_wx_draw_bitmap( ret
.GetBitmap(), gc
, GetBitmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1215 gdk_gc_destroy( gc
);
1220 wxMask
*mask
= new wxMask
;
1221 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, rect
.width
, rect
.height
, 1 );
1223 GdkGC
*gc
= gdk_gc_new( mask
->m_bitmap
);
1225 col
.pixel
= 0xFFFFFF;
1226 gdk_gc_set_foreground( gc
, &col
);
1228 gdk_gc_set_background( gc
, &col
);
1229 gdk_wx_draw_bitmap( mask
->m_bitmap
, gc
, M_BMPDATA
->m_mask
->m_bitmap
, rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1230 gdk_gc_destroy( gc
);
1232 ret
.SetMask( mask
);
1238 bool wxBitmap::SaveFile( const wxString
&name
, wxBitmapType type
, const wxPalette
*WXUNUSED(palette
) ) const
1240 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1242 // Try to save the bitmap via wxImage handlers:
1244 wxImage image
= ConvertToImage();
1245 if (image
.Ok()) return image
.SaveFile( name
, type
);
1251 bool wxBitmap::LoadFile( const wxString
&name
, wxBitmapType type
)
1255 if (!wxFileExists(name
))
1258 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
1260 if (type
== wxBITMAP_TYPE_XPM
)
1262 m_refData
= new wxBitmapRefData();
1264 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
1266 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm
1268 wxGetRootWindow()->window
,
1276 M_BMPDATA
->m_mask
= new wxMask();
1277 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
1280 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
1282 M_BMPDATA
->m_bpp
= visual
->depth
;
1284 else // try if wxImage can load it
1287 if ( !image
.LoadFile( name
, type
) || !image
.Ok() )
1290 *this = wxBitmap(image
);
1297 wxPalette
*wxBitmap::GetPalette() const
1300 return (wxPalette
*) NULL
;
1302 return M_BMPDATA
->m_palette
;
1305 void wxBitmap::SetPalette(const wxPalette
& WXUNUSED(palette
))
1309 #endif // wxUSE_PALETTE
1311 void wxBitmap::SetHeight( int height
)
1314 M_BMPDATA
->m_height
= height
;
1317 void wxBitmap::SetWidth( int width
)
1320 M_BMPDATA
->m_width
= width
;
1323 void wxBitmap::SetDepth( int depth
)
1326 M_BMPDATA
->m_bpp
= depth
;
1329 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
1332 m_refData
= new wxBitmapRefData();
1334 M_BMPDATA
->m_pixmap
= pixmap
;
1337 void wxBitmap::SetBitmap( GdkPixmap
*bitmap
)
1340 m_refData
= new wxBitmapRefData();
1342 M_BMPDATA
->m_bitmap
= bitmap
;
1345 GdkPixmap
*wxBitmap::GetPixmap() const
1347 wxCHECK_MSG( Ok(), (GdkPixmap
*) NULL
, wxT("invalid bitmap") );
1349 return M_BMPDATA
->m_pixmap
;
1352 bool wxBitmap::HasPixmap() const
1354 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1356 return M_BMPDATA
->m_pixmap
!= NULL
;
1359 GdkBitmap
*wxBitmap::GetBitmap() const
1361 wxCHECK_MSG( Ok(), (GdkBitmap
*) NULL
, wxT("invalid bitmap") );
1363 return M_BMPDATA
->m_bitmap
;
1366 void *wxBitmap::GetRawData(wxPixelDataBase
& WXUNUSED(data
), int WXUNUSED(bpp
))
1371 void wxBitmap::UngetRawData(wxPixelDataBase
& WXUNUSED(data
))
1375 bool wxBitmap::HasAlpha() const
1380 //-----------------------------------------------------------------------------
1382 //-----------------------------------------------------------------------------
1384 IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler
, wxBitmapHandlerBase
)
1386 /* static */ void wxBitmap::InitStandardHandlers()
1388 // TODO: Insert handler based on GdkPixbufs handler later