1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/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"
15 #include "wx/bitmap.h"
16 #include "wx/palette.h"
18 #include "wx/filefn.h"
20 #include "wx/dcmemory.h"
24 #include "wx/rawbmp.h"
25 // need this to get gdk_image_new_bitmap()
26 #define GDK_ENABLE_BROKEN
34 #include <gdk/gdkimage.h>
36 #include <gdk/gdkrgb.h>
37 #endif // GTK+ 2.0/1.2
41 extern void gdk_wx_draw_bitmap (GdkDrawable
*drawable
,
51 //-----------------------------------------------------------------------------
53 //-----------------------------------------------------------------------------
55 extern GtkWidget
*wxGetRootWindow();
57 //-----------------------------------------------------------------------------
59 //-----------------------------------------------------------------------------
61 IMPLEMENT_DYNAMIC_CLASS(wxMask
,wxObject
)
65 m_bitmap
= (GdkBitmap
*) NULL
;
68 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
70 m_bitmap
= (GdkBitmap
*) NULL
;
71 Create( bitmap
, colour
);
75 wxMask::wxMask( const wxBitmap
& bitmap
, int paletteIndex
)
77 m_bitmap
= (GdkBitmap
*) NULL
;
78 Create( bitmap
, paletteIndex
);
80 #endif // wxUSE_PALETTE
82 wxMask::wxMask( const wxBitmap
& bitmap
)
84 m_bitmap
= (GdkBitmap
*) NULL
;
91 gdk_bitmap_unref( m_bitmap
);
94 bool wxMask::Create( const wxBitmap
& bitmap
,
95 const wxColour
& colour
)
99 gdk_bitmap_unref( m_bitmap
);
100 m_bitmap
= (GdkBitmap
*) NULL
;
103 wxImage image
= bitmap
.ConvertToImage();
104 if (!image
.Ok()) return false;
106 m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, image
.GetWidth(), image
.GetHeight(), 1 );
107 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
114 gdk_gc_set_foreground( gc
, &color
);
115 gdk_gc_set_fill( gc
, GDK_SOLID
);
116 gdk_draw_rectangle( m_bitmap
, gc
, TRUE
, 0, 0, image
.GetWidth(), image
.GetHeight() );
118 unsigned char *data
= image
.GetData();
121 unsigned char red
= colour
.Red();
122 unsigned char green
= colour
.Green();
123 unsigned char blue
= colour
.Blue();
125 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
127 int bpp
= visual
->depth
;
128 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800))
133 green
= green
& 0xf8;
139 green
= green
& 0xfc;
145 green
= green
& 0xf0;
153 gdk_gc_set_foreground( gc
, &color
);
155 for (int j
= 0; j
< image
.GetHeight(); j
++)
159 for (i
= 0; i
< image
.GetWidth(); i
++)
161 if ((data
[index
] == red
) &&
162 (data
[index
+1] == green
) &&
163 (data
[index
+2] == blue
))
172 gdk_draw_line( m_bitmap
, gc
, start_x
, j
, i
-1, j
);
179 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
));
199 #endif // wxUSE_PALETTE
201 bool wxMask::Create( const wxBitmap
& bitmap
)
205 gdk_bitmap_unref( m_bitmap
);
206 m_bitmap
= (GdkBitmap
*) NULL
;
209 if (!bitmap
.Ok()) return false;
211 wxCHECK_MSG( bitmap
.GetBitmap(), false, wxT("Cannot create mask from colour bitmap") );
213 m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, bitmap
.GetWidth(), bitmap
.GetHeight(), 1 );
215 if (!m_bitmap
) return false;
217 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
219 gdk_wx_draw_bitmap( m_bitmap
, gc
, bitmap
.GetBitmap(), 0, 0, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight() );
226 GdkBitmap
*wxMask::GetBitmap() const
231 //-----------------------------------------------------------------------------
233 //-----------------------------------------------------------------------------
235 class wxBitmapRefData
: public wxObjectRefData
250 wxPalette
*m_palette
;
253 wxBitmapRefData::wxBitmapRefData()
255 m_pixmap
= (GdkPixmap
*) NULL
;
256 m_bitmap
= (GdkBitmap
*) NULL
;
258 m_pixbuf
= (GdkPixbuf
*) NULL
;
260 m_mask
= (wxMask
*) NULL
;
264 m_palette
= (wxPalette
*) NULL
;
267 wxBitmapRefData::~wxBitmapRefData()
270 gdk_pixmap_unref( m_pixmap
);
272 gdk_bitmap_unref( m_bitmap
);
275 gdk_pixbuf_unref( m_pixbuf
);
280 #endif // wxUSE_PALETTE
283 //-----------------------------------------------------------------------------
285 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
287 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
293 wxBitmap::wxBitmap( int width
, int height
, int depth
)
295 Create( width
, height
, depth
);
298 bool wxBitmap::Create( int width
, int height
, int depth
)
302 if ( width
<= 0 || height
<= 0 )
307 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
310 depth
= visual
->depth
;
312 wxCHECK_MSG( (depth
== visual
->depth
) || (depth
== 1) || (depth
== 32), false,
313 wxT("invalid bitmap depth") )
315 m_refData
= new wxBitmapRefData();
316 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
317 M_BMPDATA
->m_width
= width
;
318 M_BMPDATA
->m_height
= height
;
321 M_BMPDATA
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
322 M_BMPDATA
->m_bpp
= 1;
325 else if (depth
== 32)
327 M_BMPDATA
->m_pixbuf
= gdk_pixbuf_new( GDK_COLORSPACE_RGB
, true,
329 M_BMPDATA
->m_bpp
= 32;
334 M_BMPDATA
->m_pixmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, depth
);
335 M_BMPDATA
->m_bpp
= visual
->depth
;
341 bool wxBitmap::CreateFromXpm( const char **bits
)
345 wxCHECK_MSG( bits
!= NULL
, false, wxT("invalid bitmap data") )
347 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
349 m_refData
= new wxBitmapRefData();
351 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
353 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm_d( wxGetRootWindow()->window
, &mask
, NULL
, (gchar
**) bits
);
355 wxCHECK_MSG( M_BMPDATA
->m_pixmap
, false, wxT("couldn't create pixmap") );
359 M_BMPDATA
->m_mask
= new wxMask();
360 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
363 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
365 M_BMPDATA
->m_bpp
= visual
->depth
; // Can we get a different depth from create_from_xpm_d() ?
370 wxBitmap
wxBitmap::Rescale( int clipx
, int clipy
, int clipwidth
, int clipheight
, int newx
, int newy
)
372 wxCHECK_MSG( Ok(), wxNullBitmap
, wxT("invalid bitmap") );
374 if (newy
==M_BMPDATA
->m_width
&& newy
==M_BMPDATA
->m_height
)
377 int width
= wxMax(newx
, 1);
378 int height
= wxMax(newy
, 1);
379 width
= wxMin(width
, clipwidth
);
380 height
= wxMin(height
, clipheight
);
388 bmp
.SetHeight(height
);
389 bmp
.SetDepth(GetDepth());
390 bmp
.SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
391 gdk_pixbuf_get_has_alpha(GetPixbuf()),
393 gdk_pixbuf_scale(GetPixbuf(), bmp
.GetPixbuf(),
396 (double)newx
/GetWidth(), (double)newy
/GetHeight(),
397 GDK_INTERP_BILINEAR
);
400 #endif // __WXGTK20__
402 GdkImage
*img
= (GdkImage
*) NULL
;
404 img
= gdk_image_get( GetPixmap(), 0, 0, GetWidth(), GetHeight() );
405 else if (GetBitmap())
406 img
= gdk_image_get( GetBitmap(), 0, 0, GetWidth(), GetHeight() );
408 wxFAIL_MSG( wxT("Ill-formed bitmap") );
410 wxCHECK_MSG( img
, wxNullBitmap
, wxT("couldn't create image") );
416 GdkPixmap
*dstpix
= NULL
;
419 GdkVisual
*visual
= gdk_window_get_visual( GetPixmap() );
421 visual
= wxTheApp
->GetGdkVisual();
424 bmp
= wxBitmap(width
,height
,bpp
);
425 dstpix
= bmp
.GetPixmap();
426 gc
= gdk_gc_new( dstpix
);
430 long dstbyteperline
= 0;
435 dstbyteperline
= width
/8*M_BMPDATA
->m_bpp
;
436 if (width
*M_BMPDATA
->m_bpp
% 8 != 0)
438 dst
= (char*) malloc(dstbyteperline
*height
);
441 // be careful to use the right scaling factor
442 float scx
= (float)M_BMPDATA
->m_width
/(float)newx
;
443 float scy
= (float)M_BMPDATA
->m_height
/(float)newy
;
444 // prepare accel-tables
445 int *tablex
= (int *)calloc(width
,sizeof(int));
446 int *tabley
= (int *)calloc(height
,sizeof(int));
448 // accel table filled with clipped values
449 for (int x
= 0; x
< width
; x
++)
450 tablex
[x
] = (int) (scx
* (x
+clipx
));
451 for (int y
= 0; y
< height
; y
++)
452 tabley
[y
] = (int) (scy
* (y
+clipy
));
454 // Main rescaling routine starts here
455 for (int h
= 0; h
< height
; h
++)
459 guint32 old_pixval
= 0;
461 for (int w
= 0; w
< width
; w
++)
469 pixval
= gdk_image_get_pixel( img
, x
, tabley
[h
] );
479 char shift
= bit
<< (w
% 8);
485 dst
[h
*dstbyteperline
+w
/8] = outbyte
;
493 gdk_gc_set_foreground( gc
, &col
);
494 gdk_draw_point( dstpix
, gc
, w
, h
);
498 // do not forget the last byte
499 if ((bpp
== 1) && (width
% 8 != 0))
500 dst
[h
*dstbyteperline
+width
/8] = outbyte
;
503 gdk_image_destroy( img
);
504 if (gc
) gdk_gc_unref( gc
);
508 bmp
= wxBitmap( (const char *)dst
, width
, height
, 1 );
514 dstbyteperline
= width
/8;
517 dst
= (char*) malloc(dstbyteperline
*height
);
518 img
= gdk_image_get( GetMask()->GetBitmap(), 0, 0, GetWidth(), GetHeight() );
520 for (int h
= 0; h
< height
; h
++)
524 guint32 old_pixval
= 0;
526 for (int w
= 0; w
< width
; w
++)
534 pixval
= gdk_image_get_pixel( img
, x
, tabley
[h
] );
542 char shift
= bit
<< (w
% 8);
548 dst
[h
*dstbyteperline
+w
/8] = outbyte
;
553 // do not forget the last byte
555 dst
[h
*dstbyteperline
+width
/8] = outbyte
;
557 wxMask
* mask
= new wxMask
;
558 mask
->m_bitmap
= gdk_bitmap_create_from_data( wxGetRootWindow()->window
, (gchar
*) dst
, width
, height
);
562 gdk_image_destroy( img
);
572 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
576 wxCHECK_MSG( image
.Ok(), false, wxT("invalid image") )
577 wxCHECK_MSG( depth
== -1 || depth
== 1, false, wxT("invalid bitmap depth") )
579 if (image
.GetWidth() <= 0 || image
.GetHeight() <= 0)
582 m_refData
= new wxBitmapRefData();
586 return CreateFromImageAsBitmap(image
);
591 if (image
.HasAlpha())
592 return CreateFromImageAsPixbuf(image
);
594 return CreateFromImageAsPixmap(image
);
598 // conversion to mono bitmap:
599 bool wxBitmap::CreateFromImageAsBitmap(const wxImage
& img
)
601 // convert alpha channel to mask, if it is present:
603 image
.ConvertAlphaToMask();
605 int width
= image
.GetWidth();
606 int height
= image
.GetHeight();
611 SetBitmap( gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 ) );
615 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
617 // Create picture image
619 unsigned char *data_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
621 GdkImage
*data_image
=
622 gdk_image_new_bitmap( visual
, data_data
, width
, height
);
626 GdkImage
*mask_image
= (GdkImage
*) NULL
;
630 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
632 mask_image
= gdk_image_new_bitmap( visual
, mask_data
, width
, height
);
634 wxMask
*mask
= new wxMask();
635 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
640 int r_mask
= image
.GetMaskRed();
641 int g_mask
= image
.GetMaskGreen();
642 int b_mask
= image
.GetMaskBlue();
644 unsigned char* data
= image
.GetData();
647 for (int y
= 0; y
< height
; y
++)
649 for (int x
= 0; x
< width
; x
++)
660 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
661 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
663 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
666 if ((r
== 255) && (b
== 255) && (g
== 255))
667 gdk_image_put_pixel( data_image
, x
, y
, 1 );
669 gdk_image_put_pixel( data_image
, x
, y
, 0 );
676 GdkGC
*data_gc
= gdk_gc_new( GetBitmap() );
678 gdk_draw_image( GetBitmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
680 gdk_image_destroy( data_image
);
681 gdk_gc_unref( data_gc
);
687 GdkGC
*mask_gc
= gdk_gc_new( GetMask()->GetBitmap() );
689 gdk_draw_image( GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
691 gdk_image_destroy( mask_image
);
692 gdk_gc_unref( mask_gc
);
698 // conversion to colour bitmap:
699 bool wxBitmap::CreateFromImageAsPixmap(const wxImage
& img
)
701 // convert alpha channel to mask, if it is present:
703 image
.ConvertAlphaToMask();
705 int width
= image
.GetWidth();
706 int height
= image
.GetHeight();
711 SetPixmap( gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, -1 ) );
713 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
715 int bpp
= visual
->depth
;
719 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800))
724 // We handle 8-bit bitmaps ourselves using the colour cube, 12-bit
725 // visuals are not supported by GDK so we do these ourselves, too.
726 // 15-bit and 16-bit should actually work and 24-bit certainly does.
728 if (!image
.HasMask() && (bpp
> 16))
730 if (!image
.HasMask() && (bpp
> 12))
733 static bool s_hasInitialized
= false;
735 if (!s_hasInitialized
)
738 s_hasInitialized
= true;
741 GdkGC
*gc
= gdk_gc_new( GetPixmap() );
743 gdk_draw_rgb_image( GetPixmap(),
755 // Create picture image
757 GdkImage
*data_image
=
758 gdk_image_new( GDK_IMAGE_FASTEST
, visual
, width
, height
);
762 GdkImage
*mask_image
= (GdkImage
*) NULL
;
766 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
768 mask_image
= gdk_image_new_bitmap( visual
, mask_data
, width
, height
);
770 wxMask
*mask
= new wxMask();
771 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
778 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
779 byte_order b_o
= RGB
;
783 if ((visual
->red_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->blue_mask
)) b_o
= RGB
;
784 else if ((visual
->red_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->green_mask
)) b_o
= RBG
;
785 else if ((visual
->blue_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->green_mask
)) b_o
= BRG
;
786 else if ((visual
->blue_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->red_mask
)) b_o
= BGR
;
787 else if ((visual
->green_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->blue_mask
)) b_o
= GRB
;
788 else if ((visual
->green_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->red_mask
)) b_o
= GBR
;
791 int r_mask
= image
.GetMaskRed();
792 int g_mask
= image
.GetMaskGreen();
793 int b_mask
= image
.GetMaskBlue();
795 unsigned char* data
= image
.GetData();
798 for (int y
= 0; y
< height
; y
++)
800 for (int x
= 0; x
< width
; x
++)
811 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
812 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
814 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
822 if (wxTheApp
->m_colorCube
)
824 pixel
= wxTheApp
->m_colorCube
[ ((r
& 0xf8) << 7) + ((g
& 0xf8) << 2) + ((b
& 0xf8) >> 3) ];
828 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
829 GdkColor
*colors
= cmap
->colors
;
830 int max
= 3 * (65536);
832 for (int i
= 0; i
< cmap
->size
; i
++)
834 int rdiff
= (r
<< 8) - colors
[i
].red
;
835 int gdiff
= (g
<< 8) - colors
[i
].green
;
836 int bdiff
= (b
<< 8) - colors
[i
].blue
;
837 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
838 if (sum
< max
) { pixel
= i
; max
= sum
; }
842 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
851 case RGB
: pixel
= ((r
& 0xf0) << 4) | (g
& 0xf0) | ((b
& 0xf0) >> 4); break;
852 case RBG
: pixel
= ((r
& 0xf0) << 4) | (b
& 0xf0) | ((g
& 0xf0) >> 4); break;
853 case GRB
: pixel
= ((g
& 0xf0) << 4) | (r
& 0xf0) | ((b
& 0xf0) >> 4); break;
854 case GBR
: pixel
= ((g
& 0xf0) << 4) | (b
& 0xf0) | ((r
& 0xf0) >> 4); break;
855 case BRG
: pixel
= ((b
& 0xf0) << 4) | (r
& 0xf0) | ((g
& 0xf0) >> 4); break;
856 case BGR
: pixel
= ((b
& 0xf0) << 4) | (g
& 0xf0) | ((r
& 0xf0) >> 4); break;
858 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
866 case RGB
: pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
867 case RBG
: pixel
= ((r
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
868 case GRB
: pixel
= ((g
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
869 case GBR
: pixel
= ((g
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
870 case BRG
: pixel
= ((b
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
871 case BGR
: pixel
= ((b
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
873 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
878 // I actually don't know if for 16-bit displays, it is alway the green
879 // component or the second component which has 6 bits.
883 case RGB
: pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
884 case RBG
: pixel
= ((r
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
885 case GRB
: pixel
= ((g
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
886 case GBR
: pixel
= ((g
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
887 case BRG
: pixel
= ((b
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
888 case BGR
: pixel
= ((b
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
890 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
899 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
900 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
901 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
902 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
903 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
904 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
906 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
916 GdkGC
*data_gc
= gdk_gc_new( GetPixmap() );
918 gdk_draw_image( GetPixmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
920 gdk_image_destroy( data_image
);
921 gdk_gc_unref( data_gc
);
927 GdkGC
*mask_gc
= gdk_gc_new( GetMask()->GetBitmap() );
929 gdk_draw_image( GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
931 gdk_image_destroy( mask_image
);
932 gdk_gc_unref( mask_gc
);
939 bool wxBitmap::CreateFromImageAsPixbuf(const wxImage
& image
)
941 int width
= image
.GetWidth();
942 int height
= image
.GetHeight();
944 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
946 8 /* bits per sample */,
951 wxASSERT( image
.HasAlpha() ); // for now
952 wxASSERT( gdk_pixbuf_get_n_channels(pixbuf
) == 4 );
953 wxASSERT( gdk_pixbuf_get_width(pixbuf
) == width
);
954 wxASSERT( gdk_pixbuf_get_height(pixbuf
) == height
);
956 M_BMPDATA
->m_pixbuf
= pixbuf
;
959 SetDepth(wxTheApp
->GetGdkVisual()->depth
);
962 unsigned char *in
= image
.GetData();
963 unsigned char *out
= gdk_pixbuf_get_pixels(pixbuf
);
964 unsigned char *alpha
= image
.GetAlpha();
966 int rowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
968 for (int y
= 0; y
< height
; y
++, out
+= rowinc
)
970 for (int x
= 0; x
< width
; x
++, alpha
++, out
+= 4, in
+= 3)
981 #endif // __WXGTK20__
983 wxImage
wxBitmap::ConvertToImage() const
987 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
989 image
.Create(GetWidth(), GetHeight());
990 unsigned char *data
= image
.GetData();
994 wxFAIL_MSG( wxT("couldn't create image") );
1001 GdkPixbuf
*pixbuf
= GetPixbuf();
1002 wxASSERT( gdk_pixbuf_get_has_alpha(pixbuf
) );
1005 int h
= GetHeight();
1009 unsigned char *alpha
= image
.GetAlpha();
1010 unsigned char *in
= gdk_pixbuf_get_pixels(pixbuf
);
1011 unsigned char *out
= data
;
1012 int rowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * w
;
1014 for (int y
= 0; y
< h
; y
++, in
+= rowinc
)
1016 for (int x
= 0; x
< w
; x
++, in
+= 4, out
+= 3, alpha
++)
1026 #endif // __WXGTK20__
1028 // the colour used as transparent one in wxImage and the one it is
1029 // replaced with when it really occurs in the bitmap
1030 static const int MASK_RED
= 1;
1031 static const int MASK_GREEN
= 2;
1032 static const int MASK_BLUE
= 3;
1033 static const int MASK_BLUE_REPLACEMENT
= 2;
1035 GdkImage
*gdk_image
= (GdkImage
*) NULL
;
1039 gdk_image
= gdk_image_get( GetPixmap(),
1041 GetWidth(), GetHeight() );
1043 else if (GetBitmap())
1045 gdk_image
= gdk_image_get( GetBitmap(),
1047 GetWidth(), GetHeight() );
1051 wxFAIL_MSG( wxT("Ill-formed bitmap") );
1054 wxCHECK_MSG( gdk_image
, wxNullImage
, wxT("couldn't create image") );
1056 GdkImage
*gdk_image_mask
= (GdkImage
*) NULL
;
1059 gdk_image_mask
= gdk_image_get( GetMask()->GetBitmap(),
1061 GetWidth(), GetHeight() );
1063 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
1067 int red_shift_right
= 0;
1068 int green_shift_right
= 0;
1069 int blue_shift_right
= 0;
1070 int red_shift_left
= 0;
1071 int green_shift_left
= 0;
1072 int blue_shift_left
= 0;
1073 bool use_shift
= false;
1077 GdkVisual
*visual
= gdk_window_get_visual( GetPixmap() );
1079 visual
= wxTheApp
->GetGdkVisual();
1081 bpp
= visual
->depth
;
1083 bpp
= visual
->red_prec
+ visual
->green_prec
+ visual
->blue_prec
;
1084 red_shift_right
= visual
->red_shift
;
1085 red_shift_left
= 8-visual
->red_prec
;
1086 green_shift_right
= visual
->green_shift
;
1087 green_shift_left
= 8-visual
->green_prec
;
1088 blue_shift_right
= visual
->blue_shift
;
1089 blue_shift_left
= 8-visual
->blue_prec
;
1091 use_shift
= (visual
->type
== GDK_VISUAL_TRUE_COLOR
) || (visual
->type
== GDK_VISUAL_DIRECT_COLOR
);
1099 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
1102 for (int j
= 0; j
< GetHeight(); j
++)
1104 for (int i
= 0; i
< GetWidth(); i
++)
1106 wxUint32 pixel
= gdk_image_get_pixel( gdk_image
, i
, j
);
1124 data
[pos
] = (pixel
>> red_shift_right
) << red_shift_left
;
1125 data
[pos
+1] = (pixel
>> green_shift_right
) << green_shift_left
;
1126 data
[pos
+2] = (pixel
>> blue_shift_right
) << blue_shift_left
;
1128 else if (cmap
->colors
)
1130 data
[pos
] = cmap
->colors
[pixel
].red
>> 8;
1131 data
[pos
+1] = cmap
->colors
[pixel
].green
>> 8;
1132 data
[pos
+2] = cmap
->colors
[pixel
].blue
>> 8;
1136 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
1141 int mask_pixel
= gdk_image_get_pixel( gdk_image_mask
, i
, j
);
1142 if (mask_pixel
== 0)
1144 data
[pos
] = MASK_RED
;
1145 data
[pos
+1] = MASK_GREEN
;
1146 data
[pos
+2] = MASK_BLUE
;
1148 else if ( data
[pos
] == MASK_RED
&&
1149 data
[pos
+1] == MASK_GREEN
&&
1150 data
[pos
+2] == MASK_BLUE
)
1152 data
[pos
+2] = MASK_BLUE_REPLACEMENT
;
1160 gdk_image_destroy( gdk_image
);
1161 if (gdk_image_mask
) gdk_image_destroy( gdk_image_mask
);
1167 wxBitmap::wxBitmap( const wxBitmap
& bmp
)
1173 wxBitmap::wxBitmap( const wxString
&filename
, wxBitmapType type
)
1175 LoadFile( filename
, type
);
1178 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int WXUNUSED(depth
))
1180 if ( width
> 0 && height
> 0 )
1182 m_refData
= new wxBitmapRefData();
1184 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
1185 M_BMPDATA
->m_bitmap
= gdk_bitmap_create_from_data
1187 wxGetRootWindow()->window
,
1192 M_BMPDATA
->m_width
= width
;
1193 M_BMPDATA
->m_height
= height
;
1194 M_BMPDATA
->m_bpp
= 1;
1196 wxASSERT_MSG( M_BMPDATA
->m_bitmap
, wxT("couldn't create bitmap") );
1200 wxBitmap::~wxBitmap()
1204 wxBitmap
& wxBitmap::operator = ( const wxBitmap
& bmp
)
1206 if ( m_refData
!= bmp
.m_refData
)
1212 bool wxBitmap::operator == ( const wxBitmap
& bmp
) const
1214 return m_refData
== bmp
.m_refData
;
1217 bool wxBitmap::operator != ( const wxBitmap
& bmp
) const
1219 return m_refData
!= bmp
.m_refData
;
1222 bool wxBitmap::Ok() const
1224 return (m_refData
!= NULL
) &&
1227 M_BMPDATA
->m_pixbuf
||
1229 M_BMPDATA
->m_bitmap
|| M_BMPDATA
->m_pixmap
1233 int wxBitmap::GetHeight() const
1235 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1237 return M_BMPDATA
->m_height
;
1240 int wxBitmap::GetWidth() const
1242 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1244 return M_BMPDATA
->m_width
;
1247 int wxBitmap::GetDepth() const
1249 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1251 return M_BMPDATA
->m_bpp
;
1254 wxMask
*wxBitmap::GetMask() const
1256 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
1258 return M_BMPDATA
->m_mask
;
1261 void wxBitmap::SetMask( wxMask
*mask
)
1263 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
1265 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
1267 M_BMPDATA
->m_mask
= mask
;
1270 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
1276 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
1278 wxCHECK_MSG( Ok() &&
1279 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1280 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) && (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
1281 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
1283 wxBitmap
ret( rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
1284 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
1289 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
1290 gdk_pixbuf_get_has_alpha(GetPixbuf()),
1291 8, rect
.width
, rect
.height
);
1292 ret
.SetPixbuf(pixbuf
);
1293 gdk_pixbuf_copy_area(GetPixbuf(),
1294 rect
.x
, rect
.y
, rect
.width
, rect
.height
,
1298 #endif // __WXGTK20__
1300 if (ret
.GetPixmap())
1302 GdkGC
*gc
= gdk_gc_new( ret
.GetPixmap() );
1303 gdk_draw_pixmap( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1304 gdk_gc_destroy( gc
);
1308 GdkGC
*gc
= gdk_gc_new( ret
.GetBitmap() );
1310 col
.pixel
= 0xFFFFFF;
1311 gdk_gc_set_foreground( gc
, &col
);
1313 gdk_gc_set_background( gc
, &col
);
1314 gdk_wx_draw_bitmap( ret
.GetBitmap(), gc
, GetBitmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1315 gdk_gc_destroy( gc
);
1321 wxMask
*mask
= new wxMask
;
1322 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, rect
.width
, rect
.height
, 1 );
1324 GdkGC
*gc
= gdk_gc_new( mask
->m_bitmap
);
1326 col
.pixel
= 0xFFFFFF;
1327 gdk_gc_set_foreground( gc
, &col
);
1329 gdk_gc_set_background( gc
, &col
);
1330 gdk_wx_draw_bitmap( mask
->m_bitmap
, gc
, M_BMPDATA
->m_mask
->m_bitmap
, rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1331 gdk_gc_destroy( gc
);
1333 ret
.SetMask( mask
);
1339 bool wxBitmap::SaveFile( const wxString
&name
, wxBitmapType type
, const wxPalette
*WXUNUSED(palette
) ) const
1341 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1343 // Try to save the bitmap via wxImage handlers:
1345 wxImage image
= ConvertToImage();
1346 if (image
.Ok()) return image
.SaveFile( name
, type
);
1352 bool wxBitmap::LoadFile( const wxString
&name
, wxBitmapType type
)
1356 if (!wxFileExists(name
))
1359 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
1361 if (type
== wxBITMAP_TYPE_XPM
)
1363 m_refData
= new wxBitmapRefData();
1365 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
1367 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm
1369 wxGetRootWindow()->window
,
1377 M_BMPDATA
->m_mask
= new wxMask();
1378 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
1381 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
1383 M_BMPDATA
->m_bpp
= visual
->depth
;
1385 else // try if wxImage can load it
1388 if ( !image
.LoadFile( name
, type
) || !image
.Ok() )
1391 *this = wxBitmap(image
);
1398 wxPalette
*wxBitmap::GetPalette() const
1401 return (wxPalette
*) NULL
;
1403 return M_BMPDATA
->m_palette
;
1406 void wxBitmap::SetPalette(const wxPalette
& WXUNUSED(palette
))
1410 #endif // wxUSE_PALETTE
1412 void wxBitmap::SetHeight( int height
)
1415 m_refData
= new wxBitmapRefData();
1417 M_BMPDATA
->m_height
= height
;
1420 void wxBitmap::SetWidth( int width
)
1423 m_refData
= new wxBitmapRefData();
1425 M_BMPDATA
->m_width
= width
;
1428 void wxBitmap::SetDepth( int depth
)
1431 m_refData
= new wxBitmapRefData();
1433 M_BMPDATA
->m_bpp
= depth
;
1436 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
1439 m_refData
= new wxBitmapRefData();
1441 M_BMPDATA
->m_pixmap
= pixmap
;
1443 PurgeOtherRepresentations(Pixmap
);
1447 void wxBitmap::SetBitmap( GdkPixmap
*bitmap
)
1450 m_refData
= new wxBitmapRefData();
1452 M_BMPDATA
->m_bitmap
= bitmap
;
1454 PurgeOtherRepresentations(Pixmap
);
1458 GdkPixmap
*wxBitmap::GetPixmap() const
1460 wxCHECK_MSG( Ok(), (GdkPixmap
*) NULL
, wxT("invalid bitmap") );
1463 // create the pixmap on the fly if we use Pixbuf representation:
1464 if (HasPixbuf() && !HasPixmap())
1466 delete M_BMPDATA
->m_mask
;
1467 M_BMPDATA
->m_mask
= new wxMask();
1468 gdk_pixbuf_render_pixmap_and_mask(M_BMPDATA
->m_pixbuf
,
1469 &M_BMPDATA
->m_pixmap
,
1470 &M_BMPDATA
->m_mask
->m_bitmap
,
1473 #endif // __WXGTK20__
1475 return M_BMPDATA
->m_pixmap
;
1478 bool wxBitmap::HasPixmap() const
1480 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1482 return M_BMPDATA
->m_pixmap
!= NULL
;
1485 GdkBitmap
*wxBitmap::GetBitmap() const
1487 wxCHECK_MSG( Ok(), (GdkBitmap
*) NULL
, wxT("invalid bitmap") );
1489 return M_BMPDATA
->m_bitmap
;
1493 GdkPixbuf
*wxBitmap::GetPixbuf() const
1495 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
1497 if (HasPixmap() && !HasPixbuf())
1499 int width
= GetWidth();
1500 int height
= GetHeight();
1502 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
1505 M_BMPDATA
->m_pixbuf
=
1506 gdk_pixbuf_get_from_drawable(pixbuf
, M_BMPDATA
->m_pixmap
, NULL
,
1507 0, 0, 0, 0, width
, height
);
1509 // apply the mask to created pixbuf:
1510 if (M_BMPDATA
->m_pixbuf
&& M_BMPDATA
->m_mask
)
1513 gdk_pixbuf_get_from_drawable(NULL
,
1514 M_BMPDATA
->m_mask
->GetBitmap(),
1516 0, 0, 0, 0, width
, height
);
1519 guchar
*bmp
= gdk_pixbuf_get_pixels(pixbuf
);
1520 guchar
*mask
= gdk_pixbuf_get_pixels(pmask
);
1521 int bmprowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
1522 int maskrowinc
= gdk_pixbuf_get_rowstride(pmask
) - 3 * width
;
1524 for (int y
= 0; y
< height
;
1525 y
++, bmp
+= bmprowinc
, mask
+= maskrowinc
)
1527 for (int x
= 0; x
< width
; x
++, bmp
+= 4, mask
+= 3)
1529 if (mask
[0] == 0 /*black pixel*/)
1534 gdk_pixbuf_unref(pmask
);
1539 return M_BMPDATA
->m_pixbuf
;
1542 bool wxBitmap::HasPixbuf() const
1544 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1546 return M_BMPDATA
->m_pixbuf
!= NULL
;
1549 void wxBitmap::SetPixbuf( GdkPixbuf
*pixbuf
)
1552 m_refData
= new wxBitmapRefData();
1554 M_BMPDATA
->m_pixbuf
= pixbuf
;
1555 PurgeOtherRepresentations(Pixbuf
);
1558 void wxBitmap::PurgeOtherRepresentations(wxBitmap::Representation keep
)
1560 if (keep
== Pixmap
&& HasPixbuf())
1562 gdk_pixbuf_unref( M_BMPDATA
->m_pixbuf
);
1563 M_BMPDATA
->m_pixbuf
= NULL
;
1565 if (keep
== Pixbuf
&& HasPixmap())
1567 gdk_pixmap_unref( M_BMPDATA
->m_pixmap
);
1568 M_BMPDATA
->m_pixmap
= NULL
;
1572 #endif // __WXGTK20__
1574 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1580 GdkPixbuf
*pixbuf
= GetPixbuf();
1585 if (gdk_pixbuf_get_has_alpha( pixbuf
))
1586 wxPrintf( wxT("Has alpha\n") );
1588 wxPrintf( wxT("No alpha.\n") );
1591 data
.m_height
= gdk_pixbuf_get_height( pixbuf
);
1592 data
.m_width
= gdk_pixbuf_get_width( pixbuf
);
1593 data
.m_stride
= gdk_pixbuf_get_rowstride( pixbuf
);
1595 return gdk_pixbuf_get_pixels( pixbuf
);
1601 void wxBitmap::UngetRawData(wxPixelDataBase
& WXUNUSED(data
))
1606 bool wxBitmap::HasAlpha() const
1615 void wxBitmap::UseAlpha()
1622 //-----------------------------------------------------------------------------
1624 //-----------------------------------------------------------------------------
1626 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
,wxBitmapHandlerBase
)
1628 wxBitmapHandler::~wxBitmapHandler()
1632 bool wxBitmapHandler::Create(wxBitmap
* WXUNUSED(bitmap
),
1633 void * WXUNUSED(data
),
1634 long WXUNUSED(type
),
1635 int WXUNUSED(width
),
1636 int WXUNUSED(height
),
1637 int WXUNUSED(depth
))
1639 wxFAIL_MSG( _T("not implemented") );
1644 bool wxBitmapHandler::LoadFile(wxBitmap
* WXUNUSED(bitmap
),
1645 const wxString
& WXUNUSED(name
),
1646 long WXUNUSED(flags
),
1647 int WXUNUSED(desiredWidth
),
1648 int WXUNUSED(desiredHeight
))
1650 wxFAIL_MSG( _T("not implemented") );
1655 bool wxBitmapHandler::SaveFile(const wxBitmap
* WXUNUSED(bitmap
),
1656 const wxString
& WXUNUSED(name
),
1658 const wxPalette
* WXUNUSED(palette
))
1660 wxFAIL_MSG( _T("not implemented") );
1665 /* static */ void wxBitmap::InitStandardHandlers()
1667 // TODO: Insert handler based on GdkPixbufs handler later