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