1 /////////////////////////////////////////////////////////////////////////////
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
);
74 wxMask::wxMask( const wxBitmap
& bitmap
, int paletteIndex
)
76 m_bitmap
= (GdkBitmap
*) NULL
;
77 Create( bitmap
, paletteIndex
);
80 wxMask::wxMask( const wxBitmap
& bitmap
)
82 m_bitmap
= (GdkBitmap
*) NULL
;
89 gdk_bitmap_unref( m_bitmap
);
92 bool wxMask::Create( const wxBitmap
& bitmap
,
93 const wxColour
& colour
)
97 gdk_bitmap_unref( m_bitmap
);
98 m_bitmap
= (GdkBitmap
*) NULL
;
101 wxImage image
= bitmap
.ConvertToImage();
102 if (!image
.Ok()) return false;
104 m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, image
.GetWidth(), image
.GetHeight(), 1 );
105 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
112 gdk_gc_set_foreground( gc
, &color
);
113 gdk_gc_set_fill( gc
, GDK_SOLID
);
114 gdk_draw_rectangle( m_bitmap
, gc
, TRUE
, 0, 0, image
.GetWidth(), image
.GetHeight() );
116 unsigned char *data
= image
.GetData();
119 unsigned char red
= colour
.Red();
120 unsigned char green
= colour
.Green();
121 unsigned char blue
= colour
.Blue();
123 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
125 int bpp
= visual
->depth
;
126 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800))
131 green
= green
& 0xf8;
137 green
= green
& 0xfc;
143 green
= green
& 0xf0;
151 gdk_gc_set_foreground( gc
, &color
);
153 for (int j
= 0; j
< image
.GetHeight(); j
++)
157 for (i
= 0; i
< image
.GetWidth(); i
++)
159 if ((data
[index
] == red
) &&
160 (data
[index
+1] == green
) &&
161 (data
[index
+2] == blue
))
170 gdk_draw_line( m_bitmap
, gc
, start_x
, j
, i
-1, j
);
177 gdk_draw_line( m_bitmap
, gc
, start_x
, j
, i
, j
);
185 bool wxMask::Create( const wxBitmap
& bitmap
, int paletteIndex
)
188 wxPalette
*pal
= bitmap
.GetPalette();
190 wxCHECK_MSG( pal
, false, wxT("Cannot create mask from bitmap without palette") );
192 pal
->GetRGB(paletteIndex
, &r
, &g
, &b
);
194 return Create(bitmap
, wxColour(r
, g
, b
));
197 bool wxMask::Create( const wxBitmap
& bitmap
)
201 gdk_bitmap_unref( m_bitmap
);
202 m_bitmap
= (GdkBitmap
*) NULL
;
205 if (!bitmap
.Ok()) return false;
207 wxCHECK_MSG( bitmap
.GetBitmap(), false, wxT("Cannot create mask from colour bitmap") );
209 m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, bitmap
.GetWidth(), bitmap
.GetHeight(), 1 );
211 if (!m_bitmap
) return false;
213 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
215 gdk_wx_draw_bitmap( m_bitmap
, gc
, bitmap
.GetBitmap(), 0, 0, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight() );
222 GdkBitmap
*wxMask::GetBitmap() const
227 //-----------------------------------------------------------------------------
229 //-----------------------------------------------------------------------------
231 class wxBitmapRefData
: public wxObjectRefData
246 wxPalette
*m_palette
;
249 wxBitmapRefData::wxBitmapRefData()
251 m_pixmap
= (GdkPixmap
*) NULL
;
252 m_bitmap
= (GdkBitmap
*) NULL
;
254 m_pixbuf
= (GdkPixbuf
*) NULL
;
256 m_mask
= (wxMask
*) NULL
;
260 m_palette
= (wxPalette
*) NULL
;
263 wxBitmapRefData::~wxBitmapRefData()
266 gdk_pixmap_unref( m_pixmap
);
268 gdk_bitmap_unref( m_bitmap
);
271 gdk_pixbuf_unref( m_pixbuf
);
277 //-----------------------------------------------------------------------------
279 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
281 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
287 wxBitmap::wxBitmap( int width
, int height
, int depth
)
289 Create( width
, height
, depth
);
292 bool wxBitmap::Create( int width
, int height
, int depth
)
296 if ( width
<= 0 || height
<= 0 )
301 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
304 depth
= visual
->depth
;
306 wxCHECK_MSG( (depth
== visual
->depth
) || (depth
== 1) || (depth
== 32), false,
307 wxT("invalid bitmap depth") )
309 m_refData
= new wxBitmapRefData();
310 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
311 M_BMPDATA
->m_width
= width
;
312 M_BMPDATA
->m_height
= height
;
315 M_BMPDATA
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
316 M_BMPDATA
->m_bpp
= 1;
319 else if (depth
== 32)
321 M_BMPDATA
->m_pixbuf
= gdk_pixbuf_new( GDK_COLORSPACE_RGB
, true,
323 M_BMPDATA
->m_bpp
= 32;
328 M_BMPDATA
->m_pixmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, depth
);
329 M_BMPDATA
->m_bpp
= visual
->depth
;
335 bool wxBitmap::CreateFromXpm( const char **bits
)
339 wxCHECK_MSG( bits
!= NULL
, false, wxT("invalid bitmap data") )
341 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
343 m_refData
= new wxBitmapRefData();
345 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
347 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm_d( wxGetRootWindow()->window
, &mask
, NULL
, (gchar
**) bits
);
349 wxCHECK_MSG( M_BMPDATA
->m_pixmap
, false, wxT("couldn't create pixmap") );
353 M_BMPDATA
->m_mask
= new wxMask();
354 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
357 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
359 M_BMPDATA
->m_bpp
= visual
->depth
; // Can we get a different depth from create_from_xpm_d() ?
364 wxBitmap
wxBitmap::Rescale( int clipx
, int clipy
, int clipwidth
, int clipheight
, int newx
, int newy
)
366 wxCHECK_MSG( Ok(), wxNullBitmap
, wxT("invalid bitmap") );
368 if (newy
==M_BMPDATA
->m_width
&& newy
==M_BMPDATA
->m_height
)
371 int width
= wxMax(newx
, 1);
372 int height
= wxMax(newy
, 1);
373 width
= wxMin(width
, clipwidth
);
374 height
= wxMin(height
, clipheight
);
382 bmp
.SetHeight(height
);
383 bmp
.SetDepth(GetDepth());
384 bmp
.SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
385 gdk_pixbuf_get_has_alpha(GetPixbuf()),
387 gdk_pixbuf_scale(GetPixbuf(), bmp
.GetPixbuf(),
390 (double)newx
/GetWidth(), (double)newy
/GetHeight(),
391 GDK_INTERP_BILINEAR
);
394 #endif // __WXGTK20__
396 GdkImage
*img
= (GdkImage
*) NULL
;
398 img
= gdk_image_get( GetPixmap(), 0, 0, GetWidth(), GetHeight() );
399 else if (GetBitmap())
400 img
= gdk_image_get( GetBitmap(), 0, 0, GetWidth(), GetHeight() );
402 wxFAIL_MSG( wxT("Ill-formed bitmap") );
404 wxCHECK_MSG( img
, wxNullBitmap
, wxT("couldn't create image") );
410 GdkPixmap
*dstpix
= NULL
;
413 GdkVisual
*visual
= gdk_window_get_visual( GetPixmap() );
415 visual
= wxTheApp
->GetGdkVisual();
418 bmp
= wxBitmap(width
,height
,bpp
);
419 dstpix
= bmp
.GetPixmap();
420 gc
= gdk_gc_new( dstpix
);
424 long dstbyteperline
= 0;
429 dstbyteperline
= width
/8*M_BMPDATA
->m_bpp
;
430 if (width
*M_BMPDATA
->m_bpp
% 8 != 0)
432 dst
= (char*) malloc(dstbyteperline
*height
);
435 // be careful to use the right scaling factor
436 float scx
= (float)M_BMPDATA
->m_width
/(float)newx
;
437 float scy
= (float)M_BMPDATA
->m_height
/(float)newy
;
438 // prepare accel-tables
439 int *tablex
= (int *)calloc(width
,sizeof(int));
440 int *tabley
= (int *)calloc(height
,sizeof(int));
442 // accel table filled with clipped values
443 for (int x
= 0; x
< width
; x
++)
444 tablex
[x
] = (int) (scx
* (x
+clipx
));
445 for (int y
= 0; y
< height
; y
++)
446 tabley
[y
] = (int) (scy
* (y
+clipy
));
448 // Main rescaling routine starts here
449 for (int h
= 0; h
< height
; h
++)
453 guint32 old_pixval
= 0;
455 for (int w
= 0; w
< width
; w
++)
463 pixval
= gdk_image_get_pixel( img
, x
, tabley
[h
] );
473 char shift
= bit
<< (w
% 8);
479 dst
[h
*dstbyteperline
+w
/8] = outbyte
;
487 gdk_gc_set_foreground( gc
, &col
);
488 gdk_draw_point( dstpix
, gc
, w
, h
);
492 // do not forget the last byte
493 if ((bpp
== 1) && (width
% 8 != 0))
494 dst
[h
*dstbyteperline
+width
/8] = outbyte
;
497 gdk_image_destroy( img
);
498 if (gc
) gdk_gc_unref( gc
);
502 bmp
= wxBitmap( (const char *)dst
, width
, height
, 1 );
508 dstbyteperline
= width
/8;
511 dst
= (char*) malloc(dstbyteperline
*height
);
512 img
= gdk_image_get( GetMask()->GetBitmap(), 0, 0, GetWidth(), GetHeight() );
514 for (int h
= 0; h
< height
; h
++)
518 guint32 old_pixval
= 0;
520 for (int w
= 0; w
< width
; w
++)
528 pixval
= gdk_image_get_pixel( img
, x
, tabley
[h
] );
536 char shift
= bit
<< (w
% 8);
542 dst
[h
*dstbyteperline
+w
/8] = outbyte
;
547 // do not forget the last byte
549 dst
[h
*dstbyteperline
+width
/8] = outbyte
;
551 wxMask
* mask
= new wxMask
;
552 mask
->m_bitmap
= gdk_bitmap_create_from_data( wxGetRootWindow()->window
, (gchar
*) dst
, width
, height
);
556 gdk_image_destroy( img
);
566 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
570 wxCHECK_MSG( image
.Ok(), false, wxT("invalid image") )
571 wxCHECK_MSG( depth
== -1 || depth
== 1, false, wxT("invalid bitmap depth") )
573 if (image
.GetWidth() <= 0 || image
.GetHeight() <= 0)
576 m_refData
= new wxBitmapRefData();
580 return CreateFromImageAsBitmap(image
);
585 if (image
.HasAlpha())
586 return CreateFromImageAsPixbuf(image
);
588 return CreateFromImageAsPixmap(image
);
592 // conversion to mono bitmap:
593 bool wxBitmap::CreateFromImageAsBitmap(const wxImage
& img
)
595 // convert alpha channel to mask, if it is present:
597 image
.ConvertAlphaToMask();
599 int width
= image
.GetWidth();
600 int height
= image
.GetHeight();
605 SetBitmap( gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 ) );
609 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
611 // Create picture image
613 unsigned char *data_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
615 GdkImage
*data_image
=
616 gdk_image_new_bitmap( visual
, data_data
, width
, height
);
620 GdkImage
*mask_image
= (GdkImage
*) NULL
;
624 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
626 mask_image
= gdk_image_new_bitmap( visual
, mask_data
, width
, height
);
628 wxMask
*mask
= new wxMask();
629 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
634 int r_mask
= image
.GetMaskRed();
635 int g_mask
= image
.GetMaskGreen();
636 int b_mask
= image
.GetMaskBlue();
638 unsigned char* data
= image
.GetData();
641 for (int y
= 0; y
< height
; y
++)
643 for (int x
= 0; x
< width
; x
++)
654 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
655 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
657 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
660 if ((r
== 255) && (b
== 255) && (g
== 255))
661 gdk_image_put_pixel( data_image
, x
, y
, 1 );
663 gdk_image_put_pixel( data_image
, x
, y
, 0 );
670 GdkGC
*data_gc
= gdk_gc_new( GetBitmap() );
672 gdk_draw_image( GetBitmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
674 gdk_image_destroy( data_image
);
675 gdk_gc_unref( data_gc
);
681 GdkGC
*mask_gc
= gdk_gc_new( GetMask()->GetBitmap() );
683 gdk_draw_image( GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
685 gdk_image_destroy( mask_image
);
686 gdk_gc_unref( mask_gc
);
692 // conversion to colour bitmap:
693 bool wxBitmap::CreateFromImageAsPixmap(const wxImage
& img
)
695 // convert alpha channel to mask, if it is present:
697 image
.ConvertAlphaToMask();
699 int width
= image
.GetWidth();
700 int height
= image
.GetHeight();
705 SetPixmap( gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, -1 ) );
707 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
709 int bpp
= visual
->depth
;
713 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800))
718 // We handle 8-bit bitmaps ourselves using the colour cube, 12-bit
719 // visuals are not supported by GDK so we do these ourselves, too.
720 // 15-bit and 16-bit should actually work and 24-bit certainly does.
722 if (!image
.HasMask() && (bpp
> 16))
724 if (!image
.HasMask() && (bpp
> 12))
727 static bool s_hasInitialized
= false;
729 if (!s_hasInitialized
)
732 s_hasInitialized
= true;
735 GdkGC
*gc
= gdk_gc_new( GetPixmap() );
737 gdk_draw_rgb_image( GetPixmap(),
749 // Create picture image
751 GdkImage
*data_image
=
752 gdk_image_new( GDK_IMAGE_FASTEST
, visual
, width
, height
);
756 GdkImage
*mask_image
= (GdkImage
*) NULL
;
760 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
762 mask_image
= gdk_image_new_bitmap( visual
, mask_data
, width
, height
);
764 wxMask
*mask
= new wxMask();
765 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
772 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
773 byte_order b_o
= RGB
;
777 if ((visual
->red_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->blue_mask
)) b_o
= RGB
;
778 else if ((visual
->red_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->green_mask
)) b_o
= RBG
;
779 else if ((visual
->blue_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->green_mask
)) b_o
= BRG
;
780 else if ((visual
->blue_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->red_mask
)) b_o
= BGR
;
781 else if ((visual
->green_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->blue_mask
)) b_o
= GRB
;
782 else if ((visual
->green_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->red_mask
)) b_o
= GBR
;
785 int r_mask
= image
.GetMaskRed();
786 int g_mask
= image
.GetMaskGreen();
787 int b_mask
= image
.GetMaskBlue();
789 unsigned char* data
= image
.GetData();
792 for (int y
= 0; y
< height
; y
++)
794 for (int x
= 0; x
< width
; x
++)
805 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
806 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
808 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
816 if (wxTheApp
->m_colorCube
)
818 pixel
= wxTheApp
->m_colorCube
[ ((r
& 0xf8) << 7) + ((g
& 0xf8) << 2) + ((b
& 0xf8) >> 3) ];
822 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
823 GdkColor
*colors
= cmap
->colors
;
824 int max
= 3 * (65536);
826 for (int i
= 0; i
< cmap
->size
; i
++)
828 int rdiff
= (r
<< 8) - colors
[i
].red
;
829 int gdiff
= (g
<< 8) - colors
[i
].green
;
830 int bdiff
= (b
<< 8) - colors
[i
].blue
;
831 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
832 if (sum
< max
) { pixel
= i
; max
= sum
; }
836 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
845 case RGB
: pixel
= ((r
& 0xf0) << 4) | (g
& 0xf0) | ((b
& 0xf0) >> 4); break;
846 case RBG
: pixel
= ((r
& 0xf0) << 4) | (b
& 0xf0) | ((g
& 0xf0) >> 4); break;
847 case GRB
: pixel
= ((g
& 0xf0) << 4) | (r
& 0xf0) | ((b
& 0xf0) >> 4); break;
848 case GBR
: pixel
= ((g
& 0xf0) << 4) | (b
& 0xf0) | ((r
& 0xf0) >> 4); break;
849 case BRG
: pixel
= ((b
& 0xf0) << 4) | (r
& 0xf0) | ((g
& 0xf0) >> 4); break;
850 case BGR
: pixel
= ((b
& 0xf0) << 4) | (g
& 0xf0) | ((r
& 0xf0) >> 4); break;
852 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
860 case RGB
: pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
861 case RBG
: pixel
= ((r
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
862 case GRB
: pixel
= ((g
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
863 case GBR
: pixel
= ((g
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
864 case BRG
: pixel
= ((b
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
865 case BGR
: pixel
= ((b
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
867 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
872 // I actually don't know if for 16-bit displays, it is alway the green
873 // component or the second component which has 6 bits.
877 case RGB
: pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
878 case RBG
: pixel
= ((r
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
879 case GRB
: pixel
= ((g
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
880 case GBR
: pixel
= ((g
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
881 case BRG
: pixel
= ((b
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
882 case BGR
: pixel
= ((b
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
884 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
893 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
894 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
895 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
896 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
897 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
898 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
900 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
910 GdkGC
*data_gc
= gdk_gc_new( GetPixmap() );
912 gdk_draw_image( GetPixmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
914 gdk_image_destroy( data_image
);
915 gdk_gc_unref( data_gc
);
921 GdkGC
*mask_gc
= gdk_gc_new( GetMask()->GetBitmap() );
923 gdk_draw_image( GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
925 gdk_image_destroy( mask_image
);
926 gdk_gc_unref( mask_gc
);
933 bool wxBitmap::CreateFromImageAsPixbuf(const wxImage
& image
)
935 int width
= image
.GetWidth();
936 int height
= image
.GetHeight();
938 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
940 8 /* bits per sample */,
945 wxASSERT( image
.HasAlpha() ); // for now
946 wxASSERT( gdk_pixbuf_get_n_channels(pixbuf
) == 4 );
947 wxASSERT( gdk_pixbuf_get_width(pixbuf
) == width
);
948 wxASSERT( gdk_pixbuf_get_height(pixbuf
) == height
);
950 M_BMPDATA
->m_pixbuf
= pixbuf
;
953 SetDepth(wxTheApp
->GetGdkVisual()->depth
);
956 unsigned char *in
= image
.GetData();
957 unsigned char *out
= gdk_pixbuf_get_pixels(pixbuf
);
958 unsigned char *alpha
= image
.GetAlpha();
960 int rowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
962 for (int y
= 0; y
< height
; y
++, out
+= rowinc
)
964 for (int x
= 0; x
< width
; x
++, alpha
++, out
+= 4, in
+= 3)
975 #endif // __WXGTK20__
977 wxImage
wxBitmap::ConvertToImage() const
981 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
983 image
.Create(GetWidth(), GetHeight());
984 unsigned char *data
= image
.GetData();
988 wxFAIL_MSG( wxT("couldn't create image") );
995 GdkPixbuf
*pixbuf
= GetPixbuf();
996 wxASSERT( gdk_pixbuf_get_has_alpha(pixbuf
) );
1003 unsigned char *alpha
= image
.GetAlpha();
1004 unsigned char *in
= gdk_pixbuf_get_pixels(pixbuf
);
1005 unsigned char *out
= data
;
1006 int rowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * w
;
1008 for (int y
= 0; y
< h
; y
++, in
+= rowinc
)
1010 for (int x
= 0; x
< w
; x
++, in
+= 4, out
+= 3, alpha
++)
1020 #endif // __WXGTK20__
1022 // the colour used as transparent one in wxImage and the one it is
1023 // replaced with when it really occurs in the bitmap
1024 static const int MASK_RED
= 1;
1025 static const int MASK_GREEN
= 2;
1026 static const int MASK_BLUE
= 3;
1027 static const int MASK_BLUE_REPLACEMENT
= 2;
1029 GdkImage
*gdk_image
= (GdkImage
*) NULL
;
1033 gdk_image
= gdk_image_get( GetPixmap(),
1035 GetWidth(), GetHeight() );
1037 else if (GetBitmap())
1039 gdk_image
= gdk_image_get( GetBitmap(),
1041 GetWidth(), GetHeight() );
1045 wxFAIL_MSG( wxT("Ill-formed bitmap") );
1048 wxCHECK_MSG( gdk_image
, wxNullImage
, wxT("couldn't create image") );
1050 GdkImage
*gdk_image_mask
= (GdkImage
*) NULL
;
1053 gdk_image_mask
= gdk_image_get( GetMask()->GetBitmap(),
1055 GetWidth(), GetHeight() );
1057 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
1061 int red_shift_right
= 0;
1062 int green_shift_right
= 0;
1063 int blue_shift_right
= 0;
1064 int red_shift_left
= 0;
1065 int green_shift_left
= 0;
1066 int blue_shift_left
= 0;
1067 bool use_shift
= false;
1071 GdkVisual
*visual
= gdk_window_get_visual( GetPixmap() );
1073 visual
= wxTheApp
->GetGdkVisual();
1075 bpp
= visual
->depth
;
1077 bpp
= visual
->red_prec
+ visual
->green_prec
+ visual
->blue_prec
;
1078 red_shift_right
= visual
->red_shift
;
1079 red_shift_left
= 8-visual
->red_prec
;
1080 green_shift_right
= visual
->green_shift
;
1081 green_shift_left
= 8-visual
->green_prec
;
1082 blue_shift_right
= visual
->blue_shift
;
1083 blue_shift_left
= 8-visual
->blue_prec
;
1085 use_shift
= (visual
->type
== GDK_VISUAL_TRUE_COLOR
) || (visual
->type
== GDK_VISUAL_DIRECT_COLOR
);
1093 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
1096 for (int j
= 0; j
< GetHeight(); j
++)
1098 for (int i
= 0; i
< GetWidth(); i
++)
1100 wxUint32 pixel
= gdk_image_get_pixel( gdk_image
, i
, j
);
1118 data
[pos
] = (pixel
>> red_shift_right
) << red_shift_left
;
1119 data
[pos
+1] = (pixel
>> green_shift_right
) << green_shift_left
;
1120 data
[pos
+2] = (pixel
>> blue_shift_right
) << blue_shift_left
;
1122 else if (cmap
->colors
)
1124 data
[pos
] = cmap
->colors
[pixel
].red
>> 8;
1125 data
[pos
+1] = cmap
->colors
[pixel
].green
>> 8;
1126 data
[pos
+2] = cmap
->colors
[pixel
].blue
>> 8;
1130 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
1135 int mask_pixel
= gdk_image_get_pixel( gdk_image_mask
, i
, j
);
1136 if (mask_pixel
== 0)
1138 data
[pos
] = MASK_RED
;
1139 data
[pos
+1] = MASK_GREEN
;
1140 data
[pos
+2] = MASK_BLUE
;
1142 else if ( data
[pos
] == MASK_RED
&&
1143 data
[pos
+1] == MASK_GREEN
&&
1144 data
[pos
+2] == MASK_BLUE
)
1146 data
[pos
+2] = MASK_BLUE_REPLACEMENT
;
1154 gdk_image_destroy( gdk_image
);
1155 if (gdk_image_mask
) gdk_image_destroy( gdk_image_mask
);
1161 wxBitmap::wxBitmap( const wxBitmap
& bmp
)
1167 wxBitmap::wxBitmap( const wxString
&filename
, wxBitmapType type
)
1169 LoadFile( filename
, type
);
1172 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int WXUNUSED(depth
))
1174 if ( width
> 0 && height
> 0 )
1176 m_refData
= new wxBitmapRefData();
1178 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
1179 M_BMPDATA
->m_bitmap
= gdk_bitmap_create_from_data
1181 wxGetRootWindow()->window
,
1186 M_BMPDATA
->m_width
= width
;
1187 M_BMPDATA
->m_height
= height
;
1188 M_BMPDATA
->m_bpp
= 1;
1190 wxASSERT_MSG( M_BMPDATA
->m_bitmap
, wxT("couldn't create bitmap") );
1194 wxBitmap::~wxBitmap()
1198 wxBitmap
& wxBitmap::operator = ( const wxBitmap
& bmp
)
1200 if ( m_refData
!= bmp
.m_refData
)
1206 bool wxBitmap::operator == ( const wxBitmap
& bmp
) const
1208 return m_refData
== bmp
.m_refData
;
1211 bool wxBitmap::operator != ( const wxBitmap
& bmp
) const
1213 return m_refData
!= bmp
.m_refData
;
1216 bool wxBitmap::Ok() const
1218 return (m_refData
!= NULL
) &&
1221 M_BMPDATA
->m_pixbuf
||
1223 M_BMPDATA
->m_bitmap
|| M_BMPDATA
->m_pixmap
1227 int wxBitmap::GetHeight() const
1229 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1231 return M_BMPDATA
->m_height
;
1234 int wxBitmap::GetWidth() const
1236 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1238 return M_BMPDATA
->m_width
;
1241 int wxBitmap::GetDepth() const
1243 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1245 return M_BMPDATA
->m_bpp
;
1248 wxMask
*wxBitmap::GetMask() const
1250 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
1252 return M_BMPDATA
->m_mask
;
1255 void wxBitmap::SetMask( wxMask
*mask
)
1257 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
1259 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
1261 M_BMPDATA
->m_mask
= mask
;
1264 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
1270 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
1272 wxCHECK_MSG( Ok() &&
1273 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1274 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) && (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
1275 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
1277 wxBitmap
ret( rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
1278 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
1283 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
1284 gdk_pixbuf_get_has_alpha(GetPixbuf()),
1285 8, rect
.width
, rect
.height
);
1286 ret
.SetPixbuf(pixbuf
);
1287 gdk_pixbuf_copy_area(GetPixbuf(),
1288 rect
.x
, rect
.y
, rect
.width
, rect
.height
,
1292 #endif // __WXGTK20__
1294 if (ret
.GetPixmap())
1296 GdkGC
*gc
= gdk_gc_new( ret
.GetPixmap() );
1297 gdk_draw_pixmap( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1298 gdk_gc_destroy( gc
);
1302 GdkGC
*gc
= gdk_gc_new( ret
.GetBitmap() );
1304 col
.pixel
= 0xFFFFFF;
1305 gdk_gc_set_foreground( gc
, &col
);
1307 gdk_gc_set_background( gc
, &col
);
1308 gdk_wx_draw_bitmap( ret
.GetBitmap(), gc
, GetBitmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1309 gdk_gc_destroy( gc
);
1315 wxMask
*mask
= new wxMask
;
1316 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, rect
.width
, rect
.height
, 1 );
1318 GdkGC
*gc
= gdk_gc_new( mask
->m_bitmap
);
1320 col
.pixel
= 0xFFFFFF;
1321 gdk_gc_set_foreground( gc
, &col
);
1323 gdk_gc_set_background( gc
, &col
);
1324 gdk_wx_draw_bitmap( mask
->m_bitmap
, gc
, M_BMPDATA
->m_mask
->m_bitmap
, rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1325 gdk_gc_destroy( gc
);
1327 ret
.SetMask( mask
);
1333 bool wxBitmap::SaveFile( const wxString
&name
, wxBitmapType type
, const wxPalette
*WXUNUSED(palette
) ) const
1335 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1337 // Try to save the bitmap via wxImage handlers:
1339 wxImage image
= ConvertToImage();
1340 if (image
.Ok()) return image
.SaveFile( name
, type
);
1346 bool wxBitmap::LoadFile( const wxString
&name
, wxBitmapType type
)
1350 if (!wxFileExists(name
))
1353 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
1355 if (type
== wxBITMAP_TYPE_XPM
)
1357 m_refData
= new wxBitmapRefData();
1359 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
1361 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm
1363 wxGetRootWindow()->window
,
1371 M_BMPDATA
->m_mask
= new wxMask();
1372 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
1375 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
1377 M_BMPDATA
->m_bpp
= visual
->depth
;
1379 else // try if wxImage can load it
1382 if ( !image
.LoadFile( name
, type
) || !image
.Ok() )
1385 *this = wxBitmap(image
);
1391 wxPalette
*wxBitmap::GetPalette() const
1394 return (wxPalette
*) NULL
;
1396 return M_BMPDATA
->m_palette
;
1399 void wxBitmap::SetPalette(const wxPalette
& WXUNUSED(palette
))
1404 void wxBitmap::SetHeight( int height
)
1407 m_refData
= new wxBitmapRefData();
1409 M_BMPDATA
->m_height
= height
;
1412 void wxBitmap::SetWidth( int width
)
1415 m_refData
= new wxBitmapRefData();
1417 M_BMPDATA
->m_width
= width
;
1420 void wxBitmap::SetDepth( int depth
)
1423 m_refData
= new wxBitmapRefData();
1425 M_BMPDATA
->m_bpp
= depth
;
1428 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
1431 m_refData
= new wxBitmapRefData();
1433 M_BMPDATA
->m_pixmap
= pixmap
;
1435 PurgeOtherRepresentations(Pixmap
);
1439 void wxBitmap::SetBitmap( GdkPixmap
*bitmap
)
1442 m_refData
= new wxBitmapRefData();
1444 M_BMPDATA
->m_bitmap
= bitmap
;
1446 PurgeOtherRepresentations(Pixmap
);
1450 GdkPixmap
*wxBitmap::GetPixmap() const
1452 wxCHECK_MSG( Ok(), (GdkPixmap
*) NULL
, wxT("invalid bitmap") );
1455 // create the pixmap on the fly if we use Pixbuf representation:
1456 if (HasPixbuf() && !HasPixmap())
1458 delete M_BMPDATA
->m_mask
;
1459 M_BMPDATA
->m_mask
= new wxMask();
1460 gdk_pixbuf_render_pixmap_and_mask(M_BMPDATA
->m_pixbuf
,
1461 &M_BMPDATA
->m_pixmap
,
1462 &M_BMPDATA
->m_mask
->m_bitmap
,
1465 #endif // __WXGTK20__
1467 return M_BMPDATA
->m_pixmap
;
1470 bool wxBitmap::HasPixmap() const
1472 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1474 return M_BMPDATA
->m_pixmap
!= NULL
;
1477 GdkBitmap
*wxBitmap::GetBitmap() const
1479 wxCHECK_MSG( Ok(), (GdkBitmap
*) NULL
, wxT("invalid bitmap") );
1481 return M_BMPDATA
->m_bitmap
;
1485 GdkPixbuf
*wxBitmap::GetPixbuf() const
1487 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
1489 if (HasPixmap() && !HasPixbuf())
1491 int width
= GetWidth();
1492 int height
= GetHeight();
1494 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
1497 M_BMPDATA
->m_pixbuf
=
1498 gdk_pixbuf_get_from_drawable(pixbuf
, M_BMPDATA
->m_pixmap
, NULL
,
1499 0, 0, 0, 0, width
, height
);
1501 // apply the mask to created pixbuf:
1502 if (M_BMPDATA
->m_pixbuf
&& M_BMPDATA
->m_mask
)
1505 gdk_pixbuf_get_from_drawable(NULL
,
1506 M_BMPDATA
->m_mask
->GetBitmap(),
1508 0, 0, 0, 0, width
, height
);
1511 guchar
*bmp
= gdk_pixbuf_get_pixels(pixbuf
);
1512 guchar
*mask
= gdk_pixbuf_get_pixels(pmask
);
1513 int bmprowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
1514 int maskrowinc
= gdk_pixbuf_get_rowstride(pmask
) - 3 * width
;
1516 for (int y
= 0; y
< height
;
1517 y
++, bmp
+= bmprowinc
, mask
+= maskrowinc
)
1519 for (int x
= 0; x
< width
; x
++, bmp
+= 4, mask
+= 3)
1521 if (mask
[0] == 0 /*black pixel*/)
1526 gdk_pixbuf_unref(pmask
);
1531 return M_BMPDATA
->m_pixbuf
;
1534 bool wxBitmap::HasPixbuf() const
1536 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1538 return M_BMPDATA
->m_pixbuf
!= NULL
;
1541 void wxBitmap::SetPixbuf( GdkPixbuf
*pixbuf
)
1544 m_refData
= new wxBitmapRefData();
1546 M_BMPDATA
->m_pixbuf
= pixbuf
;
1547 PurgeOtherRepresentations(Pixbuf
);
1550 void wxBitmap::PurgeOtherRepresentations(wxBitmap::Representation keep
)
1552 if (keep
== Pixmap
&& HasPixbuf())
1554 gdk_pixbuf_unref( M_BMPDATA
->m_pixbuf
);
1555 M_BMPDATA
->m_pixbuf
= NULL
;
1557 if (keep
== Pixbuf
&& HasPixmap())
1559 gdk_pixmap_unref( M_BMPDATA
->m_pixmap
);
1560 M_BMPDATA
->m_pixmap
= NULL
;
1564 #endif // __WXGTK20__
1566 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1572 GdkPixbuf
*pixbuf
= GetPixbuf();
1577 if (gdk_pixbuf_get_has_alpha( pixbuf
))
1578 wxPrintf( wxT("Has alpha\n") );
1580 wxPrintf( wxT("No alpha.\n") );
1583 data
.m_height
= gdk_pixbuf_get_height( pixbuf
);
1584 data
.m_width
= gdk_pixbuf_get_width( pixbuf
);
1585 data
.m_stride
= gdk_pixbuf_get_rowstride( pixbuf
);
1587 return gdk_pixbuf_get_pixels( pixbuf
);
1593 void wxBitmap::UngetRawData(wxPixelDataBase
& WXUNUSED(data
))
1598 bool wxBitmap::HasAlpha() const
1607 void wxBitmap::UseAlpha()
1614 //-----------------------------------------------------------------------------
1616 //-----------------------------------------------------------------------------
1618 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
,wxBitmapHandlerBase
)
1620 wxBitmapHandler::~wxBitmapHandler()
1624 bool wxBitmapHandler::Create(wxBitmap
* WXUNUSED(bitmap
),
1625 void * WXUNUSED(data
),
1626 long WXUNUSED(type
),
1627 int WXUNUSED(width
),
1628 int WXUNUSED(height
),
1629 int WXUNUSED(depth
))
1631 wxFAIL_MSG( _T("not implemented") );
1636 bool wxBitmapHandler::LoadFile(wxBitmap
* WXUNUSED(bitmap
),
1637 const wxString
& WXUNUSED(name
),
1638 long WXUNUSED(flags
),
1639 int WXUNUSED(desiredWidth
),
1640 int WXUNUSED(desiredHeight
))
1642 wxFAIL_MSG( _T("not implemented") );
1647 bool wxBitmapHandler::SaveFile(const wxBitmap
* WXUNUSED(bitmap
),
1648 const wxString
& WXUNUSED(name
),
1650 const wxPalette
* WXUNUSED(palette
))
1652 wxFAIL_MSG( _T("not implemented") );
1657 /* static */ void wxBitmap::InitStandardHandlers()
1659 // TODO: Insert handler based on GdkPixbufs handler later