1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/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"
24 #include "wx/filefn.h"
30 #include <gdk/gdkrgb.h>
33 void gdk_wx_draw_bitmap (GdkDrawable
*drawable
,
43 //-----------------------------------------------------------------------------
45 //-----------------------------------------------------------------------------
47 extern GtkWidget
*wxGetRootWindow();
49 //-----------------------------------------------------------------------------
51 //-----------------------------------------------------------------------------
53 IMPLEMENT_DYNAMIC_CLASS(wxMask
,wxObject
)
57 m_bitmap
= (GdkBitmap
*) NULL
;
60 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
62 m_bitmap
= (GdkBitmap
*) NULL
;
63 Create( bitmap
, colour
);
67 wxMask::wxMask( const wxBitmap
& bitmap
, int paletteIndex
)
69 m_bitmap
= (GdkBitmap
*) NULL
;
70 Create( bitmap
, paletteIndex
);
72 #endif // wxUSE_PALETTE
74 wxMask::wxMask( const wxBitmap
& bitmap
)
76 m_bitmap
= (GdkBitmap
*) NULL
;
83 gdk_bitmap_unref( m_bitmap
);
86 bool wxMask::Create( const wxBitmap
& bitmap
,
87 const wxColour
& colour
)
91 gdk_bitmap_unref( m_bitmap
);
92 m_bitmap
= (GdkBitmap
*) NULL
;
95 wxImage image
= bitmap
.ConvertToImage();
96 if (!image
.Ok()) return false;
98 m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, image
.GetWidth(), image
.GetHeight(), 1 );
99 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
106 gdk_gc_set_foreground( gc
, &color
);
107 gdk_gc_set_fill( gc
, GDK_SOLID
);
108 gdk_draw_rectangle( m_bitmap
, gc
, TRUE
, 0, 0, image
.GetWidth(), image
.GetHeight() );
110 unsigned char *data
= image
.GetData();
113 unsigned char red
= colour
.Red();
114 unsigned char green
= colour
.Green();
115 unsigned char blue
= colour
.Blue();
117 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
119 int bpp
= visual
->depth
;
120 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800))
125 green
= green
& 0xf8;
131 green
= green
& 0xfc;
137 green
= green
& 0xf0;
145 gdk_gc_set_foreground( gc
, &color
);
147 for (int j
= 0; j
< image
.GetHeight(); j
++)
151 for (i
= 0; i
< image
.GetWidth(); i
++)
153 if ((data
[index
] == red
) &&
154 (data
[index
+1] == green
) &&
155 (data
[index
+2] == blue
))
164 gdk_draw_line( m_bitmap
, gc
, start_x
, j
, i
-1, j
);
171 gdk_draw_line( m_bitmap
, gc
, start_x
, j
, i
, j
);
180 bool wxMask::Create( const wxBitmap
& bitmap
, int paletteIndex
)
183 wxPalette
*pal
= bitmap
.GetPalette();
185 wxCHECK_MSG( pal
, false, wxT("Cannot create mask from bitmap without palette") );
187 pal
->GetRGB(paletteIndex
, &r
, &g
, &b
);
189 return Create(bitmap
, wxColour(r
, g
, b
));
191 #endif // wxUSE_PALETTE
193 bool wxMask::Create( const wxBitmap
& bitmap
)
197 gdk_bitmap_unref( m_bitmap
);
198 m_bitmap
= (GdkBitmap
*) NULL
;
201 if (!bitmap
.Ok()) return false;
203 wxCHECK_MSG( bitmap
.GetBitmap(), false, wxT("Cannot create mask from colour bitmap") );
205 m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, bitmap
.GetWidth(), bitmap
.GetHeight(), 1 );
207 if (!m_bitmap
) return false;
209 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
211 gdk_wx_draw_bitmap( m_bitmap
, gc
, bitmap
.GetBitmap(), 0, 0, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight() );
218 GdkBitmap
*wxMask::GetBitmap() const
223 //-----------------------------------------------------------------------------
225 //-----------------------------------------------------------------------------
227 class wxBitmapRefData
: public wxObjectRefData
231 virtual ~wxBitmapRefData();
239 wxPalette
*m_palette
;
242 wxBitmapRefData::wxBitmapRefData()
244 m_pixmap
= (GdkPixmap
*) NULL
;
245 m_bitmap
= (GdkBitmap
*) NULL
;
246 m_mask
= (wxMask
*) NULL
;
250 m_palette
= (wxPalette
*) NULL
;
253 wxBitmapRefData::~wxBitmapRefData()
256 gdk_pixmap_unref( m_pixmap
);
258 gdk_bitmap_unref( m_bitmap
);
262 #endif // wxUSE_PALETTE
265 //-----------------------------------------------------------------------------
267 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
269 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
275 wxBitmap::wxBitmap( int width
, int height
, int depth
)
277 Create( width
, height
, depth
);
280 bool wxBitmap::Create( int width
, int height
, int depth
)
284 if ( width
<= 0 || height
<= 0 )
289 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
292 depth
= visual
->depth
;
294 wxCHECK_MSG( (depth
== visual
->depth
) || (depth
== 1) || (depth
== 32), false,
295 wxT("invalid bitmap depth") );
297 m_refData
= new wxBitmapRefData();
298 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
299 M_BMPDATA
->m_width
= width
;
300 M_BMPDATA
->m_height
= height
;
303 M_BMPDATA
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
304 M_BMPDATA
->m_bpp
= 1;
308 M_BMPDATA
->m_pixmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, depth
);
309 M_BMPDATA
->m_bpp
= visual
->depth
;
315 bool wxBitmap::CreateFromXpm( const char **bits
)
319 wxCHECK_MSG( bits
!= NULL
, false, wxT("invalid bitmap data") );
321 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
323 m_refData
= new wxBitmapRefData();
325 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
327 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm_d( wxGetRootWindow()->window
, &mask
, NULL
, (gchar
**) bits
);
329 wxCHECK_MSG( M_BMPDATA
->m_pixmap
, false, wxT("couldn't create pixmap") );
333 M_BMPDATA
->m_mask
= new wxMask();
334 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
337 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
339 M_BMPDATA
->m_bpp
= visual
->depth
; // Can we get a different depth from create_from_xpm_d() ?
344 wxBitmap
wxBitmap::Rescale( int clipx
, int clipy
, int clipwidth
, int clipheight
, int newx
, int newy
)
346 wxCHECK_MSG( Ok(), wxNullBitmap
, wxT("invalid bitmap") );
348 if (newy
==M_BMPDATA
->m_width
&& newy
==M_BMPDATA
->m_height
)
351 int width
= wxMax(newx
, 1);
352 int height
= wxMax(newy
, 1);
353 width
= wxMin(width
, clipwidth
);
354 height
= wxMin(height
, clipheight
);
358 GdkImage
*img
= (GdkImage
*) NULL
;
360 img
= gdk_image_get( GetPixmap(), 0, 0, GetWidth(), GetHeight() );
361 else if (GetBitmap())
362 img
= gdk_image_get( GetBitmap(), 0, 0, GetWidth(), GetHeight() );
364 wxFAIL_MSG( wxT("Ill-formed bitmap") );
366 wxCHECK_MSG( img
, wxNullBitmap
, wxT("couldn't create image") );
372 GdkPixmap
*dstpix
= NULL
;
375 GdkVisual
*visual
= gdk_window_get_visual( GetPixmap() );
377 visual
= wxTheApp
->GetGdkVisual();
380 bmp
= wxBitmap(width
,height
,bpp
);
381 dstpix
= bmp
.GetPixmap();
382 gc
= gdk_gc_new( dstpix
);
386 long dstbyteperline
= 0;
391 dstbyteperline
= width
/8*M_BMPDATA
->m_bpp
;
392 if (width
*M_BMPDATA
->m_bpp
% 8 != 0)
394 dst
= (char*) malloc(dstbyteperline
*height
);
397 // be careful to use the right scaling factor
398 float scx
= (float)M_BMPDATA
->m_width
/(float)newx
;
399 float scy
= (float)M_BMPDATA
->m_height
/(float)newy
;
400 // prepare accel-tables
401 int *tablex
= (int *)calloc(width
,sizeof(int));
402 int *tabley
= (int *)calloc(height
,sizeof(int));
404 // accel table filled with clipped values
405 for (int x
= 0; x
< width
; x
++)
406 tablex
[x
] = (int) (scx
* (x
+clipx
));
407 for (int y
= 0; y
< height
; y
++)
408 tabley
[y
] = (int) (scy
* (y
+clipy
));
410 // Main rescaling routine starts here
411 for (int h
= 0; h
< height
; h
++)
415 guint32 old_pixval
= 0;
417 for (int w
= 0; w
< width
; w
++)
425 pixval
= gdk_image_get_pixel( img
, x
, tabley
[h
] );
435 char shift
= bit
<< (w
% 8);
441 dst
[h
*dstbyteperline
+w
/8] = outbyte
;
449 gdk_gc_set_foreground( gc
, &col
);
450 gdk_draw_point( dstpix
, gc
, w
, h
);
454 // do not forget the last byte
455 if ((bpp
== 1) && (width
% 8 != 0))
456 dst
[h
*dstbyteperline
+width
/8] = outbyte
;
459 gdk_image_destroy( img
);
460 if (gc
) gdk_gc_unref( gc
);
464 bmp
= wxBitmap( (const char *)dst
, width
, height
, 1 );
470 dstbyteperline
= width
/8;
473 dst
= (char*) malloc(dstbyteperline
*height
);
474 img
= gdk_image_get( GetMask()->GetBitmap(), 0, 0, GetWidth(), GetHeight() );
476 for (int h
= 0; h
< height
; h
++)
480 guint32 old_pixval
= 0;
482 for (int w
= 0; w
< width
; w
++)
490 pixval
= gdk_image_get_pixel( img
, x
, tabley
[h
] );
498 char shift
= bit
<< (w
% 8);
504 dst
[h
*dstbyteperline
+w
/8] = outbyte
;
509 // do not forget the last byte
511 dst
[h
*dstbyteperline
+width
/8] = outbyte
;
513 wxMask
* mask
= new wxMask
;
514 mask
->m_bitmap
= gdk_bitmap_create_from_data( wxGetRootWindow()->window
, (gchar
*) dst
, width
, height
);
518 gdk_image_destroy( img
);
527 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
531 wxCHECK_MSG( image
.Ok(), false, wxT("invalid image") );
532 wxCHECK_MSG( depth
== -1 || depth
== 1, false, wxT("invalid bitmap depth") );
534 if (image
.GetWidth() <= 0 || image
.GetHeight() <= 0)
537 m_refData
= new wxBitmapRefData();
541 return CreateFromImageAsBitmap(image
);
545 return CreateFromImageAsPixmap(image
);
549 // conversion to mono bitmap:
550 bool wxBitmap::CreateFromImageAsBitmap(const wxImage
& img
)
552 // convert alpha channel to mask, if it is present:
554 image
.ConvertAlphaToMask();
556 int width
= image
.GetWidth();
557 int height
= image
.GetHeight();
562 SetBitmap( gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 ) );
566 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
568 // Create picture image
570 unsigned char *data_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
572 GdkImage
*data_image
=
573 gdk_image_new_bitmap( visual
, data_data
, width
, height
);
577 GdkImage
*mask_image
= (GdkImage
*) NULL
;
581 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
583 mask_image
= gdk_image_new_bitmap( visual
, mask_data
, width
, height
);
585 wxMask
*mask
= new wxMask();
586 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
591 int r_mask
= image
.GetMaskRed();
592 int g_mask
= image
.GetMaskGreen();
593 int b_mask
= image
.GetMaskBlue();
595 unsigned char* data
= image
.GetData();
598 for (int y
= 0; y
< height
; y
++)
600 for (int x
= 0; x
< width
; x
++)
611 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
612 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
614 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
617 if ((r
== 255) && (b
== 255) && (g
== 255))
618 gdk_image_put_pixel( data_image
, x
, y
, 1 );
620 gdk_image_put_pixel( data_image
, x
, y
, 0 );
627 GdkGC
*data_gc
= gdk_gc_new( GetBitmap() );
629 gdk_draw_image( GetBitmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
631 gdk_image_destroy( data_image
);
632 gdk_gc_unref( data_gc
);
638 GdkGC
*mask_gc
= gdk_gc_new( GetMask()->GetBitmap() );
640 gdk_draw_image( GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
642 gdk_image_destroy( mask_image
);
643 gdk_gc_unref( mask_gc
);
649 // conversion to colour bitmap:
650 bool wxBitmap::CreateFromImageAsPixmap(const wxImage
& img
)
652 // convert alpha channel to mask, if it is present:
654 image
.ConvertAlphaToMask();
656 int width
= image
.GetWidth();
657 int height
= image
.GetHeight();
662 SetPixmap( gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, -1 ) );
664 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
666 int bpp
= visual
->depth
;
670 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800))
675 // We handle 8-bit bitmaps ourselves using the colour cube, 12-bit
676 // visuals are not supported by GDK so we do these ourselves, too.
677 // 15-bit and 16-bit should actually work and 24-bit certainly does.
679 if (!image
.HasMask() && (bpp
> 16))
681 if (!image
.HasMask() && (bpp
> 12))
684 static bool s_hasInitialized
= false;
686 if (!s_hasInitialized
)
689 s_hasInitialized
= true;
692 GdkGC
*gc
= gdk_gc_new( GetPixmap() );
694 gdk_draw_rgb_image( GetPixmap(),
706 // Create picture image
708 GdkImage
*data_image
=
709 gdk_image_new( GDK_IMAGE_FASTEST
, visual
, width
, height
);
713 GdkImage
*mask_image
= (GdkImage
*) NULL
;
717 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
719 mask_image
= gdk_image_new_bitmap( visual
, mask_data
, width
, height
);
721 wxMask
*mask
= new wxMask();
722 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
729 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
730 byte_order b_o
= RGB
;
734 if ((visual
->red_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->blue_mask
)) b_o
= RGB
;
735 else if ((visual
->red_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->green_mask
)) b_o
= RBG
;
736 else if ((visual
->blue_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->green_mask
)) b_o
= BRG
;
737 else if ((visual
->blue_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->red_mask
)) b_o
= BGR
;
738 else if ((visual
->green_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->blue_mask
)) b_o
= GRB
;
739 else if ((visual
->green_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->red_mask
)) b_o
= GBR
;
742 int r_mask
= image
.GetMaskRed();
743 int g_mask
= image
.GetMaskGreen();
744 int b_mask
= image
.GetMaskBlue();
746 unsigned char* data
= image
.GetData();
749 for (int y
= 0; y
< height
; y
++)
751 for (int x
= 0; x
< width
; x
++)
762 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
763 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
765 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
773 if (wxTheApp
->m_colorCube
)
775 pixel
= wxTheApp
->m_colorCube
[ ((r
& 0xf8) << 7) + ((g
& 0xf8) << 2) + ((b
& 0xf8) >> 3) ];
779 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
780 GdkColor
*colors
= cmap
->colors
;
781 int max
= 3 * (65536);
783 for (int i
= 0; i
< cmap
->size
; i
++)
785 int rdiff
= (r
<< 8) - colors
[i
].red
;
786 int gdiff
= (g
<< 8) - colors
[i
].green
;
787 int bdiff
= (b
<< 8) - colors
[i
].blue
;
788 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
789 if (sum
< max
) { pixel
= i
; max
= sum
; }
793 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
802 case RGB
: pixel
= ((r
& 0xf0) << 4) | (g
& 0xf0) | ((b
& 0xf0) >> 4); break;
803 case RBG
: pixel
= ((r
& 0xf0) << 4) | (b
& 0xf0) | ((g
& 0xf0) >> 4); break;
804 case GRB
: pixel
= ((g
& 0xf0) << 4) | (r
& 0xf0) | ((b
& 0xf0) >> 4); break;
805 case GBR
: pixel
= ((g
& 0xf0) << 4) | (b
& 0xf0) | ((r
& 0xf0) >> 4); break;
806 case BRG
: pixel
= ((b
& 0xf0) << 4) | (r
& 0xf0) | ((g
& 0xf0) >> 4); break;
807 case BGR
: pixel
= ((b
& 0xf0) << 4) | (g
& 0xf0) | ((r
& 0xf0) >> 4); break;
809 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
817 case RGB
: pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
818 case RBG
: pixel
= ((r
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
819 case GRB
: pixel
= ((g
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
820 case GBR
: pixel
= ((g
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
821 case BRG
: pixel
= ((b
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
822 case BGR
: pixel
= ((b
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
824 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
829 // I actually don't know if for 16-bit displays, it is alway the green
830 // component or the second component which has 6 bits.
834 case RGB
: pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
835 case RBG
: pixel
= ((r
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
836 case GRB
: pixel
= ((g
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
837 case GBR
: pixel
= ((g
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
838 case BRG
: pixel
= ((b
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
839 case BGR
: pixel
= ((b
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
841 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
850 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
851 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
852 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
853 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
854 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
855 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
857 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
867 GdkGC
*data_gc
= gdk_gc_new( GetPixmap() );
869 gdk_draw_image( GetPixmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
871 gdk_image_destroy( data_image
);
872 gdk_gc_unref( data_gc
);
878 GdkGC
*mask_gc
= gdk_gc_new( GetMask()->GetBitmap() );
880 gdk_draw_image( GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
882 gdk_image_destroy( mask_image
);
883 gdk_gc_unref( mask_gc
);
889 wxImage
wxBitmap::ConvertToImage() const
893 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
895 image
.Create(GetWidth(), GetHeight());
896 unsigned char *data
= image
.GetData();
900 wxFAIL_MSG( wxT("couldn't create image") );
904 // the colour used as transparent one in wxImage and the one it is
905 // replaced with when it really occurs in the bitmap
906 static const int MASK_RED
= 1;
907 static const int MASK_GREEN
= 2;
908 static const int MASK_BLUE
= 3;
909 static const int MASK_BLUE_REPLACEMENT
= 2;
911 GdkImage
*gdk_image
= (GdkImage
*) NULL
;
915 gdk_image
= gdk_image_get( GetPixmap(),
917 GetWidth(), GetHeight() );
919 else if (GetBitmap())
921 gdk_image
= gdk_image_get( GetBitmap(),
923 GetWidth(), GetHeight() );
927 wxFAIL_MSG( wxT("Ill-formed bitmap") );
930 wxCHECK_MSG( gdk_image
, wxNullImage
, wxT("couldn't create image") );
932 GdkImage
*gdk_image_mask
= (GdkImage
*) NULL
;
935 gdk_image_mask
= gdk_image_get( GetMask()->GetBitmap(),
937 GetWidth(), GetHeight() );
939 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
943 int red_shift_right
= 0;
944 int green_shift_right
= 0;
945 int blue_shift_right
= 0;
946 int red_shift_left
= 0;
947 int green_shift_left
= 0;
948 int blue_shift_left
= 0;
949 bool use_shift
= false;
953 GdkVisual
*visual
= gdk_window_get_visual( GetPixmap() );
955 visual
= wxTheApp
->GetGdkVisual();
959 bpp
= visual
->red_prec
+ visual
->green_prec
+ visual
->blue_prec
;
960 red_shift_right
= visual
->red_shift
;
961 red_shift_left
= 8-visual
->red_prec
;
962 green_shift_right
= visual
->green_shift
;
963 green_shift_left
= 8-visual
->green_prec
;
964 blue_shift_right
= visual
->blue_shift
;
965 blue_shift_left
= 8-visual
->blue_prec
;
967 use_shift
= (visual
->type
== GDK_VISUAL_TRUE_COLOR
) || (visual
->type
== GDK_VISUAL_DIRECT_COLOR
);
975 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
978 for (int j
= 0; j
< GetHeight(); j
++)
980 for (int i
= 0; i
< GetWidth(); i
++)
982 wxUint32 pixel
= gdk_image_get_pixel( gdk_image
, i
, j
);
1000 data
[pos
] = (pixel
>> red_shift_right
) << red_shift_left
;
1001 data
[pos
+1] = (pixel
>> green_shift_right
) << green_shift_left
;
1002 data
[pos
+2] = (pixel
>> blue_shift_right
) << blue_shift_left
;
1004 else if (cmap
->colors
)
1006 data
[pos
] = cmap
->colors
[pixel
].red
>> 8;
1007 data
[pos
+1] = cmap
->colors
[pixel
].green
>> 8;
1008 data
[pos
+2] = cmap
->colors
[pixel
].blue
>> 8;
1012 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
1017 int mask_pixel
= gdk_image_get_pixel( gdk_image_mask
, i
, j
);
1018 if (mask_pixel
== 0)
1020 data
[pos
] = MASK_RED
;
1021 data
[pos
+1] = MASK_GREEN
;
1022 data
[pos
+2] = MASK_BLUE
;
1024 else if ( data
[pos
] == MASK_RED
&&
1025 data
[pos
+1] == MASK_GREEN
&&
1026 data
[pos
+2] == MASK_BLUE
)
1028 data
[pos
+2] = MASK_BLUE_REPLACEMENT
;
1036 gdk_image_destroy( gdk_image
);
1037 if (gdk_image_mask
) gdk_image_destroy( gdk_image_mask
);
1042 wxBitmap::wxBitmap( const wxString
&filename
, wxBitmapType type
)
1044 LoadFile( filename
, type
);
1047 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int WXUNUSED(depth
))
1049 if ( width
> 0 && height
> 0 )
1051 m_refData
= new wxBitmapRefData();
1053 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
1054 M_BMPDATA
->m_bitmap
= gdk_bitmap_create_from_data
1056 wxGetRootWindow()->window
,
1061 M_BMPDATA
->m_width
= width
;
1062 M_BMPDATA
->m_height
= height
;
1063 M_BMPDATA
->m_bpp
= 1;
1065 wxASSERT_MSG( M_BMPDATA
->m_bitmap
, wxT("couldn't create bitmap") );
1069 wxBitmap::~wxBitmap()
1073 bool wxBitmap::operator == ( const wxBitmap
& bmp
) const
1075 return m_refData
== bmp
.m_refData
;
1078 bool wxBitmap::operator != ( const wxBitmap
& bmp
) const
1080 return m_refData
!= bmp
.m_refData
;
1083 bool wxBitmap::Ok() const
1085 return (m_refData
!= NULL
) &&
1086 (M_BMPDATA
->m_bitmap
|| M_BMPDATA
->m_pixmap
);
1089 int wxBitmap::GetHeight() const
1091 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1093 return M_BMPDATA
->m_height
;
1096 int wxBitmap::GetWidth() const
1098 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1100 return M_BMPDATA
->m_width
;
1103 int wxBitmap::GetDepth() const
1105 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1107 return M_BMPDATA
->m_bpp
;
1110 wxMask
*wxBitmap::GetMask() const
1112 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
1114 return M_BMPDATA
->m_mask
;
1117 void wxBitmap::SetMask( wxMask
*mask
)
1119 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
1121 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
1123 M_BMPDATA
->m_mask
= mask
;
1126 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
1132 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
1134 wxCHECK_MSG( Ok() &&
1135 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1136 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) && (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
1137 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
1139 wxBitmap
ret( rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
1140 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
1142 if (ret
.GetPixmap())
1144 GdkGC
*gc
= gdk_gc_new( ret
.GetPixmap() );
1145 gdk_draw_pixmap( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1146 gdk_gc_destroy( gc
);
1150 GdkGC
*gc
= gdk_gc_new( ret
.GetBitmap() );
1152 col
.pixel
= 0xFFFFFF;
1153 gdk_gc_set_foreground( gc
, &col
);
1155 gdk_gc_set_background( gc
, &col
);
1156 gdk_wx_draw_bitmap( ret
.GetBitmap(), gc
, GetBitmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1157 gdk_gc_destroy( gc
);
1162 wxMask
*mask
= new wxMask
;
1163 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, rect
.width
, rect
.height
, 1 );
1165 GdkGC
*gc
= gdk_gc_new( mask
->m_bitmap
);
1167 col
.pixel
= 0xFFFFFF;
1168 gdk_gc_set_foreground( gc
, &col
);
1170 gdk_gc_set_background( gc
, &col
);
1171 gdk_wx_draw_bitmap( mask
->m_bitmap
, gc
, M_BMPDATA
->m_mask
->m_bitmap
, rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1172 gdk_gc_destroy( gc
);
1174 ret
.SetMask( mask
);
1180 bool wxBitmap::SaveFile( const wxString
&name
, wxBitmapType type
, const wxPalette
*WXUNUSED(palette
) ) const
1182 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1184 // Try to save the bitmap via wxImage handlers:
1186 wxImage image
= ConvertToImage();
1187 if (image
.Ok()) return image
.SaveFile( name
, type
);
1193 bool wxBitmap::LoadFile( const wxString
&name
, wxBitmapType type
)
1197 if (!wxFileExists(name
))
1200 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
1202 if (type
== wxBITMAP_TYPE_XPM
)
1204 m_refData
= new wxBitmapRefData();
1206 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
1208 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm
1210 wxGetRootWindow()->window
,
1218 M_BMPDATA
->m_mask
= new wxMask();
1219 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
1222 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
1224 M_BMPDATA
->m_bpp
= visual
->depth
;
1226 else // try if wxImage can load it
1229 if ( !image
.LoadFile( name
, type
) || !image
.Ok() )
1232 *this = wxBitmap(image
);
1239 wxPalette
*wxBitmap::GetPalette() const
1242 return (wxPalette
*) NULL
;
1244 return M_BMPDATA
->m_palette
;
1247 void wxBitmap::SetPalette(const wxPalette
& WXUNUSED(palette
))
1251 #endif // wxUSE_PALETTE
1253 void wxBitmap::SetHeight( int height
)
1256 m_refData
= new wxBitmapRefData();
1258 M_BMPDATA
->m_height
= height
;
1261 void wxBitmap::SetWidth( int width
)
1264 m_refData
= new wxBitmapRefData();
1266 M_BMPDATA
->m_width
= width
;
1269 void wxBitmap::SetDepth( int depth
)
1272 m_refData
= new wxBitmapRefData();
1274 M_BMPDATA
->m_bpp
= depth
;
1277 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
1280 m_refData
= new wxBitmapRefData();
1282 M_BMPDATA
->m_pixmap
= pixmap
;
1285 void wxBitmap::SetBitmap( GdkPixmap
*bitmap
)
1288 m_refData
= new wxBitmapRefData();
1290 M_BMPDATA
->m_bitmap
= bitmap
;
1293 GdkPixmap
*wxBitmap::GetPixmap() const
1295 wxCHECK_MSG( Ok(), (GdkPixmap
*) NULL
, wxT("invalid bitmap") );
1297 return M_BMPDATA
->m_pixmap
;
1300 bool wxBitmap::HasPixmap() const
1302 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1304 return M_BMPDATA
->m_pixmap
!= NULL
;
1307 GdkBitmap
*wxBitmap::GetBitmap() const
1309 wxCHECK_MSG( Ok(), (GdkBitmap
*) NULL
, wxT("invalid bitmap") );
1311 return M_BMPDATA
->m_bitmap
;
1314 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1319 void wxBitmap::UngetRawData(wxPixelDataBase
& WXUNUSED(data
))
1324 bool wxBitmap::HasAlpha() const
1329 void wxBitmap::UseAlpha()
1333 //-----------------------------------------------------------------------------
1335 //-----------------------------------------------------------------------------
1337 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
,wxBitmapHandlerBase
)
1339 wxBitmapHandler::~wxBitmapHandler()
1343 bool wxBitmapHandler::Create(wxBitmap
* WXUNUSED(bitmap
),
1344 void * WXUNUSED(data
),
1345 long WXUNUSED(type
),
1346 int WXUNUSED(width
),
1347 int WXUNUSED(height
),
1348 int WXUNUSED(depth
))
1350 wxFAIL_MSG( _T("not implemented") );
1355 bool wxBitmapHandler::LoadFile(wxBitmap
* WXUNUSED(bitmap
),
1356 const wxString
& WXUNUSED(name
),
1357 long WXUNUSED(flags
),
1358 int WXUNUSED(desiredWidth
),
1359 int WXUNUSED(desiredHeight
))
1361 wxFAIL_MSG( _T("not implemented") );
1366 bool wxBitmapHandler::SaveFile(const wxBitmap
* WXUNUSED(bitmap
),
1367 const wxString
& WXUNUSED(name
),
1369 const wxPalette
* WXUNUSED(palette
))
1371 wxFAIL_MSG( _T("not implemented") );
1376 /* static */ void wxBitmap::InitStandardHandlers()
1378 // TODO: Insert handler based on GdkPixbufs handler later