for file load/save, always try wxImage first, fallback to pixbuf
[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/icon.h"
17 #include "wx/image.h"
18 #include "wx/colour.h"
19 #endif
20
21 #include "wx/rawbmp.h"
22
23 #include "wx/gtk/private/object.h"
24
25 #include <gtk/gtk.h>
26
27 extern GtkWidget *wxGetRootWindow();
28
29 static void PixmapToPixbuf(GdkPixmap* pixmap, GdkPixbuf* pixbuf, int w, int h)
30 {
31 gdk_pixbuf_get_from_drawable(pixbuf, pixmap, NULL, 0, 0, 0, 0, w, h);
32 if (gdk_drawable_get_depth(pixmap) == 1)
33 {
34 // invert to match XBM convention
35 guchar* p = gdk_pixbuf_get_pixels(pixbuf);
36 const int inc = 3 + int(gdk_pixbuf_get_has_alpha(pixbuf) != 0);
37 const int rowpad = gdk_pixbuf_get_rowstride(pixbuf) - w * inc;
38 for (int y = h; y; y--, p += rowpad)
39 for (int x = w; x; x--, p += inc)
40 {
41 // pixels are either (0,0,0) or (0xff,0xff,0xff)
42 p[0] = ~p[0];
43 p[1] = ~p[1];
44 p[2] = ~p[2];
45 }
46 }
47 }
48
49 static void MaskToAlpha(GdkPixmap* mask, GdkPixbuf* pixbuf, int w, int h)
50 {
51 GdkPixbuf* mask_pixbuf = gdk_pixbuf_get_from_drawable(
52 NULL, mask, NULL, 0, 0, 0, 0, w, h);
53 guchar* p = gdk_pixbuf_get_pixels(pixbuf) + 3;
54 const guchar* mask_data = gdk_pixbuf_get_pixels(mask_pixbuf);
55 const int rowpad = gdk_pixbuf_get_rowstride(pixbuf) - w * 4;
56 const int mask_rowpad = gdk_pixbuf_get_rowstride(mask_pixbuf) - w * 3;
57 for (int y = h; y; y--, p += rowpad, mask_data += mask_rowpad)
58 {
59 for (int x = w; x; x--, p += 4, mask_data += 3)
60 {
61 *p = 255;
62 // no need to test all 3 components,
63 // pixels are either (0,0,0) or (0xff,0xff,0xff)
64 if (mask_data[0] == 0)
65 *p = 0;
66 }
67 }
68 g_object_unref(mask_pixbuf);
69 }
70
71 //-----------------------------------------------------------------------------
72 // wxMask
73 //-----------------------------------------------------------------------------
74
75 IMPLEMENT_DYNAMIC_CLASS(wxMask, wxMaskBase)
76
77 wxMask::wxMask()
78 {
79 m_bitmap = NULL;
80 }
81
82 wxMask::wxMask( const wxBitmap& bitmap, const wxColour& colour )
83 {
84 m_bitmap = NULL;
85 InitFromColour(bitmap, colour);
86 }
87
88 #if wxUSE_PALETTE
89 wxMask::wxMask( const wxBitmap& bitmap, int paletteIndex )
90 {
91 m_bitmap = NULL;
92 Create( bitmap, paletteIndex );
93 }
94 #endif // wxUSE_PALETTE
95
96 wxMask::wxMask( const wxBitmap& bitmap )
97 {
98 m_bitmap = NULL;
99 InitFromMonoBitmap(bitmap);
100 }
101
102 wxMask::~wxMask()
103 {
104 if (m_bitmap)
105 g_object_unref (m_bitmap);
106 }
107
108 void wxMask::FreeData()
109 {
110 if (m_bitmap)
111 {
112 g_object_unref (m_bitmap);
113 m_bitmap = NULL;
114 }
115 }
116
117 bool wxMask::InitFromColour(const wxBitmap& bitmap, const wxColour& colour)
118 {
119 const int w = bitmap.GetWidth();
120 const int h = bitmap.GetHeight();
121
122 // create mask as XBM format bitmap
123
124 // one bit per pixel, each row starts on a byte boundary
125 const size_t out_size = size_t((w + 7) / 8) * unsigned(h);
126 wxByte* out = new wxByte[out_size];
127 // set bits are unmasked
128 memset(out, 0xff, out_size);
129 unsigned bit_index = 0;
130 if (bitmap.HasPixbuf())
131 {
132 const wxByte r_mask = colour.Red();
133 const wxByte g_mask = colour.Green();
134 const wxByte b_mask = colour.Blue();
135 GdkPixbuf* pixbuf = bitmap.GetPixbuf();
136 const wxByte* in = gdk_pixbuf_get_pixels(pixbuf);
137 const int inc = 3 + int(gdk_pixbuf_get_has_alpha(pixbuf) != 0);
138 const int rowpadding = gdk_pixbuf_get_rowstride(pixbuf) - inc * w;
139 for (int y = 0; y < h; y++, in += rowpadding)
140 {
141 for (int x = 0; x < w; x++, in += inc, bit_index++)
142 if (in[0] == r_mask && in[1] == g_mask && in[2] == b_mask)
143 out[bit_index >> 3] ^= 1 << (bit_index & 7);
144 // move index to next byte boundary
145 bit_index = (bit_index + 7) & ~7u;
146 }
147 }
148 else
149 {
150 GdkImage* image = gdk_drawable_get_image(bitmap.GetPixmap(), 0, 0, w, h);
151 GdkColormap* colormap = gdk_image_get_colormap(image);
152 guint32 mask_pixel;
153 if (colormap == NULL)
154 // mono bitmap, white is pixel value 0
155 mask_pixel = guint32(colour.Red() != 255 || colour.Green() != 255 || colour.Blue() != 255);
156 else
157 {
158 wxColor c(colour);
159 c.CalcPixel(colormap);
160 mask_pixel = c.GetPixel();
161 }
162 for (int y = 0; y < h; y++)
163 {
164 for (int x = 0; x < w; x++, bit_index++)
165 if (gdk_image_get_pixel(image, x, y) == mask_pixel)
166 out[bit_index >> 3] ^= 1 << (bit_index & 7);
167 bit_index = (bit_index + 7) & ~7u;
168 }
169 g_object_unref(image);
170 }
171 m_bitmap = gdk_bitmap_create_from_data(wxGetRootWindow()->window, (char*)out, w, h);
172 delete[] out;
173 return true;
174 }
175
176 bool wxMask::InitFromMonoBitmap(const wxBitmap& bitmap)
177 {
178 if (!bitmap.IsOk()) return false;
179
180 wxCHECK_MSG( bitmap.GetDepth() == 1, false, wxT("Cannot create mask from colour bitmap") );
181
182 m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, bitmap.GetWidth(), bitmap.GetHeight(), 1 );
183
184 if (!m_bitmap) return false;
185
186 wxGtkObject<GdkGC> gc(gdk_gc_new( m_bitmap ));
187 gdk_gc_set_function(gc, GDK_COPY_INVERT);
188 gdk_draw_drawable(m_bitmap, gc, bitmap.GetPixmap(), 0, 0, 0, 0, bitmap.GetWidth(), bitmap.GetHeight());
189
190 return true;
191 }
192
193 GdkBitmap *wxMask::GetBitmap() const
194 {
195 return m_bitmap;
196 }
197
198 //-----------------------------------------------------------------------------
199 // wxBitmapRefData
200 //-----------------------------------------------------------------------------
201
202 class wxBitmapRefData: public wxGDIRefData
203 {
204 public:
205 wxBitmapRefData(int width, int height, int depth);
206 virtual ~wxBitmapRefData();
207
208 virtual bool IsOk() const;
209
210 GdkPixmap *m_pixmap;
211 GdkPixbuf *m_pixbuf;
212 wxMask *m_mask;
213 int m_width;
214 int m_height;
215 int m_bpp;
216 bool m_alphaRequested;
217 };
218
219 wxBitmapRefData::wxBitmapRefData(int width, int height, int depth)
220 {
221 m_pixmap = NULL;
222 m_pixbuf = NULL;
223 m_mask = NULL;
224 m_width = width;
225 m_height = height;
226 m_bpp = depth;
227 if (m_bpp < 0)
228 m_bpp = gdk_drawable_get_depth(wxGetRootWindow()->window);
229 m_alphaRequested = depth == 32;
230 }
231
232 wxBitmapRefData::~wxBitmapRefData()
233 {
234 if (m_pixmap)
235 g_object_unref (m_pixmap);
236 if (m_pixbuf)
237 g_object_unref (m_pixbuf);
238 delete m_mask;
239 }
240
241 bool wxBitmapRefData::IsOk() const
242 {
243 return m_bpp != 0;
244 }
245
246 //-----------------------------------------------------------------------------
247 // wxBitmap
248 //-----------------------------------------------------------------------------
249
250 #define M_BMPDATA static_cast<wxBitmapRefData*>(m_refData)
251
252 IMPLEMENT_DYNAMIC_CLASS(wxBitmap,wxGDIObject)
253
254 wxBitmap::wxBitmap(const wxString &filename, wxBitmapType type)
255 {
256 LoadFile(filename, type);
257 }
258
259 wxBitmap::wxBitmap(const char bits[], int width, int height, int depth)
260 {
261 wxASSERT(depth == 1);
262 if (width > 0 && height > 0 && depth == 1)
263 {
264 SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window, bits, width, height));
265 }
266 }
267
268 wxBitmap::wxBitmap(const char* const* bits)
269 {
270 wxCHECK2_MSG(bits != NULL, return, wxT("invalid bitmap data"));
271
272 GdkBitmap* mask = NULL;
273 SetPixmap(gdk_pixmap_create_from_xpm_d(wxGetRootWindow()->window, &mask, NULL, const_cast<char**>(bits)));
274 if (!M_BMPDATA)
275 return;
276
277 if (M_BMPDATA->m_pixmap != NULL && mask != NULL)
278 {
279 M_BMPDATA->m_mask = new wxMask;
280 M_BMPDATA->m_mask->m_bitmap = mask;
281 }
282 }
283
284 wxBitmap::~wxBitmap()
285 {
286 }
287
288 bool wxBitmap::Create( int width, int height, int depth )
289 {
290 UnRef();
291 wxCHECK_MSG(width >= 0 && height >= 0, false, "invalid bitmap size");
292 m_refData = new wxBitmapRefData(width, height, depth);
293 return true;
294 }
295
296 #if wxUSE_IMAGE
297
298 bool wxBitmap::CreateFromImage(const wxImage& image, int depth)
299 {
300 UnRef();
301
302 wxCHECK_MSG( image.IsOk(), false, wxT("invalid image") );
303 wxCHECK_MSG( depth == -1 || depth == 1, false, wxT("invalid bitmap depth") );
304
305 if (image.GetWidth() <= 0 || image.GetHeight() <= 0)
306 return false;
307
308 // create pixbuf if image has alpha and requested depth is compatible
309 if (image.HasAlpha() && (depth == -1 || depth == 32))
310 return CreateFromImageAsPixbuf(image);
311
312 // otherwise create pixmap, if alpha is present it will be converted to mask
313 return CreateFromImageAsPixmap(image, depth);
314 }
315
316 bool wxBitmap::CreateFromImageAsPixmap(const wxImage& image, int depth)
317 {
318 const int w = image.GetWidth();
319 const int h = image.GetHeight();
320 if (depth == 1)
321 {
322 // create XBM format bitmap
323
324 // one bit per pixel, each row starts on a byte boundary
325 const size_t out_size = size_t((w + 7) / 8) * unsigned(h);
326 wxByte* out = new wxByte[out_size];
327 // set bits are black
328 memset(out, 0xff, out_size);
329 const wxByte* in = image.GetData();
330 unsigned bit_index = 0;
331 for (int y = 0; y < h; y++)
332 {
333 for (int x = 0; x < w; x++, in += 3, bit_index++)
334 if (in[0] == 255 && in[1] == 255 && in[2] == 255)
335 out[bit_index >> 3] ^= 1 << (bit_index & 7);
336 // move index to next byte boundary
337 bit_index = (bit_index + 7) & ~7u;
338 }
339 SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window, (char*)out, w, h));
340 delete[] out;
341
342 if (!M_BMPDATA) // SetPixmap may have failed
343 return false;
344 }
345 else
346 {
347 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window, w, h, depth));
348 if (!M_BMPDATA)
349 return false;
350
351 wxGtkObject<GdkGC> gc(gdk_gc_new(M_BMPDATA->m_pixmap));
352 gdk_draw_rgb_image(
353 M_BMPDATA->m_pixmap, gc,
354 0, 0, w, h,
355 GDK_RGB_DITHER_NONE, image.GetData(), w * 3);
356 }
357
358 const wxByte* alpha = image.GetAlpha();
359 if (alpha != NULL || image.HasMask())
360 {
361 // create mask as XBM format bitmap
362
363 const size_t out_size = size_t((w + 7) / 8) * unsigned(h);
364 wxByte* out = new wxByte[out_size];
365 memset(out, 0xff, out_size);
366 unsigned bit_index = 0;
367 if (alpha != NULL)
368 {
369 for (int y = 0; y < h; y++)
370 {
371 for (int x = 0; x < w; x++, bit_index++)
372 if (*alpha++ < wxIMAGE_ALPHA_THRESHOLD)
373 out[bit_index >> 3] ^= 1 << (bit_index & 7);
374 bit_index = (bit_index + 7) & ~7u;
375 }
376 }
377 else
378 {
379 const wxByte r_mask = image.GetMaskRed();
380 const wxByte g_mask = image.GetMaskGreen();
381 const wxByte b_mask = image.GetMaskBlue();
382 const wxByte* in = image.GetData();
383 for (int y = 0; y < h; y++)
384 {
385 for (int x = 0; x < w; x++, in += 3, bit_index++)
386 if (in[0] == r_mask && in[1] == g_mask && in[2] == b_mask)
387 out[bit_index >> 3] ^= 1 << (bit_index & 7);
388 bit_index = (bit_index + 7) & ~7u;
389 }
390 }
391 wxMask* mask = new wxMask;
392 mask->m_bitmap = gdk_bitmap_create_from_data(M_BMPDATA->m_pixmap, (char*)out, w, h);
393 SetMask(mask);
394 delete[] out;
395 }
396 return IsOk();
397 }
398
399 bool wxBitmap::CreateFromImageAsPixbuf(const wxImage& image)
400 {
401 wxASSERT(image.HasAlpha());
402
403 int width = image.GetWidth();
404 int height = image.GetHeight();
405
406 Create(width, height, 32);
407 GdkPixbuf* pixbuf = GetPixbuf();
408 if (!pixbuf)
409 return false;
410
411 // Copy the data:
412 const unsigned char* in = image.GetData();
413 unsigned char *out = gdk_pixbuf_get_pixels(pixbuf);
414 unsigned char *alpha = image.GetAlpha();
415
416 int rowpad = gdk_pixbuf_get_rowstride(pixbuf) - 4 * width;
417
418 for (int y = 0; y < height; y++, out += rowpad)
419 {
420 for (int x = 0; x < width; x++, alpha++, out += 4, in += 3)
421 {
422 out[0] = in[0];
423 out[1] = in[1];
424 out[2] = in[2];
425 out[3] = *alpha;
426 }
427 }
428
429 return true;
430 }
431
432 wxImage wxBitmap::ConvertToImage() const
433 {
434 wxCHECK_MSG( IsOk(), wxNullImage, wxT("invalid bitmap") );
435
436 const int w = GetWidth();
437 const int h = GetHeight();
438 wxImage image(w, h, false);
439 unsigned char *data = image.GetData();
440
441 wxCHECK_MSG(data != NULL, wxNullImage, wxT("couldn't create image") );
442
443 // prefer pixbuf if available, it will preserve alpha and should be quicker
444 if (HasPixbuf())
445 {
446 GdkPixbuf *pixbuf = GetPixbuf();
447 unsigned char* alpha = NULL;
448 if (gdk_pixbuf_get_has_alpha(pixbuf))
449 {
450 image.SetAlpha();
451 alpha = image.GetAlpha();
452 }
453 const unsigned char* in = gdk_pixbuf_get_pixels(pixbuf);
454 unsigned char *out = data;
455 const int inc = 3 + int(alpha != NULL);
456 const int rowpad = gdk_pixbuf_get_rowstride(pixbuf) - inc * w;
457
458 for (int y = 0; y < h; y++, in += rowpad)
459 {
460 for (int x = 0; x < w; x++, in += inc, out += 3)
461 {
462 out[0] = in[0];
463 out[1] = in[1];
464 out[2] = in[2];
465 if (alpha != NULL)
466 *alpha++ = in[3];
467 }
468 }
469 }
470 else
471 {
472 GdkPixmap* pixmap = GetPixmap();
473 GdkPixmap* pixmap_invert = NULL;
474 if (GetDepth() == 1)
475 {
476 // mono bitmaps are inverted, i.e. 0 is white
477 pixmap_invert = gdk_pixmap_new(pixmap, w, h, 1);
478 wxGtkObject<GdkGC> gc(gdk_gc_new(pixmap_invert));
479 gdk_gc_set_function(gc, GDK_COPY_INVERT);
480 gdk_draw_drawable(pixmap_invert, gc, pixmap, 0, 0, 0, 0, w, h);
481 pixmap = pixmap_invert;
482 }
483 // create a pixbuf which shares data with the wxImage
484 GdkPixbuf* pixbuf = gdk_pixbuf_new_from_data(
485 data, GDK_COLORSPACE_RGB, false, 8, w, h, 3 * w, NULL, NULL);
486
487 gdk_pixbuf_get_from_drawable(pixbuf, pixmap, NULL, 0, 0, 0, 0, w, h);
488
489 g_object_unref(pixbuf);
490 if (pixmap_invert != NULL)
491 g_object_unref(pixmap_invert);
492 }
493 // convert mask, unless there is already alpha
494 if (GetMask() && !image.HasAlpha())
495 {
496 // we hard code the mask colour for now but we could also make an
497 // effort (and waste time) to choose a colour not present in the
498 // image already to avoid having to fudge the pixels below --
499 // whether it's worth to do it is unclear however
500 const int MASK_RED = 1;
501 const int MASK_GREEN = 2;
502 const int MASK_BLUE = 3;
503 const int MASK_BLUE_REPLACEMENT = 2;
504
505 image.SetMaskColour(MASK_RED, MASK_GREEN, MASK_BLUE);
506 GdkImage* image_mask = gdk_drawable_get_image(GetMask()->GetBitmap(), 0, 0, w, h);
507
508 for (int y = 0; y < h; y++)
509 {
510 for (int x = 0; x < w; x++, data += 3)
511 {
512 if (gdk_image_get_pixel(image_mask, x, y) == 0)
513 {
514 data[0] = MASK_RED;
515 data[1] = MASK_GREEN;
516 data[2] = MASK_BLUE;
517 }
518 else if (data[0] == MASK_RED && data[1] == MASK_GREEN && data[2] == MASK_BLUE)
519 {
520 // we have to fudge the colour a bit to prevent
521 // this pixel from appearing transparent
522 data[2] = MASK_BLUE_REPLACEMENT;
523 }
524 }
525 }
526 g_object_unref(image_mask);
527 }
528
529 return image;
530 }
531
532 #endif // wxUSE_IMAGE
533
534 int wxBitmap::GetHeight() const
535 {
536 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
537
538 return M_BMPDATA->m_height;
539 }
540
541 int wxBitmap::GetWidth() const
542 {
543 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
544
545 return M_BMPDATA->m_width;
546 }
547
548 int wxBitmap::GetDepth() const
549 {
550 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
551
552 return M_BMPDATA->m_bpp;
553 }
554
555 wxMask *wxBitmap::GetMask() const
556 {
557 wxCHECK_MSG( IsOk(), NULL, wxT("invalid bitmap") );
558
559 return M_BMPDATA->m_mask;
560 }
561
562 void wxBitmap::SetMask( wxMask *mask )
563 {
564 wxCHECK_RET( IsOk(), wxT("invalid bitmap") );
565
566 AllocExclusive();
567 delete M_BMPDATA->m_mask;
568 M_BMPDATA->m_mask = mask;
569 }
570
571 bool wxBitmap::CopyFromIcon(const wxIcon& icon)
572 {
573 *this = icon;
574 return IsOk();
575 }
576
577 wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const
578 {
579 wxBitmap ret;
580
581 wxCHECK_MSG(IsOk(), ret, wxT("invalid bitmap"));
582
583 const int w = rect.width;
584 const int h = rect.height;
585 const wxBitmapRefData* bmpData = M_BMPDATA;
586
587 wxCHECK_MSG(rect.x >= 0 && rect.y >= 0 &&
588 rect.x + w <= bmpData->m_width &&
589 rect.y + h <= bmpData->m_height,
590 ret, wxT("invalid bitmap region"));
591
592 wxBitmapRefData* newRef = new wxBitmapRefData(*bmpData);
593 ret.m_refData = newRef;
594 newRef->m_width = w;
595 newRef->m_height = h;
596
597 if (bmpData->m_pixbuf)
598 {
599 GdkPixbuf* pixbuf =
600 gdk_pixbuf_new_subpixbuf(bmpData->m_pixbuf, rect.x, rect.y, w, h);
601 newRef->m_pixbuf = gdk_pixbuf_copy(pixbuf);
602 g_object_unref(pixbuf);
603 }
604 if (bmpData->m_pixmap)
605 {
606 newRef->m_pixmap = gdk_pixmap_new(bmpData->m_pixmap, w, h, -1);
607 GdkGC* gc = gdk_gc_new(newRef->m_pixmap);
608 gdk_draw_drawable(
609 newRef->m_pixmap, gc, bmpData->m_pixmap, rect.x, rect.y, 0, 0, w, h);
610 g_object_unref(gc);
611 }
612 newRef->m_mask = NULL;
613 if (bmpData->m_mask && bmpData->m_mask->m_bitmap)
614 {
615 GdkPixmap* sub_mask = gdk_pixmap_new(bmpData->m_mask->m_bitmap, w, h, 1);
616 newRef->m_mask = new wxMask;
617 newRef->m_mask->m_bitmap = sub_mask;
618 GdkGC* gc = gdk_gc_new(sub_mask);
619 gdk_draw_drawable(
620 sub_mask, gc, bmpData->m_mask->m_bitmap, rect.x, rect.y, 0, 0, w, h);
621 g_object_unref(gc);
622 }
623
624 return ret;
625 }
626
627 bool wxBitmap::SaveFile( const wxString &name, wxBitmapType type, const wxPalette *WXUNUSED(palette) ) const
628 {
629 wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") );
630
631 #if wxUSE_IMAGE
632 wxImage image = ConvertToImage();
633 if (image.IsOk() && image.SaveFile(name, type))
634 return true;
635 #endif
636 const char* type_name = NULL;
637 switch (type)
638 {
639 case wxBITMAP_TYPE_BMP: type_name = "bmp"; break;
640 case wxBITMAP_TYPE_ICO: type_name = "ico"; break;
641 case wxBITMAP_TYPE_JPEG: type_name = "jpeg"; break;
642 case wxBITMAP_TYPE_PNG: type_name = "png"; break;
643 default: break;
644 }
645 return type_name &&
646 gdk_pixbuf_save(GetPixbuf(), name.fn_str(), type_name, NULL, NULL);
647 }
648
649 bool wxBitmap::LoadFile( const wxString &name, wxBitmapType type )
650 {
651 #if wxUSE_IMAGE
652 wxImage image;
653 if (image.LoadFile(name, type) && image.IsOk())
654 *this = wxBitmap(image);
655 else
656 #endif
657 {
658 UnRef();
659 GdkPixbuf* pixbuf = gdk_pixbuf_new_from_file(name.fn_str(), NULL);
660 if (pixbuf)
661 SetPixbuf(pixbuf);
662 }
663
664 return IsOk();
665 }
666
667 #if wxUSE_PALETTE
668 wxPalette *wxBitmap::GetPalette() const
669 {
670 return NULL;
671 }
672
673 void wxBitmap::SetPalette(const wxPalette& WXUNUSED(palette))
674 {
675 // TODO
676 }
677 #endif // wxUSE_PALETTE
678
679 void wxBitmap::SetHeight( int height )
680 {
681 AllocExclusive();
682 M_BMPDATA->m_height = height;
683 }
684
685 void wxBitmap::SetWidth( int width )
686 {
687 AllocExclusive();
688 M_BMPDATA->m_width = width;
689 }
690
691 void wxBitmap::SetDepth( int depth )
692 {
693 AllocExclusive();
694 M_BMPDATA->m_bpp = depth;
695 }
696
697 void wxBitmap::SetPixmap( GdkPixmap *pixmap )
698 {
699 UnRef();
700
701 if (!pixmap)
702 return;
703
704 int w, h;
705 gdk_drawable_get_size(pixmap, &w, &h);
706 wxBitmapRefData* bmpData = new wxBitmapRefData(w, h, 0);
707 m_refData = bmpData;
708 bmpData->m_pixmap = pixmap;
709 bmpData->m_bpp = gdk_drawable_get_depth(pixmap);
710 }
711
712 GdkPixmap *wxBitmap::GetPixmap() const
713 {
714 wxCHECK_MSG( IsOk(), NULL, wxT("invalid bitmap") );
715
716 wxBitmapRefData* bmpData = M_BMPDATA;
717 if (bmpData->m_pixmap)
718 return bmpData->m_pixmap;
719
720 if (bmpData->m_pixbuf)
721 {
722 GdkPixmap** mask_pixmap = NULL;
723 if (gdk_pixbuf_get_has_alpha(bmpData->m_pixbuf))
724 {
725 // make new mask from alpha
726 delete bmpData->m_mask;
727 bmpData->m_mask = new wxMask;
728 mask_pixmap = &bmpData->m_mask->m_bitmap;
729 }
730 gdk_pixbuf_render_pixmap_and_mask(
731 bmpData->m_pixbuf, &bmpData->m_pixmap, mask_pixmap, 128);
732 }
733 else
734 {
735 bmpData->m_pixmap = gdk_pixmap_new(wxGetRootWindow()->window,
736 bmpData->m_width, bmpData->m_height, bmpData->m_bpp == 1 ? 1 : -1);
737 }
738 return bmpData->m_pixmap;
739 }
740
741 bool wxBitmap::HasPixmap() const
742 {
743 wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") );
744
745 return M_BMPDATA->m_pixmap != NULL;
746 }
747
748 GdkPixbuf *wxBitmap::GetPixbuf() const
749 {
750 wxCHECK_MSG( IsOk(), NULL, wxT("invalid bitmap") );
751
752 wxBitmapRefData* bmpData = M_BMPDATA;
753 if (bmpData->m_pixbuf)
754 return bmpData->m_pixbuf;
755
756 const int w = bmpData->m_width;
757 const int h = bmpData->m_height;
758 GdkPixmap* mask = NULL;
759 if (bmpData->m_mask)
760 mask = bmpData->m_mask->m_bitmap;
761 const bool useAlpha = bmpData->m_alphaRequested || mask;
762 bmpData->m_pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, useAlpha, 8, w, h);
763 if (bmpData->m_pixmap)
764 PixmapToPixbuf(bmpData->m_pixmap, bmpData->m_pixbuf, w, h);
765 if (mask)
766 MaskToAlpha(mask, bmpData->m_pixbuf, w, h);
767 return bmpData->m_pixbuf;
768 }
769
770 bool wxBitmap::HasPixbuf() const
771 {
772 wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") );
773
774 return M_BMPDATA->m_pixbuf != NULL;
775 }
776
777 void wxBitmap::SetPixbuf(GdkPixbuf* pixbuf)
778 {
779 UnRef();
780
781 if (!pixbuf)
782 return;
783
784 int depth = -1;
785 if (gdk_pixbuf_get_has_alpha(pixbuf))
786 depth = 32;
787 m_refData = new wxBitmapRefData(
788 gdk_pixbuf_get_width(pixbuf), gdk_pixbuf_get_height(pixbuf), depth);
789
790 M_BMPDATA->m_pixbuf = pixbuf;
791 }
792
793 void wxBitmap::PurgeOtherRepresentations(wxBitmap::Representation keep)
794 {
795 if (keep == Pixmap && HasPixbuf())
796 {
797 g_object_unref (M_BMPDATA->m_pixbuf);
798 M_BMPDATA->m_pixbuf = NULL;
799 }
800 if (keep == Pixbuf && HasPixmap())
801 {
802 g_object_unref (M_BMPDATA->m_pixmap);
803 M_BMPDATA->m_pixmap = NULL;
804 }
805 }
806
807 #ifdef wxHAS_RAW_BITMAP
808 void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp)
809 {
810 void* bits = NULL;
811 GdkPixbuf *pixbuf = GetPixbuf();
812 const bool hasAlpha = HasAlpha();
813
814 // allow access if bpp is valid and matches existence of alpha
815 if ( pixbuf && ((bpp == 24 && !hasAlpha) || (bpp == 32 && hasAlpha)) )
816 {
817 data.m_height = gdk_pixbuf_get_height( pixbuf );
818 data.m_width = gdk_pixbuf_get_width( pixbuf );
819 data.m_stride = gdk_pixbuf_get_rowstride( pixbuf );
820 bits = gdk_pixbuf_get_pixels(pixbuf);
821 }
822 return bits;
823 }
824
825 void wxBitmap::UngetRawData(wxPixelDataBase& WXUNUSED(data))
826 {
827 }
828 #endif // wxHAS_RAW_BITMAP
829
830 bool wxBitmap::HasAlpha() const
831 {
832 const wxBitmapRefData* bmpData = M_BMPDATA;
833 return bmpData && (bmpData->m_alphaRequested ||
834 (bmpData->m_pixbuf && gdk_pixbuf_get_has_alpha(bmpData->m_pixbuf)));
835 }
836
837 wxGDIRefData* wxBitmap::CreateGDIRefData() const
838 {
839 return new wxBitmapRefData(0, 0, 0);
840 }
841
842 wxGDIRefData* wxBitmap::CloneGDIRefData(const wxGDIRefData* data) const
843 {
844 const wxBitmapRefData* oldRef = static_cast<const wxBitmapRefData*>(data);
845 wxBitmapRefData* newRef = new wxBitmapRefData(*oldRef);
846 if (oldRef->m_pixmap != NULL)
847 {
848 newRef->m_pixmap = gdk_pixmap_new(
849 oldRef->m_pixmap, oldRef->m_width, oldRef->m_height,
850 // use pixmap depth, m_bpp may not match
851 gdk_drawable_get_depth(oldRef->m_pixmap));
852 wxGtkObject<GdkGC> gc(gdk_gc_new(newRef->m_pixmap));
853 gdk_draw_drawable(
854 newRef->m_pixmap, gc, oldRef->m_pixmap, 0, 0, 0, 0, -1, -1);
855 }
856 if (oldRef->m_pixbuf != NULL)
857 {
858 newRef->m_pixbuf = gdk_pixbuf_copy(oldRef->m_pixbuf);
859 }
860 if (oldRef->m_mask != NULL)
861 {
862 newRef->m_mask = new wxMask;
863 newRef->m_mask->m_bitmap = gdk_pixmap_new(
864 oldRef->m_mask->m_bitmap, oldRef->m_width, oldRef->m_height, 1);
865 wxGtkObject<GdkGC> gc(gdk_gc_new(newRef->m_mask->m_bitmap));
866 gdk_draw_drawable(newRef->m_mask->m_bitmap,
867 gc, oldRef->m_mask->m_bitmap, 0, 0, 0, 0, -1, -1);
868 }
869
870 return newRef;
871 }
872
873 /* static */ void wxBitmap::InitStandardHandlers()
874 {
875 // TODO: Insert handler based on GdkPixbufs handler later
876 }