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"
14 #include "wx/palette.h"
16 #include "wx/filefn.h"
18 #include "wx/dcmemory.h"
25 #include <gdk/gdkrgb.h>
30 void gdk_wx_draw_bitmap (GdkDrawable
*drawable
,
40 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
44 extern GtkWidget
*wxGetRootWindow();
46 //-----------------------------------------------------------------------------
48 //-----------------------------------------------------------------------------
50 IMPLEMENT_DYNAMIC_CLASS(wxMask
,wxObject
)
54 m_bitmap
= (GdkBitmap
*) NULL
;
57 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
59 m_bitmap
= (GdkBitmap
*) NULL
;
60 Create( bitmap
, colour
);
64 wxMask::wxMask( const wxBitmap
& bitmap
, int paletteIndex
)
66 m_bitmap
= (GdkBitmap
*) NULL
;
67 Create( bitmap
, paletteIndex
);
69 #endif // wxUSE_PALETTE
71 wxMask::wxMask( const wxBitmap
& bitmap
)
73 m_bitmap
= (GdkBitmap
*) NULL
;
80 gdk_bitmap_unref( m_bitmap
);
83 bool wxMask::Create( const wxBitmap
& bitmap
,
84 const wxColour
& colour
)
88 gdk_bitmap_unref( m_bitmap
);
89 m_bitmap
= (GdkBitmap
*) NULL
;
92 wxImage image
= bitmap
.ConvertToImage();
93 if (!image
.Ok()) return false;
95 m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, image
.GetWidth(), image
.GetHeight(), 1 );
96 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
103 gdk_gc_set_foreground( gc
, &color
);
104 gdk_gc_set_fill( gc
, GDK_SOLID
);
105 gdk_draw_rectangle( m_bitmap
, gc
, TRUE
, 0, 0, image
.GetWidth(), image
.GetHeight() );
107 unsigned char *data
= image
.GetData();
110 unsigned char red
= colour
.Red();
111 unsigned char green
= colour
.Green();
112 unsigned char blue
= colour
.Blue();
114 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
116 int bpp
= visual
->depth
;
117 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800))
122 green
= green
& 0xf8;
128 green
= green
& 0xfc;
134 green
= green
& 0xf0;
142 gdk_gc_set_foreground( gc
, &color
);
144 for (int j
= 0; j
< image
.GetHeight(); j
++)
148 for (i
= 0; i
< image
.GetWidth(); i
++)
150 if ((data
[index
] == red
) &&
151 (data
[index
+1] == green
) &&
152 (data
[index
+2] == blue
))
161 gdk_draw_line( m_bitmap
, gc
, start_x
, j
, i
-1, j
);
168 gdk_draw_line( m_bitmap
, gc
, start_x
, j
, i
, j
);
177 bool wxMask::Create( const wxBitmap
& bitmap
, int paletteIndex
)
180 wxPalette
*pal
= bitmap
.GetPalette();
182 wxCHECK_MSG( pal
, false, wxT("Cannot create mask from bitmap without palette") );
184 pal
->GetRGB(paletteIndex
, &r
, &g
, &b
);
186 return Create(bitmap
, wxColour(r
, g
, b
));
188 #endif // wxUSE_PALETTE
190 bool wxMask::Create( const wxBitmap
& bitmap
)
194 gdk_bitmap_unref( m_bitmap
);
195 m_bitmap
= (GdkBitmap
*) NULL
;
198 if (!bitmap
.Ok()) return false;
200 wxCHECK_MSG( bitmap
.GetBitmap(), false, wxT("Cannot create mask from colour bitmap") );
202 m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, bitmap
.GetWidth(), bitmap
.GetHeight(), 1 );
204 if (!m_bitmap
) return false;
206 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
208 gdk_wx_draw_bitmap( m_bitmap
, gc
, bitmap
.GetBitmap(), 0, 0, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight() );
215 GdkBitmap
*wxMask::GetBitmap() const
220 //-----------------------------------------------------------------------------
222 //-----------------------------------------------------------------------------
224 class wxBitmapRefData
: public wxObjectRefData
236 wxPalette
*m_palette
;
239 wxBitmapRefData::wxBitmapRefData()
241 m_pixmap
= (GdkPixmap
*) NULL
;
242 m_bitmap
= (GdkBitmap
*) NULL
;
243 m_mask
= (wxMask
*) NULL
;
247 m_palette
= (wxPalette
*) NULL
;
250 wxBitmapRefData::~wxBitmapRefData()
253 gdk_pixmap_unref( m_pixmap
);
255 gdk_bitmap_unref( m_bitmap
);
259 #endif // wxUSE_PALETTE
262 //-----------------------------------------------------------------------------
264 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
266 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
272 wxBitmap::wxBitmap( int width
, int height
, int depth
)
274 Create( width
, height
, depth
);
277 bool wxBitmap::Create( int width
, int height
, int depth
)
281 if ( width
<= 0 || height
<= 0 )
286 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
289 depth
= visual
->depth
;
291 wxCHECK_MSG( (depth
== visual
->depth
) || (depth
== 1) || (depth
== 32), false,
292 wxT("invalid bitmap depth") );
294 m_refData
= new wxBitmapRefData();
295 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
296 M_BMPDATA
->m_width
= width
;
297 M_BMPDATA
->m_height
= height
;
300 M_BMPDATA
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
301 M_BMPDATA
->m_bpp
= 1;
305 M_BMPDATA
->m_pixmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, depth
);
306 M_BMPDATA
->m_bpp
= visual
->depth
;
312 bool wxBitmap::CreateFromXpm( const char **bits
)
316 wxCHECK_MSG( bits
!= NULL
, false, wxT("invalid bitmap data") );
318 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
320 m_refData
= new wxBitmapRefData();
322 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
324 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm_d( wxGetRootWindow()->window
, &mask
, NULL
, (gchar
**) bits
);
326 wxCHECK_MSG( M_BMPDATA
->m_pixmap
, false, wxT("couldn't create pixmap") );
330 M_BMPDATA
->m_mask
= new wxMask();
331 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
334 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
336 M_BMPDATA
->m_bpp
= visual
->depth
; // Can we get a different depth from create_from_xpm_d() ?
341 wxBitmap
wxBitmap::Rescale( int clipx
, int clipy
, int clipwidth
, int clipheight
, int newx
, int newy
)
343 wxCHECK_MSG( Ok(), wxNullBitmap
, wxT("invalid bitmap") );
345 if (newy
==M_BMPDATA
->m_width
&& newy
==M_BMPDATA
->m_height
)
348 int width
= wxMax(newx
, 1);
349 int height
= wxMax(newy
, 1);
350 width
= wxMin(width
, clipwidth
);
351 height
= wxMin(height
, clipheight
);
355 GdkImage
*img
= (GdkImage
*) NULL
;
357 img
= gdk_image_get( GetPixmap(), 0, 0, GetWidth(), GetHeight() );
358 else if (GetBitmap())
359 img
= gdk_image_get( GetBitmap(), 0, 0, GetWidth(), GetHeight() );
361 wxFAIL_MSG( wxT("Ill-formed bitmap") );
363 wxCHECK_MSG( img
, wxNullBitmap
, wxT("couldn't create image") );
369 GdkPixmap
*dstpix
= NULL
;
372 GdkVisual
*visual
= gdk_window_get_visual( GetPixmap() );
374 visual
= wxTheApp
->GetGdkVisual();
377 bmp
= wxBitmap(width
,height
,bpp
);
378 dstpix
= bmp
.GetPixmap();
379 gc
= gdk_gc_new( dstpix
);
383 long dstbyteperline
= 0;
388 dstbyteperline
= width
/8*M_BMPDATA
->m_bpp
;
389 if (width
*M_BMPDATA
->m_bpp
% 8 != 0)
391 dst
= (char*) malloc(dstbyteperline
*height
);
394 // be careful to use the right scaling factor
395 float scx
= (float)M_BMPDATA
->m_width
/(float)newx
;
396 float scy
= (float)M_BMPDATA
->m_height
/(float)newy
;
397 // prepare accel-tables
398 int *tablex
= (int *)calloc(width
,sizeof(int));
399 int *tabley
= (int *)calloc(height
,sizeof(int));
401 // accel table filled with clipped values
402 for (int x
= 0; x
< width
; x
++)
403 tablex
[x
] = (int) (scx
* (x
+clipx
));
404 for (int y
= 0; y
< height
; y
++)
405 tabley
[y
] = (int) (scy
* (y
+clipy
));
407 // Main rescaling routine starts here
408 for (int h
= 0; h
< height
; h
++)
412 guint32 old_pixval
= 0;
414 for (int w
= 0; w
< width
; w
++)
422 pixval
= gdk_image_get_pixel( img
, x
, tabley
[h
] );
432 char shift
= bit
<< (w
% 8);
438 dst
[h
*dstbyteperline
+w
/8] = outbyte
;
446 gdk_gc_set_foreground( gc
, &col
);
447 gdk_draw_point( dstpix
, gc
, w
, h
);
451 // do not forget the last byte
452 if ((bpp
== 1) && (width
% 8 != 0))
453 dst
[h
*dstbyteperline
+width
/8] = outbyte
;
456 gdk_image_destroy( img
);
457 if (gc
) gdk_gc_unref( gc
);
461 bmp
= wxBitmap( (const char *)dst
, width
, height
, 1 );
467 dstbyteperline
= width
/8;
470 dst
= (char*) malloc(dstbyteperline
*height
);
471 img
= gdk_image_get( GetMask()->GetBitmap(), 0, 0, GetWidth(), GetHeight() );
473 for (int h
= 0; h
< height
; h
++)
477 guint32 old_pixval
= 0;
479 for (int w
= 0; w
< width
; w
++)
487 pixval
= gdk_image_get_pixel( img
, x
, tabley
[h
] );
495 char shift
= bit
<< (w
% 8);
501 dst
[h
*dstbyteperline
+w
/8] = outbyte
;
506 // do not forget the last byte
508 dst
[h
*dstbyteperline
+width
/8] = outbyte
;
510 wxMask
* mask
= new wxMask
;
511 mask
->m_bitmap
= gdk_bitmap_create_from_data( wxGetRootWindow()->window
, (gchar
*) dst
, width
, height
);
515 gdk_image_destroy( img
);
524 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
528 wxCHECK_MSG( image
.Ok(), false, wxT("invalid image") );
529 wxCHECK_MSG( depth
== -1 || depth
== 1, false, wxT("invalid bitmap depth") );
531 if (image
.GetWidth() <= 0 || image
.GetHeight() <= 0)
534 m_refData
= new wxBitmapRefData();
538 return CreateFromImageAsBitmap(image
);
542 return CreateFromImageAsPixmap(image
);
546 // conversion to mono bitmap:
547 bool wxBitmap::CreateFromImageAsBitmap(const wxImage
& img
)
549 // convert alpha channel to mask, if it is present:
551 image
.ConvertAlphaToMask();
553 int width
= image
.GetWidth();
554 int height
= image
.GetHeight();
559 SetBitmap( gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 ) );
563 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
565 // Create picture image
567 unsigned char *data_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
569 GdkImage
*data_image
=
570 gdk_image_new_bitmap( visual
, data_data
, width
, height
);
574 GdkImage
*mask_image
= (GdkImage
*) NULL
;
578 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
580 mask_image
= gdk_image_new_bitmap( visual
, mask_data
, width
, height
);
582 wxMask
*mask
= new wxMask();
583 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
588 int r_mask
= image
.GetMaskRed();
589 int g_mask
= image
.GetMaskGreen();
590 int b_mask
= image
.GetMaskBlue();
592 unsigned char* data
= image
.GetData();
595 for (int y
= 0; y
< height
; y
++)
597 for (int x
= 0; x
< width
; x
++)
608 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
609 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
611 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
614 if ((r
== 255) && (b
== 255) && (g
== 255))
615 gdk_image_put_pixel( data_image
, x
, y
, 1 );
617 gdk_image_put_pixel( data_image
, x
, y
, 0 );
624 GdkGC
*data_gc
= gdk_gc_new( GetBitmap() );
626 gdk_draw_image( GetBitmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
628 gdk_image_destroy( data_image
);
629 gdk_gc_unref( data_gc
);
635 GdkGC
*mask_gc
= gdk_gc_new( GetMask()->GetBitmap() );
637 gdk_draw_image( GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
639 gdk_image_destroy( mask_image
);
640 gdk_gc_unref( mask_gc
);
646 // conversion to colour bitmap:
647 bool wxBitmap::CreateFromImageAsPixmap(const wxImage
& img
)
649 // convert alpha channel to mask, if it is present:
651 image
.ConvertAlphaToMask();
653 int width
= image
.GetWidth();
654 int height
= image
.GetHeight();
659 SetPixmap( gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, -1 ) );
661 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
663 int bpp
= visual
->depth
;
667 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800))
672 // We handle 8-bit bitmaps ourselves using the colour cube, 12-bit
673 // visuals are not supported by GDK so we do these ourselves, too.
674 // 15-bit and 16-bit should actually work and 24-bit certainly does.
676 if (!image
.HasMask() && (bpp
> 16))
678 if (!image
.HasMask() && (bpp
> 12))
681 static bool s_hasInitialized
= false;
683 if (!s_hasInitialized
)
686 s_hasInitialized
= true;
689 GdkGC
*gc
= gdk_gc_new( GetPixmap() );
691 gdk_draw_rgb_image( GetPixmap(),
703 // Create picture image
705 GdkImage
*data_image
=
706 gdk_image_new( GDK_IMAGE_FASTEST
, visual
, width
, height
);
710 GdkImage
*mask_image
= (GdkImage
*) NULL
;
714 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
716 mask_image
= gdk_image_new_bitmap( visual
, mask_data
, width
, height
);
718 wxMask
*mask
= new wxMask();
719 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
726 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
727 byte_order b_o
= RGB
;
731 if ((visual
->red_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->blue_mask
)) b_o
= RGB
;
732 else if ((visual
->red_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->green_mask
)) b_o
= RBG
;
733 else if ((visual
->blue_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->green_mask
)) b_o
= BRG
;
734 else if ((visual
->blue_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->red_mask
)) b_o
= BGR
;
735 else if ((visual
->green_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->blue_mask
)) b_o
= GRB
;
736 else if ((visual
->green_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->red_mask
)) b_o
= GBR
;
739 int r_mask
= image
.GetMaskRed();
740 int g_mask
= image
.GetMaskGreen();
741 int b_mask
= image
.GetMaskBlue();
743 unsigned char* data
= image
.GetData();
746 for (int y
= 0; y
< height
; y
++)
748 for (int x
= 0; x
< width
; x
++)
759 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
760 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
762 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
770 if (wxTheApp
->m_colorCube
)
772 pixel
= wxTheApp
->m_colorCube
[ ((r
& 0xf8) << 7) + ((g
& 0xf8) << 2) + ((b
& 0xf8) >> 3) ];
776 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
777 GdkColor
*colors
= cmap
->colors
;
778 int max
= 3 * (65536);
780 for (int i
= 0; i
< cmap
->size
; i
++)
782 int rdiff
= (r
<< 8) - colors
[i
].red
;
783 int gdiff
= (g
<< 8) - colors
[i
].green
;
784 int bdiff
= (b
<< 8) - colors
[i
].blue
;
785 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
786 if (sum
< max
) { pixel
= i
; max
= sum
; }
790 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
799 case RGB
: pixel
= ((r
& 0xf0) << 4) | (g
& 0xf0) | ((b
& 0xf0) >> 4); break;
800 case RBG
: pixel
= ((r
& 0xf0) << 4) | (b
& 0xf0) | ((g
& 0xf0) >> 4); break;
801 case GRB
: pixel
= ((g
& 0xf0) << 4) | (r
& 0xf0) | ((b
& 0xf0) >> 4); break;
802 case GBR
: pixel
= ((g
& 0xf0) << 4) | (b
& 0xf0) | ((r
& 0xf0) >> 4); break;
803 case BRG
: pixel
= ((b
& 0xf0) << 4) | (r
& 0xf0) | ((g
& 0xf0) >> 4); break;
804 case BGR
: pixel
= ((b
& 0xf0) << 4) | (g
& 0xf0) | ((r
& 0xf0) >> 4); break;
806 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
814 case RGB
: pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
815 case RBG
: pixel
= ((r
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
816 case GRB
: pixel
= ((g
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
817 case GBR
: pixel
= ((g
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
818 case BRG
: pixel
= ((b
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
819 case BGR
: pixel
= ((b
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
821 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
826 // I actually don't know if for 16-bit displays, it is alway the green
827 // component or the second component which has 6 bits.
831 case RGB
: pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
832 case RBG
: pixel
= ((r
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
833 case GRB
: pixel
= ((g
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
834 case GBR
: pixel
= ((g
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
835 case BRG
: pixel
= ((b
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
836 case BGR
: pixel
= ((b
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
838 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
847 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
848 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
849 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
850 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
851 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
852 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
854 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
864 GdkGC
*data_gc
= gdk_gc_new( GetPixmap() );
866 gdk_draw_image( GetPixmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
868 gdk_image_destroy( data_image
);
869 gdk_gc_unref( data_gc
);
875 GdkGC
*mask_gc
= gdk_gc_new( GetMask()->GetBitmap() );
877 gdk_draw_image( GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
879 gdk_image_destroy( mask_image
);
880 gdk_gc_unref( mask_gc
);
886 wxImage
wxBitmap::ConvertToImage() const
890 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
892 image
.Create(GetWidth(), GetHeight());
893 unsigned char *data
= image
.GetData();
897 wxFAIL_MSG( wxT("couldn't create image") );
901 // the colour used as transparent one in wxImage and the one it is
902 // replaced with when it really occurs in the bitmap
903 static const int MASK_RED
= 1;
904 static const int MASK_GREEN
= 2;
905 static const int MASK_BLUE
= 3;
906 static const int MASK_BLUE_REPLACEMENT
= 2;
908 GdkImage
*gdk_image
= (GdkImage
*) NULL
;
912 gdk_image
= gdk_image_get( GetPixmap(),
914 GetWidth(), GetHeight() );
916 else if (GetBitmap())
918 gdk_image
= gdk_image_get( GetBitmap(),
920 GetWidth(), GetHeight() );
924 wxFAIL_MSG( wxT("Ill-formed bitmap") );
927 wxCHECK_MSG( gdk_image
, wxNullImage
, wxT("couldn't create image") );
929 GdkImage
*gdk_image_mask
= (GdkImage
*) NULL
;
932 gdk_image_mask
= gdk_image_get( GetMask()->GetBitmap(),
934 GetWidth(), GetHeight() );
936 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
940 int red_shift_right
= 0;
941 int green_shift_right
= 0;
942 int blue_shift_right
= 0;
943 int red_shift_left
= 0;
944 int green_shift_left
= 0;
945 int blue_shift_left
= 0;
946 bool use_shift
= false;
950 GdkVisual
*visual
= gdk_window_get_visual( GetPixmap() );
952 visual
= wxTheApp
->GetGdkVisual();
956 bpp
= visual
->red_prec
+ visual
->green_prec
+ visual
->blue_prec
;
957 red_shift_right
= visual
->red_shift
;
958 red_shift_left
= 8-visual
->red_prec
;
959 green_shift_right
= visual
->green_shift
;
960 green_shift_left
= 8-visual
->green_prec
;
961 blue_shift_right
= visual
->blue_shift
;
962 blue_shift_left
= 8-visual
->blue_prec
;
964 use_shift
= (visual
->type
== GDK_VISUAL_TRUE_COLOR
) || (visual
->type
== GDK_VISUAL_DIRECT_COLOR
);
972 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
975 for (int j
= 0; j
< GetHeight(); j
++)
977 for (int i
= 0; i
< GetWidth(); i
++)
979 wxUint32 pixel
= gdk_image_get_pixel( gdk_image
, i
, j
);
997 data
[pos
] = (pixel
>> red_shift_right
) << red_shift_left
;
998 data
[pos
+1] = (pixel
>> green_shift_right
) << green_shift_left
;
999 data
[pos
+2] = (pixel
>> blue_shift_right
) << blue_shift_left
;
1001 else if (cmap
->colors
)
1003 data
[pos
] = cmap
->colors
[pixel
].red
>> 8;
1004 data
[pos
+1] = cmap
->colors
[pixel
].green
>> 8;
1005 data
[pos
+2] = cmap
->colors
[pixel
].blue
>> 8;
1009 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
1014 int mask_pixel
= gdk_image_get_pixel( gdk_image_mask
, i
, j
);
1015 if (mask_pixel
== 0)
1017 data
[pos
] = MASK_RED
;
1018 data
[pos
+1] = MASK_GREEN
;
1019 data
[pos
+2] = MASK_BLUE
;
1021 else if ( data
[pos
] == MASK_RED
&&
1022 data
[pos
+1] == MASK_GREEN
&&
1023 data
[pos
+2] == MASK_BLUE
)
1025 data
[pos
+2] = MASK_BLUE_REPLACEMENT
;
1033 gdk_image_destroy( gdk_image
);
1034 if (gdk_image_mask
) gdk_image_destroy( gdk_image_mask
);
1039 wxBitmap::wxBitmap( const wxString
&filename
, wxBitmapType type
)
1041 LoadFile( filename
, type
);
1044 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int WXUNUSED(depth
))
1046 if ( width
> 0 && height
> 0 )
1048 m_refData
= new wxBitmapRefData();
1050 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
1051 M_BMPDATA
->m_bitmap
= gdk_bitmap_create_from_data
1053 wxGetRootWindow()->window
,
1058 M_BMPDATA
->m_width
= width
;
1059 M_BMPDATA
->m_height
= height
;
1060 M_BMPDATA
->m_bpp
= 1;
1062 wxASSERT_MSG( M_BMPDATA
->m_bitmap
, wxT("couldn't create bitmap") );
1066 wxBitmap::~wxBitmap()
1070 bool wxBitmap::operator == ( const wxBitmap
& bmp
) const
1072 return m_refData
== bmp
.m_refData
;
1075 bool wxBitmap::operator != ( const wxBitmap
& bmp
) const
1077 return m_refData
!= bmp
.m_refData
;
1080 bool wxBitmap::Ok() const
1082 return (m_refData
!= NULL
) &&
1083 (M_BMPDATA
->m_bitmap
|| M_BMPDATA
->m_pixmap
);
1086 int wxBitmap::GetHeight() const
1088 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1090 return M_BMPDATA
->m_height
;
1093 int wxBitmap::GetWidth() const
1095 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1097 return M_BMPDATA
->m_width
;
1100 int wxBitmap::GetDepth() const
1102 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1104 return M_BMPDATA
->m_bpp
;
1107 wxMask
*wxBitmap::GetMask() const
1109 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
1111 return M_BMPDATA
->m_mask
;
1114 void wxBitmap::SetMask( wxMask
*mask
)
1116 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
1118 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
1120 M_BMPDATA
->m_mask
= mask
;
1123 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
1129 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
1131 wxCHECK_MSG( Ok() &&
1132 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1133 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) && (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
1134 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
1136 wxBitmap
ret( rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
1137 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
1139 if (ret
.GetPixmap())
1141 GdkGC
*gc
= gdk_gc_new( ret
.GetPixmap() );
1142 gdk_draw_pixmap( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1143 gdk_gc_destroy( gc
);
1147 GdkGC
*gc
= gdk_gc_new( ret
.GetBitmap() );
1149 col
.pixel
= 0xFFFFFF;
1150 gdk_gc_set_foreground( gc
, &col
);
1152 gdk_gc_set_background( gc
, &col
);
1153 gdk_wx_draw_bitmap( ret
.GetBitmap(), gc
, GetBitmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1154 gdk_gc_destroy( gc
);
1159 wxMask
*mask
= new wxMask
;
1160 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, rect
.width
, rect
.height
, 1 );
1162 GdkGC
*gc
= gdk_gc_new( mask
->m_bitmap
);
1164 col
.pixel
= 0xFFFFFF;
1165 gdk_gc_set_foreground( gc
, &col
);
1167 gdk_gc_set_background( gc
, &col
);
1168 gdk_wx_draw_bitmap( mask
->m_bitmap
, gc
, M_BMPDATA
->m_mask
->m_bitmap
, rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1169 gdk_gc_destroy( gc
);
1171 ret
.SetMask( mask
);
1177 bool wxBitmap::SaveFile( const wxString
&name
, wxBitmapType type
, const wxPalette
*WXUNUSED(palette
) ) const
1179 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1181 // Try to save the bitmap via wxImage handlers:
1183 wxImage image
= ConvertToImage();
1184 if (image
.Ok()) return image
.SaveFile( name
, type
);
1190 bool wxBitmap::LoadFile( const wxString
&name
, wxBitmapType type
)
1194 if (!wxFileExists(name
))
1197 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
1199 if (type
== wxBITMAP_TYPE_XPM
)
1201 m_refData
= new wxBitmapRefData();
1203 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
1205 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm
1207 wxGetRootWindow()->window
,
1215 M_BMPDATA
->m_mask
= new wxMask();
1216 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
1219 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
1221 M_BMPDATA
->m_bpp
= visual
->depth
;
1223 else // try if wxImage can load it
1226 if ( !image
.LoadFile( name
, type
) || !image
.Ok() )
1229 *this = wxBitmap(image
);
1236 wxPalette
*wxBitmap::GetPalette() const
1239 return (wxPalette
*) NULL
;
1241 return M_BMPDATA
->m_palette
;
1244 void wxBitmap::SetPalette(const wxPalette
& WXUNUSED(palette
))
1248 #endif // wxUSE_PALETTE
1250 void wxBitmap::SetHeight( int height
)
1253 m_refData
= new wxBitmapRefData();
1255 M_BMPDATA
->m_height
= height
;
1258 void wxBitmap::SetWidth( int width
)
1261 m_refData
= new wxBitmapRefData();
1263 M_BMPDATA
->m_width
= width
;
1266 void wxBitmap::SetDepth( int depth
)
1269 m_refData
= new wxBitmapRefData();
1271 M_BMPDATA
->m_bpp
= depth
;
1274 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
1277 m_refData
= new wxBitmapRefData();
1279 M_BMPDATA
->m_pixmap
= pixmap
;
1282 void wxBitmap::SetBitmap( GdkPixmap
*bitmap
)
1285 m_refData
= new wxBitmapRefData();
1287 M_BMPDATA
->m_bitmap
= bitmap
;
1290 GdkPixmap
*wxBitmap::GetPixmap() const
1292 wxCHECK_MSG( Ok(), (GdkPixmap
*) NULL
, wxT("invalid bitmap") );
1294 return M_BMPDATA
->m_pixmap
;
1297 bool wxBitmap::HasPixmap() const
1299 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1301 return M_BMPDATA
->m_pixmap
!= NULL
;
1304 GdkBitmap
*wxBitmap::GetBitmap() const
1306 wxCHECK_MSG( Ok(), (GdkBitmap
*) NULL
, wxT("invalid bitmap") );
1308 return M_BMPDATA
->m_bitmap
;
1311 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1316 void wxBitmap::UngetRawData(wxPixelDataBase
& WXUNUSED(data
))
1321 bool wxBitmap::HasAlpha() const
1326 void wxBitmap::UseAlpha()
1330 //-----------------------------------------------------------------------------
1332 //-----------------------------------------------------------------------------
1334 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
,wxBitmapHandlerBase
)
1336 wxBitmapHandler::~wxBitmapHandler()
1340 bool wxBitmapHandler::Create(wxBitmap
* WXUNUSED(bitmap
),
1341 void * WXUNUSED(data
),
1342 long WXUNUSED(type
),
1343 int WXUNUSED(width
),
1344 int WXUNUSED(height
),
1345 int WXUNUSED(depth
))
1347 wxFAIL_MSG( _T("not implemented") );
1352 bool wxBitmapHandler::LoadFile(wxBitmap
* WXUNUSED(bitmap
),
1353 const wxString
& WXUNUSED(name
),
1354 long WXUNUSED(flags
),
1355 int WXUNUSED(desiredWidth
),
1356 int WXUNUSED(desiredHeight
))
1358 wxFAIL_MSG( _T("not implemented") );
1363 bool wxBitmapHandler::SaveFile(const wxBitmap
* WXUNUSED(bitmap
),
1364 const wxString
& WXUNUSED(name
),
1366 const wxPalette
* WXUNUSED(palette
))
1368 wxFAIL_MSG( _T("not implemented") );
1373 /* static */ void wxBitmap::InitStandardHandlers()
1375 // TODO: Insert handler based on GdkPixbufs handler later