added alpha channel support to wxGTK2's wxBitmap
[wxWidgets.git] / src / gtk / bitmap.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: bitmap.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // RCS-ID: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "bitmap.h"
12 #endif
13
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
16
17 #include "wx/defs.h"
18
19 #include "wx/palette.h"
20 #include "wx/bitmap.h"
21 #include "wx/icon.h"
22 #include "wx/filefn.h"
23 #include "wx/image.h"
24 #include "wx/dcmemory.h"
25 #include "wx/app.h"
26
27 #ifdef __WXGTK20__
28 // need this to get gdk_image_new_bitmap()
29 #define GDK_ENABLE_BROKEN
30 #endif
31
32 #include <gdk/gdk.h>
33 #include <gtk/gtk.h>
34 #include <gdk/gdkx.h>
35
36 #ifdef __WXGTK20__
37 #include <gdk/gdkimage.h>
38 #else // GTK+ 1.2
39 #include <gdk/gdkrgb.h>
40 #endif // GTK+ 2.0/1.2
41
42 #include <math.h>
43
44 extern void gdk_wx_draw_bitmap (GdkDrawable *drawable,
45 GdkGC *gc,
46 GdkDrawable *src,
47 gint xsrc,
48 gint ysrc,
49 gint xdest,
50 gint ydest,
51 gint width,
52 gint height);
53
54 //-----------------------------------------------------------------------------
55 // data
56 //-----------------------------------------------------------------------------
57
58 extern GtkWidget *wxGetRootWindow();
59
60 //-----------------------------------------------------------------------------
61 // wxMask
62 //-----------------------------------------------------------------------------
63
64 IMPLEMENT_DYNAMIC_CLASS(wxMask,wxObject)
65
66 wxMask::wxMask()
67 {
68 m_bitmap = (GdkBitmap *) NULL;
69 }
70
71 wxMask::wxMask( const wxBitmap& bitmap, const wxColour& colour )
72 {
73 m_bitmap = (GdkBitmap *) NULL;
74 Create( bitmap, colour );
75 }
76
77 wxMask::wxMask( const wxBitmap& bitmap, int paletteIndex )
78 {
79 m_bitmap = (GdkBitmap *) NULL;
80 Create( bitmap, paletteIndex );
81 }
82
83 wxMask::wxMask( const wxBitmap& bitmap )
84 {
85 m_bitmap = (GdkBitmap *) NULL;
86 Create( bitmap );
87 }
88
89 wxMask::~wxMask()
90 {
91 if (m_bitmap)
92 gdk_bitmap_unref( m_bitmap );
93 }
94
95 bool wxMask::Create( const wxBitmap& bitmap,
96 const wxColour& colour )
97 {
98 if (m_bitmap)
99 {
100 gdk_bitmap_unref( m_bitmap );
101 m_bitmap = (GdkBitmap*) NULL;
102 }
103
104 wxImage image = bitmap.ConvertToImage();
105 if (!image.Ok()) return FALSE;
106
107 m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, image.GetWidth(), image.GetHeight(), 1 );
108 GdkGC *gc = gdk_gc_new( m_bitmap );
109
110 GdkColor color;
111 color.red = 65000;
112 color.green = 65000;
113 color.blue = 65000;
114 color.pixel = 1;
115 gdk_gc_set_foreground( gc, &color );
116 gdk_gc_set_fill( gc, GDK_SOLID );
117 gdk_draw_rectangle( m_bitmap, gc, TRUE, 0, 0, image.GetWidth(), image.GetHeight() );
118
119 unsigned char *data = image.GetData();
120 int index = 0;
121
122 unsigned char red = colour.Red();
123 unsigned char green = colour.Green();
124 unsigned char blue = colour.Blue();
125
126 GdkVisual *visual = wxTheApp->GetGdkVisual();
127
128 int bpp = visual->depth;
129 if ((bpp == 16) && (visual->red_mask != 0xf800))
130 bpp = 15;
131 if (bpp == 15)
132 {
133 red = red & 0xf8;
134 green = green & 0xf8;
135 blue = blue & 0xf8;
136 }
137 else if (bpp == 16)
138 {
139 red = red & 0xf8;
140 green = green & 0xfc;
141 blue = blue & 0xf8;
142 }
143 else if (bpp == 12)
144 {
145 red = red & 0xf0;
146 green = green & 0xf0;
147 blue = blue & 0xf0;
148 }
149
150 color.red = 0;
151 color.green = 0;
152 color.blue = 0;
153 color.pixel = 0;
154 gdk_gc_set_foreground( gc, &color );
155
156 for (int j = 0; j < image.GetHeight(); j++)
157 {
158 int start_x = -1;
159 int i;
160 for (i = 0; i < image.GetWidth(); i++)
161 {
162 if ((data[index] == red) &&
163 (data[index+1] == green) &&
164 (data[index+2] == blue))
165 {
166 if (start_x == -1)
167 start_x = i;
168 }
169 else
170 {
171 if (start_x != -1)
172 {
173 gdk_draw_line( m_bitmap, gc, start_x, j, i-1, j );
174 start_x = -1;
175 }
176 }
177 index += 3;
178 }
179 if (start_x != -1)
180 gdk_draw_line( m_bitmap, gc, start_x, j, i, j );
181 }
182
183 gdk_gc_unref( gc );
184
185 return TRUE;
186 }
187
188 bool wxMask::Create( const wxBitmap& bitmap, int paletteIndex )
189 {
190 unsigned char r,g,b;
191 wxPalette *pal = bitmap.GetPalette();
192
193 wxCHECK_MSG( pal, FALSE, wxT("Cannot create mask from bitmap without palette") );
194
195 pal->GetRGB(paletteIndex, &r, &g, &b);
196
197 return Create(bitmap, wxColour(r, g, b));
198 }
199
200 bool wxMask::Create( const wxBitmap& bitmap )
201 {
202 if (m_bitmap)
203 {
204 gdk_bitmap_unref( m_bitmap );
205 m_bitmap = (GdkBitmap*) NULL;
206 }
207
208 if (!bitmap.Ok()) return FALSE;
209
210 wxCHECK_MSG( bitmap.GetBitmap(), FALSE, wxT("Cannot create mask from colour bitmap") );
211
212 m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, bitmap.GetWidth(), bitmap.GetHeight(), 1 );
213
214 if (!m_bitmap) return FALSE;
215
216 GdkGC *gc = gdk_gc_new( m_bitmap );
217
218 gdk_wx_draw_bitmap( m_bitmap, gc, bitmap.GetBitmap(), 0, 0, 0, 0, bitmap.GetWidth(), bitmap.GetHeight() );
219
220 gdk_gc_unref( gc );
221
222 return TRUE;
223 }
224
225 GdkBitmap *wxMask::GetBitmap() const
226 {
227 return m_bitmap;
228 }
229
230 //-----------------------------------------------------------------------------
231 // wxBitmap
232 //-----------------------------------------------------------------------------
233
234 class wxBitmapRefData: public wxObjectRefData
235 {
236 public:
237 wxBitmapRefData();
238 ~wxBitmapRefData();
239
240 GdkPixmap *m_pixmap;
241 GdkBitmap *m_bitmap;
242 #ifdef __WXGTK20__
243 GdkPixbuf *m_pixbuf;
244 #endif
245 wxMask *m_mask;
246 int m_width;
247 int m_height;
248 int m_bpp;
249 wxPalette *m_palette;
250 };
251
252 wxBitmapRefData::wxBitmapRefData()
253 {
254 m_pixmap = (GdkPixmap *) NULL;
255 m_bitmap = (GdkBitmap *) NULL;
256 #ifdef __WXGTK20__
257 m_pixbuf = (GdkPixbuf *) NULL;
258 #endif
259 m_mask = (wxMask *) NULL;
260 m_width = 0;
261 m_height = 0;
262 m_bpp = 0;
263 m_palette = (wxPalette *) NULL;
264 }
265
266 wxBitmapRefData::~wxBitmapRefData()
267 {
268 if (m_pixmap)
269 gdk_pixmap_unref( m_pixmap );
270 if (m_bitmap)
271 gdk_bitmap_unref( m_bitmap );
272 #ifdef __WXGTK20__
273 if (m_pixbuf)
274 gdk_pixbuf_unref( m_pixbuf );
275 #endif
276 delete m_mask;
277 delete m_palette;
278 }
279
280 //-----------------------------------------------------------------------------
281
282 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
283
284 IMPLEMENT_DYNAMIC_CLASS(wxBitmap,wxGDIObject)
285
286 wxBitmap::wxBitmap()
287 {
288 }
289
290 wxBitmap::wxBitmap( int width, int height, int depth )
291 {
292 Create( width, height, depth );
293 }
294
295 bool wxBitmap::Create( int width, int height, int depth )
296 {
297 UnRef();
298
299 if ( width <= 0 || height <= 0 )
300 {
301 return false;
302 }
303
304 GdkVisual *visual = wxTheApp->GetGdkVisual();
305
306 if (depth == -1)
307 depth = visual->depth;
308
309 wxCHECK_MSG( (depth == visual->depth) || (depth == 1), FALSE,
310 wxT("invalid bitmap depth") )
311
312 m_refData = new wxBitmapRefData();
313 M_BMPDATA->m_mask = (wxMask *) NULL;
314 M_BMPDATA->m_width = width;
315 M_BMPDATA->m_height = height;
316 if (depth == 1)
317 {
318 M_BMPDATA->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 );
319 M_BMPDATA->m_bpp = 1;
320 }
321 else
322 {
323 M_BMPDATA->m_pixmap = gdk_pixmap_new( wxGetRootWindow()->window, width, height, depth );
324 M_BMPDATA->m_bpp = visual->depth;
325 }
326
327 return Ok();
328 }
329
330 bool wxBitmap::CreateFromXpm( const char **bits )
331 {
332 UnRef();
333
334 wxCHECK_MSG( bits != NULL, FALSE, wxT("invalid bitmap data") )
335
336 GdkVisual *visual = wxTheApp->GetGdkVisual();
337
338 m_refData = new wxBitmapRefData();
339
340 GdkBitmap *mask = (GdkBitmap*) NULL;
341
342 M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm_d( wxGetRootWindow()->window, &mask, NULL, (gchar **) bits );
343
344 wxCHECK_MSG( M_BMPDATA->m_pixmap, FALSE, wxT("couldn't create pixmap") );
345
346 if (mask)
347 {
348 M_BMPDATA->m_mask = new wxMask();
349 M_BMPDATA->m_mask->m_bitmap = mask;
350 }
351
352 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
353
354 M_BMPDATA->m_bpp = visual->depth; // Can we get a different depth from create_from_xpm_d() ?
355
356 return TRUE;
357 }
358
359 wxBitmap wxBitmap::Rescale( int clipx, int clipy, int clipwidth, int clipheight, int newx, int newy )
360 {
361 wxCHECK_MSG( Ok(), wxNullBitmap, wxT("invalid bitmap") );
362
363 if (newy==M_BMPDATA->m_width && newy==M_BMPDATA->m_height)
364 return *this;
365
366 int width = wxMax(newx, 1);
367 int height = wxMax(newy, 1);
368 width = wxMin(width, clipwidth);
369 height = wxMin(height, clipheight);
370
371 wxBitmap bmp;
372
373 #ifdef __WXGTK20__
374 if (HasPixbuf())
375 {
376 bmp.SetWidth(width);
377 bmp.SetHeight(height);
378 bmp.SetDepth(GetDepth());
379 bmp.SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE/*has_alpha*/,
380 8, width, height));
381 gdk_pixbuf_scale(M_BMPDATA->m_pixbuf, bmp.GetPixbuf(),
382 0, 0, width, height,
383 clipx, clipy,
384 (double)newx/GetWidth(), (double)newy/GetHeight(),
385 GDK_INTERP_BILINEAR);
386 }
387 else
388 #endif // __WXGTK20__
389 {
390 GdkImage *img = (GdkImage*) NULL;
391 if (GetPixmap())
392 img = gdk_image_get( GetPixmap(), 0, 0, GetWidth(), GetHeight() );
393 else if (GetBitmap())
394 img = gdk_image_get( GetBitmap(), 0, 0, GetWidth(), GetHeight() );
395 else
396 wxFAIL_MSG( wxT("Ill-formed bitmap") );
397
398 wxCHECK_MSG( img, wxNullBitmap, wxT("couldn't create image") );
399
400 int bpp = -1;
401
402
403 GdkGC *gc = NULL;
404 GdkPixmap *dstpix = NULL;
405 if (GetPixmap())
406 {
407 GdkVisual *visual = gdk_window_get_visual( GetPixmap() );
408 if (visual == NULL)
409 visual = wxTheApp->GetGdkVisual();
410
411 bpp = visual->depth;
412 bmp = wxBitmap(width,height,bpp);
413 dstpix = bmp.GetPixmap();
414 gc = gdk_gc_new( dstpix );
415 }
416
417 char *dst = NULL;
418 long dstbyteperline = 0;
419
420 if (GetBitmap())
421 {
422 bpp = 1;
423 dstbyteperline = width/8*M_BMPDATA->m_bpp;
424 if (width*M_BMPDATA->m_bpp % 8 != 0)
425 dstbyteperline++;
426 dst = (char*) malloc(dstbyteperline*height);
427 }
428
429 // be careful to use the right scaling factor
430 float scx = (float)M_BMPDATA->m_width/(float)newx;
431 float scy = (float)M_BMPDATA->m_height/(float)newy;
432 // prepare accel-tables
433 int *tablex = (int *)calloc(width,sizeof(int));
434 int *tabley = (int *)calloc(height,sizeof(int));
435
436 // accel table filled with clipped values
437 for (int x = 0; x < width; x++)
438 tablex[x] = (int) (scx * (x+clipx));
439 for (int y = 0; y < height; y++)
440 tabley[y] = (int) (scy * (y+clipy));
441
442 // Main rescaling routine starts here
443 for (int h = 0; h < height; h++)
444 {
445 char outbyte = 0;
446 int old_x = -1;
447 guint32 old_pixval = 0;
448
449 for (int w = 0; w < width; w++)
450 {
451 guint32 pixval;
452 int x = tablex[w];
453 if (x == old_x)
454 pixval = old_pixval;
455 else
456 {
457 pixval = gdk_image_get_pixel( img, x, tabley[h] );
458 old_pixval = pixval;
459 old_x = x;
460 }
461
462 if (bpp == 1)
463 {
464 if (!pixval)
465 {
466 char bit=1;
467 char shift = bit << w % 8;
468 outbyte |= shift;
469 }
470
471 if ((w+1)%8==0)
472 {
473 dst[h*dstbyteperline+w/8] = outbyte;
474 outbyte = 0;
475 }
476 }
477 else
478 {
479 GdkColor col;
480 col.pixel = pixval;
481 gdk_gc_set_foreground( gc, &col );
482 gdk_draw_point( dstpix, gc, w, h);
483 }
484 }
485
486 // do not forget the last byte
487 if ((bpp == 1) && (width % 8 != 0))
488 dst[h*dstbyteperline+width/8] = outbyte;
489 }
490
491 gdk_image_destroy( img );
492 if (gc) gdk_gc_unref( gc );
493
494 if (bpp == 1)
495 {
496 bmp = wxBitmap( (const char *)dst, width, height, 1 );
497 free( dst );
498 }
499
500 if (GetMask())
501 {
502 dstbyteperline = width/8;
503 if (width % 8 != 0)
504 dstbyteperline++;
505 dst = (char*) malloc(dstbyteperline*height);
506 img = gdk_image_get( GetMask()->GetBitmap(), 0, 0, GetWidth(), GetHeight() );
507
508 for (int h = 0; h < height; h++)
509 {
510 char outbyte = 0;
511 int old_x = -1;
512 guint32 old_pixval = 0;
513
514 for (int w = 0; w < width; w++)
515 {
516 guint32 pixval;
517 int x = tablex[w];
518 if (x == old_x)
519 pixval = old_pixval;
520 else
521 {
522 pixval = gdk_image_get_pixel( img, x, tabley[h] );
523 old_pixval = pixval;
524 old_x = x;
525 }
526
527 if (pixval)
528 {
529 char bit=1;
530 char shift = bit << w % 8;
531 outbyte |= shift;
532 }
533
534 if ((w+1)%8 == 0)
535 {
536 dst[h*dstbyteperline+w/8] = outbyte;
537 outbyte = 0;
538 }
539 }
540
541 // do not forget the last byte
542 if (width % 8 != 0)
543 dst[h*dstbyteperline+width/8] = outbyte;
544 }
545 wxMask* mask = new wxMask;
546 mask->m_bitmap = gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) dst, width, height );
547 bmp.SetMask(mask);
548
549 free( dst );
550 gdk_image_destroy( img );
551 }
552
553 free( tablex );
554 free( tabley );
555 }
556
557 return bmp;
558 }
559
560 bool wxBitmap::CreateFromImage(const wxImage& image, int depth)
561 {
562 UnRef();
563
564 wxCHECK_MSG( image.Ok(), FALSE, wxT("invalid image") )
565 wxCHECK_MSG( depth == -1 || depth == 1, FALSE, wxT("invalid bitmap depth") )
566
567 if (image.GetWidth() <= 0 || image.GetHeight() <= 0)
568 return false;
569
570 m_refData = new wxBitmapRefData();
571
572 if (depth == 1)
573 {
574 return CreateFromImageAsBitmap(image);
575 }
576 else
577 {
578 #ifdef __WXGTK20__
579 if (image.HasAlpha())
580 return CreateFromImageAsPixbuf(image);
581 #endif
582 return CreateFromImageAsPixmap(image);
583 }
584 }
585
586 // conversion to mono bitmap:
587 bool wxBitmap::CreateFromImageAsBitmap(const wxImage& img)
588 {
589 // convert alpha channel to mask, if it is present:
590 wxImage image(img);
591 image.ConvertAlphaToMask();
592
593 int width = image.GetWidth();
594 int height = image.GetHeight();
595
596 SetHeight( height );
597 SetWidth( width );
598
599 SetBitmap( gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 ) );
600
601 SetDepth( 1 );
602
603 GdkVisual *visual = wxTheApp->GetGdkVisual();
604
605 // Create picture image
606
607 unsigned char *data_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
608
609 GdkImage *data_image =
610 gdk_image_new_bitmap( visual, data_data, width, height );
611
612 // Create mask image
613
614 GdkImage *mask_image = (GdkImage*) NULL;
615
616 if (image.HasMask())
617 {
618 unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
619
620 mask_image = gdk_image_new_bitmap( visual, mask_data, width, height );
621
622 wxMask *mask = new wxMask();
623 mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 );
624
625 SetMask( mask );
626 }
627
628 int r_mask = image.GetMaskRed();
629 int g_mask = image.GetMaskGreen();
630 int b_mask = image.GetMaskBlue();
631
632 unsigned char* data = image.GetData();
633
634 int index = 0;
635 for (int y = 0; y < height; y++)
636 {
637 for (int x = 0; x < width; x++)
638 {
639 int r = data[index];
640 index++;
641 int g = data[index];
642 index++;
643 int b = data[index];
644 index++;
645
646 if (image.HasMask())
647 {
648 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
649 gdk_image_put_pixel( mask_image, x, y, 1 );
650 else
651 gdk_image_put_pixel( mask_image, x, y, 0 );
652 }
653
654 if ((r == 255) && (b == 255) && (g == 255))
655 gdk_image_put_pixel( data_image, x, y, 1 );
656 else
657 gdk_image_put_pixel( data_image, x, y, 0 );
658
659 } // for
660 } // for
661
662 // Blit picture
663
664 GdkGC *data_gc = gdk_gc_new( GetBitmap() );
665
666 gdk_draw_image( GetBitmap(), data_gc, data_image, 0, 0, 0, 0, width, height );
667
668 gdk_image_destroy( data_image );
669 gdk_gc_unref( data_gc );
670
671 // Blit mask
672
673 if (image.HasMask())
674 {
675 GdkGC *mask_gc = gdk_gc_new( GetMask()->GetBitmap() );
676
677 gdk_draw_image( GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height );
678
679 gdk_image_destroy( mask_image );
680 gdk_gc_unref( mask_gc );
681 }
682
683 return true;
684 }
685
686 // conversion to colour bitmap:
687 bool wxBitmap::CreateFromImageAsPixmap(const wxImage& img)
688 {
689 // convert alpha channel to mask, if it is present:
690 wxImage image(img);
691 image.ConvertAlphaToMask();
692
693 int width = image.GetWidth();
694 int height = image.GetHeight();
695
696 SetHeight( height );
697 SetWidth( width );
698
699 SetPixmap( gdk_pixmap_new( wxGetRootWindow()->window, width, height, -1 ) );
700
701 GdkVisual *visual = wxTheApp->GetGdkVisual();
702
703 int bpp = visual->depth;
704
705 SetDepth( bpp );
706
707 if ((bpp == 16) && (visual->red_mask != 0xf800))
708 bpp = 15;
709 else if (bpp < 8)
710 bpp = 8;
711
712 // We handle 8-bit bitmaps ourselves using the colour cube, 12-bit
713 // visuals are not supported by GDK so we do these ourselves, too.
714 // 15-bit and 16-bit should actually work and 24-bit certainly does.
715 #ifdef __sgi
716 if (!image.HasMask() && (bpp > 16))
717 #else
718 if (!image.HasMask() && (bpp > 12))
719 #endif
720 {
721 static bool s_hasInitialized = FALSE;
722
723 if (!s_hasInitialized)
724 {
725 gdk_rgb_init();
726 s_hasInitialized = TRUE;
727 }
728
729 GdkGC *gc = gdk_gc_new( GetPixmap() );
730
731 gdk_draw_rgb_image( GetPixmap(),
732 gc,
733 0, 0,
734 width, height,
735 GDK_RGB_DITHER_NONE,
736 image.GetData(),
737 width*3 );
738
739 gdk_gc_unref( gc );
740 return TRUE;
741 }
742
743 // Create picture image
744
745 GdkImage *data_image =
746 gdk_image_new( GDK_IMAGE_FASTEST, visual, width, height );
747
748 // Create mask image
749
750 GdkImage *mask_image = (GdkImage*) NULL;
751
752 if (image.HasMask())
753 {
754 unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
755
756 mask_image = gdk_image_new_bitmap( visual, mask_data, width, height );
757
758 wxMask *mask = new wxMask();
759 mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 );
760
761 SetMask( mask );
762 }
763
764 // Render
765
766 enum byte_order { RGB, RBG, BRG, BGR, GRB, GBR };
767 byte_order b_o = RGB;
768
769 if (bpp > 8)
770 {
771 if ((visual->red_mask > visual->green_mask) && (visual->green_mask > visual->blue_mask)) b_o = RGB;
772 else if ((visual->red_mask > visual->blue_mask) && (visual->blue_mask > visual->green_mask)) b_o = RBG;
773 else if ((visual->blue_mask > visual->red_mask) && (visual->red_mask > visual->green_mask)) b_o = BRG;
774 else if ((visual->blue_mask > visual->green_mask) && (visual->green_mask > visual->red_mask)) b_o = BGR;
775 else if ((visual->green_mask > visual->red_mask) && (visual->red_mask > visual->blue_mask)) b_o = GRB;
776 else if ((visual->green_mask > visual->blue_mask) && (visual->blue_mask > visual->red_mask)) b_o = GBR;
777 }
778
779 int r_mask = image.GetMaskRed();
780 int g_mask = image.GetMaskGreen();
781 int b_mask = image.GetMaskBlue();
782
783 unsigned char* data = image.GetData();
784
785 int index = 0;
786 for (int y = 0; y < height; y++)
787 {
788 for (int x = 0; x < width; x++)
789 {
790 int r = data[index];
791 index++;
792 int g = data[index];
793 index++;
794 int b = data[index];
795 index++;
796
797 if (image.HasMask())
798 {
799 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
800 gdk_image_put_pixel( mask_image, x, y, 1 );
801 else
802 gdk_image_put_pixel( mask_image, x, y, 0 );
803 }
804
805 switch (bpp)
806 {
807 case 8:
808 {
809 int pixel = -1;
810 if (wxTheApp->m_colorCube)
811 {
812 pixel = wxTheApp->m_colorCube[ ((r & 0xf8) << 7) + ((g & 0xf8) << 2) + ((b & 0xf8) >> 3) ];
813 }
814 else
815 {
816 GdkColormap *cmap = gtk_widget_get_default_colormap();
817 GdkColor *colors = cmap->colors;
818 int max = 3 * (65536);
819
820 for (int i = 0; i < cmap->size; i++)
821 {
822 int rdiff = (r << 8) - colors[i].red;
823 int gdiff = (g << 8) - colors[i].green;
824 int bdiff = (b << 8) - colors[i].blue;
825 int sum = ABS (rdiff) + ABS (gdiff) + ABS (bdiff);
826 if (sum < max) { pixel = i; max = sum; }
827 }
828 }
829
830 gdk_image_put_pixel( data_image, x, y, pixel );
831
832 break;
833 }
834 case 12: // SGI only
835 {
836 guint32 pixel = 0;
837 switch (b_o)
838 {
839 case RGB: pixel = ((r & 0xf0) << 4) | (g & 0xf0) | ((b & 0xf0) >> 4); break;
840 case RBG: pixel = ((r & 0xf0) << 4) | (b & 0xf0) | ((g & 0xf0) >> 4); break;
841 case GRB: pixel = ((g & 0xf0) << 4) | (r & 0xf0) | ((b & 0xf0) >> 4); break;
842 case GBR: pixel = ((g & 0xf0) << 4) | (b & 0xf0) | ((r & 0xf0) >> 4); break;
843 case BRG: pixel = ((b & 0xf0) << 4) | (r & 0xf0) | ((g & 0xf0) >> 4); break;
844 case BGR: pixel = ((b & 0xf0) << 4) | (g & 0xf0) | ((r & 0xf0) >> 4); break;
845 }
846 gdk_image_put_pixel( data_image, x, y, pixel );
847 break;
848 }
849 case 15:
850 {
851 guint32 pixel = 0;
852 switch (b_o)
853 {
854 case RGB: pixel = ((r & 0xf8) << 7) | ((g & 0xf8) << 2) | ((b & 0xf8) >> 3); break;
855 case RBG: pixel = ((r & 0xf8) << 7) | ((b & 0xf8) << 2) | ((g & 0xf8) >> 3); break;
856 case GRB: pixel = ((g & 0xf8) << 7) | ((r & 0xf8) << 2) | ((b & 0xf8) >> 3); break;
857 case GBR: pixel = ((g & 0xf8) << 7) | ((b & 0xf8) << 2) | ((r & 0xf8) >> 3); break;
858 case BRG: pixel = ((b & 0xf8) << 7) | ((r & 0xf8) << 2) | ((g & 0xf8) >> 3); break;
859 case BGR: pixel = ((b & 0xf8) << 7) | ((g & 0xf8) << 2) | ((r & 0xf8) >> 3); break;
860 }
861 gdk_image_put_pixel( data_image, x, y, pixel );
862 break;
863 }
864 case 16:
865 {
866 // I actually don't know if for 16-bit displays, it is alway the green
867 // component or the second component which has 6 bits.
868 guint32 pixel = 0;
869 switch (b_o)
870 {
871 case RGB: pixel = ((r & 0xf8) << 8) | ((g & 0xfc) << 3) | ((b & 0xf8) >> 3); break;
872 case RBG: pixel = ((r & 0xf8) << 8) | ((b & 0xfc) << 3) | ((g & 0xf8) >> 3); break;
873 case GRB: pixel = ((g & 0xf8) << 8) | ((r & 0xfc) << 3) | ((b & 0xf8) >> 3); break;
874 case GBR: pixel = ((g & 0xf8) << 8) | ((b & 0xfc) << 3) | ((r & 0xf8) >> 3); break;
875 case BRG: pixel = ((b & 0xf8) << 8) | ((r & 0xfc) << 3) | ((g & 0xf8) >> 3); break;
876 case BGR: pixel = ((b & 0xf8) << 8) | ((g & 0xfc) << 3) | ((r & 0xf8) >> 3); break;
877 }
878 gdk_image_put_pixel( data_image, x, y, pixel );
879 break;
880 }
881 case 32:
882 case 24:
883 {
884 guint32 pixel = 0;
885 switch (b_o)
886 {
887 case RGB: pixel = (r << 16) | (g << 8) | b; break;
888 case RBG: pixel = (r << 16) | (b << 8) | g; break;
889 case BRG: pixel = (b << 16) | (r << 8) | g; break;
890 case BGR: pixel = (b << 16) | (g << 8) | r; break;
891 case GRB: pixel = (g << 16) | (r << 8) | b; break;
892 case GBR: pixel = (g << 16) | (b << 8) | r; break;
893 }
894 gdk_image_put_pixel( data_image, x, y, pixel );
895 }
896 default: break;
897 }
898 } // for
899 } // for
900
901 // Blit picture
902
903 GdkGC *data_gc = gdk_gc_new( GetPixmap() );
904
905 gdk_draw_image( GetPixmap(), data_gc, data_image, 0, 0, 0, 0, width, height );
906
907 gdk_image_destroy( data_image );
908 gdk_gc_unref( data_gc );
909
910 // Blit mask
911
912 if (image.HasMask())
913 {
914 GdkGC *mask_gc = gdk_gc_new( GetMask()->GetBitmap() );
915
916 gdk_draw_image( GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height );
917
918 gdk_image_destroy( mask_image );
919 gdk_gc_unref( mask_gc );
920 }
921
922 return true;
923 }
924
925 #ifdef __WXGTK20__
926 bool wxBitmap::CreateFromImageAsPixbuf(const wxImage& image)
927 {
928 int width = image.GetWidth();
929 int height = image.GetHeight();
930
931 GdkPixbuf *pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB,
932 image.HasAlpha(),
933 8 /* bits per sample */,
934 width, height);
935 if (!pixbuf)
936 return false;
937
938 wxASSERT( gdk_pixbuf_get_n_channels(pixbuf) == 4 );
939 wxASSERT( gdk_pixbuf_get_width(pixbuf) == width );
940 wxASSERT( gdk_pixbuf_get_height(pixbuf) == height );
941
942 M_BMPDATA->m_pixbuf = pixbuf;
943 SetHeight(height);
944 SetWidth(width);
945 SetDepth(wxTheApp->GetGdkVisual()->depth);
946
947 // Copy the data:
948 unsigned char *in = image.GetData();
949 unsigned char *out = gdk_pixbuf_get_pixels(pixbuf);
950 unsigned char *alpha = image.GetAlpha();
951
952 int rowinc = gdk_pixbuf_get_rowstride(pixbuf) - 4 * width;
953
954
955 for (int y = 0; y < height; y++, out += rowinc)
956 {
957 for (int x = 0; x < width; x++, alpha++, out += 4, in += 3)
958 {
959 out[0] = in[0];
960 out[1] = in[1];
961 out[2] = in[2];
962 out[3] = *alpha;
963 }
964 }
965
966 return true;
967 }
968 #endif // __WXGTK20__
969
970 wxImage wxBitmap::ConvertToImage() const
971 {
972 wxImage image;
973
974 wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
975
976 image.Create(GetWidth(), GetHeight());
977 unsigned char *data = image.GetData();
978
979 if (!data)
980 {
981 wxFAIL_MSG( wxT("couldn't create image") );
982 return wxNullImage;
983 }
984
985 #ifdef __WXGTK20__
986 if (HasPixbuf())
987 {
988 GdkPixbuf *pixbuf = GetPixbuf();
989 wxASSERT( gdk_pixbuf_get_has_alpha(pixbuf) );
990
991 int w = GetWidth();
992 int h = GetHeight();
993
994 image.SetAlpha();
995
996 unsigned char *alpha = image.GetAlpha();
997 unsigned char *in = gdk_pixbuf_get_pixels(pixbuf);
998 unsigned char *out = data;
999 int rowinc = gdk_pixbuf_get_rowstride(pixbuf) - 4 * w;
1000
1001 for (int y = 0; y < h; y++, in += rowinc)
1002 {
1003 for (int x = 0; x < w; x++, in += 4, out += 3, alpha++)
1004 {
1005 out[0] = in[0];
1006 out[1] = in[1];
1007 out[2] = in[2];
1008 *alpha = in[3];
1009 }
1010 }
1011 }
1012 else
1013 #endif // __WXGTK20__
1014 {
1015 // the colour used as transparent one in wxImage and the one it is
1016 // replaced with when it really occurs in the bitmap
1017 static const int MASK_RED = 1;
1018 static const int MASK_GREEN = 2;
1019 static const int MASK_BLUE = 3;
1020 static const int MASK_BLUE_REPLACEMENT = 2;
1021
1022 GdkImage *gdk_image = (GdkImage*) NULL;
1023
1024 if (HasPixmap())
1025 {
1026 gdk_image = gdk_image_get( GetPixmap(),
1027 0, 0,
1028 GetWidth(), GetHeight() );
1029 }
1030 else if (GetBitmap())
1031 {
1032 gdk_image = gdk_image_get( GetBitmap(),
1033 0, 0,
1034 GetWidth(), GetHeight() );
1035 }
1036 else
1037 {
1038 wxFAIL_MSG( wxT("Ill-formed bitmap") );
1039 }
1040
1041 wxCHECK_MSG( gdk_image, wxNullImage, wxT("couldn't create image") );
1042
1043 GdkImage *gdk_image_mask = (GdkImage*) NULL;
1044 if (GetMask())
1045 {
1046 gdk_image_mask = gdk_image_get( GetMask()->GetBitmap(),
1047 0, 0,
1048 GetWidth(), GetHeight() );
1049
1050 image.SetMaskColour( MASK_RED, MASK_GREEN, MASK_BLUE );
1051 }
1052
1053 int bpp = -1;
1054 int red_shift_right = 0;
1055 int green_shift_right = 0;
1056 int blue_shift_right = 0;
1057 int red_shift_left = 0;
1058 int green_shift_left = 0;
1059 int blue_shift_left = 0;
1060 bool use_shift = FALSE;
1061
1062 if (GetPixmap())
1063 {
1064 GdkVisual *visual = gdk_window_get_visual( GetPixmap() );
1065 if (visual == NULL)
1066 visual = wxTheApp->GetGdkVisual();
1067
1068 bpp = visual->depth;
1069 if (bpp == 16)
1070 bpp = visual->red_prec + visual->green_prec + visual->blue_prec;
1071 red_shift_right = visual->red_shift;
1072 red_shift_left = 8-visual->red_prec;
1073 green_shift_right = visual->green_shift;
1074 green_shift_left = 8-visual->green_prec;
1075 blue_shift_right = visual->blue_shift;
1076 blue_shift_left = 8-visual->blue_prec;
1077
1078 use_shift = (visual->type == GDK_VISUAL_TRUE_COLOR) || (visual->type == GDK_VISUAL_DIRECT_COLOR);
1079 }
1080 if (GetBitmap())
1081 {
1082 bpp = 1;
1083 }
1084
1085
1086 GdkColormap *cmap = gtk_widget_get_default_colormap();
1087
1088 long pos = 0;
1089 for (int j = 0; j < GetHeight(); j++)
1090 {
1091 for (int i = 0; i < GetWidth(); i++)
1092 {
1093 wxUint32 pixel = gdk_image_get_pixel( gdk_image, i, j );
1094 if (bpp == 1)
1095 {
1096 if (pixel == 0)
1097 {
1098 data[pos] = 0;
1099 data[pos+1] = 0;
1100 data[pos+2] = 0;
1101 }
1102 else
1103 {
1104 data[pos] = 255;
1105 data[pos+1] = 255;
1106 data[pos+2] = 255;
1107 }
1108 }
1109 else if (use_shift)
1110 {
1111 data[pos] = (pixel >> red_shift_right) << red_shift_left;
1112 data[pos+1] = (pixel >> green_shift_right) << green_shift_left;
1113 data[pos+2] = (pixel >> blue_shift_right) << blue_shift_left;
1114 }
1115 else if (cmap->colors)
1116 {
1117 data[pos] = cmap->colors[pixel].red >> 8;
1118 data[pos+1] = cmap->colors[pixel].green >> 8;
1119 data[pos+2] = cmap->colors[pixel].blue >> 8;
1120 }
1121 else
1122 {
1123 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
1124 }
1125
1126 if (gdk_image_mask)
1127 {
1128 int mask_pixel = gdk_image_get_pixel( gdk_image_mask, i, j );
1129 if (mask_pixel == 0)
1130 {
1131 data[pos] = MASK_RED;
1132 data[pos+1] = MASK_GREEN;
1133 data[pos+2] = MASK_BLUE;
1134 }
1135 else if ( data[pos] == MASK_RED &&
1136 data[pos+1] == MASK_GREEN &&
1137 data[pos+2] == MASK_BLUE )
1138 {
1139 data[pos+2] = MASK_BLUE_REPLACEMENT;
1140 }
1141 }
1142
1143 pos += 3;
1144 }
1145 }
1146
1147 gdk_image_destroy( gdk_image );
1148 if (gdk_image_mask) gdk_image_destroy( gdk_image_mask );
1149 }
1150
1151 return image;
1152 }
1153
1154 wxBitmap::wxBitmap( const wxBitmap& bmp )
1155 : wxGDIObject()
1156 {
1157 Ref( bmp );
1158 }
1159
1160 wxBitmap::wxBitmap( const wxString &filename, int type )
1161 {
1162 LoadFile( filename, type );
1163 }
1164
1165 wxBitmap::wxBitmap( const char bits[], int width, int height, int WXUNUSED(depth))
1166 {
1167 if ( width > 0 && height > 0 )
1168 {
1169 m_refData = new wxBitmapRefData();
1170
1171 M_BMPDATA->m_mask = (wxMask *) NULL;
1172 M_BMPDATA->m_bitmap = gdk_bitmap_create_from_data
1173 (
1174 wxGetRootWindow()->window,
1175 (gchar *) bits,
1176 width,
1177 height
1178 );
1179 M_BMPDATA->m_width = width;
1180 M_BMPDATA->m_height = height;
1181 M_BMPDATA->m_bpp = 1;
1182
1183 wxASSERT_MSG( M_BMPDATA->m_bitmap, wxT("couldn't create bitmap") );
1184 }
1185 }
1186
1187 wxBitmap::~wxBitmap()
1188 {
1189 }
1190
1191 wxBitmap& wxBitmap::operator = ( const wxBitmap& bmp )
1192 {
1193 if ( m_refData != bmp.m_refData )
1194 Ref( bmp );
1195
1196 return *this;
1197 }
1198
1199 bool wxBitmap::operator == ( const wxBitmap& bmp ) const
1200 {
1201 return m_refData == bmp.m_refData;
1202 }
1203
1204 bool wxBitmap::operator != ( const wxBitmap& bmp ) const
1205 {
1206 return m_refData != bmp.m_refData;
1207 }
1208
1209 bool wxBitmap::Ok() const
1210 {
1211 return (m_refData != NULL) &&
1212 (
1213 #ifdef __WXGTK20__
1214 M_BMPDATA->m_pixbuf ||
1215 #endif
1216 M_BMPDATA->m_bitmap || M_BMPDATA->m_pixmap
1217 );
1218 }
1219
1220 int wxBitmap::GetHeight() const
1221 {
1222 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1223
1224 return M_BMPDATA->m_height;
1225 }
1226
1227 int wxBitmap::GetWidth() const
1228 {
1229 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1230
1231 return M_BMPDATA->m_width;
1232 }
1233
1234 int wxBitmap::GetDepth() const
1235 {
1236 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1237
1238 return M_BMPDATA->m_bpp;
1239 }
1240
1241 wxMask *wxBitmap::GetMask() const
1242 {
1243 wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") );
1244
1245 return M_BMPDATA->m_mask;
1246 }
1247
1248 void wxBitmap::SetMask( wxMask *mask )
1249 {
1250 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
1251
1252 if (M_BMPDATA->m_mask) delete M_BMPDATA->m_mask;
1253
1254 M_BMPDATA->m_mask = mask;
1255 }
1256
1257 bool wxBitmap::CopyFromIcon(const wxIcon& icon)
1258 {
1259 *this = icon;
1260 return TRUE;
1261 }
1262
1263 wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const
1264 {
1265 wxCHECK_MSG( Ok() &&
1266 (rect.x >= 0) && (rect.y >= 0) &&
1267 (rect.x+rect.width <= M_BMPDATA->m_width) && (rect.y+rect.height <= M_BMPDATA->m_height),
1268 wxNullBitmap, wxT("invalid bitmap or bitmap region") );
1269
1270 wxBitmap ret( rect.width, rect.height, M_BMPDATA->m_bpp );
1271 wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") );
1272
1273 #ifdef __WXGTK20__
1274 if (HasPixbuf())
1275 {
1276 GdkPixbuf *pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB,
1277 TRUE/*has_alpha*/,
1278 8, GetWidth(), GetHeight());
1279 ret.SetPixbuf(pixbuf);
1280 gdk_pixbuf_copy_area(M_BMPDATA->m_pixbuf,
1281 rect.x, rect.y, rect.width, rect.height,
1282 pixbuf, 0, 0);
1283 }
1284 else
1285 #endif // __WXGTK20__
1286 {
1287 if (ret.GetPixmap())
1288 {
1289 GdkGC *gc = gdk_gc_new( ret.GetPixmap() );
1290 gdk_draw_pixmap( ret.GetPixmap(), gc, GetPixmap(), rect.x, rect.y, 0, 0, rect.width, rect.height );
1291 gdk_gc_destroy( gc );
1292 }
1293 else
1294 {
1295 GdkGC *gc = gdk_gc_new( ret.GetBitmap() );
1296 GdkColor col;
1297 col.pixel = 0xFFFFFF;
1298 gdk_gc_set_foreground( gc, &col );
1299 col.pixel = 0;
1300 gdk_gc_set_background( gc, &col );
1301 gdk_wx_draw_bitmap( ret.GetBitmap(), gc, GetBitmap(), rect.x, rect.y, 0, 0, rect.width, rect.height );
1302 gdk_gc_destroy( gc );
1303 }
1304 }
1305
1306 if (GetMask())
1307 {
1308 wxMask *mask = new wxMask;
1309 mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, rect.width, rect.height, 1 );
1310
1311 GdkGC *gc = gdk_gc_new( mask->m_bitmap );
1312 GdkColor col;
1313 col.pixel = 0xFFFFFF;
1314 gdk_gc_set_foreground( gc, &col );
1315 col.pixel = 0;
1316 gdk_gc_set_background( gc, &col );
1317 gdk_wx_draw_bitmap( mask->m_bitmap, gc, M_BMPDATA->m_mask->m_bitmap, rect.x, rect.y, 0, 0, rect.width, rect.height );
1318 gdk_gc_destroy( gc );
1319
1320 ret.SetMask( mask );
1321 }
1322
1323 return ret;
1324 }
1325
1326 bool wxBitmap::SaveFile( const wxString &name, int type, wxPalette *WXUNUSED(palette) )
1327 {
1328 wxCHECK_MSG( Ok(), FALSE, wxT("invalid bitmap") );
1329
1330 // Try to save the bitmap via wxImage handlers:
1331 {
1332 wxImage image = ConvertToImage();
1333 if (image.Ok()) return image.SaveFile( name, type );
1334 }
1335
1336 return FALSE;
1337 }
1338
1339 bool wxBitmap::LoadFile( const wxString &name, int type )
1340 {
1341 UnRef();
1342
1343 if (!wxFileExists(name))
1344 return FALSE;
1345
1346 GdkVisual *visual = wxTheApp->GetGdkVisual();
1347
1348 if (type == wxBITMAP_TYPE_XPM)
1349 {
1350 m_refData = new wxBitmapRefData();
1351
1352 GdkBitmap *mask = (GdkBitmap*) NULL;
1353
1354 M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm
1355 (
1356 wxGetRootWindow()->window,
1357 &mask,
1358 NULL,
1359 name.fn_str()
1360 );
1361
1362 if (mask)
1363 {
1364 M_BMPDATA->m_mask = new wxMask();
1365 M_BMPDATA->m_mask->m_bitmap = mask;
1366 }
1367
1368 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
1369
1370 M_BMPDATA->m_bpp = visual->depth;
1371 }
1372 else // try if wxImage can load it
1373 {
1374 wxImage image;
1375 if ( !image.LoadFile( name, type ) || !image.Ok() )
1376 return FALSE;
1377
1378 *this = wxBitmap(image);
1379 }
1380
1381 return TRUE;
1382 }
1383
1384 wxPalette *wxBitmap::GetPalette() const
1385 {
1386 if (!Ok())
1387 return (wxPalette *) NULL;
1388
1389 return M_BMPDATA->m_palette;
1390 }
1391
1392 void wxBitmap::SetHeight( int height )
1393 {
1394 if (!m_refData)
1395 m_refData = new wxBitmapRefData();
1396
1397 M_BMPDATA->m_height = height;
1398 }
1399
1400 void wxBitmap::SetWidth( int width )
1401 {
1402 if (!m_refData)
1403 m_refData = new wxBitmapRefData();
1404
1405 M_BMPDATA->m_width = width;
1406 }
1407
1408 void wxBitmap::SetDepth( int depth )
1409 {
1410 if (!m_refData)
1411 m_refData = new wxBitmapRefData();
1412
1413 M_BMPDATA->m_bpp = depth;
1414 }
1415
1416 void wxBitmap::SetPixmap( GdkPixmap *pixmap )
1417 {
1418 if (!m_refData)
1419 m_refData = new wxBitmapRefData();
1420
1421 M_BMPDATA->m_pixmap = pixmap;
1422 }
1423
1424 void wxBitmap::SetBitmap( GdkPixmap *bitmap )
1425 {
1426 if (!m_refData)
1427 m_refData = new wxBitmapRefData();
1428
1429 M_BMPDATA->m_bitmap = bitmap;
1430 }
1431
1432 GdkPixmap *wxBitmap::GetPixmap() const
1433 {
1434 wxCHECK_MSG( Ok(), (GdkPixmap *) NULL, wxT("invalid bitmap") );
1435
1436 #ifdef __WXGTK20__
1437 // create the pixmap on the fly if we use Pixbuf representation:
1438 if (HasPixbuf() && !HasPixmap())
1439 {
1440 gdk_pixbuf_render_pixmap_and_mask(M_BMPDATA->m_pixbuf,
1441 &M_BMPDATA->m_pixmap,
1442 NULL /*mask*/,
1443 128 /*threshold*/);
1444 }
1445 #endif // __WXGTK20__
1446
1447 return M_BMPDATA->m_pixmap;
1448 }
1449
1450 bool wxBitmap::HasPixmap() const
1451 {
1452 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1453
1454 return M_BMPDATA->m_pixmap != NULL;
1455 }
1456
1457 GdkBitmap *wxBitmap::GetBitmap() const
1458 {
1459 wxCHECK_MSG( Ok(), (GdkBitmap *) NULL, wxT("invalid bitmap") );
1460
1461 return M_BMPDATA->m_bitmap;
1462 }
1463
1464 #ifdef __WXGTK20__
1465 GdkPixbuf *wxBitmap::GetPixbuf() const
1466 {
1467 wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") );
1468
1469 return M_BMPDATA->m_pixbuf;
1470 }
1471
1472 bool wxBitmap::HasPixbuf() const
1473 {
1474 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1475
1476 return M_BMPDATA->m_pixbuf != NULL;
1477 }
1478
1479 void wxBitmap::SetPixbuf( GdkPixbuf *pixbuf )
1480 {
1481 if (!m_refData)
1482 m_refData = new wxBitmapRefData();
1483
1484 M_BMPDATA->m_pixbuf = pixbuf;
1485 }
1486
1487 void wxBitmap::PurgeOtherRepresentations(wxBitmap::Representation keep)
1488 {
1489 if (keep == Pixmap && HasPixbuf())
1490 {
1491 gdk_pixbuf_unref( M_BMPDATA->m_pixbuf );
1492 M_BMPDATA->m_pixbuf = NULL;
1493 }
1494 if (keep == Pixbuf && HasPixmap())
1495 {
1496 gdk_pixmap_unref( M_BMPDATA->m_pixmap );
1497 M_BMPDATA->m_pixmap = NULL;
1498 }
1499 }
1500
1501 #endif // __WXGTK20__