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