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"
13 #include "wx/bitmap.h"
17 #include "wx/dcmemory.h"
18 #include "wx/palette.h"
23 #include "wx/filefn.h"
26 #include "wx/rawbmp.h"
27 // need this to get gdk_image_new_bitmap()
28 #define GDK_ENABLE_BROKEN
34 #include <gdk/gdkimage.h>
36 extern void gdk_wx_draw_bitmap (GdkDrawable
*drawable
,
46 //-----------------------------------------------------------------------------
48 //-----------------------------------------------------------------------------
50 extern GtkWidget
*wxGetRootWindow();
52 //-----------------------------------------------------------------------------
54 //-----------------------------------------------------------------------------
56 IMPLEMENT_DYNAMIC_CLASS(wxMask
,wxObject
)
60 m_bitmap
= (GdkBitmap
*) NULL
;
63 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
65 m_bitmap
= (GdkBitmap
*) NULL
;
66 Create( bitmap
, colour
);
70 wxMask::wxMask( const wxBitmap
& bitmap
, int paletteIndex
)
72 m_bitmap
= (GdkBitmap
*) NULL
;
73 Create( bitmap
, paletteIndex
);
75 #endif // wxUSE_PALETTE
77 wxMask::wxMask( const wxBitmap
& bitmap
)
79 m_bitmap
= (GdkBitmap
*) NULL
;
86 g_object_unref (m_bitmap
);
89 bool wxMask::Create( const wxBitmap
& bitmap
,
90 const wxColour
& colour
)
94 g_object_unref (m_bitmap
);
95 m_bitmap
= (GdkBitmap
*) NULL
;
98 wxImage image
= bitmap
.ConvertToImage();
99 if (!image
.Ok()) return false;
101 m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, image
.GetWidth(), image
.GetHeight(), 1 );
102 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
109 gdk_gc_set_foreground( gc
, &color
);
110 gdk_gc_set_fill( gc
, GDK_SOLID
);
111 gdk_draw_rectangle( m_bitmap
, gc
, TRUE
, 0, 0, image
.GetWidth(), image
.GetHeight() );
113 unsigned char *data
= image
.GetData();
116 unsigned char red
= colour
.Red();
117 unsigned char green
= colour
.Green();
118 unsigned char blue
= colour
.Blue();
120 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
122 int bpp
= visual
->depth
;
123 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800))
128 green
= green
& 0xf8;
134 green
= green
& 0xfc;
140 green
= green
& 0xf0;
148 gdk_gc_set_foreground( gc
, &color
);
150 for (int j
= 0; j
< image
.GetHeight(); j
++)
154 for (i
= 0; i
< image
.GetWidth(); i
++)
156 if ((data
[index
] == red
) &&
157 (data
[index
+1] == green
) &&
158 (data
[index
+2] == blue
))
167 gdk_draw_line( m_bitmap
, gc
, start_x
, j
, i
-1, j
);
174 gdk_draw_line( m_bitmap
, gc
, start_x
, j
, i
, j
);
183 bool wxMask::Create( const wxBitmap
& bitmap
, int paletteIndex
)
186 wxPalette
*pal
= bitmap
.GetPalette();
188 wxCHECK_MSG( pal
, false, wxT("Cannot create mask from bitmap without palette") );
190 pal
->GetRGB(paletteIndex
, &r
, &g
, &b
);
192 return Create(bitmap
, wxColour(r
, g
, b
));
194 #endif // wxUSE_PALETTE
196 bool wxMask::Create( const wxBitmap
& bitmap
)
200 g_object_unref (m_bitmap
);
201 m_bitmap
= (GdkBitmap
*) NULL
;
204 if (!bitmap
.Ok()) return false;
206 wxCHECK_MSG( bitmap
.GetBitmap(), false, wxT("Cannot create mask from colour bitmap") );
208 m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, bitmap
.GetWidth(), bitmap
.GetHeight(), 1 );
210 if (!m_bitmap
) return false;
212 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
214 gdk_wx_draw_bitmap( m_bitmap
, gc
, bitmap
.GetBitmap(), 0, 0, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight() );
221 GdkBitmap
*wxMask::GetBitmap() const
226 //-----------------------------------------------------------------------------
228 //-----------------------------------------------------------------------------
230 class wxBitmapRefData
: public wxObjectRefData
243 wxPalette
*m_palette
;
246 wxBitmapRefData::wxBitmapRefData()
248 m_pixmap
= (GdkPixmap
*) NULL
;
249 m_bitmap
= (GdkBitmap
*) NULL
;
250 m_pixbuf
= (GdkPixbuf
*) NULL
;
251 m_mask
= (wxMask
*) NULL
;
255 m_palette
= (wxPalette
*) NULL
;
258 wxBitmapRefData::~wxBitmapRefData()
261 g_object_unref (m_pixmap
);
263 g_object_unref (m_bitmap
);
265 g_object_unref (m_pixbuf
);
269 #endif // wxUSE_PALETTE
272 //-----------------------------------------------------------------------------
274 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
276 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
282 wxBitmap::wxBitmap( int width
, int height
, int depth
)
284 Create( width
, height
, depth
);
287 bool wxBitmap::Create( int width
, int height
, int depth
)
291 if ( width
<= 0 || height
<= 0 )
296 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
299 depth
= visual
->depth
;
301 wxCHECK_MSG( (depth
== visual
->depth
) || (depth
== 1) || (depth
== 32), false,
302 wxT("invalid bitmap depth") );
304 m_refData
= new wxBitmapRefData();
305 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
306 M_BMPDATA
->m_width
= width
;
307 M_BMPDATA
->m_height
= height
;
310 M_BMPDATA
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
311 M_BMPDATA
->m_bpp
= 1;
313 else if (depth
== 32)
315 M_BMPDATA
->m_pixbuf
= gdk_pixbuf_new( GDK_COLORSPACE_RGB
, true,
317 M_BMPDATA
->m_bpp
= 32;
321 M_BMPDATA
->m_pixmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, depth
);
322 M_BMPDATA
->m_bpp
= visual
->depth
;
328 bool wxBitmap::CreateFromXpm( const char **bits
)
332 wxCHECK_MSG( bits
!= NULL
, false, wxT("invalid bitmap data") );
334 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
336 m_refData
= new wxBitmapRefData();
338 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
340 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm_d( wxGetRootWindow()->window
, &mask
, NULL
, (gchar
**) bits
);
342 wxCHECK_MSG( M_BMPDATA
->m_pixmap
, false, wxT("couldn't create pixmap") );
346 M_BMPDATA
->m_mask
= new wxMask();
347 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
350 gdk_drawable_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
352 M_BMPDATA
->m_bpp
= visual
->depth
; // Can we get a different depth from create_from_xpm_d() ?
357 wxBitmap
wxBitmap::Rescale( int clipx
, int clipy
, int clipwidth
, int clipheight
, int newx
, int newy
)
359 wxCHECK_MSG( Ok(), wxNullBitmap
, wxT("invalid bitmap") );
361 if (newy
==M_BMPDATA
->m_width
&& newy
==M_BMPDATA
->m_height
)
364 int width
= wxMax(newx
, 1);
365 int height
= wxMax(newy
, 1);
366 width
= wxMin(width
, clipwidth
);
367 height
= wxMin(height
, clipheight
);
374 bmp
.SetHeight(height
);
375 bmp
.SetDepth(GetDepth());
376 bmp
.SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
377 gdk_pixbuf_get_has_alpha(GetPixbuf()),
379 gdk_pixbuf_scale(GetPixbuf(), bmp
.GetPixbuf(),
382 (double)newx
/GetWidth(), (double)newy
/GetHeight(),
383 GDK_INTERP_BILINEAR
);
387 GdkImage
*img
= (GdkImage
*) NULL
;
389 img
= gdk_image_get( GetPixmap(), 0, 0, GetWidth(), GetHeight() );
390 else if (GetBitmap())
391 img
= gdk_image_get( GetBitmap(), 0, 0, GetWidth(), GetHeight() );
393 wxFAIL_MSG( wxT("Ill-formed bitmap") );
395 wxCHECK_MSG( img
, wxNullBitmap
, wxT("couldn't create image") );
401 GdkPixmap
*dstpix
= NULL
;
404 GdkVisual
*visual
= gdk_drawable_get_visual( GetPixmap() );
406 visual
= wxTheApp
->GetGdkVisual();
409 bmp
= wxBitmap(width
,height
,bpp
);
410 dstpix
= bmp
.GetPixmap();
411 gc
= gdk_gc_new( dstpix
);
415 long dstbyteperline
= 0;
420 dstbyteperline
= width
/8*M_BMPDATA
->m_bpp
;
421 if (width
*M_BMPDATA
->m_bpp
% 8 != 0)
423 dst
= (char*) malloc(dstbyteperline
*height
);
426 // be careful to use the right scaling factor
427 float scx
= (float)M_BMPDATA
->m_width
/(float)newx
;
428 float scy
= (float)M_BMPDATA
->m_height
/(float)newy
;
429 // prepare accel-tables
430 int *tablex
= (int *)calloc(width
,sizeof(int));
431 int *tabley
= (int *)calloc(height
,sizeof(int));
433 // accel table filled with clipped values
434 for (int x
= 0; x
< width
; x
++)
435 tablex
[x
] = (int) (scx
* (x
+clipx
));
436 for (int y
= 0; y
< height
; y
++)
437 tabley
[y
] = (int) (scy
* (y
+clipy
));
439 // Main rescaling routine starts here
440 for (int h
= 0; h
< height
; h
++)
444 guint32 old_pixval
= 0;
446 for (int w
= 0; w
< width
; w
++)
454 pixval
= gdk_image_get_pixel( img
, x
, tabley
[h
] );
464 char shift
= bit
<< (w
% 8);
470 dst
[h
*dstbyteperline
+w
/8] = outbyte
;
478 gdk_gc_set_foreground( gc
, &col
);
479 gdk_draw_point( dstpix
, gc
, w
, h
);
483 // do not forget the last byte
484 if ( dst
&& (width
% 8 != 0) )
485 dst
[h
*dstbyteperline
+width
/8] = outbyte
;
488 g_object_unref (img
);
489 if (gc
) g_object_unref (gc
);
493 bmp
= wxBitmap( (const char *)dst
, width
, height
, 1 );
499 dstbyteperline
= width
/8;
502 dst
= (char*) malloc(dstbyteperline
*height
);
503 img
= gdk_image_get( GetMask()->GetBitmap(), 0, 0, GetWidth(), GetHeight() );
505 for (int h
= 0; h
< height
; h
++)
509 guint32 old_pixval
= 0;
511 for (int w
= 0; w
< width
; w
++)
519 pixval
= gdk_image_get_pixel( img
, x
, tabley
[h
] );
527 char shift
= bit
<< (w
% 8);
533 dst
[h
*dstbyteperline
+w
/8] = outbyte
;
538 // do not forget the last byte
540 dst
[h
*dstbyteperline
+width
/8] = outbyte
;
542 wxMask
* mask
= new wxMask
;
543 mask
->m_bitmap
= gdk_bitmap_create_from_data( wxGetRootWindow()->window
, (gchar
*) dst
, width
, height
);
547 g_object_unref (img
);
557 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
561 wxCHECK_MSG( image
.Ok(), false, wxT("invalid image") );
562 wxCHECK_MSG( depth
== -1 || depth
== 1, false, wxT("invalid bitmap depth") );
564 if (image
.GetWidth() <= 0 || image
.GetHeight() <= 0)
567 m_refData
= new wxBitmapRefData();
571 return CreateFromImageAsBitmap(image
);
575 if (image
.HasAlpha())
576 return CreateFromImageAsPixbuf(image
);
578 return CreateFromImageAsPixmap(image
);
582 // conversion to mono bitmap:
583 bool wxBitmap::CreateFromImageAsBitmap(const wxImage
& img
)
585 // convert alpha channel to mask, if it is present:
587 image
.ConvertAlphaToMask();
589 int width
= image
.GetWidth();
590 int height
= image
.GetHeight();
595 SetBitmap( gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 ) );
599 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
601 // Create picture image
603 unsigned char *data_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
605 GdkImage
*data_image
=
606 gdk_image_new_bitmap( visual
, data_data
, width
, height
);
610 GdkImage
*mask_image
= (GdkImage
*) NULL
;
614 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
616 mask_image
= gdk_image_new_bitmap( visual
, mask_data
, width
, height
);
618 wxMask
*mask
= new wxMask();
619 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
624 int r_mask
= image
.GetMaskRed();
625 int g_mask
= image
.GetMaskGreen();
626 int b_mask
= image
.GetMaskBlue();
628 unsigned char* data
= image
.GetData();
631 for (int y
= 0; y
< height
; y
++)
633 for (int x
= 0; x
< width
; x
++)
644 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
645 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
647 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
650 if ((r
== 255) && (b
== 255) && (g
== 255))
651 gdk_image_put_pixel( data_image
, x
, y
, 1 );
653 gdk_image_put_pixel( data_image
, x
, y
, 0 );
660 GdkGC
*data_gc
= gdk_gc_new( GetBitmap() );
662 gdk_draw_image( GetBitmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
664 g_object_unref (data_image
);
665 g_object_unref (data_gc
);
671 GdkGC
*mask_gc
= gdk_gc_new( GetMask()->GetBitmap() );
673 gdk_draw_image( GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
675 g_object_unref (mask_image
);
676 g_object_unref (mask_gc
);
682 // conversion to colour bitmap:
683 bool wxBitmap::CreateFromImageAsPixmap(const wxImage
& img
)
685 // convert alpha channel to mask, if it is present:
687 image
.ConvertAlphaToMask();
689 int width
= image
.GetWidth();
690 int height
= image
.GetHeight();
695 SetPixmap( gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, -1 ) );
697 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
699 int bpp
= visual
->depth
;
703 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800))
708 // We handle 8-bit bitmaps ourselves using the colour cube, 12-bit
709 // visuals are not supported by GDK so we do these ourselves, too.
710 // 15-bit and 16-bit should actually work and 24-bit certainly does.
712 if (!image
.HasMask() && (bpp
> 16))
714 if (!image
.HasMask() && (bpp
> 12))
717 static bool s_hasInitialized
= false;
719 if (!s_hasInitialized
)
722 s_hasInitialized
= true;
725 GdkGC
*gc
= gdk_gc_new( GetPixmap() );
727 gdk_draw_rgb_image( GetPixmap(),
739 // Create picture image
741 GdkImage
*data_image
=
742 gdk_image_new( GDK_IMAGE_FASTEST
, visual
, width
, height
);
746 GdkImage
*mask_image
= (GdkImage
*) NULL
;
750 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
752 mask_image
= gdk_image_new_bitmap( visual
, mask_data
, width
, height
);
754 wxMask
*mask
= new wxMask();
755 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
762 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
763 byte_order b_o
= RGB
;
767 if ((visual
->red_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->blue_mask
)) b_o
= RGB
;
768 else if ((visual
->red_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->green_mask
)) b_o
= RBG
;
769 else if ((visual
->blue_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->green_mask
)) b_o
= BRG
;
770 else if ((visual
->blue_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->red_mask
)) b_o
= BGR
;
771 else if ((visual
->green_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->blue_mask
)) b_o
= GRB
;
772 else if ((visual
->green_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->red_mask
)) b_o
= GBR
;
775 int r_mask
= image
.GetMaskRed();
776 int g_mask
= image
.GetMaskGreen();
777 int b_mask
= image
.GetMaskBlue();
779 unsigned char* data
= image
.GetData();
782 for (int y
= 0; y
< height
; y
++)
784 for (int x
= 0; x
< width
; x
++)
795 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
796 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
798 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
806 if (wxTheApp
->m_colorCube
)
808 pixel
= wxTheApp
->m_colorCube
[ ((r
& 0xf8) << 7) + ((g
& 0xf8) << 2) + ((b
& 0xf8) >> 3) ];
812 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
813 GdkColor
*colors
= cmap
->colors
;
814 int max
= 3 * (65536);
816 for (int i
= 0; i
< cmap
->size
; i
++)
818 int rdiff
= (r
<< 8) - colors
[i
].red
;
819 int gdiff
= (g
<< 8) - colors
[i
].green
;
820 int bdiff
= (b
<< 8) - colors
[i
].blue
;
821 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
822 if (sum
< max
) { pixel
= i
; max
= sum
; }
826 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
835 case RGB
: pixel
= ((r
& 0xf0) << 4) | (g
& 0xf0) | ((b
& 0xf0) >> 4); break;
836 case RBG
: pixel
= ((r
& 0xf0) << 4) | (b
& 0xf0) | ((g
& 0xf0) >> 4); break;
837 case GRB
: pixel
= ((g
& 0xf0) << 4) | (r
& 0xf0) | ((b
& 0xf0) >> 4); break;
838 case GBR
: pixel
= ((g
& 0xf0) << 4) | (b
& 0xf0) | ((r
& 0xf0) >> 4); break;
839 case BRG
: pixel
= ((b
& 0xf0) << 4) | (r
& 0xf0) | ((g
& 0xf0) >> 4); break;
840 case BGR
: pixel
= ((b
& 0xf0) << 4) | (g
& 0xf0) | ((r
& 0xf0) >> 4); break;
842 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
850 case RGB
: pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
851 case RBG
: pixel
= ((r
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
852 case GRB
: pixel
= ((g
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
853 case GBR
: pixel
= ((g
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
854 case BRG
: pixel
= ((b
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
855 case BGR
: pixel
= ((b
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
857 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
862 // I actually don't know if for 16-bit displays, it is alway the green
863 // component or the second component which has 6 bits.
867 case RGB
: pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
868 case RBG
: pixel
= ((r
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
869 case GRB
: pixel
= ((g
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
870 case GBR
: pixel
= ((g
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
871 case BRG
: pixel
= ((b
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
872 case BGR
: pixel
= ((b
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
874 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
883 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
884 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
885 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
886 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
887 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
888 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
890 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
900 GdkGC
*data_gc
= gdk_gc_new( GetPixmap() );
902 gdk_draw_image( GetPixmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
904 g_object_unref (data_image
);
905 g_object_unref (data_gc
);
911 GdkGC
*mask_gc
= gdk_gc_new( GetMask()->GetBitmap() );
913 gdk_draw_image( GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
915 g_object_unref (mask_image
);
916 g_object_unref (mask_gc
);
922 bool wxBitmap::CreateFromImageAsPixbuf(const wxImage
& image
)
924 int width
= image
.GetWidth();
925 int height
= image
.GetHeight();
927 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
929 8 /* bits per sample */,
934 wxASSERT( image
.HasAlpha() ); // for now
935 wxASSERT( gdk_pixbuf_get_n_channels(pixbuf
) == 4 );
936 wxASSERT( gdk_pixbuf_get_width(pixbuf
) == width
);
937 wxASSERT( gdk_pixbuf_get_height(pixbuf
) == height
);
939 M_BMPDATA
->m_pixbuf
= pixbuf
;
942 SetDepth(wxTheApp
->GetGdkVisual()->depth
);
945 unsigned char *in
= image
.GetData();
946 unsigned char *out
= gdk_pixbuf_get_pixels(pixbuf
);
947 unsigned char *alpha
= image
.GetAlpha();
949 int rowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
951 for (int y
= 0; y
< height
; y
++, out
+= rowinc
)
953 for (int x
= 0; x
< width
; x
++, alpha
++, out
+= 4, in
+= 3)
965 wxImage
wxBitmap::ConvertToImage() const
969 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
971 image
.Create(GetWidth(), GetHeight());
972 unsigned char *data
= image
.GetData();
976 wxFAIL_MSG( wxT("couldn't create image") );
982 GdkPixbuf
*pixbuf
= GetPixbuf();
983 wxASSERT( gdk_pixbuf_get_has_alpha(pixbuf
) );
990 unsigned char *alpha
= image
.GetAlpha();
991 unsigned char *in
= gdk_pixbuf_get_pixels(pixbuf
);
992 unsigned char *out
= data
;
993 int rowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * w
;
995 for (int y
= 0; y
< h
; y
++, in
+= rowinc
)
997 for (int x
= 0; x
< w
; x
++, in
+= 4, out
+= 3, alpha
++)
1008 // the colour used as transparent one in wxImage and the one it is
1009 // replaced with when it really occurs in the bitmap
1010 static const int MASK_RED
= 1;
1011 static const int MASK_GREEN
= 2;
1012 static const int MASK_BLUE
= 3;
1013 static const int MASK_BLUE_REPLACEMENT
= 2;
1015 GdkImage
*gdk_image
= (GdkImage
*) NULL
;
1019 gdk_image
= gdk_image_get( GetPixmap(),
1021 GetWidth(), GetHeight() );
1023 else if (GetBitmap())
1025 gdk_image
= gdk_image_get( GetBitmap(),
1027 GetWidth(), GetHeight() );
1031 wxFAIL_MSG( wxT("Ill-formed bitmap") );
1034 wxCHECK_MSG( gdk_image
, wxNullImage
, wxT("couldn't create image") );
1036 GdkImage
*gdk_image_mask
= (GdkImage
*) NULL
;
1039 gdk_image_mask
= gdk_image_get( GetMask()->GetBitmap(),
1041 GetWidth(), GetHeight() );
1043 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
1047 int red_shift_right
= 0;
1048 int green_shift_right
= 0;
1049 int blue_shift_right
= 0;
1050 int red_shift_left
= 0;
1051 int green_shift_left
= 0;
1052 int blue_shift_left
= 0;
1053 bool use_shift
= false;
1057 GdkVisual
*visual
= gdk_drawable_get_visual( GetPixmap() );
1059 visual
= wxTheApp
->GetGdkVisual();
1061 bpp
= visual
->depth
;
1063 bpp
= visual
->red_prec
+ visual
->green_prec
+ visual
->blue_prec
;
1064 red_shift_right
= visual
->red_shift
;
1065 red_shift_left
= 8-visual
->red_prec
;
1066 green_shift_right
= visual
->green_shift
;
1067 green_shift_left
= 8-visual
->green_prec
;
1068 blue_shift_right
= visual
->blue_shift
;
1069 blue_shift_left
= 8-visual
->blue_prec
;
1071 use_shift
= (visual
->type
== GDK_VISUAL_TRUE_COLOR
) || (visual
->type
== GDK_VISUAL_DIRECT_COLOR
);
1079 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
1082 for (int j
= 0; j
< GetHeight(); j
++)
1084 for (int i
= 0; i
< GetWidth(); i
++)
1086 wxUint32 pixel
= gdk_image_get_pixel( gdk_image
, i
, j
);
1104 data
[pos
] = (pixel
>> red_shift_right
) << red_shift_left
;
1105 data
[pos
+1] = (pixel
>> green_shift_right
) << green_shift_left
;
1106 data
[pos
+2] = (pixel
>> blue_shift_right
) << blue_shift_left
;
1108 else if (cmap
->colors
)
1110 data
[pos
] = cmap
->colors
[pixel
].red
>> 8;
1111 data
[pos
+1] = cmap
->colors
[pixel
].green
>> 8;
1112 data
[pos
+2] = cmap
->colors
[pixel
].blue
>> 8;
1116 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
1121 int mask_pixel
= gdk_image_get_pixel( gdk_image_mask
, i
, j
);
1122 if (mask_pixel
== 0)
1124 data
[pos
] = MASK_RED
;
1125 data
[pos
+1] = MASK_GREEN
;
1126 data
[pos
+2] = MASK_BLUE
;
1128 else if ( data
[pos
] == MASK_RED
&&
1129 data
[pos
+1] == MASK_GREEN
&&
1130 data
[pos
+2] == MASK_BLUE
)
1132 data
[pos
+2] = MASK_BLUE_REPLACEMENT
;
1140 g_object_unref (gdk_image
);
1141 if (gdk_image_mask
) g_object_unref (gdk_image_mask
);
1147 wxBitmap::wxBitmap( const wxString
&filename
, wxBitmapType type
)
1149 LoadFile( filename
, type
);
1152 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int WXUNUSED(depth
))
1154 if ( width
> 0 && height
> 0 )
1156 m_refData
= new wxBitmapRefData();
1158 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
1159 M_BMPDATA
->m_bitmap
= gdk_bitmap_create_from_data
1161 wxGetRootWindow()->window
,
1166 M_BMPDATA
->m_width
= width
;
1167 M_BMPDATA
->m_height
= height
;
1168 M_BMPDATA
->m_bpp
= 1;
1170 wxASSERT_MSG( M_BMPDATA
->m_bitmap
, wxT("couldn't create bitmap") );
1174 wxBitmap::~wxBitmap()
1178 bool wxBitmap::operator == ( const wxBitmap
& bmp
) const
1180 return m_refData
== bmp
.m_refData
;
1183 bool wxBitmap::operator != ( const wxBitmap
& bmp
) const
1185 return m_refData
!= bmp
.m_refData
;
1188 bool wxBitmap::Ok() const
1190 return (m_refData
!= NULL
) &&
1192 M_BMPDATA
->m_pixbuf
||
1193 M_BMPDATA
->m_bitmap
|| M_BMPDATA
->m_pixmap
1197 int wxBitmap::GetHeight() const
1199 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1201 return M_BMPDATA
->m_height
;
1204 int wxBitmap::GetWidth() const
1206 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1208 return M_BMPDATA
->m_width
;
1211 int wxBitmap::GetDepth() const
1213 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1215 return M_BMPDATA
->m_bpp
;
1218 wxMask
*wxBitmap::GetMask() const
1220 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
1222 return M_BMPDATA
->m_mask
;
1225 void wxBitmap::SetMask( wxMask
*mask
)
1227 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
1229 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
1231 M_BMPDATA
->m_mask
= mask
;
1234 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
1240 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
1242 wxCHECK_MSG( Ok() &&
1243 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1244 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) && (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
1245 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
1247 wxBitmap
ret( rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
1248 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
1252 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
1253 gdk_pixbuf_get_has_alpha(GetPixbuf()),
1254 8, rect
.width
, rect
.height
);
1255 ret
.SetPixbuf(pixbuf
);
1256 gdk_pixbuf_copy_area(GetPixbuf(),
1257 rect
.x
, rect
.y
, rect
.width
, rect
.height
,
1262 if (ret
.GetPixmap())
1264 GdkGC
*gc
= gdk_gc_new( ret
.GetPixmap() );
1265 gdk_draw_drawable( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1266 g_object_unref (gc
);
1270 GdkGC
*gc
= gdk_gc_new( ret
.GetBitmap() );
1272 col
.pixel
= 0xFFFFFF;
1273 gdk_gc_set_foreground( gc
, &col
);
1275 gdk_gc_set_background( gc
, &col
);
1276 gdk_wx_draw_bitmap( ret
.GetBitmap(), gc
, GetBitmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1277 g_object_unref (gc
);
1283 wxMask
*mask
= new wxMask
;
1284 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, rect
.width
, rect
.height
, 1 );
1286 GdkGC
*gc
= gdk_gc_new( mask
->m_bitmap
);
1288 col
.pixel
= 0xFFFFFF;
1289 gdk_gc_set_foreground( gc
, &col
);
1291 gdk_gc_set_background( gc
, &col
);
1292 gdk_wx_draw_bitmap( mask
->m_bitmap
, gc
, M_BMPDATA
->m_mask
->m_bitmap
, rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1293 g_object_unref (gc
);
1295 ret
.SetMask( mask
);
1301 bool wxBitmap::SaveFile( const wxString
&name
, wxBitmapType type
, const wxPalette
*WXUNUSED(palette
) ) const
1303 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1305 // Try to save the bitmap via wxImage handlers:
1307 wxImage image
= ConvertToImage();
1308 if (image
.Ok()) return image
.SaveFile( name
, type
);
1314 bool wxBitmap::LoadFile( const wxString
&name
, wxBitmapType type
)
1318 if (!wxFileExists(name
))
1321 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
1323 if (type
== wxBITMAP_TYPE_XPM
)
1325 m_refData
= new wxBitmapRefData();
1327 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
1329 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm
1331 wxGetRootWindow()->window
,
1339 M_BMPDATA
->m_mask
= new wxMask();
1340 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
1343 gdk_drawable_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
1345 M_BMPDATA
->m_bpp
= visual
->depth
;
1347 else // try if wxImage can load it
1350 if ( !image
.LoadFile( name
, type
) || !image
.Ok() )
1353 *this = wxBitmap(image
);
1360 wxPalette
*wxBitmap::GetPalette() const
1363 return (wxPalette
*) NULL
;
1365 return M_BMPDATA
->m_palette
;
1368 void wxBitmap::SetPalette(const wxPalette
& WXUNUSED(palette
))
1372 #endif // wxUSE_PALETTE
1374 void wxBitmap::SetHeight( int height
)
1377 m_refData
= new wxBitmapRefData();
1379 M_BMPDATA
->m_height
= height
;
1382 void wxBitmap::SetWidth( int width
)
1385 m_refData
= new wxBitmapRefData();
1387 M_BMPDATA
->m_width
= width
;
1390 void wxBitmap::SetDepth( int depth
)
1393 m_refData
= new wxBitmapRefData();
1395 M_BMPDATA
->m_bpp
= depth
;
1398 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
1401 m_refData
= new wxBitmapRefData();
1403 M_BMPDATA
->m_pixmap
= pixmap
;
1404 PurgeOtherRepresentations(Pixmap
);
1407 void wxBitmap::SetBitmap( GdkPixmap
*bitmap
)
1410 m_refData
= new wxBitmapRefData();
1412 M_BMPDATA
->m_bitmap
= bitmap
;
1413 PurgeOtherRepresentations(Pixmap
);
1416 GdkPixmap
*wxBitmap::GetPixmap() const
1418 wxCHECK_MSG( Ok(), (GdkPixmap
*) NULL
, wxT("invalid bitmap") );
1420 // create the pixmap on the fly if we use Pixbuf representation:
1421 if (HasPixbuf() && !HasPixmap())
1423 delete M_BMPDATA
->m_mask
;
1424 M_BMPDATA
->m_mask
= new wxMask();
1425 gdk_pixbuf_render_pixmap_and_mask(M_BMPDATA
->m_pixbuf
,
1426 &M_BMPDATA
->m_pixmap
,
1427 &M_BMPDATA
->m_mask
->m_bitmap
,
1431 return M_BMPDATA
->m_pixmap
;
1434 bool wxBitmap::HasPixmap() const
1436 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1438 return M_BMPDATA
->m_pixmap
!= NULL
;
1441 GdkBitmap
*wxBitmap::GetBitmap() const
1443 wxCHECK_MSG( Ok(), (GdkBitmap
*) NULL
, wxT("invalid bitmap") );
1445 return M_BMPDATA
->m_bitmap
;
1448 GdkPixbuf
*wxBitmap::GetPixbuf() const
1450 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
1452 if (HasPixmap() && !HasPixbuf())
1454 int width
= GetWidth();
1455 int height
= GetHeight();
1457 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
1460 M_BMPDATA
->m_pixbuf
=
1461 gdk_pixbuf_get_from_drawable(pixbuf
, M_BMPDATA
->m_pixmap
, NULL
,
1462 0, 0, 0, 0, width
, height
);
1464 // apply the mask to created pixbuf:
1465 if (M_BMPDATA
->m_pixbuf
&& M_BMPDATA
->m_mask
)
1468 gdk_pixbuf_get_from_drawable(NULL
,
1469 M_BMPDATA
->m_mask
->GetBitmap(),
1471 0, 0, 0, 0, width
, height
);
1474 guchar
*bmp
= gdk_pixbuf_get_pixels(pixbuf
);
1475 guchar
*mask
= gdk_pixbuf_get_pixels(pmask
);
1476 int bmprowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
1477 int maskrowinc
= gdk_pixbuf_get_rowstride(pmask
) - 3 * width
;
1479 for (int y
= 0; y
< height
;
1480 y
++, bmp
+= bmprowinc
, mask
+= maskrowinc
)
1482 for (int x
= 0; x
< width
; x
++, bmp
+= 4, mask
+= 3)
1484 if (mask
[0] == 0 /*black pixel*/)
1489 g_object_unref (pmask
);
1494 return M_BMPDATA
->m_pixbuf
;
1497 bool wxBitmap::HasPixbuf() const
1499 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1501 return M_BMPDATA
->m_pixbuf
!= NULL
;
1504 void wxBitmap::SetPixbuf( GdkPixbuf
*pixbuf
)
1507 m_refData
= new wxBitmapRefData();
1509 M_BMPDATA
->m_pixbuf
= pixbuf
;
1510 PurgeOtherRepresentations(Pixbuf
);
1513 void wxBitmap::PurgeOtherRepresentations(wxBitmap::Representation keep
)
1515 if (keep
== Pixmap
&& HasPixbuf())
1517 g_object_unref (M_BMPDATA
->m_pixbuf
);
1518 M_BMPDATA
->m_pixbuf
= NULL
;
1520 if (keep
== Pixbuf
&& HasPixmap())
1522 g_object_unref (M_BMPDATA
->m_pixmap
);
1523 M_BMPDATA
->m_pixmap
= NULL
;
1527 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1532 GdkPixbuf
*pixbuf
= GetPixbuf();
1537 if (gdk_pixbuf_get_has_alpha( pixbuf
))
1538 wxPrintf( wxT("Has alpha\n") );
1540 wxPrintf( wxT("No alpha.\n") );
1543 data
.m_height
= gdk_pixbuf_get_height( pixbuf
);
1544 data
.m_width
= gdk_pixbuf_get_width( pixbuf
);
1545 data
.m_stride
= gdk_pixbuf_get_rowstride( pixbuf
);
1547 return gdk_pixbuf_get_pixels( pixbuf
);
1550 void wxBitmap::UngetRawData(wxPixelDataBase
& WXUNUSED(data
))
1555 bool wxBitmap::HasAlpha() const
1560 void wxBitmap::UseAlpha()
1565 //-----------------------------------------------------------------------------
1567 //-----------------------------------------------------------------------------
1569 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
,wxBitmapHandlerBase
)
1571 wxBitmapHandler::~wxBitmapHandler()
1575 bool wxBitmapHandler::Create(wxBitmap
* WXUNUSED(bitmap
),
1576 void * WXUNUSED(data
),
1577 long WXUNUSED(type
),
1578 int WXUNUSED(width
),
1579 int WXUNUSED(height
),
1580 int WXUNUSED(depth
))
1582 wxFAIL_MSG( _T("not implemented") );
1587 bool wxBitmapHandler::LoadFile(wxBitmap
* WXUNUSED(bitmap
),
1588 const wxString
& WXUNUSED(name
),
1589 long WXUNUSED(flags
),
1590 int WXUNUSED(desiredWidth
),
1591 int WXUNUSED(desiredHeight
))
1593 wxFAIL_MSG( _T("not implemented") );
1598 bool wxBitmapHandler::SaveFile(const wxBitmap
* WXUNUSED(bitmap
),
1599 const wxString
& WXUNUSED(name
),
1601 const wxPalette
* WXUNUSED(palette
))
1603 wxFAIL_MSG( _T("not implemented") );
1608 /* static */ void wxBitmap::InitStandardHandlers()
1610 // TODO: Insert handler based on GdkPixbufs handler later