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