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