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"
15 #include "wx/bitmap.h"
16 #include "wx/palette.h"
18 #include "wx/filefn.h"
20 #include "wx/dcmemory.h"
27 #include <gdk/gdkrgb.h>
32 void gdk_wx_draw_bitmap (GdkDrawable
*drawable
,
42 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
46 extern GtkWidget
*wxGetRootWindow();
48 //-----------------------------------------------------------------------------
50 //-----------------------------------------------------------------------------
52 IMPLEMENT_DYNAMIC_CLASS(wxMask
,wxObject
)
56 m_bitmap
= (GdkBitmap
*) NULL
;
59 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
61 m_bitmap
= (GdkBitmap
*) NULL
;
62 Create( bitmap
, colour
);
66 wxMask::wxMask( const wxBitmap
& bitmap
, int paletteIndex
)
68 m_bitmap
= (GdkBitmap
*) NULL
;
69 Create( bitmap
, paletteIndex
);
71 #endif // wxUSE_PALETTE
73 wxMask::wxMask( const wxBitmap
& bitmap
)
75 m_bitmap
= (GdkBitmap
*) NULL
;
82 gdk_bitmap_unref( m_bitmap
);
85 bool wxMask::Create( const wxBitmap
& bitmap
,
86 const wxColour
& colour
)
90 gdk_bitmap_unref( m_bitmap
);
91 m_bitmap
= (GdkBitmap
*) NULL
;
94 wxImage image
= bitmap
.ConvertToImage();
95 if (!image
.Ok()) return false;
97 m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, image
.GetWidth(), image
.GetHeight(), 1 );
98 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
105 gdk_gc_set_foreground( gc
, &color
);
106 gdk_gc_set_fill( gc
, GDK_SOLID
);
107 gdk_draw_rectangle( m_bitmap
, gc
, TRUE
, 0, 0, image
.GetWidth(), image
.GetHeight() );
109 unsigned char *data
= image
.GetData();
112 unsigned char red
= colour
.Red();
113 unsigned char green
= colour
.Green();
114 unsigned char blue
= colour
.Blue();
116 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
118 int bpp
= visual
->depth
;
119 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800))
124 green
= green
& 0xf8;
130 green
= green
& 0xfc;
136 green
= green
& 0xf0;
144 gdk_gc_set_foreground( gc
, &color
);
146 for (int j
= 0; j
< image
.GetHeight(); j
++)
150 for (i
= 0; i
< image
.GetWidth(); i
++)
152 if ((data
[index
] == red
) &&
153 (data
[index
+1] == green
) &&
154 (data
[index
+2] == blue
))
163 gdk_draw_line( m_bitmap
, gc
, start_x
, j
, i
-1, j
);
170 gdk_draw_line( m_bitmap
, gc
, start_x
, j
, i
, j
);
179 bool wxMask::Create( const wxBitmap
& bitmap
, int paletteIndex
)
182 wxPalette
*pal
= bitmap
.GetPalette();
184 wxCHECK_MSG( pal
, false, wxT("Cannot create mask from bitmap without palette") );
186 pal
->GetRGB(paletteIndex
, &r
, &g
, &b
);
188 return Create(bitmap
, wxColour(r
, g
, b
));
190 #endif // wxUSE_PALETTE
192 bool wxMask::Create( const wxBitmap
& bitmap
)
196 gdk_bitmap_unref( m_bitmap
);
197 m_bitmap
= (GdkBitmap
*) NULL
;
200 if (!bitmap
.Ok()) return false;
202 wxCHECK_MSG( bitmap
.GetBitmap(), false, wxT("Cannot create mask from colour bitmap") );
204 m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, bitmap
.GetWidth(), bitmap
.GetHeight(), 1 );
206 if (!m_bitmap
) return false;
208 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
210 gdk_wx_draw_bitmap( m_bitmap
, gc
, bitmap
.GetBitmap(), 0, 0, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight() );
217 GdkBitmap
*wxMask::GetBitmap() const
222 //-----------------------------------------------------------------------------
224 //-----------------------------------------------------------------------------
226 class wxBitmapRefData
: public wxObjectRefData
238 wxPalette
*m_palette
;
241 wxBitmapRefData::wxBitmapRefData()
243 m_pixmap
= (GdkPixmap
*) NULL
;
244 m_bitmap
= (GdkBitmap
*) NULL
;
245 m_mask
= (wxMask
*) NULL
;
249 m_palette
= (wxPalette
*) NULL
;
252 wxBitmapRefData::~wxBitmapRefData()
255 gdk_pixmap_unref( m_pixmap
);
257 gdk_bitmap_unref( m_bitmap
);
261 #endif // wxUSE_PALETTE
264 //-----------------------------------------------------------------------------
266 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
268 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
274 wxBitmap::wxBitmap( int width
, int height
, int depth
)
276 Create( width
, height
, depth
);
279 bool wxBitmap::Create( int width
, int height
, int depth
)
283 if ( width
<= 0 || height
<= 0 )
288 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
291 depth
= visual
->depth
;
293 wxCHECK_MSG( (depth
== visual
->depth
) || (depth
== 1) || (depth
== 32), false,
294 wxT("invalid bitmap depth") )
296 m_refData
= new wxBitmapRefData();
297 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
298 M_BMPDATA
->m_width
= width
;
299 M_BMPDATA
->m_height
= height
;
302 M_BMPDATA
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
303 M_BMPDATA
->m_bpp
= 1;
307 M_BMPDATA
->m_pixmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, depth
);
308 M_BMPDATA
->m_bpp
= visual
->depth
;
314 bool wxBitmap::CreateFromXpm( const char **bits
)
318 wxCHECK_MSG( bits
!= NULL
, false, wxT("invalid bitmap data") )
320 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
322 m_refData
= new wxBitmapRefData();
324 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
326 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm_d( wxGetRootWindow()->window
, &mask
, NULL
, (gchar
**) bits
);
328 wxCHECK_MSG( M_BMPDATA
->m_pixmap
, false, wxT("couldn't create pixmap") );
332 M_BMPDATA
->m_mask
= new wxMask();
333 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
336 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
338 M_BMPDATA
->m_bpp
= visual
->depth
; // Can we get a different depth from create_from_xpm_d() ?
343 wxBitmap
wxBitmap::Rescale( int clipx
, int clipy
, int clipwidth
, int clipheight
, int newx
, int newy
)
345 wxCHECK_MSG( Ok(), wxNullBitmap
, wxT("invalid bitmap") );
347 if (newy
==M_BMPDATA
->m_width
&& newy
==M_BMPDATA
->m_height
)
350 int width
= wxMax(newx
, 1);
351 int height
= wxMax(newy
, 1);
352 width
= wxMin(width
, clipwidth
);
353 height
= wxMin(height
, clipheight
);
357 GdkImage
*img
= (GdkImage
*) NULL
;
359 img
= gdk_image_get( GetPixmap(), 0, 0, GetWidth(), GetHeight() );
360 else if (GetBitmap())
361 img
= gdk_image_get( GetBitmap(), 0, 0, GetWidth(), GetHeight() );
363 wxFAIL_MSG( wxT("Ill-formed bitmap") );
365 wxCHECK_MSG( img
, wxNullBitmap
, wxT("couldn't create image") );
371 GdkPixmap
*dstpix
= NULL
;
374 GdkVisual
*visual
= gdk_window_get_visual( GetPixmap() );
376 visual
= wxTheApp
->GetGdkVisual();
379 bmp
= wxBitmap(width
,height
,bpp
);
380 dstpix
= bmp
.GetPixmap();
381 gc
= gdk_gc_new( dstpix
);
385 long dstbyteperline
= 0;
390 dstbyteperline
= width
/8*M_BMPDATA
->m_bpp
;
391 if (width
*M_BMPDATA
->m_bpp
% 8 != 0)
393 dst
= (char*) malloc(dstbyteperline
*height
);
396 // be careful to use the right scaling factor
397 float scx
= (float)M_BMPDATA
->m_width
/(float)newx
;
398 float scy
= (float)M_BMPDATA
->m_height
/(float)newy
;
399 // prepare accel-tables
400 int *tablex
= (int *)calloc(width
,sizeof(int));
401 int *tabley
= (int *)calloc(height
,sizeof(int));
403 // accel table filled with clipped values
404 for (int x
= 0; x
< width
; x
++)
405 tablex
[x
] = (int) (scx
* (x
+clipx
));
406 for (int y
= 0; y
< height
; y
++)
407 tabley
[y
] = (int) (scy
* (y
+clipy
));
409 // Main rescaling routine starts here
410 for (int h
= 0; h
< height
; h
++)
414 guint32 old_pixval
= 0;
416 for (int w
= 0; w
< width
; w
++)
424 pixval
= gdk_image_get_pixel( img
, x
, tabley
[h
] );
434 char shift
= bit
<< (w
% 8);
440 dst
[h
*dstbyteperline
+w
/8] = outbyte
;
448 gdk_gc_set_foreground( gc
, &col
);
449 gdk_draw_point( dstpix
, gc
, w
, h
);
453 // do not forget the last byte
454 if ((bpp
== 1) && (width
% 8 != 0))
455 dst
[h
*dstbyteperline
+width
/8] = outbyte
;
458 gdk_image_destroy( img
);
459 if (gc
) gdk_gc_unref( gc
);
463 bmp
= wxBitmap( (const char *)dst
, width
, height
, 1 );
469 dstbyteperline
= width
/8;
472 dst
= (char*) malloc(dstbyteperline
*height
);
473 img
= gdk_image_get( GetMask()->GetBitmap(), 0, 0, GetWidth(), GetHeight() );
475 for (int h
= 0; h
< height
; h
++)
479 guint32 old_pixval
= 0;
481 for (int w
= 0; w
< width
; w
++)
489 pixval
= gdk_image_get_pixel( img
, x
, tabley
[h
] );
497 char shift
= bit
<< (w
% 8);
503 dst
[h
*dstbyteperline
+w
/8] = outbyte
;
508 // do not forget the last byte
510 dst
[h
*dstbyteperline
+width
/8] = outbyte
;
512 wxMask
* mask
= new wxMask
;
513 mask
->m_bitmap
= gdk_bitmap_create_from_data( wxGetRootWindow()->window
, (gchar
*) dst
, width
, height
);
517 gdk_image_destroy( img
);
526 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
530 wxCHECK_MSG( image
.Ok(), false, wxT("invalid image") )
531 wxCHECK_MSG( depth
== -1 || depth
== 1, false, wxT("invalid bitmap depth") )
533 if (image
.GetWidth() <= 0 || image
.GetHeight() <= 0)
536 m_refData
= new wxBitmapRefData();
540 return CreateFromImageAsBitmap(image
);
544 return CreateFromImageAsPixmap(image
);
548 // conversion to mono bitmap:
549 bool wxBitmap::CreateFromImageAsBitmap(const wxImage
& img
)
551 // convert alpha channel to mask, if it is present:
553 image
.ConvertAlphaToMask();
555 int width
= image
.GetWidth();
556 int height
= image
.GetHeight();
561 SetBitmap( gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 ) );
565 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
567 // Create picture image
569 unsigned char *data_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
571 GdkImage
*data_image
=
572 gdk_image_new_bitmap( visual
, data_data
, width
, height
);
576 GdkImage
*mask_image
= (GdkImage
*) NULL
;
580 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
582 mask_image
= gdk_image_new_bitmap( visual
, mask_data
, width
, height
);
584 wxMask
*mask
= new wxMask();
585 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
590 int r_mask
= image
.GetMaskRed();
591 int g_mask
= image
.GetMaskGreen();
592 int b_mask
= image
.GetMaskBlue();
594 unsigned char* data
= image
.GetData();
597 for (int y
= 0; y
< height
; y
++)
599 for (int x
= 0; x
< width
; x
++)
610 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
611 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
613 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
616 if ((r
== 255) && (b
== 255) && (g
== 255))
617 gdk_image_put_pixel( data_image
, x
, y
, 1 );
619 gdk_image_put_pixel( data_image
, x
, y
, 0 );
626 GdkGC
*data_gc
= gdk_gc_new( GetBitmap() );
628 gdk_draw_image( GetBitmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
630 gdk_image_destroy( data_image
);
631 gdk_gc_unref( data_gc
);
637 GdkGC
*mask_gc
= gdk_gc_new( GetMask()->GetBitmap() );
639 gdk_draw_image( GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
641 gdk_image_destroy( mask_image
);
642 gdk_gc_unref( mask_gc
);
648 // conversion to colour bitmap:
649 bool wxBitmap::CreateFromImageAsPixmap(const wxImage
& img
)
651 // convert alpha channel to mask, if it is present:
653 image
.ConvertAlphaToMask();
655 int width
= image
.GetWidth();
656 int height
= image
.GetHeight();
661 SetPixmap( gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, -1 ) );
663 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
665 int bpp
= visual
->depth
;
669 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800))
674 // We handle 8-bit bitmaps ourselves using the colour cube, 12-bit
675 // visuals are not supported by GDK so we do these ourselves, too.
676 // 15-bit and 16-bit should actually work and 24-bit certainly does.
678 if (!image
.HasMask() && (bpp
> 16))
680 if (!image
.HasMask() && (bpp
> 12))
683 static bool s_hasInitialized
= false;
685 if (!s_hasInitialized
)
688 s_hasInitialized
= true;
691 GdkGC
*gc
= gdk_gc_new( GetPixmap() );
693 gdk_draw_rgb_image( GetPixmap(),
705 // Create picture image
707 GdkImage
*data_image
=
708 gdk_image_new( GDK_IMAGE_FASTEST
, visual
, width
, height
);
712 GdkImage
*mask_image
= (GdkImage
*) NULL
;
716 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
718 mask_image
= gdk_image_new_bitmap( visual
, mask_data
, width
, height
);
720 wxMask
*mask
= new wxMask();
721 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
728 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
729 byte_order b_o
= RGB
;
733 if ((visual
->red_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->blue_mask
)) b_o
= RGB
;
734 else if ((visual
->red_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->green_mask
)) b_o
= RBG
;
735 else if ((visual
->blue_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->green_mask
)) b_o
= BRG
;
736 else if ((visual
->blue_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->red_mask
)) b_o
= BGR
;
737 else if ((visual
->green_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->blue_mask
)) b_o
= GRB
;
738 else if ((visual
->green_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->red_mask
)) b_o
= GBR
;
741 int r_mask
= image
.GetMaskRed();
742 int g_mask
= image
.GetMaskGreen();
743 int b_mask
= image
.GetMaskBlue();
745 unsigned char* data
= image
.GetData();
748 for (int y
= 0; y
< height
; y
++)
750 for (int x
= 0; x
< width
; x
++)
761 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
762 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
764 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
772 if (wxTheApp
->m_colorCube
)
774 pixel
= wxTheApp
->m_colorCube
[ ((r
& 0xf8) << 7) + ((g
& 0xf8) << 2) + ((b
& 0xf8) >> 3) ];
778 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
779 GdkColor
*colors
= cmap
->colors
;
780 int max
= 3 * (65536);
782 for (int i
= 0; i
< cmap
->size
; i
++)
784 int rdiff
= (r
<< 8) - colors
[i
].red
;
785 int gdiff
= (g
<< 8) - colors
[i
].green
;
786 int bdiff
= (b
<< 8) - colors
[i
].blue
;
787 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
788 if (sum
< max
) { pixel
= i
; max
= sum
; }
792 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
801 case RGB
: pixel
= ((r
& 0xf0) << 4) | (g
& 0xf0) | ((b
& 0xf0) >> 4); break;
802 case RBG
: pixel
= ((r
& 0xf0) << 4) | (b
& 0xf0) | ((g
& 0xf0) >> 4); break;
803 case GRB
: pixel
= ((g
& 0xf0) << 4) | (r
& 0xf0) | ((b
& 0xf0) >> 4); break;
804 case GBR
: pixel
= ((g
& 0xf0) << 4) | (b
& 0xf0) | ((r
& 0xf0) >> 4); break;
805 case BRG
: pixel
= ((b
& 0xf0) << 4) | (r
& 0xf0) | ((g
& 0xf0) >> 4); break;
806 case BGR
: pixel
= ((b
& 0xf0) << 4) | (g
& 0xf0) | ((r
& 0xf0) >> 4); break;
808 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
816 case RGB
: pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
817 case RBG
: pixel
= ((r
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
818 case GRB
: pixel
= ((g
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
819 case GBR
: pixel
= ((g
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
820 case BRG
: pixel
= ((b
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
821 case BGR
: pixel
= ((b
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
823 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
828 // I actually don't know if for 16-bit displays, it is alway the green
829 // component or the second component which has 6 bits.
833 case RGB
: pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
834 case RBG
: pixel
= ((r
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
835 case GRB
: pixel
= ((g
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
836 case GBR
: pixel
= ((g
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
837 case BRG
: pixel
= ((b
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
838 case BGR
: pixel
= ((b
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
840 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
849 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
850 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
851 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
852 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
853 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
854 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
856 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
866 GdkGC
*data_gc
= gdk_gc_new( GetPixmap() );
868 gdk_draw_image( GetPixmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
870 gdk_image_destroy( data_image
);
871 gdk_gc_unref( data_gc
);
877 GdkGC
*mask_gc
= gdk_gc_new( GetMask()->GetBitmap() );
879 gdk_draw_image( GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
881 gdk_image_destroy( mask_image
);
882 gdk_gc_unref( mask_gc
);
888 wxImage
wxBitmap::ConvertToImage() const
892 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
894 image
.Create(GetWidth(), GetHeight());
895 unsigned char *data
= image
.GetData();
899 wxFAIL_MSG( wxT("couldn't create image") );
903 // the colour used as transparent one in wxImage and the one it is
904 // replaced with when it really occurs in the bitmap
905 static const int MASK_RED
= 1;
906 static const int MASK_GREEN
= 2;
907 static const int MASK_BLUE
= 3;
908 static const int MASK_BLUE_REPLACEMENT
= 2;
910 GdkImage
*gdk_image
= (GdkImage
*) NULL
;
914 gdk_image
= gdk_image_get( GetPixmap(),
916 GetWidth(), GetHeight() );
918 else if (GetBitmap())
920 gdk_image
= gdk_image_get( GetBitmap(),
922 GetWidth(), GetHeight() );
926 wxFAIL_MSG( wxT("Ill-formed bitmap") );
929 wxCHECK_MSG( gdk_image
, wxNullImage
, wxT("couldn't create image") );
931 GdkImage
*gdk_image_mask
= (GdkImage
*) NULL
;
934 gdk_image_mask
= gdk_image_get( GetMask()->GetBitmap(),
936 GetWidth(), GetHeight() );
938 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
942 int red_shift_right
= 0;
943 int green_shift_right
= 0;
944 int blue_shift_right
= 0;
945 int red_shift_left
= 0;
946 int green_shift_left
= 0;
947 int blue_shift_left
= 0;
948 bool use_shift
= false;
952 GdkVisual
*visual
= gdk_window_get_visual( GetPixmap() );
954 visual
= wxTheApp
->GetGdkVisual();
958 bpp
= visual
->red_prec
+ visual
->green_prec
+ visual
->blue_prec
;
959 red_shift_right
= visual
->red_shift
;
960 red_shift_left
= 8-visual
->red_prec
;
961 green_shift_right
= visual
->green_shift
;
962 green_shift_left
= 8-visual
->green_prec
;
963 blue_shift_right
= visual
->blue_shift
;
964 blue_shift_left
= 8-visual
->blue_prec
;
966 use_shift
= (visual
->type
== GDK_VISUAL_TRUE_COLOR
) || (visual
->type
== GDK_VISUAL_DIRECT_COLOR
);
974 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
977 for (int j
= 0; j
< GetHeight(); j
++)
979 for (int i
= 0; i
< GetWidth(); i
++)
981 wxUint32 pixel
= gdk_image_get_pixel( gdk_image
, i
, j
);
999 data
[pos
] = (pixel
>> red_shift_right
) << red_shift_left
;
1000 data
[pos
+1] = (pixel
>> green_shift_right
) << green_shift_left
;
1001 data
[pos
+2] = (pixel
>> blue_shift_right
) << blue_shift_left
;
1003 else if (cmap
->colors
)
1005 data
[pos
] = cmap
->colors
[pixel
].red
>> 8;
1006 data
[pos
+1] = cmap
->colors
[pixel
].green
>> 8;
1007 data
[pos
+2] = cmap
->colors
[pixel
].blue
>> 8;
1011 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
1016 int mask_pixel
= gdk_image_get_pixel( gdk_image_mask
, i
, j
);
1017 if (mask_pixel
== 0)
1019 data
[pos
] = MASK_RED
;
1020 data
[pos
+1] = MASK_GREEN
;
1021 data
[pos
+2] = MASK_BLUE
;
1023 else if ( data
[pos
] == MASK_RED
&&
1024 data
[pos
+1] == MASK_GREEN
&&
1025 data
[pos
+2] == MASK_BLUE
)
1027 data
[pos
+2] = MASK_BLUE_REPLACEMENT
;
1035 gdk_image_destroy( gdk_image
);
1036 if (gdk_image_mask
) gdk_image_destroy( gdk_image_mask
);
1041 wxBitmap::wxBitmap( const wxString
&filename
, wxBitmapType type
)
1043 LoadFile( filename
, type
);
1046 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int WXUNUSED(depth
))
1048 if ( width
> 0 && height
> 0 )
1050 m_refData
= new wxBitmapRefData();
1052 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
1053 M_BMPDATA
->m_bitmap
= gdk_bitmap_create_from_data
1055 wxGetRootWindow()->window
,
1060 M_BMPDATA
->m_width
= width
;
1061 M_BMPDATA
->m_height
= height
;
1062 M_BMPDATA
->m_bpp
= 1;
1064 wxASSERT_MSG( M_BMPDATA
->m_bitmap
, wxT("couldn't create bitmap") );
1068 wxBitmap::~wxBitmap()
1072 bool wxBitmap::operator == ( const wxBitmap
& bmp
) const
1074 return m_refData
== bmp
.m_refData
;
1077 bool wxBitmap::operator != ( const wxBitmap
& bmp
) const
1079 return m_refData
!= bmp
.m_refData
;
1082 bool wxBitmap::Ok() const
1084 return (m_refData
!= NULL
) &&
1085 (M_BMPDATA
->m_bitmap
|| M_BMPDATA
->m_pixmap
);
1088 int wxBitmap::GetHeight() const
1090 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1092 return M_BMPDATA
->m_height
;
1095 int wxBitmap::GetWidth() const
1097 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1099 return M_BMPDATA
->m_width
;
1102 int wxBitmap::GetDepth() const
1104 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1106 return M_BMPDATA
->m_bpp
;
1109 wxMask
*wxBitmap::GetMask() const
1111 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
1113 return M_BMPDATA
->m_mask
;
1116 void wxBitmap::SetMask( wxMask
*mask
)
1118 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
1120 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
1122 M_BMPDATA
->m_mask
= mask
;
1125 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
1131 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
1133 wxCHECK_MSG( Ok() &&
1134 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1135 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) && (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
1136 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
1138 wxBitmap
ret( rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
1139 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
1141 if (ret
.GetPixmap())
1143 GdkGC
*gc
= gdk_gc_new( ret
.GetPixmap() );
1144 gdk_draw_pixmap( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1145 gdk_gc_destroy( gc
);
1149 GdkGC
*gc
= gdk_gc_new( ret
.GetBitmap() );
1151 col
.pixel
= 0xFFFFFF;
1152 gdk_gc_set_foreground( gc
, &col
);
1154 gdk_gc_set_background( gc
, &col
);
1155 gdk_wx_draw_bitmap( ret
.GetBitmap(), gc
, GetBitmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1156 gdk_gc_destroy( gc
);
1161 wxMask
*mask
= new wxMask
;
1162 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, rect
.width
, rect
.height
, 1 );
1164 GdkGC
*gc
= gdk_gc_new( mask
->m_bitmap
);
1166 col
.pixel
= 0xFFFFFF;
1167 gdk_gc_set_foreground( gc
, &col
);
1169 gdk_gc_set_background( gc
, &col
);
1170 gdk_wx_draw_bitmap( mask
->m_bitmap
, gc
, M_BMPDATA
->m_mask
->m_bitmap
, rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1171 gdk_gc_destroy( gc
);
1173 ret
.SetMask( mask
);
1179 bool wxBitmap::SaveFile( const wxString
&name
, wxBitmapType type
, const wxPalette
*WXUNUSED(palette
) ) const
1181 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1183 // Try to save the bitmap via wxImage handlers:
1185 wxImage image
= ConvertToImage();
1186 if (image
.Ok()) return image
.SaveFile( name
, type
);
1192 bool wxBitmap::LoadFile( const wxString
&name
, wxBitmapType type
)
1196 if (!wxFileExists(name
))
1199 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
1201 if (type
== wxBITMAP_TYPE_XPM
)
1203 m_refData
= new wxBitmapRefData();
1205 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
1207 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm
1209 wxGetRootWindow()->window
,
1217 M_BMPDATA
->m_mask
= new wxMask();
1218 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
1221 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
1223 M_BMPDATA
->m_bpp
= visual
->depth
;
1225 else // try if wxImage can load it
1228 if ( !image
.LoadFile( name
, type
) || !image
.Ok() )
1231 *this = wxBitmap(image
);
1238 wxPalette
*wxBitmap::GetPalette() const
1241 return (wxPalette
*) NULL
;
1243 return M_BMPDATA
->m_palette
;
1246 void wxBitmap::SetPalette(const wxPalette
& WXUNUSED(palette
))
1250 #endif // wxUSE_PALETTE
1252 void wxBitmap::SetHeight( int height
)
1255 m_refData
= new wxBitmapRefData();
1257 M_BMPDATA
->m_height
= height
;
1260 void wxBitmap::SetWidth( int width
)
1263 m_refData
= new wxBitmapRefData();
1265 M_BMPDATA
->m_width
= width
;
1268 void wxBitmap::SetDepth( int depth
)
1271 m_refData
= new wxBitmapRefData();
1273 M_BMPDATA
->m_bpp
= depth
;
1276 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
1279 m_refData
= new wxBitmapRefData();
1281 M_BMPDATA
->m_pixmap
= pixmap
;
1284 void wxBitmap::SetBitmap( GdkPixmap
*bitmap
)
1287 m_refData
= new wxBitmapRefData();
1289 M_BMPDATA
->m_bitmap
= bitmap
;
1292 GdkPixmap
*wxBitmap::GetPixmap() const
1294 wxCHECK_MSG( Ok(), (GdkPixmap
*) NULL
, wxT("invalid bitmap") );
1296 return M_BMPDATA
->m_pixmap
;
1299 bool wxBitmap::HasPixmap() const
1301 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1303 return M_BMPDATA
->m_pixmap
!= NULL
;
1306 GdkBitmap
*wxBitmap::GetBitmap() const
1308 wxCHECK_MSG( Ok(), (GdkBitmap
*) NULL
, wxT("invalid bitmap") );
1310 return M_BMPDATA
->m_bitmap
;
1313 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1318 void wxBitmap::UngetRawData(wxPixelDataBase
& WXUNUSED(data
))
1323 bool wxBitmap::HasAlpha() const
1328 void wxBitmap::UseAlpha()
1332 //-----------------------------------------------------------------------------
1334 //-----------------------------------------------------------------------------
1336 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
,wxBitmapHandlerBase
)
1338 wxBitmapHandler::~wxBitmapHandler()
1342 bool wxBitmapHandler::Create(wxBitmap
* WXUNUSED(bitmap
),
1343 void * WXUNUSED(data
),
1344 long WXUNUSED(type
),
1345 int WXUNUSED(width
),
1346 int WXUNUSED(height
),
1347 int WXUNUSED(depth
))
1349 wxFAIL_MSG( _T("not implemented") );
1354 bool wxBitmapHandler::LoadFile(wxBitmap
* WXUNUSED(bitmap
),
1355 const wxString
& WXUNUSED(name
),
1356 long WXUNUSED(flags
),
1357 int WXUNUSED(desiredWidth
),
1358 int WXUNUSED(desiredHeight
))
1360 wxFAIL_MSG( _T("not implemented") );
1365 bool wxBitmapHandler::SaveFile(const wxBitmap
* WXUNUSED(bitmap
),
1366 const wxString
& WXUNUSED(name
),
1368 const wxPalette
* WXUNUSED(palette
))
1370 wxFAIL_MSG( _T("not implemented") );
1375 /* static */ void wxBitmap::InitStandardHandlers()
1377 // TODO: Insert handler based on GdkPixbufs handler later