]> git.saurik.com Git - wxWidgets.git/blame_incremental - src/gtk/bitmap.cpp
replace the mismatched new[]/delete (bug #10234) with a wxVector
[wxWidgets.git] / src / gtk / bitmap.cpp
... / ...
CommitLineData
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
34extern GtkWidget *wxGetRootWindow();
35
36//-----------------------------------------------------------------------------
37// wxMask
38//-----------------------------------------------------------------------------
39
40IMPLEMENT_DYNAMIC_CLASS(wxMask,wxObject)
41
42wxMask::wxMask()
43{
44 m_bitmap = (GdkBitmap *) NULL;
45}
46
47wxMask::wxMask( const wxBitmap& bitmap, const wxColour& colour )
48{
49 m_bitmap = (GdkBitmap *) NULL;
50 Create( bitmap, colour );
51}
52
53#if wxUSE_PALETTE
54wxMask::wxMask( const wxBitmap& bitmap, int paletteIndex )
55{
56 m_bitmap = (GdkBitmap *) NULL;
57 Create( bitmap, paletteIndex );
58}
59#endif // wxUSE_PALETTE
60
61wxMask::wxMask( const wxBitmap& bitmap )
62{
63 m_bitmap = (GdkBitmap *) NULL;
64 Create( bitmap );
65}
66
67wxMask::~wxMask()
68{
69 if (m_bitmap)
70 g_object_unref (m_bitmap);
71}
72
73bool 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
140bool 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
153bool 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
176GdkBitmap *wxMask::GetBitmap() const
177{
178 return m_bitmap;
179}
180
181//-----------------------------------------------------------------------------
182// wxBitmap
183//-----------------------------------------------------------------------------
184
185class wxBitmapRefData: public wxGDIRefData
186{
187public:
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
204wxBitmapRefData::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
217wxBitmapRefData::~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
233IMPLEMENT_DYNAMIC_CLASS(wxBitmap,wxGDIObject)
234
235wxBitmap::wxBitmap(int width, int height, int depth)
236{
237 Create(width, height, depth);
238}
239
240wxBitmap::wxBitmap(const wxString &filename, wxBitmapType type)
241{
242 LoadFile(filename, type);
243}
244
245wxBitmap::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
256wxBitmap::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
270wxBitmap::~wxBitmap()
271{
272}
273
274bool 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 } else
289 if (depth == 24)
290 {
291 if (visual->depth == depth)
292 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window, width, height, depth));
293 else
294 SetPixbuf(gdk_pixbuf_new(GDK_COLORSPACE_RGB, false, 8, width, height), 24);
295 }
296 else
297 {
298 if (depth != 1)
299 {
300 if (depth == -1)
301 depth = visual->depth;
302 }
303 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window, width, height, depth));
304 }
305
306 return Ok();
307}
308
309wxBitmap wxBitmap::Rescale(int clipx, int clipy, int clipwidth, int clipheight,
310 int width, int height) const
311{
312 wxBitmap bmp;
313
314 wxCHECK_MSG(Ok(), bmp, wxT("invalid bitmap"));
315
316 width = wxMax(width, 1);
317 height = wxMax(height, 1);
318
319 const double scale_x = double(width) / clipwidth;
320 const double scale_y = double(height) / clipheight;
321
322 // Converting to pixbuf, scaling with gdk_pixbuf_scale, and converting
323 // back, is faster than scaling pixmap ourselves.
324
325 // use pixbuf if already available,
326 // otherwise create temporary pixbuf from pixmap
327 GdkPixbuf* pixbuf = M_BMPDATA->m_pixbuf;
328 if (pixbuf)
329 g_object_ref(pixbuf);
330 else
331 pixbuf = gdk_pixbuf_get_from_drawable(
332 NULL, M_BMPDATA->m_pixmap, NULL,
333 0, 0, 0, 0, M_BMPDATA->m_width, M_BMPDATA->m_height);
334
335 // new pixbuf for scaled wxBitmap
336 GdkPixbuf* pixbuf_scaled = gdk_pixbuf_new(
337 GDK_COLORSPACE_RGB, gdk_pixbuf_get_has_alpha(pixbuf), 8, width, height);
338
339 // GDK_INTERP_NEAREST is the lowest-quality method for continuous-tone
340 // images, but the only one which preserves sharp edges
341 gdk_pixbuf_scale(
342 pixbuf, pixbuf_scaled,
343 0, 0, width, height, -clipx*scale_x, -clipy*scale_y, scale_x, scale_y,
344 GDK_INTERP_NEAREST);
345
346 g_object_unref(pixbuf);
347 bmp.SetPixbuf(pixbuf_scaled, M_BMPDATA->m_bpp);
348
349 if (M_BMPDATA->m_mask)
350 {
351 pixbuf = gdk_pixbuf_get_from_drawable(
352 NULL, M_BMPDATA->m_mask->m_bitmap, NULL,
353 0, 0, 0, 0, M_BMPDATA->m_width, M_BMPDATA->m_height);
354
355 pixbuf_scaled = gdk_pixbuf_new(
356 GDK_COLORSPACE_RGB, false, 8, width, height);
357
358 gdk_pixbuf_scale(
359 pixbuf, pixbuf_scaled,
360 0, 0, width, height, -clipx, -clipy, scale_x, scale_y,
361 GDK_INTERP_NEAREST);
362
363 g_object_unref(pixbuf);
364
365 // use existing functionality to create mask from scaled pixbuf
366 wxBitmap maskbmp;
367 maskbmp.SetPixbuf(pixbuf_scaled);
368 bmp.SetMask(new wxMask(maskbmp, *wxBLACK));
369 }
370 return bmp;
371}
372
373#if wxUSE_IMAGE
374
375bool wxBitmap::CreateFromImage(const wxImage& image, int depth)
376{
377 UnRef();
378
379 wxCHECK_MSG( image.Ok(), false, wxT("invalid image") );
380 wxCHECK_MSG( depth == -1 || depth == 1, false, wxT("invalid bitmap depth") );
381
382 if (image.GetWidth() <= 0 || image.GetHeight() <= 0)
383 return false;
384
385 // create pixbuf if image has alpha and requested depth is compatible
386 if (image.HasAlpha() && (depth == -1 || depth == 32))
387 return CreateFromImageAsPixbuf(image);
388
389 // otherwise create pixmap, if alpha is present it will be converted to mask
390 return CreateFromImageAsPixmap(image, depth);
391}
392
393bool wxBitmap::CreateFromImageAsPixmap(const wxImage& image, int depth)
394{
395 const int w = image.GetWidth();
396 const int h = image.GetHeight();
397 if (depth == 1)
398 {
399 // create XBM format bitmap
400
401 // one bit per pixel, each row starts on a byte boundary
402 const size_t out_size = size_t((w + 7) / 8) * unsigned(h);
403 wxByte* out = new wxByte[out_size];
404 // set bits are black
405 memset(out, 0xff, out_size);
406 const wxByte* in = image.GetData();
407 unsigned bit_index = 0;
408 for (int y = 0; y < h; y++)
409 {
410 for (int x = 0; x < w; x++, in += 3, bit_index++)
411 if (in[0] == 255 && in[1] == 255 && in[2] == 255)
412 out[bit_index >> 3] ^= 1 << (bit_index & 7);
413 // move index to next byte boundary
414 bit_index = (bit_index + 7) & ~7u;
415 }
416 SetPixmap(gdk_bitmap_create_from_data(wxGetRootWindow()->window, (char*)out, w, h));
417 delete[] out;
418 }
419 else
420 {
421 SetPixmap(gdk_pixmap_new(wxGetRootWindow()->window, w, h, depth));
422 wxGtkObject<GdkGC> gc(gdk_gc_new(M_BMPDATA->m_pixmap));
423 gdk_draw_rgb_image(
424 M_BMPDATA->m_pixmap, gc,
425 0, 0, w, h,
426 GDK_RGB_DITHER_NONE, image.GetData(), w * 3);
427 }
428
429 const wxByte* alpha = image.GetAlpha();
430 if (alpha != NULL || image.HasMask())
431 {
432 // create mask as XBM format bitmap
433
434 const size_t out_size = size_t((w + 7) / 8) * unsigned(h);
435 wxByte* out = new wxByte[out_size];
436 memset(out, 0xff, out_size);
437 unsigned bit_index = 0;
438 if (alpha != NULL)
439 {
440 for (int y = 0; y < h; y++)
441 {
442 for (int x = 0; x < w; x++, bit_index++)
443 if (*alpha++ < wxIMAGE_ALPHA_THRESHOLD)
444 out[bit_index >> 3] ^= 1 << (bit_index & 7);
445 bit_index = (bit_index + 7) & ~7u;
446 }
447 }
448 else
449 {
450 const wxByte r_mask = image.GetMaskRed();
451 const wxByte g_mask = image.GetMaskGreen();
452 const wxByte b_mask = image.GetMaskBlue();
453 const wxByte* in = image.GetData();
454 for (int y = 0; y < h; y++)
455 {
456 for (int x = 0; x < w; x++, in += 3, bit_index++)
457 if (in[0] == r_mask && in[1] == g_mask && in[2] == b_mask)
458 out[bit_index >> 3] ^= 1 << (bit_index & 7);
459 bit_index = (bit_index + 7) & ~7u;
460 }
461 }
462 wxMask* mask = new wxMask;
463 mask->m_bitmap = gdk_bitmap_create_from_data(M_BMPDATA->m_pixmap, (char*)out, w, h);
464 SetMask(mask);
465 delete[] out;
466 }
467 return true;
468}
469
470bool wxBitmap::CreateFromImageAsPixbuf(const wxImage& image)
471{
472 wxASSERT(image.HasAlpha());
473
474 int width = image.GetWidth();
475 int height = image.GetHeight();
476
477 Create(width, height, 32);
478 GdkPixbuf* pixbuf = M_BMPDATA->m_pixbuf;
479 if (!pixbuf)
480 return false;
481
482 // Copy the data:
483 const unsigned char* in = image.GetData();
484 unsigned char *out = gdk_pixbuf_get_pixels(pixbuf);
485 unsigned char *alpha = image.GetAlpha();
486
487 int rowpad = gdk_pixbuf_get_rowstride(pixbuf) - 4 * width;
488
489 for (int y = 0; y < height; y++, out += rowpad)
490 {
491 for (int x = 0; x < width; x++, alpha++, out += 4, in += 3)
492 {
493 out[0] = in[0];
494 out[1] = in[1];
495 out[2] = in[2];
496 out[3] = *alpha;
497 }
498 }
499
500 return true;
501}
502
503wxImage wxBitmap::ConvertToImage() const
504{
505 wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
506
507 const int w = GetWidth();
508 const int h = GetHeight();
509 wxImage image(w, h, false);
510 unsigned char *data = image.GetData();
511
512 wxCHECK_MSG(data != NULL, wxNullImage, wxT("couldn't create image") );
513
514 // prefer pixbuf if available, it will preserve alpha and should be quicker
515 if (HasPixbuf())
516 {
517 GdkPixbuf *pixbuf = GetPixbuf();
518 unsigned char* alpha = NULL;
519 if (gdk_pixbuf_get_has_alpha(pixbuf))
520 {
521 image.SetAlpha();
522 alpha = image.GetAlpha();
523 }
524 const unsigned char* in = gdk_pixbuf_get_pixels(pixbuf);
525 unsigned char *out = data;
526 const int inc = 3 + int(alpha != NULL);
527 const int rowpad = gdk_pixbuf_get_rowstride(pixbuf) - inc * w;
528
529 for (int y = 0; y < h; y++, in += rowpad)
530 {
531 for (int x = 0; x < w; x++, in += inc, out += 3)
532 {
533 out[0] = in[0];
534 out[1] = in[1];
535 out[2] = in[2];
536 if (alpha != NULL)
537 *alpha++ = in[3];
538 }
539 }
540 }
541 else
542 {
543 GdkPixmap* pixmap = GetPixmap();
544 GdkPixmap* pixmap_invert = NULL;
545 if (GetDepth() == 1)
546 {
547 // mono bitmaps are inverted, i.e. 0 is white
548 pixmap_invert = gdk_pixmap_new(pixmap, w, h, 1);
549 wxGtkObject<GdkGC> gc(gdk_gc_new(pixmap_invert));
550 gdk_gc_set_function(gc, GDK_COPY_INVERT);
551 gdk_draw_drawable(pixmap_invert, gc, pixmap, 0, 0, 0, 0, w, h);
552 pixmap = pixmap_invert;
553 }
554 // create a pixbuf which shares data with the wxImage
555 GdkPixbuf* pixbuf = gdk_pixbuf_new_from_data(
556 data, GDK_COLORSPACE_RGB, false, 8, w, h, 3 * w, NULL, NULL);
557
558 gdk_pixbuf_get_from_drawable(pixbuf, pixmap, NULL, 0, 0, 0, 0, w, h);
559
560 g_object_unref(pixbuf);
561 if (pixmap_invert != NULL)
562 g_object_unref(pixmap_invert);
563 }
564 // convert mask, unless there is already alpha
565 if (GetMask() && !image.HasAlpha())
566 {
567 // we hard code the mask colour for now but we could also make an
568 // effort (and waste time) to choose a colour not present in the
569 // image already to avoid having to fudge the pixels below --
570 // whether it's worth to do it is unclear however
571 const int MASK_RED = 1;
572 const int MASK_GREEN = 2;
573 const int MASK_BLUE = 3;
574 const int MASK_BLUE_REPLACEMENT = 2;
575
576 image.SetMaskColour(MASK_RED, MASK_GREEN, MASK_BLUE);
577 GdkImage* image_mask = gdk_drawable_get_image(GetMask()->GetBitmap(), 0, 0, w, h);
578
579 for (int y = 0; y < h; y++)
580 {
581 for (int x = 0; x < w; x++, data += 3)
582 {
583 if (gdk_image_get_pixel(image_mask, x, y) == 0)
584 {
585 data[0] = MASK_RED;
586 data[1] = MASK_GREEN;
587 data[2] = MASK_BLUE;
588 }
589 else if (data[0] == MASK_RED && data[1] == MASK_GREEN && data[2] == MASK_BLUE)
590 {
591 // we have to fudge the colour a bit to prevent
592 // this pixel from appearing transparent
593 data[2] = MASK_BLUE_REPLACEMENT;
594 }
595 }
596 }
597 g_object_unref(image_mask);
598 }
599
600 return image;
601}
602
603#endif // wxUSE_IMAGE
604
605int wxBitmap::GetHeight() const
606{
607 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
608
609 return M_BMPDATA->m_height;
610}
611
612int wxBitmap::GetWidth() const
613{
614 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
615
616 return M_BMPDATA->m_width;
617}
618
619int wxBitmap::GetDepth() const
620{
621 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
622
623 return M_BMPDATA->m_bpp;
624}
625
626wxMask *wxBitmap::GetMask() const
627{
628 wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") );
629
630 return M_BMPDATA->m_mask;
631}
632
633void wxBitmap::SetMask( wxMask *mask )
634{
635 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
636
637 AllocExclusive();
638 delete M_BMPDATA->m_mask;
639 M_BMPDATA->m_mask = mask;
640}
641
642bool wxBitmap::CopyFromIcon(const wxIcon& icon)
643{
644 *this = icon;
645 return Ok();
646}
647
648wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const
649{
650 wxBitmap ret;
651
652 wxCHECK_MSG(Ok(), ret, wxT("invalid bitmap"));
653 wxCHECK_MSG(rect.x >= 0 && rect.y >= 0 &&
654 rect.x + rect.width <= M_BMPDATA->m_width &&
655 rect.y + rect.height <= M_BMPDATA->m_height,
656 ret, wxT("invalid bitmap region"));
657
658 if (HasPixbuf() || M_BMPDATA->m_bpp == 32)
659 {
660 GdkPixbuf *pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB,
661 gdk_pixbuf_get_has_alpha(GetPixbuf()),
662 8, rect.width, rect.height);
663 ret.SetPixbuf(pixbuf, M_BMPDATA->m_bpp);
664 gdk_pixbuf_copy_area(GetPixbuf(),
665 rect.x, rect.y, rect.width, rect.height,
666 pixbuf, 0, 0);
667 }
668 else
669 {
670 ret.Create(rect.width, rect.height, M_BMPDATA->m_bpp);
671 wxGtkObject<GdkGC> gc(gdk_gc_new( ret.GetPixmap() ));
672 gdk_draw_drawable( ret.GetPixmap(), gc, GetPixmap(), rect.x, rect.y, 0, 0, rect.width, rect.height );
673 }
674 // make mask, unless there is already alpha
675 if (GetMask() && !HasAlpha())
676 {
677 wxMask *mask = new wxMask;
678 mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, rect.width, rect.height, 1 );
679
680 wxGtkObject<GdkGC> gc(gdk_gc_new( mask->m_bitmap ));
681 gdk_draw_drawable(mask->m_bitmap, gc, M_BMPDATA->m_mask->m_bitmap, rect.x, rect.y, 0, 0, rect.width, rect.height);
682
683 ret.SetMask( mask );
684 }
685
686 return ret;
687}
688
689bool wxBitmap::SaveFile( const wxString &name, wxBitmapType type, const wxPalette *WXUNUSED(palette) ) const
690{
691 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
692
693#if wxUSE_IMAGE
694 // Try to save the bitmap via wxImage handlers:
695 wxImage image = ConvertToImage();
696 return image.Ok() && image.SaveFile(name, type);
697#else // !wxUSE_IMAGE
698 wxUnusedVar(name);
699 wxUnusedVar(type);
700
701 return false;
702#endif // wxUSE_IMAGE
703}
704
705bool wxBitmap::LoadFile( const wxString &name, wxBitmapType type )
706{
707 UnRef();
708
709 if (type == wxBITMAP_TYPE_XPM)
710 {
711 GdkBitmap *mask = (GdkBitmap*) NULL;
712 SetPixmap(gdk_pixmap_create_from_xpm(wxGetRootWindow()->window, &mask, NULL, name.fn_str()));
713
714 if (mask)
715 {
716 M_BMPDATA->m_mask = new wxMask;
717 M_BMPDATA->m_mask->m_bitmap = mask;
718 }
719 }
720#if wxUSE_IMAGE
721 else // try if wxImage can load it
722 {
723 wxImage image;
724 if (image.LoadFile(name, type) && image.Ok())
725 CreateFromImage(image, -1);
726 }
727#endif // wxUSE_IMAGE
728
729 return Ok();
730}
731
732#if wxUSE_PALETTE
733wxPalette *wxBitmap::GetPalette() const
734{
735 wxCHECK_MSG(Ok(), NULL, wxT("invalid bitmap"));
736
737 return M_BMPDATA->m_palette;
738}
739
740void wxBitmap::SetPalette(const wxPalette& WXUNUSED(palette))
741{
742 // TODO
743}
744#endif // wxUSE_PALETTE
745
746void wxBitmap::SetHeight( int height )
747{
748 AllocExclusive();
749 M_BMPDATA->m_height = height;
750}
751
752void wxBitmap::SetWidth( int width )
753{
754 AllocExclusive();
755 M_BMPDATA->m_width = width;
756}
757
758void wxBitmap::SetDepth( int depth )
759{
760 AllocExclusive();
761 M_BMPDATA->m_bpp = depth;
762}
763
764void wxBitmap::SetPixmap( GdkPixmap *pixmap )
765{
766 if (!m_refData)
767 m_refData = new wxBitmapRefData;
768
769 // AllocExclusive should not be needed for this internal function
770 wxASSERT(m_refData->GetRefCount() == 1);
771 wxASSERT(M_BMPDATA->m_pixmap == NULL);
772 M_BMPDATA->m_pixmap = pixmap;
773 gdk_drawable_get_size(pixmap, &M_BMPDATA->m_width, &M_BMPDATA->m_height);
774 M_BMPDATA->m_bpp = gdk_drawable_get_depth(pixmap);
775 PurgeOtherRepresentations(Pixmap);
776}
777
778GdkPixmap *wxBitmap::GetPixmap() const
779{
780 wxCHECK_MSG( Ok(), (GdkPixmap *) NULL, wxT("invalid bitmap") );
781
782 // create the pixmap on the fly if we use Pixbuf representation:
783 if (M_BMPDATA->m_pixmap == NULL)
784 {
785 GdkPixmap** pmask = NULL;
786 if (gdk_pixbuf_get_has_alpha(M_BMPDATA->m_pixbuf))
787 {
788 // make new mask from alpha
789 delete M_BMPDATA->m_mask;
790 M_BMPDATA->m_mask = new wxMask;
791 pmask = &M_BMPDATA->m_mask->m_bitmap;
792 }
793 gdk_pixbuf_render_pixmap_and_mask(M_BMPDATA->m_pixbuf,
794 &M_BMPDATA->m_pixmap,
795 pmask,
796 0x80 /* alpha threshold */);
797 }
798
799 return M_BMPDATA->m_pixmap;
800}
801
802bool wxBitmap::HasPixmap() const
803{
804 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
805
806 return M_BMPDATA->m_pixmap != NULL;
807}
808
809GdkPixbuf *wxBitmap::GetPixbuf() const
810{
811 wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") );
812
813 if (M_BMPDATA->m_pixbuf == NULL)
814 {
815
816 int width = GetWidth();
817 int height = GetHeight();
818
819 GdkPixbuf *pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB,
820 GetMask() != NULL,
821 8, width, height);
822 M_BMPDATA->m_pixbuf = pixbuf;
823 gdk_pixbuf_get_from_drawable(pixbuf, M_BMPDATA->m_pixmap, NULL,
824 0, 0, 0, 0, width, height);
825 // apply the mask to created pixbuf:
826 if (M_BMPDATA->m_pixbuf && M_BMPDATA->m_mask)
827 {
828 GdkPixbuf *pmask =
829 gdk_pixbuf_get_from_drawable(NULL,
830 M_BMPDATA->m_mask->GetBitmap(),
831 NULL,
832 0, 0, 0, 0, width, height);
833 if (pmask)
834 {
835 guchar *bmp = gdk_pixbuf_get_pixels(pixbuf);
836 guchar *mask = gdk_pixbuf_get_pixels(pmask);
837 int bmprowinc = gdk_pixbuf_get_rowstride(pixbuf) - 4 * width;
838 int maskrowinc = gdk_pixbuf_get_rowstride(pmask) - 3 * width;
839
840 for (int y = 0; y < height;
841 y++, bmp += bmprowinc, mask += maskrowinc)
842 {
843 for (int x = 0; x < width; x++, bmp += 4, mask += 3)
844 {
845 if (mask[0] == 0 /*black pixel*/)
846 bmp[3] = 0;
847 }
848 }
849
850 g_object_unref (pmask);
851 }
852 }
853 }
854
855 return M_BMPDATA->m_pixbuf;
856}
857
858bool wxBitmap::HasPixbuf() const
859{
860 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
861
862 return M_BMPDATA->m_pixbuf != NULL;
863}
864
865void wxBitmap::SetPixbuf(GdkPixbuf* pixbuf, int depth)
866{
867 if (!m_refData)
868 m_refData = new wxBitmapRefData;
869
870 // AllocExclusive should not be needed for this internal function
871 wxASSERT(m_refData->GetRefCount() == 1);
872 wxASSERT(M_BMPDATA->m_pixbuf == NULL);
873 M_BMPDATA->m_pixbuf = pixbuf;
874 M_BMPDATA->m_width = gdk_pixbuf_get_width(pixbuf);
875 M_BMPDATA->m_height = gdk_pixbuf_get_height(pixbuf);
876 // if depth specified
877 if (depth != 0)
878 M_BMPDATA->m_bpp = depth;
879 else if (M_BMPDATA->m_bpp == 0)
880 // use something reasonable
881 M_BMPDATA->m_bpp = wxTheApp->GetGdkVisual()->depth;
882 PurgeOtherRepresentations(Pixbuf);
883}
884
885void wxBitmap::PurgeOtherRepresentations(wxBitmap::Representation keep)
886{
887 if (keep == Pixmap && HasPixbuf())
888 {
889 g_object_unref (M_BMPDATA->m_pixbuf);
890 M_BMPDATA->m_pixbuf = NULL;
891 }
892 if (keep == Pixbuf && HasPixmap())
893 {
894 g_object_unref (M_BMPDATA->m_pixmap);
895 M_BMPDATA->m_pixmap = NULL;
896 }
897}
898
899void *wxBitmap::GetRawData(wxPixelDataBase& data, int bpp)
900{
901 void* bits = NULL;
902 GdkPixbuf *pixbuf = GetPixbuf();
903 const bool hasAlpha = HasAlpha();
904
905 // allow access if bpp is valid and matches existence of alpha
906 if ( pixbuf && ((bpp == 24 && !hasAlpha) || (bpp == 32 && hasAlpha)) )
907 {
908 data.m_height = gdk_pixbuf_get_height( pixbuf );
909 data.m_width = gdk_pixbuf_get_width( pixbuf );
910 data.m_stride = gdk_pixbuf_get_rowstride( pixbuf );
911 bits = gdk_pixbuf_get_pixels(pixbuf);
912 }
913 return bits;
914}
915
916void wxBitmap::UngetRawData(wxPixelDataBase& WXUNUSED(data))
917{
918}
919
920bool wxBitmap::HasAlpha() const
921{
922 return m_refData != NULL && M_BMPDATA->m_pixbuf != NULL &&
923 gdk_pixbuf_get_has_alpha(M_BMPDATA->m_pixbuf);
924}
925
926wxGDIRefData* wxBitmap::CreateGDIRefData() const
927{
928 return new wxBitmapRefData;
929}
930
931wxGDIRefData* wxBitmap::CloneGDIRefData(const wxGDIRefData* data) const
932{
933 const wxBitmapRefData* oldRef = static_cast<const wxBitmapRefData*>(data);
934 wxBitmapRefData* newRef = new wxBitmapRefData;
935 newRef->m_width = oldRef->m_width;
936 newRef->m_height = oldRef->m_height;
937 newRef->m_bpp = oldRef->m_bpp;
938 if (oldRef->m_pixmap != NULL)
939 {
940 newRef->m_pixmap = gdk_pixmap_new(
941 oldRef->m_pixmap, oldRef->m_width, oldRef->m_height,
942 // use pixmap depth, m_bpp may not match
943 gdk_drawable_get_depth(oldRef->m_pixmap));
944 wxGtkObject<GdkGC> gc(gdk_gc_new(newRef->m_pixmap));
945 gdk_draw_drawable(
946 newRef->m_pixmap, gc, oldRef->m_pixmap, 0, 0, 0, 0, -1, -1);
947 }
948 if (oldRef->m_pixbuf != NULL)
949 {
950 newRef->m_pixbuf = gdk_pixbuf_copy(oldRef->m_pixbuf);
951 }
952 if (oldRef->m_mask != NULL)
953 {
954 newRef->m_mask = new wxMask;
955 newRef->m_mask->m_bitmap = gdk_pixmap_new(
956 oldRef->m_mask->m_bitmap, oldRef->m_width, oldRef->m_height, 1);
957 wxGtkObject<GdkGC> gc(gdk_gc_new(newRef->m_mask->m_bitmap));
958 gdk_draw_drawable(newRef->m_mask->m_bitmap,
959 gc, oldRef->m_mask->m_bitmap, 0, 0, 0, 0, -1, -1);
960 }
961#if wxUSE_PALETTE
962 // implement this if SetPalette is ever implemented
963 wxASSERT(oldRef->m_palette == NULL);
964#endif
965
966 return newRef;
967}
968
969/* static */ void wxBitmap::InitStandardHandlers()
970{
971 // TODO: Insert handler based on GdkPixbufs handler later
972}