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/bitmap.h"
20 #include "wx/palette.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
,
380 gdk_pixbuf_get_has_alpha(GetPixbuf()),
382 gdk_pixbuf_scale(GetPixbuf(), bmp
.GetPixbuf(),
385 (double)newx
/GetWidth(), (double)newy
/GetHeight(),
386 GDK_INTERP_BILINEAR
);
389 #endif // __WXGTK20__
391 GdkImage
*img
= (GdkImage
*) NULL
;
393 img
= gdk_image_get( GetPixmap(), 0, 0, GetWidth(), GetHeight() );
394 else if (GetBitmap())
395 img
= gdk_image_get( GetBitmap(), 0, 0, GetWidth(), GetHeight() );
397 wxFAIL_MSG( wxT("Ill-formed bitmap") );
399 wxCHECK_MSG( img
, wxNullBitmap
, wxT("couldn't create image") );
405 GdkPixmap
*dstpix
= NULL
;
408 GdkVisual
*visual
= gdk_window_get_visual( GetPixmap() );
410 visual
= wxTheApp
->GetGdkVisual();
413 bmp
= wxBitmap(width
,height
,bpp
);
414 dstpix
= bmp
.GetPixmap();
415 gc
= gdk_gc_new( dstpix
);
419 long dstbyteperline
= 0;
424 dstbyteperline
= width
/8*M_BMPDATA
->m_bpp
;
425 if (width
*M_BMPDATA
->m_bpp
% 8 != 0)
427 dst
= (char*) malloc(dstbyteperline
*height
);
430 // be careful to use the right scaling factor
431 float scx
= (float)M_BMPDATA
->m_width
/(float)newx
;
432 float scy
= (float)M_BMPDATA
->m_height
/(float)newy
;
433 // prepare accel-tables
434 int *tablex
= (int *)calloc(width
,sizeof(int));
435 int *tabley
= (int *)calloc(height
,sizeof(int));
437 // accel table filled with clipped values
438 for (int x
= 0; x
< width
; x
++)
439 tablex
[x
] = (int) (scx
* (x
+clipx
));
440 for (int y
= 0; y
< height
; y
++)
441 tabley
[y
] = (int) (scy
* (y
+clipy
));
443 // Main rescaling routine starts here
444 for (int h
= 0; h
< height
; h
++)
448 guint32 old_pixval
= 0;
450 for (int w
= 0; w
< width
; w
++)
458 pixval
= gdk_image_get_pixel( img
, x
, tabley
[h
] );
468 char shift
= bit
<< w
% 8;
474 dst
[h
*dstbyteperline
+w
/8] = outbyte
;
482 gdk_gc_set_foreground( gc
, &col
);
483 gdk_draw_point( dstpix
, gc
, w
, h
);
487 // do not forget the last byte
488 if ((bpp
== 1) && (width
% 8 != 0))
489 dst
[h
*dstbyteperline
+width
/8] = outbyte
;
492 gdk_image_destroy( img
);
493 if (gc
) gdk_gc_unref( gc
);
497 bmp
= wxBitmap( (const char *)dst
, width
, height
, 1 );
503 dstbyteperline
= width
/8;
506 dst
= (char*) malloc(dstbyteperline
*height
);
507 img
= gdk_image_get( GetMask()->GetBitmap(), 0, 0, GetWidth(), GetHeight() );
509 for (int h
= 0; h
< height
; h
++)
513 guint32 old_pixval
= 0;
515 for (int w
= 0; w
< width
; w
++)
523 pixval
= gdk_image_get_pixel( img
, x
, tabley
[h
] );
531 char shift
= bit
<< w
% 8;
537 dst
[h
*dstbyteperline
+w
/8] = outbyte
;
542 // do not forget the last byte
544 dst
[h
*dstbyteperline
+width
/8] = outbyte
;
546 wxMask
* mask
= new wxMask
;
547 mask
->m_bitmap
= gdk_bitmap_create_from_data( wxGetRootWindow()->window
, (gchar
*) dst
, width
, height
);
551 gdk_image_destroy( img
);
561 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
565 wxCHECK_MSG( image
.Ok(), FALSE
, wxT("invalid image") )
566 wxCHECK_MSG( depth
== -1 || depth
== 1, FALSE
, wxT("invalid bitmap depth") )
568 if (image
.GetWidth() <= 0 || image
.GetHeight() <= 0)
571 m_refData
= new wxBitmapRefData();
575 return CreateFromImageAsBitmap(image
);
580 if (image
.HasAlpha())
581 return CreateFromImageAsPixbuf(image
);
583 return CreateFromImageAsPixmap(image
);
587 // conversion to mono bitmap:
588 bool wxBitmap::CreateFromImageAsBitmap(const wxImage
& img
)
590 // convert alpha channel to mask, if it is present:
592 image
.ConvertAlphaToMask();
594 int width
= image
.GetWidth();
595 int height
= image
.GetHeight();
600 SetBitmap( gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 ) );
604 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
606 // Create picture image
608 unsigned char *data_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
610 GdkImage
*data_image
=
611 gdk_image_new_bitmap( visual
, data_data
, width
, height
);
615 GdkImage
*mask_image
= (GdkImage
*) NULL
;
619 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
621 mask_image
= gdk_image_new_bitmap( visual
, mask_data
, width
, height
);
623 wxMask
*mask
= new wxMask();
624 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
629 int r_mask
= image
.GetMaskRed();
630 int g_mask
= image
.GetMaskGreen();
631 int b_mask
= image
.GetMaskBlue();
633 unsigned char* data
= image
.GetData();
636 for (int y
= 0; y
< height
; y
++)
638 for (int x
= 0; x
< width
; x
++)
649 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
650 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
652 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
655 if ((r
== 255) && (b
== 255) && (g
== 255))
656 gdk_image_put_pixel( data_image
, x
, y
, 1 );
658 gdk_image_put_pixel( data_image
, x
, y
, 0 );
665 GdkGC
*data_gc
= gdk_gc_new( GetBitmap() );
667 gdk_draw_image( GetBitmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
669 gdk_image_destroy( data_image
);
670 gdk_gc_unref( data_gc
);
676 GdkGC
*mask_gc
= gdk_gc_new( GetMask()->GetBitmap() );
678 gdk_draw_image( GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
680 gdk_image_destroy( mask_image
);
681 gdk_gc_unref( mask_gc
);
687 // conversion to colour bitmap:
688 bool wxBitmap::CreateFromImageAsPixmap(const wxImage
& img
)
690 // convert alpha channel to mask, if it is present:
692 image
.ConvertAlphaToMask();
694 int width
= image
.GetWidth();
695 int height
= image
.GetHeight();
700 SetPixmap( gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, -1 ) );
702 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
704 int bpp
= visual
->depth
;
708 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800))
713 // We handle 8-bit bitmaps ourselves using the colour cube, 12-bit
714 // visuals are not supported by GDK so we do these ourselves, too.
715 // 15-bit and 16-bit should actually work and 24-bit certainly does.
717 if (!image
.HasMask() && (bpp
> 16))
719 if (!image
.HasMask() && (bpp
> 12))
722 static bool s_hasInitialized
= FALSE
;
724 if (!s_hasInitialized
)
727 s_hasInitialized
= TRUE
;
730 GdkGC
*gc
= gdk_gc_new( GetPixmap() );
732 gdk_draw_rgb_image( GetPixmap(),
744 // Create picture image
746 GdkImage
*data_image
=
747 gdk_image_new( GDK_IMAGE_FASTEST
, visual
, width
, height
);
751 GdkImage
*mask_image
= (GdkImage
*) NULL
;
755 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
757 mask_image
= gdk_image_new_bitmap( visual
, mask_data
, width
, height
);
759 wxMask
*mask
= new wxMask();
760 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
767 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
768 byte_order b_o
= RGB
;
772 if ((visual
->red_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->blue_mask
)) b_o
= RGB
;
773 else if ((visual
->red_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->green_mask
)) b_o
= RBG
;
774 else if ((visual
->blue_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->green_mask
)) b_o
= BRG
;
775 else if ((visual
->blue_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->red_mask
)) b_o
= BGR
;
776 else if ((visual
->green_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->blue_mask
)) b_o
= GRB
;
777 else if ((visual
->green_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->red_mask
)) b_o
= GBR
;
780 int r_mask
= image
.GetMaskRed();
781 int g_mask
= image
.GetMaskGreen();
782 int b_mask
= image
.GetMaskBlue();
784 unsigned char* data
= image
.GetData();
787 for (int y
= 0; y
< height
; y
++)
789 for (int x
= 0; x
< width
; x
++)
800 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
801 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
803 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
811 if (wxTheApp
->m_colorCube
)
813 pixel
= wxTheApp
->m_colorCube
[ ((r
& 0xf8) << 7) + ((g
& 0xf8) << 2) + ((b
& 0xf8) >> 3) ];
817 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
818 GdkColor
*colors
= cmap
->colors
;
819 int max
= 3 * (65536);
821 for (int i
= 0; i
< cmap
->size
; i
++)
823 int rdiff
= (r
<< 8) - colors
[i
].red
;
824 int gdiff
= (g
<< 8) - colors
[i
].green
;
825 int bdiff
= (b
<< 8) - colors
[i
].blue
;
826 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
827 if (sum
< max
) { pixel
= i
; max
= sum
; }
831 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
840 case RGB
: pixel
= ((r
& 0xf0) << 4) | (g
& 0xf0) | ((b
& 0xf0) >> 4); break;
841 case RBG
: pixel
= ((r
& 0xf0) << 4) | (b
& 0xf0) | ((g
& 0xf0) >> 4); break;
842 case GRB
: pixel
= ((g
& 0xf0) << 4) | (r
& 0xf0) | ((b
& 0xf0) >> 4); break;
843 case GBR
: pixel
= ((g
& 0xf0) << 4) | (b
& 0xf0) | ((r
& 0xf0) >> 4); break;
844 case BRG
: pixel
= ((b
& 0xf0) << 4) | (r
& 0xf0) | ((g
& 0xf0) >> 4); break;
845 case BGR
: pixel
= ((b
& 0xf0) << 4) | (g
& 0xf0) | ((r
& 0xf0) >> 4); break;
847 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
855 case RGB
: pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
856 case RBG
: pixel
= ((r
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
857 case GRB
: pixel
= ((g
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
858 case GBR
: pixel
= ((g
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
859 case BRG
: pixel
= ((b
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
860 case BGR
: pixel
= ((b
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
862 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
867 // I actually don't know if for 16-bit displays, it is alway the green
868 // component or the second component which has 6 bits.
872 case RGB
: pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
873 case RBG
: pixel
= ((r
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
874 case GRB
: pixel
= ((g
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
875 case GBR
: pixel
= ((g
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
876 case BRG
: pixel
= ((b
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
877 case BGR
: pixel
= ((b
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
879 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
888 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
889 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
890 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
891 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
892 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
893 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
895 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
904 GdkGC
*data_gc
= gdk_gc_new( GetPixmap() );
906 gdk_draw_image( GetPixmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
908 gdk_image_destroy( data_image
);
909 gdk_gc_unref( data_gc
);
915 GdkGC
*mask_gc
= gdk_gc_new( GetMask()->GetBitmap() );
917 gdk_draw_image( GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
919 gdk_image_destroy( mask_image
);
920 gdk_gc_unref( mask_gc
);
927 bool wxBitmap::CreateFromImageAsPixbuf(const wxImage
& image
)
929 int width
= image
.GetWidth();
930 int height
= image
.GetHeight();
932 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
934 8 /* bits per sample */,
939 wxASSERT( image
.HasAlpha() ); // for now
940 wxASSERT( gdk_pixbuf_get_n_channels(pixbuf
) == 4 );
941 wxASSERT( gdk_pixbuf_get_width(pixbuf
) == width
);
942 wxASSERT( gdk_pixbuf_get_height(pixbuf
) == height
);
944 M_BMPDATA
->m_pixbuf
= pixbuf
;
947 SetDepth(wxTheApp
->GetGdkVisual()->depth
);
950 unsigned char *in
= image
.GetData();
951 unsigned char *out
= gdk_pixbuf_get_pixels(pixbuf
);
952 unsigned char *alpha
= image
.GetAlpha();
954 int rowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
956 for (int y
= 0; y
< height
; y
++, out
+= rowinc
)
958 for (int x
= 0; x
< width
; x
++, alpha
++, out
+= 4, in
+= 3)
969 #endif // __WXGTK20__
971 wxImage
wxBitmap::ConvertToImage() const
975 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
977 image
.Create(GetWidth(), GetHeight());
978 unsigned char *data
= image
.GetData();
982 wxFAIL_MSG( wxT("couldn't create image") );
989 GdkPixbuf
*pixbuf
= GetPixbuf();
990 wxASSERT( gdk_pixbuf_get_has_alpha(pixbuf
) );
997 unsigned char *alpha
= image
.GetAlpha();
998 unsigned char *in
= gdk_pixbuf_get_pixels(pixbuf
);
999 unsigned char *out
= data
;
1000 int rowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * w
;
1002 for (int y
= 0; y
< h
; y
++, in
+= rowinc
)
1004 for (int x
= 0; x
< w
; x
++, in
+= 4, out
+= 3, alpha
++)
1014 #endif // __WXGTK20__
1016 // the colour used as transparent one in wxImage and the one it is
1017 // replaced with when it really occurs in the bitmap
1018 static const int MASK_RED
= 1;
1019 static const int MASK_GREEN
= 2;
1020 static const int MASK_BLUE
= 3;
1021 static const int MASK_BLUE_REPLACEMENT
= 2;
1023 GdkImage
*gdk_image
= (GdkImage
*) NULL
;
1027 gdk_image
= gdk_image_get( GetPixmap(),
1029 GetWidth(), GetHeight() );
1031 else if (GetBitmap())
1033 gdk_image
= gdk_image_get( GetBitmap(),
1035 GetWidth(), GetHeight() );
1039 wxFAIL_MSG( wxT("Ill-formed bitmap") );
1042 wxCHECK_MSG( gdk_image
, wxNullImage
, wxT("couldn't create image") );
1044 GdkImage
*gdk_image_mask
= (GdkImage
*) NULL
;
1047 gdk_image_mask
= gdk_image_get( GetMask()->GetBitmap(),
1049 GetWidth(), GetHeight() );
1051 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
1055 int red_shift_right
= 0;
1056 int green_shift_right
= 0;
1057 int blue_shift_right
= 0;
1058 int red_shift_left
= 0;
1059 int green_shift_left
= 0;
1060 int blue_shift_left
= 0;
1061 bool use_shift
= FALSE
;
1065 GdkVisual
*visual
= gdk_window_get_visual( GetPixmap() );
1067 visual
= wxTheApp
->GetGdkVisual();
1069 bpp
= visual
->depth
;
1071 bpp
= visual
->red_prec
+ visual
->green_prec
+ visual
->blue_prec
;
1072 red_shift_right
= visual
->red_shift
;
1073 red_shift_left
= 8-visual
->red_prec
;
1074 green_shift_right
= visual
->green_shift
;
1075 green_shift_left
= 8-visual
->green_prec
;
1076 blue_shift_right
= visual
->blue_shift
;
1077 blue_shift_left
= 8-visual
->blue_prec
;
1079 use_shift
= (visual
->type
== GDK_VISUAL_TRUE_COLOR
) || (visual
->type
== GDK_VISUAL_DIRECT_COLOR
);
1087 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
1090 for (int j
= 0; j
< GetHeight(); j
++)
1092 for (int i
= 0; i
< GetWidth(); i
++)
1094 wxUint32 pixel
= gdk_image_get_pixel( gdk_image
, i
, j
);
1112 data
[pos
] = (pixel
>> red_shift_right
) << red_shift_left
;
1113 data
[pos
+1] = (pixel
>> green_shift_right
) << green_shift_left
;
1114 data
[pos
+2] = (pixel
>> blue_shift_right
) << blue_shift_left
;
1116 else if (cmap
->colors
)
1118 data
[pos
] = cmap
->colors
[pixel
].red
>> 8;
1119 data
[pos
+1] = cmap
->colors
[pixel
].green
>> 8;
1120 data
[pos
+2] = cmap
->colors
[pixel
].blue
>> 8;
1124 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
1129 int mask_pixel
= gdk_image_get_pixel( gdk_image_mask
, i
, j
);
1130 if (mask_pixel
== 0)
1132 data
[pos
] = MASK_RED
;
1133 data
[pos
+1] = MASK_GREEN
;
1134 data
[pos
+2] = MASK_BLUE
;
1136 else if ( data
[pos
] == MASK_RED
&&
1137 data
[pos
+1] == MASK_GREEN
&&
1138 data
[pos
+2] == MASK_BLUE
)
1140 data
[pos
+2] = MASK_BLUE_REPLACEMENT
;
1148 gdk_image_destroy( gdk_image
);
1149 if (gdk_image_mask
) gdk_image_destroy( gdk_image_mask
);
1155 wxBitmap::wxBitmap( const wxBitmap
& bmp
)
1161 wxBitmap::wxBitmap( const wxString
&filename
, wxBitmapType type
)
1163 LoadFile( filename
, type
);
1166 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int WXUNUSED(depth
))
1168 if ( width
> 0 && height
> 0 )
1170 m_refData
= new wxBitmapRefData();
1172 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
1173 M_BMPDATA
->m_bitmap
= gdk_bitmap_create_from_data
1175 wxGetRootWindow()->window
,
1180 M_BMPDATA
->m_width
= width
;
1181 M_BMPDATA
->m_height
= height
;
1182 M_BMPDATA
->m_bpp
= 1;
1184 wxASSERT_MSG( M_BMPDATA
->m_bitmap
, wxT("couldn't create bitmap") );
1188 wxBitmap::~wxBitmap()
1192 wxBitmap
& wxBitmap::operator = ( const wxBitmap
& bmp
)
1194 if ( m_refData
!= bmp
.m_refData
)
1200 bool wxBitmap::operator == ( const wxBitmap
& bmp
) const
1202 return m_refData
== bmp
.m_refData
;
1205 bool wxBitmap::operator != ( const wxBitmap
& bmp
) const
1207 return m_refData
!= bmp
.m_refData
;
1210 bool wxBitmap::Ok() const
1212 return (m_refData
!= NULL
) &&
1215 M_BMPDATA
->m_pixbuf
||
1217 M_BMPDATA
->m_bitmap
|| M_BMPDATA
->m_pixmap
1221 int wxBitmap::GetHeight() const
1223 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1225 return M_BMPDATA
->m_height
;
1228 int wxBitmap::GetWidth() const
1230 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1232 return M_BMPDATA
->m_width
;
1235 int wxBitmap::GetDepth() const
1237 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1239 return M_BMPDATA
->m_bpp
;
1242 wxMask
*wxBitmap::GetMask() const
1244 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
1246 return M_BMPDATA
->m_mask
;
1249 void wxBitmap::SetMask( wxMask
*mask
)
1251 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
1253 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
1255 M_BMPDATA
->m_mask
= mask
;
1258 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
1264 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
1266 wxCHECK_MSG( Ok() &&
1267 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1268 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) && (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
1269 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
1271 wxBitmap
ret( rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
1272 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
1277 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
1278 gdk_pixbuf_get_has_alpha(GetPixbuf()),
1279 8, GetWidth(), GetHeight());
1280 ret
.SetPixbuf(pixbuf
);
1281 gdk_pixbuf_copy_area(GetPixbuf(),
1282 rect
.x
, rect
.y
, rect
.width
, rect
.height
,
1286 #endif // __WXGTK20__
1288 if (ret
.GetPixmap())
1290 GdkGC
*gc
= gdk_gc_new( ret
.GetPixmap() );
1291 gdk_draw_pixmap( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1292 gdk_gc_destroy( gc
);
1296 GdkGC
*gc
= gdk_gc_new( ret
.GetBitmap() );
1298 col
.pixel
= 0xFFFFFF;
1299 gdk_gc_set_foreground( gc
, &col
);
1301 gdk_gc_set_background( gc
, &col
);
1302 gdk_wx_draw_bitmap( ret
.GetBitmap(), gc
, GetBitmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1303 gdk_gc_destroy( gc
);
1309 wxMask
*mask
= new wxMask
;
1310 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, rect
.width
, rect
.height
, 1 );
1312 GdkGC
*gc
= gdk_gc_new( mask
->m_bitmap
);
1314 col
.pixel
= 0xFFFFFF;
1315 gdk_gc_set_foreground( gc
, &col
);
1317 gdk_gc_set_background( gc
, &col
);
1318 gdk_wx_draw_bitmap( mask
->m_bitmap
, gc
, M_BMPDATA
->m_mask
->m_bitmap
, rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1319 gdk_gc_destroy( gc
);
1321 ret
.SetMask( mask
);
1327 bool wxBitmap::SaveFile( const wxString
&name
, wxBitmapType type
, const wxPalette
*WXUNUSED(palette
) ) const
1329 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid bitmap") );
1331 // Try to save the bitmap via wxImage handlers:
1333 wxImage image
= ConvertToImage();
1334 if (image
.Ok()) return image
.SaveFile( name
, type
);
1340 bool wxBitmap::LoadFile( const wxString
&name
, wxBitmapType type
)
1344 if (!wxFileExists(name
))
1347 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
1349 if (type
== wxBITMAP_TYPE_XPM
)
1351 m_refData
= new wxBitmapRefData();
1353 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
1355 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm
1357 wxGetRootWindow()->window
,
1365 M_BMPDATA
->m_mask
= new wxMask();
1366 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
1369 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
1371 M_BMPDATA
->m_bpp
= visual
->depth
;
1373 else // try if wxImage can load it
1376 if ( !image
.LoadFile( name
, type
) || !image
.Ok() )
1379 *this = wxBitmap(image
);
1385 wxPalette
*wxBitmap::GetPalette() const
1388 return (wxPalette
*) NULL
;
1390 return M_BMPDATA
->m_palette
;
1393 void wxBitmap::SetPalette(const wxPalette
& WXUNUSED(palette
))
1398 void wxBitmap::SetHeight( int height
)
1401 m_refData
= new wxBitmapRefData();
1403 M_BMPDATA
->m_height
= height
;
1406 void wxBitmap::SetWidth( int width
)
1409 m_refData
= new wxBitmapRefData();
1411 M_BMPDATA
->m_width
= width
;
1414 void wxBitmap::SetDepth( int depth
)
1417 m_refData
= new wxBitmapRefData();
1419 M_BMPDATA
->m_bpp
= depth
;
1422 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
1425 m_refData
= new wxBitmapRefData();
1427 M_BMPDATA
->m_pixmap
= pixmap
;
1429 PurgeOtherRepresentations(Pixmap
);
1433 void wxBitmap::SetBitmap( GdkPixmap
*bitmap
)
1436 m_refData
= new wxBitmapRefData();
1438 M_BMPDATA
->m_bitmap
= bitmap
;
1440 PurgeOtherRepresentations(Pixmap
);
1444 GdkPixmap
*wxBitmap::GetPixmap() const
1446 wxCHECK_MSG( Ok(), (GdkPixmap
*) NULL
, wxT("invalid bitmap") );
1449 // create the pixmap on the fly if we use Pixbuf representation:
1450 if (HasPixbuf() && !HasPixmap())
1452 delete M_BMPDATA
->m_mask
;
1453 M_BMPDATA
->m_mask
= new wxMask();
1454 gdk_pixbuf_render_pixmap_and_mask(M_BMPDATA
->m_pixbuf
,
1455 &M_BMPDATA
->m_pixmap
,
1456 &M_BMPDATA
->m_mask
->m_bitmap
,
1459 #endif // __WXGTK20__
1461 return M_BMPDATA
->m_pixmap
;
1464 bool wxBitmap::HasPixmap() const
1466 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1468 return M_BMPDATA
->m_pixmap
!= NULL
;
1471 GdkBitmap
*wxBitmap::GetBitmap() const
1473 wxCHECK_MSG( Ok(), (GdkBitmap
*) NULL
, wxT("invalid bitmap") );
1475 return M_BMPDATA
->m_bitmap
;
1479 GdkPixbuf
*wxBitmap::GetPixbuf() const
1481 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
1483 if (HasPixmap() && !HasPixbuf())
1485 int width
= GetWidth();
1486 int height
= GetHeight();
1488 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
1491 M_BMPDATA
->m_pixbuf
=
1492 gdk_pixbuf_get_from_drawable(pixbuf
, M_BMPDATA
->m_pixmap
, NULL
,
1493 0, 0, 0, 0, width
, height
);
1495 // apply the mask to created pixbuf:
1496 if (M_BMPDATA
->m_pixbuf
&& M_BMPDATA
->m_mask
)
1499 gdk_pixbuf_get_from_drawable(NULL
,
1500 M_BMPDATA
->m_mask
->GetBitmap(),
1502 0, 0, 0, 0, width
, height
);
1505 guchar
*bmp
= gdk_pixbuf_get_pixels(pixbuf
);
1506 guchar
*mask
= gdk_pixbuf_get_pixels(pmask
);
1507 int bmprowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
1508 int maskrowinc
= gdk_pixbuf_get_rowstride(pmask
) - 3 * width
;
1510 for (int y
= 0; y
< height
;
1511 y
++, bmp
+= bmprowinc
, mask
+= maskrowinc
)
1513 for (int x
= 0; x
< width
; x
++, bmp
+= 4, mask
+= 3)
1515 if (mask
[0] == 0 /*black pixel*/)
1520 gdk_pixbuf_unref(pmask
);
1525 return M_BMPDATA
->m_pixbuf
;
1528 bool wxBitmap::HasPixbuf() const
1530 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1532 return M_BMPDATA
->m_pixbuf
!= NULL
;
1535 void wxBitmap::SetPixbuf( GdkPixbuf
*pixbuf
)
1538 m_refData
= new wxBitmapRefData();
1540 M_BMPDATA
->m_pixbuf
= pixbuf
;
1541 PurgeOtherRepresentations(Pixbuf
);
1544 void wxBitmap::PurgeOtherRepresentations(wxBitmap::Representation keep
)
1546 if (keep
== Pixmap
&& HasPixbuf())
1548 gdk_pixbuf_unref( M_BMPDATA
->m_pixbuf
);
1549 M_BMPDATA
->m_pixbuf
= NULL
;
1551 if (keep
== Pixbuf
&& HasPixmap())
1553 gdk_pixmap_unref( M_BMPDATA
->m_pixmap
);
1554 M_BMPDATA
->m_pixmap
= NULL
;
1558 #endif // __WXGTK20__
1560 //-----------------------------------------------------------------------------
1562 //-----------------------------------------------------------------------------
1564 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
,wxBitmapHandlerBase
)
1566 wxBitmapHandler::~wxBitmapHandler()
1570 bool wxBitmapHandler::Create(wxBitmap
*bitmap
, void *data
, long type
, int width
, int height
, int depth
)
1575 bool wxBitmapHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1576 int desiredWidth
, int desiredHeight
)
1581 bool wxBitmapHandler::SaveFile(const wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
)
1586 /* static */ void wxBitmap::InitStandardHandlers()
1588 // TODO: Insert handler based on GdkPixbufs handler later