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