]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk/bitmap.cpp
use GTK+ API for setting icons instead of direct X11 calls
[wxWidgets.git] / src / gtk / bitmap.cpp
... / ...
CommitLineData
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
44extern 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
58extern GtkWidget *wxGetRootWindow();
59
60//-----------------------------------------------------------------------------
61// wxMask
62//-----------------------------------------------------------------------------
63
64IMPLEMENT_DYNAMIC_CLASS(wxMask,wxObject)
65
66wxMask::wxMask()
67{
68 m_bitmap = (GdkBitmap *) NULL;
69}
70
71wxMask::wxMask( const wxBitmap& bitmap, const wxColour& colour )
72{
73 m_bitmap = (GdkBitmap *) NULL;
74 Create( bitmap, colour );
75}
76
77wxMask::wxMask( const wxBitmap& bitmap, int paletteIndex )
78{
79 m_bitmap = (GdkBitmap *) NULL;
80 Create( bitmap, paletteIndex );
81}
82
83wxMask::wxMask( const wxBitmap& bitmap )
84{
85 m_bitmap = (GdkBitmap *) NULL;
86 Create( bitmap );
87}
88
89wxMask::~wxMask()
90{
91 if (m_bitmap)
92 gdk_bitmap_unref( m_bitmap );
93}
94
95bool 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
188bool 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
200bool 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
225GdkBitmap *wxMask::GetBitmap() const
226{
227 return m_bitmap;
228}
229
230//-----------------------------------------------------------------------------
231// wxBitmap
232//-----------------------------------------------------------------------------
233
234class wxBitmapRefData: public wxObjectRefData
235{
236public:
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
252wxBitmapRefData::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
266wxBitmapRefData::~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
284IMPLEMENT_DYNAMIC_CLASS(wxBitmap,wxGDIObject)
285
286wxBitmap::wxBitmap()
287{
288}
289
290wxBitmap::wxBitmap( int width, int height, int depth )
291{
292 Create( width, height, depth );
293}
294
295bool 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
330bool 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
359wxBitmap 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,
380 gdk_pixbuf_get_has_alpha(GetPixbuf()),
381 8, width, height));
382 gdk_pixbuf_scale(GetPixbuf(), bmp.GetPixbuf(),
383 0, 0, width, height,
384 clipx, clipy,
385 (double)newx/GetWidth(), (double)newy/GetHeight(),
386 GDK_INTERP_BILINEAR);
387 }
388 else
389#endif // __WXGTK20__
390 {
391 GdkImage *img = (GdkImage*) NULL;
392 if (GetPixmap())
393 img = gdk_image_get( GetPixmap(), 0, 0, GetWidth(), GetHeight() );
394 else if (GetBitmap())
395 img = gdk_image_get( GetBitmap(), 0, 0, GetWidth(), GetHeight() );
396 else
397 wxFAIL_MSG( wxT("Ill-formed bitmap") );
398
399 wxCHECK_MSG( img, wxNullBitmap, wxT("couldn't create image") );
400
401 int bpp = -1;
402
403
404 GdkGC *gc = NULL;
405 GdkPixmap *dstpix = NULL;
406 if (GetPixmap())
407 {
408 GdkVisual *visual = gdk_window_get_visual( GetPixmap() );
409 if (visual == NULL)
410 visual = wxTheApp->GetGdkVisual();
411
412 bpp = visual->depth;
413 bmp = wxBitmap(width,height,bpp);
414 dstpix = bmp.GetPixmap();
415 gc = gdk_gc_new( dstpix );
416 }
417
418 char *dst = NULL;
419 long dstbyteperline = 0;
420
421 if (GetBitmap())
422 {
423 bpp = 1;
424 dstbyteperline = width/8*M_BMPDATA->m_bpp;
425 if (width*M_BMPDATA->m_bpp % 8 != 0)
426 dstbyteperline++;
427 dst = (char*) malloc(dstbyteperline*height);
428 }
429
430 // be careful to use the right scaling factor
431 float scx = (float)M_BMPDATA->m_width/(float)newx;
432 float scy = (float)M_BMPDATA->m_height/(float)newy;
433 // prepare accel-tables
434 int *tablex = (int *)calloc(width,sizeof(int));
435 int *tabley = (int *)calloc(height,sizeof(int));
436
437 // accel table filled with clipped values
438 for (int x = 0; x < width; x++)
439 tablex[x] = (int) (scx * (x+clipx));
440 for (int y = 0; y < height; y++)
441 tabley[y] = (int) (scy * (y+clipy));
442
443 // Main rescaling routine starts here
444 for (int h = 0; h < height; h++)
445 {
446 char outbyte = 0;
447 int old_x = -1;
448 guint32 old_pixval = 0;
449
450 for (int w = 0; w < width; w++)
451 {
452 guint32 pixval;
453 int x = tablex[w];
454 if (x == old_x)
455 pixval = old_pixval;
456 else
457 {
458 pixval = gdk_image_get_pixel( img, x, tabley[h] );
459 old_pixval = pixval;
460 old_x = x;
461 }
462
463 if (bpp == 1)
464 {
465 if (!pixval)
466 {
467 char bit=1;
468 char shift = bit << w % 8;
469 outbyte |= shift;
470 }
471
472 if ((w+1)%8==0)
473 {
474 dst[h*dstbyteperline+w/8] = outbyte;
475 outbyte = 0;
476 }
477 }
478 else
479 {
480 GdkColor col;
481 col.pixel = pixval;
482 gdk_gc_set_foreground( gc, &col );
483 gdk_draw_point( dstpix, gc, w, h);
484 }
485 }
486
487 // do not forget the last byte
488 if ((bpp == 1) && (width % 8 != 0))
489 dst[h*dstbyteperline+width/8] = outbyte;
490 }
491
492 gdk_image_destroy( img );
493 if (gc) gdk_gc_unref( gc );
494
495 if (bpp == 1)
496 {
497 bmp = wxBitmap( (const char *)dst, width, height, 1 );
498 free( dst );
499 }
500
501 if (GetMask())
502 {
503 dstbyteperline = width/8;
504 if (width % 8 != 0)
505 dstbyteperline++;
506 dst = (char*) malloc(dstbyteperline*height);
507 img = gdk_image_get( GetMask()->GetBitmap(), 0, 0, GetWidth(), GetHeight() );
508
509 for (int h = 0; h < height; h++)
510 {
511 char outbyte = 0;
512 int old_x = -1;
513 guint32 old_pixval = 0;
514
515 for (int w = 0; w < width; w++)
516 {
517 guint32 pixval;
518 int x = tablex[w];
519 if (x == old_x)
520 pixval = old_pixval;
521 else
522 {
523 pixval = gdk_image_get_pixel( img, x, tabley[h] );
524 old_pixval = pixval;
525 old_x = x;
526 }
527
528 if (pixval)
529 {
530 char bit=1;
531 char shift = bit << w % 8;
532 outbyte |= shift;
533 }
534
535 if ((w+1)%8 == 0)
536 {
537 dst[h*dstbyteperline+w/8] = outbyte;
538 outbyte = 0;
539 }
540 }
541
542 // do not forget the last byte
543 if (width % 8 != 0)
544 dst[h*dstbyteperline+width/8] = outbyte;
545 }
546 wxMask* mask = new wxMask;
547 mask->m_bitmap = gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) dst, width, height );
548 bmp.SetMask(mask);
549
550 free( dst );
551 gdk_image_destroy( img );
552 }
553
554 free( tablex );
555 free( tabley );
556 }
557
558 return bmp;
559}
560
561bool wxBitmap::CreateFromImage(const wxImage& image, int depth)
562{
563 UnRef();
564
565 wxCHECK_MSG( image.Ok(), FALSE, wxT("invalid image") )
566 wxCHECK_MSG( depth == -1 || depth == 1, FALSE, wxT("invalid bitmap depth") )
567
568 if (image.GetWidth() <= 0 || image.GetHeight() <= 0)
569 return false;
570
571 m_refData = new wxBitmapRefData();
572
573 if (depth == 1)
574 {
575 return CreateFromImageAsBitmap(image);
576 }
577 else
578 {
579#ifdef __WXGTK20__
580 if (image.HasAlpha())
581 return CreateFromImageAsPixbuf(image);
582#endif
583 return CreateFromImageAsPixmap(image);
584 }
585}
586
587// conversion to mono bitmap:
588bool wxBitmap::CreateFromImageAsBitmap(const wxImage& img)
589{
590 // convert alpha channel to mask, if it is present:
591 wxImage image(img);
592 image.ConvertAlphaToMask();
593
594 int width = image.GetWidth();
595 int height = image.GetHeight();
596
597 SetHeight( height );
598 SetWidth( width );
599
600 SetBitmap( gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 ) );
601
602 SetDepth( 1 );
603
604 GdkVisual *visual = wxTheApp->GetGdkVisual();
605
606 // Create picture image
607
608 unsigned char *data_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
609
610 GdkImage *data_image =
611 gdk_image_new_bitmap( visual, data_data, width, height );
612
613 // Create mask image
614
615 GdkImage *mask_image = (GdkImage*) NULL;
616
617 if (image.HasMask())
618 {
619 unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
620
621 mask_image = gdk_image_new_bitmap( visual, mask_data, width, height );
622
623 wxMask *mask = new wxMask();
624 mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 );
625
626 SetMask( mask );
627 }
628
629 int r_mask = image.GetMaskRed();
630 int g_mask = image.GetMaskGreen();
631 int b_mask = image.GetMaskBlue();
632
633 unsigned char* data = image.GetData();
634
635 int index = 0;
636 for (int y = 0; y < height; y++)
637 {
638 for (int x = 0; x < width; x++)
639 {
640 int r = data[index];
641 index++;
642 int g = data[index];
643 index++;
644 int b = data[index];
645 index++;
646
647 if (image.HasMask())
648 {
649 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
650 gdk_image_put_pixel( mask_image, x, y, 1 );
651 else
652 gdk_image_put_pixel( mask_image, x, y, 0 );
653 }
654
655 if ((r == 255) && (b == 255) && (g == 255))
656 gdk_image_put_pixel( data_image, x, y, 1 );
657 else
658 gdk_image_put_pixel( data_image, x, y, 0 );
659
660 } // for
661 } // for
662
663 // Blit picture
664
665 GdkGC *data_gc = gdk_gc_new( GetBitmap() );
666
667 gdk_draw_image( GetBitmap(), data_gc, data_image, 0, 0, 0, 0, width, height );
668
669 gdk_image_destroy( data_image );
670 gdk_gc_unref( data_gc );
671
672 // Blit mask
673
674 if (image.HasMask())
675 {
676 GdkGC *mask_gc = gdk_gc_new( GetMask()->GetBitmap() );
677
678 gdk_draw_image( GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height );
679
680 gdk_image_destroy( mask_image );
681 gdk_gc_unref( mask_gc );
682 }
683
684 return true;
685}
686
687// conversion to colour bitmap:
688bool wxBitmap::CreateFromImageAsPixmap(const wxImage& img)
689{
690 // convert alpha channel to mask, if it is present:
691 wxImage image(img);
692 image.ConvertAlphaToMask();
693
694 int width = image.GetWidth();
695 int height = image.GetHeight();
696
697 SetHeight( height );
698 SetWidth( width );
699
700 SetPixmap( gdk_pixmap_new( wxGetRootWindow()->window, width, height, -1 ) );
701
702 GdkVisual *visual = wxTheApp->GetGdkVisual();
703
704 int bpp = visual->depth;
705
706 SetDepth( bpp );
707
708 if ((bpp == 16) && (visual->red_mask != 0xf800))
709 bpp = 15;
710 else if (bpp < 8)
711 bpp = 8;
712
713 // We handle 8-bit bitmaps ourselves using the colour cube, 12-bit
714 // visuals are not supported by GDK so we do these ourselves, too.
715 // 15-bit and 16-bit should actually work and 24-bit certainly does.
716#ifdef __sgi
717 if (!image.HasMask() && (bpp > 16))
718#else
719 if (!image.HasMask() && (bpp > 12))
720#endif
721 {
722 static bool s_hasInitialized = FALSE;
723
724 if (!s_hasInitialized)
725 {
726 gdk_rgb_init();
727 s_hasInitialized = TRUE;
728 }
729
730 GdkGC *gc = gdk_gc_new( GetPixmap() );
731
732 gdk_draw_rgb_image( GetPixmap(),
733 gc,
734 0, 0,
735 width, height,
736 GDK_RGB_DITHER_NONE,
737 image.GetData(),
738 width*3 );
739
740 gdk_gc_unref( gc );
741 return TRUE;
742 }
743
744 // Create picture image
745
746 GdkImage *data_image =
747 gdk_image_new( GDK_IMAGE_FASTEST, visual, width, height );
748
749 // Create mask image
750
751 GdkImage *mask_image = (GdkImage*) NULL;
752
753 if (image.HasMask())
754 {
755 unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
756
757 mask_image = gdk_image_new_bitmap( visual, mask_data, width, height );
758
759 wxMask *mask = new wxMask();
760 mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 );
761
762 SetMask( mask );
763 }
764
765 // Render
766
767 enum byte_order { RGB, RBG, BRG, BGR, GRB, GBR };
768 byte_order b_o = RGB;
769
770 if (bpp > 8)
771 {
772 if ((visual->red_mask > visual->green_mask) && (visual->green_mask > visual->blue_mask)) b_o = RGB;
773 else if ((visual->red_mask > visual->blue_mask) && (visual->blue_mask > visual->green_mask)) b_o = RBG;
774 else if ((visual->blue_mask > visual->red_mask) && (visual->red_mask > visual->green_mask)) b_o = BRG;
775 else if ((visual->blue_mask > visual->green_mask) && (visual->green_mask > visual->red_mask)) b_o = BGR;
776 else if ((visual->green_mask > visual->red_mask) && (visual->red_mask > visual->blue_mask)) b_o = GRB;
777 else if ((visual->green_mask > visual->blue_mask) && (visual->blue_mask > visual->red_mask)) b_o = GBR;
778 }
779
780 int r_mask = image.GetMaskRed();
781 int g_mask = image.GetMaskGreen();
782 int b_mask = image.GetMaskBlue();
783
784 unsigned char* data = image.GetData();
785
786 int index = 0;
787 for (int y = 0; y < height; y++)
788 {
789 for (int x = 0; x < width; x++)
790 {
791 int r = data[index];
792 index++;
793 int g = data[index];
794 index++;
795 int b = data[index];
796 index++;
797
798 if (image.HasMask())
799 {
800 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
801 gdk_image_put_pixel( mask_image, x, y, 1 );
802 else
803 gdk_image_put_pixel( mask_image, x, y, 0 );
804 }
805
806 switch (bpp)
807 {
808 case 8:
809 {
810 int pixel = -1;
811 if (wxTheApp->m_colorCube)
812 {
813 pixel = wxTheApp->m_colorCube[ ((r & 0xf8) << 7) + ((g & 0xf8) << 2) + ((b & 0xf8) >> 3) ];
814 }
815 else
816 {
817 GdkColormap *cmap = gtk_widget_get_default_colormap();
818 GdkColor *colors = cmap->colors;
819 int max = 3 * (65536);
820
821 for (int i = 0; i < cmap->size; i++)
822 {
823 int rdiff = (r << 8) - colors[i].red;
824 int gdiff = (g << 8) - colors[i].green;
825 int bdiff = (b << 8) - colors[i].blue;
826 int sum = ABS (rdiff) + ABS (gdiff) + ABS (bdiff);
827 if (sum < max) { pixel = i; max = sum; }
828 }
829 }
830
831 gdk_image_put_pixel( data_image, x, y, pixel );
832
833 break;
834 }
835 case 12: // SGI only
836 {
837 guint32 pixel = 0;
838 switch (b_o)
839 {
840 case RGB: pixel = ((r & 0xf0) << 4) | (g & 0xf0) | ((b & 0xf0) >> 4); break;
841 case RBG: pixel = ((r & 0xf0) << 4) | (b & 0xf0) | ((g & 0xf0) >> 4); break;
842 case GRB: pixel = ((g & 0xf0) << 4) | (r & 0xf0) | ((b & 0xf0) >> 4); break;
843 case GBR: pixel = ((g & 0xf0) << 4) | (b & 0xf0) | ((r & 0xf0) >> 4); break;
844 case BRG: pixel = ((b & 0xf0) << 4) | (r & 0xf0) | ((g & 0xf0) >> 4); break;
845 case BGR: pixel = ((b & 0xf0) << 4) | (g & 0xf0) | ((r & 0xf0) >> 4); break;
846 }
847 gdk_image_put_pixel( data_image, x, y, pixel );
848 break;
849 }
850 case 15:
851 {
852 guint32 pixel = 0;
853 switch (b_o)
854 {
855 case RGB: pixel = ((r & 0xf8) << 7) | ((g & 0xf8) << 2) | ((b & 0xf8) >> 3); break;
856 case RBG: pixel = ((r & 0xf8) << 7) | ((b & 0xf8) << 2) | ((g & 0xf8) >> 3); break;
857 case GRB: pixel = ((g & 0xf8) << 7) | ((r & 0xf8) << 2) | ((b & 0xf8) >> 3); break;
858 case GBR: pixel = ((g & 0xf8) << 7) | ((b & 0xf8) << 2) | ((r & 0xf8) >> 3); break;
859 case BRG: pixel = ((b & 0xf8) << 7) | ((r & 0xf8) << 2) | ((g & 0xf8) >> 3); break;
860 case BGR: pixel = ((b & 0xf8) << 7) | ((g & 0xf8) << 2) | ((r & 0xf8) >> 3); break;
861 }
862 gdk_image_put_pixel( data_image, x, y, pixel );
863 break;
864 }
865 case 16:
866 {
867 // I actually don't know if for 16-bit displays, it is alway the green
868 // component or the second component which has 6 bits.
869 guint32 pixel = 0;
870 switch (b_o)
871 {
872 case RGB: pixel = ((r & 0xf8) << 8) | ((g & 0xfc) << 3) | ((b & 0xf8) >> 3); break;
873 case RBG: pixel = ((r & 0xf8) << 8) | ((b & 0xfc) << 3) | ((g & 0xf8) >> 3); break;
874 case GRB: pixel = ((g & 0xf8) << 8) | ((r & 0xfc) << 3) | ((b & 0xf8) >> 3); break;
875 case GBR: pixel = ((g & 0xf8) << 8) | ((b & 0xfc) << 3) | ((r & 0xf8) >> 3); break;
876 case BRG: pixel = ((b & 0xf8) << 8) | ((r & 0xfc) << 3) | ((g & 0xf8) >> 3); break;
877 case BGR: pixel = ((b & 0xf8) << 8) | ((g & 0xfc) << 3) | ((r & 0xf8) >> 3); break;
878 }
879 gdk_image_put_pixel( data_image, x, y, pixel );
880 break;
881 }
882 case 32:
883 case 24:
884 {
885 guint32 pixel = 0;
886 switch (b_o)
887 {
888 case RGB: pixel = (r << 16) | (g << 8) | b; break;
889 case RBG: pixel = (r << 16) | (b << 8) | g; break;
890 case BRG: pixel = (b << 16) | (r << 8) | g; break;
891 case BGR: pixel = (b << 16) | (g << 8) | r; break;
892 case GRB: pixel = (g << 16) | (r << 8) | b; break;
893 case GBR: pixel = (g << 16) | (b << 8) | r; break;
894 }
895 gdk_image_put_pixel( data_image, x, y, pixel );
896 }
897 default: break;
898 }
899 } // for
900 } // for
901
902 // Blit picture
903
904 GdkGC *data_gc = gdk_gc_new( GetPixmap() );
905
906 gdk_draw_image( GetPixmap(), data_gc, data_image, 0, 0, 0, 0, width, height );
907
908 gdk_image_destroy( data_image );
909 gdk_gc_unref( data_gc );
910
911 // Blit mask
912
913 if (image.HasMask())
914 {
915 GdkGC *mask_gc = gdk_gc_new( GetMask()->GetBitmap() );
916
917 gdk_draw_image( GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height );
918
919 gdk_image_destroy( mask_image );
920 gdk_gc_unref( mask_gc );
921 }
922
923 return true;
924}
925
926#ifdef __WXGTK20__
927bool wxBitmap::CreateFromImageAsPixbuf(const wxImage& image)
928{
929 int width = image.GetWidth();
930 int height = image.GetHeight();
931
932 GdkPixbuf *pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB,
933 image.HasAlpha(),
934 8 /* bits per sample */,
935 width, height);
936 if (!pixbuf)
937 return false;
938
939 wxASSERT( image.HasAlpha() ); // for now
940 wxASSERT( gdk_pixbuf_get_n_channels(pixbuf) == 4 );
941 wxASSERT( gdk_pixbuf_get_width(pixbuf) == width );
942 wxASSERT( gdk_pixbuf_get_height(pixbuf) == height );
943
944 M_BMPDATA->m_pixbuf = pixbuf;
945 SetHeight(height);
946 SetWidth(width);
947 SetDepth(wxTheApp->GetGdkVisual()->depth);
948
949 // Copy the data:
950 unsigned char *in = image.GetData();
951 unsigned char *out = gdk_pixbuf_get_pixels(pixbuf);
952 unsigned char *alpha = image.GetAlpha();
953
954 int rowinc = gdk_pixbuf_get_rowstride(pixbuf) - 4 * width;
955
956 for (int y = 0; y < height; y++, out += rowinc)
957 {
958 for (int x = 0; x < width; x++, alpha++, out += 4, in += 3)
959 {
960 out[0] = in[0];
961 out[1] = in[1];
962 out[2] = in[2];
963 out[3] = *alpha;
964 }
965 }
966
967 return true;
968}
969#endif // __WXGTK20__
970
971wxImage wxBitmap::ConvertToImage() const
972{
973 wxImage image;
974
975 wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
976
977 image.Create(GetWidth(), GetHeight());
978 unsigned char *data = image.GetData();
979
980 if (!data)
981 {
982 wxFAIL_MSG( wxT("couldn't create image") );
983 return wxNullImage;
984 }
985
986#ifdef __WXGTK20__
987 if (HasPixbuf())
988 {
989 GdkPixbuf *pixbuf = GetPixbuf();
990 wxASSERT( gdk_pixbuf_get_has_alpha(pixbuf) );
991
992 int w = GetWidth();
993 int h = GetHeight();
994
995 image.SetAlpha();
996
997 unsigned char *alpha = image.GetAlpha();
998 unsigned char *in = gdk_pixbuf_get_pixels(pixbuf);
999 unsigned char *out = data;
1000 int rowinc = gdk_pixbuf_get_rowstride(pixbuf) - 4 * w;
1001
1002 for (int y = 0; y < h; y++, in += rowinc)
1003 {
1004 for (int x = 0; x < w; x++, in += 4, out += 3, alpha++)
1005 {
1006 out[0] = in[0];
1007 out[1] = in[1];
1008 out[2] = in[2];
1009 *alpha = in[3];
1010 }
1011 }
1012 }
1013 else
1014#endif // __WXGTK20__
1015 {
1016 // the colour used as transparent one in wxImage and the one it is
1017 // replaced with when it really occurs in the bitmap
1018 static const int MASK_RED = 1;
1019 static const int MASK_GREEN = 2;
1020 static const int MASK_BLUE = 3;
1021 static const int MASK_BLUE_REPLACEMENT = 2;
1022
1023 GdkImage *gdk_image = (GdkImage*) NULL;
1024
1025 if (HasPixmap())
1026 {
1027 gdk_image = gdk_image_get( GetPixmap(),
1028 0, 0,
1029 GetWidth(), GetHeight() );
1030 }
1031 else if (GetBitmap())
1032 {
1033 gdk_image = gdk_image_get( GetBitmap(),
1034 0, 0,
1035 GetWidth(), GetHeight() );
1036 }
1037 else
1038 {
1039 wxFAIL_MSG( wxT("Ill-formed bitmap") );
1040 }
1041
1042 wxCHECK_MSG( gdk_image, wxNullImage, wxT("couldn't create image") );
1043
1044 GdkImage *gdk_image_mask = (GdkImage*) NULL;
1045 if (GetMask())
1046 {
1047 gdk_image_mask = gdk_image_get( GetMask()->GetBitmap(),
1048 0, 0,
1049 GetWidth(), GetHeight() );
1050
1051 image.SetMaskColour( MASK_RED, MASK_GREEN, MASK_BLUE );
1052 }
1053
1054 int bpp = -1;
1055 int red_shift_right = 0;
1056 int green_shift_right = 0;
1057 int blue_shift_right = 0;
1058 int red_shift_left = 0;
1059 int green_shift_left = 0;
1060 int blue_shift_left = 0;
1061 bool use_shift = FALSE;
1062
1063 if (GetPixmap())
1064 {
1065 GdkVisual *visual = gdk_window_get_visual( GetPixmap() );
1066 if (visual == NULL)
1067 visual = wxTheApp->GetGdkVisual();
1068
1069 bpp = visual->depth;
1070 if (bpp == 16)
1071 bpp = visual->red_prec + visual->green_prec + visual->blue_prec;
1072 red_shift_right = visual->red_shift;
1073 red_shift_left = 8-visual->red_prec;
1074 green_shift_right = visual->green_shift;
1075 green_shift_left = 8-visual->green_prec;
1076 blue_shift_right = visual->blue_shift;
1077 blue_shift_left = 8-visual->blue_prec;
1078
1079 use_shift = (visual->type == GDK_VISUAL_TRUE_COLOR) || (visual->type == GDK_VISUAL_DIRECT_COLOR);
1080 }
1081 if (GetBitmap())
1082 {
1083 bpp = 1;
1084 }
1085
1086
1087 GdkColormap *cmap = gtk_widget_get_default_colormap();
1088
1089 long pos = 0;
1090 for (int j = 0; j < GetHeight(); j++)
1091 {
1092 for (int i = 0; i < GetWidth(); i++)
1093 {
1094 wxUint32 pixel = gdk_image_get_pixel( gdk_image, i, j );
1095 if (bpp == 1)
1096 {
1097 if (pixel == 0)
1098 {
1099 data[pos] = 0;
1100 data[pos+1] = 0;
1101 data[pos+2] = 0;
1102 }
1103 else
1104 {
1105 data[pos] = 255;
1106 data[pos+1] = 255;
1107 data[pos+2] = 255;
1108 }
1109 }
1110 else if (use_shift)
1111 {
1112 data[pos] = (pixel >> red_shift_right) << red_shift_left;
1113 data[pos+1] = (pixel >> green_shift_right) << green_shift_left;
1114 data[pos+2] = (pixel >> blue_shift_right) << blue_shift_left;
1115 }
1116 else if (cmap->colors)
1117 {
1118 data[pos] = cmap->colors[pixel].red >> 8;
1119 data[pos+1] = cmap->colors[pixel].green >> 8;
1120 data[pos+2] = cmap->colors[pixel].blue >> 8;
1121 }
1122 else
1123 {
1124 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
1125 }
1126
1127 if (gdk_image_mask)
1128 {
1129 int mask_pixel = gdk_image_get_pixel( gdk_image_mask, i, j );
1130 if (mask_pixel == 0)
1131 {
1132 data[pos] = MASK_RED;
1133 data[pos+1] = MASK_GREEN;
1134 data[pos+2] = MASK_BLUE;
1135 }
1136 else if ( data[pos] == MASK_RED &&
1137 data[pos+1] == MASK_GREEN &&
1138 data[pos+2] == MASK_BLUE )
1139 {
1140 data[pos+2] = MASK_BLUE_REPLACEMENT;
1141 }
1142 }
1143
1144 pos += 3;
1145 }
1146 }
1147
1148 gdk_image_destroy( gdk_image );
1149 if (gdk_image_mask) gdk_image_destroy( gdk_image_mask );
1150 }
1151
1152 return image;
1153}
1154
1155wxBitmap::wxBitmap( const wxBitmap& bmp )
1156 : wxGDIObject()
1157{
1158 Ref( bmp );
1159}
1160
1161wxBitmap::wxBitmap( const wxString &filename, int type )
1162{
1163 LoadFile( filename, type );
1164}
1165
1166wxBitmap::wxBitmap( const char bits[], int width, int height, int WXUNUSED(depth))
1167{
1168 if ( width > 0 && height > 0 )
1169 {
1170 m_refData = new wxBitmapRefData();
1171
1172 M_BMPDATA->m_mask = (wxMask *) NULL;
1173 M_BMPDATA->m_bitmap = gdk_bitmap_create_from_data
1174 (
1175 wxGetRootWindow()->window,
1176 (gchar *) bits,
1177 width,
1178 height
1179 );
1180 M_BMPDATA->m_width = width;
1181 M_BMPDATA->m_height = height;
1182 M_BMPDATA->m_bpp = 1;
1183
1184 wxASSERT_MSG( M_BMPDATA->m_bitmap, wxT("couldn't create bitmap") );
1185 }
1186}
1187
1188wxBitmap::~wxBitmap()
1189{
1190}
1191
1192wxBitmap& wxBitmap::operator = ( const wxBitmap& bmp )
1193{
1194 if ( m_refData != bmp.m_refData )
1195 Ref( bmp );
1196
1197 return *this;
1198}
1199
1200bool wxBitmap::operator == ( const wxBitmap& bmp ) const
1201{
1202 return m_refData == bmp.m_refData;
1203}
1204
1205bool wxBitmap::operator != ( const wxBitmap& bmp ) const
1206{
1207 return m_refData != bmp.m_refData;
1208}
1209
1210bool wxBitmap::Ok() const
1211{
1212 return (m_refData != NULL) &&
1213 (
1214#ifdef __WXGTK20__
1215 M_BMPDATA->m_pixbuf ||
1216#endif
1217 M_BMPDATA->m_bitmap || M_BMPDATA->m_pixmap
1218 );
1219}
1220
1221int wxBitmap::GetHeight() const
1222{
1223 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1224
1225 return M_BMPDATA->m_height;
1226}
1227
1228int wxBitmap::GetWidth() const
1229{
1230 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1231
1232 return M_BMPDATA->m_width;
1233}
1234
1235int wxBitmap::GetDepth() const
1236{
1237 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1238
1239 return M_BMPDATA->m_bpp;
1240}
1241
1242wxMask *wxBitmap::GetMask() const
1243{
1244 wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") );
1245
1246 return M_BMPDATA->m_mask;
1247}
1248
1249void wxBitmap::SetMask( wxMask *mask )
1250{
1251 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
1252
1253 if (M_BMPDATA->m_mask) delete M_BMPDATA->m_mask;
1254
1255 M_BMPDATA->m_mask = mask;
1256}
1257
1258bool wxBitmap::CopyFromIcon(const wxIcon& icon)
1259{
1260 *this = icon;
1261 return TRUE;
1262}
1263
1264wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const
1265{
1266 wxCHECK_MSG( Ok() &&
1267 (rect.x >= 0) && (rect.y >= 0) &&
1268 (rect.x+rect.width <= M_BMPDATA->m_width) && (rect.y+rect.height <= M_BMPDATA->m_height),
1269 wxNullBitmap, wxT("invalid bitmap or bitmap region") );
1270
1271 wxBitmap ret( rect.width, rect.height, M_BMPDATA->m_bpp );
1272 wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") );
1273
1274#ifdef __WXGTK20__
1275 if (HasPixbuf())
1276 {
1277 GdkPixbuf *pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB,
1278 gdk_pixbuf_get_has_alpha(GetPixbuf()),
1279 8, GetWidth(), GetHeight());
1280 ret.SetPixbuf(pixbuf);
1281 gdk_pixbuf_copy_area(GetPixbuf(),
1282 rect.x, rect.y, rect.width, rect.height,
1283 pixbuf, 0, 0);
1284 }
1285 else
1286#endif // __WXGTK20__
1287 {
1288 if (ret.GetPixmap())
1289 {
1290 GdkGC *gc = gdk_gc_new( ret.GetPixmap() );
1291 gdk_draw_pixmap( ret.GetPixmap(), gc, GetPixmap(), rect.x, rect.y, 0, 0, rect.width, rect.height );
1292 gdk_gc_destroy( gc );
1293 }
1294 else
1295 {
1296 GdkGC *gc = gdk_gc_new( ret.GetBitmap() );
1297 GdkColor col;
1298 col.pixel = 0xFFFFFF;
1299 gdk_gc_set_foreground( gc, &col );
1300 col.pixel = 0;
1301 gdk_gc_set_background( gc, &col );
1302 gdk_wx_draw_bitmap( ret.GetBitmap(), gc, GetBitmap(), rect.x, rect.y, 0, 0, rect.width, rect.height );
1303 gdk_gc_destroy( gc );
1304 }
1305 }
1306
1307 if (GetMask())
1308 {
1309 wxMask *mask = new wxMask;
1310 mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, rect.width, rect.height, 1 );
1311
1312 GdkGC *gc = gdk_gc_new( mask->m_bitmap );
1313 GdkColor col;
1314 col.pixel = 0xFFFFFF;
1315 gdk_gc_set_foreground( gc, &col );
1316 col.pixel = 0;
1317 gdk_gc_set_background( gc, &col );
1318 gdk_wx_draw_bitmap( mask->m_bitmap, gc, M_BMPDATA->m_mask->m_bitmap, rect.x, rect.y, 0, 0, rect.width, rect.height );
1319 gdk_gc_destroy( gc );
1320
1321 ret.SetMask( mask );
1322 }
1323
1324 return ret;
1325}
1326
1327bool wxBitmap::SaveFile( const wxString &name, int type, wxPalette *WXUNUSED(palette) )
1328{
1329 wxCHECK_MSG( Ok(), FALSE, wxT("invalid bitmap") );
1330
1331 // Try to save the bitmap via wxImage handlers:
1332 {
1333 wxImage image = ConvertToImage();
1334 if (image.Ok()) return image.SaveFile( name, type );
1335 }
1336
1337 return FALSE;
1338}
1339
1340bool wxBitmap::LoadFile( const wxString &name, int type )
1341{
1342 UnRef();
1343
1344 if (!wxFileExists(name))
1345 return FALSE;
1346
1347 GdkVisual *visual = wxTheApp->GetGdkVisual();
1348
1349 if (type == wxBITMAP_TYPE_XPM)
1350 {
1351 m_refData = new wxBitmapRefData();
1352
1353 GdkBitmap *mask = (GdkBitmap*) NULL;
1354
1355 M_BMPDATA->m_pixmap = gdk_pixmap_create_from_xpm
1356 (
1357 wxGetRootWindow()->window,
1358 &mask,
1359 NULL,
1360 name.fn_str()
1361 );
1362
1363 if (mask)
1364 {
1365 M_BMPDATA->m_mask = new wxMask();
1366 M_BMPDATA->m_mask->m_bitmap = mask;
1367 }
1368
1369 gdk_window_get_size( M_BMPDATA->m_pixmap, &(M_BMPDATA->m_width), &(M_BMPDATA->m_height) );
1370
1371 M_BMPDATA->m_bpp = visual->depth;
1372 }
1373 else // try if wxImage can load it
1374 {
1375 wxImage image;
1376 if ( !image.LoadFile( name, type ) || !image.Ok() )
1377 return FALSE;
1378
1379 *this = wxBitmap(image);
1380 }
1381
1382 return TRUE;
1383}
1384
1385wxPalette *wxBitmap::GetPalette() const
1386{
1387 if (!Ok())
1388 return (wxPalette *) NULL;
1389
1390 return M_BMPDATA->m_palette;
1391}
1392
1393void wxBitmap::SetHeight( int height )
1394{
1395 if (!m_refData)
1396 m_refData = new wxBitmapRefData();
1397
1398 M_BMPDATA->m_height = height;
1399}
1400
1401void wxBitmap::SetWidth( int width )
1402{
1403 if (!m_refData)
1404 m_refData = new wxBitmapRefData();
1405
1406 M_BMPDATA->m_width = width;
1407}
1408
1409void wxBitmap::SetDepth( int depth )
1410{
1411 if (!m_refData)
1412 m_refData = new wxBitmapRefData();
1413
1414 M_BMPDATA->m_bpp = depth;
1415}
1416
1417void wxBitmap::SetPixmap( GdkPixmap *pixmap )
1418{
1419 if (!m_refData)
1420 m_refData = new wxBitmapRefData();
1421
1422 M_BMPDATA->m_pixmap = pixmap;
1423#ifdef __WXGTK20__
1424 PurgeOtherRepresentations(Pixmap);
1425#endif
1426}
1427
1428void wxBitmap::SetBitmap( GdkPixmap *bitmap )
1429{
1430 if (!m_refData)
1431 m_refData = new wxBitmapRefData();
1432
1433 M_BMPDATA->m_bitmap = bitmap;
1434#ifdef __WXGTK20__
1435 PurgeOtherRepresentations(Pixmap);
1436#endif
1437}
1438
1439GdkPixmap *wxBitmap::GetPixmap() const
1440{
1441 wxCHECK_MSG( Ok(), (GdkPixmap *) NULL, wxT("invalid bitmap") );
1442
1443#ifdef __WXGTK20__
1444 // create the pixmap on the fly if we use Pixbuf representation:
1445 if (HasPixbuf() && !HasPixmap())
1446 {
1447 gdk_pixbuf_render_pixmap_and_mask(M_BMPDATA->m_pixbuf,
1448 &M_BMPDATA->m_pixmap,
1449 NULL /*mask*/,
1450 128 /*threshold*/);
1451 }
1452#endif // __WXGTK20__
1453
1454 return M_BMPDATA->m_pixmap;
1455}
1456
1457bool wxBitmap::HasPixmap() const
1458{
1459 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1460
1461 return M_BMPDATA->m_pixmap != NULL;
1462}
1463
1464GdkBitmap *wxBitmap::GetBitmap() const
1465{
1466 wxCHECK_MSG( Ok(), (GdkBitmap *) NULL, wxT("invalid bitmap") );
1467
1468 return M_BMPDATA->m_bitmap;
1469}
1470
1471#ifdef __WXGTK20__
1472GdkPixbuf *wxBitmap::GetPixbuf() const
1473{
1474 wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") );
1475
1476 if (HasPixmap() && !HasPixbuf())
1477 {
1478 int width = GetWidth();
1479 int height = GetHeight();
1480
1481 GdkPixbuf *pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB,
1482 GetMask() != NULL,
1483 8, width, height);
1484 M_BMPDATA->m_pixbuf =
1485 gdk_pixbuf_get_from_drawable(pixbuf, M_BMPDATA->m_pixmap, NULL,
1486 0, 0, 0, 0, width, height);
1487
1488 // apply the mask to created pixbuf:
1489 if (M_BMPDATA->m_pixbuf && M_BMPDATA->m_mask)
1490 {
1491 GdkPixbuf *pmask =
1492 gdk_pixbuf_get_from_drawable(NULL,
1493 M_BMPDATA->m_mask->GetBitmap(),
1494 NULL,
1495 0, 0, 0, 0, width, height);
1496 if (pmask)
1497 {
1498 guchar *bmp = gdk_pixbuf_get_pixels(pixbuf);
1499 guchar *mask = gdk_pixbuf_get_pixels(pmask);
1500 int bmprowinc = gdk_pixbuf_get_rowstride(pixbuf) - 4 * width;
1501 int maskrowinc = gdk_pixbuf_get_rowstride(pmask) - 3 * width;
1502
1503 for (int y = 0; y < height;
1504 y++, bmp += bmprowinc, mask += maskrowinc)
1505 {
1506 for (int x = 0; x < width; x++, bmp += 4, mask += 3)
1507 {
1508 if (mask[0] == 0 /*black pixel*/)
1509 bmp[3] = 0;
1510 }
1511 }
1512
1513 gdk_pixbuf_unref(pmask);
1514 }
1515 }
1516 }
1517
1518 return M_BMPDATA->m_pixbuf;
1519}
1520
1521bool wxBitmap::HasPixbuf() const
1522{
1523 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1524
1525 return M_BMPDATA->m_pixbuf != NULL;
1526}
1527
1528void wxBitmap::SetPixbuf( GdkPixbuf *pixbuf )
1529{
1530 if (!m_refData)
1531 m_refData = new wxBitmapRefData();
1532
1533 M_BMPDATA->m_pixbuf = pixbuf;
1534 PurgeOtherRepresentations(Pixbuf);
1535}
1536
1537void wxBitmap::PurgeOtherRepresentations(wxBitmap::Representation keep)
1538{
1539 if (keep == Pixmap && HasPixbuf())
1540 {
1541 gdk_pixbuf_unref( M_BMPDATA->m_pixbuf );
1542 M_BMPDATA->m_pixbuf = NULL;
1543 }
1544 if (keep == Pixbuf && HasPixmap())
1545 {
1546 gdk_pixmap_unref( M_BMPDATA->m_pixmap );
1547 M_BMPDATA->m_pixmap = NULL;
1548 }
1549}
1550
1551#endif // __WXGTK20__