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
249 wxPalette
*m_palette
;
252 wxBitmapRefData::wxBitmapRefData()
254 m_pixmap
= (GdkPixmap
*) NULL
;
255 m_bitmap
= (GdkBitmap
*) NULL
;
257 m_pixbuf
= (GdkPixbuf
*) NULL
;
259 m_mask
= (wxMask
*) NULL
;
263 m_palette
= (wxPalette
*) NULL
;
266 wxBitmapRefData::~wxBitmapRefData()
269 gdk_pixmap_unref( m_pixmap
);
271 gdk_bitmap_unref( m_bitmap
);
274 gdk_pixbuf_unref( m_pixbuf
);
280 //-----------------------------------------------------------------------------
282 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
284 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
290 wxBitmap::wxBitmap( int width
, int height
, int depth
)
292 Create( width
, height
, depth
);
295 bool wxBitmap::Create( int width
, int height
, int depth
)
299 if ( width
<= 0 || height
<= 0 )
304 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
307 depth
= visual
->depth
;
309 wxCHECK_MSG( (depth
== visual
->depth
) || (depth
== 1), FALSE
,
310 wxT("invalid bitmap depth") )
312 m_refData
= new wxBitmapRefData();
313 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
314 M_BMPDATA
->m_width
= width
;
315 M_BMPDATA
->m_height
= height
;
318 M_BMPDATA
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
319 M_BMPDATA
->m_bpp
= 1;
323 M_BMPDATA
->m_pixmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, depth
);
324 M_BMPDATA
->m_bpp
= visual
->depth
;
330 bool wxBitmap::CreateFromXpm( const char **bits
)
334 wxCHECK_MSG( bits
!= NULL
, FALSE
, wxT("invalid bitmap data") )
336 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
338 m_refData
= new wxBitmapRefData();
340 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
342 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm_d( wxGetRootWindow()->window
, &mask
, NULL
, (gchar
**) bits
);
344 wxCHECK_MSG( M_BMPDATA
->m_pixmap
, FALSE
, wxT("couldn't create pixmap") );
348 M_BMPDATA
->m_mask
= new wxMask();
349 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
352 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
354 M_BMPDATA
->m_bpp
= visual
->depth
; // Can we get a different depth from create_from_xpm_d() ?
359 wxBitmap
wxBitmap::Rescale( int clipx
, int clipy
, int clipwidth
, int clipheight
, int newx
, int newy
)
361 wxCHECK_MSG( Ok(), wxNullBitmap
, wxT("invalid bitmap") );
363 if (newy
==M_BMPDATA
->m_width
&& newy
==M_BMPDATA
->m_height
)
366 int width
= wxMax(newx
, 1);
367 int height
= wxMax(newy
, 1);
368 width
= wxMin(width
, clipwidth
);
369 height
= wxMin(height
, clipheight
);
377 bmp
.SetHeight(height
);
378 bmp
.SetDepth(GetDepth());
379 bmp
.SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB
, TRUE
/*has_alpha*/,
381 gdk_pixbuf_scale(M_BMPDATA
->m_pixbuf
, bmp
.GetPixbuf(),
384 (double)newx
/GetWidth(), (double)newy
/GetHeight(),
385 GDK_INTERP_BILINEAR
);
388 #endif // __WXGTK20__
390 GdkImage
*img
= (GdkImage
*) NULL
;
392 img
= gdk_image_get( GetPixmap(), 0, 0, GetWidth(), GetHeight() );
393 else if (GetBitmap())
394 img
= gdk_image_get( GetBitmap(), 0, 0, GetWidth(), GetHeight() );
396 wxFAIL_MSG( wxT("Ill-formed bitmap") );
398 wxCHECK_MSG( img
, wxNullBitmap
, wxT("couldn't create image") );
404 GdkPixmap
*dstpix
= NULL
;
407 GdkVisual
*visual
= gdk_window_get_visual( GetPixmap() );
409 visual
= wxTheApp
->GetGdkVisual();
412 bmp
= wxBitmap(width
,height
,bpp
);
413 dstpix
= bmp
.GetPixmap();
414 gc
= gdk_gc_new( dstpix
);
418 long dstbyteperline
= 0;
423 dstbyteperline
= width
/8*M_BMPDATA
->m_bpp
;
424 if (width
*M_BMPDATA
->m_bpp
% 8 != 0)
426 dst
= (char*) malloc(dstbyteperline
*height
);
429 // be careful to use the right scaling factor
430 float scx
= (float)M_BMPDATA
->m_width
/(float)newx
;
431 float scy
= (float)M_BMPDATA
->m_height
/(float)newy
;
432 // prepare accel-tables
433 int *tablex
= (int *)calloc(width
,sizeof(int));
434 int *tabley
= (int *)calloc(height
,sizeof(int));
436 // accel table filled with clipped values
437 for (int x
= 0; x
< width
; x
++)
438 tablex
[x
] = (int) (scx
* (x
+clipx
));
439 for (int y
= 0; y
< height
; y
++)
440 tabley
[y
] = (int) (scy
* (y
+clipy
));
442 // Main rescaling routine starts here
443 for (int h
= 0; h
< height
; h
++)
447 guint32 old_pixval
= 0;
449 for (int w
= 0; w
< width
; w
++)
457 pixval
= gdk_image_get_pixel( img
, x
, tabley
[h
] );
467 char shift
= bit
<< w
% 8;
473 dst
[h
*dstbyteperline
+w
/8] = outbyte
;
481 gdk_gc_set_foreground( gc
, &col
);
482 gdk_draw_point( dstpix
, gc
, w
, h
);
486 // do not forget the last byte
487 if ((bpp
== 1) && (width
% 8 != 0))
488 dst
[h
*dstbyteperline
+width
/8] = outbyte
;
491 gdk_image_destroy( img
);
492 if (gc
) gdk_gc_unref( gc
);
496 bmp
= wxBitmap( (const char *)dst
, width
, height
, 1 );
502 dstbyteperline
= width
/8;
505 dst
= (char*) malloc(dstbyteperline
*height
);
506 img
= gdk_image_get( GetMask()->GetBitmap(), 0, 0, GetWidth(), GetHeight() );
508 for (int h
= 0; h
< height
; h
++)
512 guint32 old_pixval
= 0;
514 for (int w
= 0; w
< width
; w
++)
522 pixval
= gdk_image_get_pixel( img
, x
, tabley
[h
] );
530 char shift
= bit
<< w
% 8;
536 dst
[h
*dstbyteperline
+w
/8] = outbyte
;
541 // do not forget the last byte
543 dst
[h
*dstbyteperline
+width
/8] = outbyte
;
545 wxMask
* mask
= new wxMask
;
546 mask
->m_bitmap
= gdk_bitmap_create_from_data( wxGetRootWindow()->window
, (gchar
*) dst
, width
, height
);
550 gdk_image_destroy( img
);
560 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
564 wxCHECK_MSG( image
.Ok(), FALSE
, wxT("invalid image") )
565 wxCHECK_MSG( depth
== -1 || depth
== 1, FALSE
, wxT("invalid bitmap depth") )
567 if (image
.GetWidth() <= 0 || image
.GetHeight() <= 0)
570 m_refData
= new wxBitmapRefData();
574 return CreateFromImageAsBitmap(image
);
579 if (image
.HasAlpha())
580 return CreateFromImageAsPixbuf(image
);
582 return CreateFromImageAsPixmap(image
);
586 // conversion to mono bitmap:
587 bool wxBitmap::CreateFromImageAsBitmap(const wxImage
& img
)
589 // convert alpha channel to mask, if it is present:
591 image
.ConvertAlphaToMask();
593 int width
= image
.GetWidth();
594 int height
= image
.GetHeight();
599 SetBitmap( gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 ) );
603 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
605 // Create picture image
607 unsigned char *data_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
609 GdkImage
*data_image
=
610 gdk_image_new_bitmap( visual
, data_data
, width
, height
);
614 GdkImage
*mask_image
= (GdkImage
*) NULL
;
618 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
620 mask_image
= gdk_image_new_bitmap( visual
, mask_data
, width
, height
);
622 wxMask
*mask
= new wxMask();
623 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
628 int r_mask
= image
.GetMaskRed();
629 int g_mask
= image
.GetMaskGreen();
630 int b_mask
= image
.GetMaskBlue();
632 unsigned char* data
= image
.GetData();
635 for (int y
= 0; y
< height
; y
++)
637 for (int x
= 0; x
< width
; x
++)
648 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
649 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
651 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
654 if ((r
== 255) && (b
== 255) && (g
== 255))
655 gdk_image_put_pixel( data_image
, x
, y
, 1 );
657 gdk_image_put_pixel( data_image
, x
, y
, 0 );
664 GdkGC
*data_gc
= gdk_gc_new( GetBitmap() );
666 gdk_draw_image( GetBitmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
668 gdk_image_destroy( data_image
);
669 gdk_gc_unref( data_gc
);
675 GdkGC
*mask_gc
= gdk_gc_new( GetMask()->GetBitmap() );
677 gdk_draw_image( GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
679 gdk_image_destroy( mask_image
);
680 gdk_gc_unref( mask_gc
);
686 // conversion to colour bitmap:
687 bool wxBitmap::CreateFromImageAsPixmap(const wxImage
& img
)
689 // convert alpha channel to mask, if it is present:
691 image
.ConvertAlphaToMask();
693 int width
= image
.GetWidth();
694 int height
= image
.GetHeight();
699 SetPixmap( gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, -1 ) );
701 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
703 int bpp
= visual
->depth
;
707 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800))
712 // We handle 8-bit bitmaps ourselves using the colour cube, 12-bit
713 // visuals are not supported by GDK so we do these ourselves, too.
714 // 15-bit and 16-bit should actually work and 24-bit certainly does.
716 if (!image
.HasMask() && (bpp
> 16))
718 if (!image
.HasMask() && (bpp
> 12))
721 static bool s_hasInitialized
= FALSE
;
723 if (!s_hasInitialized
)
726 s_hasInitialized
= TRUE
;
729 GdkGC
*gc
= gdk_gc_new( GetPixmap() );
731 gdk_draw_rgb_image( GetPixmap(),
743 // Create picture image
745 GdkImage
*data_image
=
746 gdk_image_new( GDK_IMAGE_FASTEST
, visual
, width
, height
);
750 GdkImage
*mask_image
= (GdkImage
*) NULL
;
754 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
756 mask_image
= gdk_image_new_bitmap( visual
, mask_data
, width
, height
);
758 wxMask
*mask
= new wxMask();
759 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
766 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
767 byte_order b_o
= RGB
;
771 if ((visual
->red_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->blue_mask
)) b_o
= RGB
;
772 else if ((visual
->red_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->green_mask
)) b_o
= RBG
;
773 else if ((visual
->blue_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->green_mask
)) b_o
= BRG
;
774 else if ((visual
->blue_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->red_mask
)) b_o
= BGR
;
775 else if ((visual
->green_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->blue_mask
)) b_o
= GRB
;
776 else if ((visual
->green_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->red_mask
)) b_o
= GBR
;
779 int r_mask
= image
.GetMaskRed();
780 int g_mask
= image
.GetMaskGreen();
781 int b_mask
= image
.GetMaskBlue();
783 unsigned char* data
= image
.GetData();
786 for (int y
= 0; y
< height
; y
++)
788 for (int x
= 0; x
< width
; x
++)
799 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
800 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
802 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
810 if (wxTheApp
->m_colorCube
)
812 pixel
= wxTheApp
->m_colorCube
[ ((r
& 0xf8) << 7) + ((g
& 0xf8) << 2) + ((b
& 0xf8) >> 3) ];
816 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
817 GdkColor
*colors
= cmap
->colors
;
818 int max
= 3 * (65536);
820 for (int i
= 0; i
< cmap
->size
; i
++)
822 int rdiff
= (r
<< 8) - colors
[i
].red
;
823 int gdiff
= (g
<< 8) - colors
[i
].green
;
824 int bdiff
= (b
<< 8) - colors
[i
].blue
;
825 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
826 if (sum
< max
) { pixel
= i
; max
= sum
; }
830 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
839 case RGB
: pixel
= ((r
& 0xf0) << 4) | (g
& 0xf0) | ((b
& 0xf0) >> 4); break;
840 case RBG
: pixel
= ((r
& 0xf0) << 4) | (b
& 0xf0) | ((g
& 0xf0) >> 4); break;
841 case GRB
: pixel
= ((g
& 0xf0) << 4) | (r
& 0xf0) | ((b
& 0xf0) >> 4); break;
842 case GBR
: pixel
= ((g
& 0xf0) << 4) | (b
& 0xf0) | ((r
& 0xf0) >> 4); break;
843 case BRG
: pixel
= ((b
& 0xf0) << 4) | (r
& 0xf0) | ((g
& 0xf0) >> 4); break;
844 case BGR
: pixel
= ((b
& 0xf0) << 4) | (g
& 0xf0) | ((r
& 0xf0) >> 4); break;
846 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
854 case RGB
: pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
855 case RBG
: pixel
= ((r
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
856 case GRB
: pixel
= ((g
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
857 case GBR
: pixel
= ((g
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
858 case BRG
: pixel
= ((b
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
859 case BGR
: pixel
= ((b
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
861 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
866 // I actually don't know if for 16-bit displays, it is alway the green
867 // component or the second component which has 6 bits.
871 case RGB
: pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
872 case RBG
: pixel
= ((r
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
873 case GRB
: pixel
= ((g
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
874 case GBR
: pixel
= ((g
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
875 case BRG
: pixel
= ((b
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
876 case BGR
: pixel
= ((b
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
878 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
887 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
888 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
889 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
890 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
891 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
892 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
894 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
903 GdkGC
*data_gc
= gdk_gc_new( GetPixmap() );
905 gdk_draw_image( GetPixmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
907 gdk_image_destroy( data_image
);
908 gdk_gc_unref( data_gc
);
914 GdkGC
*mask_gc
= gdk_gc_new( GetMask()->GetBitmap() );
916 gdk_draw_image( GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
918 gdk_image_destroy( mask_image
);
919 gdk_gc_unref( mask_gc
);
926 bool wxBitmap::CreateFromImageAsPixbuf(const wxImage
& image
)
928 int width
= image
.GetWidth();
929 int height
= image
.GetHeight();
931 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
933 8 /* bits per sample */,
938 wxASSERT( gdk_pixbuf_get_n_channels(pixbuf
) == 4 );
939 wxASSERT( gdk_pixbuf_get_width(pixbuf
) == width
);
940 wxASSERT( gdk_pixbuf_get_height(pixbuf
) == height
);
942 M_BMPDATA
->m_pixbuf
= pixbuf
;
945 SetDepth(wxTheApp
->GetGdkVisual()->depth
);
948 unsigned char *in
= image
.GetData();
949 unsigned char *out
= gdk_pixbuf_get_pixels(pixbuf
);
950 unsigned char *alpha
= image
.GetAlpha();
952 int rowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
955 for (int y
= 0; y
< height
; y
++, out
+= rowinc
)
957 for (int x
= 0; x
< width
; x
++, alpha
++, out
+= 4, in
+= 3)
968 #endif // __WXGTK20__
970 wxImage
wxBitmap::ConvertToImage() const
974 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
976 image
.Create(GetWidth(), GetHeight());
977 unsigned char *data
= image
.GetData();
981 wxFAIL_MSG( wxT("couldn't create image") );
988 GdkPixbuf
*pixbuf
= GetPixbuf();
989 wxASSERT( gdk_pixbuf_get_has_alpha(pixbuf
) );
996 unsigned char *alpha
= image
.GetAlpha();
997 unsigned char *in
= gdk_pixbuf_get_pixels(pixbuf
);
998 unsigned char *out
= data
;
999 int rowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * w
;
1001 for (int y
= 0; y
< h
; y
++, in
+= rowinc
)
1003 for (int x
= 0; x
< w
; x
++, in
+= 4, out
+= 3, alpha
++)
1013 #endif // __WXGTK20__
1015 // the colour used as transparent one in wxImage and the one it is
1016 // replaced with when it really occurs in the bitmap
1017 static const int MASK_RED
= 1;
1018 static const int MASK_GREEN
= 2;
1019 static const int MASK_BLUE
= 3;
1020 static const int MASK_BLUE_REPLACEMENT
= 2;
1022 GdkImage
*gdk_image
= (GdkImage
*) NULL
;
1026 gdk_image
= gdk_image_get( GetPixmap(),
1028 GetWidth(), GetHeight() );
1030 else if (GetBitmap())
1032 gdk_image
= gdk_image_get( GetBitmap(),
1034 GetWidth(), GetHeight() );
1038 wxFAIL_MSG( wxT("Ill-formed bitmap") );
1041 wxCHECK_MSG( gdk_image
, wxNullImage
, wxT("couldn't create image") );
1043 GdkImage
*gdk_image_mask
= (GdkImage
*) NULL
;
1046 gdk_image_mask
= gdk_image_get( GetMask()->GetBitmap(),
1048 GetWidth(), GetHeight() );
1050 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
1054 int red_shift_right
= 0;
1055 int green_shift_right
= 0;
1056 int blue_shift_right
= 0;
1057 int red_shift_left
= 0;
1058 int green_shift_left
= 0;
1059 int blue_shift_left
= 0;
1060 bool use_shift
= FALSE
;
1064 GdkVisual
*visual
= gdk_window_get_visual( GetPixmap() );
1066 visual
= wxTheApp
->GetGdkVisual();
1068 bpp
= visual
->depth
;
1070 bpp
= visual
->red_prec
+ visual
->green_prec
+ visual
->blue_prec
;
1071 red_shift_right
= visual
->red_shift
;
1072 red_shift_left
= 8-visual
->red_prec
;
1073 green_shift_right
= visual
->green_shift
;
1074 green_shift_left
= 8-visual
->green_prec
;
1075 blue_shift_right
= visual
->blue_shift
;
1076 blue_shift_left
= 8-visual
->blue_prec
;
1078 use_shift
= (visual
->type
== GDK_VISUAL_TRUE_COLOR
) || (visual
->type
== GDK_VISUAL_DIRECT_COLOR
);
1086 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
1089 for (int j
= 0; j
< GetHeight(); j
++)
1091 for (int i
= 0; i
< GetWidth(); i
++)
1093 wxUint32 pixel
= gdk_image_get_pixel( gdk_image
, i
, j
);
1111 data
[pos
] = (pixel
>> red_shift_right
) << red_shift_left
;
1112 data
[pos
+1] = (pixel
>> green_shift_right
) << green_shift_left
;
1113 data
[pos
+2] = (pixel
>> blue_shift_right
) << blue_shift_left
;
1115 else if (cmap
->colors
)
1117 data
[pos
] = cmap
->colors
[pixel
].red
>> 8;
1118 data
[pos
+1] = cmap
->colors
[pixel
].green
>> 8;
1119 data
[pos
+2] = cmap
->colors
[pixel
].blue
>> 8;
1123 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
1128 int mask_pixel
= gdk_image_get_pixel( gdk_image_mask
, i
, j
);
1129 if (mask_pixel
== 0)
1131 data
[pos
] = MASK_RED
;
1132 data
[pos
+1] = MASK_GREEN
;
1133 data
[pos
+2] = MASK_BLUE
;
1135 else if ( data
[pos
] == MASK_RED
&&
1136 data
[pos
+1] == MASK_GREEN
&&
1137 data
[pos
+2] == MASK_BLUE
)
1139 data
[pos
+2] = MASK_BLUE_REPLACEMENT
;
1147 gdk_image_destroy( gdk_image
);
1148 if (gdk_image_mask
) gdk_image_destroy( gdk_image_mask
);
1154 wxBitmap::wxBitmap( const wxBitmap
& bmp
)
1160 wxBitmap::wxBitmap( const wxString
&filename
, int type
)
1162 LoadFile( filename
, type
);
1165 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int WXUNUSED(depth
))
1167 if ( width
> 0 && height
> 0 )
1169 m_refData
= new wxBitmapRefData();
1171 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
1172 M_BMPDATA
->m_bitmap
= gdk_bitmap_create_from_data
1174 wxGetRootWindow()->window
,
1179 M_BMPDATA
->m_width
= width
;
1180 M_BMPDATA
->m_height
= height
;
1181 M_BMPDATA
->m_bpp
= 1;
1183 wxASSERT_MSG( M_BMPDATA
->m_bitmap
, wxT("couldn't create bitmap") );
1187 wxBitmap::~wxBitmap()
1191 wxBitmap
& wxBitmap::operator = ( const wxBitmap
& bmp
)
1193 if ( m_refData
!= bmp
.m_refData
)
1199 bool wxBitmap::operator == ( const wxBitmap
& bmp
) const
1201 return m_refData
== bmp
.m_refData
;
1204 bool wxBitmap::operator != ( const wxBitmap
& bmp
) const
1206 return m_refData
!= bmp
.m_refData
;
1209 bool wxBitmap::Ok() const
1211 return (m_refData
!= NULL
) &&
1214 M_BMPDATA
->m_pixbuf
||
1216 M_BMPDATA
->m_bitmap
|| M_BMPDATA
->m_pixmap
1220 int wxBitmap::GetHeight() const
1222 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1224 return M_BMPDATA
->m_height
;
1227 int wxBitmap::GetWidth() const
1229 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1231 return M_BMPDATA
->m_width
;
1234 int wxBitmap::GetDepth() const
1236 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1238 return M_BMPDATA
->m_bpp
;
1241 wxMask
*wxBitmap::GetMask() const
1243 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
1245 return M_BMPDATA
->m_mask
;
1248 void wxBitmap::SetMask( wxMask
*mask
)
1250 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
1252 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
1254 M_BMPDATA
->m_mask
= mask
;
1257 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
1263 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
1265 wxCHECK_MSG( Ok() &&
1266 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1267 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) && (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
1268 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
1270 wxBitmap
ret( rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
1271 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
1276 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
1278 8, GetWidth(), GetHeight());
1279 ret
.SetPixbuf(pixbuf
);
1280 gdk_pixbuf_copy_area(M_BMPDATA
->m_pixbuf
,
1281 rect
.x
, rect
.y
, rect
.width
, rect
.height
,
1285 #endif // __WXGTK20__
1287 if (ret
.GetPixmap())
1289 GdkGC
*gc
= gdk_gc_new( ret
.GetPixmap() );
1290 gdk_draw_pixmap( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1291 gdk_gc_destroy( gc
);
1295 GdkGC
*gc
= gdk_gc_new( ret
.GetBitmap() );
1297 col
.pixel
= 0xFFFFFF;
1298 gdk_gc_set_foreground( gc
, &col
);
1300 gdk_gc_set_background( gc
, &col
);
1301 gdk_wx_draw_bitmap( ret
.GetBitmap(), gc
, GetBitmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1302 gdk_gc_destroy( gc
);
1308 wxMask
*mask
= new wxMask
;
1309 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, rect
.width
, rect
.height
, 1 );
1311 GdkGC
*gc
= gdk_gc_new( mask
->m_bitmap
);
1313 col
.pixel
= 0xFFFFFF;
1314 gdk_gc_set_foreground( gc
, &col
);
1316 gdk_gc_set_background( gc
, &col
);
1317 gdk_wx_draw_bitmap( mask
->m_bitmap
, gc
, M_BMPDATA
->m_mask
->m_bitmap
, rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1318 gdk_gc_destroy( gc
);
1320 ret
.SetMask( mask
);
1326 bool wxBitmap::SaveFile( const wxString
&name
, int type
, wxPalette
*WXUNUSED(palette
) )
1328 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid bitmap") );
1330 // Try to save the bitmap via wxImage handlers:
1332 wxImage image
= ConvertToImage();
1333 if (image
.Ok()) return image
.SaveFile( name
, type
);
1339 bool wxBitmap::LoadFile( const wxString
&name
, int type
)
1343 if (!wxFileExists(name
))
1346 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
1348 if (type
== wxBITMAP_TYPE_XPM
)
1350 m_refData
= new wxBitmapRefData();
1352 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
1354 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm
1356 wxGetRootWindow()->window
,
1364 M_BMPDATA
->m_mask
= new wxMask();
1365 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
1368 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
1370 M_BMPDATA
->m_bpp
= visual
->depth
;
1372 else // try if wxImage can load it
1375 if ( !image
.LoadFile( name
, type
) || !image
.Ok() )
1378 *this = wxBitmap(image
);
1384 wxPalette
*wxBitmap::GetPalette() const
1387 return (wxPalette
*) NULL
;
1389 return M_BMPDATA
->m_palette
;
1392 void wxBitmap::SetHeight( int height
)
1395 m_refData
= new wxBitmapRefData();
1397 M_BMPDATA
->m_height
= height
;
1400 void wxBitmap::SetWidth( int width
)
1403 m_refData
= new wxBitmapRefData();
1405 M_BMPDATA
->m_width
= width
;
1408 void wxBitmap::SetDepth( int depth
)
1411 m_refData
= new wxBitmapRefData();
1413 M_BMPDATA
->m_bpp
= depth
;
1416 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
1419 m_refData
= new wxBitmapRefData();
1421 M_BMPDATA
->m_pixmap
= pixmap
;
1424 void wxBitmap::SetBitmap( GdkPixmap
*bitmap
)
1427 m_refData
= new wxBitmapRefData();
1429 M_BMPDATA
->m_bitmap
= bitmap
;
1432 GdkPixmap
*wxBitmap::GetPixmap() const
1434 wxCHECK_MSG( Ok(), (GdkPixmap
*) NULL
, wxT("invalid bitmap") );
1437 // create the pixmap on the fly if we use Pixbuf representation:
1438 if (HasPixbuf() && !HasPixmap())
1440 gdk_pixbuf_render_pixmap_and_mask(M_BMPDATA
->m_pixbuf
,
1441 &M_BMPDATA
->m_pixmap
,
1445 #endif // __WXGTK20__
1447 return M_BMPDATA
->m_pixmap
;
1450 bool wxBitmap::HasPixmap() const
1452 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1454 return M_BMPDATA
->m_pixmap
!= NULL
;
1457 GdkBitmap
*wxBitmap::GetBitmap() const
1459 wxCHECK_MSG( Ok(), (GdkBitmap
*) NULL
, wxT("invalid bitmap") );
1461 return M_BMPDATA
->m_bitmap
;
1465 GdkPixbuf
*wxBitmap::GetPixbuf() const
1467 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
1469 return M_BMPDATA
->m_pixbuf
;
1472 bool wxBitmap::HasPixbuf() const
1474 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1476 return M_BMPDATA
->m_pixbuf
!= NULL
;
1479 void wxBitmap::SetPixbuf( GdkPixbuf
*pixbuf
)
1482 m_refData
= new wxBitmapRefData();
1484 M_BMPDATA
->m_pixbuf
= pixbuf
;
1487 void wxBitmap::PurgeOtherRepresentations(wxBitmap::Representation keep
)
1489 if (keep
== Pixmap
&& HasPixbuf())
1491 gdk_pixbuf_unref( M_BMPDATA
->m_pixbuf
);
1492 M_BMPDATA
->m_pixbuf
= NULL
;
1494 if (keep
== Pixbuf
&& HasPixmap())
1496 gdk_pixmap_unref( M_BMPDATA
->m_pixmap
);
1497 M_BMPDATA
->m_pixmap
= NULL
;
1501 #endif // __WXGTK20__