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"
26 #include "wx/rawbmp.h"
29 // need this to get gdk_image_new_bitmap()
30 #define GDK_ENABLE_BROKEN
38 #include <gdk/gdkimage.h>
40 #include <gdk/gdkrgb.h>
41 #endif // GTK+ 2.0/1.2
45 extern void gdk_wx_draw_bitmap (GdkDrawable
*drawable
,
55 //-----------------------------------------------------------------------------
57 //-----------------------------------------------------------------------------
59 extern GtkWidget
*wxGetRootWindow();
61 //-----------------------------------------------------------------------------
63 //-----------------------------------------------------------------------------
65 IMPLEMENT_DYNAMIC_CLASS(wxMask
,wxObject
)
69 m_bitmap
= (GdkBitmap
*) NULL
;
72 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
74 m_bitmap
= (GdkBitmap
*) NULL
;
75 Create( bitmap
, colour
);
78 wxMask::wxMask( const wxBitmap
& bitmap
, int paletteIndex
)
80 m_bitmap
= (GdkBitmap
*) NULL
;
81 Create( bitmap
, paletteIndex
);
84 wxMask::wxMask( const wxBitmap
& bitmap
)
86 m_bitmap
= (GdkBitmap
*) NULL
;
93 gdk_bitmap_unref( m_bitmap
);
96 bool wxMask::Create( const wxBitmap
& bitmap
,
97 const wxColour
& colour
)
101 gdk_bitmap_unref( m_bitmap
);
102 m_bitmap
= (GdkBitmap
*) NULL
;
105 wxImage image
= bitmap
.ConvertToImage();
106 if (!image
.Ok()) return FALSE
;
108 m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, image
.GetWidth(), image
.GetHeight(), 1 );
109 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
116 gdk_gc_set_foreground( gc
, &color
);
117 gdk_gc_set_fill( gc
, GDK_SOLID
);
118 gdk_draw_rectangle( m_bitmap
, gc
, TRUE
, 0, 0, image
.GetWidth(), image
.GetHeight() );
120 unsigned char *data
= image
.GetData();
123 unsigned char red
= colour
.Red();
124 unsigned char green
= colour
.Green();
125 unsigned char blue
= colour
.Blue();
127 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
129 int bpp
= visual
->depth
;
130 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800))
135 green
= green
& 0xf8;
141 green
= green
& 0xfc;
147 green
= green
& 0xf0;
155 gdk_gc_set_foreground( gc
, &color
);
157 for (int j
= 0; j
< image
.GetHeight(); j
++)
161 for (i
= 0; i
< image
.GetWidth(); i
++)
163 if ((data
[index
] == red
) &&
164 (data
[index
+1] == green
) &&
165 (data
[index
+2] == blue
))
174 gdk_draw_line( m_bitmap
, gc
, start_x
, j
, i
-1, j
);
181 gdk_draw_line( m_bitmap
, gc
, start_x
, j
, i
, j
);
189 bool wxMask::Create( const wxBitmap
& bitmap
, int paletteIndex
)
192 wxPalette
*pal
= bitmap
.GetPalette();
194 wxCHECK_MSG( pal
, FALSE
, wxT("Cannot create mask from bitmap without palette") );
196 pal
->GetRGB(paletteIndex
, &r
, &g
, &b
);
198 return Create(bitmap
, wxColour(r
, g
, b
));
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
);
281 //-----------------------------------------------------------------------------
283 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
285 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
291 wxBitmap::wxBitmap( int width
, int height
, int depth
)
293 Create( width
, height
, depth
);
296 bool wxBitmap::Create( int width
, int height
, int depth
)
300 if ( width
<= 0 || height
<= 0 )
305 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
308 depth
= visual
->depth
;
310 wxCHECK_MSG( (depth
== visual
->depth
) || (depth
== 1) || (depth
== 32), FALSE
,
311 wxT("invalid bitmap depth") )
313 m_refData
= new wxBitmapRefData();
314 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
315 M_BMPDATA
->m_width
= width
;
316 M_BMPDATA
->m_height
= height
;
319 M_BMPDATA
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
320 M_BMPDATA
->m_bpp
= 1;
323 else if (depth
== 32)
325 M_BMPDATA
->m_pixbuf
= gdk_pixbuf_new( GDK_COLORSPACE_RGB
, true,
327 M_BMPDATA
->m_bpp
= 32;
332 M_BMPDATA
->m_pixmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, depth
);
333 M_BMPDATA
->m_bpp
= visual
->depth
;
339 bool wxBitmap::CreateFromXpm( const char **bits
)
343 wxCHECK_MSG( bits
!= NULL
, FALSE
, wxT("invalid bitmap data") )
345 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
347 m_refData
= new wxBitmapRefData();
349 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
351 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm_d( wxGetRootWindow()->window
, &mask
, NULL
, (gchar
**) bits
);
353 wxCHECK_MSG( M_BMPDATA
->m_pixmap
, FALSE
, wxT("couldn't create pixmap") );
357 M_BMPDATA
->m_mask
= new wxMask();
358 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
361 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
363 M_BMPDATA
->m_bpp
= visual
->depth
; // Can we get a different depth from create_from_xpm_d() ?
368 wxBitmap
wxBitmap::Rescale( int clipx
, int clipy
, int clipwidth
, int clipheight
, int newx
, int newy
)
370 wxCHECK_MSG( Ok(), wxNullBitmap
, wxT("invalid bitmap") );
372 if (newy
==M_BMPDATA
->m_width
&& newy
==M_BMPDATA
->m_height
)
375 int width
= wxMax(newx
, 1);
376 int height
= wxMax(newy
, 1);
377 width
= wxMin(width
, clipwidth
);
378 height
= wxMin(height
, clipheight
);
386 bmp
.SetHeight(height
);
387 bmp
.SetDepth(GetDepth());
388 bmp
.SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
389 gdk_pixbuf_get_has_alpha(GetPixbuf()),
391 gdk_pixbuf_scale(GetPixbuf(), bmp
.GetPixbuf(),
394 (double)newx
/GetWidth(), (double)newy
/GetHeight(),
395 GDK_INTERP_BILINEAR
);
398 #endif // __WXGTK20__
400 GdkImage
*img
= (GdkImage
*) NULL
;
402 img
= gdk_image_get( GetPixmap(), 0, 0, GetWidth(), GetHeight() );
403 else if (GetBitmap())
404 img
= gdk_image_get( GetBitmap(), 0, 0, GetWidth(), GetHeight() );
406 wxFAIL_MSG( wxT("Ill-formed bitmap") );
408 wxCHECK_MSG( img
, wxNullBitmap
, wxT("couldn't create image") );
414 GdkPixmap
*dstpix
= NULL
;
417 GdkVisual
*visual
= gdk_window_get_visual( GetPixmap() );
419 visual
= wxTheApp
->GetGdkVisual();
422 bmp
= wxBitmap(width
,height
,bpp
);
423 dstpix
= bmp
.GetPixmap();
424 gc
= gdk_gc_new( dstpix
);
428 long dstbyteperline
= 0;
433 dstbyteperline
= width
/8*M_BMPDATA
->m_bpp
;
434 if (width
*M_BMPDATA
->m_bpp
% 8 != 0)
436 dst
= (char*) malloc(dstbyteperline
*height
);
439 // be careful to use the right scaling factor
440 float scx
= (float)M_BMPDATA
->m_width
/(float)newx
;
441 float scy
= (float)M_BMPDATA
->m_height
/(float)newy
;
442 // prepare accel-tables
443 int *tablex
= (int *)calloc(width
,sizeof(int));
444 int *tabley
= (int *)calloc(height
,sizeof(int));
446 // accel table filled with clipped values
447 for (int x
= 0; x
< width
; x
++)
448 tablex
[x
] = (int) (scx
* (x
+clipx
));
449 for (int y
= 0; y
< height
; y
++)
450 tabley
[y
] = (int) (scy
* (y
+clipy
));
452 // Main rescaling routine starts here
453 for (int h
= 0; h
< height
; h
++)
457 guint32 old_pixval
= 0;
459 for (int w
= 0; w
< width
; w
++)
467 pixval
= gdk_image_get_pixel( img
, x
, tabley
[h
] );
477 char shift
= bit
<< w
% 8;
483 dst
[h
*dstbyteperline
+w
/8] = outbyte
;
491 gdk_gc_set_foreground( gc
, &col
);
492 gdk_draw_point( dstpix
, gc
, w
, h
);
496 // do not forget the last byte
497 if ((bpp
== 1) && (width
% 8 != 0))
498 dst
[h
*dstbyteperline
+width
/8] = outbyte
;
501 gdk_image_destroy( img
);
502 if (gc
) gdk_gc_unref( gc
);
506 bmp
= wxBitmap( (const char *)dst
, width
, height
, 1 );
512 dstbyteperline
= width
/8;
515 dst
= (char*) malloc(dstbyteperline
*height
);
516 img
= gdk_image_get( GetMask()->GetBitmap(), 0, 0, GetWidth(), GetHeight() );
518 for (int h
= 0; h
< height
; h
++)
522 guint32 old_pixval
= 0;
524 for (int w
= 0; w
< width
; w
++)
532 pixval
= gdk_image_get_pixel( img
, x
, tabley
[h
] );
540 char shift
= bit
<< w
% 8;
546 dst
[h
*dstbyteperline
+w
/8] = outbyte
;
551 // do not forget the last byte
553 dst
[h
*dstbyteperline
+width
/8] = outbyte
;
555 wxMask
* mask
= new wxMask
;
556 mask
->m_bitmap
= gdk_bitmap_create_from_data( wxGetRootWindow()->window
, (gchar
*) dst
, width
, height
);
560 gdk_image_destroy( img
);
570 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
574 wxCHECK_MSG( image
.Ok(), FALSE
, wxT("invalid image") )
575 wxCHECK_MSG( depth
== -1 || depth
== 1, FALSE
, wxT("invalid bitmap depth") )
577 if (image
.GetWidth() <= 0 || image
.GetHeight() <= 0)
580 m_refData
= new wxBitmapRefData();
584 return CreateFromImageAsBitmap(image
);
589 if (image
.HasAlpha())
590 return CreateFromImageAsPixbuf(image
);
592 return CreateFromImageAsPixmap(image
);
596 // conversion to mono bitmap:
597 bool wxBitmap::CreateFromImageAsBitmap(const wxImage
& img
)
599 // convert alpha channel to mask, if it is present:
601 image
.ConvertAlphaToMask();
603 int width
= image
.GetWidth();
604 int height
= image
.GetHeight();
609 SetBitmap( gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 ) );
613 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
615 // Create picture image
617 unsigned char *data_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
619 GdkImage
*data_image
=
620 gdk_image_new_bitmap( visual
, data_data
, width
, height
);
624 GdkImage
*mask_image
= (GdkImage
*) NULL
;
628 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
630 mask_image
= gdk_image_new_bitmap( visual
, mask_data
, width
, height
);
632 wxMask
*mask
= new wxMask();
633 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
638 int r_mask
= image
.GetMaskRed();
639 int g_mask
= image
.GetMaskGreen();
640 int b_mask
= image
.GetMaskBlue();
642 unsigned char* data
= image
.GetData();
645 for (int y
= 0; y
< height
; y
++)
647 for (int x
= 0; x
< width
; x
++)
658 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
659 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
661 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
664 if ((r
== 255) && (b
== 255) && (g
== 255))
665 gdk_image_put_pixel( data_image
, x
, y
, 1 );
667 gdk_image_put_pixel( data_image
, x
, y
, 0 );
674 GdkGC
*data_gc
= gdk_gc_new( GetBitmap() );
676 gdk_draw_image( GetBitmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
678 gdk_image_destroy( data_image
);
679 gdk_gc_unref( data_gc
);
685 GdkGC
*mask_gc
= gdk_gc_new( GetMask()->GetBitmap() );
687 gdk_draw_image( GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
689 gdk_image_destroy( mask_image
);
690 gdk_gc_unref( mask_gc
);
696 // conversion to colour bitmap:
697 bool wxBitmap::CreateFromImageAsPixmap(const wxImage
& img
)
699 // convert alpha channel to mask, if it is present:
701 image
.ConvertAlphaToMask();
703 int width
= image
.GetWidth();
704 int height
= image
.GetHeight();
709 SetPixmap( gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, -1 ) );
711 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
713 int bpp
= visual
->depth
;
717 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800))
722 // We handle 8-bit bitmaps ourselves using the colour cube, 12-bit
723 // visuals are not supported by GDK so we do these ourselves, too.
724 // 15-bit and 16-bit should actually work and 24-bit certainly does.
726 if (!image
.HasMask() && (bpp
> 16))
728 if (!image
.HasMask() && (bpp
> 12))
731 static bool s_hasInitialized
= FALSE
;
733 if (!s_hasInitialized
)
736 s_hasInitialized
= TRUE
;
739 GdkGC
*gc
= gdk_gc_new( GetPixmap() );
741 gdk_draw_rgb_image( GetPixmap(),
753 // Create picture image
755 GdkImage
*data_image
=
756 gdk_image_new( GDK_IMAGE_FASTEST
, visual
, width
, height
);
760 GdkImage
*mask_image
= (GdkImage
*) NULL
;
764 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
766 mask_image
= gdk_image_new_bitmap( visual
, mask_data
, width
, height
);
768 wxMask
*mask
= new wxMask();
769 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
776 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
777 byte_order b_o
= RGB
;
781 if ((visual
->red_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->blue_mask
)) b_o
= RGB
;
782 else if ((visual
->red_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->green_mask
)) b_o
= RBG
;
783 else if ((visual
->blue_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->green_mask
)) b_o
= BRG
;
784 else if ((visual
->blue_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->red_mask
)) b_o
= BGR
;
785 else if ((visual
->green_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->blue_mask
)) b_o
= GRB
;
786 else if ((visual
->green_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->red_mask
)) b_o
= GBR
;
789 int r_mask
= image
.GetMaskRed();
790 int g_mask
= image
.GetMaskGreen();
791 int b_mask
= image
.GetMaskBlue();
793 unsigned char* data
= image
.GetData();
796 for (int y
= 0; y
< height
; y
++)
798 for (int x
= 0; x
< width
; x
++)
809 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
810 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
812 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
820 if (wxTheApp
->m_colorCube
)
822 pixel
= wxTheApp
->m_colorCube
[ ((r
& 0xf8) << 7) + ((g
& 0xf8) << 2) + ((b
& 0xf8) >> 3) ];
826 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
827 GdkColor
*colors
= cmap
->colors
;
828 int max
= 3 * (65536);
830 for (int i
= 0; i
< cmap
->size
; i
++)
832 int rdiff
= (r
<< 8) - colors
[i
].red
;
833 int gdiff
= (g
<< 8) - colors
[i
].green
;
834 int bdiff
= (b
<< 8) - colors
[i
].blue
;
835 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
836 if (sum
< max
) { pixel
= i
; max
= sum
; }
840 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
849 case RGB
: pixel
= ((r
& 0xf0) << 4) | (g
& 0xf0) | ((b
& 0xf0) >> 4); break;
850 case RBG
: pixel
= ((r
& 0xf0) << 4) | (b
& 0xf0) | ((g
& 0xf0) >> 4); break;
851 case GRB
: pixel
= ((g
& 0xf0) << 4) | (r
& 0xf0) | ((b
& 0xf0) >> 4); break;
852 case GBR
: pixel
= ((g
& 0xf0) << 4) | (b
& 0xf0) | ((r
& 0xf0) >> 4); break;
853 case BRG
: pixel
= ((b
& 0xf0) << 4) | (r
& 0xf0) | ((g
& 0xf0) >> 4); break;
854 case BGR
: pixel
= ((b
& 0xf0) << 4) | (g
& 0xf0) | ((r
& 0xf0) >> 4); break;
856 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
864 case RGB
: pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
865 case RBG
: pixel
= ((r
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
866 case GRB
: pixel
= ((g
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
867 case GBR
: pixel
= ((g
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
868 case BRG
: pixel
= ((b
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
869 case BGR
: pixel
= ((b
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
871 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
876 // I actually don't know if for 16-bit displays, it is alway the green
877 // component or the second component which has 6 bits.
881 case RGB
: pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
882 case RBG
: pixel
= ((r
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
883 case GRB
: pixel
= ((g
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
884 case GBR
: pixel
= ((g
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
885 case BRG
: pixel
= ((b
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
886 case BGR
: pixel
= ((b
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
888 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
897 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
898 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
899 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
900 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
901 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
902 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
904 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
913 GdkGC
*data_gc
= gdk_gc_new( GetPixmap() );
915 gdk_draw_image( GetPixmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
917 gdk_image_destroy( data_image
);
918 gdk_gc_unref( data_gc
);
924 GdkGC
*mask_gc
= gdk_gc_new( GetMask()->GetBitmap() );
926 gdk_draw_image( GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
928 gdk_image_destroy( mask_image
);
929 gdk_gc_unref( mask_gc
);
936 bool wxBitmap::CreateFromImageAsPixbuf(const wxImage
& image
)
938 int width
= image
.GetWidth();
939 int height
= image
.GetHeight();
941 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
943 8 /* bits per sample */,
948 wxASSERT( image
.HasAlpha() ); // for now
949 wxASSERT( gdk_pixbuf_get_n_channels(pixbuf
) == 4 );
950 wxASSERT( gdk_pixbuf_get_width(pixbuf
) == width
);
951 wxASSERT( gdk_pixbuf_get_height(pixbuf
) == height
);
953 M_BMPDATA
->m_pixbuf
= pixbuf
;
956 SetDepth(wxTheApp
->GetGdkVisual()->depth
);
959 unsigned char *in
= image
.GetData();
960 unsigned char *out
= gdk_pixbuf_get_pixels(pixbuf
);
961 unsigned char *alpha
= image
.GetAlpha();
963 int rowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
965 for (int y
= 0; y
< height
; y
++, out
+= rowinc
)
967 for (int x
= 0; x
< width
; x
++, alpha
++, out
+= 4, in
+= 3)
978 #endif // __WXGTK20__
980 wxImage
wxBitmap::ConvertToImage() const
984 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
986 image
.Create(GetWidth(), GetHeight());
987 unsigned char *data
= image
.GetData();
991 wxFAIL_MSG( wxT("couldn't create image") );
998 GdkPixbuf
*pixbuf
= GetPixbuf();
999 wxASSERT( gdk_pixbuf_get_has_alpha(pixbuf
) );
1002 int h
= GetHeight();
1006 unsigned char *alpha
= image
.GetAlpha();
1007 unsigned char *in
= gdk_pixbuf_get_pixels(pixbuf
);
1008 unsigned char *out
= data
;
1009 int rowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * w
;
1011 for (int y
= 0; y
< h
; y
++, in
+= rowinc
)
1013 for (int x
= 0; x
< w
; x
++, in
+= 4, out
+= 3, alpha
++)
1023 #endif // __WXGTK20__
1025 // the colour used as transparent one in wxImage and the one it is
1026 // replaced with when it really occurs in the bitmap
1027 static const int MASK_RED
= 1;
1028 static const int MASK_GREEN
= 2;
1029 static const int MASK_BLUE
= 3;
1030 static const int MASK_BLUE_REPLACEMENT
= 2;
1032 GdkImage
*gdk_image
= (GdkImage
*) NULL
;
1036 gdk_image
= gdk_image_get( GetPixmap(),
1038 GetWidth(), GetHeight() );
1040 else if (GetBitmap())
1042 gdk_image
= gdk_image_get( GetBitmap(),
1044 GetWidth(), GetHeight() );
1048 wxFAIL_MSG( wxT("Ill-formed bitmap") );
1051 wxCHECK_MSG( gdk_image
, wxNullImage
, wxT("couldn't create image") );
1053 GdkImage
*gdk_image_mask
= (GdkImage
*) NULL
;
1056 gdk_image_mask
= gdk_image_get( GetMask()->GetBitmap(),
1058 GetWidth(), GetHeight() );
1060 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
1064 int red_shift_right
= 0;
1065 int green_shift_right
= 0;
1066 int blue_shift_right
= 0;
1067 int red_shift_left
= 0;
1068 int green_shift_left
= 0;
1069 int blue_shift_left
= 0;
1070 bool use_shift
= FALSE
;
1074 GdkVisual
*visual
= gdk_window_get_visual( GetPixmap() );
1076 visual
= wxTheApp
->GetGdkVisual();
1078 bpp
= visual
->depth
;
1080 bpp
= visual
->red_prec
+ visual
->green_prec
+ visual
->blue_prec
;
1081 red_shift_right
= visual
->red_shift
;
1082 red_shift_left
= 8-visual
->red_prec
;
1083 green_shift_right
= visual
->green_shift
;
1084 green_shift_left
= 8-visual
->green_prec
;
1085 blue_shift_right
= visual
->blue_shift
;
1086 blue_shift_left
= 8-visual
->blue_prec
;
1088 use_shift
= (visual
->type
== GDK_VISUAL_TRUE_COLOR
) || (visual
->type
== GDK_VISUAL_DIRECT_COLOR
);
1096 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
1099 for (int j
= 0; j
< GetHeight(); j
++)
1101 for (int i
= 0; i
< GetWidth(); i
++)
1103 wxUint32 pixel
= gdk_image_get_pixel( gdk_image
, i
, j
);
1121 data
[pos
] = (pixel
>> red_shift_right
) << red_shift_left
;
1122 data
[pos
+1] = (pixel
>> green_shift_right
) << green_shift_left
;
1123 data
[pos
+2] = (pixel
>> blue_shift_right
) << blue_shift_left
;
1125 else if (cmap
->colors
)
1127 data
[pos
] = cmap
->colors
[pixel
].red
>> 8;
1128 data
[pos
+1] = cmap
->colors
[pixel
].green
>> 8;
1129 data
[pos
+2] = cmap
->colors
[pixel
].blue
>> 8;
1133 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
1138 int mask_pixel
= gdk_image_get_pixel( gdk_image_mask
, i
, j
);
1139 if (mask_pixel
== 0)
1141 data
[pos
] = MASK_RED
;
1142 data
[pos
+1] = MASK_GREEN
;
1143 data
[pos
+2] = MASK_BLUE
;
1145 else if ( data
[pos
] == MASK_RED
&&
1146 data
[pos
+1] == MASK_GREEN
&&
1147 data
[pos
+2] == MASK_BLUE
)
1149 data
[pos
+2] = MASK_BLUE_REPLACEMENT
;
1157 gdk_image_destroy( gdk_image
);
1158 if (gdk_image_mask
) gdk_image_destroy( gdk_image_mask
);
1164 wxBitmap::wxBitmap( const wxBitmap
& bmp
)
1170 wxBitmap::wxBitmap( const wxString
&filename
, wxBitmapType type
)
1172 LoadFile( filename
, type
);
1175 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int WXUNUSED(depth
))
1177 if ( width
> 0 && height
> 0 )
1179 m_refData
= new wxBitmapRefData();
1181 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
1182 M_BMPDATA
->m_bitmap
= gdk_bitmap_create_from_data
1184 wxGetRootWindow()->window
,
1189 M_BMPDATA
->m_width
= width
;
1190 M_BMPDATA
->m_height
= height
;
1191 M_BMPDATA
->m_bpp
= 1;
1193 wxASSERT_MSG( M_BMPDATA
->m_bitmap
, wxT("couldn't create bitmap") );
1197 wxBitmap::~wxBitmap()
1201 wxBitmap
& wxBitmap::operator = ( const wxBitmap
& bmp
)
1203 if ( m_refData
!= bmp
.m_refData
)
1209 bool wxBitmap::operator == ( const wxBitmap
& bmp
) const
1211 return m_refData
== bmp
.m_refData
;
1214 bool wxBitmap::operator != ( const wxBitmap
& bmp
) const
1216 return m_refData
!= bmp
.m_refData
;
1219 bool wxBitmap::Ok() const
1221 return (m_refData
!= NULL
) &&
1224 M_BMPDATA
->m_pixbuf
||
1226 M_BMPDATA
->m_bitmap
|| M_BMPDATA
->m_pixmap
1230 int wxBitmap::GetHeight() const
1232 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1234 return M_BMPDATA
->m_height
;
1237 int wxBitmap::GetWidth() const
1239 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1241 return M_BMPDATA
->m_width
;
1244 int wxBitmap::GetDepth() const
1246 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1248 return M_BMPDATA
->m_bpp
;
1251 wxMask
*wxBitmap::GetMask() const
1253 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
1255 return M_BMPDATA
->m_mask
;
1258 void wxBitmap::SetMask( wxMask
*mask
)
1260 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
1262 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
1264 M_BMPDATA
->m_mask
= mask
;
1267 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
1273 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
1275 wxCHECK_MSG( Ok() &&
1276 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1277 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) && (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
1278 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
1280 wxBitmap
ret( rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
1281 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
1286 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
1287 gdk_pixbuf_get_has_alpha(GetPixbuf()),
1288 8, GetWidth(), GetHeight());
1289 ret
.SetPixbuf(pixbuf
);
1290 gdk_pixbuf_copy_area(GetPixbuf(),
1291 rect
.x
, rect
.y
, rect
.width
, rect
.height
,
1295 #endif // __WXGTK20__
1297 if (ret
.GetPixmap())
1299 GdkGC
*gc
= gdk_gc_new( ret
.GetPixmap() );
1300 gdk_draw_pixmap( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1301 gdk_gc_destroy( gc
);
1305 GdkGC
*gc
= gdk_gc_new( ret
.GetBitmap() );
1307 col
.pixel
= 0xFFFFFF;
1308 gdk_gc_set_foreground( gc
, &col
);
1310 gdk_gc_set_background( gc
, &col
);
1311 gdk_wx_draw_bitmap( ret
.GetBitmap(), gc
, GetBitmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1312 gdk_gc_destroy( gc
);
1318 wxMask
*mask
= new wxMask
;
1319 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, rect
.width
, rect
.height
, 1 );
1321 GdkGC
*gc
= gdk_gc_new( mask
->m_bitmap
);
1323 col
.pixel
= 0xFFFFFF;
1324 gdk_gc_set_foreground( gc
, &col
);
1326 gdk_gc_set_background( gc
, &col
);
1327 gdk_wx_draw_bitmap( mask
->m_bitmap
, gc
, M_BMPDATA
->m_mask
->m_bitmap
, rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1328 gdk_gc_destroy( gc
);
1330 ret
.SetMask( mask
);
1336 bool wxBitmap::SaveFile( const wxString
&name
, wxBitmapType type
, const wxPalette
*WXUNUSED(palette
) ) const
1338 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid bitmap") );
1340 // Try to save the bitmap via wxImage handlers:
1342 wxImage image
= ConvertToImage();
1343 if (image
.Ok()) return image
.SaveFile( name
, type
);
1349 bool wxBitmap::LoadFile( const wxString
&name
, wxBitmapType type
)
1353 if (!wxFileExists(name
))
1356 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
1358 if (type
== wxBITMAP_TYPE_XPM
)
1360 m_refData
= new wxBitmapRefData();
1362 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
1364 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm
1366 wxGetRootWindow()->window
,
1374 M_BMPDATA
->m_mask
= new wxMask();
1375 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
1378 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
1380 M_BMPDATA
->m_bpp
= visual
->depth
;
1382 else // try if wxImage can load it
1385 if ( !image
.LoadFile( name
, type
) || !image
.Ok() )
1388 *this = wxBitmap(image
);
1394 wxPalette
*wxBitmap::GetPalette() const
1397 return (wxPalette
*) NULL
;
1399 return M_BMPDATA
->m_palette
;
1402 void wxBitmap::SetPalette(const wxPalette
& WXUNUSED(palette
))
1407 void wxBitmap::SetHeight( int height
)
1410 m_refData
= new wxBitmapRefData();
1412 M_BMPDATA
->m_height
= height
;
1415 void wxBitmap::SetWidth( int width
)
1418 m_refData
= new wxBitmapRefData();
1420 M_BMPDATA
->m_width
= width
;
1423 void wxBitmap::SetDepth( int depth
)
1426 m_refData
= new wxBitmapRefData();
1428 M_BMPDATA
->m_bpp
= depth
;
1431 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
1434 m_refData
= new wxBitmapRefData();
1436 M_BMPDATA
->m_pixmap
= pixmap
;
1438 PurgeOtherRepresentations(Pixmap
);
1442 void wxBitmap::SetBitmap( GdkPixmap
*bitmap
)
1445 m_refData
= new wxBitmapRefData();
1447 M_BMPDATA
->m_bitmap
= bitmap
;
1449 PurgeOtherRepresentations(Pixmap
);
1453 GdkPixmap
*wxBitmap::GetPixmap() const
1455 wxCHECK_MSG( Ok(), (GdkPixmap
*) NULL
, wxT("invalid bitmap") );
1458 // create the pixmap on the fly if we use Pixbuf representation:
1459 if (HasPixbuf() && !HasPixmap())
1461 delete M_BMPDATA
->m_mask
;
1462 M_BMPDATA
->m_mask
= new wxMask();
1463 gdk_pixbuf_render_pixmap_and_mask(M_BMPDATA
->m_pixbuf
,
1464 &M_BMPDATA
->m_pixmap
,
1465 &M_BMPDATA
->m_mask
->m_bitmap
,
1468 #endif // __WXGTK20__
1470 return M_BMPDATA
->m_pixmap
;
1473 bool wxBitmap::HasPixmap() const
1475 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1477 return M_BMPDATA
->m_pixmap
!= NULL
;
1480 GdkBitmap
*wxBitmap::GetBitmap() const
1482 wxCHECK_MSG( Ok(), (GdkBitmap
*) NULL
, wxT("invalid bitmap") );
1484 return M_BMPDATA
->m_bitmap
;
1488 GdkPixbuf
*wxBitmap::GetPixbuf() const
1490 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
1492 if (HasPixmap() && !HasPixbuf())
1494 int width
= GetWidth();
1495 int height
= GetHeight();
1497 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
1500 M_BMPDATA
->m_pixbuf
=
1501 gdk_pixbuf_get_from_drawable(pixbuf
, M_BMPDATA
->m_pixmap
, NULL
,
1502 0, 0, 0, 0, width
, height
);
1504 // apply the mask to created pixbuf:
1505 if (M_BMPDATA
->m_pixbuf
&& M_BMPDATA
->m_mask
)
1508 gdk_pixbuf_get_from_drawable(NULL
,
1509 M_BMPDATA
->m_mask
->GetBitmap(),
1511 0, 0, 0, 0, width
, height
);
1514 guchar
*bmp
= gdk_pixbuf_get_pixels(pixbuf
);
1515 guchar
*mask
= gdk_pixbuf_get_pixels(pmask
);
1516 int bmprowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
1517 int maskrowinc
= gdk_pixbuf_get_rowstride(pmask
) - 3 * width
;
1519 for (int y
= 0; y
< height
;
1520 y
++, bmp
+= bmprowinc
, mask
+= maskrowinc
)
1522 for (int x
= 0; x
< width
; x
++, bmp
+= 4, mask
+= 3)
1524 if (mask
[0] == 0 /*black pixel*/)
1529 gdk_pixbuf_unref(pmask
);
1534 return M_BMPDATA
->m_pixbuf
;
1537 bool wxBitmap::HasPixbuf() const
1539 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1541 return M_BMPDATA
->m_pixbuf
!= NULL
;
1544 void wxBitmap::SetPixbuf( GdkPixbuf
*pixbuf
)
1547 m_refData
= new wxBitmapRefData();
1549 M_BMPDATA
->m_pixbuf
= pixbuf
;
1550 PurgeOtherRepresentations(Pixbuf
);
1553 void wxBitmap::PurgeOtherRepresentations(wxBitmap::Representation keep
)
1555 if (keep
== Pixmap
&& HasPixbuf())
1557 gdk_pixbuf_unref( M_BMPDATA
->m_pixbuf
);
1558 M_BMPDATA
->m_pixbuf
= NULL
;
1560 if (keep
== Pixbuf
&& HasPixmap())
1562 gdk_pixmap_unref( M_BMPDATA
->m_pixmap
);
1563 M_BMPDATA
->m_pixmap
= NULL
;
1567 #endif // __WXGTK20__
1569 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1575 GdkPixbuf
*pixbuf
= GetPixbuf();
1580 if (gdk_pixbuf_get_has_alpha( pixbuf
))
1581 wxPrintf( wxT("Has alpha\n") );
1583 wxPrintf( wxT("No alpha.\n") );
1586 data
.m_height
= gdk_pixbuf_get_height( pixbuf
);
1587 data
.m_width
= gdk_pixbuf_get_width( pixbuf
);
1588 data
.m_stride
= gdk_pixbuf_get_rowstride( pixbuf
);
1590 return gdk_pixbuf_get_pixels( pixbuf
);
1596 void wxBitmap::UngetRawData(wxPixelDataBase
& data
)
1601 bool wxBitmap::HasAlpha() const
1610 void wxBitmap::UseAlpha()
1617 //-----------------------------------------------------------------------------
1619 //-----------------------------------------------------------------------------
1621 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
,wxBitmapHandlerBase
)
1623 wxBitmapHandler::~wxBitmapHandler()
1627 bool wxBitmapHandler::Create(wxBitmap
*bitmap
, void *data
, long type
, int width
, int height
, int depth
)
1632 bool wxBitmapHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1633 int desiredWidth
, int desiredHeight
)
1638 bool wxBitmapHandler::SaveFile(const wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
)
1643 /* static */ void wxBitmap::InitStandardHandlers()
1645 // TODO: Insert handler based on GdkPixbufs handler later