more accurate mask creation for low color displays
[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/bitmap.h"
14
15 #ifndef WX_PRECOMP
16 #include "wx/app.h"
17 #include "wx/palette.h"
18 #include "wx/icon.h"
19 #include "wx/math.h"
20 #include "wx/image.h"
21 #endif
22
23 #include "wx/rawbmp.h"
24
25 #include <gtk/gtk.h>
26
27 extern void gdk_wx_draw_bitmap (GdkDrawable *drawable,
28 GdkGC *gc,
29 GdkDrawable *src,
30 gint xsrc,
31 gint ysrc,
32 gint xdest,
33 gint ydest,
34 gint width,
35 gint height);
36
37 //-----------------------------------------------------------------------------
38 // data
39 //-----------------------------------------------------------------------------
40
41 extern GtkWidget *wxGetRootWindow();
42
43 //-----------------------------------------------------------------------------
44 // wxMask
45 //-----------------------------------------------------------------------------
46
47 IMPLEMENT_DYNAMIC_CLASS(wxMask,wxObject)
48
49 wxMask::wxMask()
50 {
51 m_bitmap = (GdkBitmap *) NULL;
52 }
53
54 wxMask::wxMask( const wxBitmap& bitmap, const wxColour& colour )
55 {
56 m_bitmap = (GdkBitmap *) NULL;
57 Create( bitmap, colour );
58 }
59
60 #if wxUSE_PALETTE
61 wxMask::wxMask( const wxBitmap& bitmap, int paletteIndex )
62 {
63 m_bitmap = (GdkBitmap *) NULL;
64 Create( bitmap, paletteIndex );
65 }
66 #endif // wxUSE_PALETTE
67
68 wxMask::wxMask( const wxBitmap& bitmap )
69 {
70 m_bitmap = (GdkBitmap *) NULL;
71 Create( bitmap );
72 }
73
74 wxMask::~wxMask()
75 {
76 if (m_bitmap)
77 g_object_unref (m_bitmap);
78 }
79
80 bool wxMask::Create( const wxBitmap& bitmap,
81 const wxColour& colour )
82 {
83 if (m_bitmap)
84 {
85 g_object_unref (m_bitmap);
86 m_bitmap = (GdkBitmap*) NULL;
87 }
88
89 const int w = bitmap.GetWidth();
90 const int h = bitmap.GetHeight();
91 m_bitmap = gdk_pixmap_new(wxGetRootWindow()->window, w, h, 1);
92 GdkGC *gc = gdk_gc_new( m_bitmap );
93
94 GdkColor color;
95 color.pixel = 1;
96 gdk_gc_set_foreground( gc, &color );
97 gdk_draw_rectangle(m_bitmap, gc, true, 0, 0, w, h);
98
99 unsigned char red = colour.Red();
100 unsigned char green = colour.Green();
101 unsigned char blue = colour.Blue();
102 GdkImage* image = NULL;
103 wxByte* data = NULL;
104 guint32 mask_pixel = 1;
105 int rowpadding = 0;
106 int data_inc = 0;
107
108 if (bitmap.HasPixbuf())
109 {
110 GdkPixbuf* pixbuf = bitmap.GetPixbuf();
111 data = gdk_pixbuf_get_pixels(pixbuf);
112 data_inc = 3 + int(gdk_pixbuf_get_has_alpha(pixbuf) != 0);
113 rowpadding = gdk_pixbuf_get_rowstride(pixbuf) - data_inc * w;
114 }
115 else
116 {
117 image = gdk_drawable_get_image(bitmap.GetPixmap(), 0, 0, w, h);
118 GdkColormap* colormap = gdk_image_get_colormap(image);
119 if (colormap != NULL)
120 {
121 wxColor c(colour);
122 c.CalcPixel(colormap);
123 mask_pixel = c.GetPixel();
124 }
125 }
126
127 color.pixel = 0;
128 gdk_gc_set_foreground( gc, &color );
129
130 for (int y = 0; y < h; y++)
131 {
132 int start_x = -1;
133 int x;
134 for (x = 0; x < w; x++)
135 {
136 bool isMask;
137 if (image != NULL)
138 isMask = gdk_image_get_pixel(image, x, y) == mask_pixel;
139 else
140 {
141 isMask = data[0] == red && data[1] == green && data[2] == blue;
142 data += data_inc;
143 }
144 if (isMask)
145 {
146 if (start_x == -1)
147 start_x = x;
148 }
149 else
150 {
151 if (start_x != -1)
152 {
153 gdk_draw_line(m_bitmap, gc, start_x, y, x - 1, y);
154 start_x = -1;
155 }
156 }
157 }
158 if (start_x != -1)
159 gdk_draw_line(m_bitmap, gc, start_x, y, x, y);
160 data += rowpadding;
161 }
162 if (image != NULL)
163 g_object_unref(image);
164 g_object_unref (gc);
165
166 return true;
167 }
168
169 #if wxUSE_PALETTE
170 bool wxMask::Create( const wxBitmap& bitmap, int paletteIndex )
171 {
172 unsigned char r,g,b;
173 wxPalette *pal = bitmap.GetPalette();
174
175 wxCHECK_MSG( pal, false, wxT("Cannot create mask from bitmap without palette") );
176
177 pal->GetRGB(paletteIndex, &r, &g, &b);
178
179 return Create(bitmap, wxColour(r, g, b));
180 }
181 #endif // wxUSE_PALETTE
182
183 bool wxMask::Create( const wxBitmap& bitmap )
184 {
185 if (m_bitmap)
186 {
187 g_object_unref (m_bitmap);
188 m_bitmap = (GdkBitmap*) NULL;
189 }
190
191 if (!bitmap.Ok()) return false;
192
193 wxCHECK_MSG( bitmap.GetDepth() == 1, false, wxT("Cannot create mask from colour bitmap") );
194
195 m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, bitmap.GetWidth(), bitmap.GetHeight(), 1 );
196
197 if (!m_bitmap) return false;
198
199 GdkGC *gc = gdk_gc_new( m_bitmap );
200
201 gdk_wx_draw_bitmap( m_bitmap, gc, bitmap.GetPixmap(), 0, 0, 0, 0, bitmap.GetWidth(), bitmap.GetHeight() );
202
203 g_object_unref (gc);
204
205 return true;
206 }
207
208 GdkBitmap *wxMask::GetBitmap() const
209 {
210 return m_bitmap;
211 }
212
213 //-----------------------------------------------------------------------------
214 // wxBitmap
215 //-----------------------------------------------------------------------------
216
217 class wxBitmapRefData: public wxObjectRefData
218 {
219 public:
220 wxBitmapRefData();
221 ~wxBitmapRefData();
222
223 GdkPixmap *m_pixmap;
224 GdkPixbuf *m_pixbuf;
225 wxMask *m_mask;
226 int m_width;
227 int m_height;
228 int m_bpp;
229 wxPalette *m_palette;
230 };
231
232 wxBitmapRefData::wxBitmapRefData()
233 {
234 m_pixmap = (GdkPixmap *) NULL;
235 m_pixbuf = (GdkPixbuf *) NULL;
236 m_mask = (wxMask *) NULL;
237 m_width = 0;
238 m_height = 0;
239 m_bpp = 0;
240 m_palette = (wxPalette *) NULL;
241 }
242
243 wxBitmapRefData::~wxBitmapRefData()
244 {
245 if (m_pixmap)
246 g_object_unref (m_pixmap);
247 if (m_pixbuf)
248 g_object_unref (m_pixbuf);
249 delete m_mask;
250 #if wxUSE_PALETTE
251 delete m_palette;
252 #endif // wxUSE_PALETTE
253 }
254
255 //-----------------------------------------------------------------------------
256
257 #define M_BMPDATA wx_static_cast(wxBitmapRefData*, m_refData)
258
259 IMPLEMENT_DYNAMIC_CLASS(wxBitmap,wxGDIObject)
260
261 wxBitmap::wxBitmap()
262 {
263 }
264
265 wxBitmap::wxBitmap( int width, int height, int depth )
266 {
267 Create( width, height, depth );
268 }
269
270 bool wxBitmap::Create( int width, int height, int depth )
271 {
272 UnRef();
273
274 if ( width <= 0 || height <= 0 )
275 {
276 return false;
277 }
278
279 if (depth == 32)
280 {
281 SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB, true, 8, width, height));
282 M_BMPDATA->m_bpp = 32;
283 }
284 else
285 {
286 if (depth != 1)
287 {
288 const GdkVisual* visual = wxTheApp->GetGdkVisual();
289 if (depth == -1)
290 depth = visual->depth;
291
292 wxCHECK_MSG(depth == visual->depth, false, wxT("invalid bitmap depth"));
293 }
294
295 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window, width, height, depth));
296 }
297
298 return Ok();
299 }
300
301 bool wxBitmap::CreateFromXpm( const char **bits )
302 {
303 UnRef();
304
305 wxCHECK_MSG( bits != NULL, false, wxT("invalid bitmap data") );
306
307 GdkBitmap *mask = (GdkBitmap*) NULL;
308 SetPixmap(gdk_pixmap_create_from_xpm_d(wxGetRootWindow()->window, &mask, NULL, (gchar**)bits));
309
310 wxCHECK_MSG( M_BMPDATA->m_pixmap, false, wxT("couldn't create pixmap") );
311
312 if (mask)
313 {
314 M_BMPDATA->m_mask = new wxMask;
315 M_BMPDATA->m_mask->m_bitmap = mask;
316 }
317
318 return true;
319 }
320
321 wxBitmap wxBitmap::Rescale( int clipx, int clipy, int clipwidth, int clipheight, int newx, int newy )
322 {
323 wxBitmap bmp;
324
325 wxCHECK_MSG(Ok(), bmp, wxT("invalid bitmap"));
326
327 if (newy==M_BMPDATA->m_width && newy==M_BMPDATA->m_height)
328 return *this;
329
330 int width = wxMax(newx, 1);
331 int height = wxMax(newy, 1);
332 width = wxMin(width, clipwidth);
333 height = wxMin(height, clipheight);
334
335 if (HasPixbuf())
336 {
337 bmp.SetDepth(GetDepth());
338 bmp.SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB,
339 gdk_pixbuf_get_has_alpha(GetPixbuf()),
340 8, width, height));
341 gdk_pixbuf_scale(GetPixbuf(), bmp.GetPixbuf(),
342 0, 0, width, height,
343 clipx, clipy,
344 (double)newx/GetWidth(), (double)newy/GetHeight(),
345 GDK_INTERP_BILINEAR);
346 }
347 else
348 {
349 GdkImage* img = gdk_drawable_get_image(GetPixmap(), 0, 0, GetWidth(), GetHeight());
350
351 wxCHECK_MSG(img, bmp, wxT("couldn't create image"));
352
353 GdkGC *gc = NULL;
354 GdkPixmap *dstpix = NULL;
355 char *dst = NULL;
356 long dstbyteperline = 0;
357
358 if (GetDepth() != 1)
359 {
360 GdkVisual *visual = gdk_drawable_get_visual( GetPixmap() );
361 if (visual == NULL)
362 visual = wxTheApp->GetGdkVisual();
363
364 bmp = wxBitmap(width, height, visual->depth);
365 dstpix = bmp.GetPixmap();
366 gc = gdk_gc_new( dstpix );
367 }
368 else
369 {
370 dstbyteperline = (width + 7) / 8;
371 dst = (char*) malloc(dstbyteperline*height);
372 }
373
374 // be careful to use the right scaling factor
375 float scx = (float)M_BMPDATA->m_width/(float)newx;
376 float scy = (float)M_BMPDATA->m_height/(float)newy;
377 // prepare accel-tables
378 int *tablex = (int *)calloc(width,sizeof(int));
379 int *tabley = (int *)calloc(height,sizeof(int));
380
381 // accel table filled with clipped values
382 for (int x = 0; x < width; x++)
383 tablex[x] = (int) (scx * (x+clipx));
384 for (int y = 0; y < height; y++)
385 tabley[y] = (int) (scy * (y+clipy));
386
387 // Main rescaling routine starts here
388 for (int h = 0; h < height; h++)
389 {
390 char outbyte = 0;
391 int old_x = -1;
392 guint32 old_pixval = 0;
393
394 for (int w = 0; w < width; w++)
395 {
396 guint32 pixval;
397 int x = tablex[w];
398 if (x == old_x)
399 pixval = old_pixval;
400 else
401 {
402 pixval = gdk_image_get_pixel( img, x, tabley[h] );
403 old_pixval = pixval;
404 old_x = x;
405 }
406
407 if ( dst )
408 {
409 if (!pixval)
410 {
411 char bit=1;
412 char shift = bit << (w % 8);
413 outbyte |= shift;
414 }
415
416 if ((w+1)%8==0)
417 {
418 dst[h*dstbyteperline+w/8] = outbyte;
419 outbyte = 0;
420 }
421 }
422 else
423 {
424 GdkColor col;
425 col.pixel = pixval;
426 gdk_gc_set_foreground( gc, &col );
427 gdk_draw_point( dstpix, gc, w, h);
428 }
429 }
430
431 // do not forget the last byte
432 if ( dst && (width % 8 != 0) )
433 dst[h*dstbyteperline+width/8] = outbyte;
434 }
435
436 g_object_unref (img);
437 if (gc) g_object_unref (gc);
438
439 if ( dst )
440 {
441 bmp = wxBitmap( (const char *)dst, width, height, 1 );
442 free( dst );
443 }
444
445 if (GetMask())
446 {
447 dstbyteperline = (width + 7) / 8;
448 dst = (char*) malloc(dstbyteperline*height);
449 img = gdk_drawable_get_image(GetMask()->GetBitmap(), 0, 0, GetWidth(), GetHeight());
450
451 for (int h = 0; h < height; h++)
452 {
453 char outbyte = 0;
454 int old_x = -1;
455 guint32 old_pixval = 0;
456
457 for (int w = 0; w < width; w++)
458 {
459 guint32 pixval;
460 int x = tablex[w];
461 if (x == old_x)
462 pixval = old_pixval;
463 else
464 {
465 pixval = gdk_image_get_pixel( img, x, tabley[h] );
466 old_pixval = pixval;
467 old_x = x;
468 }
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
484 // do not forget the last byte
485 if (width % 8 != 0)
486 dst[h*dstbyteperline+width/8] = outbyte;
487 }
488 wxMask* mask = new wxMask;
489 mask->m_bitmap = gdk_bitmap_create_from_data( wxGetRootWindow()->window, (gchar *) dst, width, height );
490 bmp.SetMask(mask);
491
492 free( dst );
493 g_object_unref (img);
494 }
495
496 free( tablex );
497 free( tabley );
498 }
499
500 return bmp;
501 }
502
503 bool wxBitmap::CreateFromImage(const wxImage& image, int depth)
504 {
505 UnRef();
506
507 wxCHECK_MSG( image.Ok(), false, wxT("invalid image") );
508 wxCHECK_MSG( depth == -1 || depth == 1, false, wxT("invalid bitmap depth") );
509
510 if (image.GetWidth() <= 0 || image.GetHeight() <= 0)
511 return false;
512
513 if (depth == 1)
514 return CreateFromImageAsBitmap(image);
515
516 if (image.HasAlpha())
517 return CreateFromImageAsPixbuf(image);
518
519 return CreateFromImageAsPixmap(image);
520 }
521
522 // conversion to mono bitmap:
523 bool wxBitmap::CreateFromImageAsBitmap(const wxImage& img)
524 {
525 // convert alpha channel to mask, if it is present:
526 wxImage image(img);
527 image.ConvertAlphaToMask();
528
529 int width = image.GetWidth();
530 int height = image.GetHeight();
531
532 SetPixmap( gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 ) );
533
534 // Create picture image
535
536 GdkGC* data_gc = gdk_gc_new(M_BMPDATA->m_pixmap);
537 GdkColor color;
538 color.pixel = 1;
539 gdk_gc_set_foreground(data_gc, &color);
540 gdk_draw_rectangle(M_BMPDATA->m_pixmap, data_gc, true, 0, 0, width, height);
541 GdkImage* data_image = gdk_drawable_get_image(M_BMPDATA->m_pixmap, 0, 0, width, height);
542
543 // Create mask image
544
545 GdkImage *mask_image = (GdkImage*) NULL;
546 GdkGC* mask_gc = NULL;
547
548 if (image.HasMask())
549 {
550 wxMask* mask = new wxMask;
551 mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 );
552 mask_gc = gdk_gc_new(mask->m_bitmap);
553 gdk_gc_set_foreground(mask_gc, &color);
554 gdk_draw_rectangle(mask->m_bitmap, mask_gc, true, 0, 0, width, height);
555 mask_image = gdk_drawable_get_image(mask->m_bitmap, 0, 0, width, height);
556
557 SetMask( mask );
558 }
559
560 int r_mask = image.GetMaskRed();
561 int g_mask = image.GetMaskGreen();
562 int b_mask = image.GetMaskBlue();
563
564 unsigned char* data = image.GetData();
565
566 int index = 0;
567 for (int y = 0; y < height; y++)
568 {
569 for (int x = 0; x < width; x++)
570 {
571 int r = data[index];
572 index++;
573 int g = data[index];
574 index++;
575 int b = data[index];
576 index++;
577
578 if (mask_image != NULL)
579 {
580 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
581 gdk_image_put_pixel( mask_image, x, y, 0 );
582 }
583
584 if ((r == 255) && (b == 255) && (g == 255))
585 gdk_image_put_pixel( data_image, x, y, 0 );
586
587 } // for
588 } // for
589
590 // Blit picture
591
592 gdk_draw_image( GetPixmap(), data_gc, data_image, 0, 0, 0, 0, width, height );
593
594 g_object_unref (data_image);
595 g_object_unref (data_gc);
596
597 // Blit mask
598
599 if (mask_image != NULL)
600 {
601 gdk_draw_image( GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height );
602
603 g_object_unref (mask_image);
604 g_object_unref (mask_gc);
605 }
606
607 return true;
608 }
609
610 // conversion to colour bitmap:
611 bool wxBitmap::CreateFromImageAsPixmap(const wxImage& image)
612 {
613 // alpha is handled by CreateFromImageAsPixbuf
614 wxASSERT(!image.HasAlpha());
615
616 int width = image.GetWidth();
617 int height = image.GetHeight();
618
619 SetPixmap( gdk_pixmap_new( wxGetRootWindow()->window, width, height, -1 ) );
620
621 GdkGC *gc = gdk_gc_new( GetPixmap() );
622
623 gdk_draw_rgb_image( GetPixmap(),
624 gc,
625 0, 0,
626 width, height,
627 GDK_RGB_DITHER_NONE,
628 image.GetData(),
629 width*3 );
630
631 g_object_unref (gc);
632
633 // Create mask image
634
635 if (!image.HasMask())
636 return true;
637
638 wxMask* mask = new wxMask;
639 mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, width, height, 1 );
640 GdkGC* mask_gc = gdk_gc_new(mask->m_bitmap);
641 GdkColor color;
642 color.pixel = 1;
643 gdk_gc_set_foreground(mask_gc, &color);
644 gdk_draw_rectangle(mask->m_bitmap, mask_gc, true, 0, 0, width, height);
645 GdkImage* mask_image = gdk_drawable_get_image(mask->m_bitmap, 0, 0, width, height);
646
647 SetMask( mask );
648
649 int r_mask = image.GetMaskRed();
650 int g_mask = image.GetMaskGreen();
651 int b_mask = image.GetMaskBlue();
652
653 unsigned char* data = image.GetData();
654
655 int index = 0;
656 for (int y = 0; y < height; y++)
657 {
658 for (int x = 0; x < width; x++)
659 {
660 int r = data[index];
661 index++;
662 int g = data[index];
663 index++;
664 int b = data[index];
665 index++;
666
667 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
668 gdk_image_put_pixel( mask_image, x, y, 0 );
669 } // for
670 } // for
671
672 // Blit mask
673
674 gdk_draw_image( GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height );
675
676 g_object_unref (mask_image);
677 g_object_unref (mask_gc);
678
679 return true;
680 }
681
682 bool wxBitmap::CreateFromImageAsPixbuf(const wxImage& image)
683 {
684 int width = image.GetWidth();
685 int height = image.GetHeight();
686
687 GdkPixbuf *pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB,
688 image.HasAlpha(),
689 8 /* bits per sample */,
690 width, height);
691 if (!pixbuf)
692 return false;
693
694 wxASSERT( image.HasAlpha() ); // for now
695 wxASSERT( gdk_pixbuf_get_n_channels(pixbuf) == 4 );
696 wxASSERT( gdk_pixbuf_get_width(pixbuf) == width );
697 wxASSERT( gdk_pixbuf_get_height(pixbuf) == height );
698
699 SetDepth(wxTheApp->GetGdkVisual()->depth);
700 SetPixbuf(pixbuf);
701
702 // Copy the data:
703 unsigned char *in = image.GetData();
704 unsigned char *out = gdk_pixbuf_get_pixels(pixbuf);
705 unsigned char *alpha = image.GetAlpha();
706
707 int rowinc = gdk_pixbuf_get_rowstride(pixbuf) - 4 * width;
708
709 for (int y = 0; y < height; y++, out += rowinc)
710 {
711 for (int x = 0; x < width; x++, alpha++, out += 4, in += 3)
712 {
713 out[0] = in[0];
714 out[1] = in[1];
715 out[2] = in[2];
716 out[3] = *alpha;
717 }
718 }
719
720 return true;
721 }
722
723 wxImage wxBitmap::ConvertToImage() const
724 {
725 wxImage image;
726
727 wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
728
729 const int w = GetWidth();
730 const int h = GetHeight();
731 image.Create(w, h);
732 unsigned char *data = image.GetData();
733
734 wxCHECK_MSG(data != NULL, wxNullImage, wxT("couldn't create image") );
735
736 if (HasPixbuf())
737 {
738 GdkPixbuf *pixbuf = GetPixbuf();
739 wxASSERT( gdk_pixbuf_get_has_alpha(pixbuf) );
740
741 image.SetAlpha();
742
743 unsigned char *alpha = image.GetAlpha();
744 unsigned char *in = gdk_pixbuf_get_pixels(pixbuf);
745 unsigned char *out = data;
746 int rowinc = gdk_pixbuf_get_rowstride(pixbuf) - 4 * w;
747
748 for (int y = 0; y < h; y++, in += rowinc)
749 {
750 for (int x = 0; x < w; x++, in += 4, out += 3, alpha++)
751 {
752 out[0] = in[0];
753 out[1] = in[1];
754 out[2] = in[2];
755 *alpha = in[3];
756 }
757 }
758 }
759 else
760 {
761 GdkPixmap* pixmap = GetPixmap();
762 GdkPixmap* pixmap_invert = NULL;
763 if (GetDepth() == 1)
764 {
765 // mono bitmaps are inverted
766 pixmap_invert = gdk_pixmap_new(pixmap, w, h, 1);
767 GdkGC* gc = gdk_gc_new(pixmap_invert);
768 gdk_gc_set_function(gc, GDK_COPY_INVERT);
769 gdk_draw_drawable(pixmap_invert, gc, pixmap, 0, 0, 0, 0, w, h);
770 g_object_unref(gc);
771 pixmap = pixmap_invert;
772 }
773 // create a pixbuf which shares data with the wxImage
774 GdkPixbuf* pixbuf = gdk_pixbuf_new_from_data(
775 data, GDK_COLORSPACE_RGB, false, 8, w, h, 3 * w, NULL, NULL);
776
777 gdk_pixbuf_get_from_drawable(pixbuf, pixmap, NULL, 0, 0, 0, 0, w, h);
778
779 g_object_unref(pixbuf);
780 if (pixmap_invert != NULL)
781 g_object_unref(pixmap_invert);
782
783 if (GetMask())
784 {
785 // the colour used as transparent one in wxImage and the one it is
786 // replaced with when it really occurs in the bitmap
787 const int MASK_RED = 1;
788 const int MASK_GREEN = 2;
789 const int MASK_BLUE = 3;
790 const int MASK_BLUE_REPLACEMENT = 2;
791
792 image.SetMaskColour(MASK_RED, MASK_GREEN, MASK_BLUE);
793 GdkImage* image_mask = gdk_drawable_get_image(GetMask()->GetBitmap(), 0, 0, w, h);
794
795 for (int y = 0; y < h; y++)
796 {
797 for (int x = 0; x < w; x++, data += 3)
798 {
799 if (gdk_image_get_pixel(image_mask, x, y) == 0)
800 {
801 data[0] = MASK_RED;
802 data[1] = MASK_GREEN;
803 data[2] = MASK_BLUE;
804 }
805 else if (data[0] == MASK_RED && data[1] == MASK_GREEN && data[2] == MASK_BLUE)
806 {
807 data[2] = MASK_BLUE_REPLACEMENT;
808 }
809 }
810 }
811 g_object_unref(image_mask);
812 }
813 }
814
815 return image;
816 }
817
818 wxBitmap::wxBitmap( const wxString &filename, wxBitmapType type )
819 {
820 LoadFile( filename, type );
821 }
822
823 wxBitmap::wxBitmap( const char bits[], int width, int height, int WXUNUSED(depth))
824 {
825 if ( width > 0 && height > 0 )
826 {
827 SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window, bits, width, height));
828
829 wxASSERT_MSG( M_BMPDATA->m_pixmap, wxT("couldn't create bitmap") );
830 }
831 }
832
833 wxBitmap::~wxBitmap()
834 {
835 }
836
837 bool wxBitmap::operator == ( const wxBitmap& bmp ) const
838 {
839 return m_refData == bmp.m_refData;
840 }
841
842 bool wxBitmap::operator != ( const wxBitmap& bmp ) const
843 {
844 return m_refData != bmp.m_refData;
845 }
846
847 bool wxBitmap::Ok() const
848 {
849 return (m_refData != NULL) &&
850 (
851 M_BMPDATA->m_pixbuf ||
852 M_BMPDATA->m_pixmap
853 );
854 }
855
856 int wxBitmap::GetHeight() const
857 {
858 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
859
860 return M_BMPDATA->m_height;
861 }
862
863 int wxBitmap::GetWidth() const
864 {
865 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
866
867 return M_BMPDATA->m_width;
868 }
869
870 int wxBitmap::GetDepth() const
871 {
872 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
873
874 return M_BMPDATA->m_bpp;
875 }
876
877 wxMask *wxBitmap::GetMask() const
878 {
879 wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") );
880
881 return M_BMPDATA->m_mask;
882 }
883
884 void wxBitmap::SetMask( wxMask *mask )
885 {
886 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
887
888 if (M_BMPDATA->m_mask) delete M_BMPDATA->m_mask;
889
890 M_BMPDATA->m_mask = mask;
891 }
892
893 bool wxBitmap::CopyFromIcon(const wxIcon& icon)
894 {
895 *this = icon;
896 return Ok();
897 }
898
899 wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const
900 {
901 wxBitmap ret;
902
903 wxCHECK_MSG( Ok() &&
904 (rect.x >= 0) && (rect.y >= 0) &&
905 (rect.x+rect.width <= M_BMPDATA->m_width) && (rect.y+rect.height <= M_BMPDATA->m_height),
906 ret, wxT("invalid bitmap or bitmap region") );
907
908 if (HasPixbuf())
909 {
910 GdkPixbuf *pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB,
911 gdk_pixbuf_get_has_alpha(GetPixbuf()),
912 8, rect.width, rect.height);
913 ret.SetPixbuf(pixbuf);
914 ret.SetDepth(M_BMPDATA->m_bpp);
915 gdk_pixbuf_copy_area(GetPixbuf(),
916 rect.x, rect.y, rect.width, rect.height,
917 pixbuf, 0, 0);
918 }
919 else
920 {
921 ret = wxBitmap(rect.width, rect.height, M_BMPDATA->m_bpp);
922 if (M_BMPDATA->m_bpp != 1)
923 {
924 GdkGC *gc = gdk_gc_new( ret.GetPixmap() );
925 gdk_draw_drawable( ret.GetPixmap(), gc, GetPixmap(), rect.x, rect.y, 0, 0, rect.width, rect.height );
926 g_object_unref (gc);
927 }
928 else
929 {
930 GdkGC *gc = gdk_gc_new( ret.GetPixmap() );
931 GdkColor col;
932 col.pixel = 0xFFFFFF;
933 gdk_gc_set_foreground( gc, &col );
934 col.pixel = 0;
935 gdk_gc_set_background( gc, &col );
936 gdk_wx_draw_bitmap( ret.GetPixmap(), gc, GetPixmap(), rect.x, rect.y, 0, 0, rect.width, rect.height );
937 g_object_unref (gc);
938 }
939 }
940
941 if (GetMask())
942 {
943 wxMask *mask = new wxMask;
944 mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, rect.width, rect.height, 1 );
945
946 GdkGC *gc = gdk_gc_new( mask->m_bitmap );
947 GdkColor col;
948 col.pixel = 0xFFFFFF;
949 gdk_gc_set_foreground( gc, &col );
950 col.pixel = 0;
951 gdk_gc_set_background( gc, &col );
952 gdk_wx_draw_bitmap( mask->m_bitmap, gc, M_BMPDATA->m_mask->m_bitmap, rect.x, rect.y, 0, 0, rect.width, rect.height );
953 g_object_unref (gc);
954
955 ret.SetMask( mask );
956 }
957
958 return ret;
959 }
960
961 bool wxBitmap::SaveFile( const wxString &name, wxBitmapType type, const wxPalette *WXUNUSED(palette) ) const
962 {
963 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
964
965 // Try to save the bitmap via wxImage handlers:
966 wxImage image = ConvertToImage();
967 return image.Ok() && image.SaveFile(name, type);
968 }
969
970 bool wxBitmap::LoadFile( const wxString &name, wxBitmapType type )
971 {
972 UnRef();
973
974 if (type == wxBITMAP_TYPE_XPM)
975 {
976 GdkBitmap *mask = (GdkBitmap*) NULL;
977 SetPixmap(gdk_pixmap_create_from_xpm(wxGetRootWindow()->window, &mask, NULL, name.fn_str()));
978
979 if (mask)
980 {
981 M_BMPDATA->m_mask = new wxMask;
982 M_BMPDATA->m_mask->m_bitmap = mask;
983 }
984 }
985 else // try if wxImage can load it
986 {
987 wxImage image;
988 if (image.LoadFile(name, type) && image.Ok())
989 *this = wxBitmap(image);
990 }
991
992 return Ok();
993 }
994
995 #if wxUSE_PALETTE
996 wxPalette *wxBitmap::GetPalette() const
997 {
998 if (!Ok())
999 return (wxPalette *) NULL;
1000
1001 return M_BMPDATA->m_palette;
1002 }
1003
1004 void wxBitmap::SetPalette(const wxPalette& WXUNUSED(palette))
1005 {
1006 // TODO
1007 }
1008 #endif // wxUSE_PALETTE
1009
1010 void wxBitmap::SetHeight( int height )
1011 {
1012 if (!m_refData)
1013 m_refData = new wxBitmapRefData;
1014
1015 M_BMPDATA->m_height = height;
1016 }
1017
1018 void wxBitmap::SetWidth( int width )
1019 {
1020 if (!m_refData)
1021 m_refData = new wxBitmapRefData;
1022
1023 M_BMPDATA->m_width = width;
1024 }
1025
1026 void wxBitmap::SetDepth( int depth )
1027 {
1028 if (!m_refData)
1029 m_refData = new wxBitmapRefData;
1030
1031 M_BMPDATA->m_bpp = depth;
1032 }
1033
1034 void wxBitmap::SetPixmap( GdkPixmap *pixmap )
1035 {
1036 if (!m_refData)
1037 m_refData = new wxBitmapRefData;
1038
1039 wxASSERT(M_BMPDATA->m_pixmap == NULL);
1040 M_BMPDATA->m_pixmap = pixmap;
1041 gdk_drawable_get_size(pixmap, &M_BMPDATA->m_width, &M_BMPDATA->m_height);
1042 M_BMPDATA->m_bpp = gdk_drawable_get_depth(pixmap);
1043 PurgeOtherRepresentations(Pixmap);
1044 }
1045
1046 GdkPixmap *wxBitmap::GetPixmap() const
1047 {
1048 wxCHECK_MSG( Ok(), (GdkPixmap *) NULL, wxT("invalid bitmap") );
1049
1050 // create the pixmap on the fly if we use Pixbuf representation:
1051 if (M_BMPDATA->m_pixmap == NULL)
1052 {
1053 delete M_BMPDATA->m_mask;
1054 M_BMPDATA->m_mask = new wxMask;
1055 gdk_pixbuf_render_pixmap_and_mask(M_BMPDATA->m_pixbuf,
1056 &M_BMPDATA->m_pixmap,
1057 &M_BMPDATA->m_mask->m_bitmap,
1058 128 /*threshold*/);
1059 }
1060
1061 return M_BMPDATA->m_pixmap;
1062 }
1063
1064 bool wxBitmap::HasPixmap() const
1065 {
1066 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1067
1068 return M_BMPDATA->m_pixmap != NULL;
1069 }
1070
1071 GdkPixbuf *wxBitmap::GetPixbuf() const
1072 {
1073 wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") );
1074
1075 if (M_BMPDATA->m_pixbuf == NULL)
1076 {
1077 int width = GetWidth();
1078 int height = GetHeight();
1079
1080 GdkPixbuf *pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB,
1081 GetMask() != NULL,
1082 8, width, height);
1083 M_BMPDATA->m_pixbuf =
1084 gdk_pixbuf_get_from_drawable(pixbuf, M_BMPDATA->m_pixmap, NULL,
1085 0, 0, 0, 0, width, height);
1086
1087 // apply the mask to created pixbuf:
1088 if (M_BMPDATA->m_pixbuf && M_BMPDATA->m_mask)
1089 {
1090 GdkPixbuf *pmask =
1091 gdk_pixbuf_get_from_drawable(NULL,
1092 M_BMPDATA->m_mask->GetBitmap(),
1093 NULL,
1094 0, 0, 0, 0, width, height);
1095 if (pmask)
1096 {
1097 guchar *bmp = gdk_pixbuf_get_pixels(pixbuf);
1098 guchar *mask = gdk_pixbuf_get_pixels(pmask);
1099 int bmprowinc = gdk_pixbuf_get_rowstride(pixbuf) - 4 * width;
1100 int maskrowinc = gdk_pixbuf_get_rowstride(pmask) - 3 * width;
1101
1102 for (int y = 0; y < height;
1103 y++, bmp += bmprowinc, mask += maskrowinc)
1104 {
1105 for (int x = 0; x < width; x++, bmp += 4, mask += 3)
1106 {
1107 if (mask[0] == 0 /*black pixel*/)
1108 bmp[3] = 0;
1109 }
1110 }
1111
1112 g_object_unref (pmask);
1113 }
1114 }
1115 }
1116
1117 return M_BMPDATA->m_pixbuf;
1118 }
1119
1120 bool wxBitmap::HasPixbuf() const
1121 {
1122 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
1123
1124 return M_BMPDATA->m_pixbuf != NULL;
1125 }
1126
1127 void wxBitmap::SetPixbuf( GdkPixbuf *pixbuf )
1128 {
1129 if (!m_refData)
1130 m_refData = new wxBitmapRefData;
1131
1132 wxASSERT(M_BMPDATA->m_pixbuf == NULL);
1133 M_BMPDATA->m_pixbuf = pixbuf;
1134 M_BMPDATA->m_width = gdk_pixbuf_get_width(pixbuf);
1135 M_BMPDATA->m_height = gdk_pixbuf_get_height(pixbuf);
1136 PurgeOtherRepresentations(Pixbuf);
1137 }
1138
1139 void wxBitmap::PurgeOtherRepresentations(wxBitmap::Representation keep)
1140 {
1141 if (keep == Pixmap && HasPixbuf())
1142 {
1143 g_object_unref (M_BMPDATA->m_pixbuf);
1144 M_BMPDATA->m_pixbuf = NULL;
1145 }
1146 if (keep == Pixbuf && HasPixmap())
1147 {
1148 g_object_unref (M_BMPDATA->m_pixmap);
1149 M_BMPDATA->m_pixmap = NULL;
1150 }
1151 }
1152
1153 void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp)
1154 {
1155 if (bpp != 32)
1156 return NULL;
1157
1158 GdkPixbuf *pixbuf = GetPixbuf();
1159 if (!pixbuf)
1160 return NULL;
1161
1162 #if 0
1163 if (gdk_pixbuf_get_has_alpha( pixbuf ))
1164 wxPrintf( wxT("Has alpha\n") );
1165 else
1166 wxPrintf( wxT("No alpha.\n") );
1167 #endif
1168
1169 data.m_height = gdk_pixbuf_get_height( pixbuf );
1170 data.m_width = gdk_pixbuf_get_width( pixbuf );
1171 data.m_stride = gdk_pixbuf_get_rowstride( pixbuf );
1172
1173 return gdk_pixbuf_get_pixels( pixbuf );
1174 }
1175
1176 void wxBitmap::UngetRawData(wxPixelDataBase& WXUNUSED(data))
1177 {
1178 }
1179
1180
1181 bool wxBitmap::HasAlpha() const
1182 {
1183 return HasPixbuf();
1184 }
1185
1186 void wxBitmap::UseAlpha()
1187 {
1188 GetPixbuf();
1189 }
1190
1191 //-----------------------------------------------------------------------------
1192 // wxBitmapHandler
1193 //-----------------------------------------------------------------------------
1194
1195 IMPLEMENT_DYNAMIC_CLASS(wxBitmapHandler,wxBitmapHandlerBase)
1196
1197 wxBitmapHandler::~wxBitmapHandler()
1198 {
1199 }
1200
1201 bool wxBitmapHandler::Create(wxBitmap * WXUNUSED(bitmap),
1202 void * WXUNUSED(data),
1203 long WXUNUSED(type),
1204 int WXUNUSED(width),
1205 int WXUNUSED(height),
1206 int WXUNUSED(depth))
1207 {
1208 wxFAIL_MSG( _T("not implemented") );
1209
1210 return false;
1211 }
1212
1213 bool wxBitmapHandler::LoadFile(wxBitmap * WXUNUSED(bitmap),
1214 const wxString& WXUNUSED(name),
1215 long WXUNUSED(flags),
1216 int WXUNUSED(desiredWidth),
1217 int WXUNUSED(desiredHeight))
1218 {
1219 wxFAIL_MSG( _T("not implemented") );
1220
1221 return false;
1222 }
1223
1224 bool wxBitmapHandler::SaveFile(const wxBitmap * WXUNUSED(bitmap),
1225 const wxString& WXUNUSED(name),
1226 int WXUNUSED(type),
1227 const wxPalette * WXUNUSED(palette))
1228 {
1229 wxFAIL_MSG( _T("not implemented") );
1230
1231 return false;
1232 }
1233
1234 /* static */ void wxBitmap::InitStandardHandlers()
1235 {
1236 // TODO: Insert handler based on GdkPixbufs handler later
1237 }