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