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