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