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"
19 #include "wx/palette.h"
21 #include "wx/filefn.h"
23 #include "wx/dcmemory.h"
29 #include <gdk/gdkrgb.h>
34 void gdk_wx_draw_bitmap (GdkDrawable
*drawable
,
44 //-----------------------------------------------------------------------------
46 //-----------------------------------------------------------------------------
48 extern GtkWidget
*wxGetRootWindow();
50 //-----------------------------------------------------------------------------
52 //-----------------------------------------------------------------------------
54 IMPLEMENT_DYNAMIC_CLASS(wxMask
,wxObject
)
58 m_bitmap
= (GdkBitmap
*) NULL
;
61 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
63 m_bitmap
= (GdkBitmap
*) NULL
;
64 Create( bitmap
, colour
);
68 wxMask::wxMask( const wxBitmap
& bitmap
, int paletteIndex
)
70 m_bitmap
= (GdkBitmap
*) NULL
;
71 Create( bitmap
, paletteIndex
);
73 #endif // wxUSE_PALETTE
75 wxMask::wxMask( const wxBitmap
& bitmap
)
77 m_bitmap
= (GdkBitmap
*) NULL
;
84 gdk_bitmap_unref( m_bitmap
);
87 bool wxMask::Create( const wxBitmap
& bitmap
,
88 const wxColour
& colour
)
92 gdk_bitmap_unref( m_bitmap
);
93 m_bitmap
= (GdkBitmap
*) NULL
;
96 wxImage image
= bitmap
.ConvertToImage();
97 if (!image
.Ok()) return false;
99 m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, image
.GetWidth(), image
.GetHeight(), 1 );
100 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
107 gdk_gc_set_foreground( gc
, &color
);
108 gdk_gc_set_fill( gc
, GDK_SOLID
);
109 gdk_draw_rectangle( m_bitmap
, gc
, TRUE
, 0, 0, image
.GetWidth(), image
.GetHeight() );
111 unsigned char *data
= image
.GetData();
114 unsigned char red
= colour
.Red();
115 unsigned char green
= colour
.Green();
116 unsigned char blue
= colour
.Blue();
118 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
120 int bpp
= visual
->depth
;
121 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800))
126 green
= green
& 0xf8;
132 green
= green
& 0xfc;
138 green
= green
& 0xf0;
146 gdk_gc_set_foreground( gc
, &color
);
148 for (int j
= 0; j
< image
.GetHeight(); j
++)
152 for (i
= 0; i
< image
.GetWidth(); i
++)
154 if ((data
[index
] == red
) &&
155 (data
[index
+1] == green
) &&
156 (data
[index
+2] == blue
))
165 gdk_draw_line( m_bitmap
, gc
, start_x
, j
, i
-1, j
);
172 gdk_draw_line( m_bitmap
, gc
, start_x
, j
, i
, j
);
181 bool wxMask::Create( const wxBitmap
& bitmap
, int paletteIndex
)
184 wxPalette
*pal
= bitmap
.GetPalette();
186 wxCHECK_MSG( pal
, false, wxT("Cannot create mask from bitmap without palette") );
188 pal
->GetRGB(paletteIndex
, &r
, &g
, &b
);
190 return Create(bitmap
, wxColour(r
, g
, b
));
192 #endif // wxUSE_PALETTE
194 bool wxMask::Create( const wxBitmap
& bitmap
)
198 gdk_bitmap_unref( m_bitmap
);
199 m_bitmap
= (GdkBitmap
*) NULL
;
202 if (!bitmap
.Ok()) return false;
204 wxCHECK_MSG( bitmap
.GetBitmap(), false, wxT("Cannot create mask from colour bitmap") );
206 m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, bitmap
.GetWidth(), bitmap
.GetHeight(), 1 );
208 if (!m_bitmap
) return false;
210 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
212 gdk_wx_draw_bitmap( m_bitmap
, gc
, bitmap
.GetBitmap(), 0, 0, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight() );
219 GdkBitmap
*wxMask::GetBitmap() const
224 //-----------------------------------------------------------------------------
226 //-----------------------------------------------------------------------------
228 class wxBitmapRefData
: public wxObjectRefData
240 wxPalette
*m_palette
;
243 wxBitmapRefData::wxBitmapRefData()
245 m_pixmap
= (GdkPixmap
*) NULL
;
246 m_bitmap
= (GdkBitmap
*) NULL
;
247 m_mask
= (wxMask
*) NULL
;
251 m_palette
= (wxPalette
*) NULL
;
254 wxBitmapRefData::~wxBitmapRefData()
257 gdk_pixmap_unref( m_pixmap
);
259 gdk_bitmap_unref( m_bitmap
);
263 #endif // wxUSE_PALETTE
266 //-----------------------------------------------------------------------------
268 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
270 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
276 wxBitmap::wxBitmap( int width
, int height
, int depth
)
278 Create( width
, height
, depth
);
281 bool wxBitmap::Create( int width
, int height
, int depth
)
285 if ( width
<= 0 || height
<= 0 )
290 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
293 depth
= visual
->depth
;
295 wxCHECK_MSG( (depth
== visual
->depth
) || (depth
== 1) || (depth
== 32), false,
296 wxT("invalid bitmap depth") );
298 m_refData
= new wxBitmapRefData();
299 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
300 M_BMPDATA
->m_width
= width
;
301 M_BMPDATA
->m_height
= height
;
304 M_BMPDATA
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
305 M_BMPDATA
->m_bpp
= 1;
309 M_BMPDATA
->m_pixmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, depth
);
310 M_BMPDATA
->m_bpp
= visual
->depth
;
316 bool wxBitmap::CreateFromXpm( const char **bits
)
320 wxCHECK_MSG( bits
!= NULL
, false, wxT("invalid bitmap data") );
322 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
324 m_refData
= new wxBitmapRefData();
326 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
328 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm_d( wxGetRootWindow()->window
, &mask
, NULL
, (gchar
**) bits
);
330 wxCHECK_MSG( M_BMPDATA
->m_pixmap
, false, wxT("couldn't create pixmap") );
334 M_BMPDATA
->m_mask
= new wxMask();
335 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
338 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
340 M_BMPDATA
->m_bpp
= visual
->depth
; // Can we get a different depth from create_from_xpm_d() ?
345 wxBitmap
wxBitmap::Rescale( int clipx
, int clipy
, int clipwidth
, int clipheight
, int newx
, int newy
)
347 wxCHECK_MSG( Ok(), wxNullBitmap
, wxT("invalid bitmap") );
349 if (newy
==M_BMPDATA
->m_width
&& newy
==M_BMPDATA
->m_height
)
352 int width
= wxMax(newx
, 1);
353 int height
= wxMax(newy
, 1);
354 width
= wxMin(width
, clipwidth
);
355 height
= wxMin(height
, clipheight
);
359 GdkImage
*img
= (GdkImage
*) NULL
;
361 img
= gdk_image_get( GetPixmap(), 0, 0, GetWidth(), GetHeight() );
362 else if (GetBitmap())
363 img
= gdk_image_get( GetBitmap(), 0, 0, GetWidth(), GetHeight() );
365 wxFAIL_MSG( wxT("Ill-formed bitmap") );
367 wxCHECK_MSG( img
, wxNullBitmap
, wxT("couldn't create image") );
373 GdkPixmap
*dstpix
= NULL
;
376 GdkVisual
*visual
= gdk_window_get_visual( GetPixmap() );
378 visual
= wxTheApp
->GetGdkVisual();
381 bmp
= wxBitmap(width
,height
,bpp
);
382 dstpix
= bmp
.GetPixmap();
383 gc
= gdk_gc_new( dstpix
);
387 long dstbyteperline
= 0;
392 dstbyteperline
= width
/8*M_BMPDATA
->m_bpp
;
393 if (width
*M_BMPDATA
->m_bpp
% 8 != 0)
395 dst
= (char*) malloc(dstbyteperline
*height
);
398 // be careful to use the right scaling factor
399 float scx
= (float)M_BMPDATA
->m_width
/(float)newx
;
400 float scy
= (float)M_BMPDATA
->m_height
/(float)newy
;
401 // prepare accel-tables
402 int *tablex
= (int *)calloc(width
,sizeof(int));
403 int *tabley
= (int *)calloc(height
,sizeof(int));
405 // accel table filled with clipped values
406 for (int x
= 0; x
< width
; x
++)
407 tablex
[x
] = (int) (scx
* (x
+clipx
));
408 for (int y
= 0; y
< height
; y
++)
409 tabley
[y
] = (int) (scy
* (y
+clipy
));
411 // Main rescaling routine starts here
412 for (int h
= 0; h
< height
; h
++)
416 guint32 old_pixval
= 0;
418 for (int w
= 0; w
< width
; w
++)
426 pixval
= gdk_image_get_pixel( img
, x
, tabley
[h
] );
436 char shift
= bit
<< (w
% 8);
442 dst
[h
*dstbyteperline
+w
/8] = outbyte
;
450 gdk_gc_set_foreground( gc
, &col
);
451 gdk_draw_point( dstpix
, gc
, w
, h
);
455 // do not forget the last byte
456 if ((bpp
== 1) && (width
% 8 != 0))
457 dst
[h
*dstbyteperline
+width
/8] = outbyte
;
460 gdk_image_destroy( img
);
461 if (gc
) gdk_gc_unref( gc
);
465 bmp
= wxBitmap( (const char *)dst
, width
, height
, 1 );
471 dstbyteperline
= width
/8;
474 dst
= (char*) malloc(dstbyteperline
*height
);
475 img
= gdk_image_get( GetMask()->GetBitmap(), 0, 0, GetWidth(), GetHeight() );
477 for (int h
= 0; h
< height
; h
++)
481 guint32 old_pixval
= 0;
483 for (int w
= 0; w
< width
; w
++)
491 pixval
= gdk_image_get_pixel( img
, x
, tabley
[h
] );
499 char shift
= bit
<< (w
% 8);
505 dst
[h
*dstbyteperline
+w
/8] = outbyte
;
510 // do not forget the last byte
512 dst
[h
*dstbyteperline
+width
/8] = outbyte
;
514 wxMask
* mask
= new wxMask
;
515 mask
->m_bitmap
= gdk_bitmap_create_from_data( wxGetRootWindow()->window
, (gchar
*) dst
, width
, height
);
519 gdk_image_destroy( img
);
528 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
532 wxCHECK_MSG( image
.Ok(), false, wxT("invalid image") );
533 wxCHECK_MSG( depth
== -1 || depth
== 1, false, wxT("invalid bitmap depth") );
535 if (image
.GetWidth() <= 0 || image
.GetHeight() <= 0)
538 m_refData
= new wxBitmapRefData();
542 return CreateFromImageAsBitmap(image
);
546 return CreateFromImageAsPixmap(image
);
550 // conversion to mono bitmap:
551 bool wxBitmap::CreateFromImageAsBitmap(const wxImage
& img
)
553 // convert alpha channel to mask, if it is present:
555 image
.ConvertAlphaToMask();
557 int width
= image
.GetWidth();
558 int height
= image
.GetHeight();
563 SetBitmap( gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 ) );
567 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
569 // Create picture image
571 unsigned char *data_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
573 GdkImage
*data_image
=
574 gdk_image_new_bitmap( visual
, data_data
, width
, height
);
578 GdkImage
*mask_image
= (GdkImage
*) NULL
;
582 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
584 mask_image
= gdk_image_new_bitmap( visual
, mask_data
, width
, height
);
586 wxMask
*mask
= new wxMask();
587 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
592 int r_mask
= image
.GetMaskRed();
593 int g_mask
= image
.GetMaskGreen();
594 int b_mask
= image
.GetMaskBlue();
596 unsigned char* data
= image
.GetData();
599 for (int y
= 0; y
< height
; y
++)
601 for (int x
= 0; x
< width
; x
++)
612 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
613 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
615 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
618 if ((r
== 255) && (b
== 255) && (g
== 255))
619 gdk_image_put_pixel( data_image
, x
, y
, 1 );
621 gdk_image_put_pixel( data_image
, x
, y
, 0 );
628 GdkGC
*data_gc
= gdk_gc_new( GetBitmap() );
630 gdk_draw_image( GetBitmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
632 gdk_image_destroy( data_image
);
633 gdk_gc_unref( data_gc
);
639 GdkGC
*mask_gc
= gdk_gc_new( GetMask()->GetBitmap() );
641 gdk_draw_image( GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
643 gdk_image_destroy( mask_image
);
644 gdk_gc_unref( mask_gc
);
650 // conversion to colour bitmap:
651 bool wxBitmap::CreateFromImageAsPixmap(const wxImage
& img
)
653 // convert alpha channel to mask, if it is present:
655 image
.ConvertAlphaToMask();
657 int width
= image
.GetWidth();
658 int height
= image
.GetHeight();
663 SetPixmap( gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, -1 ) );
665 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
667 int bpp
= visual
->depth
;
671 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800))
676 // We handle 8-bit bitmaps ourselves using the colour cube, 12-bit
677 // visuals are not supported by GDK so we do these ourselves, too.
678 // 15-bit and 16-bit should actually work and 24-bit certainly does.
680 if (!image
.HasMask() && (bpp
> 16))
682 if (!image
.HasMask() && (bpp
> 12))
685 static bool s_hasInitialized
= false;
687 if (!s_hasInitialized
)
690 s_hasInitialized
= true;
693 GdkGC
*gc
= gdk_gc_new( GetPixmap() );
695 gdk_draw_rgb_image( GetPixmap(),
707 // Create picture image
709 GdkImage
*data_image
=
710 gdk_image_new( GDK_IMAGE_FASTEST
, visual
, width
, height
);
714 GdkImage
*mask_image
= (GdkImage
*) NULL
;
718 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
720 mask_image
= gdk_image_new_bitmap( visual
, mask_data
, width
, height
);
722 wxMask
*mask
= new wxMask();
723 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
730 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
731 byte_order b_o
= RGB
;
735 if ((visual
->red_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->blue_mask
)) b_o
= RGB
;
736 else if ((visual
->red_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->green_mask
)) b_o
= RBG
;
737 else if ((visual
->blue_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->green_mask
)) b_o
= BRG
;
738 else if ((visual
->blue_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->red_mask
)) b_o
= BGR
;
739 else if ((visual
->green_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->blue_mask
)) b_o
= GRB
;
740 else if ((visual
->green_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->red_mask
)) b_o
= GBR
;
743 int r_mask
= image
.GetMaskRed();
744 int g_mask
= image
.GetMaskGreen();
745 int b_mask
= image
.GetMaskBlue();
747 unsigned char* data
= image
.GetData();
750 for (int y
= 0; y
< height
; y
++)
752 for (int x
= 0; x
< width
; x
++)
763 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
764 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
766 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
774 if (wxTheApp
->m_colorCube
)
776 pixel
= wxTheApp
->m_colorCube
[ ((r
& 0xf8) << 7) + ((g
& 0xf8) << 2) + ((b
& 0xf8) >> 3) ];
780 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
781 GdkColor
*colors
= cmap
->colors
;
782 int max
= 3 * (65536);
784 for (int i
= 0; i
< cmap
->size
; i
++)
786 int rdiff
= (r
<< 8) - colors
[i
].red
;
787 int gdiff
= (g
<< 8) - colors
[i
].green
;
788 int bdiff
= (b
<< 8) - colors
[i
].blue
;
789 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
790 if (sum
< max
) { pixel
= i
; max
= sum
; }
794 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
803 case RGB
: pixel
= ((r
& 0xf0) << 4) | (g
& 0xf0) | ((b
& 0xf0) >> 4); break;
804 case RBG
: pixel
= ((r
& 0xf0) << 4) | (b
& 0xf0) | ((g
& 0xf0) >> 4); break;
805 case GRB
: pixel
= ((g
& 0xf0) << 4) | (r
& 0xf0) | ((b
& 0xf0) >> 4); break;
806 case GBR
: pixel
= ((g
& 0xf0) << 4) | (b
& 0xf0) | ((r
& 0xf0) >> 4); break;
807 case BRG
: pixel
= ((b
& 0xf0) << 4) | (r
& 0xf0) | ((g
& 0xf0) >> 4); break;
808 case BGR
: pixel
= ((b
& 0xf0) << 4) | (g
& 0xf0) | ((r
& 0xf0) >> 4); break;
810 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
818 case RGB
: pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
819 case RBG
: pixel
= ((r
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
820 case GRB
: pixel
= ((g
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
821 case GBR
: pixel
= ((g
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
822 case BRG
: pixel
= ((b
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
823 case BGR
: pixel
= ((b
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
825 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
830 // I actually don't know if for 16-bit displays, it is alway the green
831 // component or the second component which has 6 bits.
835 case RGB
: pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
836 case RBG
: pixel
= ((r
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
837 case GRB
: pixel
= ((g
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
838 case GBR
: pixel
= ((g
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
839 case BRG
: pixel
= ((b
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
840 case BGR
: pixel
= ((b
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
842 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
851 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
852 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
853 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
854 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
855 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
856 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
858 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
868 GdkGC
*data_gc
= gdk_gc_new( GetPixmap() );
870 gdk_draw_image( GetPixmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
872 gdk_image_destroy( data_image
);
873 gdk_gc_unref( data_gc
);
879 GdkGC
*mask_gc
= gdk_gc_new( GetMask()->GetBitmap() );
881 gdk_draw_image( GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
883 gdk_image_destroy( mask_image
);
884 gdk_gc_unref( mask_gc
);
890 wxImage
wxBitmap::ConvertToImage() const
894 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
896 image
.Create(GetWidth(), GetHeight());
897 unsigned char *data
= image
.GetData();
901 wxFAIL_MSG( wxT("couldn't create image") );
905 // the colour used as transparent one in wxImage and the one it is
906 // replaced with when it really occurs in the bitmap
907 static const int MASK_RED
= 1;
908 static const int MASK_GREEN
= 2;
909 static const int MASK_BLUE
= 3;
910 static const int MASK_BLUE_REPLACEMENT
= 2;
912 GdkImage
*gdk_image
= (GdkImage
*) NULL
;
916 gdk_image
= gdk_image_get( GetPixmap(),
918 GetWidth(), GetHeight() );
920 else if (GetBitmap())
922 gdk_image
= gdk_image_get( GetBitmap(),
924 GetWidth(), GetHeight() );
928 wxFAIL_MSG( wxT("Ill-formed bitmap") );
931 wxCHECK_MSG( gdk_image
, wxNullImage
, wxT("couldn't create image") );
933 GdkImage
*gdk_image_mask
= (GdkImage
*) NULL
;
936 gdk_image_mask
= gdk_image_get( GetMask()->GetBitmap(),
938 GetWidth(), GetHeight() );
940 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
944 int red_shift_right
= 0;
945 int green_shift_right
= 0;
946 int blue_shift_right
= 0;
947 int red_shift_left
= 0;
948 int green_shift_left
= 0;
949 int blue_shift_left
= 0;
950 bool use_shift
= false;
954 GdkVisual
*visual
= gdk_window_get_visual( GetPixmap() );
956 visual
= wxTheApp
->GetGdkVisual();
960 bpp
= visual
->red_prec
+ visual
->green_prec
+ visual
->blue_prec
;
961 red_shift_right
= visual
->red_shift
;
962 red_shift_left
= 8-visual
->red_prec
;
963 green_shift_right
= visual
->green_shift
;
964 green_shift_left
= 8-visual
->green_prec
;
965 blue_shift_right
= visual
->blue_shift
;
966 blue_shift_left
= 8-visual
->blue_prec
;
968 use_shift
= (visual
->type
== GDK_VISUAL_TRUE_COLOR
) || (visual
->type
== GDK_VISUAL_DIRECT_COLOR
);
976 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
979 for (int j
= 0; j
< GetHeight(); j
++)
981 for (int i
= 0; i
< GetWidth(); i
++)
983 wxUint32 pixel
= gdk_image_get_pixel( gdk_image
, i
, j
);
1001 data
[pos
] = (pixel
>> red_shift_right
) << red_shift_left
;
1002 data
[pos
+1] = (pixel
>> green_shift_right
) << green_shift_left
;
1003 data
[pos
+2] = (pixel
>> blue_shift_right
) << blue_shift_left
;
1005 else if (cmap
->colors
)
1007 data
[pos
] = cmap
->colors
[pixel
].red
>> 8;
1008 data
[pos
+1] = cmap
->colors
[pixel
].green
>> 8;
1009 data
[pos
+2] = cmap
->colors
[pixel
].blue
>> 8;
1013 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
1018 int mask_pixel
= gdk_image_get_pixel( gdk_image_mask
, i
, j
);
1019 if (mask_pixel
== 0)
1021 data
[pos
] = MASK_RED
;
1022 data
[pos
+1] = MASK_GREEN
;
1023 data
[pos
+2] = MASK_BLUE
;
1025 else if ( data
[pos
] == MASK_RED
&&
1026 data
[pos
+1] == MASK_GREEN
&&
1027 data
[pos
+2] == MASK_BLUE
)
1029 data
[pos
+2] = MASK_BLUE_REPLACEMENT
;
1037 gdk_image_destroy( gdk_image
);
1038 if (gdk_image_mask
) gdk_image_destroy( gdk_image_mask
);
1043 wxBitmap::wxBitmap( const wxString
&filename
, wxBitmapType type
)
1045 LoadFile( filename
, type
);
1048 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int WXUNUSED(depth
))
1050 if ( width
> 0 && height
> 0 )
1052 m_refData
= new wxBitmapRefData();
1054 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
1055 M_BMPDATA
->m_bitmap
= gdk_bitmap_create_from_data
1057 wxGetRootWindow()->window
,
1062 M_BMPDATA
->m_width
= width
;
1063 M_BMPDATA
->m_height
= height
;
1064 M_BMPDATA
->m_bpp
= 1;
1066 wxASSERT_MSG( M_BMPDATA
->m_bitmap
, wxT("couldn't create bitmap") );
1070 wxBitmap::~wxBitmap()
1074 bool wxBitmap::operator == ( const wxBitmap
& bmp
) const
1076 return m_refData
== bmp
.m_refData
;
1079 bool wxBitmap::operator != ( const wxBitmap
& bmp
) const
1081 return m_refData
!= bmp
.m_refData
;
1084 bool wxBitmap::Ok() const
1086 return (m_refData
!= NULL
) &&
1087 (M_BMPDATA
->m_bitmap
|| M_BMPDATA
->m_pixmap
);
1090 int wxBitmap::GetHeight() const
1092 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1094 return M_BMPDATA
->m_height
;
1097 int wxBitmap::GetWidth() const
1099 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1101 return M_BMPDATA
->m_width
;
1104 int wxBitmap::GetDepth() const
1106 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1108 return M_BMPDATA
->m_bpp
;
1111 wxMask
*wxBitmap::GetMask() const
1113 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
1115 return M_BMPDATA
->m_mask
;
1118 void wxBitmap::SetMask( wxMask
*mask
)
1120 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
1122 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
1124 M_BMPDATA
->m_mask
= mask
;
1127 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
1133 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
1135 wxCHECK_MSG( Ok() &&
1136 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1137 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) && (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
1138 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
1140 wxBitmap
ret( rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
1141 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
1143 if (ret
.GetPixmap())
1145 GdkGC
*gc
= gdk_gc_new( ret
.GetPixmap() );
1146 gdk_draw_pixmap( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1147 gdk_gc_destroy( gc
);
1151 GdkGC
*gc
= gdk_gc_new( ret
.GetBitmap() );
1153 col
.pixel
= 0xFFFFFF;
1154 gdk_gc_set_foreground( gc
, &col
);
1156 gdk_gc_set_background( gc
, &col
);
1157 gdk_wx_draw_bitmap( ret
.GetBitmap(), gc
, GetBitmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1158 gdk_gc_destroy( gc
);
1163 wxMask
*mask
= new wxMask
;
1164 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, rect
.width
, rect
.height
, 1 );
1166 GdkGC
*gc
= gdk_gc_new( mask
->m_bitmap
);
1168 col
.pixel
= 0xFFFFFF;
1169 gdk_gc_set_foreground( gc
, &col
);
1171 gdk_gc_set_background( gc
, &col
);
1172 gdk_wx_draw_bitmap( mask
->m_bitmap
, gc
, M_BMPDATA
->m_mask
->m_bitmap
, rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1173 gdk_gc_destroy( gc
);
1175 ret
.SetMask( mask
);
1181 bool wxBitmap::SaveFile( const wxString
&name
, wxBitmapType type
, const wxPalette
*WXUNUSED(palette
) ) const
1183 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1185 // Try to save the bitmap via wxImage handlers:
1187 wxImage image
= ConvertToImage();
1188 if (image
.Ok()) return image
.SaveFile( name
, type
);
1194 bool wxBitmap::LoadFile( const wxString
&name
, wxBitmapType type
)
1198 if (!wxFileExists(name
))
1201 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
1203 if (type
== wxBITMAP_TYPE_XPM
)
1205 m_refData
= new wxBitmapRefData();
1207 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
1209 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm
1211 wxGetRootWindow()->window
,
1219 M_BMPDATA
->m_mask
= new wxMask();
1220 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
1223 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
1225 M_BMPDATA
->m_bpp
= visual
->depth
;
1227 else // try if wxImage can load it
1230 if ( !image
.LoadFile( name
, type
) || !image
.Ok() )
1233 *this = wxBitmap(image
);
1240 wxPalette
*wxBitmap::GetPalette() const
1243 return (wxPalette
*) NULL
;
1245 return M_BMPDATA
->m_palette
;
1248 void wxBitmap::SetPalette(const wxPalette
& WXUNUSED(palette
))
1252 #endif // wxUSE_PALETTE
1254 void wxBitmap::SetHeight( int height
)
1257 m_refData
= new wxBitmapRefData();
1259 M_BMPDATA
->m_height
= height
;
1262 void wxBitmap::SetWidth( int width
)
1265 m_refData
= new wxBitmapRefData();
1267 M_BMPDATA
->m_width
= width
;
1270 void wxBitmap::SetDepth( int depth
)
1273 m_refData
= new wxBitmapRefData();
1275 M_BMPDATA
->m_bpp
= depth
;
1278 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
1281 m_refData
= new wxBitmapRefData();
1283 M_BMPDATA
->m_pixmap
= pixmap
;
1286 void wxBitmap::SetBitmap( GdkPixmap
*bitmap
)
1289 m_refData
= new wxBitmapRefData();
1291 M_BMPDATA
->m_bitmap
= bitmap
;
1294 GdkPixmap
*wxBitmap::GetPixmap() const
1296 wxCHECK_MSG( Ok(), (GdkPixmap
*) NULL
, wxT("invalid bitmap") );
1298 return M_BMPDATA
->m_pixmap
;
1301 bool wxBitmap::HasPixmap() const
1303 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1305 return M_BMPDATA
->m_pixmap
!= NULL
;
1308 GdkBitmap
*wxBitmap::GetBitmap() const
1310 wxCHECK_MSG( Ok(), (GdkBitmap
*) NULL
, wxT("invalid bitmap") );
1312 return M_BMPDATA
->m_bitmap
;
1315 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1320 void wxBitmap::UngetRawData(wxPixelDataBase
& WXUNUSED(data
))
1325 bool wxBitmap::HasAlpha() const
1330 void wxBitmap::UseAlpha()
1334 //-----------------------------------------------------------------------------
1336 //-----------------------------------------------------------------------------
1338 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
,wxBitmapHandlerBase
)
1340 wxBitmapHandler::~wxBitmapHandler()
1344 bool wxBitmapHandler::Create(wxBitmap
* WXUNUSED(bitmap
),
1345 void * WXUNUSED(data
),
1346 long WXUNUSED(type
),
1347 int WXUNUSED(width
),
1348 int WXUNUSED(height
),
1349 int WXUNUSED(depth
))
1351 wxFAIL_MSG( _T("not implemented") );
1356 bool wxBitmapHandler::LoadFile(wxBitmap
* WXUNUSED(bitmap
),
1357 const wxString
& WXUNUSED(name
),
1358 long WXUNUSED(flags
),
1359 int WXUNUSED(desiredWidth
),
1360 int WXUNUSED(desiredHeight
))
1362 wxFAIL_MSG( _T("not implemented") );
1367 bool wxBitmapHandler::SaveFile(const wxBitmap
* WXUNUSED(bitmap
),
1368 const wxString
& WXUNUSED(name
),
1370 const wxPalette
* WXUNUSED(palette
))
1372 wxFAIL_MSG( _T("not implemented") );
1377 /* static */ void wxBitmap::InitStandardHandlers()
1379 // TODO: Insert handler based on GdkPixbufs handler later