1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/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"
23 #include "wx/rawbmp.h"
24 // need this to get gdk_image_new_bitmap()
25 #define GDK_ENABLE_BROKEN
31 #include <gdk/gdkimage.h>
35 extern void gdk_wx_draw_bitmap (GdkDrawable
*drawable
,
45 //-----------------------------------------------------------------------------
47 //-----------------------------------------------------------------------------
49 extern GtkWidget
*wxGetRootWindow();
51 //-----------------------------------------------------------------------------
53 //-----------------------------------------------------------------------------
55 IMPLEMENT_DYNAMIC_CLASS(wxMask
,wxObject
)
59 m_bitmap
= (GdkBitmap
*) NULL
;
62 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
64 m_bitmap
= (GdkBitmap
*) NULL
;
65 Create( bitmap
, colour
);
69 wxMask::wxMask( const wxBitmap
& bitmap
, int paletteIndex
)
71 m_bitmap
= (GdkBitmap
*) NULL
;
72 Create( bitmap
, paletteIndex
);
74 #endif // wxUSE_PALETTE
76 wxMask::wxMask( const wxBitmap
& bitmap
)
78 m_bitmap
= (GdkBitmap
*) NULL
;
85 gdk_bitmap_unref( m_bitmap
);
88 bool wxMask::Create( const wxBitmap
& bitmap
,
89 const wxColour
& colour
)
93 gdk_bitmap_unref( m_bitmap
);
94 m_bitmap
= (GdkBitmap
*) NULL
;
97 wxImage image
= bitmap
.ConvertToImage();
98 if (!image
.Ok()) return false;
100 m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, image
.GetWidth(), image
.GetHeight(), 1 );
101 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
108 gdk_gc_set_foreground( gc
, &color
);
109 gdk_gc_set_fill( gc
, GDK_SOLID
);
110 gdk_draw_rectangle( m_bitmap
, gc
, TRUE
, 0, 0, image
.GetWidth(), image
.GetHeight() );
112 unsigned char *data
= image
.GetData();
115 unsigned char red
= colour
.Red();
116 unsigned char green
= colour
.Green();
117 unsigned char blue
= colour
.Blue();
119 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
121 int bpp
= visual
->depth
;
122 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800))
127 green
= green
& 0xf8;
133 green
= green
& 0xfc;
139 green
= green
& 0xf0;
147 gdk_gc_set_foreground( gc
, &color
);
149 for (int j
= 0; j
< image
.GetHeight(); j
++)
153 for (i
= 0; i
< image
.GetWidth(); i
++)
155 if ((data
[index
] == red
) &&
156 (data
[index
+1] == green
) &&
157 (data
[index
+2] == blue
))
166 gdk_draw_line( m_bitmap
, gc
, start_x
, j
, i
-1, j
);
173 gdk_draw_line( m_bitmap
, gc
, start_x
, j
, i
, j
);
182 bool wxMask::Create( const wxBitmap
& bitmap
, int paletteIndex
)
185 wxPalette
*pal
= bitmap
.GetPalette();
187 wxCHECK_MSG( pal
, false, wxT("Cannot create mask from bitmap without palette") );
189 pal
->GetRGB(paletteIndex
, &r
, &g
, &b
);
191 return Create(bitmap
, wxColour(r
, g
, b
));
193 #endif // wxUSE_PALETTE
195 bool wxMask::Create( const wxBitmap
& bitmap
)
199 gdk_bitmap_unref( m_bitmap
);
200 m_bitmap
= (GdkBitmap
*) NULL
;
203 if (!bitmap
.Ok()) return false;
205 wxCHECK_MSG( bitmap
.GetBitmap(), false, wxT("Cannot create mask from colour bitmap") );
207 m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, bitmap
.GetWidth(), bitmap
.GetHeight(), 1 );
209 if (!m_bitmap
) return false;
211 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
213 gdk_wx_draw_bitmap( m_bitmap
, gc
, bitmap
.GetBitmap(), 0, 0, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight() );
220 GdkBitmap
*wxMask::GetBitmap() const
225 //-----------------------------------------------------------------------------
227 //-----------------------------------------------------------------------------
229 class wxBitmapRefData
: public wxObjectRefData
242 wxPalette
*m_palette
;
245 wxBitmapRefData::wxBitmapRefData()
247 m_pixmap
= (GdkPixmap
*) NULL
;
248 m_bitmap
= (GdkBitmap
*) NULL
;
249 m_pixbuf
= (GdkPixbuf
*) NULL
;
250 m_mask
= (wxMask
*) NULL
;
254 m_palette
= (wxPalette
*) NULL
;
257 wxBitmapRefData::~wxBitmapRefData()
260 gdk_pixmap_unref( m_pixmap
);
262 gdk_bitmap_unref( m_bitmap
);
264 gdk_pixbuf_unref( m_pixbuf
);
268 #endif // wxUSE_PALETTE
271 //-----------------------------------------------------------------------------
273 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
275 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
281 wxBitmap::wxBitmap( int width
, int height
, int depth
)
283 Create( width
, height
, depth
);
286 bool wxBitmap::Create( int width
, int height
, int depth
)
290 if ( width
<= 0 || height
<= 0 )
295 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
298 depth
= visual
->depth
;
300 wxCHECK_MSG( (depth
== visual
->depth
) || (depth
== 1) || (depth
== 32), false,
301 wxT("invalid bitmap depth") )
303 m_refData
= new wxBitmapRefData();
304 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
305 M_BMPDATA
->m_width
= width
;
306 M_BMPDATA
->m_height
= height
;
309 M_BMPDATA
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
310 M_BMPDATA
->m_bpp
= 1;
312 else if (depth
== 32)
314 M_BMPDATA
->m_pixbuf
= gdk_pixbuf_new( GDK_COLORSPACE_RGB
, true,
316 M_BMPDATA
->m_bpp
= 32;
320 M_BMPDATA
->m_pixmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, depth
);
321 M_BMPDATA
->m_bpp
= visual
->depth
;
327 bool wxBitmap::CreateFromXpm( const char **bits
)
331 wxCHECK_MSG( bits
!= NULL
, false, wxT("invalid bitmap data") )
333 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
335 m_refData
= new wxBitmapRefData();
337 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
339 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm_d( wxGetRootWindow()->window
, &mask
, NULL
, (gchar
**) bits
);
341 wxCHECK_MSG( M_BMPDATA
->m_pixmap
, false, wxT("couldn't create pixmap") );
345 M_BMPDATA
->m_mask
= new wxMask();
346 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
349 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
351 M_BMPDATA
->m_bpp
= visual
->depth
; // Can we get a different depth from create_from_xpm_d() ?
356 wxBitmap
wxBitmap::Rescale( int clipx
, int clipy
, int clipwidth
, int clipheight
, int newx
, int newy
)
358 wxCHECK_MSG( Ok(), wxNullBitmap
, wxT("invalid bitmap") );
360 if (newy
==M_BMPDATA
->m_width
&& newy
==M_BMPDATA
->m_height
)
363 int width
= wxMax(newx
, 1);
364 int height
= wxMax(newy
, 1);
365 width
= wxMin(width
, clipwidth
);
366 height
= wxMin(height
, clipheight
);
373 bmp
.SetHeight(height
);
374 bmp
.SetDepth(GetDepth());
375 bmp
.SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
376 gdk_pixbuf_get_has_alpha(GetPixbuf()),
378 gdk_pixbuf_scale(GetPixbuf(), bmp
.GetPixbuf(),
381 (double)newx
/GetWidth(), (double)newy
/GetHeight(),
382 GDK_INTERP_BILINEAR
);
386 GdkImage
*img
= (GdkImage
*) NULL
;
388 img
= gdk_image_get( GetPixmap(), 0, 0, GetWidth(), GetHeight() );
389 else if (GetBitmap())
390 img
= gdk_image_get( GetBitmap(), 0, 0, GetWidth(), GetHeight() );
392 wxFAIL_MSG( wxT("Ill-formed bitmap") );
394 wxCHECK_MSG( img
, wxNullBitmap
, wxT("couldn't create image") );
400 GdkPixmap
*dstpix
= NULL
;
403 GdkVisual
*visual
= gdk_window_get_visual( GetPixmap() );
405 visual
= wxTheApp
->GetGdkVisual();
408 bmp
= wxBitmap(width
,height
,bpp
);
409 dstpix
= bmp
.GetPixmap();
410 gc
= gdk_gc_new( dstpix
);
414 long dstbyteperline
= 0;
419 dstbyteperline
= width
/8*M_BMPDATA
->m_bpp
;
420 if (width
*M_BMPDATA
->m_bpp
% 8 != 0)
422 dst
= (char*) malloc(dstbyteperline
*height
);
425 // be careful to use the right scaling factor
426 float scx
= (float)M_BMPDATA
->m_width
/(float)newx
;
427 float scy
= (float)M_BMPDATA
->m_height
/(float)newy
;
428 // prepare accel-tables
429 int *tablex
= (int *)calloc(width
,sizeof(int));
430 int *tabley
= (int *)calloc(height
,sizeof(int));
432 // accel table filled with clipped values
433 for (int x
= 0; x
< width
; x
++)
434 tablex
[x
] = (int) (scx
* (x
+clipx
));
435 for (int y
= 0; y
< height
; y
++)
436 tabley
[y
] = (int) (scy
* (y
+clipy
));
438 // Main rescaling routine starts here
439 for (int h
= 0; h
< height
; h
++)
443 guint32 old_pixval
= 0;
445 for (int w
= 0; w
< width
; w
++)
453 pixval
= gdk_image_get_pixel( img
, x
, tabley
[h
] );
463 char shift
= bit
<< (w
% 8);
469 dst
[h
*dstbyteperline
+w
/8] = outbyte
;
477 gdk_gc_set_foreground( gc
, &col
);
478 gdk_draw_point( dstpix
, gc
, w
, h
);
482 // do not forget the last byte
483 if ( dst
&& (width
% 8 != 0) )
484 dst
[h
*dstbyteperline
+width
/8] = outbyte
;
487 gdk_image_destroy( img
);
488 if (gc
) gdk_gc_unref( gc
);
492 bmp
= wxBitmap( (const char *)dst
, width
, height
, 1 );
498 dstbyteperline
= width
/8;
501 dst
= (char*) malloc(dstbyteperline
*height
);
502 img
= gdk_image_get( GetMask()->GetBitmap(), 0, 0, GetWidth(), GetHeight() );
504 for (int h
= 0; h
< height
; h
++)
508 guint32 old_pixval
= 0;
510 for (int w
= 0; w
< width
; w
++)
518 pixval
= gdk_image_get_pixel( img
, x
, tabley
[h
] );
526 char shift
= bit
<< (w
% 8);
532 dst
[h
*dstbyteperline
+w
/8] = outbyte
;
537 // do not forget the last byte
539 dst
[h
*dstbyteperline
+width
/8] = outbyte
;
541 wxMask
* mask
= new wxMask
;
542 mask
->m_bitmap
= gdk_bitmap_create_from_data( wxGetRootWindow()->window
, (gchar
*) dst
, width
, height
);
546 gdk_image_destroy( img
);
556 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
560 wxCHECK_MSG( image
.Ok(), false, wxT("invalid image") )
561 wxCHECK_MSG( depth
== -1 || depth
== 1, false, wxT("invalid bitmap depth") )
563 if (image
.GetWidth() <= 0 || image
.GetHeight() <= 0)
566 m_refData
= new wxBitmapRefData();
570 return CreateFromImageAsBitmap(image
);
574 if (image
.HasAlpha())
575 return CreateFromImageAsPixbuf(image
);
577 return CreateFromImageAsPixmap(image
);
581 // conversion to mono bitmap:
582 bool wxBitmap::CreateFromImageAsBitmap(const wxImage
& img
)
584 // convert alpha channel to mask, if it is present:
586 image
.ConvertAlphaToMask();
588 int width
= image
.GetWidth();
589 int height
= image
.GetHeight();
594 SetBitmap( gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 ) );
598 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
600 // Create picture image
602 unsigned char *data_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
604 GdkImage
*data_image
=
605 gdk_image_new_bitmap( visual
, data_data
, width
, height
);
609 GdkImage
*mask_image
= (GdkImage
*) NULL
;
613 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
615 mask_image
= gdk_image_new_bitmap( visual
, mask_data
, width
, height
);
617 wxMask
*mask
= new wxMask();
618 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
623 int r_mask
= image
.GetMaskRed();
624 int g_mask
= image
.GetMaskGreen();
625 int b_mask
= image
.GetMaskBlue();
627 unsigned char* data
= image
.GetData();
630 for (int y
= 0; y
< height
; y
++)
632 for (int x
= 0; x
< width
; x
++)
643 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
644 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
646 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
649 if ((r
== 255) && (b
== 255) && (g
== 255))
650 gdk_image_put_pixel( data_image
, x
, y
, 1 );
652 gdk_image_put_pixel( data_image
, x
, y
, 0 );
659 GdkGC
*data_gc
= gdk_gc_new( GetBitmap() );
661 gdk_draw_image( GetBitmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
663 gdk_image_destroy( data_image
);
664 gdk_gc_unref( data_gc
);
670 GdkGC
*mask_gc
= gdk_gc_new( GetMask()->GetBitmap() );
672 gdk_draw_image( GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
674 gdk_image_destroy( mask_image
);
675 gdk_gc_unref( mask_gc
);
681 // conversion to colour bitmap:
682 bool wxBitmap::CreateFromImageAsPixmap(const wxImage
& img
)
684 // convert alpha channel to mask, if it is present:
686 image
.ConvertAlphaToMask();
688 int width
= image
.GetWidth();
689 int height
= image
.GetHeight();
694 SetPixmap( gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, -1 ) );
696 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
698 int bpp
= visual
->depth
;
702 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800))
707 // We handle 8-bit bitmaps ourselves using the colour cube, 12-bit
708 // visuals are not supported by GDK so we do these ourselves, too.
709 // 15-bit and 16-bit should actually work and 24-bit certainly does.
711 if (!image
.HasMask() && (bpp
> 16))
713 if (!image
.HasMask() && (bpp
> 12))
716 static bool s_hasInitialized
= false;
718 if (!s_hasInitialized
)
721 s_hasInitialized
= true;
724 GdkGC
*gc
= gdk_gc_new( GetPixmap() );
726 gdk_draw_rgb_image( GetPixmap(),
738 // Create picture image
740 GdkImage
*data_image
=
741 gdk_image_new( GDK_IMAGE_FASTEST
, visual
, width
, height
);
745 GdkImage
*mask_image
= (GdkImage
*) NULL
;
749 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
751 mask_image
= gdk_image_new_bitmap( visual
, mask_data
, width
, height
);
753 wxMask
*mask
= new wxMask();
754 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
761 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
762 byte_order b_o
= RGB
;
766 if ((visual
->red_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->blue_mask
)) b_o
= RGB
;
767 else if ((visual
->red_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->green_mask
)) b_o
= RBG
;
768 else if ((visual
->blue_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->green_mask
)) b_o
= BRG
;
769 else if ((visual
->blue_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->red_mask
)) b_o
= BGR
;
770 else if ((visual
->green_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->blue_mask
)) b_o
= GRB
;
771 else if ((visual
->green_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->red_mask
)) b_o
= GBR
;
774 int r_mask
= image
.GetMaskRed();
775 int g_mask
= image
.GetMaskGreen();
776 int b_mask
= image
.GetMaskBlue();
778 unsigned char* data
= image
.GetData();
781 for (int y
= 0; y
< height
; y
++)
783 for (int x
= 0; x
< width
; x
++)
794 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
795 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
797 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
805 if (wxTheApp
->m_colorCube
)
807 pixel
= wxTheApp
->m_colorCube
[ ((r
& 0xf8) << 7) + ((g
& 0xf8) << 2) + ((b
& 0xf8) >> 3) ];
811 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
812 GdkColor
*colors
= cmap
->colors
;
813 int max
= 3 * (65536);
815 for (int i
= 0; i
< cmap
->size
; i
++)
817 int rdiff
= (r
<< 8) - colors
[i
].red
;
818 int gdiff
= (g
<< 8) - colors
[i
].green
;
819 int bdiff
= (b
<< 8) - colors
[i
].blue
;
820 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
821 if (sum
< max
) { pixel
= i
; max
= sum
; }
825 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
834 case RGB
: pixel
= ((r
& 0xf0) << 4) | (g
& 0xf0) | ((b
& 0xf0) >> 4); break;
835 case RBG
: pixel
= ((r
& 0xf0) << 4) | (b
& 0xf0) | ((g
& 0xf0) >> 4); break;
836 case GRB
: pixel
= ((g
& 0xf0) << 4) | (r
& 0xf0) | ((b
& 0xf0) >> 4); break;
837 case GBR
: pixel
= ((g
& 0xf0) << 4) | (b
& 0xf0) | ((r
& 0xf0) >> 4); break;
838 case BRG
: pixel
= ((b
& 0xf0) << 4) | (r
& 0xf0) | ((g
& 0xf0) >> 4); break;
839 case BGR
: pixel
= ((b
& 0xf0) << 4) | (g
& 0xf0) | ((r
& 0xf0) >> 4); break;
841 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
849 case RGB
: pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
850 case RBG
: pixel
= ((r
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
851 case GRB
: pixel
= ((g
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
852 case GBR
: pixel
= ((g
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
853 case BRG
: pixel
= ((b
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
854 case BGR
: pixel
= ((b
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
856 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
861 // I actually don't know if for 16-bit displays, it is alway the green
862 // component or the second component which has 6 bits.
866 case RGB
: pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
867 case RBG
: pixel
= ((r
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
868 case GRB
: pixel
= ((g
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
869 case GBR
: pixel
= ((g
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
870 case BRG
: pixel
= ((b
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
871 case BGR
: pixel
= ((b
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
873 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
882 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
883 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
884 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
885 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
886 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
887 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
889 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
899 GdkGC
*data_gc
= gdk_gc_new( GetPixmap() );
901 gdk_draw_image( GetPixmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
903 gdk_image_destroy( data_image
);
904 gdk_gc_unref( data_gc
);
910 GdkGC
*mask_gc
= gdk_gc_new( GetMask()->GetBitmap() );
912 gdk_draw_image( GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
914 gdk_image_destroy( mask_image
);
915 gdk_gc_unref( mask_gc
);
921 bool wxBitmap::CreateFromImageAsPixbuf(const wxImage
& image
)
923 int width
= image
.GetWidth();
924 int height
= image
.GetHeight();
926 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
928 8 /* bits per sample */,
933 wxASSERT( image
.HasAlpha() ); // for now
934 wxASSERT( gdk_pixbuf_get_n_channels(pixbuf
) == 4 );
935 wxASSERT( gdk_pixbuf_get_width(pixbuf
) == width
);
936 wxASSERT( gdk_pixbuf_get_height(pixbuf
) == height
);
938 M_BMPDATA
->m_pixbuf
= pixbuf
;
941 SetDepth(wxTheApp
->GetGdkVisual()->depth
);
944 unsigned char *in
= image
.GetData();
945 unsigned char *out
= gdk_pixbuf_get_pixels(pixbuf
);
946 unsigned char *alpha
= image
.GetAlpha();
948 int rowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
950 for (int y
= 0; y
< height
; y
++, out
+= rowinc
)
952 for (int x
= 0; x
< width
; x
++, alpha
++, out
+= 4, in
+= 3)
964 wxImage
wxBitmap::ConvertToImage() const
968 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
970 image
.Create(GetWidth(), GetHeight());
971 unsigned char *data
= image
.GetData();
975 wxFAIL_MSG( wxT("couldn't create image") );
981 GdkPixbuf
*pixbuf
= GetPixbuf();
982 wxASSERT( gdk_pixbuf_get_has_alpha(pixbuf
) );
989 unsigned char *alpha
= image
.GetAlpha();
990 unsigned char *in
= gdk_pixbuf_get_pixels(pixbuf
);
991 unsigned char *out
= data
;
992 int rowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * w
;
994 for (int y
= 0; y
< h
; y
++, in
+= rowinc
)
996 for (int x
= 0; x
< w
; x
++, in
+= 4, out
+= 3, alpha
++)
1007 // the colour used as transparent one in wxImage and the one it is
1008 // replaced with when it really occurs in the bitmap
1009 static const int MASK_RED
= 1;
1010 static const int MASK_GREEN
= 2;
1011 static const int MASK_BLUE
= 3;
1012 static const int MASK_BLUE_REPLACEMENT
= 2;
1014 GdkImage
*gdk_image
= (GdkImage
*) NULL
;
1018 gdk_image
= gdk_image_get( GetPixmap(),
1020 GetWidth(), GetHeight() );
1022 else if (GetBitmap())
1024 gdk_image
= gdk_image_get( GetBitmap(),
1026 GetWidth(), GetHeight() );
1030 wxFAIL_MSG( wxT("Ill-formed bitmap") );
1033 wxCHECK_MSG( gdk_image
, wxNullImage
, wxT("couldn't create image") );
1035 GdkImage
*gdk_image_mask
= (GdkImage
*) NULL
;
1038 gdk_image_mask
= gdk_image_get( GetMask()->GetBitmap(),
1040 GetWidth(), GetHeight() );
1042 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
1046 int red_shift_right
= 0;
1047 int green_shift_right
= 0;
1048 int blue_shift_right
= 0;
1049 int red_shift_left
= 0;
1050 int green_shift_left
= 0;
1051 int blue_shift_left
= 0;
1052 bool use_shift
= false;
1056 GdkVisual
*visual
= gdk_window_get_visual( GetPixmap() );
1058 visual
= wxTheApp
->GetGdkVisual();
1060 bpp
= visual
->depth
;
1062 bpp
= visual
->red_prec
+ visual
->green_prec
+ visual
->blue_prec
;
1063 red_shift_right
= visual
->red_shift
;
1064 red_shift_left
= 8-visual
->red_prec
;
1065 green_shift_right
= visual
->green_shift
;
1066 green_shift_left
= 8-visual
->green_prec
;
1067 blue_shift_right
= visual
->blue_shift
;
1068 blue_shift_left
= 8-visual
->blue_prec
;
1070 use_shift
= (visual
->type
== GDK_VISUAL_TRUE_COLOR
) || (visual
->type
== GDK_VISUAL_DIRECT_COLOR
);
1078 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
1081 for (int j
= 0; j
< GetHeight(); j
++)
1083 for (int i
= 0; i
< GetWidth(); i
++)
1085 wxUint32 pixel
= gdk_image_get_pixel( gdk_image
, i
, j
);
1103 data
[pos
] = (pixel
>> red_shift_right
) << red_shift_left
;
1104 data
[pos
+1] = (pixel
>> green_shift_right
) << green_shift_left
;
1105 data
[pos
+2] = (pixel
>> blue_shift_right
) << blue_shift_left
;
1107 else if (cmap
->colors
)
1109 data
[pos
] = cmap
->colors
[pixel
].red
>> 8;
1110 data
[pos
+1] = cmap
->colors
[pixel
].green
>> 8;
1111 data
[pos
+2] = cmap
->colors
[pixel
].blue
>> 8;
1115 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
1120 int mask_pixel
= gdk_image_get_pixel( gdk_image_mask
, i
, j
);
1121 if (mask_pixel
== 0)
1123 data
[pos
] = MASK_RED
;
1124 data
[pos
+1] = MASK_GREEN
;
1125 data
[pos
+2] = MASK_BLUE
;
1127 else if ( data
[pos
] == MASK_RED
&&
1128 data
[pos
+1] == MASK_GREEN
&&
1129 data
[pos
+2] == MASK_BLUE
)
1131 data
[pos
+2] = MASK_BLUE_REPLACEMENT
;
1139 gdk_image_destroy( gdk_image
);
1140 if (gdk_image_mask
) gdk_image_destroy( gdk_image_mask
);
1146 wxBitmap::wxBitmap( const wxString
&filename
, wxBitmapType type
)
1148 LoadFile( filename
, type
);
1151 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int WXUNUSED(depth
))
1153 if ( width
> 0 && height
> 0 )
1155 m_refData
= new wxBitmapRefData();
1157 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
1158 M_BMPDATA
->m_bitmap
= gdk_bitmap_create_from_data
1160 wxGetRootWindow()->window
,
1165 M_BMPDATA
->m_width
= width
;
1166 M_BMPDATA
->m_height
= height
;
1167 M_BMPDATA
->m_bpp
= 1;
1169 wxASSERT_MSG( M_BMPDATA
->m_bitmap
, wxT("couldn't create bitmap") );
1173 wxBitmap::~wxBitmap()
1177 bool wxBitmap::operator == ( const wxBitmap
& bmp
) const
1179 return m_refData
== bmp
.m_refData
;
1182 bool wxBitmap::operator != ( const wxBitmap
& bmp
) const
1184 return m_refData
!= bmp
.m_refData
;
1187 bool wxBitmap::Ok() const
1189 return (m_refData
!= NULL
) &&
1191 M_BMPDATA
->m_pixbuf
||
1192 M_BMPDATA
->m_bitmap
|| M_BMPDATA
->m_pixmap
1196 int wxBitmap::GetHeight() const
1198 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1200 return M_BMPDATA
->m_height
;
1203 int wxBitmap::GetWidth() const
1205 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1207 return M_BMPDATA
->m_width
;
1210 int wxBitmap::GetDepth() const
1212 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1214 return M_BMPDATA
->m_bpp
;
1217 wxMask
*wxBitmap::GetMask() const
1219 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
1221 return M_BMPDATA
->m_mask
;
1224 void wxBitmap::SetMask( wxMask
*mask
)
1226 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
1228 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
1230 M_BMPDATA
->m_mask
= mask
;
1233 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
1239 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
1241 wxCHECK_MSG( Ok() &&
1242 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1243 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) && (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
1244 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
1246 wxBitmap
ret( rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
1247 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
1251 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
1252 gdk_pixbuf_get_has_alpha(GetPixbuf()),
1253 8, rect
.width
, rect
.height
);
1254 ret
.SetPixbuf(pixbuf
);
1255 gdk_pixbuf_copy_area(GetPixbuf(),
1256 rect
.x
, rect
.y
, rect
.width
, rect
.height
,
1261 if (ret
.GetPixmap())
1263 GdkGC
*gc
= gdk_gc_new( ret
.GetPixmap() );
1264 gdk_draw_pixmap( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1265 gdk_gc_destroy( gc
);
1269 GdkGC
*gc
= gdk_gc_new( ret
.GetBitmap() );
1271 col
.pixel
= 0xFFFFFF;
1272 gdk_gc_set_foreground( gc
, &col
);
1274 gdk_gc_set_background( gc
, &col
);
1275 gdk_wx_draw_bitmap( ret
.GetBitmap(), gc
, GetBitmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1276 gdk_gc_destroy( gc
);
1282 wxMask
*mask
= new wxMask
;
1283 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, rect
.width
, rect
.height
, 1 );
1285 GdkGC
*gc
= gdk_gc_new( mask
->m_bitmap
);
1287 col
.pixel
= 0xFFFFFF;
1288 gdk_gc_set_foreground( gc
, &col
);
1290 gdk_gc_set_background( gc
, &col
);
1291 gdk_wx_draw_bitmap( mask
->m_bitmap
, gc
, M_BMPDATA
->m_mask
->m_bitmap
, rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1292 gdk_gc_destroy( gc
);
1294 ret
.SetMask( mask
);
1300 bool wxBitmap::SaveFile( const wxString
&name
, wxBitmapType type
, const wxPalette
*WXUNUSED(palette
) ) const
1302 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1304 // Try to save the bitmap via wxImage handlers:
1306 wxImage image
= ConvertToImage();
1307 if (image
.Ok()) return image
.SaveFile( name
, type
);
1313 bool wxBitmap::LoadFile( const wxString
&name
, wxBitmapType type
)
1317 if (!wxFileExists(name
))
1320 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
1322 if (type
== wxBITMAP_TYPE_XPM
)
1324 m_refData
= new wxBitmapRefData();
1326 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
1328 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm
1330 wxGetRootWindow()->window
,
1338 M_BMPDATA
->m_mask
= new wxMask();
1339 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
1342 gdk_window_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
1344 M_BMPDATA
->m_bpp
= visual
->depth
;
1346 else // try if wxImage can load it
1349 if ( !image
.LoadFile( name
, type
) || !image
.Ok() )
1352 *this = wxBitmap(image
);
1359 wxPalette
*wxBitmap::GetPalette() const
1362 return (wxPalette
*) NULL
;
1364 return M_BMPDATA
->m_palette
;
1367 void wxBitmap::SetPalette(const wxPalette
& WXUNUSED(palette
))
1371 #endif // wxUSE_PALETTE
1373 void wxBitmap::SetHeight( int height
)
1376 m_refData
= new wxBitmapRefData();
1378 M_BMPDATA
->m_height
= height
;
1381 void wxBitmap::SetWidth( int width
)
1384 m_refData
= new wxBitmapRefData();
1386 M_BMPDATA
->m_width
= width
;
1389 void wxBitmap::SetDepth( int depth
)
1392 m_refData
= new wxBitmapRefData();
1394 M_BMPDATA
->m_bpp
= depth
;
1397 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
1400 m_refData
= new wxBitmapRefData();
1402 M_BMPDATA
->m_pixmap
= pixmap
;
1403 PurgeOtherRepresentations(Pixmap
);
1406 void wxBitmap::SetBitmap( GdkPixmap
*bitmap
)
1409 m_refData
= new wxBitmapRefData();
1411 M_BMPDATA
->m_bitmap
= bitmap
;
1412 PurgeOtherRepresentations(Pixmap
);
1415 GdkPixmap
*wxBitmap::GetPixmap() const
1417 wxCHECK_MSG( Ok(), (GdkPixmap
*) NULL
, wxT("invalid bitmap") );
1419 // create the pixmap on the fly if we use Pixbuf representation:
1420 if (HasPixbuf() && !HasPixmap())
1422 delete M_BMPDATA
->m_mask
;
1423 M_BMPDATA
->m_mask
= new wxMask();
1424 gdk_pixbuf_render_pixmap_and_mask(M_BMPDATA
->m_pixbuf
,
1425 &M_BMPDATA
->m_pixmap
,
1426 &M_BMPDATA
->m_mask
->m_bitmap
,
1430 return M_BMPDATA
->m_pixmap
;
1433 bool wxBitmap::HasPixmap() const
1435 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1437 return M_BMPDATA
->m_pixmap
!= NULL
;
1440 GdkBitmap
*wxBitmap::GetBitmap() const
1442 wxCHECK_MSG( Ok(), (GdkBitmap
*) NULL
, wxT("invalid bitmap") );
1444 return M_BMPDATA
->m_bitmap
;
1447 GdkPixbuf
*wxBitmap::GetPixbuf() const
1449 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
1451 if (HasPixmap() && !HasPixbuf())
1453 int width
= GetWidth();
1454 int height
= GetHeight();
1456 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
1459 M_BMPDATA
->m_pixbuf
=
1460 gdk_pixbuf_get_from_drawable(pixbuf
, M_BMPDATA
->m_pixmap
, NULL
,
1461 0, 0, 0, 0, width
, height
);
1463 // apply the mask to created pixbuf:
1464 if (M_BMPDATA
->m_pixbuf
&& M_BMPDATA
->m_mask
)
1467 gdk_pixbuf_get_from_drawable(NULL
,
1468 M_BMPDATA
->m_mask
->GetBitmap(),
1470 0, 0, 0, 0, width
, height
);
1473 guchar
*bmp
= gdk_pixbuf_get_pixels(pixbuf
);
1474 guchar
*mask
= gdk_pixbuf_get_pixels(pmask
);
1475 int bmprowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
1476 int maskrowinc
= gdk_pixbuf_get_rowstride(pmask
) - 3 * width
;
1478 for (int y
= 0; y
< height
;
1479 y
++, bmp
+= bmprowinc
, mask
+= maskrowinc
)
1481 for (int x
= 0; x
< width
; x
++, bmp
+= 4, mask
+= 3)
1483 if (mask
[0] == 0 /*black pixel*/)
1488 gdk_pixbuf_unref(pmask
);
1493 return M_BMPDATA
->m_pixbuf
;
1496 bool wxBitmap::HasPixbuf() const
1498 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1500 return M_BMPDATA
->m_pixbuf
!= NULL
;
1503 void wxBitmap::SetPixbuf( GdkPixbuf
*pixbuf
)
1506 m_refData
= new wxBitmapRefData();
1508 M_BMPDATA
->m_pixbuf
= pixbuf
;
1509 PurgeOtherRepresentations(Pixbuf
);
1512 void wxBitmap::PurgeOtherRepresentations(wxBitmap::Representation keep
)
1514 if (keep
== Pixmap
&& HasPixbuf())
1516 gdk_pixbuf_unref( M_BMPDATA
->m_pixbuf
);
1517 M_BMPDATA
->m_pixbuf
= NULL
;
1519 if (keep
== Pixbuf
&& HasPixmap())
1521 gdk_pixmap_unref( M_BMPDATA
->m_pixmap
);
1522 M_BMPDATA
->m_pixmap
= NULL
;
1526 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1531 GdkPixbuf
*pixbuf
= GetPixbuf();
1536 if (gdk_pixbuf_get_has_alpha( pixbuf
))
1537 wxPrintf( wxT("Has alpha\n") );
1539 wxPrintf( wxT("No alpha.\n") );
1542 data
.m_height
= gdk_pixbuf_get_height( pixbuf
);
1543 data
.m_width
= gdk_pixbuf_get_width( pixbuf
);
1544 data
.m_stride
= gdk_pixbuf_get_rowstride( pixbuf
);
1546 return gdk_pixbuf_get_pixels( pixbuf
);
1549 void wxBitmap::UngetRawData(wxPixelDataBase
& WXUNUSED(data
))
1554 bool wxBitmap::HasAlpha() const
1559 void wxBitmap::UseAlpha()
1564 //-----------------------------------------------------------------------------
1566 //-----------------------------------------------------------------------------
1568 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
,wxBitmapHandlerBase
)
1570 wxBitmapHandler::~wxBitmapHandler()
1574 bool wxBitmapHandler::Create(wxBitmap
* WXUNUSED(bitmap
),
1575 void * WXUNUSED(data
),
1576 long WXUNUSED(type
),
1577 int WXUNUSED(width
),
1578 int WXUNUSED(height
),
1579 int WXUNUSED(depth
))
1581 wxFAIL_MSG( _T("not implemented") );
1586 bool wxBitmapHandler::LoadFile(wxBitmap
* WXUNUSED(bitmap
),
1587 const wxString
& WXUNUSED(name
),
1588 long WXUNUSED(flags
),
1589 int WXUNUSED(desiredWidth
),
1590 int WXUNUSED(desiredHeight
))
1592 wxFAIL_MSG( _T("not implemented") );
1597 bool wxBitmapHandler::SaveFile(const wxBitmap
* WXUNUSED(bitmap
),
1598 const wxString
& WXUNUSED(name
),
1600 const wxPalette
* WXUNUSED(palette
))
1602 wxFAIL_MSG( _T("not implemented") );
1607 /* static */ void wxBitmap::InitStandardHandlers()
1609 // TODO: Insert handler based on GdkPixbufs handler later