1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "bitmap.h"
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
19 #include "wx/bitmap.h"
20 #include "wx/palette.h"
22 #include "wx/filefn.h"
24 #include "wx/dcmemory.h"
28 // need this to get gdk_image_new_bitmap()
29 #define GDK_ENABLE_BROKEN
37 #include <gdk/gdkimage.h>
39 #include <gdk/gdkrgb.h>
40 #endif // GTK+ 2.0/1.2
44 extern void gdk_wx_draw_bitmap (GdkDrawable
*drawable
,
54 //-----------------------------------------------------------------------------
56 //-----------------------------------------------------------------------------
58 extern GtkWidget
*wxGetRootWindow();
60 //-----------------------------------------------------------------------------
62 //-----------------------------------------------------------------------------
64 IMPLEMENT_DYNAMIC_CLASS(wxMask
,wxObject
)
68 m_bitmap
= (GdkBitmap
*) NULL
;
71 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
73 m_bitmap
= (GdkBitmap
*) NULL
;
74 Create( bitmap
, colour
);
77 wxMask::wxMask( const wxBitmap
& bitmap
, int paletteIndex
)
79 m_bitmap
= (GdkBitmap
*) NULL
;
80 Create( bitmap
, paletteIndex
);
83 wxMask::wxMask( const wxBitmap
& bitmap
)
85 m_bitmap
= (GdkBitmap
*) NULL
;
92 gdk_bitmap_unref( m_bitmap
);
95 bool wxMask::Create( const wxBitmap
& bitmap
,
96 const wxColour
& colour
)
100 gdk_bitmap_unref( m_bitmap
);
101 m_bitmap
= (GdkBitmap
*) NULL
;
104 wxImage image
= bitmap
.ConvertToImage();
105 if (!image
.Ok()) return FALSE
;
107 m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, image
.GetWidth(), image
.GetHeight(), 1 );
108 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
115 gdk_gc_set_foreground( gc
, &color
);
116 gdk_gc_set_fill( gc
, GDK_SOLID
);
117 gdk_draw_rectangle( m_bitmap
, gc
, TRUE
, 0, 0, image
.GetWidth(), image
.GetHeight() );
119 unsigned char *data
= image
.GetData();
122 unsigned char red
= colour
.Red();
123 unsigned char green
= colour
.Green();
124 unsigned char blue
= colour
.Blue();
126 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
128 int bpp
= visual
->depth
;
129 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800))
134 green
= green
& 0xf8;
140 green
= green
& 0xfc;
146 green
= green
& 0xf0;
154 gdk_gc_set_foreground( gc
, &color
);
156 for (int j
= 0; j
< image
.GetHeight(); j
++)
160 for (i
= 0; i
< image
.GetWidth(); i
++)
162 if ((data
[index
] == red
) &&
163 (data
[index
+1] == green
) &&
164 (data
[index
+2] == blue
))
173 gdk_draw_line( m_bitmap
, gc
, start_x
, j
, i
-1, j
);
180 gdk_draw_line( m_bitmap
, gc
, start_x
, j
, i
, j
);
188 bool wxMask::Create( const wxBitmap
& bitmap
, int paletteIndex
)
191 wxPalette
*pal
= bitmap
.GetPalette();
193 wxCHECK_MSG( pal
, FALSE
, wxT("Cannot create mask from bitmap without palette") );
195 pal
->GetRGB(paletteIndex
, &r
, &g
, &b
);
197 return Create(bitmap
, wxColour(r
, g
, b
));
200 bool wxMask::Create( const wxBitmap
& bitmap
)
204 gdk_bitmap_unref( m_bitmap
);
205 m_bitmap
= (GdkBitmap
*) NULL
;
208 if (!bitmap
.Ok()) return FALSE
;
210 wxCHECK_MSG( bitmap
.GetBitmap(), FALSE
, wxT("Cannot create mask from colour bitmap") );
212 m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, bitmap
.GetWidth(), bitmap
.GetHeight(), 1 );
214 if (!m_bitmap
) return FALSE
;
216 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
218 gdk_wx_draw_bitmap( m_bitmap
, gc
, bitmap
.GetBitmap(), 0, 0, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight() );
225 GdkBitmap
*wxMask::GetBitmap() const
230 //-----------------------------------------------------------------------------
232 //-----------------------------------------------------------------------------
234 class wxBitmapRefData
: public wxObjectRefData
249 wxPalette
*m_palette
;
252 wxBitmapRefData::wxBitmapRefData()
254 m_pixmap
= (GdkPixmap
*) NULL
;
255 m_bitmap
= (GdkBitmap
*) NULL
;
257 m_pixbuf
= (GdkPixbuf
*) NULL
;
259 m_mask
= (wxMask
*) NULL
;
263 m_palette
= (wxPalette
*) NULL
;
266 wxBitmapRefData::~wxBitmapRefData()
269 gdk_pixmap_unref( m_pixmap
);
271 gdk_bitmap_unref( m_bitmap
);
274 gdk_pixbuf_unref( m_pixbuf
);
280 //-----------------------------------------------------------------------------
282 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
284 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
290 wxBitmap::wxBitmap( int width
, int height
, int depth
)
292 Create( width
, height
, depth
);
295 bool wxBitmap::Create( int width
, int height
, int depth
)
299 if ( width
<= 0 || height
<= 0 )
304 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
307 depth
= visual
->depth
;
309 wxCHECK_MSG( (depth
== visual
->depth
) || (depth
== 1) || (depth
== 32), FALSE
,
310 wxT("invalid bitmap depth") )
312 m_refData
= new wxBitmapRefData();
313 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
314 M_BMPDATA
->m_width
= width
;
315 M_BMPDATA
->m_height
= height
;
318 M_BMPDATA
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
319 M_BMPDATA
->m_bpp
= 1;
322 else if (depth
== 32)
324 M_BMPDATA
->m_pixbuf
= gdk_pixbuf_new( GDK_COLORSPACE_RGB
, true,
326 M_BMPDATA
->m_bpp
= 32;
331 M_BMPDATA
->m_pixmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, depth
);
332 M_BMPDATA
->m_bpp
= visual
->depth
;
338 bool wxBitmap::CreateFromXpm( const char **bits
)
342 wxCHECK_MSG( bits
!= NULL
, FALSE
, wxT("invalid bitmap data") )
344 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
346 m_refData
= new wxBitmapRefData();
348 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
350 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm_d( wxGetRootWindow()->window
, &mask
, NULL
, (gchar
**) bits
);
352 wxCHECK_MSG( M_BMPDATA
->m_pixmap
, FALSE
, wxT("couldn't create pixmap") );
356 M_BMPDATA
->m_mask
= new wxMask();
357 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
360 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
362 M_BMPDATA
->m_bpp
= visual
->depth
; // Can we get a different depth from create_from_xpm_d() ?
367 wxBitmap
wxBitmap::Rescale( int clipx
, int clipy
, int clipwidth
, int clipheight
, int newx
, int newy
)
369 wxCHECK_MSG( Ok(), wxNullBitmap
, wxT("invalid bitmap") );
371 if (newy
==M_BMPDATA
->m_width
&& newy
==M_BMPDATA
->m_height
)
374 int width
= wxMax(newx
, 1);
375 int height
= wxMax(newy
, 1);
376 width
= wxMin(width
, clipwidth
);
377 height
= wxMin(height
, clipheight
);
385 bmp
.SetHeight(height
);
386 bmp
.SetDepth(GetDepth());
387 bmp
.SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
388 gdk_pixbuf_get_has_alpha(GetPixbuf()),
390 gdk_pixbuf_scale(GetPixbuf(), bmp
.GetPixbuf(),
393 (double)newx
/GetWidth(), (double)newy
/GetHeight(),
394 GDK_INTERP_BILINEAR
);
397 #endif // __WXGTK20__
399 GdkImage
*img
= (GdkImage
*) NULL
;
401 img
= gdk_image_get( GetPixmap(), 0, 0, GetWidth(), GetHeight() );
402 else if (GetBitmap())
403 img
= gdk_image_get( GetBitmap(), 0, 0, GetWidth(), GetHeight() );
405 wxFAIL_MSG( wxT("Ill-formed bitmap") );
407 wxCHECK_MSG( img
, wxNullBitmap
, wxT("couldn't create image") );
413 GdkPixmap
*dstpix
= NULL
;
416 GdkVisual
*visual
= gdk_window_get_visual( GetPixmap() );
418 visual
= wxTheApp
->GetGdkVisual();
421 bmp
= wxBitmap(width
,height
,bpp
);
422 dstpix
= bmp
.GetPixmap();
423 gc
= gdk_gc_new( dstpix
);
427 long dstbyteperline
= 0;
432 dstbyteperline
= width
/8*M_BMPDATA
->m_bpp
;
433 if (width
*M_BMPDATA
->m_bpp
% 8 != 0)
435 dst
= (char*) malloc(dstbyteperline
*height
);
438 // be careful to use the right scaling factor
439 float scx
= (float)M_BMPDATA
->m_width
/(float)newx
;
440 float scy
= (float)M_BMPDATA
->m_height
/(float)newy
;
441 // prepare accel-tables
442 int *tablex
= (int *)calloc(width
,sizeof(int));
443 int *tabley
= (int *)calloc(height
,sizeof(int));
445 // accel table filled with clipped values
446 for (int x
= 0; x
< width
; x
++)
447 tablex
[x
] = (int) (scx
* (x
+clipx
));
448 for (int y
= 0; y
< height
; y
++)
449 tabley
[y
] = (int) (scy
* (y
+clipy
));
451 // Main rescaling routine starts here
452 for (int h
= 0; h
< height
; h
++)
456 guint32 old_pixval
= 0;
458 for (int w
= 0; w
< width
; w
++)
466 pixval
= gdk_image_get_pixel( img
, x
, tabley
[h
] );
476 char shift
= bit
<< w
% 8;
482 dst
[h
*dstbyteperline
+w
/8] = outbyte
;
490 gdk_gc_set_foreground( gc
, &col
);
491 gdk_draw_point( dstpix
, gc
, w
, h
);
495 // do not forget the last byte
496 if ((bpp
== 1) && (width
% 8 != 0))
497 dst
[h
*dstbyteperline
+width
/8] = outbyte
;
500 gdk_image_destroy( img
);
501 if (gc
) gdk_gc_unref( gc
);
505 bmp
= wxBitmap( (const char *)dst
, width
, height
, 1 );
511 dstbyteperline
= width
/8;
514 dst
= (char*) malloc(dstbyteperline
*height
);
515 img
= gdk_image_get( GetMask()->GetBitmap(), 0, 0, GetWidth(), GetHeight() );
517 for (int h
= 0; h
< height
; h
++)
521 guint32 old_pixval
= 0;
523 for (int w
= 0; w
< width
; w
++)
531 pixval
= gdk_image_get_pixel( img
, x
, tabley
[h
] );
539 char shift
= bit
<< w
% 8;
545 dst
[h
*dstbyteperline
+w
/8] = outbyte
;
550 // do not forget the last byte
552 dst
[h
*dstbyteperline
+width
/8] = outbyte
;
554 wxMask
* mask
= new wxMask
;
555 mask
->m_bitmap
= gdk_bitmap_create_from_data( wxGetRootWindow()->window
, (gchar
*) dst
, width
, height
);
559 gdk_image_destroy( img
);
569 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
573 wxCHECK_MSG( image
.Ok(), FALSE
, wxT("invalid image") )
574 wxCHECK_MSG( depth
== -1 || depth
== 1, FALSE
, wxT("invalid bitmap depth") )
576 if (image
.GetWidth() <= 0 || image
.GetHeight() <= 0)
579 m_refData
= new wxBitmapRefData();
583 return CreateFromImageAsBitmap(image
);
588 if (image
.HasAlpha())
589 return CreateFromImageAsPixbuf(image
);
591 return CreateFromImageAsPixmap(image
);
595 // conversion to mono bitmap:
596 bool wxBitmap::CreateFromImageAsBitmap(const wxImage
& img
)
598 // convert alpha channel to mask, if it is present:
600 image
.ConvertAlphaToMask();
602 int width
= image
.GetWidth();
603 int height
= image
.GetHeight();
608 SetBitmap( gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 ) );
612 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
614 // Create picture image
616 unsigned char *data_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
618 GdkImage
*data_image
=
619 gdk_image_new_bitmap( visual
, data_data
, width
, height
);
623 GdkImage
*mask_image
= (GdkImage
*) NULL
;
627 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
629 mask_image
= gdk_image_new_bitmap( visual
, mask_data
, width
, height
);
631 wxMask
*mask
= new wxMask();
632 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
637 int r_mask
= image
.GetMaskRed();
638 int g_mask
= image
.GetMaskGreen();
639 int b_mask
= image
.GetMaskBlue();
641 unsigned char* data
= image
.GetData();
644 for (int y
= 0; y
< height
; y
++)
646 for (int x
= 0; x
< width
; x
++)
657 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
658 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
660 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
663 if ((r
== 255) && (b
== 255) && (g
== 255))
664 gdk_image_put_pixel( data_image
, x
, y
, 1 );
666 gdk_image_put_pixel( data_image
, x
, y
, 0 );
673 GdkGC
*data_gc
= gdk_gc_new( GetBitmap() );
675 gdk_draw_image( GetBitmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
677 gdk_image_destroy( data_image
);
678 gdk_gc_unref( data_gc
);
684 GdkGC
*mask_gc
= gdk_gc_new( GetMask()->GetBitmap() );
686 gdk_draw_image( GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
688 gdk_image_destroy( mask_image
);
689 gdk_gc_unref( mask_gc
);
695 // conversion to colour bitmap:
696 bool wxBitmap::CreateFromImageAsPixmap(const wxImage
& img
)
698 // convert alpha channel to mask, if it is present:
700 image
.ConvertAlphaToMask();
702 int width
= image
.GetWidth();
703 int height
= image
.GetHeight();
708 SetPixmap( gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, -1 ) );
710 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
712 int bpp
= visual
->depth
;
716 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800))
721 // We handle 8-bit bitmaps ourselves using the colour cube, 12-bit
722 // visuals are not supported by GDK so we do these ourselves, too.
723 // 15-bit and 16-bit should actually work and 24-bit certainly does.
725 if (!image
.HasMask() && (bpp
> 16))
727 if (!image
.HasMask() && (bpp
> 12))
730 static bool s_hasInitialized
= FALSE
;
732 if (!s_hasInitialized
)
735 s_hasInitialized
= TRUE
;
738 GdkGC
*gc
= gdk_gc_new( GetPixmap() );
740 gdk_draw_rgb_image( GetPixmap(),
752 // Create picture image
754 GdkImage
*data_image
=
755 gdk_image_new( GDK_IMAGE_FASTEST
, visual
, width
, height
);
759 GdkImage
*mask_image
= (GdkImage
*) NULL
;
763 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
765 mask_image
= gdk_image_new_bitmap( visual
, mask_data
, width
, height
);
767 wxMask
*mask
= new wxMask();
768 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
775 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
776 byte_order b_o
= RGB
;
780 if ((visual
->red_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->blue_mask
)) b_o
= RGB
;
781 else if ((visual
->red_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->green_mask
)) b_o
= RBG
;
782 else if ((visual
->blue_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->green_mask
)) b_o
= BRG
;
783 else if ((visual
->blue_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->red_mask
)) b_o
= BGR
;
784 else if ((visual
->green_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->blue_mask
)) b_o
= GRB
;
785 else if ((visual
->green_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->red_mask
)) b_o
= GBR
;
788 int r_mask
= image
.GetMaskRed();
789 int g_mask
= image
.GetMaskGreen();
790 int b_mask
= image
.GetMaskBlue();
792 unsigned char* data
= image
.GetData();
795 for (int y
= 0; y
< height
; y
++)
797 for (int x
= 0; x
< width
; x
++)
808 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
809 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
811 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
819 if (wxTheApp
->m_colorCube
)
821 pixel
= wxTheApp
->m_colorCube
[ ((r
& 0xf8) << 7) + ((g
& 0xf8) << 2) + ((b
& 0xf8) >> 3) ];
825 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
826 GdkColor
*colors
= cmap
->colors
;
827 int max
= 3 * (65536);
829 for (int i
= 0; i
< cmap
->size
; i
++)
831 int rdiff
= (r
<< 8) - colors
[i
].red
;
832 int gdiff
= (g
<< 8) - colors
[i
].green
;
833 int bdiff
= (b
<< 8) - colors
[i
].blue
;
834 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
835 if (sum
< max
) { pixel
= i
; max
= sum
; }
839 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
848 case RGB
: pixel
= ((r
& 0xf0) << 4) | (g
& 0xf0) | ((b
& 0xf0) >> 4); break;
849 case RBG
: pixel
= ((r
& 0xf0) << 4) | (b
& 0xf0) | ((g
& 0xf0) >> 4); break;
850 case GRB
: pixel
= ((g
& 0xf0) << 4) | (r
& 0xf0) | ((b
& 0xf0) >> 4); break;
851 case GBR
: pixel
= ((g
& 0xf0) << 4) | (b
& 0xf0) | ((r
& 0xf0) >> 4); break;
852 case BRG
: pixel
= ((b
& 0xf0) << 4) | (r
& 0xf0) | ((g
& 0xf0) >> 4); break;
853 case BGR
: pixel
= ((b
& 0xf0) << 4) | (g
& 0xf0) | ((r
& 0xf0) >> 4); break;
855 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
863 case RGB
: pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
864 case RBG
: pixel
= ((r
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
865 case GRB
: pixel
= ((g
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
866 case GBR
: pixel
= ((g
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
867 case BRG
: pixel
= ((b
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
868 case BGR
: pixel
= ((b
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
870 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
875 // I actually don't know if for 16-bit displays, it is alway the green
876 // component or the second component which has 6 bits.
880 case RGB
: pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
881 case RBG
: pixel
= ((r
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
882 case GRB
: pixel
= ((g
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
883 case GBR
: pixel
= ((g
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
884 case BRG
: pixel
= ((b
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
885 case BGR
: pixel
= ((b
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
887 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
896 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
897 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
898 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
899 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
900 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
901 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
903 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
912 GdkGC
*data_gc
= gdk_gc_new( GetPixmap() );
914 gdk_draw_image( GetPixmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
916 gdk_image_destroy( data_image
);
917 gdk_gc_unref( data_gc
);
923 GdkGC
*mask_gc
= gdk_gc_new( GetMask()->GetBitmap() );
925 gdk_draw_image( GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
927 gdk_image_destroy( mask_image
);
928 gdk_gc_unref( mask_gc
);
935 bool wxBitmap::CreateFromImageAsPixbuf(const wxImage
& image
)
937 int width
= image
.GetWidth();
938 int height
= image
.GetHeight();
940 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
942 8 /* bits per sample */,
947 wxASSERT( image
.HasAlpha() ); // for now
948 wxASSERT( gdk_pixbuf_get_n_channels(pixbuf
) == 4 );
949 wxASSERT( gdk_pixbuf_get_width(pixbuf
) == width
);
950 wxASSERT( gdk_pixbuf_get_height(pixbuf
) == height
);
952 M_BMPDATA
->m_pixbuf
= pixbuf
;
955 SetDepth(wxTheApp
->GetGdkVisual()->depth
);
958 unsigned char *in
= image
.GetData();
959 unsigned char *out
= gdk_pixbuf_get_pixels(pixbuf
);
960 unsigned char *alpha
= image
.GetAlpha();
962 int rowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
964 for (int y
= 0; y
< height
; y
++, out
+= rowinc
)
966 for (int x
= 0; x
< width
; x
++, alpha
++, out
+= 4, in
+= 3)
977 #endif // __WXGTK20__
979 wxImage
wxBitmap::ConvertToImage() const
983 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
985 image
.Create(GetWidth(), GetHeight());
986 unsigned char *data
= image
.GetData();
990 wxFAIL_MSG( wxT("couldn't create image") );
997 GdkPixbuf
*pixbuf
= GetPixbuf();
998 wxASSERT( gdk_pixbuf_get_has_alpha(pixbuf
) );
1001 int h
= GetHeight();
1005 unsigned char *alpha
= image
.GetAlpha();
1006 unsigned char *in
= gdk_pixbuf_get_pixels(pixbuf
);
1007 unsigned char *out
= data
;
1008 int rowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * w
;
1010 for (int y
= 0; y
< h
; y
++, in
+= rowinc
)
1012 for (int x
= 0; x
< w
; x
++, in
+= 4, out
+= 3, alpha
++)
1022 #endif // __WXGTK20__
1024 // the colour used as transparent one in wxImage and the one it is
1025 // replaced with when it really occurs in the bitmap
1026 static const int MASK_RED
= 1;
1027 static const int MASK_GREEN
= 2;
1028 static const int MASK_BLUE
= 3;
1029 static const int MASK_BLUE_REPLACEMENT
= 2;
1031 GdkImage
*gdk_image
= (GdkImage
*) NULL
;
1035 gdk_image
= gdk_image_get( GetPixmap(),
1037 GetWidth(), GetHeight() );
1039 else if (GetBitmap())
1041 gdk_image
= gdk_image_get( GetBitmap(),
1043 GetWidth(), GetHeight() );
1047 wxFAIL_MSG( wxT("Ill-formed bitmap") );
1050 wxCHECK_MSG( gdk_image
, wxNullImage
, wxT("couldn't create image") );
1052 GdkImage
*gdk_image_mask
= (GdkImage
*) NULL
;
1055 gdk_image_mask
= gdk_image_get( GetMask()->GetBitmap(),
1057 GetWidth(), GetHeight() );
1059 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
1063 int red_shift_right
= 0;
1064 int green_shift_right
= 0;
1065 int blue_shift_right
= 0;
1066 int red_shift_left
= 0;
1067 int green_shift_left
= 0;
1068 int blue_shift_left
= 0;
1069 bool use_shift
= FALSE
;
1073 GdkVisual
*visual
= gdk_window_get_visual( GetPixmap() );
1075 visual
= wxTheApp
->GetGdkVisual();
1077 bpp
= visual
->depth
;
1079 bpp
= visual
->red_prec
+ visual
->green_prec
+ visual
->blue_prec
;
1080 red_shift_right
= visual
->red_shift
;
1081 red_shift_left
= 8-visual
->red_prec
;
1082 green_shift_right
= visual
->green_shift
;
1083 green_shift_left
= 8-visual
->green_prec
;
1084 blue_shift_right
= visual
->blue_shift
;
1085 blue_shift_left
= 8-visual
->blue_prec
;
1087 use_shift
= (visual
->type
== GDK_VISUAL_TRUE_COLOR
) || (visual
->type
== GDK_VISUAL_DIRECT_COLOR
);
1095 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
1098 for (int j
= 0; j
< GetHeight(); j
++)
1100 for (int i
= 0; i
< GetWidth(); i
++)
1102 wxUint32 pixel
= gdk_image_get_pixel( gdk_image
, i
, j
);
1120 data
[pos
] = (pixel
>> red_shift_right
) << red_shift_left
;
1121 data
[pos
+1] = (pixel
>> green_shift_right
) << green_shift_left
;
1122 data
[pos
+2] = (pixel
>> blue_shift_right
) << blue_shift_left
;
1124 else if (cmap
->colors
)
1126 data
[pos
] = cmap
->colors
[pixel
].red
>> 8;
1127 data
[pos
+1] = cmap
->colors
[pixel
].green
>> 8;
1128 data
[pos
+2] = cmap
->colors
[pixel
].blue
>> 8;
1132 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
1137 int mask_pixel
= gdk_image_get_pixel( gdk_image_mask
, i
, j
);
1138 if (mask_pixel
== 0)
1140 data
[pos
] = MASK_RED
;
1141 data
[pos
+1] = MASK_GREEN
;
1142 data
[pos
+2] = MASK_BLUE
;
1144 else if ( data
[pos
] == MASK_RED
&&
1145 data
[pos
+1] == MASK_GREEN
&&
1146 data
[pos
+2] == MASK_BLUE
)
1148 data
[pos
+2] = MASK_BLUE_REPLACEMENT
;
1156 gdk_image_destroy( gdk_image
);
1157 if (gdk_image_mask
) gdk_image_destroy( gdk_image_mask
);
1163 wxBitmap::wxBitmap( const wxBitmap
& bmp
)
1169 wxBitmap::wxBitmap( const wxString
&filename
, wxBitmapType type
)
1171 LoadFile( filename
, type
);
1174 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int WXUNUSED(depth
))
1176 if ( width
> 0 && height
> 0 )
1178 m_refData
= new wxBitmapRefData();
1180 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
1181 M_BMPDATA
->m_bitmap
= gdk_bitmap_create_from_data
1183 wxGetRootWindow()->window
,
1188 M_BMPDATA
->m_width
= width
;
1189 M_BMPDATA
->m_height
= height
;
1190 M_BMPDATA
->m_bpp
= 1;
1192 wxASSERT_MSG( M_BMPDATA
->m_bitmap
, wxT("couldn't create bitmap") );
1196 wxBitmap::~wxBitmap()
1200 wxBitmap
& wxBitmap::operator = ( const wxBitmap
& bmp
)
1202 if ( m_refData
!= bmp
.m_refData
)
1208 bool wxBitmap::operator == ( const wxBitmap
& bmp
) const
1210 return m_refData
== bmp
.m_refData
;
1213 bool wxBitmap::operator != ( const wxBitmap
& bmp
) const
1215 return m_refData
!= bmp
.m_refData
;
1218 bool wxBitmap::Ok() const
1220 return (m_refData
!= NULL
) &&
1223 M_BMPDATA
->m_pixbuf
||
1225 M_BMPDATA
->m_bitmap
|| M_BMPDATA
->m_pixmap
1229 int wxBitmap::GetHeight() const
1231 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1233 return M_BMPDATA
->m_height
;
1236 int wxBitmap::GetWidth() const
1238 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1240 return M_BMPDATA
->m_width
;
1243 int wxBitmap::GetDepth() const
1245 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1247 return M_BMPDATA
->m_bpp
;
1250 wxMask
*wxBitmap::GetMask() const
1252 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
1254 return M_BMPDATA
->m_mask
;
1257 void wxBitmap::SetMask( wxMask
*mask
)
1259 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
1261 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
1263 M_BMPDATA
->m_mask
= mask
;
1266 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
1272 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
1274 wxCHECK_MSG( Ok() &&
1275 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1276 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) && (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
1277 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
1279 wxBitmap
ret( rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
1280 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
1285 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
1286 gdk_pixbuf_get_has_alpha(GetPixbuf()),
1287 8, GetWidth(), GetHeight());
1288 ret
.SetPixbuf(pixbuf
);
1289 gdk_pixbuf_copy_area(GetPixbuf(),
1290 rect
.x
, rect
.y
, rect
.width
, rect
.height
,
1294 #endif // __WXGTK20__
1296 if (ret
.GetPixmap())
1298 GdkGC
*gc
= gdk_gc_new( ret
.GetPixmap() );
1299 gdk_draw_pixmap( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1300 gdk_gc_destroy( gc
);
1304 GdkGC
*gc
= gdk_gc_new( ret
.GetBitmap() );
1306 col
.pixel
= 0xFFFFFF;
1307 gdk_gc_set_foreground( gc
, &col
);
1309 gdk_gc_set_background( gc
, &col
);
1310 gdk_wx_draw_bitmap( ret
.GetBitmap(), gc
, GetBitmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1311 gdk_gc_destroy( gc
);
1317 wxMask
*mask
= new wxMask
;
1318 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, rect
.width
, rect
.height
, 1 );
1320 GdkGC
*gc
= gdk_gc_new( mask
->m_bitmap
);
1322 col
.pixel
= 0xFFFFFF;
1323 gdk_gc_set_foreground( gc
, &col
);
1325 gdk_gc_set_background( gc
, &col
);
1326 gdk_wx_draw_bitmap( mask
->m_bitmap
, gc
, M_BMPDATA
->m_mask
->m_bitmap
, rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1327 gdk_gc_destroy( gc
);
1329 ret
.SetMask( mask
);
1335 bool wxBitmap::SaveFile( const wxString
&name
, wxBitmapType type
, const wxPalette
*WXUNUSED(palette
) ) const
1337 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid bitmap") );
1339 // Try to save the bitmap via wxImage handlers:
1341 wxImage image
= ConvertToImage();
1342 if (image
.Ok()) return image
.SaveFile( name
, type
);
1348 bool wxBitmap::LoadFile( const wxString
&name
, wxBitmapType type
)
1352 if (!wxFileExists(name
))
1355 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
1357 if (type
== wxBITMAP_TYPE_XPM
)
1359 m_refData
= new wxBitmapRefData();
1361 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
1363 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm
1365 wxGetRootWindow()->window
,
1373 M_BMPDATA
->m_mask
= new wxMask();
1374 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
1377 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
1379 M_BMPDATA
->m_bpp
= visual
->depth
;
1381 else // try if wxImage can load it
1384 if ( !image
.LoadFile( name
, type
) || !image
.Ok() )
1387 *this = wxBitmap(image
);
1393 wxPalette
*wxBitmap::GetPalette() const
1396 return (wxPalette
*) NULL
;
1398 return M_BMPDATA
->m_palette
;
1401 void wxBitmap::SetPalette(const wxPalette
& WXUNUSED(palette
))
1406 void wxBitmap::SetHeight( int height
)
1409 m_refData
= new wxBitmapRefData();
1411 M_BMPDATA
->m_height
= height
;
1414 void wxBitmap::SetWidth( int width
)
1417 m_refData
= new wxBitmapRefData();
1419 M_BMPDATA
->m_width
= width
;
1422 void wxBitmap::SetDepth( int depth
)
1425 m_refData
= new wxBitmapRefData();
1427 M_BMPDATA
->m_bpp
= depth
;
1430 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
1433 m_refData
= new wxBitmapRefData();
1435 M_BMPDATA
->m_pixmap
= pixmap
;
1437 PurgeOtherRepresentations(Pixmap
);
1441 void wxBitmap::SetBitmap( GdkPixmap
*bitmap
)
1444 m_refData
= new wxBitmapRefData();
1446 M_BMPDATA
->m_bitmap
= bitmap
;
1448 PurgeOtherRepresentations(Pixmap
);
1452 GdkPixmap
*wxBitmap::GetPixmap() const
1454 wxCHECK_MSG( Ok(), (GdkPixmap
*) NULL
, wxT("invalid bitmap") );
1457 // create the pixmap on the fly if we use Pixbuf representation:
1458 if (HasPixbuf() && !HasPixmap())
1460 delete M_BMPDATA
->m_mask
;
1461 M_BMPDATA
->m_mask
= new wxMask();
1462 gdk_pixbuf_render_pixmap_and_mask(M_BMPDATA
->m_pixbuf
,
1463 &M_BMPDATA
->m_pixmap
,
1464 &M_BMPDATA
->m_mask
->m_bitmap
,
1467 #endif // __WXGTK20__
1469 return M_BMPDATA
->m_pixmap
;
1472 bool wxBitmap::HasPixmap() const
1474 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1476 return M_BMPDATA
->m_pixmap
!= NULL
;
1479 GdkBitmap
*wxBitmap::GetBitmap() const
1481 wxCHECK_MSG( Ok(), (GdkBitmap
*) NULL
, wxT("invalid bitmap") );
1483 return M_BMPDATA
->m_bitmap
;
1487 GdkPixbuf
*wxBitmap::GetPixbuf() const
1489 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
1491 if (HasPixmap() && !HasPixbuf())
1493 int width
= GetWidth();
1494 int height
= GetHeight();
1496 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
1499 M_BMPDATA
->m_pixbuf
=
1500 gdk_pixbuf_get_from_drawable(pixbuf
, M_BMPDATA
->m_pixmap
, NULL
,
1501 0, 0, 0, 0, width
, height
);
1503 // apply the mask to created pixbuf:
1504 if (M_BMPDATA
->m_pixbuf
&& M_BMPDATA
->m_mask
)
1507 gdk_pixbuf_get_from_drawable(NULL
,
1508 M_BMPDATA
->m_mask
->GetBitmap(),
1510 0, 0, 0, 0, width
, height
);
1513 guchar
*bmp
= gdk_pixbuf_get_pixels(pixbuf
);
1514 guchar
*mask
= gdk_pixbuf_get_pixels(pmask
);
1515 int bmprowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
1516 int maskrowinc
= gdk_pixbuf_get_rowstride(pmask
) - 3 * width
;
1518 for (int y
= 0; y
< height
;
1519 y
++, bmp
+= bmprowinc
, mask
+= maskrowinc
)
1521 for (int x
= 0; x
< width
; x
++, bmp
+= 4, mask
+= 3)
1523 if (mask
[0] == 0 /*black pixel*/)
1528 gdk_pixbuf_unref(pmask
);
1533 return M_BMPDATA
->m_pixbuf
;
1536 bool wxBitmap::HasPixbuf() const
1538 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1540 return M_BMPDATA
->m_pixbuf
!= NULL
;
1543 void wxBitmap::SetPixbuf( GdkPixbuf
*pixbuf
)
1546 m_refData
= new wxBitmapRefData();
1548 M_BMPDATA
->m_pixbuf
= pixbuf
;
1549 PurgeOtherRepresentations(Pixbuf
);
1552 void wxBitmap::PurgeOtherRepresentations(wxBitmap::Representation keep
)
1554 if (keep
== Pixmap
&& HasPixbuf())
1556 gdk_pixbuf_unref( M_BMPDATA
->m_pixbuf
);
1557 M_BMPDATA
->m_pixbuf
= NULL
;
1559 if (keep
== Pixbuf
&& HasPixmap())
1561 gdk_pixmap_unref( M_BMPDATA
->m_pixmap
);
1562 M_BMPDATA
->m_pixmap
= NULL
;
1566 #endif // __WXGTK20__
1568 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1574 GdkPixbuf
*pixbuf
= GetPixbuf();
1579 if (gdk_pixbuf_get_has_alpha( pixbuf
))
1580 wxPrintf( wxT("Has alpha\n") );
1582 wxPrintf( wxT("No alpha.\n") );
1585 data
.m_height
= gdk_pixbuf_get_height( pixbuf
);
1586 data
.m_width
= gdk_pixbuf_get_width( pixbuf
);
1587 data
.m_stride
= gdk_pixbuf_get_rowstride( pixbuf
);
1589 return gdk_pixbuf_get_pixels( pixbuf
);
1595 void wxBitmap::UngetRawData(wxPixelDataBase
& data
)
1599 //-----------------------------------------------------------------------------
1601 //-----------------------------------------------------------------------------
1603 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
,wxBitmapHandlerBase
)
1605 wxBitmapHandler::~wxBitmapHandler()
1609 bool wxBitmapHandler::Create(wxBitmap
*bitmap
, void *data
, long type
, int width
, int height
, int depth
)
1614 bool wxBitmapHandler::LoadFile(wxBitmap
*bitmap
, const wxString
& name
, long flags
,
1615 int desiredWidth
, int desiredHeight
)
1620 bool wxBitmapHandler::SaveFile(const wxBitmap
*bitmap
, const wxString
& name
, int type
, const wxPalette
*palette
)
1625 /* static */ void wxBitmap::InitStandardHandlers()
1627 // TODO: Insert handler based on GdkPixbufs handler later