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"
13 #include "wx/bitmap.h"
17 #include "wx/dcmemory.h"
18 #include "wx/palette.h"
22 #include "wx/filefn.h"
25 #include "wx/rawbmp.h"
26 // need this to get gdk_image_new_bitmap()
27 #define GDK_ENABLE_BROKEN
33 #include <gdk/gdkimage.h>
37 extern void gdk_wx_draw_bitmap (GdkDrawable
*drawable
,
47 //-----------------------------------------------------------------------------
49 //-----------------------------------------------------------------------------
51 extern GtkWidget
*wxGetRootWindow();
53 //-----------------------------------------------------------------------------
55 //-----------------------------------------------------------------------------
57 IMPLEMENT_DYNAMIC_CLASS(wxMask
,wxObject
)
61 m_bitmap
= (GdkBitmap
*) NULL
;
64 wxMask::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
66 m_bitmap
= (GdkBitmap
*) NULL
;
67 Create( bitmap
, colour
);
71 wxMask::wxMask( const wxBitmap
& bitmap
, int paletteIndex
)
73 m_bitmap
= (GdkBitmap
*) NULL
;
74 Create( bitmap
, paletteIndex
);
76 #endif // wxUSE_PALETTE
78 wxMask::wxMask( const wxBitmap
& bitmap
)
80 m_bitmap
= (GdkBitmap
*) NULL
;
87 g_object_unref (m_bitmap
);
90 bool wxMask::Create( const wxBitmap
& bitmap
,
91 const wxColour
& colour
)
95 g_object_unref (m_bitmap
);
96 m_bitmap
= (GdkBitmap
*) NULL
;
99 wxImage image
= bitmap
.ConvertToImage();
100 if (!image
.Ok()) return false;
102 m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, image
.GetWidth(), image
.GetHeight(), 1 );
103 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
110 gdk_gc_set_foreground( gc
, &color
);
111 gdk_gc_set_fill( gc
, GDK_SOLID
);
112 gdk_draw_rectangle( m_bitmap
, gc
, TRUE
, 0, 0, image
.GetWidth(), image
.GetHeight() );
114 unsigned char *data
= image
.GetData();
117 unsigned char red
= colour
.Red();
118 unsigned char green
= colour
.Green();
119 unsigned char blue
= colour
.Blue();
121 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
123 int bpp
= visual
->depth
;
124 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800))
129 green
= green
& 0xf8;
135 green
= green
& 0xfc;
141 green
= green
& 0xf0;
149 gdk_gc_set_foreground( gc
, &color
);
151 for (int j
= 0; j
< image
.GetHeight(); j
++)
155 for (i
= 0; i
< image
.GetWidth(); i
++)
157 if ((data
[index
] == red
) &&
158 (data
[index
+1] == green
) &&
159 (data
[index
+2] == blue
))
168 gdk_draw_line( m_bitmap
, gc
, start_x
, j
, i
-1, j
);
175 gdk_draw_line( m_bitmap
, gc
, start_x
, j
, i
, j
);
184 bool wxMask::Create( const wxBitmap
& bitmap
, int paletteIndex
)
187 wxPalette
*pal
= bitmap
.GetPalette();
189 wxCHECK_MSG( pal
, false, wxT("Cannot create mask from bitmap without palette") );
191 pal
->GetRGB(paletteIndex
, &r
, &g
, &b
);
193 return Create(bitmap
, wxColour(r
, g
, b
));
195 #endif // wxUSE_PALETTE
197 bool wxMask::Create( const wxBitmap
& bitmap
)
201 g_object_unref (m_bitmap
);
202 m_bitmap
= (GdkBitmap
*) NULL
;
205 if (!bitmap
.Ok()) return false;
207 wxCHECK_MSG( bitmap
.GetBitmap(), false, wxT("Cannot create mask from colour bitmap") );
209 m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, bitmap
.GetWidth(), bitmap
.GetHeight(), 1 );
211 if (!m_bitmap
) return false;
213 GdkGC
*gc
= gdk_gc_new( m_bitmap
);
215 gdk_wx_draw_bitmap( m_bitmap
, gc
, bitmap
.GetBitmap(), 0, 0, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight() );
222 GdkBitmap
*wxMask::GetBitmap() const
227 //-----------------------------------------------------------------------------
229 //-----------------------------------------------------------------------------
231 class wxBitmapRefData
: public wxObjectRefData
244 wxPalette
*m_palette
;
247 wxBitmapRefData::wxBitmapRefData()
249 m_pixmap
= (GdkPixmap
*) NULL
;
250 m_bitmap
= (GdkBitmap
*) NULL
;
251 m_pixbuf
= (GdkPixbuf
*) NULL
;
252 m_mask
= (wxMask
*) NULL
;
256 m_palette
= (wxPalette
*) NULL
;
259 wxBitmapRefData::~wxBitmapRefData()
262 g_object_unref (m_pixmap
);
264 g_object_unref (m_bitmap
);
266 g_object_unref (m_pixbuf
);
270 #endif // wxUSE_PALETTE
273 //-----------------------------------------------------------------------------
275 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
277 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
283 wxBitmap::wxBitmap( int width
, int height
, int depth
)
285 Create( width
, height
, depth
);
288 bool wxBitmap::Create( int width
, int height
, int depth
)
292 if ( width
<= 0 || height
<= 0 )
297 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
300 depth
= visual
->depth
;
302 wxCHECK_MSG( (depth
== visual
->depth
) || (depth
== 1) || (depth
== 32), false,
303 wxT("invalid bitmap depth") );
305 m_refData
= new wxBitmapRefData();
306 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
307 M_BMPDATA
->m_width
= width
;
308 M_BMPDATA
->m_height
= height
;
311 M_BMPDATA
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
312 M_BMPDATA
->m_bpp
= 1;
314 else if (depth
== 32)
316 M_BMPDATA
->m_pixbuf
= gdk_pixbuf_new( GDK_COLORSPACE_RGB
, true,
318 M_BMPDATA
->m_bpp
= 32;
322 M_BMPDATA
->m_pixmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, depth
);
323 M_BMPDATA
->m_bpp
= visual
->depth
;
329 bool wxBitmap::CreateFromXpm( const char **bits
)
333 wxCHECK_MSG( bits
!= NULL
, false, wxT("invalid bitmap data") );
335 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
337 m_refData
= new wxBitmapRefData();
339 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
341 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm_d( wxGetRootWindow()->window
, &mask
, NULL
, (gchar
**) bits
);
343 wxCHECK_MSG( M_BMPDATA
->m_pixmap
, false, wxT("couldn't create pixmap") );
347 M_BMPDATA
->m_mask
= new wxMask();
348 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
351 gdk_drawable_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
353 M_BMPDATA
->m_bpp
= visual
->depth
; // Can we get a different depth from create_from_xpm_d() ?
358 wxBitmap
wxBitmap::Rescale( int clipx
, int clipy
, int clipwidth
, int clipheight
, int newx
, int newy
)
360 wxCHECK_MSG( Ok(), wxNullBitmap
, wxT("invalid bitmap") );
362 if (newy
==M_BMPDATA
->m_width
&& newy
==M_BMPDATA
->m_height
)
365 int width
= wxMax(newx
, 1);
366 int height
= wxMax(newy
, 1);
367 width
= wxMin(width
, clipwidth
);
368 height
= wxMin(height
, clipheight
);
375 bmp
.SetHeight(height
);
376 bmp
.SetDepth(GetDepth());
377 bmp
.SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
378 gdk_pixbuf_get_has_alpha(GetPixbuf()),
380 gdk_pixbuf_scale(GetPixbuf(), bmp
.GetPixbuf(),
383 (double)newx
/GetWidth(), (double)newy
/GetHeight(),
384 GDK_INTERP_BILINEAR
);
388 GdkImage
*img
= (GdkImage
*) NULL
;
390 img
= gdk_image_get( GetPixmap(), 0, 0, GetWidth(), GetHeight() );
391 else if (GetBitmap())
392 img
= gdk_image_get( GetBitmap(), 0, 0, GetWidth(), GetHeight() );
394 wxFAIL_MSG( wxT("Ill-formed bitmap") );
396 wxCHECK_MSG( img
, wxNullBitmap
, wxT("couldn't create image") );
402 GdkPixmap
*dstpix
= NULL
;
405 GdkVisual
*visual
= gdk_drawable_get_visual( GetPixmap() );
407 visual
= wxTheApp
->GetGdkVisual();
410 bmp
= wxBitmap(width
,height
,bpp
);
411 dstpix
= bmp
.GetPixmap();
412 gc
= gdk_gc_new( dstpix
);
416 long dstbyteperline
= 0;
421 dstbyteperline
= width
/8*M_BMPDATA
->m_bpp
;
422 if (width
*M_BMPDATA
->m_bpp
% 8 != 0)
424 dst
= (char*) malloc(dstbyteperline
*height
);
427 // be careful to use the right scaling factor
428 float scx
= (float)M_BMPDATA
->m_width
/(float)newx
;
429 float scy
= (float)M_BMPDATA
->m_height
/(float)newy
;
430 // prepare accel-tables
431 int *tablex
= (int *)calloc(width
,sizeof(int));
432 int *tabley
= (int *)calloc(height
,sizeof(int));
434 // accel table filled with clipped values
435 for (int x
= 0; x
< width
; x
++)
436 tablex
[x
] = (int) (scx
* (x
+clipx
));
437 for (int y
= 0; y
< height
; y
++)
438 tabley
[y
] = (int) (scy
* (y
+clipy
));
440 // Main rescaling routine starts here
441 for (int h
= 0; h
< height
; h
++)
445 guint32 old_pixval
= 0;
447 for (int w
= 0; w
< width
; w
++)
455 pixval
= gdk_image_get_pixel( img
, x
, tabley
[h
] );
465 char shift
= bit
<< (w
% 8);
471 dst
[h
*dstbyteperline
+w
/8] = outbyte
;
479 gdk_gc_set_foreground( gc
, &col
);
480 gdk_draw_point( dstpix
, gc
, w
, h
);
484 // do not forget the last byte
485 if ( dst
&& (width
% 8 != 0) )
486 dst
[h
*dstbyteperline
+width
/8] = outbyte
;
489 g_object_unref (img
);
490 if (gc
) g_object_unref (gc
);
494 bmp
= wxBitmap( (const char *)dst
, width
, height
, 1 );
500 dstbyteperline
= width
/8;
503 dst
= (char*) malloc(dstbyteperline
*height
);
504 img
= gdk_image_get( GetMask()->GetBitmap(), 0, 0, GetWidth(), GetHeight() );
506 for (int h
= 0; h
< height
; h
++)
510 guint32 old_pixval
= 0;
512 for (int w
= 0; w
< width
; w
++)
520 pixval
= gdk_image_get_pixel( img
, x
, tabley
[h
] );
528 char shift
= bit
<< (w
% 8);
534 dst
[h
*dstbyteperline
+w
/8] = outbyte
;
539 // do not forget the last byte
541 dst
[h
*dstbyteperline
+width
/8] = outbyte
;
543 wxMask
* mask
= new wxMask
;
544 mask
->m_bitmap
= gdk_bitmap_create_from_data( wxGetRootWindow()->window
, (gchar
*) dst
, width
, height
);
548 g_object_unref (img
);
558 bool wxBitmap::CreateFromImage(const wxImage
& image
, int depth
)
562 wxCHECK_MSG( image
.Ok(), false, wxT("invalid image") );
563 wxCHECK_MSG( depth
== -1 || depth
== 1, false, wxT("invalid bitmap depth") );
565 if (image
.GetWidth() <= 0 || image
.GetHeight() <= 0)
568 m_refData
= new wxBitmapRefData();
572 return CreateFromImageAsBitmap(image
);
576 if (image
.HasAlpha())
577 return CreateFromImageAsPixbuf(image
);
579 return CreateFromImageAsPixmap(image
);
583 // conversion to mono bitmap:
584 bool wxBitmap::CreateFromImageAsBitmap(const wxImage
& img
)
586 // convert alpha channel to mask, if it is present:
588 image
.ConvertAlphaToMask();
590 int width
= image
.GetWidth();
591 int height
= image
.GetHeight();
596 SetBitmap( gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 ) );
600 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
602 // Create picture image
604 unsigned char *data_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
606 GdkImage
*data_image
=
607 gdk_image_new_bitmap( visual
, data_data
, width
, height
);
611 GdkImage
*mask_image
= (GdkImage
*) NULL
;
615 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
617 mask_image
= gdk_image_new_bitmap( visual
, mask_data
, width
, height
);
619 wxMask
*mask
= new wxMask();
620 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
625 int r_mask
= image
.GetMaskRed();
626 int g_mask
= image
.GetMaskGreen();
627 int b_mask
= image
.GetMaskBlue();
629 unsigned char* data
= image
.GetData();
632 for (int y
= 0; y
< height
; y
++)
634 for (int x
= 0; x
< width
; x
++)
645 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
646 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
648 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
651 if ((r
== 255) && (b
== 255) && (g
== 255))
652 gdk_image_put_pixel( data_image
, x
, y
, 1 );
654 gdk_image_put_pixel( data_image
, x
, y
, 0 );
661 GdkGC
*data_gc
= gdk_gc_new( GetBitmap() );
663 gdk_draw_image( GetBitmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
665 g_object_unref (data_image
);
666 g_object_unref (data_gc
);
672 GdkGC
*mask_gc
= gdk_gc_new( GetMask()->GetBitmap() );
674 gdk_draw_image( GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
676 g_object_unref (mask_image
);
677 g_object_unref (mask_gc
);
683 // conversion to colour bitmap:
684 bool wxBitmap::CreateFromImageAsPixmap(const wxImage
& img
)
686 // convert alpha channel to mask, if it is present:
688 image
.ConvertAlphaToMask();
690 int width
= image
.GetWidth();
691 int height
= image
.GetHeight();
696 SetPixmap( gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, -1 ) );
698 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
700 int bpp
= visual
->depth
;
704 if ((bpp
== 16) && (visual
->red_mask
!= 0xf800))
709 // We handle 8-bit bitmaps ourselves using the colour cube, 12-bit
710 // visuals are not supported by GDK so we do these ourselves, too.
711 // 15-bit and 16-bit should actually work and 24-bit certainly does.
713 if (!image
.HasMask() && (bpp
> 16))
715 if (!image
.HasMask() && (bpp
> 12))
718 static bool s_hasInitialized
= false;
720 if (!s_hasInitialized
)
723 s_hasInitialized
= true;
726 GdkGC
*gc
= gdk_gc_new( GetPixmap() );
728 gdk_draw_rgb_image( GetPixmap(),
740 // Create picture image
742 GdkImage
*data_image
=
743 gdk_image_new( GDK_IMAGE_FASTEST
, visual
, width
, height
);
747 GdkImage
*mask_image
= (GdkImage
*) NULL
;
751 unsigned char *mask_data
= (unsigned char*)malloc( ((width
>> 3)+8) * height
);
753 mask_image
= gdk_image_new_bitmap( visual
, mask_data
, width
, height
);
755 wxMask
*mask
= new wxMask();
756 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, width
, height
, 1 );
763 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
764 byte_order b_o
= RGB
;
768 if ((visual
->red_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->blue_mask
)) b_o
= RGB
;
769 else if ((visual
->red_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->green_mask
)) b_o
= RBG
;
770 else if ((visual
->blue_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->green_mask
)) b_o
= BRG
;
771 else if ((visual
->blue_mask
> visual
->green_mask
) && (visual
->green_mask
> visual
->red_mask
)) b_o
= BGR
;
772 else if ((visual
->green_mask
> visual
->red_mask
) && (visual
->red_mask
> visual
->blue_mask
)) b_o
= GRB
;
773 else if ((visual
->green_mask
> visual
->blue_mask
) && (visual
->blue_mask
> visual
->red_mask
)) b_o
= GBR
;
776 int r_mask
= image
.GetMaskRed();
777 int g_mask
= image
.GetMaskGreen();
778 int b_mask
= image
.GetMaskBlue();
780 unsigned char* data
= image
.GetData();
783 for (int y
= 0; y
< height
; y
++)
785 for (int x
= 0; x
< width
; x
++)
796 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
797 gdk_image_put_pixel( mask_image
, x
, y
, 1 );
799 gdk_image_put_pixel( mask_image
, x
, y
, 0 );
807 if (wxTheApp
->m_colorCube
)
809 pixel
= wxTheApp
->m_colorCube
[ ((r
& 0xf8) << 7) + ((g
& 0xf8) << 2) + ((b
& 0xf8) >> 3) ];
813 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
814 GdkColor
*colors
= cmap
->colors
;
815 int max
= 3 * (65536);
817 for (int i
= 0; i
< cmap
->size
; i
++)
819 int rdiff
= (r
<< 8) - colors
[i
].red
;
820 int gdiff
= (g
<< 8) - colors
[i
].green
;
821 int bdiff
= (b
<< 8) - colors
[i
].blue
;
822 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
823 if (sum
< max
) { pixel
= i
; max
= sum
; }
827 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
836 case RGB
: pixel
= ((r
& 0xf0) << 4) | (g
& 0xf0) | ((b
& 0xf0) >> 4); break;
837 case RBG
: pixel
= ((r
& 0xf0) << 4) | (b
& 0xf0) | ((g
& 0xf0) >> 4); break;
838 case GRB
: pixel
= ((g
& 0xf0) << 4) | (r
& 0xf0) | ((b
& 0xf0) >> 4); break;
839 case GBR
: pixel
= ((g
& 0xf0) << 4) | (b
& 0xf0) | ((r
& 0xf0) >> 4); break;
840 case BRG
: pixel
= ((b
& 0xf0) << 4) | (r
& 0xf0) | ((g
& 0xf0) >> 4); break;
841 case BGR
: pixel
= ((b
& 0xf0) << 4) | (g
& 0xf0) | ((r
& 0xf0) >> 4); break;
843 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
851 case RGB
: pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
852 case RBG
: pixel
= ((r
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
853 case GRB
: pixel
= ((g
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
854 case GBR
: pixel
= ((g
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
855 case BRG
: pixel
= ((b
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
856 case BGR
: pixel
= ((b
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
858 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
863 // I actually don't know if for 16-bit displays, it is alway the green
864 // component or the second component which has 6 bits.
868 case RGB
: pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
869 case RBG
: pixel
= ((r
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
870 case GRB
: pixel
= ((g
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
871 case GBR
: pixel
= ((g
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
872 case BRG
: pixel
= ((b
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
873 case BGR
: pixel
= ((b
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
875 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
884 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
885 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
886 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
887 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
888 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
889 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
891 gdk_image_put_pixel( data_image
, x
, y
, pixel
);
901 GdkGC
*data_gc
= gdk_gc_new( GetPixmap() );
903 gdk_draw_image( GetPixmap(), data_gc
, data_image
, 0, 0, 0, 0, width
, height
);
905 g_object_unref (data_image
);
906 g_object_unref (data_gc
);
912 GdkGC
*mask_gc
= gdk_gc_new( GetMask()->GetBitmap() );
914 gdk_draw_image( GetMask()->GetBitmap(), mask_gc
, mask_image
, 0, 0, 0, 0, width
, height
);
916 g_object_unref (mask_image
);
917 g_object_unref (mask_gc
);
923 bool wxBitmap::CreateFromImageAsPixbuf(const wxImage
& image
)
925 int width
= image
.GetWidth();
926 int height
= image
.GetHeight();
928 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
930 8 /* bits per sample */,
935 wxASSERT( image
.HasAlpha() ); // for now
936 wxASSERT( gdk_pixbuf_get_n_channels(pixbuf
) == 4 );
937 wxASSERT( gdk_pixbuf_get_width(pixbuf
) == width
);
938 wxASSERT( gdk_pixbuf_get_height(pixbuf
) == height
);
940 M_BMPDATA
->m_pixbuf
= pixbuf
;
943 SetDepth(wxTheApp
->GetGdkVisual()->depth
);
946 unsigned char *in
= image
.GetData();
947 unsigned char *out
= gdk_pixbuf_get_pixels(pixbuf
);
948 unsigned char *alpha
= image
.GetAlpha();
950 int rowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
952 for (int y
= 0; y
< height
; y
++, out
+= rowinc
)
954 for (int x
= 0; x
< width
; x
++, alpha
++, out
+= 4, in
+= 3)
966 wxImage
wxBitmap::ConvertToImage() const
970 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
972 image
.Create(GetWidth(), GetHeight());
973 unsigned char *data
= image
.GetData();
977 wxFAIL_MSG( wxT("couldn't create image") );
983 GdkPixbuf
*pixbuf
= GetPixbuf();
984 wxASSERT( gdk_pixbuf_get_has_alpha(pixbuf
) );
991 unsigned char *alpha
= image
.GetAlpha();
992 unsigned char *in
= gdk_pixbuf_get_pixels(pixbuf
);
993 unsigned char *out
= data
;
994 int rowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * w
;
996 for (int y
= 0; y
< h
; y
++, in
+= rowinc
)
998 for (int x
= 0; x
< w
; x
++, in
+= 4, out
+= 3, alpha
++)
1009 // the colour used as transparent one in wxImage and the one it is
1010 // replaced with when it really occurs in the bitmap
1011 static const int MASK_RED
= 1;
1012 static const int MASK_GREEN
= 2;
1013 static const int MASK_BLUE
= 3;
1014 static const int MASK_BLUE_REPLACEMENT
= 2;
1016 GdkImage
*gdk_image
= (GdkImage
*) NULL
;
1020 gdk_image
= gdk_image_get( GetPixmap(),
1022 GetWidth(), GetHeight() );
1024 else if (GetBitmap())
1026 gdk_image
= gdk_image_get( GetBitmap(),
1028 GetWidth(), GetHeight() );
1032 wxFAIL_MSG( wxT("Ill-formed bitmap") );
1035 wxCHECK_MSG( gdk_image
, wxNullImage
, wxT("couldn't create image") );
1037 GdkImage
*gdk_image_mask
= (GdkImage
*) NULL
;
1040 gdk_image_mask
= gdk_image_get( GetMask()->GetBitmap(),
1042 GetWidth(), GetHeight() );
1044 image
.SetMaskColour( MASK_RED
, MASK_GREEN
, MASK_BLUE
);
1048 int red_shift_right
= 0;
1049 int green_shift_right
= 0;
1050 int blue_shift_right
= 0;
1051 int red_shift_left
= 0;
1052 int green_shift_left
= 0;
1053 int blue_shift_left
= 0;
1054 bool use_shift
= false;
1058 GdkVisual
*visual
= gdk_drawable_get_visual( GetPixmap() );
1060 visual
= wxTheApp
->GetGdkVisual();
1062 bpp
= visual
->depth
;
1064 bpp
= visual
->red_prec
+ visual
->green_prec
+ visual
->blue_prec
;
1065 red_shift_right
= visual
->red_shift
;
1066 red_shift_left
= 8-visual
->red_prec
;
1067 green_shift_right
= visual
->green_shift
;
1068 green_shift_left
= 8-visual
->green_prec
;
1069 blue_shift_right
= visual
->blue_shift
;
1070 blue_shift_left
= 8-visual
->blue_prec
;
1072 use_shift
= (visual
->type
== GDK_VISUAL_TRUE_COLOR
) || (visual
->type
== GDK_VISUAL_DIRECT_COLOR
);
1080 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
1083 for (int j
= 0; j
< GetHeight(); j
++)
1085 for (int i
= 0; i
< GetWidth(); i
++)
1087 wxUint32 pixel
= gdk_image_get_pixel( gdk_image
, i
, j
);
1105 data
[pos
] = (pixel
>> red_shift_right
) << red_shift_left
;
1106 data
[pos
+1] = (pixel
>> green_shift_right
) << green_shift_left
;
1107 data
[pos
+2] = (pixel
>> blue_shift_right
) << blue_shift_left
;
1109 else if (cmap
->colors
)
1111 data
[pos
] = cmap
->colors
[pixel
].red
>> 8;
1112 data
[pos
+1] = cmap
->colors
[pixel
].green
>> 8;
1113 data
[pos
+2] = cmap
->colors
[pixel
].blue
>> 8;
1117 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
1122 int mask_pixel
= gdk_image_get_pixel( gdk_image_mask
, i
, j
);
1123 if (mask_pixel
== 0)
1125 data
[pos
] = MASK_RED
;
1126 data
[pos
+1] = MASK_GREEN
;
1127 data
[pos
+2] = MASK_BLUE
;
1129 else if ( data
[pos
] == MASK_RED
&&
1130 data
[pos
+1] == MASK_GREEN
&&
1131 data
[pos
+2] == MASK_BLUE
)
1133 data
[pos
+2] = MASK_BLUE_REPLACEMENT
;
1141 g_object_unref (gdk_image
);
1142 if (gdk_image_mask
) g_object_unref (gdk_image_mask
);
1148 wxBitmap::wxBitmap( const wxString
&filename
, wxBitmapType type
)
1150 LoadFile( filename
, type
);
1153 wxBitmap::wxBitmap( const char bits
[], int width
, int height
, int WXUNUSED(depth
))
1155 if ( width
> 0 && height
> 0 )
1157 m_refData
= new wxBitmapRefData();
1159 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
1160 M_BMPDATA
->m_bitmap
= gdk_bitmap_create_from_data
1162 wxGetRootWindow()->window
,
1167 M_BMPDATA
->m_width
= width
;
1168 M_BMPDATA
->m_height
= height
;
1169 M_BMPDATA
->m_bpp
= 1;
1171 wxASSERT_MSG( M_BMPDATA
->m_bitmap
, wxT("couldn't create bitmap") );
1175 wxBitmap::~wxBitmap()
1179 bool wxBitmap::operator == ( const wxBitmap
& bmp
) const
1181 return m_refData
== bmp
.m_refData
;
1184 bool wxBitmap::operator != ( const wxBitmap
& bmp
) const
1186 return m_refData
!= bmp
.m_refData
;
1189 bool wxBitmap::Ok() const
1191 return (m_refData
!= NULL
) &&
1193 M_BMPDATA
->m_pixbuf
||
1194 M_BMPDATA
->m_bitmap
|| M_BMPDATA
->m_pixmap
1198 int wxBitmap::GetHeight() const
1200 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1202 return M_BMPDATA
->m_height
;
1205 int wxBitmap::GetWidth() const
1207 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1209 return M_BMPDATA
->m_width
;
1212 int wxBitmap::GetDepth() const
1214 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1216 return M_BMPDATA
->m_bpp
;
1219 wxMask
*wxBitmap::GetMask() const
1221 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
1223 return M_BMPDATA
->m_mask
;
1226 void wxBitmap::SetMask( wxMask
*mask
)
1228 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
1230 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
1232 M_BMPDATA
->m_mask
= mask
;
1235 bool wxBitmap::CopyFromIcon(const wxIcon
& icon
)
1241 wxBitmap
wxBitmap::GetSubBitmap( const wxRect
& rect
) const
1243 wxCHECK_MSG( Ok() &&
1244 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1245 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) && (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
1246 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
1248 wxBitmap
ret( rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
1249 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
1253 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
1254 gdk_pixbuf_get_has_alpha(GetPixbuf()),
1255 8, rect
.width
, rect
.height
);
1256 ret
.SetPixbuf(pixbuf
);
1257 gdk_pixbuf_copy_area(GetPixbuf(),
1258 rect
.x
, rect
.y
, rect
.width
, rect
.height
,
1263 if (ret
.GetPixmap())
1265 GdkGC
*gc
= gdk_gc_new( ret
.GetPixmap() );
1266 gdk_draw_drawable( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1267 g_object_unref (gc
);
1271 GdkGC
*gc
= gdk_gc_new( ret
.GetBitmap() );
1273 col
.pixel
= 0xFFFFFF;
1274 gdk_gc_set_foreground( gc
, &col
);
1276 gdk_gc_set_background( gc
, &col
);
1277 gdk_wx_draw_bitmap( ret
.GetBitmap(), gc
, GetBitmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1278 g_object_unref (gc
);
1284 wxMask
*mask
= new wxMask
;
1285 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, rect
.width
, rect
.height
, 1 );
1287 GdkGC
*gc
= gdk_gc_new( mask
->m_bitmap
);
1289 col
.pixel
= 0xFFFFFF;
1290 gdk_gc_set_foreground( gc
, &col
);
1292 gdk_gc_set_background( gc
, &col
);
1293 gdk_wx_draw_bitmap( mask
->m_bitmap
, gc
, M_BMPDATA
->m_mask
->m_bitmap
, rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1294 g_object_unref (gc
);
1296 ret
.SetMask( mask
);
1302 bool wxBitmap::SaveFile( const wxString
&name
, wxBitmapType type
, const wxPalette
*WXUNUSED(palette
) ) const
1304 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1306 // Try to save the bitmap via wxImage handlers:
1308 wxImage image
= ConvertToImage();
1309 if (image
.Ok()) return image
.SaveFile( name
, type
);
1315 bool wxBitmap::LoadFile( const wxString
&name
, wxBitmapType type
)
1319 if (!wxFileExists(name
))
1322 GdkVisual
*visual
= wxTheApp
->GetGdkVisual();
1324 if (type
== wxBITMAP_TYPE_XPM
)
1326 m_refData
= new wxBitmapRefData();
1328 GdkBitmap
*mask
= (GdkBitmap
*) NULL
;
1330 M_BMPDATA
->m_pixmap
= gdk_pixmap_create_from_xpm
1332 wxGetRootWindow()->window
,
1340 M_BMPDATA
->m_mask
= new wxMask();
1341 M_BMPDATA
->m_mask
->m_bitmap
= mask
;
1344 gdk_drawable_get_size( M_BMPDATA
->m_pixmap
, &(M_BMPDATA
->m_width
), &(M_BMPDATA
->m_height
) );
1346 M_BMPDATA
->m_bpp
= visual
->depth
;
1348 else // try if wxImage can load it
1351 if ( !image
.LoadFile( name
, type
) || !image
.Ok() )
1354 *this = wxBitmap(image
);
1361 wxPalette
*wxBitmap::GetPalette() const
1364 return (wxPalette
*) NULL
;
1366 return M_BMPDATA
->m_palette
;
1369 void wxBitmap::SetPalette(const wxPalette
& WXUNUSED(palette
))
1373 #endif // wxUSE_PALETTE
1375 void wxBitmap::SetHeight( int height
)
1378 m_refData
= new wxBitmapRefData();
1380 M_BMPDATA
->m_height
= height
;
1383 void wxBitmap::SetWidth( int width
)
1386 m_refData
= new wxBitmapRefData();
1388 M_BMPDATA
->m_width
= width
;
1391 void wxBitmap::SetDepth( int depth
)
1394 m_refData
= new wxBitmapRefData();
1396 M_BMPDATA
->m_bpp
= depth
;
1399 void wxBitmap::SetPixmap( GdkPixmap
*pixmap
)
1402 m_refData
= new wxBitmapRefData();
1404 M_BMPDATA
->m_pixmap
= pixmap
;
1405 PurgeOtherRepresentations(Pixmap
);
1408 void wxBitmap::SetBitmap( GdkPixmap
*bitmap
)
1411 m_refData
= new wxBitmapRefData();
1413 M_BMPDATA
->m_bitmap
= bitmap
;
1414 PurgeOtherRepresentations(Pixmap
);
1417 GdkPixmap
*wxBitmap::GetPixmap() const
1419 wxCHECK_MSG( Ok(), (GdkPixmap
*) NULL
, wxT("invalid bitmap") );
1421 // create the pixmap on the fly if we use Pixbuf representation:
1422 if (HasPixbuf() && !HasPixmap())
1424 delete M_BMPDATA
->m_mask
;
1425 M_BMPDATA
->m_mask
= new wxMask();
1426 gdk_pixbuf_render_pixmap_and_mask(M_BMPDATA
->m_pixbuf
,
1427 &M_BMPDATA
->m_pixmap
,
1428 &M_BMPDATA
->m_mask
->m_bitmap
,
1432 return M_BMPDATA
->m_pixmap
;
1435 bool wxBitmap::HasPixmap() const
1437 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1439 return M_BMPDATA
->m_pixmap
!= NULL
;
1442 GdkBitmap
*wxBitmap::GetBitmap() const
1444 wxCHECK_MSG( Ok(), (GdkBitmap
*) NULL
, wxT("invalid bitmap") );
1446 return M_BMPDATA
->m_bitmap
;
1449 GdkPixbuf
*wxBitmap::GetPixbuf() const
1451 wxCHECK_MSG( Ok(), NULL
, wxT("invalid bitmap") );
1453 if (HasPixmap() && !HasPixbuf())
1455 int width
= GetWidth();
1456 int height
= GetHeight();
1458 GdkPixbuf
*pixbuf
= gdk_pixbuf_new(GDK_COLORSPACE_RGB
,
1461 M_BMPDATA
->m_pixbuf
=
1462 gdk_pixbuf_get_from_drawable(pixbuf
, M_BMPDATA
->m_pixmap
, NULL
,
1463 0, 0, 0, 0, width
, height
);
1465 // apply the mask to created pixbuf:
1466 if (M_BMPDATA
->m_pixbuf
&& M_BMPDATA
->m_mask
)
1469 gdk_pixbuf_get_from_drawable(NULL
,
1470 M_BMPDATA
->m_mask
->GetBitmap(),
1472 0, 0, 0, 0, width
, height
);
1475 guchar
*bmp
= gdk_pixbuf_get_pixels(pixbuf
);
1476 guchar
*mask
= gdk_pixbuf_get_pixels(pmask
);
1477 int bmprowinc
= gdk_pixbuf_get_rowstride(pixbuf
) - 4 * width
;
1478 int maskrowinc
= gdk_pixbuf_get_rowstride(pmask
) - 3 * width
;
1480 for (int y
= 0; y
< height
;
1481 y
++, bmp
+= bmprowinc
, mask
+= maskrowinc
)
1483 for (int x
= 0; x
< width
; x
++, bmp
+= 4, mask
+= 3)
1485 if (mask
[0] == 0 /*black pixel*/)
1490 g_object_unref (pmask
);
1495 return M_BMPDATA
->m_pixbuf
;
1498 bool wxBitmap::HasPixbuf() const
1500 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1502 return M_BMPDATA
->m_pixbuf
!= NULL
;
1505 void wxBitmap::SetPixbuf( GdkPixbuf
*pixbuf
)
1508 m_refData
= new wxBitmapRefData();
1510 M_BMPDATA
->m_pixbuf
= pixbuf
;
1511 PurgeOtherRepresentations(Pixbuf
);
1514 void wxBitmap::PurgeOtherRepresentations(wxBitmap::Representation keep
)
1516 if (keep
== Pixmap
&& HasPixbuf())
1518 g_object_unref (M_BMPDATA
->m_pixbuf
);
1519 M_BMPDATA
->m_pixbuf
= NULL
;
1521 if (keep
== Pixbuf
&& HasPixmap())
1523 g_object_unref (M_BMPDATA
->m_pixmap
);
1524 M_BMPDATA
->m_pixmap
= NULL
;
1528 void *wxBitmap::GetRawData(wxPixelDataBase
& data
, int bpp
)
1533 GdkPixbuf
*pixbuf
= GetPixbuf();
1538 if (gdk_pixbuf_get_has_alpha( pixbuf
))
1539 wxPrintf( wxT("Has alpha\n") );
1541 wxPrintf( wxT("No alpha.\n") );
1544 data
.m_height
= gdk_pixbuf_get_height( pixbuf
);
1545 data
.m_width
= gdk_pixbuf_get_width( pixbuf
);
1546 data
.m_stride
= gdk_pixbuf_get_rowstride( pixbuf
);
1548 return gdk_pixbuf_get_pixels( pixbuf
);
1551 void wxBitmap::UngetRawData(wxPixelDataBase
& WXUNUSED(data
))
1556 bool wxBitmap::HasAlpha() const
1561 void wxBitmap::UseAlpha()
1566 //-----------------------------------------------------------------------------
1568 //-----------------------------------------------------------------------------
1570 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler
,wxBitmapHandlerBase
)
1572 wxBitmapHandler::~wxBitmapHandler()
1576 bool wxBitmapHandler::Create(wxBitmap
* WXUNUSED(bitmap
),
1577 void * WXUNUSED(data
),
1578 long WXUNUSED(type
),
1579 int WXUNUSED(width
),
1580 int WXUNUSED(height
),
1581 int WXUNUSED(depth
))
1583 wxFAIL_MSG( _T("not implemented") );
1588 bool wxBitmapHandler::LoadFile(wxBitmap
* WXUNUSED(bitmap
),
1589 const wxString
& WXUNUSED(name
),
1590 long WXUNUSED(flags
),
1591 int WXUNUSED(desiredWidth
),
1592 int WXUNUSED(desiredHeight
))
1594 wxFAIL_MSG( _T("not implemented") );
1599 bool wxBitmapHandler::SaveFile(const wxBitmap
* WXUNUSED(bitmap
),
1600 const wxString
& WXUNUSED(name
),
1602 const wxPalette
* WXUNUSED(palette
))
1604 wxFAIL_MSG( _T("not implemented") );
1609 /* static */ void wxBitmap::InitStandardHandlers()
1611 // TODO: Insert handler based on GdkPixbufs handler later